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. | |
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 | # ------------------------------------------------------ | |
a47ab64a JG |
32 | ses_name = "babeltrace-lttng-test" |
33 | trace_path = "/lttng-traces/babeltrace-lttng-trace/" | |
34 | out_file = "babeltrace-lttng-trace-text-output.txt" | |
24a3136a DS |
35 | # ------------------------------------------------------ |
36 | ||
37 | ||
38 | import time | |
39 | try: | |
a47ab64a JG |
40 | import babeltrace |
41 | import lttng | |
24a3136a | 42 | except ImportError: |
a47ab64a JG |
43 | raise ImportError( "both babeltrace and lttng-tools " |
44 | "python modules must be installed") | |
24a3136a DS |
45 | |
46 | ||
47 | # Errors to raise if something goes wrong | |
48 | class LTTngError(Exception): | |
a47ab64a JG |
49 | pass |
50 | ||
51 | ||
24a3136a | 52 | class BabeltraceError(Exception): |
a47ab64a | 53 | pass |
24a3136a DS |
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 | |
a47ab64a | 62 | ret = lttng.create(ses_name, trace_path) |
24a3136a | 63 | if ret < 0: |
a47ab64a | 64 | raise LTTngError(lttng.strerror(ret)) |
24a3136a | 65 | |
300b4c33 JG |
66 | domain = lttng.Domain() |
67 | domain.type = lttng.DOMAIN_KERNEL | |
68 | ||
24a3136a | 69 | han = None |
300b4c33 | 70 | han = lttng.Handle(ses_name, domain) |
24a3136a | 71 | if han is None: |
a47ab64a | 72 | raise LTTngError("Handle not created") |
24a3136a DS |
73 | |
74 | ||
75 | # Enabling all events | |
300b4c33 JG |
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) | |
24a3136a | 80 | if ret < 0: |
a47ab64a | 81 | raise LTTngError(lttng.strerror(ret)) |
24a3136a | 82 | |
24a3136a DS |
83 | # Start, wait, stop |
84 | ret = lttng.start(ses_name) | |
85 | if ret < 0: | |
a47ab64a | 86 | raise LTTngError(lttng.strerror(ret)) |
24a3136a DS |
87 | print("Tracing...") |
88 | time.sleep(2) | |
89 | print("Stopped.") | |
90 | ret = lttng.stop(ses_name) | |
91 | if ret < 0: | |
a47ab64a | 92 | raise LTTngError(lttng.strerror(ret)) |
24a3136a DS |
93 | |
94 | ||
95 | # Destroying tracing session | |
96 | ret = lttng.destroy(ses_name) | |
97 | if ret < 0: | |
a47ab64a | 98 | raise LTTngError(lttng.strerror(ret)) |
24a3136a DS |
99 | |
100 | ||
101 | # BABELTRACE | |
102 | ||
74ea15ad JG |
103 | # Create TraceCollecion and add trace: |
104 | traces = babeltrace.TraceCollection() | |
105 | ret = traces.add_trace(trace_path + "/kernel", "ctf") | |
24a3136a | 106 | if ret is None: |
a47ab64a | 107 | raise BabeltraceError("Error adding trace") |
24a3136a | 108 | |
24a3136a DS |
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 | ||
74ea15ad | 115 | for event in traces.events: |
a47ab64a JG |
116 | output.write("TS: {}, {} : {}\n".format( |
117 | event.timestamp, event.cycles, event.name)) | |
24a3136a | 118 | |
24a3136a DS |
119 | # Closing file |
120 | output.close() | |
121 | ||
24a3136a | 122 | print("Done.") |