ctf: refactor metadata decoder to always have an instance
[babeltrace.git] / src / plugins / ctf / fs-src / query.c
1 /*
2 * query.c
3 *
4 * Babeltrace CTF file system Reader Component queries
5 *
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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_LOG_OUTPUT_LEVEL log_level
28 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY"
29 #include "logging/log.h"
30
31 #include "query.h"
32 #include <stdbool.h>
33 #include "common/assert.h"
34 #include "metadata.h"
35 #include "../common/metadata/decoder.h"
36 #include "common/common.h"
37 #include "common/macros.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "fs.h"
40 #include "logging/comp-logging.h"
41
42 #define METADATA_TEXT_SIG "/* CTF 1.8"
43
44 struct range {
45 int64_t begin_ns;
46 int64_t end_ns;
47 bool set;
48 };
49
50 BT_HIDDEN
51 bt_component_class_query_method_status metadata_info_query(
52 bt_self_component_class_source *self_comp_class_src,
53 const bt_value *params, bt_logging_level log_level,
54 const bt_value **user_result)
55 {
56 bt_component_class_query_method_status status =
57 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
58 bt_self_component_class *self_comp_class =
59 bt_self_component_class_source_as_self_component_class(self_comp_class_src);
60 bt_value *result = NULL;
61 const bt_value *path_value = NULL;
62 FILE *metadata_fp = NULL;
63 int ret;
64 int bo;
65 const char *path;
66 bool is_packetized;
67 struct ctf_metadata_decoder *decoder = NULL;
68 struct ctf_metadata_decoder_config decoder_cfg = { 0 };
69 enum ctf_metadata_decoder_status decoder_status;
70
71 result = bt_value_map_create();
72 if (!result) {
73 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
74 goto error;
75 }
76
77 BT_ASSERT(params);
78
79 if (!bt_value_is_map(params)) {
80 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
81 "Query parameters is not a map value object.");
82 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
83 goto error;
84 }
85
86 path_value = bt_value_map_borrow_entry_value_const(params, "path");
87 if (!path_value) {
88 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
89 "Mandatory `path` parameter missing");
90 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
91 goto error;
92 }
93
94 if (!bt_value_is_string(path_value)) {
95 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
96 "`path` parameter is required to be a string value");
97 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
98 goto error;
99 }
100
101 path = bt_value_string_get(path_value);
102
103 BT_ASSERT(path);
104 metadata_fp = ctf_fs_metadata_open_file(path);
105 if (!metadata_fp) {
106 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
107 "Cannot open trace metadata: path=\"%s\".", path);
108 goto error;
109 }
110
111 ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized,
112 &bo, log_level, NULL);
113 if (ret) {
114 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
115 "Cannot check whether or not the metadata stream is packetized: path=\"%s\".",
116 path);
117 goto error;
118 }
119
120 decoder_cfg.log_level = log_level;
121 decoder_cfg.keep_plain_text = true;
122 decoder = ctf_metadata_decoder_create(&decoder_cfg);
123 if (!decoder) {
124 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
125 "Cannot create metadata decoder: path=\"%s\".", path);
126 goto error;
127 }
128
129 rewind(metadata_fp);
130 decoder_status = ctf_metadata_decoder_append_content(decoder,
131 metadata_fp);
132 if (decoder_status) {
133 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
134 "Cannot update metadata decoder's content: path=\"%s\".",
135 path);
136 goto error;
137 }
138
139 ret = bt_value_map_insert_string_entry(result, "text",
140 ctf_metadata_decoder_get_text(decoder));
141 if (ret) {
142 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
143 "Cannot insert metadata text into query result.");
144 goto error;
145 }
146
147 ret = bt_value_map_insert_bool_entry(result, "is-packetized",
148 is_packetized);
149 if (ret) {
150 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
151 "Cannot insert \"is-packetized\" attribute into query result.");
152 goto error;
153 }
154
155 goto end;
156
157 error:
158 BT_VALUE_PUT_REF_AND_RESET(result);
159 result = NULL;
160
161 if (status >= 0) {
162 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
163 }
164
165 end:
166 ctf_metadata_decoder_destroy(decoder);
167
168 if (metadata_fp) {
169 fclose(metadata_fp);
170 }
171
172 *user_result = result;
173 return status;
174 }
175
176 static
177 int add_range(bt_value *info, struct range *range,
178 const char *range_name)
179 {
180 int ret = 0;
181 bt_value_map_insert_entry_status status;
182 bt_value *range_map;
183
184 if (!range->set) {
185 /* Not an error. */
186 goto end;
187 }
188
189 status = bt_value_map_insert_empty_map_entry(info, range_name,
190 &range_map);
191 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
192 ret = -1;
193 goto end;
194 }
195
196 status = bt_value_map_insert_signed_integer_entry(range_map, "begin",
197 range->begin_ns);
198 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
199 ret = -1;
200 goto end;
201 }
202
203 status = bt_value_map_insert_signed_integer_entry(range_map, "end",
204 range->end_ns);
205 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
206 ret = -1;
207 goto end;
208 }
209
210 end:
211 return ret;
212 }
213
214 static
215 int populate_stream_info(struct ctf_fs_ds_file_group *group,
216 bt_value *group_info, struct range *stream_range)
217 {
218 int ret = 0;
219 bt_value_map_insert_entry_status insert_status;
220 struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
221 gchar *port_name = NULL;
222
223 /*
224 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
225 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
226 * the timestamp_begin of the first index entry and the timestamp_end
227 * of the last index entry.
228 */
229 BT_ASSERT(group->index);
230 BT_ASSERT(group->index->entries);
231 BT_ASSERT(group->index->entries->len > 0);
232
233 /* First entry. */
234 first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
235 group->index->entries, 0);
236
237 /* Last entry. */
238 last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
239 group->index->entries, group->index->entries->len - 1);
240
241 stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns;
242 stream_range->end_ns = last_ds_index_entry->timestamp_end_ns;
243
244 /*
245 * If any of the begin and end timestamps is not set it means that
246 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
247 * in their packet context so we can't set the range.
248 */
249 stream_range->set = stream_range->begin_ns != UINT64_C(-1) &&
250 stream_range->end_ns != UINT64_C(-1);
251
252 ret = add_range(group_info, stream_range, "range-ns");
253 if (ret) {
254 goto end;
255 }
256
257 port_name = ctf_fs_make_port_name(group);
258 if (!port_name) {
259 ret = -1;
260 goto end;
261 }
262
263 insert_status = bt_value_map_insert_string_entry(group_info,
264 "port-name", port_name);
265 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
266 ret = -1;
267 goto end;
268 }
269
270 end:
271 g_free(port_name);
272 return ret;
273 }
274
275 static
276 int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info)
277 {
278 int ret = 0;
279 size_t group_idx;
280 bt_value_map_insert_entry_status insert_status;
281 bt_value_array_append_element_status append_status;
282 bt_value *file_groups = NULL;
283 struct range trace_intersection = {
284 .begin_ns = 0,
285 .end_ns = INT64_MAX,
286 .set = false,
287 };
288
289 BT_ASSERT(trace->ds_file_groups);
290 /* Add trace range info only if it contains streams. */
291 if (trace->ds_file_groups->len == 0) {
292 ret = -1;
293 goto end;
294 }
295
296 insert_status = bt_value_map_insert_empty_array_entry(trace_info,
297 "streams", &file_groups);
298 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
299 ret = -1;
300 goto end;
301 }
302
303 /* Find range of all stream groups, and of the trace. */
304 for (group_idx = 0; group_idx < trace->ds_file_groups->len;
305 group_idx++) {
306 bt_value *group_info;
307 struct range group_range = { .set = false };
308 struct ctf_fs_ds_file_group *group = g_ptr_array_index(
309 trace->ds_file_groups, group_idx);
310
311 append_status = bt_value_array_append_empty_map_element(
312 file_groups, &group_info);
313 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
314 ret = -1;
315 goto end;
316 }
317
318 ret = populate_stream_info(group, group_info, &group_range);
319 if (ret) {
320 goto end;
321 }
322
323 if (group_range.set) {
324 trace_intersection.begin_ns = MAX(trace_intersection.begin_ns,
325 group_range.begin_ns);
326 trace_intersection.end_ns = MIN(trace_intersection.end_ns,
327 group_range.end_ns);
328 trace_intersection.set = true;
329 }
330 }
331
332 if (trace_intersection.begin_ns < trace_intersection.end_ns) {
333 ret = add_range(trace_info, &trace_intersection,
334 "intersection-range-ns");
335 if (ret) {
336 goto end;
337 }
338 }
339
340 end:
341 return ret;
342 }
343
344 BT_HIDDEN
345 bt_component_class_query_method_status trace_info_query(
346 bt_self_component_class_source *self_comp_class_src,
347 const bt_value *params, bt_logging_level log_level,
348 const bt_value **user_result)
349 {
350 struct ctf_fs_component *ctf_fs = NULL;
351 bt_component_class_query_method_status status =
352 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
353 bt_self_component_class *self_comp_class =
354 bt_self_component_class_source_as_self_component_class(
355 self_comp_class_src);
356 bt_value *result = NULL;
357 const bt_value *inputs_value = NULL;
358 int ret = 0;
359 guint i;
360
361 BT_ASSERT(params);
362
363 if (!bt_value_is_map(params)) {
364 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
365 "Query parameters is not a map value object.");
366 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
367 goto error;
368 }
369
370 ctf_fs = ctf_fs_component_create(log_level, NULL);
371 if (!ctf_fs) {
372 goto error;
373 }
374
375 if (!read_src_fs_parameters(params, &inputs_value, ctf_fs, NULL,
376 self_comp_class)) {
377 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
378 goto error;
379 }
380
381 if (ctf_fs_component_create_ctf_fs_traces(ctf_fs, inputs_value, NULL,
382 self_comp_class)) {
383 goto error;
384 }
385
386 result = bt_value_array_create();
387 if (!result) {
388 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
389 goto error;
390 }
391
392 for (i = 0; i < ctf_fs->traces->len; i++) {
393 struct ctf_fs_trace *trace;
394 bt_value *trace_info;
395 bt_value_array_append_element_status append_status;
396
397 trace = g_ptr_array_index(ctf_fs->traces, i);
398 BT_ASSERT(trace);
399
400 append_status = bt_value_array_append_empty_map_element(result,
401 &trace_info);
402 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
403 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
404 "Failed to create trace info map.");
405 goto error;
406 }
407
408 ret = populate_trace_info(trace, trace_info);
409 if (ret) {
410 goto error;
411 }
412 }
413
414 goto end;
415
416 error:
417 BT_VALUE_PUT_REF_AND_RESET(result);
418 result = NULL;
419
420 if (status >= 0) {
421 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
422 }
423
424 end:
425 if (ctf_fs) {
426 ctf_fs_destroy(ctf_fs);
427 ctf_fs = NULL;
428 }
429
430 *user_result = result;
431 return status;
432 }
433
434 BT_HIDDEN
435 bt_component_class_query_method_status support_info_query(
436 bt_self_component_class_source *comp_class,
437 const bt_value *params, bt_logging_level log_level,
438 const bt_value **user_result)
439 {
440 const bt_value *input_type_value;
441 const char *input_type;
442 bt_component_class_query_method_status status;
443 double weight = 0;
444 gchar *metadata_path = NULL;
445 bt_value *result = NULL;
446
447 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
448 BT_ASSERT(input_type_value);
449 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
450 input_type = bt_value_string_get(input_type_value);
451
452 result = bt_value_map_create();
453 if (!result) {
454 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
455 goto end;
456 }
457
458 if (strcmp(input_type, "directory") == 0) {
459 const bt_value *input_value;
460 const char *path;
461
462 input_value = bt_value_map_borrow_entry_value_const(params, "input");
463 BT_ASSERT(input_value);
464 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
465 path = bt_value_string_get(input_value);
466
467 metadata_path = g_build_filename(path, CTF_FS_METADATA_FILENAME, NULL);
468 if (!metadata_path) {
469 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
470 goto end;
471 }
472
473 /*
474 * If the metadata file exists in this directory, consider it to
475 * be a CTF trace.
476 */
477 if (g_file_test(metadata_path, G_FILE_TEST_EXISTS)) {
478 weight = 0.5;
479 }
480 }
481
482 if (bt_value_map_insert_real_entry(result, "weight", weight) != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
483 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
484 goto end;
485 }
486
487 /*
488 * Use the arbitrary constant string "ctf" as the group, such that all
489 * found ctf traces are passed to the same instance of src.ctf.fs.
490 */
491 if (bt_value_map_insert_string_entry(result, "group", "ctf") != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
492 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
493 goto end;
494 }
495
496 *user_result = result;
497 result = NULL;
498 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
499
500 end:
501 g_free(metadata_path);
502 bt_value_put_ref(result);
503
504 return status;
505 }
This page took 0.038745 seconds and 4 git commands to generate.