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