Commit | Line | Data |
---|---|---|
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 |
40 | ses_name = "babeltrace-lttng-test" |
41 | trace_path = "/lttng-traces/babeltrace-lttng-trace/" | |
42 | out_file = "babeltrace-lttng-trace-text-output.txt" | |
24a3136a DS |
43 | # ------------------------------------------------------ |
44 | ||
45 | ||
46 | import time | |
47 | try: | |
a47ab64a JG |
48 | import babeltrace |
49 | import lttng | |
24a3136a | 50 | except 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 | |
56 | class LTTngError(Exception): | |
a47ab64a JG |
57 | pass |
58 | ||
59 | ||
24a3136a | 60 | class BabeltraceError(Exception): |
a47ab64a | 61 | pass |
24a3136a DS |
62 | |
63 | ||
64 | # LTTNG-TOOLS | |
65 | ||
66 | # Making sure session does not already exist | |
67 | lttng.destroy(ses_name) | |
68 | ||
69 | # Creating a new session and handle | |
a47ab64a | 70 | ret = lttng.create(ses_name, trace_path) |
24a3136a | 71 | if ret < 0: |
a47ab64a | 72 | raise LTTngError(lttng.strerror(ret)) |
24a3136a | 73 | |
300b4c33 JG |
74 | domain = lttng.Domain() |
75 | domain.type = lttng.DOMAIN_KERNEL | |
76 | ||
24a3136a | 77 | han = None |
300b4c33 | 78 | han = lttng.Handle(ses_name, domain) |
24a3136a | 79 | if han is None: |
a47ab64a | 80 | raise LTTngError("Handle not created") |
24a3136a DS |
81 | |
82 | ||
83 | # Enabling all events | |
300b4c33 JG |
84 | event = lttng.Event() |
85 | event.type = lttng.EVENT_ALL | |
86 | event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL | |
87 | ret = lttng.enable_event(han, event, None) | |
24a3136a | 88 | if ret < 0: |
a47ab64a | 89 | raise LTTngError(lttng.strerror(ret)) |
24a3136a | 90 | |
24a3136a DS |
91 | # Start, wait, stop |
92 | ret = lttng.start(ses_name) | |
93 | if ret < 0: | |
a47ab64a | 94 | raise LTTngError(lttng.strerror(ret)) |
24a3136a DS |
95 | print("Tracing...") |
96 | time.sleep(2) | |
97 | print("Stopped.") | |
98 | ret = lttng.stop(ses_name) | |
99 | if ret < 0: | |
a47ab64a | 100 | raise LTTngError(lttng.strerror(ret)) |
24a3136a DS |
101 | |
102 | ||
103 | # Destroying tracing session | |
104 | ret = lttng.destroy(ses_name) | |
105 | if ret < 0: | |
a47ab64a | 106 | raise LTTngError(lttng.strerror(ret)) |
24a3136a DS |
107 | |
108 | ||
109 | # BABELTRACE | |
110 | ||
74ea15ad JG |
111 | # Create TraceCollecion and add trace: |
112 | traces = babeltrace.TraceCollection() | |
113 | ret = traces.add_trace(trace_path + "/kernel", "ctf") | |
24a3136a | 114 | if 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 | |
120 | print("Writing trace file...") | |
121 | output = open(out_file, "wt") | |
122 | ||
74ea15ad | 123 | for 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 |
128 | output.close() | |
129 | ||
24a3136a | 130 | print("Done.") |