| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
| 7 | |
| 8 | import uuid |
| 9 | import os |
| 10 | import subprocess |
| 11 | import shutil |
| 12 | import sys |
| 13 | import time |
| 14 | import tempfile |
| 15 | |
| 16 | # Import lttng bindings generated in the current tree |
| 17 | lttng_bindings_path = os.path.dirname(os.path.abspath(__file__)) + "/" |
| 18 | for i in range(3): |
| 19 | lttng_bindings_path = os.path.dirname(lttng_bindings_path) |
| 20 | lttng_bindings_path = lttng_bindings_path + "/extras/bindings/swig/python" |
| 21 | lttng_bindings_libs_path = lttng_bindings_path + "/.libs" |
| 22 | sys.path.append(lttng_bindings_path) |
| 23 | sys.path.append(lttng_bindings_libs_path) |
| 24 | from lttng import * |
| 25 | |
| 26 | _time_tests = True |
| 27 | if os.getenv("TAP_AUTOTIME", "1") == "0": |
| 28 | _time_tests = False |
| 29 | |
| 30 | |
| 31 | def _get_time_ns(): |
| 32 | # type: () -> int |
| 33 | |
| 34 | # time.monotonic is only available since Python 3.3. We don't support |
| 35 | # those older versions so we can simply assert here. |
| 36 | assert sys.version_info >= (3, 3, 0) |
| 37 | |
| 38 | # time.monotonic_ns is only available for python >= 3.8, |
| 39 | # so the value is multiplied by 10^9 to maintain compatibility with |
| 40 | # older versions of the interpreter. |
| 41 | return int(time.monotonic() * 1000000000) |
| 42 | |
| 43 | |
| 44 | _last_time = _get_time_ns() |
| 45 | |
| 46 | BABELTRACE_BIN="babeltrace2" |
| 47 | |
| 48 | class SessionInfo: |
| 49 | def __init__(self, handle, session_name, tmp_directory, channel_name): |
| 50 | self.handle = handle |
| 51 | self.name = session_name |
| 52 | self.tmp_directory = tmp_directory |
| 53 | self.trace_path = tmp_directory + "/" + session_name |
| 54 | self.channel_name = channel_name |
| 55 | |
| 56 | def bail(diag, session_info = None): |
| 57 | print("Bail out!") |
| 58 | print("#", diag) |
| 59 | |
| 60 | if session_info is not None: |
| 61 | stop_session(session_info, True) |
| 62 | |
| 63 | if os.path.exists(session_info.tmp_directory): |
| 64 | shutil.rmtree(session_info.tmp_directory) |
| 65 | exit(-1) |
| 66 | |
| 67 | def print_automatic_test_timing(): |
| 68 | global _time_tests |
| 69 | global _last_time |
| 70 | if not _time_tests: |
| 71 | return |
| 72 | duration_ns = _get_time_ns() - _last_time |
| 73 | print(" ---\n duration_ms: {:02f}\n ...".format(duration_ns / 1000000)) |
| 74 | _last_time = _get_time_ns() |
| 75 | |
| 76 | def print_test_result(result, number, description): |
| 77 | result_string = None |
| 78 | if result is True: |
| 79 | result_string = "ok" |
| 80 | else: |
| 81 | result_string = "not ok" |
| 82 | |
| 83 | result_string += " {0} - {1}".format(number, description) |
| 84 | print(result_string) |
| 85 | print_automatic_test_timing() |
| 86 | |
| 87 | def skip_test(number, description): |
| 88 | print('ok {} # skip {}'.format(number, description)) |
| 89 | print_automatic_test_timing() |
| 90 | |
| 91 | def enable_ust_tracepoint_event(session_info, event_name): |
| 92 | event = Event() |
| 93 | event.name = event_name |
| 94 | event.type = EVENT_TRACEPOINT |
| 95 | event.loglevel = EVENT_LOGLEVEL_ALL |
| 96 | res = enable_event(session_info.handle, event, session_info.channel_name) |
| 97 | if res < 0: |
| 98 | bail("Failed to enable userspace event " + event_name, session_info) |
| 99 | |
| 100 | def create_session(): |
| 101 | dom = Domain() |
| 102 | dom.type = DOMAIN_UST |
| 103 | |
| 104 | session_name = str(uuid.uuid1()) |
| 105 | tmp_directory = tempfile.mkdtemp() |
| 106 | trace_path = tmp_directory + "/" + session_name |
| 107 | |
| 108 | res = create(session_name, trace_path) |
| 109 | if res < 0: |
| 110 | bail("Failed to create recording session.") |
| 111 | |
| 112 | channel = Channel() |
| 113 | channel.name = "channel0" |
| 114 | channel_set_default_attr(dom, channel.attr) |
| 115 | |
| 116 | han = Handle(session_name, dom) |
| 117 | res = enable_channel(han, channel) |
| 118 | |
| 119 | session_info = SessionInfo(han, session_name, tmp_directory, channel.name) |
| 120 | if res < 0: |
| 121 | bail("Failed to enable channel " + channel.name, session_info) |
| 122 | return session_info |
| 123 | |
| 124 | def start_session(session_info): |
| 125 | start(session_info.name) |
| 126 | |
| 127 | def stop_session(session_info, bailing = False): |
| 128 | # Workaround lttng-ctl outputing directly to stdout by spawning a subprocess. |
| 129 | lttng_binary_path = os.path.dirname(os.path.abspath(__file__)) + "/" |
| 130 | for i in range(3): |
| 131 | lttng_binary_path = os.path.dirname(lttng_binary_path) |
| 132 | lttng_binary_path = lttng_binary_path + "/src/bin/lttng/lttng" |
| 133 | |
| 134 | retcode = subprocess.call([lttng_binary_path, "stop", session_info.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 135 | if retcode != 0 and not bailing: |
| 136 | bail("Unable to stop session " + session_info.name, session_info) |
| 137 | destroy(session_info.name) |