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