src.ctf.lttng-live: use the new metadata stream parser and message iterator
[deliverable/babeltrace.git] / src / plugins / ctf / lttng-live / metadata.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
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
7 */
8
9 #define BT_CLOG_CFG logCfg
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
11
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <glib.h>
17 #include "compat/memstream.h"
18 #include <babeltrace2/babeltrace.h>
19
20 #include "cpp-common/libc-up.hpp"
21
22 #include "metadata.hpp"
23 #include "../common/src/metadata/tsdl/ctf-meta-configure-ir-trace.hpp"
24 #include "cpp-common/cfg-logging.hpp"
25 #include "cpp-common/cfg-logging-error-reporting.hpp"
26
27 #define TSDL_MAGIC 0x75d11d57
28
29 struct packet_header
30 {
31 uint32_t magic;
32 uint8_t uuid[16];
33 uint32_t checksum;
34 uint32_t content_size;
35 uint32_t packet_size;
36 uint8_t compression_scheme;
37 uint8_t encryption_scheme;
38 uint8_t checksum_scheme;
39 uint8_t major;
40 uint8_t minor;
41 } __attribute__((__packed__));
42
43 static bool stream_classes_all_have_default_clock_class(bt2::ConstTraceClass tc,
44 const bt2_common::LogCfg& logCfg)
45 {
46 for (std::uint64_t i = 0; i < tc.size(); ++i) {
47 bt2::ConstStreamClass sc = tc[i];
48 nonstd::optional<bt2::ConstClockClass> cc = sc.defaultClockClass();
49
50 if (!cc) {
51 BT_CLOGE_APPEND_CAUSE("Stream class doesn't have a default clock class: "
52 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
53 sc.id(), sc.name()->c_str());
54 return false;
55 }
56 }
57
58 return true;
59 }
60 /*
61 * Iterate over the stream classes and returns the first clock class
62 * encountered. This is useful to create message iterator inactivity message as
63 * we don't need a particular clock class.
64 */
65 static bt2::ConstClockClass borrow_any_clock_class(bt2::ConstTraceClass tc)
66 {
67 return *tc[0].defaultClockClass();
68 }
69
70 BT_HIDDEN
71 enum lttng_live_iterator_status lttng_live_metadata_update(struct lttng_live_trace *trace)
72 {
73 struct lttng_live_session *session = trace->session;
74 struct lttng_live_metadata *metadata = trace->metadata.get();
75 bool keep_receiving;
76 const bt2_common::LogCfg& logCfg = trace->logCfg;
77 enum lttng_live_get_one_metadata_status metadata_status;
78
79 BT_CLOGD("Updating metadata for trace: session-id=%" PRIu64 ", trace-id=%" PRIu64, session->id,
80 trace->id);
81
82 /* No metadata stream yet. */
83 if (!metadata) {
84 if (session->closed) {
85 /*
86 * The session is closed AND we never received any
87 * metadata this indicates that we will never receive
88 * any metadata.
89 */
90 return LTTNG_LIVE_ITERATOR_STATUS_END;
91 } else if (session->new_streams_needed) {
92 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
93 } else {
94 session->new_streams_needed = true;
95 return LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
96 }
97 }
98
99 if (trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) {
100 return LTTNG_LIVE_ITERATOR_STATUS_OK;
101 }
102
103 keep_receiving = true;
104 /* Grab all available metadata. */
105 std::vector<uint8_t> metadataBuf;
106 while (keep_receiving) {
107 /*
108 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
109 * for new metadata. If new metadata is received, the function
110 * writes it to the provided file handle and updates the
111 * reply_len output parameter. We call this function in loop
112 * until it returns _END meaning that no new metadata is
113 * available.
114 * We may receive a _CLOSED status if the metadata stream we
115 * are requesting is no longer available on the relay.
116 * If we receive an _ERROR status, it means there was a
117 * networking, allocating, or some other unrecoverable error.
118 */
119 metadata_status = lttng_live_get_one_metadata_packet(trace, metadataBuf);
120
121 switch (metadata_status) {
122 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK:
123 break;
124 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END:
125 keep_receiving = false;
126 break;
127 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED:
128 BT_CLOGD("Metadata stream was closed by the Relay, the trace is no longer active: "
129 "trace-id=%" PRIu64 ", metadata-stream-id=%" PRIu64,
130 trace->id, metadata->stream_id);
131 /*
132 * The stream was closed and we received everything
133 * there was to receive for this metadata stream.
134 * We go on with the decoding of what we received. So
135 * that data stream can be decoded.
136 */
137 keep_receiving = false;
138 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED;
139 break;
140 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR:
141 BT_CLOGE_APPEND_CAUSE("Error getting one trace metadata packet: "
142 "trace-id=%" PRIu64,
143 trace->id);
144 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
145 default:
146 bt_common_abort();
147 }
148 }
149
150 if (metadataBuf.empty()) {
151 if (!trace->trace) {
152 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
153 }
154
155 /* The relay sent zero bytes of metdata. */
156 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
157 return LTTNG_LIVE_ITERATOR_STATUS_OK;
158 }
159
160 /*
161 * The call to ctf_metadata_decoder_append_content() will append
162 * new metadata to our current trace class.
163 */
164 BT_CLOGD("Appending new metadata to the ctf_trace class");
165 metadata->parseSection(metadataBuf.data(), metadataBuf.data() + metadataBuf.size());
166 if (!trace->trace) {
167 const ctf::src::TraceCls *ctfTraceCls = metadata->traceCls();
168 BT_ASSERT(ctfTraceCls);
169 nonstd::optional<bt2::TraceClass> irTraceCls = ctfTraceCls->libCls();
170
171 if (irTraceCls) {
172 trace->trace = irTraceCls->instantiate();
173
174 ctf_trace_class_configure_ir_trace(*ctfTraceCls, **trace->trace, logCfg);
175
176 if (!stream_classes_all_have_default_clock_class((*trace->trace)->cls(), logCfg)) {
177 /* Error logged in function. */
178 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
179 }
180
181 trace->clock_class = borrow_any_clock_class((*trace->trace)->cls());
182 }
183 }
184
185 /* The metadata was updated succesfully. */
186 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
187
188 return LTTNG_LIVE_ITERATOR_STATUS_OK;
189 }
190
191 BT_HIDDEN
192 int lttng_live_metadata_create_stream(struct lttng_live_session *session, uint64_t ctf_trace_id,
193 uint64_t stream_id, const char *trace_name)
194 {
195 const bt2_common::LogCfg& logCfg = session->logCfg;
196 struct lttng_live_trace *trace;
197 lttng_live_metadata::UP metadata =
198 bt2_common::makeUnique<lttng_live_metadata>(session->self_comp, logCfg);
199
200 metadata->stream_id = stream_id;
201
202 trace = lttng_live_session_borrow_or_create_trace_by_id(session, ctf_trace_id);
203 if (!trace) {
204 BT_CLOGE_APPEND_CAUSE("Failed to borrow trace");
205 return -1;
206 }
207 trace->metadata = std::move(metadata);
208 return 0;
209 }
This page took 0.035707 seconds and 5 git commands to generate.