Add a python bindings sequence test
[babeltrace.git] / bindings / python / examples / python2 / syscalls_by_pid.py
CommitLineData
300b4c33 1#!/usr/bin/env python2
24a3136a 2# syscall_by_pid.py
300b4c33 3#
24a3136a 4# Babeltrace syscall by pid example script
300b4c33 5#
24a3136a 6# Copyright 2012 EfficiOS Inc.
300b4c33 7#
24a3136a 8# Author: Danny Serres <danny.serres@efficios.com>
300b4c33 9#
24a3136a
DS
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:
300b4c33 16#
24a3136a
DS
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
30import sys
31from babeltrace import *
32from output_format_modules.pprint_table import pprint_table as pprint
33
34if len(sys.argv) < 2 :
35 raise TypeError("Usage: python syscalls_by_pid.py path/to/trace")
36
37ctx = Context()
38ret = ctx.add_trace(sys.argv[1], "ctf")
39if ret is None:
40 raise IOError("Error adding trace")
41
42data = {}
43
44# Setting iterator
45bp = IterPos(SEEK_BEGIN)
46ctf_it = ctf.Iterator(ctx, bp)
47
48# Reading events
49event = ctf_it.read_event()
50while 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
75del ctf_it
76
77# Setting table for output
78table = []
79for item in data:
80 table.append([data[item], item]) # [count, pid]
81table.sort(reverse = True) # [big count first, pid]
82for i in range(len(table)):
83 table[i].reverse() # [pid, big count first]
84table.insert(0, ["PID", "SYSCALL COUNT"])
85pprint(table)
This page took 0.026412 seconds and 4 git commands to generate.