src.ctf.lttng-live: make `lttng_live_get_one_metadata_packet()` return status
[babeltrace.git] / src / plugins / ctf / lttng-live / metadata.c
1 /*
2 * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
3 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
6 * Some functions are based on older functions written by Mathieu Desnoyers.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_COMP_LOG_SELF_COMP self_comp
28 #define BT_LOG_OUTPUT_LEVEL log_level
29 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
30 #include "logging/comp-logging.h"
31
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <glib.h>
37 #include "compat/memstream.h"
38 #include <babeltrace2/babeltrace.h>
39
40 #include "metadata.h"
41 #include "../common/metadata/decoder.h"
42 #include "../common/metadata/ctf-meta-configure-ir-trace.h"
43
44 #define TSDL_MAGIC 0x75d11d57
45
46 struct packet_header {
47 uint32_t magic;
48 uint8_t uuid[16];
49 uint32_t checksum;
50 uint32_t content_size;
51 uint32_t packet_size;
52 uint8_t compression_scheme;
53 uint8_t encryption_scheme;
54 uint8_t checksum_scheme;
55 uint8_t major;
56 uint8_t minor;
57 } __attribute__((__packed__));
58
59
60 static
61 bool stream_classes_all_have_default_clock_class(bt_trace_class *tc,
62 bt_logging_level log_level,
63 bt_self_component *self_comp)
64 {
65 uint64_t i, sc_count;
66 const bt_clock_class *cc = NULL;
67 const bt_stream_class *sc;
68 bool ret = true;
69
70 sc_count = bt_trace_class_get_stream_class_count(tc);
71 for (i = 0; i < sc_count; i++) {
72 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
73
74 BT_ASSERT(sc);
75
76 cc = bt_stream_class_borrow_default_clock_class_const(sc);
77 if (!cc) {
78 ret = false;
79 BT_COMP_LOGE("Stream class doesn't have a default clock class: "
80 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
81 bt_stream_class_get_id(sc),
82 bt_stream_class_get_name(sc));
83 goto end;
84 }
85 }
86
87 end:
88 return ret;
89 }
90 /*
91 * Iterate over the stream classes and returns the first clock class
92 * encountered. This is useful to create message iterator inactivity message as
93 * we don't need a particular clock class.
94 */
95 static
96 const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc)
97 {
98 uint64_t i, sc_count;
99 const bt_clock_class *cc = NULL;
100 const bt_stream_class *sc;
101
102 sc_count = bt_trace_class_get_stream_class_count(tc);
103 for (i = 0; i < sc_count; i++) {
104 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
105 BT_ASSERT_DBG(sc);
106
107 cc = bt_stream_class_borrow_default_clock_class_const(sc);
108 if (cc) {
109 goto end;
110 }
111 }
112 end:
113 BT_ASSERT_DBG(cc);
114 return cc;
115 }
116
117 BT_HIDDEN
118 enum lttng_live_iterator_status lttng_live_metadata_update(
119 struct lttng_live_trace *trace)
120 {
121 struct lttng_live_session *session = trace->session;
122 struct lttng_live_metadata *metadata = trace->metadata;
123 size_t size, len_read = 0;
124 char *metadata_buf = NULL;
125 bool keep_receiving;
126 FILE *fp = NULL;
127 enum ctf_metadata_decoder_status decoder_status;
128 enum lttng_live_iterator_status status =
129 LTTNG_LIVE_ITERATOR_STATUS_OK;
130 bt_logging_level log_level = trace->log_level;
131 bt_self_component *self_comp = trace->self_comp;
132 enum lttng_live_get_one_metadata_status metadata_status;
133
134 /* No metadata stream yet. */
135 if (!metadata) {
136 if (session->new_streams_needed) {
137 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
138 } else {
139 session->new_streams_needed = true;
140 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
141 }
142 goto end;
143 }
144
145 if (!metadata->trace) {
146 trace->new_metadata_needed = false;
147 }
148
149 if (!trace->new_metadata_needed) {
150 goto end;
151 }
152
153 /* Open for writing */
154 fp = bt_open_memstream(&metadata_buf, &size);
155 if (!fp) {
156 BT_COMP_LOGE("Metadata open_memstream: %s", strerror(errno));
157 goto error;
158 }
159
160 keep_receiving = true;
161 /* Grab all available metadata. */
162 while (keep_receiving) {
163 size_t reply_len = 0;
164 /*
165 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
166 * for new metadata. If new metadata is received, the function
167 * writes it to the provided file handle and updates the
168 * reply_len output parameter. We call this function in loop
169 * until it returns _END meaning that no new metadata is
170 * available.
171 * We may receive a _CLOSED status if the metadata stream we
172 * are requesting is no longer available on the relay.
173 * If we receive an _ERROR status, it means there was a
174 * networking, allocating, or some other unrecoverable error.
175 */
176 metadata_status = lttng_live_get_one_metadata_packet(trace, fp,
177 &reply_len);
178
179 switch (metadata_status) {
180 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK:
181 len_read += reply_len;
182 break;
183 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END:
184 keep_receiving = false;
185 break;
186 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED:
187 keep_receiving = false;
188 break;
189 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR:
190 goto error;
191 default:
192 abort();
193 }
194 }
195
196 /*
197 * A closed metadata stream means the trace is no longer active. Return
198 * _END so that the caller can remove the trace from its list.
199 */
200 if (metadata_status == LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED) {
201 status = LTTNG_LIVE_ITERATOR_STATUS_END;
202 goto end;
203 }
204
205 if (bt_close_memstream(&metadata_buf, &size, fp)) {
206 BT_COMP_LOGE("bt_close_memstream: %s", strerror(errno));
207 }
208
209 fp = NULL;
210
211 if (len_read == 0) {
212 if (!trace->trace) {
213 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
214 goto end;
215 }
216 trace->new_metadata_needed = false;
217 goto end;
218 }
219
220 fp = bt_fmemopen(metadata_buf, len_read, "rb");
221 if (!fp) {
222 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s",
223 strerror(errno));
224 goto error;
225 }
226
227 /*
228 * The call to ctf_metadata_decoder_append_content() will append
229 * new metadata to our current trace class.
230 */
231 decoder_status = ctf_metadata_decoder_append_content(
232 metadata->decoder, fp);
233 switch (decoder_status) {
234 case CTF_METADATA_DECODER_STATUS_OK:
235 if (!trace->trace_class) {
236 struct ctf_trace_class *tc =
237 ctf_metadata_decoder_borrow_ctf_trace_class(
238 metadata->decoder);
239
240 trace->trace_class =
241 ctf_metadata_decoder_get_ir_trace_class(
242 metadata->decoder);
243 trace->trace = bt_trace_create(trace->trace_class);
244 if (!trace->trace) {
245 goto error;
246 }
247 if (ctf_trace_class_configure_ir_trace(tc,
248 trace->trace)) {
249 goto error;
250 }
251 if (!stream_classes_all_have_default_clock_class(
252 trace->trace_class, log_level,
253 self_comp)) {
254 /* Error logged in function. */
255 goto error;
256 }
257 trace->clock_class =
258 borrow_any_clock_class(trace->trace_class);
259 }
260 trace->new_metadata_needed = false;
261
262 break;
263 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
264 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
265 break;
266 default:
267 goto error;
268 }
269
270 goto end;
271
272 error:
273 if (errno == EINTR) {
274 if (lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
275 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
276 }
277 } else {
278 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
279 }
280 end:
281 if (fp) {
282 int closeret;
283
284 closeret = fclose(fp);
285 if (closeret) {
286 BT_COMP_LOGE("Error on fclose");
287 }
288 }
289 free(metadata_buf);
290 return status;
291 }
292
293 BT_HIDDEN
294 int lttng_live_metadata_create_stream(struct lttng_live_session *session,
295 uint64_t ctf_trace_id, uint64_t stream_id,
296 const char *trace_name)
297 {
298 struct lttng_live_metadata *metadata = NULL;
299 struct lttng_live_trace *trace;
300 struct ctf_metadata_decoder_config cfg = {
301 .log_level = session->log_level,
302 .self_comp = session->self_comp,
303 .clock_class_offset_s = 0,
304 .clock_class_offset_ns = 0,
305 .create_trace_class = true,
306 };
307
308 metadata = g_new0(struct lttng_live_metadata, 1);
309 if (!metadata) {
310 return -1;
311 }
312 metadata->log_level = session->log_level;
313 metadata->self_comp = session->self_comp;
314 metadata->stream_id = stream_id;
315
316 metadata->decoder = ctf_metadata_decoder_create(&cfg);
317 if (!metadata->decoder) {
318 goto error;
319 }
320 trace = lttng_live_borrow_trace(session, ctf_trace_id);
321 if (!trace) {
322 goto error;
323 }
324 metadata->trace = trace;
325 trace->metadata = metadata;
326 return 0;
327
328 error:
329 ctf_metadata_decoder_destroy(metadata->decoder);
330 g_free(metadata);
331 return -1;
332 }
333
334 BT_HIDDEN
335 void lttng_live_metadata_fini(struct lttng_live_trace *trace)
336 {
337 struct lttng_live_metadata *metadata = trace->metadata;
338
339 if (!metadata) {
340 return;
341 }
342 ctf_metadata_decoder_destroy(metadata->decoder);
343 trace->metadata = NULL;
344 g_free(metadata);
345 }
This page took 0.036028 seconds and 4 git commands to generate.