OILS / benchmarks / uftrace_plugin.py View on Github | oilshell.org

99 lines, 68 significant
1#!/usr/bin/env python2
2"""
3uftrace_plugin.py
4"""
5from __future__ import print_function
6
7import collections
8
9stack = []
10
11# call stack (list) -> count
12list_callers = collections.Counter()
13str_callers = collections.Counter()
14slice_callers = collections.Counter()
15t2_callers = collections.Counter()
16t3_callers = collections.Counter()
17t4_callers = collections.Counter()
18new_callers = collections.Counter()
19malloc_callers = collections.Counter()
20
21
22def uftrace_begin(ctx):
23 pass
24
25
26def uftrace_entry(ctx):
27 # TODO: When function is List::List, get the STACK
28 func = ctx["name"]
29
30 if func == 'List::List':
31 list_callers[stack[-1]] += 1
32 #print('STACK %s' % stack)
33 elif func == 'Str::Str':
34 str_callers[stack[-1]] += 1
35 elif func == 'Str::slice':
36 slice_callers[stack[-1]] += 1
37 elif func == 'Tuple2::Tuple2':
38 t2_callers[stack[-1]] += 1
39 elif func == 'Tuple3::Tuple3':
40 t3_callers[stack[-1]] += 1
41 elif func == 'Tuple4::Tuple4':
42 t4_callers[stack[-1]] += 1
43 elif func == 'operator new':
44 new_callers[stack[-1]] += 1
45 elif func == 'malloc':
46 malloc_callers[stack[-1]] += 1
47
48 stack.append(func)
49 #print("entry : " + func + "()")
50
51
52def uftrace_exit(ctx):
53 func = ctx["name"]
54 #print("exit : " + func + "()")
55 stack.pop()
56
57
58def PrintMostCommon(c, k):
59 total = sum(c.values())
60 for caller, count in c.most_common(k):
61 percent = count * 100.0 / total
62 print('%5.2f%% %5d %s' % (percent, count, caller))
63 print(' ...')
64 print(' %5d TOTAL' % total)
65
66
67def uftrace_end():
68 k = 10
69 print('')
70 print('List')
71 PrintMostCommon(list_callers, k)
72
73 print('')
74 print('Str')
75 PrintMostCommon(str_callers, k)
76
77 print('')
78 print('slice')
79 PrintMostCommon(slice_callers, k)
80
81 print('')
82 print('Tuple2')
83 PrintMostCommon(t2_callers, k)
84
85 print('')
86 print('Tuple3')
87 PrintMostCommon(t3_callers, k)
88
89 print('')
90 print('Tuple4')
91 PrintMostCommon(t4_callers, k)
92
93 print('')
94 print('operator new')
95 PrintMostCommon(new_callers, k)
96
97 print('')
98 print('malloc')
99 PrintMostCommon(malloc_callers, k)