Commit | Line | Data |
---|---|---|
300b4c33 | 1 | #!/usr/bin/env python3 |
24a3136a DS |
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 | ||
300b4c33 JG |
63 | domain = lttng.Domain() |
64 | domain.type = lttng.DOMAIN_KERNEL | |
65 | ||
24a3136a | 66 | han = None |
300b4c33 | 67 | han = lttng.Handle(ses_name, domain) |
24a3136a DS |
68 | if han is None: |
69 | raise LTTngError("Handle not created") | |
70 | ||
71 | ||
72 | # Enabling all events | |
300b4c33 JG |
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) | |
24a3136a DS |
77 | if ret < 0: |
78 | raise LTTngError(lttng.strerror(ret)) | |
79 | ||
24a3136a DS |
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 | ||
74ea15ad JG |
100 | # Create TraceCollecion and add trace: |
101 | traces = babeltrace.TraceCollection() | |
102 | ret = traces.add_trace(trace_path + "/kernel", "ctf") | |
24a3136a DS |
103 | if ret is None: |
104 | raise BabeltraceError("Error adding trace") | |
105 | ||
24a3136a DS |
106 | # Reading events from trace |
107 | # and outputting timestamps and event names | |
108 | # in out_file | |
109 | print("Writing trace file...") | |
110 | output = open(out_file, "wt") | |
111 | ||
74ea15ad | 112 | for event in traces.events: |
24a3136a DS |
113 | output.write("TS: {}, {} : {}\n".format(event.get_timestamp(), |
114 | event.get_cycles(), event.get_name())) | |
115 | ||
24a3136a DS |
116 | # Closing file |
117 | output.close() | |
118 | ||
24a3136a | 119 | print("Done.") |