Commit | Line | Data |
---|---|---|
24a3136a DS |
1 | # babeltrace_and_lttng.py |
2 | # | |
3 | # Babeltrace and LTTng example script | |
4 | # | |
5 | # Copyright 2012 EfficiOS Inc. | |
6 | # | |
7 | # Author: Danny Serres <danny.serres@efficios.com> | |
8 | # | |
9 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | # of this software and associated documentation files (the "Software"), to deal | |
11 | # in the Software without restriction, including without limitation the rights | |
12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | # copies of the Software, and to permit persons to whom the Software is | |
14 | # furnished to do so, subject to the following conditions: | |
15 | # | |
16 | # The above copyright notice and this permission notice shall be included in | |
17 | # all copies or substantial portions of the Software. | |
18 | ||
19 | ||
20 | # This script uses both lttng-tools and babeltrace | |
21 | # python modules. It creates a session, enables | |
22 | # events, starts tracing for 2 seconds, stops tracing, | |
23 | # destroys the session and outputs the trace in the | |
24 | # specified output file. | |
25 | # | |
26 | # WARNING: will destroy any existing trace having | |
27 | # the same name as ses_name | |
28 | ||
29 | ||
30 | # ------------------------------------------------------ | |
31 | ses_name = "babeltrace-lttng-test" | |
32 | trace_path = "/lttng-traces/babeltrace-lttng-trace/" | |
33 | out_file = "babeltrace-lttng-trace-text-output.txt" | |
34 | # ------------------------------------------------------ | |
35 | ||
36 | ||
37 | import time | |
38 | try: | |
39 | import babeltrace, lttng | |
40 | except ImportError: | |
41 | raise ImportError( "both babeltrace and lttng-tools " | |
42 | "python modules must be installed" ) | |
43 | ||
44 | ||
45 | # Errors to raise if something goes wrong | |
46 | class LTTngError(Exception): | |
47 | pass | |
48 | class BabeltraceError(Exception): | |
49 | pass | |
50 | ||
51 | ||
52 | # LTTNG-TOOLS | |
53 | ||
54 | # Making sure session does not already exist | |
55 | lttng.destroy(ses_name) | |
56 | ||
57 | # Creating a new session and handle | |
58 | ret = lttng.create(ses_name,trace_path) | |
59 | if ret < 0: | |
60 | raise LTTngError(lttng.strerror(ret)) | |
61 | ||
62 | han = None | |
63 | han = lttng.Handle(ses_name, lttng.Domain()) | |
64 | if han is None: | |
65 | raise LTTngError("Handle not created") | |
66 | ||
67 | ||
68 | # Enabling all events | |
69 | ret = lttng.enable_event(han, lttng.Event(), None) | |
70 | if ret < 0: | |
71 | raise LTTngError(lttng.strerror(ret)) | |
72 | ||
73 | ||
74 | # Start, wait, stop | |
75 | ret = lttng.start(ses_name) | |
76 | if ret < 0: | |
77 | raise LTTngError(lttng.strerror(ret)) | |
78 | print("Tracing...") | |
79 | time.sleep(2) | |
80 | print("Stopped.") | |
81 | ret = lttng.stop(ses_name) | |
82 | if ret < 0: | |
83 | raise LTTngError(lttng.strerror(ret)) | |
84 | ||
85 | ||
86 | # Destroying tracing session | |
87 | ret = lttng.destroy(ses_name) | |
88 | if ret < 0: | |
89 | raise LTTngError(lttng.strerror(ret)) | |
90 | ||
91 | ||
92 | # BABELTRACE | |
93 | ||
94 | # Create context and add trace: | |
95 | ctx = babeltrace.Context() | |
96 | ret = ctx.add_trace(trace_path + "/kernel", "ctf") | |
97 | if ret is None: | |
98 | raise BabeltraceError("Error adding trace") | |
99 | ||
100 | # Iterator setup | |
101 | bp = babeltrace.IterPos(babeltrace.SEEK_BEGIN) | |
102 | ctf_it = babeltrace.ctf.Iterator(ctx,bp) | |
103 | ||
104 | # Reading events from trace | |
105 | # and outputting timestamps and event names | |
106 | # in out_file | |
107 | print("Writing trace file...") | |
108 | output = open(out_file, "wt") | |
109 | ||
110 | event = ctf_it.read_event() | |
111 | while(event is not None): | |
112 | output.write("TS: {}, {} : {}\n".format(event.get_timestamp(), | |
113 | event.get_cycles(), event.get_name())) | |
114 | ||
115 | # Next event | |
116 | ret = ctf_it.next() | |
117 | if ret < 0: | |
118 | break | |
119 | event = ctf_it.read_event() | |
120 | ||
121 | # Closing file | |
122 | output.close() | |
123 | ||
124 | # Destroying dynamic elements | |
125 | del ctf_it, han | |
126 | print("Done.") |