Commit | Line | Data |
---|---|---|
7cdc2bab | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
7cdc2bab | 3 | * |
0235b0db MJ |
4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
5 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> | |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation | |
7cdc2bab MD |
7 | */ |
8 | ||
2ece7dd0 | 9 | #define BT_COMP_LOG_SELF_COMP self_comp |
4164020e SM |
10 | #define BT_LOG_OUTPUT_LEVEL log_level |
11 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META" | |
d9c39b0a | 12 | #include "logging/comp-logging.h" |
020bc26f | 13 | |
7cdc2bab MD |
14 | #include <stdio.h> |
15 | #include <stdint.h> | |
16 | #include <stdlib.h> | |
17 | #include <stdbool.h> | |
18 | #include <glib.h> | |
578e048b | 19 | #include "compat/memstream.h" |
3fadfbc0 | 20 | #include <babeltrace2/babeltrace.h> |
7cdc2bab | 21 | |
087cd0f5 SM |
22 | #include "metadata.hpp" |
23 | #include "../common/metadata/decoder.hpp" | |
24 | #include "../common/metadata/ctf-meta-configure-ir-trace.hpp" | |
7cdc2bab | 25 | |
4164020e | 26 | #define TSDL_MAGIC 0x75d11d57 |
7cdc2bab | 27 | |
4164020e SM |
28 | struct packet_header |
29 | { | |
30 | uint32_t magic; | |
31 | uint8_t uuid[16]; | |
32 | uint32_t checksum; | |
33 | uint32_t content_size; | |
34 | uint32_t packet_size; | |
35 | uint8_t compression_scheme; | |
36 | uint8_t encryption_scheme; | |
37 | uint8_t checksum_scheme; | |
38 | uint8_t major; | |
39 | uint8_t minor; | |
40 | } __attribute__((__packed__)); | |
14f28187 | 41 | |
4164020e SM |
42 | static bool stream_classes_all_have_default_clock_class(bt_trace_class *tc, |
43 | bt_logging_level log_level, | |
44 | bt_self_component *self_comp) | |
7cdc2bab | 45 | { |
4164020e SM |
46 | uint64_t i, sc_count; |
47 | const bt_clock_class *cc = NULL; | |
48 | const bt_stream_class *sc; | |
49 | bool ret = true; | |
50 | ||
51 | sc_count = bt_trace_class_get_stream_class_count(tc); | |
52 | for (i = 0; i < sc_count; i++) { | |
53 | sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i); | |
54 | ||
55 | BT_ASSERT(sc); | |
56 | ||
57 | cc = bt_stream_class_borrow_default_clock_class_const(sc); | |
58 | if (!cc) { | |
59 | ret = false; | |
60 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
61 | "Stream class doesn't have a default clock class: " | |
62 | "sc-id=%" PRIu64 ", sc-name=\"%s\"", | |
63 | bt_stream_class_get_id(sc), bt_stream_class_get_name(sc)); | |
64 | goto end; | |
65 | } | |
66 | } | |
7cdc2bab | 67 | |
7cdc2bab | 68 | end: |
4164020e | 69 | return ret; |
14f28187 FD |
70 | } |
71 | /* | |
72 | * Iterate over the stream classes and returns the first clock class | |
73 | * encountered. This is useful to create message iterator inactivity message as | |
74 | * we don't need a particular clock class. | |
75 | */ | |
4164020e | 76 | static const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc) |
14f28187 | 77 | { |
4164020e SM |
78 | uint64_t i, sc_count; |
79 | const bt_clock_class *cc = NULL; | |
80 | const bt_stream_class *sc; | |
81 | ||
82 | sc_count = bt_trace_class_get_stream_class_count(tc); | |
83 | for (i = 0; i < sc_count; i++) { | |
84 | sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i); | |
85 | BT_ASSERT_DBG(sc); | |
86 | ||
87 | cc = bt_stream_class_borrow_default_clock_class_const(sc); | |
88 | if (cc) { | |
89 | goto end; | |
90 | } | |
91 | } | |
14f28187 | 92 | end: |
4164020e SM |
93 | BT_ASSERT_DBG(cc); |
94 | return cc; | |
7cdc2bab MD |
95 | } |
96 | ||
97 | BT_HIDDEN | |
4164020e | 98 | enum lttng_live_iterator_status lttng_live_metadata_update(struct lttng_live_trace *trace) |
7cdc2bab | 99 | { |
4164020e SM |
100 | struct lttng_live_session *session = trace->session; |
101 | struct lttng_live_metadata *metadata = trace->metadata; | |
102 | size_t size, len_read = 0; | |
103 | char *metadata_buf = NULL; | |
104 | bool keep_receiving; | |
105 | FILE *fp = NULL; | |
106 | enum ctf_metadata_decoder_status decoder_status; | |
107 | enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
108 | bt_logging_level log_level = trace->log_level; | |
109 | bt_self_component *self_comp = trace->self_comp; | |
110 | enum lttng_live_get_one_metadata_status metadata_status; | |
111 | ||
112 | BT_COMP_LOGD("Updating metadata for trace: session-id=%" PRIu64 ", trace-id=%" PRIu64, | |
113 | session->id, trace->id); | |
114 | ||
115 | /* No metadata stream yet. */ | |
116 | if (!metadata) { | |
117 | if (session->new_streams_needed) { | |
118 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
119 | } else { | |
120 | session->new_streams_needed = true; | |
121 | status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
122 | } | |
123 | goto end; | |
124 | } | |
125 | ||
126 | if (trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { | |
127 | goto end; | |
128 | } | |
129 | ||
130 | /* | |
131 | * Open a new write only file handle to populate the `metadata_buf` | |
132 | * memory buffer so we can write in loop in it easily. | |
133 | */ | |
134 | fp = bt_open_memstream(&metadata_buf, &size); | |
135 | if (!fp) { | |
136 | if (errno == EINTR && lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) { | |
137 | session->lttng_live_msg_iter->was_interrupted = true; | |
138 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
139 | } else { | |
140 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, "Metadata open_memstream", "."); | |
141 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
142 | } | |
143 | goto end; | |
144 | } | |
145 | ||
146 | keep_receiving = true; | |
147 | /* Grab all available metadata. */ | |
148 | while (keep_receiving) { | |
149 | size_t reply_len = 0; | |
150 | /* | |
151 | * lttng_live_get_one_metadata_packet() asks the Relay Daemon | |
152 | * for new metadata. If new metadata is received, the function | |
153 | * writes it to the provided file handle and updates the | |
154 | * reply_len output parameter. We call this function in loop | |
155 | * until it returns _END meaning that no new metadata is | |
156 | * available. | |
157 | * We may receive a _CLOSED status if the metadata stream we | |
158 | * are requesting is no longer available on the relay. | |
159 | * If we receive an _ERROR status, it means there was a | |
160 | * networking, allocating, or some other unrecoverable error. | |
161 | */ | |
162 | metadata_status = lttng_live_get_one_metadata_packet(trace, fp, &reply_len); | |
163 | ||
164 | switch (metadata_status) { | |
165 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK: | |
166 | len_read += reply_len; | |
167 | break; | |
168 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END: | |
169 | keep_receiving = false; | |
170 | break; | |
171 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED: | |
172 | BT_COMP_LOGD("Metadata stream was closed by the Relay, the trace is no longer active: " | |
173 | "trace-id=%" PRIu64 ", metadata-stream-id=%" PRIu64, | |
174 | trace->id, metadata->stream_id); | |
175 | /* | |
176 | * The stream was closed and we received everything | |
177 | * there was to receive for this metadata stream. | |
178 | * We go on with the decoding of what we received. So | |
179 | * that data stream can be decoded. | |
180 | */ | |
181 | keep_receiving = false; | |
182 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED; | |
183 | break; | |
184 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR: | |
185 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
186 | "Error getting one trace metadata packet: " | |
187 | "trace-id=%" PRIu64, | |
188 | trace->id); | |
189 | goto error; | |
190 | default: | |
191 | bt_common_abort(); | |
192 | } | |
193 | } | |
194 | ||
195 | /* The memory buffer `metadata_buf` contains all the metadata. */ | |
196 | if (bt_close_memstream(&metadata_buf, &size, fp)) { | |
197 | BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", "."); | |
198 | } | |
199 | ||
200 | fp = NULL; | |
201 | ||
202 | if (len_read == 0) { | |
203 | if (!trace->trace) { | |
204 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
205 | goto end; | |
206 | } | |
207 | ||
208 | /* The relay sent zero bytes of metdata. */ | |
209 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED; | |
210 | goto end; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Open a new reading file handle on the `metadata_buf` and pass it to | |
215 | * the metadata decoder. | |
216 | */ | |
217 | fp = bt_fmemopen(metadata_buf, len_read, "rb"); | |
218 | if (!fp) { | |
219 | if (errno == EINTR && lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) { | |
220 | session->lttng_live_msg_iter->was_interrupted = true; | |
221 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
222 | } else { | |
223 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, "Cannot memory-open metadata buffer", "."); | |
224 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
225 | } | |
226 | goto end; | |
227 | } | |
228 | ||
229 | /* | |
230 | * The call to ctf_metadata_decoder_append_content() will append | |
231 | * new metadata to our current trace class. | |
232 | */ | |
233 | BT_COMP_LOGD("Appending new metadata to the ctf_trace class"); | |
234 | decoder_status = ctf_metadata_decoder_append_content(metadata->decoder, fp); | |
235 | switch (decoder_status) { | |
236 | case CTF_METADATA_DECODER_STATUS_OK: | |
237 | if (!trace->trace_class) { | |
238 | struct ctf_trace_class *tc = | |
239 | ctf_metadata_decoder_borrow_ctf_trace_class(metadata->decoder); | |
240 | ||
241 | trace->trace_class = ctf_metadata_decoder_get_ir_trace_class(metadata->decoder); | |
242 | trace->trace = bt_trace_create(trace->trace_class); | |
243 | if (!trace->trace) { | |
244 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create bt_trace"); | |
245 | goto error; | |
246 | } | |
247 | if (ctf_trace_class_configure_ir_trace(tc, trace->trace)) { | |
248 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to configure ctf trace class"); | |
249 | goto error; | |
250 | } | |
251 | if (!stream_classes_all_have_default_clock_class(trace->trace_class, log_level, | |
252 | self_comp)) { | |
253 | /* Error logged in function. */ | |
254 | goto error; | |
255 | } | |
256 | trace->clock_class = borrow_any_clock_class(trace->trace_class); | |
257 | } | |
258 | ||
259 | /* The metadata was updated succesfully. */ | |
260 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED; | |
261 | ||
262 | break; | |
263 | default: | |
264 | goto error; | |
265 | } | |
266 | ||
267 | goto end; | |
c28512ab | 268 | |
7cdc2bab | 269 | error: |
4164020e | 270 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
7cdc2bab | 271 | end: |
4164020e SM |
272 | if (fp) { |
273 | int closeret; | |
274 | ||
275 | closeret = fclose(fp); | |
276 | if (closeret) { | |
277 | BT_COMP_LOGW_ERRNO("Error on fclose", "."); | |
278 | } | |
279 | } | |
280 | free(metadata_buf); | |
281 | return status; | |
7cdc2bab MD |
282 | } |
283 | ||
284 | BT_HIDDEN | |
4164020e SM |
285 | int lttng_live_metadata_create_stream(struct lttng_live_session *session, uint64_t ctf_trace_id, |
286 | uint64_t stream_id, const char *trace_name) | |
7cdc2bab | 287 | { |
4164020e SM |
288 | bt_self_component *self_comp = session->self_comp; |
289 | bt_logging_level log_level = session->log_level; | |
290 | struct lttng_live_metadata *metadata = NULL; | |
291 | struct lttng_live_trace *trace; | |
292 | ||
293 | ctf_metadata_decoder_config cfg {}; | |
294 | cfg.log_level = session->log_level; | |
295 | cfg.self_comp = session->self_comp; | |
296 | cfg.clock_class_offset_s = 0; | |
297 | cfg.clock_class_offset_ns = 0; | |
298 | cfg.create_trace_class = true; | |
299 | ||
300 | metadata = g_new0(struct lttng_live_metadata, 1); | |
301 | if (!metadata) { | |
302 | return -1; | |
303 | } | |
304 | metadata->log_level = session->log_level; | |
305 | metadata->self_comp = session->self_comp; | |
306 | metadata->stream_id = stream_id; | |
307 | ||
308 | metadata->decoder = ctf_metadata_decoder_create(&cfg); | |
309 | if (!metadata->decoder) { | |
310 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create CTF metadata decoder"); | |
311 | goto error; | |
312 | } | |
313 | trace = lttng_live_session_borrow_or_create_trace_by_id(session, ctf_trace_id); | |
314 | if (!trace) { | |
315 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to borrow trace"); | |
316 | goto error; | |
317 | } | |
318 | trace->metadata = metadata; | |
319 | return 0; | |
7cdc2bab MD |
320 | |
321 | error: | |
4164020e SM |
322 | ctf_metadata_decoder_destroy(metadata->decoder); |
323 | g_free(metadata); | |
324 | return -1; | |
7cdc2bab MD |
325 | } |
326 | ||
327 | BT_HIDDEN | |
328 | void lttng_live_metadata_fini(struct lttng_live_trace *trace) | |
329 | { | |
4164020e SM |
330 | struct lttng_live_metadata *metadata = trace->metadata; |
331 | ||
332 | if (!metadata) { | |
333 | return; | |
334 | } | |
335 | ctf_metadata_decoder_destroy(metadata->decoder); | |
336 | trace->metadata = NULL; | |
337 | g_free(metadata); | |
7cdc2bab | 338 | } |