6
6
7
7
from utils import FLAMEGRAPHS_DIR , get_git_root
8
8
9
- def get_stack_lines (metrics_dict , group_by_kvs , stack_keys , metric_name ):
9
+ def get_stack_lines (metrics_dict , group_by_kvs , stack_keys , metric_name , sum_metrics = None ):
10
10
"""
11
11
Filters a metrics_dict obtained from json for entries that look like:
12
12
[ { labels: [["key1", "span1;span2"], ["key2", "span3"]], "metric": metric_name, "value": 2 } ]
13
13
14
14
It will find entries that have all of stack_keys as present in the labels and then concatenate the corresponding values into a single flat stack entry and then add the value at the end.
15
15
It will write a file with one line each for flamegraph.pl or inferno-flamegraph to consume.
16
+ If sum_metrics is not None, instead of searching for metric_name, it will sum the values of the metrics in sum_metrics.
16
17
"""
17
18
lines = []
19
+ stack_sums = {}
20
+ non_zero = False
18
21
19
22
# Process counters
20
23
for counter in metrics_dict .get ('counter' , []):
21
- if counter ['metric' ] != metric_name :
24
+ if (sum_metrics is not None and counter ['metric' ] not in sum_metrics ) or \
25
+ (sum_metrics is None and counter ['metric' ] != metric_name ):
22
26
continue
27
+
23
28
# list of pairs -> dict
24
29
labels = dict (counter ['labels' ])
25
30
filter = False
@@ -41,15 +46,21 @@ def get_stack_lines(metrics_dict, group_by_kvs, stack_keys, metric_name):
41
46
42
47
stack = ';' .join (stack_values )
43
48
value = int (counter ['value' ])
49
+ stack_sums [stack ] = stack_sums .get (stack , 0 ) + value
50
+
51
+ if value != 0 :
52
+ non_zero = True
44
53
45
- lines . append ( f"{ stack } { value } " )
54
+ lines = [ f"{ stack } { value } " for stack , value in stack_sums . items () if value != 0 ]
46
55
47
56
# Currently cycle tracker does not use gauge
48
- return lines
57
+ return lines if non_zero else []
49
58
50
59
51
- def create_flamegraph (fname , metrics_dict , group_by_kvs , stack_keys , metric_name , reverse = False ):
52
- lines = get_stack_lines (metrics_dict , group_by_kvs , stack_keys , metric_name )
60
+ def create_flamegraph (fname , metrics_dict , group_by_kvs , stack_keys , metric_name , sum_metrics = None , reverse = False ):
61
+ lines = get_stack_lines (metrics_dict , group_by_kvs , stack_keys , metric_name , sum_metrics )
62
+ if not lines :
63
+ return
53
64
54
65
suffixes = [key for key in stack_keys if key != "cycle_tracker_span" ]
55
66
@@ -74,7 +85,7 @@ def create_flamegraph(fname, metrics_dict, group_by_kvs, stack_keys, metric_name
74
85
print (f"Created flamegraph at { flamegraph_path } " )
75
86
76
87
77
- def create_flamegraphs (metrics_file , group_by , stack_keys , metric_name , reverse = False ):
88
+ def create_flamegraphs (metrics_file , group_by , stack_keys , metric_name , sum_metrics = None , reverse = False ):
78
89
fname_prefix = os .path .splitext (os .path .basename (metrics_file ))[0 ]
79
90
80
91
with open (metrics_file , 'r' ) as f :
@@ -92,7 +103,7 @@ def create_flamegraphs(metrics_file, group_by, stack_keys, metric_name, reverse=
92
103
for group_by_values in group_by_values_list :
93
104
group_by_kvs = list (zip (group_by , group_by_values ))
94
105
fname = fname_prefix + '-' + '-' .join (group_by_values )
95
- create_flamegraph (fname , metrics_dict , group_by_kvs , stack_keys , metric_name , reverse = reverse )
106
+ create_flamegraph (fname , metrics_dict , group_by_kvs , stack_keys , metric_name , sum_metrics , reverse = reverse )
96
107
97
108
98
109
def create_custom_flamegraphs (metrics_file , group_by = ["group" ]):
@@ -101,6 +112,9 @@ def create_custom_flamegraphs(metrics_file, group_by=["group"]):
101
112
reverse = reverse )
102
113
create_flamegraphs (metrics_file , group_by , ["cycle_tracker_span" , "dsl_ir" , "opcode" , "air_name" ], "cells_used" ,
103
114
reverse = reverse )
115
+ create_flamegraphs (metrics_file , group_by , ["cell_tracker_span" ], "cells_used" ,
116
+ sum_metrics = ["simple_advice_cells" , "fixed_cells" , "lookup_advice_cells" ],
117
+ reverse = reverse )
104
118
105
119
106
120
def main ():
0 commit comments