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. | |
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) | |
24d5c942 | 46 | ctf_it = CTFReader.Iterator(ctx, bp) |
24a3136a DS |
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 | |
24d5c942 | 54 | sco = event.get_top_level_scope(CTFReader.scope.STREAM_EVENT_CONTEXT) |
24a3136a DS |
55 | if sco is None: |
56 | print("ERROR: Cannot get definition scope for sched_switch") | |
57 | break # Next event | |
58 | ||
59 | # Getting PID | |
db551f28 | 60 | pid_field = event.get_field_with_scope(sco, "pid") |
3c2ce778 | 61 | if pid_field is None: |
24a3136a DS |
62 | print("ERROR: Missing PID info for sched_switch") |
63 | break # Next event | |
3c2ce778 | 64 | pid = pid_field.get_value() |
24a3136a DS |
65 | if usePID and (pid != long(sys.argv[1])): |
66 | break # Next event | |
67 | ||
24d5c942 | 68 | sco = event.get_top_level_scope(CTFReader.scope.EVENT_FIELDS) |
24a3136a DS |
69 | |
70 | # prev_comm | |
db551f28 | 71 | field = event.get_field_with_scope(sco, "prev_comm") |
3c2ce778 | 72 | if field is None: |
24a3136a | 73 | print("ERROR: Missing prev_comm context info") |
3c2ce778 | 74 | prev_comm = field.get_value() |
24a3136a DS |
75 | |
76 | # prev_tid | |
db551f28 | 77 | field = event.get_field_with_scope(sco, "prev_tid") |
3c2ce778 | 78 | if field is None: |
24a3136a | 79 | print("ERROR: Missing prev_tid context info") |
3c2ce778 | 80 | prev_tid = field.get_value() |
24a3136a DS |
81 | |
82 | # prev_prio | |
db551f28 | 83 | field = event.get_field_with_scope(sco, "prev_prio") |
3c2ce778 | 84 | if field is None: |
24a3136a | 85 | print("ERROR: Missing prev_prio context info") |
3c2ce778 | 86 | prev_prio = field.get_value() |
24a3136a DS |
87 | |
88 | # prev_state | |
db551f28 | 89 | field = event.get_field_with_scope(sco, "prev_state") |
3c2ce778 | 90 | if field is None: |
24a3136a | 91 | print("ERROR: Missing prev_state context info") |
3c2ce778 | 92 | prev_state = field.get_value() |
24a3136a DS |
93 | |
94 | # next_comm | |
db551f28 | 95 | field = event.get_field_with_scope(sco, "next_comm") |
3c2ce778 | 96 | if field is None: |
24a3136a | 97 | print("ERROR: Missing next_comm context info") |
3c2ce778 | 98 | next_comm = field.get_value() |
24a3136a DS |
99 | |
100 | # next_tid | |
db551f28 | 101 | field = event.get_field_with_scope(sco, "next_tid") |
3c2ce778 | 102 | if field is None: |
24a3136a | 103 | print("ERROR: Missing next_tid context info") |
3c2ce778 | 104 | next_tid = field.get_value() |
24a3136a DS |
105 | |
106 | # next_prio | |
db551f28 | 107 | field = event.get_field_with_scope(sco, "next_prio") |
3c2ce778 | 108 | if field is None: |
24a3136a | 109 | print("ERROR: Missing next_prio context info") |
3c2ce778 | 110 | next_prio = field.get_value() |
24a3136a DS |
111 | |
112 | # Output | |
113 | print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t" | |
114 | "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t" | |
115 | "next_comm = {}, next_tid = {}, next_prio = {}".format( | |
116 | pid, event.get_timestamp(), prev_comm, prev_tid, | |
117 | prev_prio, prev_state, next_comm, next_tid, next_prio)) | |
118 | ||
119 | break # Next event | |
120 | ||
121 | # Next event | |
122 | ret = ctf_it.next() | |
123 | if ret < 0: | |
124 | break | |
125 | event = ctf_it.read_event() | |
126 | ||
127 | del ctf_it |