lib: bt_object_{get,put}_ref(): accept a `const` parameter
[babeltrace.git] / plugins / ctf / fs-src / query.c
CommitLineData
04c0ba87
JG
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#include "query.h"
28#include <stdbool.h>
f6ccaed9 29#include <babeltrace/assert-internal.h>
04c0ba87
JG
30#include "metadata.h"
31#include "../common/metadata/decoder.h"
55314f2a 32#include <babeltrace/common-internal.h>
97ade20b 33#include <babeltrace/babeltrace-internal.h>
9d408fca 34#include <babeltrace/babeltrace.h>
97ade20b 35#include "fs.h"
55314f2a
JG
36
37#define BT_LOG_TAG "PLUGIN-CTF-FS-QUERY-SRC"
38#include "logging.h"
04c0ba87
JG
39
40#define METADATA_TEXT_SIG "/* CTF 1.8"
41
97ade20b
JG
42struct range {
43 int64_t begin_ns;
44 int64_t end_ns;
45 bool set;
46};
47
04c0ba87 48BT_HIDDEN
d94d92ac
PP
49enum bt_query_status metadata_info_query(
50 struct bt_self_component_class_source *comp_class,
51 struct bt_value *params, struct bt_value **user_result)
04c0ba87 52{
d94d92ac 53 enum bt_query_status status = BT_QUERY_STATUS_OK;
da91b29a 54 struct bt_private_value *result = NULL;
04c0ba87
JG
55 struct bt_value *path_value = NULL;
56 char *metadata_text = NULL;
57 FILE *metadata_fp = NULL;
58 GString *g_metadata_text = NULL;
59 int ret;
60 int bo;
61 const char *path;
62 bool is_packetized;
63
da91b29a
PP
64 result = bt_private_value_map_create();
65 if (!result) {
d94d92ac 66 status = BT_QUERY_STATUS_NOMEM;
04c0ba87
JG
67 goto error;
68 }
69
f6ccaed9
PP
70 BT_ASSERT(params);
71
04c0ba87 72 if (!bt_value_is_map(params)) {
f7d0e29c 73 BT_LOGE_STR("Query parameters is not a map value object.");
d94d92ac 74 status = BT_QUERY_STATUS_INVALID_PARAMS;
04c0ba87
JG
75 goto error;
76 }
77
07208d85 78 path_value = bt_value_map_borrow_entry_value(params, "path");
601b0d3c 79 path = bt_value_string_get(path_value);
04c0ba87 80
f6ccaed9 81 BT_ASSERT(path);
04c0ba87
JG
82 metadata_fp = ctf_fs_metadata_open_file(path);
83 if (!metadata_fp) {
f7d0e29c 84 BT_LOGE("Cannot open trace metadata: path=\"%s\".", path);
04c0ba87
JG
85 goto error;
86 }
87
88 is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp,
89 &bo);
90
91 if (is_packetized) {
92 ret = ctf_metadata_decoder_packetized_file_stream_to_buf(
93 metadata_fp, &metadata_text, bo);
94 if (ret) {
f7d0e29c
JG
95 BT_LOGE("Cannot decode packetized metadata file: path=\"%s\"",
96 path);
04c0ba87
JG
97 goto error;
98 }
99 } else {
100 long filesize;
101
677e1a21
MD
102 ret = fseek(metadata_fp, 0, SEEK_END);
103 if (ret) {
f7d0e29c
JG
104 BT_LOGE_ERRNO("Failed to seek to the end of the metadata file",
105 ": path=\"%s\"", path);
677e1a21
MD
106 goto error;
107 }
04c0ba87 108 filesize = ftell(metadata_fp);
677e1a21 109 if (filesize < 0) {
f7d0e29c
JG
110 BT_LOGE_ERRNO("Failed to get the current position in the metadata file",
111 ": path=\"%s\"", path);
677e1a21
MD
112 goto error;
113 }
04c0ba87
JG
114 rewind(metadata_fp);
115 metadata_text = malloc(filesize + 1);
116 if (!metadata_text) {
f7d0e29c 117 BT_LOGE_STR("Cannot allocate buffer for metadata text.");
04c0ba87
JG
118 goto error;
119 }
120
121 if (fread(metadata_text, filesize, 1, metadata_fp) != 1) {
f7d0e29c
JG
122 BT_LOGE_ERRNO("Cannot read metadata file", ": path=\"%s\"",
123 path);
04c0ba87
JG
124 goto error;
125 }
126
127 metadata_text[filesize] = '\0';
128 }
129
130 g_metadata_text = g_string_new(NULL);
131 if (!g_metadata_text) {
132 goto error;
133 }
134
135 if (strncmp(metadata_text, METADATA_TEXT_SIG,
136 sizeof(METADATA_TEXT_SIG) - 1) != 0) {
137 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
138 g_string_append(g_metadata_text, " */\n\n");
139 }
140
141 g_string_append(g_metadata_text, metadata_text);
142
da91b29a 143 ret = bt_private_value_map_insert_string_entry(result, "text",
04c0ba87
JG
144 g_metadata_text->str);
145 if (ret) {
f7d0e29c 146 BT_LOGE_STR("Cannot insert metadata text into query result.");
04c0ba87
JG
147 goto error;
148 }
149
da91b29a 150 ret = bt_private_value_map_insert_bool_entry(result, "is-packetized",
04c0ba87
JG
151 is_packetized);
152 if (ret) {
f7d0e29c 153 BT_LOGE_STR("Cannot insert \"is-packetized\" attribute into query result.");
04c0ba87
JG
154 goto error;
155 }
156
157 goto end;
158
159error:
da91b29a 160 BT_OBJECT_PUT_REF_AND_RESET(result);
d94d92ac 161 result = NULL;
c7eee084 162
d94d92ac
PP
163 if (status >= 0) {
164 status = BT_QUERY_STATUS_ERROR;
c7eee084 165 }
04c0ba87
JG
166
167end:
04c0ba87
JG
168 free(metadata_text);
169
170 if (g_metadata_text) {
171 g_string_free(g_metadata_text, TRUE);
172 }
173
174 if (metadata_fp) {
175 fclose(metadata_fp);
176 }
c7eee084 177
707b7d35 178 *user_result = bt_private_value_as_value(result);
d94d92ac 179 return status;
97ade20b 180}
9ec238a8 181
97ade20b 182static
da91b29a 183int add_range(struct bt_private_value *info, struct range *range,
97ade20b
JG
184 const char *range_name)
185{
186 int ret = 0;
187 enum bt_value_status status;
da91b29a 188 struct bt_private_value *range_map = NULL;
97ade20b
JG
189
190 if (!range->set) {
191 /* Not an error. */
192 goto end;
193 }
194
da91b29a 195 range_map = bt_private_value_map_create();
97ade20b
JG
196 if (!range_map) {
197 ret = -1;
198 goto end;
199 }
200
da91b29a 201 status = bt_private_value_map_insert_integer_entry(range_map, "begin",
97ade20b
JG
202 range->begin_ns);
203 if (status != BT_VALUE_STATUS_OK) {
204 ret = -1;
205 goto end;
206 }
207
da91b29a 208 status = bt_private_value_map_insert_integer_entry(range_map, "end",
97ade20b
JG
209 range->end_ns);
210 if (status != BT_VALUE_STATUS_OK) {
211 ret = -1;
212 goto end;
213 }
214
da91b29a 215 status = bt_private_value_map_insert_entry(info, range_name,
707b7d35 216 bt_private_value_as_value(range_map));
97ade20b
JG
217 if (status != BT_VALUE_STATUS_OK) {
218 ret = -1;
219 goto end;
220 }
da91b29a 221
97ade20b 222end:
65300d60 223 bt_object_put_ref(range_map);
97ade20b
JG
224 return ret;
225}
226
227static
e5be10ef
PP
228int add_stream_ids(struct bt_private_value *info,
229 struct bt_stream *stream)
97ade20b
JG
230{
231 int ret = 0;
232 int64_t stream_class_id, stream_instance_id;
233 enum bt_value_status status;
50842bdc 234 struct bt_stream_class *stream_class = NULL;
97ade20b 235
50842bdc 236 stream_instance_id = bt_stream_get_id(stream);
97ade20b 237 if (stream_instance_id != -1) {
da91b29a 238 status = bt_private_value_map_insert_integer_entry(info, "id",
97ade20b
JG
239 stream_instance_id);
240 if (status != BT_VALUE_STATUS_OK) {
241 ret = -1;
242 goto end;
243 }
244 }
245
0b29603d 246 stream_class = bt_stream_borrow_class(stream);
97ade20b
JG
247 if (!stream_class) {
248 ret = -1;
249 goto end;
250 }
251
50842bdc 252 stream_class_id = bt_stream_class_get_id(stream_class);
97ade20b
JG
253 if (stream_class_id == -1) {
254 ret = -1;
255 goto end;
256 }
257
da91b29a 258 status = bt_private_value_map_insert_integer_entry(info, "class-id", stream_class_id);
97ade20b
JG
259 if (status != BT_VALUE_STATUS_OK) {
260 ret = -1;
261 goto end;
262 }
0b29603d 263
97ade20b 264end:
97ade20b
JG
265 return ret;
266}
267
268static
269int populate_stream_info(struct ctf_fs_ds_file_group *group,
da91b29a 270 struct bt_private_value *group_info,
97ade20b
JG
271 struct range *stream_range)
272{
273 int ret = 0;
274 size_t file_idx;
275 enum bt_value_status status;
da91b29a 276 struct bt_private_value *file_paths;
97ade20b
JG
277
278 stream_range->begin_ns = INT64_MAX;
279 stream_range->end_ns = 0;
280
da91b29a 281 file_paths = bt_private_value_array_create();
97ade20b
JG
282 if (!file_paths) {
283 ret = -1;
284 goto end;
285 }
286
287 for (file_idx = 0; file_idx < group->ds_file_infos->len; file_idx++) {
288 int64_t file_begin_epoch, file_end_epoch;
289 struct ctf_fs_ds_file_info *info =
290 g_ptr_array_index(group->ds_file_infos,
291 file_idx);
292
293 if (!info->index || info->index->entries->len == 0) {
294 BT_LOGW("Cannot determine range of unindexed stream file \'%s\'",
295 info->path->str);
296 ret = -1;
297 goto end;
298 }
299
da91b29a 300 status = bt_private_value_array_append_string_element(file_paths,
97ade20b
JG
301 info->path->str);
302 if (status != BT_VALUE_STATUS_OK) {
303 ret = -1;
304 goto end;
305 }
306
307 /*
308 * file range is from timestamp_begin of the first entry to the
309 * timestamp_end of the last entry.
310 */
311 file_begin_epoch = ((struct ctf_fs_ds_index_entry *) &g_array_index(info->index->entries,
312 struct ctf_fs_ds_index_entry, 0))->timestamp_begin_ns;
313 file_end_epoch = ((struct ctf_fs_ds_index_entry *) &g_array_index(info->index->entries,
314 struct ctf_fs_ds_index_entry, info->index->entries->len - 1))->timestamp_end_ns;
315
316 stream_range->begin_ns = min(stream_range->begin_ns, file_begin_epoch);
317 stream_range->end_ns = max(stream_range->end_ns, file_end_epoch);
318 stream_range->set = true;
319 }
320
321 if (stream_range->set) {
322 ret = add_range(group_info, stream_range, "range-ns");
323 if (ret) {
324 goto end;
325 }
326 }
327
da91b29a 328 status = bt_private_value_map_insert_entry(group_info, "paths",
707b7d35 329 bt_private_value_as_value(file_paths));
97ade20b
JG
330 if (status != BT_VALUE_STATUS_OK) {
331 ret = -1;
332 goto end;
333 }
334
e5be10ef 335 ret = add_stream_ids(group_info,
707b7d35 336 bt_private_stream_as_stream(group->stream));
97ade20b
JG
337 if (ret) {
338 goto end;
339 }
340end:
65300d60 341 bt_object_put_ref(file_paths);
97ade20b
JG
342 return ret;
343}
344
345static
346int populate_trace_info(const char *trace_path, const char *trace_name,
da91b29a 347 struct bt_private_value *trace_info)
97ade20b
JG
348{
349 int ret = 0;
350 size_t group_idx;
351 struct ctf_fs_trace *trace = NULL;
352 enum bt_value_status status;
da91b29a 353 struct bt_private_value *file_groups;
97ade20b
JG
354 struct range trace_range = {
355 .begin_ns = INT64_MAX,
356 .end_ns = 0,
357 .set = false,
358 };
359 struct range trace_intersection = {
360 .begin_ns = 0,
361 .end_ns = INT64_MAX,
362 .set = false,
363 };
364
da91b29a 365 file_groups = bt_private_value_array_create();
97ade20b
JG
366 if (!file_groups) {
367 goto end;
368 }
369
da91b29a
PP
370 status = bt_private_value_map_insert_string_entry(trace_info, "name",
371 trace_name);
97ade20b
JG
372 if (status != BT_VALUE_STATUS_OK) {
373 ret = -1;
374 goto end;
375 }
da91b29a 376 status = bt_private_value_map_insert_string_entry(trace_info, "path",
97ade20b
JG
377 trace_path);
378 if (status != BT_VALUE_STATUS_OK) {
379 ret = -1;
380 goto end;
381 }
382
e5be10ef 383 trace = ctf_fs_trace_create(trace_path, trace_name, NULL);
97ade20b
JG
384 if (!trace) {
385 BT_LOGE("Failed to create fs trace at \'%s\'", trace_path);
386 ret = -1;
387 goto end;
388 }
389
f6ccaed9 390 BT_ASSERT(trace->ds_file_groups);
97ade20b
JG
391 /* Add trace range info only if it contains streams. */
392 if (trace->ds_file_groups->len == 0) {
393 ret = -1;
394 goto end;
395 }
396
397 /* Find range of all stream groups, and of the trace. */
398 for (group_idx = 0; group_idx < trace->ds_file_groups->len;
399 group_idx++) {
da91b29a 400 struct bt_private_value *group_info;
97ade20b
JG
401 struct range group_range = { .set = false };
402 struct ctf_fs_ds_file_group *group = g_ptr_array_index(
403 trace->ds_file_groups, group_idx);
404
da91b29a 405 group_info = bt_private_value_map_create();
97ade20b
JG
406 if (!group_info) {
407 ret = -1;
408 goto end;
409 }
410
411 ret = populate_stream_info(group, group_info, &group_range);
412 if (ret) {
65300d60 413 bt_object_put_ref(group_info);
97ade20b
JG
414 goto end;
415 }
416
417 if (group_range.set) {
418 trace_range.begin_ns = min(trace_range.begin_ns,
419 group_range.begin_ns);
420 trace_range.end_ns = max(trace_range.end_ns,
421 group_range.end_ns);
422 trace_range.set = true;
423
424 trace_intersection.begin_ns = max(trace_intersection.begin_ns,
425 group_range.begin_ns);
426 trace_intersection.end_ns = min(trace_intersection.end_ns,
427 group_range.end_ns);
428 trace_intersection.set = true;
da91b29a
PP
429 status = bt_private_value_array_append_element(
430 file_groups,
707b7d35 431 bt_private_value_as_value(group_info));
65300d60 432 bt_object_put_ref(group_info);
97ade20b
JG
433 if (status != BT_VALUE_STATUS_OK) {
434 goto end;
435 }
436 }
437 }
438
439 ret = add_range(trace_info, &trace_range, "range-ns");
440 if (ret) {
441 goto end;
442 }
c6daf8b2
PP
443
444 if (trace_intersection.begin_ns < trace_intersection.end_ns) {
445 ret = add_range(trace_info, &trace_intersection,
446 "intersection-range-ns");
447 if (ret) {
448 goto end;
449 }
97ade20b
JG
450 }
451
da91b29a 452 status = bt_private_value_map_insert_entry(trace_info, "streams",
707b7d35 453 bt_private_value_as_value(file_groups));
65300d60 454 BT_OBJECT_PUT_REF_AND_RESET(file_groups);
97ade20b
JG
455 if (status != BT_VALUE_STATUS_OK) {
456 ret = -1;
457 goto end;
458 }
459
460end:
65300d60 461 bt_object_put_ref(file_groups);
97ade20b
JG
462 ctf_fs_trace_destroy(trace);
463 return ret;
464}
465
466BT_HIDDEN
d94d92ac
PP
467enum bt_query_status trace_info_query(
468 struct bt_self_component_class_source *comp_class,
469 struct bt_value *params, struct bt_value **user_result)
97ade20b 470{
d94d92ac 471 enum bt_query_status status = BT_QUERY_STATUS_OK;
da91b29a 472 struct bt_private_value *result = NULL;
97ade20b
JG
473 struct bt_value *path_value = NULL;
474 int ret = 0;
475 const char *path = NULL;
476 GList *trace_paths = NULL;
477 GList *trace_names = NULL;
478 GList *tp_node = NULL;
479 GList *tn_node = NULL;
480 GString *normalized_path = NULL;
481
f6ccaed9
PP
482 BT_ASSERT(params);
483
97ade20b
JG
484 if (!bt_value_is_map(params)) {
485 BT_LOGE("Query parameters is not a map value object.");
d94d92ac 486 status = BT_QUERY_STATUS_INVALID_PARAMS;
97ade20b
JG
487 goto error;
488 }
489
07208d85 490 path_value = bt_value_map_borrow_entry_value(params, "path");
601b0d3c 491 path = bt_value_string_get(path_value);
97ade20b
JG
492
493 normalized_path = bt_common_normalize_path(path, NULL);
494 if (!normalized_path) {
495 BT_LOGE("Failed to normalize path: `%s`.", path);
496 goto error;
497 }
f6ccaed9 498 BT_ASSERT(path);
97ade20b
JG
499
500 ret = ctf_fs_find_traces(&trace_paths, normalized_path->str);
501 if (ret) {
502 goto error;
503 }
504
505 trace_names = ctf_fs_create_trace_names(trace_paths,
506 normalized_path->str);
507 if (!trace_names) {
508 BT_LOGE("Cannot create trace names from trace paths.");
509 goto error;
510 }
511
da91b29a
PP
512 result = bt_private_value_array_create();
513 if (!result) {
d94d92ac 514 status = BT_QUERY_STATUS_NOMEM;
97ade20b
JG
515 goto error;
516 }
517
518 /* Iterates over both trace paths and names simultaneously. */
519 for (tp_node = trace_paths, tn_node = trace_names; tp_node;
520 tp_node = g_list_next(tp_node),
521 tn_node = g_list_next(tn_node)) {
522 GString *trace_path = tp_node->data;
523 GString *trace_name = tn_node->data;
524 enum bt_value_status status;
da91b29a 525 struct bt_private_value *trace_info;
97ade20b 526
da91b29a 527 trace_info = bt_private_value_map_create();
97ade20b
JG
528 if (!trace_info) {
529 BT_LOGE("Failed to create trace info map.");
530 goto error;
531 }
532
533 ret = populate_trace_info(trace_path->str, trace_name->str,
534 trace_info);
535 if (ret) {
65300d60 536 bt_object_put_ref(trace_info);
97ade20b
JG
537 goto error;
538 }
539
da91b29a 540 status = bt_private_value_array_append_element(result,
707b7d35 541 bt_private_value_as_value(trace_info));
65300d60 542 bt_object_put_ref(trace_info);
97ade20b
JG
543 if (status != BT_VALUE_STATUS_OK) {
544 goto error;
545 }
546 }
547
548 goto end;
549
550error:
da91b29a 551 BT_OBJECT_PUT_REF_AND_RESET(result);
d94d92ac 552 result = NULL;
c7eee084 553
d94d92ac
PP
554 if (status >= 0) {
555 status = BT_QUERY_STATUS_ERROR;
c7eee084
PP
556 }
557
97ade20b
JG
558end:
559 if (normalized_path) {
560 g_string_free(normalized_path, TRUE);
561 }
562 if (trace_paths) {
563 for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) {
564 if (tp_node->data) {
565 g_string_free(tp_node->data, TRUE);
566 }
567 }
568 g_list_free(trace_paths);
569 }
570 if (trace_names) {
571 for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) {
572 if (tn_node->data) {
573 g_string_free(tn_node->data, TRUE);
574 }
575 }
576 g_list_free(trace_names);
577 }
d94d92ac 578
707b7d35 579 *user_result = bt_private_value_as_value(result);
d94d92ac 580 return status;
04c0ba87 581}
This page took 0.055141 seconds and 4 git commands to generate.