Added Python interpreter version directives and reorganized example scripts
[babeltrace.git] / bindings / python / examples / python2 / syscalls_by_pid.py
1 #!/usr/bin/env python2
2 # syscall_by_pid.py
3 #
4 # Babeltrace syscall by pid example script
5 #
6 # Copyright 2012 EfficiOS Inc.
7 #
8 # Author: Danny Serres <danny.serres@efficios.com>
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19
20 # The script checks the number of events in the trace
21 # and outputs a table and a .svg histogram for the specified
22 # range (microseconds) or the total trace if no range specified.
23 # The graph is generated using the cairoplot module.
24
25 # The script checks all syscall in the trace and prints a list
26 # showing the number of systemcalls executed by each PID
27 # ordered from greatest to least number of syscalls.
28 # The trace needs PID context (lttng add-context -k -t pid)
29
30 import sys
31 from babeltrace import *
32 from output_format_modules.pprint_table import pprint_table as pprint
33
34 if len(sys.argv) < 2 :
35 raise TypeError("Usage: python syscalls_by_pid.py path/to/trace")
36
37 ctx = Context()
38 ret = ctx.add_trace(sys.argv[1], "ctf")
39 if ret is None:
40 raise IOError("Error adding trace")
41
42 data = {}
43
44 # Setting iterator
45 bp = IterPos(SEEK_BEGIN)
46 ctf_it = ctf.Iterator(ctx, bp)
47
48 # Reading events
49 event = ctf_it.read_event()
50 while event is not None:
51 if event.get_name().find("sys") >= 0:
52 # Getting scope definition
53 sco = event.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT)
54 if sco is None:
55 print("ERROR: Cannot get definition scope for {}".format(
56 event.get_name()))
57 else:
58 # Getting PID
59 pid_field = event.get_field(sco, "_pid")
60 pid = pid_field.get_int64()
61
62 if ctf.field_error():
63 print("ERROR: Missing PID info for sched_switch".format(
64 event.get_name()))
65 elif pid in data:
66 data[pid] += 1
67 else:
68 data[pid] = 1
69 # Next event
70 ret = ctf_it.next()
71 if ret < 0:
72 break
73 event = ctf_it.read_event()
74
75 del ctf_it
76
77 # Setting table for output
78 table = []
79 for item in data:
80 table.append([data[item], item]) # [count, pid]
81 table.sort(reverse = True) # [big count first, pid]
82 for i in range(len(table)):
83 table[i].reverse() # [pid, big count first]
84 table.insert(0, ["PID", "SYSCALL COUNT"])
85 pprint(table)
This page took 0.031281 seconds and 4 git commands to generate.