Python bindings: sched_switch example clean-up
[babeltrace.git] / bindings / python / examples / sched_switch.py
CommitLineData
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
28import sys
29from babeltrace import *
30
31if len(sys.argv) < 2 or len(sys.argv) > 3:
32 raise TypeError("Usage: python sched_switch.py [pid] path/to/trace")
33elif len(sys.argv) == 3:
b94a25e9 34 filterPID = True
24a3136a 35else:
b94a25e9 36 filterPID = False
24a3136a 37
74ea15ad
JG
38traces = TraceCollection()
39ret = traces.add_trace(sys.argv[len(sys.argv)-1], "ctf")
24a3136a
DS
40if ret is None:
41 raise IOError("Error adding trace")
42
74ea15ad 43for event in traces.events:
b94a25e9
JG
44 if event.name != "sched_switch":
45 continue
78d714e8 46
b94a25e9
JG
47 # Getting PID
48 pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT)
49 if pid is None:
50 print("ERROR: Missing PID info for sched_switch")
51 continue # Next event
24a3136a 52
b94a25e9
JG
53 if filterPID and (pid != long(sys.argv[1])):
54 continue # Next event
24a3136a 55
b94a25e9
JG
56 prev_comm = event["prev_comm"]
57 prev_tid = event["prev_tid"]
58 prev_prio = event["prev_prio"]
59 prev_state = event["prev_state"]
60 next_comm = event["next_comm"]
61 next_tid = event["next_tid"]
62 next_prio = event["next_prio"]
24a3136a 63
b94a25e9
JG
64 # Output
65 print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
66 "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t"
67 "next_comm = {}, next_tid = {}, next_prio = {}".format(
68 pid, event.timestamp, prev_comm, prev_tid,
69 prev_prio, prev_state, next_comm, next_tid, next_prio))
This page took 0.025359 seconds and 4 git commands to generate.