Babeltrace python binding
[babeltrace.git] / bindings / python / examples / sched_switch.py
1 # sched_switch.py
2 #
3 # Babeltrace example script with sched_switch events
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 takes one optional argument (pid)
20 # The script will read events based on pid and
21 # print the scheduler switches happening with the process.
22 # If no arguments are passed, it displays all the scheduler switches.
23 # This can be used to understand which tasks schedule out the current
24 # process being traced, and when it gets scheduled in again.
25 # The trace needs PID context (lttng add-context -k -t pid)
26
27 import sys
28 from babeltrace import *
29
30 if len(sys.argv) < 2 or len(sys.argv) > 3:
31 raise TypeError("Usage: python sched_switch.py [pid] path/to/trace")
32 elif len(sys.argv) == 3:
33 usePID = True
34 else:
35 usePID = False
36
37
38 ctx = Context()
39 ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf")
40 if ret is None:
41 raise IOError("Error adding trace")
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 while True:
51 if event.get_name() == "sched_switch":
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 sched_switch")
56 break # Next event
57
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")
64 break # Next event
65
66 if usePID and (pid != long(sys.argv[1])):
67 break # Next event
68
69 sco = event.get_top_level_scope(ctf.scope.EVENT_FIELDS)
70
71 # prev_comm
72 field = event.get_field(sco, "_prev_comm")
73 prev_comm = field.get_char_array()
74 if ctf.field_error():
75 print("ERROR: Missing prev_comm context info")
76
77 # prev_tid
78 field = event.get_field(sco, "_prev_tid")
79 prev_tid = field.get_int64()
80 if ctf.field_error():
81 print("ERROR: Missing prev_tid context info")
82
83 # prev_prio
84 field = event.get_field(sco, "_prev_prio")
85 prev_prio = field.get_int64()
86 if ctf.field_error():
87 print("ERROR: Missing prev_prio context info")
88
89 # prev_state
90 field = event.get_field(sco, "_prev_state")
91 prev_state = field.get_int64()
92 if ctf.field_error():
93 print("ERROR: Missing prev_state context info")
94
95 # next_comm
96 field = event.get_field(sco, "_next_comm")
97 next_comm = field.get_char_array()
98 if ctf.field_error():
99 print("ERROR: Missing next_comm context info")
100
101 # next_tid
102 field = event.get_field(sco, "_next_tid")
103 next_tid = field.get_int64()
104 if ctf.field_error():
105 print("ERROR: Missing next_tid context info")
106
107 # next_prio
108 field = event.get_field(sco, "_next_prio")
109 next_prio = field.get_int64()
110 if ctf.field_error():
111 print("ERROR: Missing next_prio context info")
112
113 # Output
114 print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
115 "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t"
116 "next_comm = {}, next_tid = {}, next_prio = {}".format(
117 pid, event.get_timestamp(), prev_comm, prev_tid,
118 prev_prio, prev_state, next_comm, next_tid, next_prio))
119
120 break # Next event
121
122 # Next event
123 ret = ctf_it.next()
124 if ret < 0:
125 break
126 event = ctf_it.read_event()
127
128 del ctf_it
This page took 0.031023 seconds and 4 git commands to generate.