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