Fix: missing paragraph in python bindings license (MIT)
[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.
5aa9939f
JD
19#
20# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26# SOFTWARE.
24a3136a
DS
27
28
29# This script uses both lttng-tools and babeltrace
30# python modules. It creates a session, enables
31# events, starts tracing for 2 seconds, stops tracing,
32# destroys the session and outputs the trace in the
33# specified output file.
34#
35# WARNING: will destroy any existing trace having
36# the same name as ses_name
37
38
39# ------------------------------------------------------
a47ab64a
JG
40ses_name = "babeltrace-lttng-test"
41trace_path = "/lttng-traces/babeltrace-lttng-trace/"
42out_file = "babeltrace-lttng-trace-text-output.txt"
24a3136a
DS
43# ------------------------------------------------------
44
45
46import time
47try:
a47ab64a
JG
48 import babeltrace
49 import lttng
24a3136a 50except ImportError:
a47ab64a
JG
51 raise ImportError( "both babeltrace and lttng-tools "
52 "python modules must be installed")
24a3136a
DS
53
54
55# Errors to raise if something goes wrong
56class LTTngError(Exception):
a47ab64a
JG
57 pass
58
59
24a3136a 60class BabeltraceError(Exception):
a47ab64a 61 pass
24a3136a
DS
62
63
64# LTTNG-TOOLS
65
66# Making sure session does not already exist
67lttng.destroy(ses_name)
68
69# Creating a new session and handle
a47ab64a 70ret = lttng.create(ses_name, trace_path)
24a3136a 71if ret < 0:
a47ab64a 72 raise LTTngError(lttng.strerror(ret))
24a3136a 73
300b4c33
JG
74domain = lttng.Domain()
75domain.type = lttng.DOMAIN_KERNEL
76
24a3136a 77han = None
300b4c33 78han = lttng.Handle(ses_name, domain)
24a3136a 79if han is None:
a47ab64a 80 raise LTTngError("Handle not created")
24a3136a
DS
81
82
83# Enabling all events
300b4c33
JG
84event = lttng.Event()
85event.type = lttng.EVENT_ALL
86event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL
87ret = lttng.enable_event(han, event, None)
24a3136a 88if ret < 0:
a47ab64a 89 raise LTTngError(lttng.strerror(ret))
24a3136a 90
24a3136a
DS
91# Start, wait, stop
92ret = lttng.start(ses_name)
93if ret < 0:
a47ab64a 94 raise LTTngError(lttng.strerror(ret))
24a3136a
DS
95print("Tracing...")
96time.sleep(2)
97print("Stopped.")
98ret = lttng.stop(ses_name)
99if ret < 0:
a47ab64a 100 raise LTTngError(lttng.strerror(ret))
24a3136a
DS
101
102
103# Destroying tracing session
104ret = lttng.destroy(ses_name)
105if ret < 0:
a47ab64a 106 raise LTTngError(lttng.strerror(ret))
24a3136a
DS
107
108
109# BABELTRACE
110
74ea15ad
JG
111# Create TraceCollecion and add trace:
112traces = babeltrace.TraceCollection()
113ret = traces.add_trace(trace_path + "/kernel", "ctf")
24a3136a 114if ret is None:
a47ab64a 115 raise BabeltraceError("Error adding trace")
24a3136a 116
24a3136a
DS
117# Reading events from trace
118# and outputting timestamps and event names
119# in out_file
120print("Writing trace file...")
121output = open(out_file, "wt")
122
74ea15ad 123for event in traces.events:
a47ab64a
JG
124 output.write("TS: {}, {} : {}\n".format(
125 event.timestamp, event.cycles, event.name))
24a3136a 126
24a3136a
DS
127# Closing file
128output.close()
129
24a3136a 130print("Done.")
This page took 0.030038 seconds and 4 git commands to generate.