Commit | Line | Data |
---|---|---|
300b4c33 | 1 | #!/usr/bin/env python3 |
24a3136a | 2 | # sched_switch.py |
300b4c33 | 3 | # |
24a3136a | 4 | # Babeltrace example script with sched_switch events |
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. | |
5aa9939f JD |
19 | # |
20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | # SOFTWARE. | |
24a3136a DS |
27 | |
28 | # The script takes one optional argument (pid) | |
29 | # The script will read events based on pid and | |
30 | # print the scheduler switches happening with the process. | |
31 | # If no arguments are passed, it displays all the scheduler switches. | |
32 | # This can be used to understand which tasks schedule out the current | |
33 | # process being traced, and when it gets scheduled in again. | |
34 | # The trace needs PID context (lttng add-context -k -t pid) | |
35 | ||
36 | import sys | |
37 | from babeltrace import * | |
38 | ||
39 | if len(sys.argv) < 2 or len(sys.argv) > 3: | |
a47ab64a | 40 | raise TypeError("Usage: python sched_switch.py [pid] path/to/trace") |
24a3136a | 41 | elif len(sys.argv) == 3: |
a47ab64a | 42 | filterPID = True |
24a3136a | 43 | else: |
a47ab64a | 44 | filterPID = False |
24a3136a | 45 | |
74ea15ad | 46 | traces = TraceCollection() |
a47ab64a | 47 | ret = traces.add_trace(sys.argv[len(sys.argv) - 1], "ctf") |
24a3136a | 48 | if ret is None: |
a47ab64a | 49 | raise IOError("Error adding trace") |
24a3136a | 50 | |
74ea15ad | 51 | for event in traces.events: |
a47ab64a JG |
52 | if event.name != "sched_switch": |
53 | continue | |
78d714e8 | 54 | |
a47ab64a JG |
55 | # Getting PID |
56 | pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT) | |
57 | if pid is None: | |
58 | print("ERROR: Missing PID info for sched_switch") | |
59 | continue # Next event | |
24a3136a | 60 | |
a47ab64a JG |
61 | if filterPID and (pid != long(sys.argv[1])): |
62 | continue # Next event | |
24a3136a | 63 | |
a47ab64a JG |
64 | prev_comm = event["prev_comm"] |
65 | prev_tid = event["prev_tid"] | |
66 | prev_prio = event["prev_prio"] | |
67 | prev_state = event["prev_state"] | |
68 | next_comm = event["next_comm"] | |
69 | next_tid = event["next_tid"] | |
70 | next_prio = event["next_prio"] | |
24a3136a | 71 | |
a47ab64a JG |
72 | # Output |
73 | print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t" | |
74 | "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t" | |
75 | "next_comm = {}, next_tid = {}, next_prio = {}".format( | |
76 | pid, event.timestamp, prev_comm, prev_tid, | |
77 | prev_prio, prev_state, next_comm, next_tid, next_prio)) |