3a99b1fbae2af1ad3eca68b725cda34553361c04
[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, lttng
41 except ImportError:
42 raise ImportError( "both babeltrace and lttng-tools "
43 "python modules must be installed" )
44
45
46 # Errors to raise if something goes wrong
47 class LTTngError(Exception):
48 pass
49 class BabeltraceError(Exception):
50 pass
51
52
53 # LTTNG-TOOLS
54
55 # Making sure session does not already exist
56 lttng.destroy(ses_name)
57
58 # Creating a new session and handle
59 ret = lttng.create(ses_name,trace_path)
60 if ret < 0:
61 raise LTTngError(lttng.strerror(ret))
62
63 domain = lttng.Domain()
64 domain.type = lttng.DOMAIN_KERNEL
65
66 han = None
67 han = lttng.Handle(ses_name, domain)
68 if han is None:
69 raise LTTngError("Handle not created")
70
71
72 # Enabling all events
73 event = lttng.Event()
74 event.type = lttng.EVENT_ALL
75 event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL
76 ret = lttng.enable_event(han, event, None)
77 if ret < 0:
78 raise LTTngError(lttng.strerror(ret))
79
80 # Start, wait, stop
81 ret = lttng.start(ses_name)
82 if ret < 0:
83 raise LTTngError(lttng.strerror(ret))
84 print("Tracing...")
85 time.sleep(2)
86 print("Stopped.")
87 ret = lttng.stop(ses_name)
88 if ret < 0:
89 raise LTTngError(lttng.strerror(ret))
90
91
92 # Destroying tracing session
93 ret = lttng.destroy(ses_name)
94 if ret < 0:
95 raise LTTngError(lttng.strerror(ret))
96
97
98 # BABELTRACE
99
100 # Create context and add trace:
101 ctx = babeltrace.Context()
102 ret = ctx.add_trace(trace_path + "/kernel", "ctf")
103 if ret is None:
104 raise BabeltraceError("Error adding trace")
105
106 # Iterator setup
107 bp = babeltrace.IterPos(babeltrace.SEEK_BEGIN)
108 ctf_it = babeltrace.CTFReader.Iterator(ctx,bp)
109
110 # Reading events from trace
111 # and outputting timestamps and event names
112 # in out_file
113 print("Writing trace file...")
114 output = open(out_file, "wt")
115
116 event = ctf_it.read_event()
117 while(event is not None):
118 output.write("TS: {}, {} : {}\n".format(event.get_timestamp(),
119 event.get_cycles(), event.get_name()))
120
121 # Next event
122 ret = ctf_it.next()
123 if ret < 0:
124 break
125 event = ctf_it.read_event()
126
127 # Closing file
128 output.close()
129
130 # Destroying dynamic elements
131 del ctf_it, han
132 print("Done.")
This page took 0.031608 seconds and 3 git commands to generate.