-
Notifications
You must be signed in to change notification settings - Fork 0
/
flameGraphFormatGenerator.py
118 lines (101 loc) · 4.19 KB
/
flameGraphFormatGenerator.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
import pandas as pd
import csv
import argparse
import os
import subprocess
flamegraphPlPath = "C:\\dev\\FlameGraph\\flamegraph.pl"
parser = argparse.ArgumentParser(description="A simple script to generate a file that can be used to create flamegraph")
parser.add_argument("input_file_name", type=str, help="Provide the name of the input file")
parser.add_argument('-o', "--output", type=str, help="Name of the output txt file")
parser.add_argument('-s', "--svg", type=str, help="Generate flamegraph vector image")
args = parser.parse_args()
input_file = args.input_file_name
output_csv_file = 'profile.csv'
with open(input_file, 'r', newline='') as infile, open(output_csv_file, 'w', newline='') as outfile:
csv_writer = csv.writer(outfile, delimiter=',')
firstLine = "VI Name VI Time Sub VIs Time Total Time # Runs Average Shortest Longest Diagram Display Draw Tracking Locals Avg Bytes Min Bytes Max Bytes Avg Blocks Min Blocks Max Blocks Project Library Application Instance"
data = firstLine.strip().split('\t')
csv_writer.writerow(data)
for line in infile:
data = line.strip().split('\t')
csv_writer.writerow(data)
data = pd.read_csv('./profile.csv')
viNameList = data['VI Name']
totalViTimeList = data['Total Time']
viTimeList = data["VI Time"]
totalViCnt = len(data['VI Name'])
graph = {}
viParentTimeMap = {}
viParentChildTimeMap = {}
totalParentTimeMap = {}
totalParentChildTimeMap = {}
vis = {}
i = 0
while i < totalViCnt:
parentName = viNameList[i].split(':')[-1]
totalParentTime = totalViTimeList[i]
viParentTime = viTimeList[i]
totalParentTimeMap[parentName] = totalParentTime
viParentTimeMap[parentName] = viParentTime
vis[parentName] = False
i += 1
child = []
while i < totalViCnt and viNameList[i].startswith('-->'):
childName = viNameList[i].split('-->')[-1].split(':')[-1]
vis[childName] = False
totalChildTime = totalViTimeList[i]
viChildTime = viTimeList[i]
if parentName not in totalParentChildTimeMap.keys():
totalParentChildTimeMap[parentName] = {}
if parentName not in viParentChildTimeMap.keys():
viParentChildTimeMap[parentName] = {}
totalParentChildTimeMap[parentName][childName] = totalChildTime
viParentChildTimeMap[parentName][childName] = viChildTime
child.append(childName)
graph[childName] = []
i += 1
graph[parentName] = child
i = 0
while i < totalViCnt:
parentName = viNameList[i].split(':')[-1]
i += 1
while i < totalViCnt and viNameList[i].startswith('-->'):
childName = viNameList[i].split('-->')[-1].split(':')[-1]
totalChildTime = totalViTimeList[i]
if childName not in totalParentTimeMap.keys():
totalParentTimeMap[childName] = totalChildTime
i += 1
def dfs(node:str, graph:map, vis:map, path:str, vec:list, totalParentTimeMap:map, totalParentChildTimeMap:map):
vis[node] = True
if len(graph[node]) == 0:
vec.append(path+';'+node+' '+str(totalParentTimeMap[node]))
return
vec.append(path+';'+node+' '+str(viParentTimeMap[node]))
for child in graph[node]:
if vis[child] == False and totalParentChildTimeMap[node][child] == totalParentTimeMap[child]:
dfs(child, graph, vis, path+';'+node, vec, totalParentTimeMap, totalParentChildTimeMap)
else:
# vec.append(path+';'+node+' '+str(viParentChildTimeMap[node][child]))
vec.append(path+';'+node+';'+child+' '+str(totalParentChildTimeMap[node][child]))
vis[node] = False
vec = []
dfs('Main.vi',graph , vis, "", vec, totalParentTimeMap, totalParentChildTimeMap)
output_file = 'a.txt'
if args.output is not None:
output_file = args.output
with open(output_file, "w") as f:
for line in vec:
f.write(line[1:])
f.write('\n')
os.remove(output_csv_file)
if args.svg is not None:
if not os.path.exists(flamegraphPlPath):
print("Please provide correct flamegraph.pl path")
else:
subprocess.run([
flamegraphPlPath,
"--title", "gRPC Time Analysis",
"--countname", "millisecond(s)",
"--nametype", "VI:",
output_file, ">", args.svg
], shell=True)