Commit | Line | Data |
---|---|---|
cfb007a4 JG |
1 | #!/usr/bin/env python3 |
2 | # | |
9d16b343 | 3 | # Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
cfb007a4 | 4 | # |
9d16b343 | 5 | # SPDX-License-Identifier: GPL-2.0-only |
cfb007a4 | 6 | # |
cfb007a4 JG |
7 | |
8 | import uuid | |
9 | import os | |
10 | import subprocess | |
11 | import shutil | |
12 | import sys | |
2a69bf14 | 13 | import time |
cfb007a4 JG |
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 | ||
2a69bf14 | 26 | _time_tests = True |
83dc3923 | 27 | if os.getenv("LTTNG_TESTS_TAP_AUTOTIME", "1") == "0": |
2a69bf14 | 28 | _time_tests = False |
f7169e41 | 29 | |
b26c3b64 | 30 | |
f7169e41 | 31 | def _get_time_ns(): |
b26c3b64 JG |
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) | |
f7169e41 KS |
42 | |
43 | ||
44 | _last_time = _get_time_ns() | |
2a69bf14 | 45 | |
6a871bbe KS |
46 | BABELTRACE_BIN = "babeltrace2" |
47 | ||
cfb007a4 JG |
48 | |
49 | class SessionInfo: | |
50 | def __init__(self, handle, session_name, tmp_directory, channel_name): | |
51 | self.handle = handle | |
52 | self.name = session_name | |
53 | self.tmp_directory = tmp_directory | |
54 | self.trace_path = tmp_directory + "/" + session_name | |
55 | self.channel_name = channel_name | |
56 | ||
6a871bbe KS |
57 | |
58 | def bail(diag, session_info=None): | |
cfb007a4 JG |
59 | print("Bail out!") |
60 | print("#", diag) | |
61 | ||
62 | if session_info is not None: | |
63 | stop_session(session_info, True) | |
64 | ||
7f2841b7 JG |
65 | if os.path.exists(session_info.tmp_directory): |
66 | shutil.rmtree(session_info.tmp_directory) | |
cfb007a4 JG |
67 | exit(-1) |
68 | ||
6a871bbe | 69 | |
2a69bf14 KS |
70 | def print_automatic_test_timing(): |
71 | global _time_tests | |
72 | global _last_time | |
73 | if not _time_tests: | |
74 | return | |
f7169e41 KS |
75 | duration_ns = _get_time_ns() - _last_time |
76 | print(" ---\n duration_ms: {:02f}\n ...".format(duration_ns / 1000000)) | |
77 | _last_time = _get_time_ns() | |
2a69bf14 | 78 | |
6a871bbe | 79 | |
cfb007a4 JG |
80 | def print_test_result(result, number, description): |
81 | result_string = None | |
82 | if result is True: | |
83 | result_string = "ok" | |
84 | else: | |
85 | result_string = "not ok" | |
86 | ||
87 | result_string += " {0} - {1}".format(number, description) | |
88 | print(result_string) | |
2a69bf14 | 89 | print_automatic_test_timing() |
cfb007a4 | 90 | |
6a871bbe | 91 | |
bc1d8ca0 | 92 | def skip_test(number, description): |
6a871bbe | 93 | print("ok {} # skip {}".format(number, description)) |
2a69bf14 | 94 | print_automatic_test_timing() |
bc1d8ca0 | 95 | |
6a871bbe | 96 | |
cfb007a4 JG |
97 | def enable_ust_tracepoint_event(session_info, event_name): |
98 | event = Event() | |
99 | event.name = event_name | |
100 | event.type = EVENT_TRACEPOINT | |
101 | event.loglevel = EVENT_LOGLEVEL_ALL | |
102 | res = enable_event(session_info.handle, event, session_info.channel_name) | |
103 | if res < 0: | |
104 | bail("Failed to enable userspace event " + event_name, session_info) | |
105 | ||
6a871bbe | 106 | |
cfb007a4 JG |
107 | def create_session(): |
108 | dom = Domain() | |
109 | dom.type = DOMAIN_UST | |
110 | ||
111 | session_name = str(uuid.uuid1()) | |
112 | tmp_directory = tempfile.mkdtemp() | |
113 | trace_path = tmp_directory + "/" + session_name | |
114 | ||
115 | res = create(session_name, trace_path) | |
116 | if res < 0: | |
e9711845 | 117 | bail("Failed to create recording session.") |
cfb007a4 JG |
118 | |
119 | channel = Channel() | |
120 | channel.name = "channel0" | |
121 | channel_set_default_attr(dom, channel.attr) | |
122 | ||
123 | han = Handle(session_name, dom) | |
124 | res = enable_channel(han, channel) | |
125 | ||
126 | session_info = SessionInfo(han, session_name, tmp_directory, channel.name) | |
127 | if res < 0: | |
128 | bail("Failed to enable channel " + channel.name, session_info) | |
129 | return session_info | |
130 | ||
6a871bbe | 131 | |
cfb007a4 JG |
132 | def start_session(session_info): |
133 | start(session_info.name) | |
134 | ||
6a871bbe KS |
135 | |
136 | def stop_session(session_info, bailing=False): | |
cfb007a4 JG |
137 | # Workaround lttng-ctl outputing directly to stdout by spawning a subprocess. |
138 | lttng_binary_path = os.path.dirname(os.path.abspath(__file__)) + "/" | |
139 | for i in range(3): | |
140 | lttng_binary_path = os.path.dirname(lttng_binary_path) | |
141 | lttng_binary_path = lttng_binary_path + "/src/bin/lttng/lttng" | |
142 | ||
6a871bbe KS |
143 | retcode = subprocess.call( |
144 | [lttng_binary_path, "stop", session_info.name], | |
145 | stdout=subprocess.PIPE, | |
146 | stderr=subprocess.PIPE, | |
147 | ) | |
cfb007a4 JG |
148 | if retcode != 0 and not bailing: |
149 | bail("Unable to stop session " + session_info.name, session_info) | |
150 | destroy(session_info.name) |