lib: strictly type function return status enumerations
[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
41 #define METADATA_TEXT_SIG "/* CTF 1.8"
42
43 struct range {
44 int64_t begin_ns;
45 int64_t end_ns;
46 bool set;
47 };
48
49 BT_HIDDEN
50 bt_component_class_query_method_status metadata_info_query(
51 bt_self_component_class_source *comp_class,
52 const bt_value *params, bt_logging_level log_level,
53 const bt_value **user_result)
54 {
55 bt_component_class_query_method_status status =
56 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
57 bt_value *result = NULL;
58 const 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 result = bt_value_map_create();
68 if (!result) {
69 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
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 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS;
78 goto error;
79 }
80
81 path_value = bt_value_map_borrow_entry_value_const(params, "path");
82 if (!path_value) {
83 BT_LOGE_STR("Mandatory `path` parameter missing");
84 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS;
85 goto error;
86 }
87
88 if (!bt_value_is_string(path_value)) {
89 BT_LOGE_STR("`path` parameter is required to be a string value");
90 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS;
91 goto error;
92 }
93
94 path = bt_value_string_get(path_value);
95
96 BT_ASSERT(path);
97 metadata_fp = ctf_fs_metadata_open_file(path);
98 if (!metadata_fp) {
99 BT_LOGE("Cannot open trace metadata: path=\"%s\".", path);
100 goto error;
101 }
102
103 is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp,
104 &bo, log_level, NULL);
105
106 if (is_packetized) {
107 ret = ctf_metadata_decoder_packetized_file_stream_to_buf(
108 metadata_fp, &metadata_text, bo, NULL, NULL,
109 log_level, NULL);
110 if (ret) {
111 BT_LOGE("Cannot decode packetized metadata file: path=\"%s\"",
112 path);
113 goto error;
114 }
115 } else {
116 long filesize;
117
118 ret = fseek(metadata_fp, 0, SEEK_END);
119 if (ret) {
120 BT_LOGE_ERRNO("Failed to seek to the end of the metadata file",
121 ": path=\"%s\"", path);
122 goto error;
123 }
124 filesize = ftell(metadata_fp);
125 if (filesize < 0) {
126 BT_LOGE_ERRNO("Failed to get the current position in the metadata file",
127 ": path=\"%s\"", path);
128 goto error;
129 }
130 rewind(metadata_fp);
131 metadata_text = malloc(filesize + 1);
132 if (!metadata_text) {
133 BT_LOGE_STR("Cannot allocate buffer for metadata text.");
134 goto error;
135 }
136
137 if (fread(metadata_text, filesize, 1, metadata_fp) != 1) {
138 BT_LOGE_ERRNO("Cannot read metadata file", ": path=\"%s\"",
139 path);
140 goto error;
141 }
142
143 metadata_text[filesize] = '\0';
144 }
145
146 g_metadata_text = g_string_new(NULL);
147 if (!g_metadata_text) {
148 goto error;
149 }
150
151 if (strncmp(metadata_text, METADATA_TEXT_SIG,
152 sizeof(METADATA_TEXT_SIG) - 1) != 0) {
153 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
154 g_string_append(g_metadata_text, " */\n\n");
155 }
156
157 g_string_append(g_metadata_text, metadata_text);
158
159 ret = bt_value_map_insert_string_entry(result, "text",
160 g_metadata_text->str);
161 if (ret) {
162 BT_LOGE_STR("Cannot insert metadata text into query result.");
163 goto error;
164 }
165
166 ret = bt_value_map_insert_bool_entry(result, "is-packetized",
167 is_packetized);
168 if (ret) {
169 BT_LOGE_STR("Cannot insert \"is-packetized\" attribute into query result.");
170 goto error;
171 }
172
173 goto end;
174
175 error:
176 BT_VALUE_PUT_REF_AND_RESET(result);
177 result = NULL;
178
179 if (status >= 0) {
180 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
181 }
182
183 end:
184 free(metadata_text);
185
186 if (g_metadata_text) {
187 g_string_free(g_metadata_text, TRUE);
188 }
189
190 if (metadata_fp) {
191 fclose(metadata_fp);
192 }
193
194 *user_result = result;
195 return status;
196 }
197
198 static
199 int add_range(bt_value *info, struct range *range,
200 const char *range_name)
201 {
202 int ret = 0;
203 bt_value_map_insert_entry_status status;
204 bt_value *range_map = NULL;
205
206 if (!range->set) {
207 /* Not an error. */
208 goto end;
209 }
210
211 range_map = bt_value_map_create();
212 if (!range_map) {
213 ret = -1;
214 goto end;
215 }
216
217 status = bt_value_map_insert_signed_integer_entry(range_map, "begin",
218 range->begin_ns);
219 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
220 ret = -1;
221 goto end;
222 }
223
224 status = bt_value_map_insert_signed_integer_entry(range_map, "end",
225 range->end_ns);
226 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
227 ret = -1;
228 goto end;
229 }
230
231 status = bt_value_map_insert_entry(info, range_name,
232 range_map);
233 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
234 ret = -1;
235 goto end;
236 }
237
238 end:
239 bt_value_put_ref(range_map);
240 return ret;
241 }
242
243 static
244 int add_stream_ids(bt_value *info, struct ctf_fs_ds_file_group *ds_file_group)
245 {
246 int ret = 0;
247 bt_value_map_insert_entry_status status;
248
249 if (ds_file_group->stream_id != UINT64_C(-1)) {
250 status = bt_value_map_insert_unsigned_integer_entry(info, "id",
251 ds_file_group->stream_id);
252 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
253 ret = -1;
254 goto end;
255 }
256 }
257
258 status = bt_value_map_insert_unsigned_integer_entry(info, "class-id",
259 ds_file_group->sc->id);
260 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
261 ret = -1;
262 goto end;
263 }
264
265 end:
266 return ret;
267 }
268
269 static
270 int populate_stream_info(struct ctf_fs_ds_file_group *group,
271 bt_value *group_info, struct range *stream_range)
272 {
273 int ret = 0;
274 size_t file_idx;
275 bt_value_map_insert_entry_status insert_status;
276 bt_value_array_append_element_status append_status;
277 bt_value *file_paths;
278 struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
279 gchar *port_name = NULL;
280
281 file_paths = bt_value_array_create();
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 struct ctf_fs_ds_file_info *info =
289 g_ptr_array_index(group->ds_file_infos,
290 file_idx);
291
292 append_status = bt_value_array_append_string_element(file_paths,
293 info->path->str);
294 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
295 ret = -1;
296 goto end;
297 }
298 }
299
300 /*
301 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
302 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
303 * the timestamp_begin of the first index entry and the timestamp_end
304 * of the last index entry.
305 */
306 BT_ASSERT(group->index);
307 BT_ASSERT(group->index->entries);
308 BT_ASSERT(group->index->entries->len > 0);
309
310 /* First entry. */
311 first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
312 group->index->entries, 0);
313
314 /* Last entry. */
315 last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
316 group->index->entries, group->index->entries->len - 1);
317
318 stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns;
319 stream_range->end_ns = last_ds_index_entry->timestamp_end_ns;
320
321 /*
322 * If any of the begin and end timestamps is not set it means that
323 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
324 * in their packet context so we can't set the range.
325 */
326 stream_range->set = stream_range->begin_ns != UINT64_C(-1) &&
327 stream_range->end_ns != UINT64_C(-1);
328
329 ret = add_range(group_info, stream_range, "range-ns");
330 if (ret) {
331 goto end;
332 }
333
334 insert_status = bt_value_map_insert_entry(group_info, "paths",
335 file_paths);
336 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
337 ret = -1;
338 goto end;
339 }
340
341 ret = add_stream_ids(group_info, group);
342 if (ret) {
343 goto end;
344 }
345
346 port_name = ctf_fs_make_port_name(group);
347 if (!port_name) {
348 ret = -1;
349 goto end;
350 }
351
352 insert_status = bt_value_map_insert_string_entry(group_info,
353 "port-name", port_name);
354 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
355 ret = -1;
356 goto end;
357 }
358
359 end:
360 bt_value_put_ref(file_paths);
361 return ret;
362 }
363
364 static
365 int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info)
366 {
367 int ret = 0;
368 size_t group_idx;
369 bt_value_map_insert_entry_status insert_status;
370 bt_value_array_append_element_status append_status;
371 bt_value *file_groups = NULL;
372 struct range trace_range = {
373 .begin_ns = INT64_MAX,
374 .end_ns = 0,
375 .set = false,
376 };
377 struct range trace_intersection = {
378 .begin_ns = 0,
379 .end_ns = INT64_MAX,
380 .set = false,
381 };
382
383 BT_ASSERT(trace->ds_file_groups);
384 /* Add trace range info only if it contains streams. */
385 if (trace->ds_file_groups->len == 0) {
386 ret = -1;
387 goto end;
388 }
389
390 file_groups = bt_value_array_create();
391 if (!file_groups) {
392 goto end;
393 }
394
395 insert_status = bt_value_map_insert_string_entry(trace_info, "name",
396 trace->name->str);
397 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
398 ret = -1;
399 goto end;
400 }
401 insert_status = bt_value_map_insert_string_entry(trace_info, "path",
402 trace->path->str);
403 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
404 ret = -1;
405 goto end;
406 }
407
408 /* Find range of all stream groups, and of the trace. */
409 for (group_idx = 0; group_idx < trace->ds_file_groups->len;
410 group_idx++) {
411 bt_value *group_info;
412 struct range group_range = { .set = false };
413 struct ctf_fs_ds_file_group *group = g_ptr_array_index(
414 trace->ds_file_groups, group_idx);
415
416 group_info = bt_value_map_create();
417 if (!group_info) {
418 ret = -1;
419 goto end;
420 }
421
422 ret = populate_stream_info(group, group_info, &group_range);
423 if (ret) {
424 bt_value_put_ref(group_info);
425 goto end;
426 }
427
428 append_status = bt_value_array_append_element(file_groups,
429 group_info);
430 bt_value_put_ref(group_info);
431 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
432 goto end;
433 }
434
435 if (group_range.set) {
436 trace_range.begin_ns = MIN(trace_range.begin_ns,
437 group_range.begin_ns);
438 trace_range.end_ns = MAX(trace_range.end_ns,
439 group_range.end_ns);
440 trace_range.set = true;
441
442 trace_intersection.begin_ns = MAX(trace_intersection.begin_ns,
443 group_range.begin_ns);
444 trace_intersection.end_ns = MIN(trace_intersection.end_ns,
445 group_range.end_ns);
446 trace_intersection.set = true;
447 }
448 }
449
450 ret = add_range(trace_info, &trace_range, "range-ns");
451 if (ret) {
452 goto end;
453 }
454
455 if (trace_intersection.begin_ns < trace_intersection.end_ns) {
456 ret = add_range(trace_info, &trace_intersection,
457 "intersection-range-ns");
458 if (ret) {
459 goto end;
460 }
461 }
462
463 insert_status = bt_value_map_insert_entry(trace_info, "streams",
464 file_groups);
465 BT_VALUE_PUT_REF_AND_RESET(file_groups);
466 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
467 ret = -1;
468 goto end;
469 }
470
471 end:
472 bt_value_put_ref(file_groups);
473 return ret;
474 }
475
476 BT_HIDDEN
477 bt_component_class_query_method_status trace_info_query(
478 bt_self_component_class_source *comp_class,
479 const bt_value *params, bt_logging_level log_level,
480 const bt_value **user_result)
481 {
482 struct ctf_fs_component *ctf_fs = NULL;
483 bt_component_class_query_method_status status =
484 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
485 bt_value *result = NULL;
486 const bt_value *paths_value = NULL;
487 int ret = 0;
488 guint i;
489
490 BT_ASSERT(params);
491
492 if (!bt_value_is_map(params)) {
493 BT_LOGE("Query parameters is not a map value object.");
494 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS;
495 goto error;
496 }
497
498 ctf_fs = ctf_fs_component_create(log_level, NULL);
499 if (!ctf_fs) {
500 goto error;
501 }
502
503 if (!read_src_fs_parameters(params, &paths_value, ctf_fs)) {
504 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS;
505 goto error;
506 }
507
508 if (ctf_fs_component_create_ctf_fs_traces(NULL, ctf_fs, paths_value)) {
509 goto error;
510 }
511
512 result = bt_value_array_create();
513 if (!result) {
514 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
515 goto error;
516 }
517
518 for (i = 0; i < ctf_fs->traces->len; i++) {
519 struct ctf_fs_trace *trace;
520 bt_value *trace_info;
521 bt_value_array_append_element_status append_status;
522
523 trace = g_ptr_array_index(ctf_fs->traces, i);
524 BT_ASSERT(trace);
525
526 trace_info = bt_value_map_create();
527 if (!trace_info) {
528 BT_LOGE("Failed to create trace info map.");
529 goto error;
530 }
531
532 ret = populate_trace_info(trace, trace_info);
533 if (ret) {
534 bt_value_put_ref(trace_info);
535 goto error;
536 }
537
538 append_status = bt_value_array_append_element(result,
539 trace_info);
540 bt_value_put_ref(trace_info);
541 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
542 goto error;
543 }
544 }
545
546 goto end;
547
548 error:
549 BT_VALUE_PUT_REF_AND_RESET(result);
550 result = NULL;
551
552 if (status >= 0) {
553 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
554 }
555
556 end:
557 if (ctf_fs) {
558 ctf_fs_destroy(ctf_fs);
559 ctf_fs = NULL;
560 }
561
562 *user_result = result;
563 return status;
564 }
This page took 0.040186 seconds and 4 git commands to generate.