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