forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadstall_analyzer.py
executable file
·137 lines (110 loc) · 4.58 KB
/
threadstall_analyzer.py
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
#!/usr/bin/env python3
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0
"""
This script is used to analyze threadstalls. It will print out the most common
stack patterns and the most frequent interesting functions.
"""
import os
import re
from collections import Counter, defaultdict
from typing import List, Dict, Set
class ThreadDumpAnalyzer:
def __init__(self):
# Add interesting path prefixes as a class attribute
self.interesting_prefixes = [
'crates/',
'consensus/',
'sui-execution/',
'external-crates/',
]
def is_interesting(self, line: str) -> bool:
# Check if line starts with any of the interesting prefixes
return any(line.startswith(prefix) for prefix in self.interesting_prefixes)
def parse_dump_file(self, file_path: str) -> List[List[str]]:
stacks = []
current_stack = []
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if line.startswith('Thread'):
if current_stack:
stacks.append(current_stack)
current_stack = []
elif line and not line.startswith('---'):
if ' at ' in line:
func = line.split(' at ')[-1].strip()
current_stack.append(func)
if current_stack:
stacks.append(current_stack)
return stacks
def compress_stack(self, stack: List[str]) -> List[str]:
"""Compress stack to only interesting parts while maintaining context"""
compressed = []
last_was_skipped = False
for frame in stack:
if self.is_interesting(frame):
compressed.append(frame)
last_was_skipped = False
else:
if not last_was_skipped:
compressed.append("...") # Add ellipsis to show skipped frames
last_was_skipped = True
# Remove trailing ellipsis
if compressed and compressed[-1] == "...":
compressed.pop()
return compressed
def analyze_dumps(self, dump_dir: str) -> Dict:
all_dumps_analysis = {
'individual_dumps': [],
'common_patterns': defaultdict(int),
'frequent_functions': Counter()
}
dump_files = [f for f in os.listdir(dump_dir)]
for dump_file in sorted(dump_files):
file_path = os.path.join(dump_dir, dump_file)
stacks = self.parse_dump_file(file_path)
dump_analysis = {
'file': dump_file,
'compressed_stacks': []
}
for stack in stacks:
compressed = self.compress_stack(stack)
if compressed:
dump_analysis['compressed_stacks'].append(compressed)
# Track individual interesting functions
for frame in compressed:
if frame != "...":
all_dumps_analysis['frequent_functions'][frame] += 1
# Track stack patterns
stack_signature = ' -> '.join(compressed)
all_dumps_analysis['common_patterns'][stack_signature] += 1
all_dumps_analysis['individual_dumps'].append(dump_analysis)
return all_dumps_analysis
def print_analysis(self, analysis: Dict):
print("=== Individual Dump Analysis ===")
for dump in analysis['individual_dumps']:
print(f"\nFile: {dump['file']}")
print("Compressed stacks:")
for stack_num, stack in enumerate(dump['compressed_stacks'], 1):
print(f"\nStack {stack_num}:")
for frame in stack:
print(f" {frame}")
print("\n=== Most Common Stack Patterns ===")
for pattern, count in sorted(analysis['common_patterns'].items(), key=lambda x: x[1], reverse=True)[:10]:
print(f"\nOccurred {count} times:")
for frame in pattern.split(' -> '):
print(f" {frame}")
print("\n=== Most Frequent Functions ===")
for func, count in analysis['frequent_functions'].most_common(20):
print(f"{func}: {count} occurrences")
def main():
analyzer = ThreadDumpAnalyzer()
import sys
if len(sys.argv) != 2:
print("Usage: threadstall_analyzer.py <dump_directory>")
sys.exit(1)
analysis = analyzer.analyze_dumps(sys.argv[1])
analyzer.print_analysis(analysis)
if __name__ == "__main__":
main()