3 # Babeltrace example script with sched_switch events
5 # Copyright 2012 EfficiOS Inc.
7 # Author: Danny Serres <danny.serres@efficios.com>
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:
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
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)
28 from babeltrace
import *
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:
39 ret
= ctx
.add_trace(sys
.argv
[len(sys
.argv
)-1], "ctf")
41 raise IOError("Error adding trace")
44 bp
= IterPos(SEEK_BEGIN
)
45 ctf_it
= ctf
.Iterator(ctx
, bp
)
48 event
= ctf_it
.read_event()
49 while event
is not None:
51 if event
.get_name() == "sched_switch":
52 # Getting scope definition
53 sco
= event
.get_top_level_scope(ctf
.scope
.STREAM_EVENT_CONTEXT
)
55 print("ERROR: Cannot get definition scope for sched_switch")
59 pid_field
= event
.get_field(sco
, "_pid")
60 pid
= pid_field
.get_int64()
63 print("ERROR: Missing PID info for sched_switch")
66 if usePID
and (pid
!= long(sys
.argv
[1])):
69 sco
= event
.get_top_level_scope(ctf
.scope
.EVENT_FIELDS
)
72 field
= event
.get_field(sco
, "_prev_comm")
73 prev_comm
= field
.get_char_array()
75 print("ERROR: Missing prev_comm context info")
78 field
= event
.get_field(sco
, "_prev_tid")
79 prev_tid
= field
.get_int64()
81 print("ERROR: Missing prev_tid context info")
84 field
= event
.get_field(sco
, "_prev_prio")
85 prev_prio
= field
.get_int64()
87 print("ERROR: Missing prev_prio context info")
90 field
= event
.get_field(sco
, "_prev_state")
91 prev_state
= field
.get_int64()
93 print("ERROR: Missing prev_state context info")
96 field
= event
.get_field(sco
, "_next_comm")
97 next_comm
= field
.get_char_array()
99 print("ERROR: Missing next_comm context info")
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")
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")
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
))
126 event
= ctf_it
.read_event()
This page took 0.076857 seconds and 4 git commands to generate.