-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_call_graph.py
More file actions
69 lines (54 loc) · 1.96 KB
/
Copy pathparse_call_graph.py
File metadata and controls
69 lines (54 loc) · 1.96 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
import sys
import re
def parse_graph(text):
lines = text.strip().split('\n')
# 1. Find total samples
total_samples = 0
for line in lines:
line = line.strip()
if line.startswith("Call graph:"):
continue
parts = line.split()
if not parts:
continue
if parts[0].isdigit() and "Thread_" in line:
total_samples += int(parts[0])
if total_samples == 0:
return "Could not determine total samples."
output = []
output.append(f"Total Samples: {total_samples}")
output.append("=" * 60)
# 2. Parse and format lines
for line in lines:
if line.strip() == "Call graph:" or not line.strip():
continue
# Count indents to maintain tree structure
# The structure looks like: " + ! 2229 system__soft_links__tasking__timed_delay_t ..."
# find where the number starts
m = re.search(r'\b\d+\b', line)
if not m:
output.append(line)
continue
start_idx = m.start()
prefix = line[:start_idx]
content = line[start_idx:]
parts = content.split()
if not parts:
continue
try:
samples = int(parts[0])
pct = (samples / total_samples) * 100
# Reconstruct the function name
func_name = " ".join(parts[1:])
# Format: [PREFIX] [PCT%] [SAMPLES] Function
pct_str = f"{pct:5.1f}%"
samp_str = f"({samples:4d})"
output.append(f"{prefix}{pct_str} {samp_str} {func_name}")
except ValueError:
# If it doesn't start with a number, just print it as is
output.append(line)
return "\n".join(output)
if __name__ == "__main__":
with open('graph.txt', 'r') as f:
text = f.read()
print(parse_graph(text))