1 # babeltrace_and_lttng.py
3 # Babeltrace and LTTng example script
5 # Copyright 2012 EfficiOS Inc.
7 # Author: Danny Serres <danny.serres@efficios.com>
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:
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
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.
26 # WARNING: will destroy any existing trace having
27 # the same name as ses_name
30 # ------------------------------------------------------
31 ses_name
= "babeltrace-lttng-test"
32 trace_path
= "/lttng-traces/babeltrace-lttng-trace/"
33 out_file
= "babeltrace-lttng-trace-text-output.txt"
34 # ------------------------------------------------------
39 import babeltrace
, lttng
41 raise ImportError( "both babeltrace and lttng-tools "
42 "python modules must be installed" )
45 # Errors to raise if something goes wrong
46 class LTTngError(Exception):
48 class BabeltraceError(Exception):
54 # Making sure session does not already exist
55 lttng
.destroy(ses_name
)
57 # Creating a new session and handle
58 ret
= lttng
.create(ses_name
,trace_path
)
60 raise LTTngError(lttng
.strerror(ret
))
63 han
= lttng
.Handle(ses_name
, lttng
.Domain())
65 raise LTTngError("Handle not created")
69 ret
= lttng
.enable_event(han
, lttng
.Event(), None)
71 raise LTTngError(lttng
.strerror(ret
))
75 ret
= lttng
.start(ses_name
)
77 raise LTTngError(lttng
.strerror(ret
))
81 ret
= lttng
.stop(ses_name
)
83 raise LTTngError(lttng
.strerror(ret
))
86 # Destroying tracing session
87 ret
= lttng
.destroy(ses_name
)
89 raise LTTngError(lttng
.strerror(ret
))
94 # Create context and add trace:
95 ctx
= babeltrace
.Context()
96 ret
= ctx
.add_trace(trace_path
+ "/kernel", "ctf")
98 raise BabeltraceError("Error adding trace")
101 bp
= babeltrace
.IterPos(babeltrace
.SEEK_BEGIN
)
102 ctf_it
= babeltrace
.ctf
.Iterator(ctx
,bp
)
104 # Reading events from trace
105 # and outputting timestamps and event names
107 print("Writing trace file...")
108 output
= open(out_file
, "wt")
110 event
= ctf_it
.read_event()
111 while(event
is not None):
112 output
.write("TS: {}, {} : {}\n".format(event
.get_timestamp(),
113 event
.get_cycles(), event
.get_name()))
119 event
= ctf_it
.read_event()
124 # Destroying dynamic elements
This page took 0.041535 seconds and 4 git commands to generate.