Add 6 tools tracking dbus.
[platform/upstream/bcc.git] / tools / dbus-tok.py
1 #!/usr/bin/python
2
3 from bcc import BPF
4 from time import sleep
5 from ctypes import c_ushort, c_int, c_ulonglong
6 from sys import argv
7
8 def usage():
9         print("[interval] [count]")
10         exit(0)
11
12 print("Hit Ctrl-C to end.")
13 count = 100
14 interval = 5
15 loop = 0
16 # load BPF program
17 b = BPF(src_file="dbus-tok.c")
18
19 b.attach_uprobe(name="dbus-1", sym="_dbus_poll", fn_name="do_entry")
20
21 b.attach_uretprobe(name="dbus-1", sym="_dbus_poll", fn_name="do_return")
22
23 while (1):
24         if count > 0:
25
26                 loop += 1
27                 if loop > count:
28                         exit()
29         sleep(interval)
30         print ("%d : \n" % loop)
31         stats_t = b["stats"]
32         timestamp = b["timestamp_e"]
33         timeframe = 0
34         for v in timestamp.values():
35                 timeframe = v.value
36         timestamp = b["timestamp_b"]
37         for v in timestamp.values():
38                 timeframe -= v.value
39         for v in stats_t.values():
40                 v.usage = (v.run * 100 / timeframe)
41         for v in sorted(stats_t.values(), key=lambda stats_t: stats_t.run, reverse=True):
42                 print("%10d %20s %10.6f" % (v.pid, v.buf.encode('string-escape'), float(v.run) / float(timeframe)))