4 # Babeltrace example script with sched_switch events
6 # Copyright 2012 EfficiOS Inc.
8 # Author: Danny Serres <danny.serres@efficios.com>
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:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
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)
29 from babeltrace
import *
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:
38 traces
= TraceCollection()
39 ret
= traces
.add_trace(sys
.argv
[len(sys
.argv
)-1], "ctf")
41 raise IOError("Error adding trace")
43 for event
in traces
.events
:
45 if event
.get_name() == "sched_switch":
46 # Getting scope definition
47 sco
= event
.get_top_level_scope(CTFReader
.scope
.STREAM_EVENT_CONTEXT
)
49 print("ERROR: Cannot get definition scope for sched_switch")
53 pid_field
= event
.get_field_with_scope(sco
, "pid")
55 print("ERROR: Missing PID info for sched_switch")
57 pid
= pid_field
.get_value()
58 if usePID
and (pid
!= long(sys
.argv
[1])):
61 sco
= event
.get_top_level_scope(CTFReader
.scope
.EVENT_FIELDS
)
64 field
= event
.get_field_with_scope(sco
, "prev_comm")
66 print("ERROR: Missing prev_comm context info")
67 prev_comm
= field
.get_value()
70 field
= event
.get_field_with_scope(sco
, "prev_tid")
72 print("ERROR: Missing prev_tid context info")
73 prev_tid
= field
.get_value()
76 field
= event
.get_field_with_scope(sco
, "prev_prio")
78 print("ERROR: Missing prev_prio context info")
79 prev_prio
= field
.get_value()
82 field
= event
.get_field_with_scope(sco
, "prev_state")
84 print("ERROR: Missing prev_state context info")
85 prev_state
= field
.get_value()
88 field
= event
.get_field_with_scope(sco
, "next_comm")
90 print("ERROR: Missing next_comm context info")
91 next_comm
= field
.get_value()
94 field
= event
.get_field_with_scope(sco
, "next_tid")
96 print("ERROR: Missing next_tid context info")
97 next_tid
= field
.get_value()
100 field
= event
.get_field_with_scope(sco
, "next_prio")
102 print("ERROR: Missing next_prio context info")
103 next_prio
= field
.get_value()
106 print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
107 "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t"
108 "next_comm = {}, next_tid = {}, next_prio = {}".format(
109 pid
, event
.get_timestamp(), prev_comm
, prev_tid
,
110 prev_prio
, prev_state
, next_comm
, next_tid
, next_prio
))
This page took 0.034166 seconds and 5 git commands to generate.