-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_count.py
More file actions
58 lines (47 loc) · 2.03 KB
/
Copy pathfunction_count.py
File metadata and controls
58 lines (47 loc) · 2.03 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
import argparse
from collections import Counter
from time import sleep
from tracing.ftrace import function
class FunctionCount(object):
def __init__(self, pid_filter=False, function_filter=False, interval=1.0, limit=10):
self.ft = function.Function()
self.pid_filter = pid_filter
self.function_filter = function_filter
self.interval = interval
self.limit = limit
if function_filter:
self.ft.filter_function_name(function_filter)
if pid_filter:
self.ft.generic_filter_pid(pid_filter)
def cleanup(self):
self.ft.disable_tracing()
exit(0)
def count_functions(self):
functions = [l.split(":")[-1].split(" ")[1] for l in self.ft.get_trace_snapshot()]
function_counts = Counter(functions).most_common(self.limit)
for f in function_counts:
print "%s (%s)" % (f[0], f[1])
print
def trace(self):
print "Counting function calls every %s seconds\n" % (self.interval)
self.ft.enable_tracing()
while True:
try:
sleep(self.interval)
self.count_functions()
except KeyboardInterrupt:
self.cleanup()
self.cleanup()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--pid", action="store", dest="pid", required=False, default=False, help="Pid to filter")
parser.add_argument("-f", "--function", action="store", dest="function", required=False, default=False, help="Function to filter")
parser.add_argument("-i", "--interval", action="store", dest="interval", default=1.0, help="Sampling interval")
parser.add_argument("-l", "--limit", action="store", dest="limit", default=10, help="Number of functions to show counts for")
return parser.parse_args()
def main():
args = parse_args()
fc = FunctionCount(pid_filter=args.pid, function_filter=args.function, interval=float(args.interval), limit=int(args.limit))
fc.trace()
if __name__ == "__main__":
main()