#!/usr/bin/env python3
# babeltrace_and_lttng.py
-#
+#
# Babeltrace and LTTng example script
-#
+#
# Copyright 2012 EfficiOS Inc.
-#
+#
# Author: Danny Serres <danny.serres@efficios.com>
-#
+#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
-#
+#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# ------------------------------------------------------
-ses_name = "babeltrace-lttng-test"
-trace_path = "/lttng-traces/babeltrace-lttng-trace/"
-out_file = "babeltrace-lttng-trace-text-output.txt"
+ses_name = "babeltrace-lttng-test"
+trace_path = "/lttng-traces/babeltrace-lttng-trace/"
+out_file = "babeltrace-lttng-trace-text-output.txt"
# ------------------------------------------------------
import time
try:
- import babeltrace, lttng
+ import babeltrace
+ import lttng
except ImportError:
- raise ImportError( "both babeltrace and lttng-tools "
- "python modules must be installed" )
+ raise ImportError( "both babeltrace and lttng-tools "
+ "python modules must be installed")
# Errors to raise if something goes wrong
class LTTngError(Exception):
- pass
+ pass
+
+
class BabeltraceError(Exception):
- pass
+ pass
# LTTNG-TOOLS
lttng.destroy(ses_name)
# Creating a new session and handle
-ret = lttng.create(ses_name,trace_path)
+ret = lttng.create(ses_name, trace_path)
if ret < 0:
- raise LTTngError(lttng.strerror(ret))
+ raise LTTngError(lttng.strerror(ret))
domain = lttng.Domain()
domain.type = lttng.DOMAIN_KERNEL
han = None
han = lttng.Handle(ses_name, domain)
if han is None:
- raise LTTngError("Handle not created")
+ raise LTTngError("Handle not created")
# Enabling all events
event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL
ret = lttng.enable_event(han, event, None)
if ret < 0:
- raise LTTngError(lttng.strerror(ret))
+ raise LTTngError(lttng.strerror(ret))
# Start, wait, stop
ret = lttng.start(ses_name)
if ret < 0:
- raise LTTngError(lttng.strerror(ret))
+ raise LTTngError(lttng.strerror(ret))
print("Tracing...")
time.sleep(2)
print("Stopped.")
ret = lttng.stop(ses_name)
if ret < 0:
- raise LTTngError(lttng.strerror(ret))
+ raise LTTngError(lttng.strerror(ret))
# Destroying tracing session
ret = lttng.destroy(ses_name)
if ret < 0:
- raise LTTngError(lttng.strerror(ret))
+ raise LTTngError(lttng.strerror(ret))
# BABELTRACE
traces = babeltrace.TraceCollection()
ret = traces.add_trace(trace_path + "/kernel", "ctf")
if ret is None:
- raise BabeltraceError("Error adding trace")
+ raise BabeltraceError("Error adding trace")
# Reading events from trace
# and outputting timestamps and event names
output = open(out_file, "wt")
for event in traces.events:
- output.write("TS: {}, {} : {}\n".format(event.timestamp, event.cycles, event.name))
+ output.write("TS: {}, {} : {}\n".format(
+ event.timestamp, event.cycles, event.name))
# Closing file
output.close()
packet_context_type = stream_class.packet_context_type
print("\nFields in default packet context:")
for field in packet_context_type.fields:
- print(str(type(field[1])) + " " + field[0])
+ print(str(type(field[1])) + " " + field[0])
packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
stream_class.packet_context_type = packet_context_type
stream = writer.create_stream(stream_class)
for i in range(100):
- event = CTFWriter.Event(event_class)
+ event = CTFWriter.Event(event_class)
- clock.time = i * 1000
- structure_field = event.payload("structure_field")
- integer_field = structure_field.field("an_integer")
- integer_field.value = i
+ clock.time = i * 1000
+ structure_field = event.payload("structure_field")
+ integer_field = structure_field.field("an_integer")
+ integer_field.value = i
- string_field = structure_field.field("a_string_field")
- string_field.value = "Test string."
+ string_field = structure_field.field("a_string_field")
+ string_field.value = "Test string."
- float_field = event.payload("float_field")
- float_field.value = float(i) + (float(i) / 100.0)
+ float_field = event.payload("float_field")
+ float_field.value = float(i) + (float(i) / 100.0)
- array_field = event.payload("array_field")
- for j in range(5):
- element = array_field.field(j)
- element.value = i + j
+ array_field = event.payload("array_field")
+ for j in range(5):
+ element = array_field.field(j)
+ element.value = i + j
- event.payload("sequence_len").value = i % 10
- sequence_field = event.payload("sequence_field")
- sequence_field.length = event.payload("sequence_len")
- for j in range(event.payload("sequence_len").value):
- sequence_field.field(j).value = i + j
+ event.payload("sequence_len").value = i % 10
+ sequence_field = event.payload("sequence_field")
+ sequence_field.length = event.payload("sequence_len")
+ for j in range(event.payload("sequence_len").value):
+ sequence_field.field(j).value = i + j
- enumeration_field = event.payload("enum_field")
- integer_field = enumeration_field.container
- enumeration_field.value = i % 10
+ enumeration_field = event.payload("enum_field")
+ integer_field = enumeration_field.container
+ enumeration_field.value = i % 10
- stream.append_event(event)
+ stream.append_event(event)
# Populate custom packet context field before flushing
packet_context = stream.packet_context
# Check for path arg:
if len(sys.argv) < 2:
- raise TypeError("Usage: python example-api-test.py path/to/file")
+ raise TypeError("Usage: python example-api-test.py path/to/file")
# Create TraceCollection and add trace:
traces = TraceCollection()
trace_handle = traces.add_trace(sys.argv[1], "ctf")
if trace_handle is None:
- raise IOError("Error adding trace")
+ raise IOError("Error adding trace")
# Listing events
print("--- Event list ---")
for event_declaration in trace_handle.events:
- print("event : {}".format(event_declaration.name))
- if event_declaration.name == "sched_switch":
- for field_declaration in event_declaration.fields:
- print(field_declaration)
+ print("event : {}".format(event_declaration.name))
+ if event_declaration.name == "sched_switch":
+ for field_declaration in event_declaration.fields:
+ print(field_declaration)
print("--- Done ---")
for event in traces.events:
- print("TS: {}, {} : {}".format(event.timestamp, event.cycles, event.name))
-
- if event.name == "sched_switch":
- prev_comm = event["prev_comm"]
- if prev_comm is None:
- print("ERROR: Missing prev_comm context info")
- else:
- print("sched_switch prev_comm: {}".format(prev_comm))
-
- if event.name == "exit_syscall":
- ret_code = event["ret"]
- if ret_code is None:
- print("ERROR: Unable to extract ret")
- else:
- print("exit_syscall ret: {}".format(ret_code))
+ print("TS: {}, {} : {}".format(event.timestamp, event.cycles, event.name))
+
+ if event.name == "sched_switch":
+ prev_comm = event["prev_comm"]
+ if prev_comm is None:
+ print("ERROR: Missing prev_comm context info")
+ else:
+ print("sched_switch prev_comm: {}".format(prev_comm))
+
+ if event.name == "exit_syscall":
+ ret_code = event["ret"]
+ if ret_code is None:
+ print("ERROR: Unable to extract ret")
+ else:
+ print("exit_syscall ret: {}".format(ret_code))
from babeltrace import *
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:
- filterPID = True
+ filterPID = True
else:
- filterPID = False
+ filterPID = False
traces = TraceCollection()
-ret = traces.add_trace(sys.argv[len(sys.argv)-1], "ctf")
+ret = traces.add_trace(sys.argv[len(sys.argv) - 1], "ctf")
if ret is None:
- raise IOError("Error adding trace")
+ raise IOError("Error adding trace")
for event in traces.events:
- if event.name != "sched_switch":
- continue
+ if event.name != "sched_switch":
+ continue
- # Getting PID
- pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT)
- if pid is None:
- print("ERROR: Missing PID info for sched_switch")
- continue # Next event
+ # Getting PID
+ pid = event.field_with_scope("pid", 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
+ 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"]
+ 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))
+ # 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))
# Check for path arg:
if len(sys.argv) < 2:
- raise TypeError("Usage: sequence_test.py path/to/file")
+ raise TypeError("Usage: sequence_test.py path/to/file")
# Create TraceCollection and add trace:
traces = TraceCollection()
trace_handle = traces.add_trace(sys.argv[1], "ctf")
if trace_handle is None:
- raise IOError("Error adding trace")
+ raise IOError("Error adding trace")
# Listing events
print("--- Event list ---")
for event_declaration in trace_handle.events:
- print("event : {}".format(event_declaration.name))
+ print("event : {}".format(event_declaration.name))
print("--- Done ---")
for event in traces.events:
- print("TS: {}, {} : {}".format(event.timestamp,
- event.cycles, event.name))
-
- try:
- sequence = event["seq_int_field"]
- print("int sequence values: {}". format(sequence))
- except KeyError:
- pass
-
- try:
- sequence = event["seq_long_field"]
- print("long sequence values: {}". format(sequence))
- except KeyError:
- pass
+ print("TS: {}, {} : {}".format(event.timestamp,
+ event.cycles, event.name))
+
+ try:
+ sequence = event["seq_int_field"]
+ print("int sequence values: {}". format(sequence))
+ except KeyError:
+ pass
+
+ try:
+ sequence = event["seq_long_field"]
+ print("long sequence values: {}". format(sequence))
+ except KeyError:
+ pass