Python bindings: Make examples PEP8 compliant
[babeltrace.git] / bindings / python / examples / babeltrace_and_lttng.py
CommitLineData
300b4c33 1#!/usr/bin/env python3
24a3136a 2# babeltrace_and_lttng.py
a47ab64a 3#
24a3136a 4# Babeltrace and LTTng example script
a47ab64a 5#
24a3136a 6# Copyright 2012 EfficiOS Inc.
a47ab64a 7#
24a3136a 8# Author: Danny Serres <danny.serres@efficios.com>
a47ab64a 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:
a47ab64a 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
21# This script uses both lttng-tools and babeltrace
22# python modules. It creates a session, enables
23# events, starts tracing for 2 seconds, stops tracing,
24# destroys the session and outputs the trace in the
25# specified output file.
26#
27# WARNING: will destroy any existing trace having
28# the same name as ses_name
29
30
31# ------------------------------------------------------
a47ab64a
JG
32ses_name = "babeltrace-lttng-test"
33trace_path = "/lttng-traces/babeltrace-lttng-trace/"
34out_file = "babeltrace-lttng-trace-text-output.txt"
24a3136a
DS
35# ------------------------------------------------------
36
37
38import time
39try:
a47ab64a
JG
40 import babeltrace
41 import lttng
24a3136a 42except ImportError:
a47ab64a
JG
43 raise ImportError( "both babeltrace and lttng-tools "
44 "python modules must be installed")
24a3136a
DS
45
46
47# Errors to raise if something goes wrong
48class LTTngError(Exception):
a47ab64a
JG
49 pass
50
51
24a3136a 52class BabeltraceError(Exception):
a47ab64a 53 pass
24a3136a
DS
54
55
56# LTTNG-TOOLS
57
58# Making sure session does not already exist
59lttng.destroy(ses_name)
60
61# Creating a new session and handle
a47ab64a 62ret = lttng.create(ses_name, trace_path)
24a3136a 63if ret < 0:
a47ab64a 64 raise LTTngError(lttng.strerror(ret))
24a3136a 65
300b4c33
JG
66domain = lttng.Domain()
67domain.type = lttng.DOMAIN_KERNEL
68
24a3136a 69han = None
300b4c33 70han = lttng.Handle(ses_name, domain)
24a3136a 71if han is None:
a47ab64a 72 raise LTTngError("Handle not created")
24a3136a
DS
73
74
75# Enabling all events
300b4c33
JG
76event = lttng.Event()
77event.type = lttng.EVENT_ALL
78event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL
79ret = lttng.enable_event(han, event, None)
24a3136a 80if ret < 0:
a47ab64a 81 raise LTTngError(lttng.strerror(ret))
24a3136a 82
24a3136a
DS
83# Start, wait, stop
84ret = lttng.start(ses_name)
85if ret < 0:
a47ab64a 86 raise LTTngError(lttng.strerror(ret))
24a3136a
DS
87print("Tracing...")
88time.sleep(2)
89print("Stopped.")
90ret = lttng.stop(ses_name)
91if ret < 0:
a47ab64a 92 raise LTTngError(lttng.strerror(ret))
24a3136a
DS
93
94
95# Destroying tracing session
96ret = lttng.destroy(ses_name)
97if ret < 0:
a47ab64a 98 raise LTTngError(lttng.strerror(ret))
24a3136a
DS
99
100
101# BABELTRACE
102
74ea15ad
JG
103# Create TraceCollecion and add trace:
104traces = babeltrace.TraceCollection()
105ret = traces.add_trace(trace_path + "/kernel", "ctf")
24a3136a 106if ret is None:
a47ab64a 107 raise BabeltraceError("Error adding trace")
24a3136a 108
24a3136a
DS
109# Reading events from trace
110# and outputting timestamps and event names
111# in out_file
112print("Writing trace file...")
113output = open(out_file, "wt")
114
74ea15ad 115for event in traces.events:
a47ab64a
JG
116 output.write("TS: {}, {} : {}\n".format(
117 event.timestamp, event.cycles, event.name))
24a3136a 118
24a3136a
DS
119# Closing file
120output.close()
121
24a3136a 122print("Done.")
This page took 0.028275 seconds and 4 git commands to generate.