Python: examples: import explicit BT modules
[babeltrace.git] / bindings / python / examples / sched_switch.py
index 1d9f671961a608f5454a2b429d972b09a9070505..0deb4a5320af4a13f70c17cb3a89df820364b93b 100644 (file)
 #
 # The above copyright notice and this permission notice shall be included in
 # all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
 
 # The script takes one optional argument (pid)
 # The script will read events based on pid and
 # The trace needs PID context (lttng add-context -k -t pid)
 
 import sys
-from babeltrace import *
+import babeltrace.reader
+import babeltrace.common
+
 
 if len(sys.argv) < 2 or len(sys.argv) > 3:
-       raise TypeError("Usage: python sched_switch.py [pid] path/to/trace")
+    raise TypeError("Usage: python sched_switch.py [pid] path/to/trace")
 elif len(sys.argv) == 3:
-       usePID = True
+    filterPID = True
 else:
-       usePID = False
-
+    filterPID = False
 
-ctx = Context()
-ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf")
+traces = babeltrace.reader.TraceCollection()
+ret = traces.add_trace(sys.argv[len(sys.argv) - 1], "ctf")
 if ret is None:
-       raise IOError("Error adding trace")
-
-# Setting iterator
-bp = IterPos(SEEK_BEGIN)
-ctf_it = CTFReader.Iterator(ctx, bp)
-
-# Reading events
-event = ctf_it.read_event()
-while event is not None:
-       while True:
-               if event.get_name() == "sched_switch":
-                       # Getting scope definition
-                       sco = event.get_top_level_scope(CTFReader.scope.STREAM_EVENT_CONTEXT)
-                       if sco is None:
-                               print("ERROR: Cannot get definition scope for sched_switch")
-                               break # Next event
-
-                       # Getting PID
-                       pid_field = event.get_field_with_scope(sco, "pid")
-                       if pid_field is None:
-                               print("ERROR: Missing PID info for sched_switch")
-                               break # Next event
-                       pid = pid_field.get_value()
-                       if usePID and (pid != long(sys.argv[1])):
-                               break # Next event
-
-                       sco = event.get_top_level_scope(CTFReader.scope.EVENT_FIELDS)
-
-                       # prev_comm
-                       field = event.get_field_with_scope(sco, "prev_comm")
-                       if field is None:
-                               print("ERROR: Missing prev_comm context info")
-                       prev_comm = field.get_value()
-
-                       # prev_tid
-                       field = event.get_field_with_scope(sco, "prev_tid")
-                       if field is None:
-                               print("ERROR: Missing prev_tid context info")
-                       prev_tid = field.get_value()
-
-                       # prev_prio
-                       field = event.get_field_with_scope(sco, "prev_prio")
-                       if field is None:
-                               print("ERROR: Missing prev_prio context info")
-                       prev_prio = field.get_value()
-
-                       # prev_state
-                       field = event.get_field_with_scope(sco, "prev_state")
-                       if field is None:
-                               print("ERROR: Missing prev_state context info")
-                       prev_state = field.get_value()
-
-                       # next_comm
-                       field = event.get_field_with_scope(sco, "next_comm")
-                       if field is None:
-                               print("ERROR: Missing next_comm context info")
-                       next_comm = field.get_value()
-
-                       # next_tid
-                       field = event.get_field_with_scope(sco, "next_tid")
-                       if field is None:
-                               print("ERROR: Missing next_tid context info")
-                       next_tid = field.get_value()
-
-                       # next_prio
-                       field = event.get_field_with_scope(sco, "next_prio")
-                       if field is None:
-                               print("ERROR: Missing next_prio context info")
-                       next_prio = field.get_value()
-
-                       # Output
-                       print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
-                               "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t"
-                               "next_comm = {}, next_tid = {}, next_prio = {}".format(
-                               pid, event.get_timestamp(), prev_comm, prev_tid,
-                               prev_prio, prev_state, next_comm, next_tid, next_prio))
-
-               break # Next event
-
-       # Next event
-       ret = ctf_it.next()
-       if ret < 0:
-               break
-       event = ctf_it.read_event()
-
-del ctf_it
+    raise IOError("Error adding trace")
+
+for event in traces.events:
+    if event.name != "sched_switch":
+        continue
+
+    # Getting PID
+    pid = event.field_with_scope("pid", babeltrace.common.CTFScope.STREAM_EVENT_CONTEXT)
+    if pid is None:
+        print("ERROR: Missing PID info for sched_switch")
+        continue  # Next event
+
+    if filterPID and (pid != long(sys.argv[1])):
+        continue  # Next event
+
+    prev_comm = event["prev_comm"]
+    prev_tid = event["prev_tid"]
+    prev_prio = event["prev_prio"]
+    prev_state = event["prev_state"]
+    next_comm = event["next_comm"]
+    next_tid = event["next_tid"]
+    next_prio = event["next_prio"]
+
+    # Output
+    print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
+          "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t"
+          "next_comm = {}, next_tid = {}, next_prio = {}".format(
+              pid, event.timestamp, prev_comm, prev_tid,
+              prev_prio, prev_state, next_comm, next_tid, next_prio))
This page took 0.025642 seconds and 4 git commands to generate.