-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackcollapse-jstack.py
More file actions
executable file
·174 lines (142 loc) · 5.35 KB
/
Copy pathstackcollapse-jstack.py
File metadata and controls
executable file
·174 lines (142 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python3
# The MIT License (MIT)
#
# Copyright (c) 2022 Gabriel Corona
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Alternative to https://github.com/brendangregg/FlameGraph/blob/master/stackcollapse-jstack.pl
#
# Differences:
# ============
#
# * Includes samples where the thread is in a waiting state.
# * Option to mark samples where the thread is in waiting state.
import sys
import re
from dataclasses import dataclass
import argparse
from typing import List, Optional
# eg. "main" #1 prio=5 os_prio=0 cpu=15,63ms elapsed=0,90s tid=0x00007f0f1c016dc0 nid=0x9dcd waiting on condition [0x00007f0f22727000]
THREAD_LINE = re.compile('^"([^"]+)" #([0-9]+) ')
# eg. "pool-12-thread-1" prio=5 Id=86 TIMED_WAITING
THREAD_LINE2 = re.compile('^"([^"]+)" prio=[0-9]+ Id=([0-9]+) ')
# eg. java.lang.Thread.State: TIMED_WAITING (sleeping)
THREAD_STATE_LINE = re.compile(r"^\s+java\.lang\.Thread\.State: ([A-Z_]+)")
# eg. at java.lang.Thread.sleep(java.base@17.0.2/Native Method)
AT_LINE = re.compile(r"^\s+at ([^\(]+)\(([^\)]+)")
INFO = re.compile(":([0-9]+)$")
@dataclass
class Sample:
tname: str
tid: int
stack: List[str]
# See https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Thread.State.html
state: str = ""
WAITING_STATES = ["WAITING", "TIMED_WAITING", "LOCKED"]
def main():
parser = argparse.ArgumentParser(
description="Generate flamegraph stacks from jstack/jcmd output",
allow_abbrev=False,
)
parser.add_argument(
"infiles", nargs="*", type=argparse.FileType("rt"), default=[sys.stdin]
)
parser.add_argument(
"--line-numbers", default=True, action=argparse.BooleanOptionalAction
)
parser.add_argument(
"--short-line-numbers", default=False, action=argparse.BooleanOptionalAction
)
parser.add_argument(
"--state",
dest="show_state",
default=True,
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"--streaming", default=True, action=argparse.BooleanOptionalAction
)
args = parser.parse_args()
line_numbers: bool = args.line_numbers
show_state: bool = args.show_state
short_line_numbers: bool = args.short_line_numbers
streaming: bool = args.streaming
stacks = {}
def commit(sample: Sample):
if not sample.stack:
return
if sample.state == "NEW" or sample.state == "TERMINATED":
return
thread = sample.tname + "#" + str(sample.tid)
suffix = ""
if show_state and sample.state:
suffix = ";" + sample.state
stack = thread + ";" + ";".join(sample.stack) + suffix
if streaming:
print(stack + " 1")
else:
stacks[stack] = stacks.get(stack, 0) + 1
sample: Optional[Sample] = None
print(args.infiles)
for infile in args.infiles:
for line in infile:
line = line.rstrip()
if not line:
if sample is not None:
commit(sample)
sample = None
continue
match = THREAD_LINE.match(line)
if match is None:
match = THREAD_LINE2.match(line)
if match is not None:
sample = Sample(
tname=match.group(1),
tid=int(match.group(2)),
stack=[],
)
continue
if sample is None:
continue
match = THREAD_STATE_LINE.match(line)
if match is not None:
sample.state = match.group(1)
continue
match = AT_LINE.match(line)
if match is not None:
code = match.group(1)
info = match.group(2)
match2 = INFO.search(info)
if match2 is not None and line_numbers:
line = match2.group(1)
if short_line_numbers:
sample.stack.insert(0, ":" + str(line))
else:
sample.stack.insert(0, code + ":" + str(line))
sample.stack.insert(0, code)
if sample is not None:
commit(sample)
sample = None
if not streaming:
for stack, count in stacks.items():
print(stack + " " + str(count))
if __name__ == "__main__":
main()