| 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 | # 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. |
| 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 | # ------------------------------------------------------ |
| 40 | ses_name = "babeltrace-lttng-test" |
| 41 | trace_path = "/lttng-traces/babeltrace-lttng-trace/" |
| 42 | out_file = "babeltrace-lttng-trace-text-output.txt" |
| 43 | # ------------------------------------------------------ |
| 44 | |
| 45 | |
| 46 | import time |
| 47 | try: |
| 48 | import babeltrace |
| 49 | import lttng |
| 50 | except ImportError: |
| 51 | raise ImportError( "both babeltrace and lttng-tools " |
| 52 | "python modules must be installed") |
| 53 | |
| 54 | |
| 55 | # Errors to raise if something goes wrong |
| 56 | class LTTngError(Exception): |
| 57 | pass |
| 58 | |
| 59 | |
| 60 | class BabeltraceError(Exception): |
| 61 | pass |
| 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 |
| 70 | ret = lttng.create(ses_name, trace_path) |
| 71 | if ret < 0: |
| 72 | raise LTTngError(lttng.strerror(ret)) |
| 73 | |
| 74 | domain = lttng.Domain() |
| 75 | domain.type = lttng.DOMAIN_KERNEL |
| 76 | |
| 77 | han = None |
| 78 | han = lttng.Handle(ses_name, domain) |
| 79 | if han is None: |
| 80 | raise LTTngError("Handle not created") |
| 81 | |
| 82 | |
| 83 | # Enabling all events |
| 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) |
| 88 | if ret < 0: |
| 89 | raise LTTngError(lttng.strerror(ret)) |
| 90 | |
| 91 | # Start, wait, stop |
| 92 | ret = lttng.start(ses_name) |
| 93 | if ret < 0: |
| 94 | raise LTTngError(lttng.strerror(ret)) |
| 95 | print("Tracing...") |
| 96 | time.sleep(2) |
| 97 | print("Stopped.") |
| 98 | ret = lttng.stop(ses_name) |
| 99 | if ret < 0: |
| 100 | raise LTTngError(lttng.strerror(ret)) |
| 101 | |
| 102 | |
| 103 | # Destroying tracing session |
| 104 | ret = lttng.destroy(ses_name) |
| 105 | if ret < 0: |
| 106 | raise LTTngError(lttng.strerror(ret)) |
| 107 | |
| 108 | |
| 109 | # BABELTRACE |
| 110 | |
| 111 | # Create TraceCollecion and add trace: |
| 112 | traces = babeltrace.TraceCollection() |
| 113 | ret = traces.add_trace(trace_path + "/kernel", "ctf") |
| 114 | if ret is None: |
| 115 | raise BabeltraceError("Error adding trace") |
| 116 | |
| 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 | |
| 123 | for event in traces.events: |
| 124 | output.write("TS: {}, {} : {}\n".format( |
| 125 | event.timestamp, event.cycles, event.name)) |
| 126 | |
| 127 | # Closing file |
| 128 | output.close() |
| 129 | |
| 130 | print("Done.") |