Babeltrace python binding
[babeltrace.git] / bindings / python / examples / babeltrace_and_lttng.py
CommitLineData
24a3136a
DS
1# babeltrace_and_lttng.py
2#
3# Babeltrace and LTTng example script
4#
5# Copyright 2012 EfficiOS Inc.
6#
7# Author: Danny Serres <danny.serres@efficios.com>
8#
9# Permission is hereby granted, free of charge, to any person obtaining a copy
10# of this software and associated documentation files (the "Software"), to deal
11# in the Software without restriction, including without limitation the rights
12# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13# copies of the Software, and to permit persons to whom the Software is
14# furnished to do so, subject to the following conditions:
15#
16# The above copyright notice and this permission notice shall be included in
17# all copies or substantial portions of the Software.
18
19
20# This script uses both lttng-tools and babeltrace
21# python modules. It creates a session, enables
22# events, starts tracing for 2 seconds, stops tracing,
23# destroys the session and outputs the trace in the
24# specified output file.
25#
26# WARNING: will destroy any existing trace having
27# the same name as ses_name
28
29
30# ------------------------------------------------------
31ses_name = "babeltrace-lttng-test"
32trace_path = "/lttng-traces/babeltrace-lttng-trace/"
33out_file = "babeltrace-lttng-trace-text-output.txt"
34# ------------------------------------------------------
35
36
37import time
38try:
39 import babeltrace, lttng
40except ImportError:
41 raise ImportError( "both babeltrace and lttng-tools "
42 "python modules must be installed" )
43
44
45# Errors to raise if something goes wrong
46class LTTngError(Exception):
47 pass
48class BabeltraceError(Exception):
49 pass
50
51
52# LTTNG-TOOLS
53
54# Making sure session does not already exist
55lttng.destroy(ses_name)
56
57# Creating a new session and handle
58ret = lttng.create(ses_name,trace_path)
59if ret < 0:
60 raise LTTngError(lttng.strerror(ret))
61
62han = None
63han = lttng.Handle(ses_name, lttng.Domain())
64if han is None:
65 raise LTTngError("Handle not created")
66
67
68# Enabling all events
69ret = lttng.enable_event(han, lttng.Event(), None)
70if ret < 0:
71 raise LTTngError(lttng.strerror(ret))
72
73
74# Start, wait, stop
75ret = lttng.start(ses_name)
76if ret < 0:
77 raise LTTngError(lttng.strerror(ret))
78print("Tracing...")
79time.sleep(2)
80print("Stopped.")
81ret = lttng.stop(ses_name)
82if ret < 0:
83 raise LTTngError(lttng.strerror(ret))
84
85
86# Destroying tracing session
87ret = lttng.destroy(ses_name)
88if ret < 0:
89 raise LTTngError(lttng.strerror(ret))
90
91
92# BABELTRACE
93
94# Create context and add trace:
95ctx = babeltrace.Context()
96ret = ctx.add_trace(trace_path + "/kernel", "ctf")
97if ret is None:
98 raise BabeltraceError("Error adding trace")
99
100# Iterator setup
101bp = babeltrace.IterPos(babeltrace.SEEK_BEGIN)
102ctf_it = babeltrace.ctf.Iterator(ctx,bp)
103
104# Reading events from trace
105# and outputting timestamps and event names
106# in out_file
107print("Writing trace file...")
108output = open(out_file, "wt")
109
110event = ctf_it.read_event()
111while(event is not None):
112 output.write("TS: {}, {} : {}\n".format(event.get_timestamp(),
113 event.get_cycles(), event.get_name()))
114
115 # Next event
116 ret = ctf_it.next()
117 if ret < 0:
118 break
119 event = ctf_it.read_event()
120
121# Closing file
122output.close()
123
124# Destroying dynamic elements
125del ctf_it, han
126print("Done.")
This page took 0.026942 seconds and 4 git commands to generate.