lib: graph: add "self" and some "private" APIs
[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 enum 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)
52 {
53 enum bt_query_status status = BT_QUERY_STATUS_OK;
54 struct bt_private_value *result = NULL;
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
64 result = bt_private_value_map_create();
65 if (!result) {
66 status = BT_QUERY_STATUS_NOMEM;
67 goto error;
68 }
69
70 BT_ASSERT(params);
71
72 if (!bt_value_is_map(params)) {
73 BT_LOGE_STR("Query parameters is not a map value object.");
74 status = BT_QUERY_STATUS_INVALID_PARAMS;
75 goto error;
76 }
77
78 path_value = bt_value_map_borrow_entry_value(params, "path");
79 path = bt_value_string_get(path_value);
80
81 BT_ASSERT(path);
82 metadata_fp = ctf_fs_metadata_open_file(path);
83 if (!metadata_fp) {
84 BT_LOGE("Cannot open trace metadata: path=\"%s\".", path);
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) {
95 BT_LOGE("Cannot decode packetized metadata file: path=\"%s\"",
96 path);
97 goto error;
98 }
99 } else {
100 long filesize;
101
102 ret = fseek(metadata_fp, 0, SEEK_END);
103 if (ret) {
104 BT_LOGE_ERRNO("Failed to seek to the end of the metadata file",
105 ": path=\"%s\"", path);
106 goto error;
107 }
108 filesize = ftell(metadata_fp);
109 if (filesize < 0) {
110 BT_LOGE_ERRNO("Failed to get the current position in the metadata file",
111 ": path=\"%s\"", path);
112 goto error;
113 }
114 rewind(metadata_fp);
115 metadata_text = malloc(filesize + 1);
116 if (!metadata_text) {
117 BT_LOGE_STR("Cannot allocate buffer for metadata text.");
118 goto error;
119 }
120
121 if (fread(metadata_text, filesize, 1, metadata_fp) != 1) {
122 BT_LOGE_ERRNO("Cannot read metadata file", ": path=\"%s\"",
123 path);
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
143 ret = bt_private_value_map_insert_string_entry(result, "text",
144 g_metadata_text->str);
145 if (ret) {
146 BT_LOGE_STR("Cannot insert metadata text into query result.");
147 goto error;
148 }
149
150 ret = bt_private_value_map_insert_bool_entry(result, "is-packetized",
151 is_packetized);
152 if (ret) {
153 BT_LOGE_STR("Cannot insert \"is-packetized\" attribute into query result.");
154 goto error;
155 }
156
157 goto end;
158
159 error:
160 BT_OBJECT_PUT_REF_AND_RESET(result);
161 result = NULL;
162
163 if (status >= 0) {
164 status = BT_QUERY_STATUS_ERROR;
165 }
166
167 end:
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 }
177
178 *user_result = bt_private_value_borrow_value(result);
179 return status;
180 }
181
182 static
183 int add_range(struct bt_private_value *info, struct range *range,
184 const char *range_name)
185 {
186 int ret = 0;
187 enum bt_value_status status;
188 struct bt_private_value *range_map = NULL;
189
190 if (!range->set) {
191 /* Not an error. */
192 goto end;
193 }
194
195 range_map = bt_private_value_map_create();
196 if (!range_map) {
197 ret = -1;
198 goto end;
199 }
200
201 status = bt_private_value_map_insert_integer_entry(range_map, "begin",
202 range->begin_ns);
203 if (status != BT_VALUE_STATUS_OK) {
204 ret = -1;
205 goto end;
206 }
207
208 status = bt_private_value_map_insert_integer_entry(range_map, "end",
209 range->end_ns);
210 if (status != BT_VALUE_STATUS_OK) {
211 ret = -1;
212 goto end;
213 }
214
215 status = bt_private_value_map_insert_entry(info, range_name,
216 bt_private_value_borrow_value(range_map));
217 if (status != BT_VALUE_STATUS_OK) {
218 ret = -1;
219 goto end;
220 }
221
222 end:
223 bt_object_put_ref(range_map);
224 return ret;
225 }
226
227 static
228 int add_stream_ids(struct bt_private_value *info,
229 struct bt_stream *stream)
230 {
231 int ret = 0;
232 int64_t stream_class_id, stream_instance_id;
233 enum bt_value_status status;
234 struct bt_stream_class *stream_class = NULL;
235
236 stream_instance_id = bt_stream_get_id(stream);
237 if (stream_instance_id != -1) {
238 status = bt_private_value_map_insert_integer_entry(info, "id",
239 stream_instance_id);
240 if (status != BT_VALUE_STATUS_OK) {
241 ret = -1;
242 goto end;
243 }
244 }
245
246 stream_class = bt_stream_borrow_class(stream);
247 if (!stream_class) {
248 ret = -1;
249 goto end;
250 }
251
252 stream_class_id = bt_stream_class_get_id(stream_class);
253 if (stream_class_id == -1) {
254 ret = -1;
255 goto end;
256 }
257
258 status = bt_private_value_map_insert_integer_entry(info, "class-id", stream_class_id);
259 if (status != BT_VALUE_STATUS_OK) {
260 ret = -1;
261 goto end;
262 }
263
264 end:
265 return ret;
266 }
267
268 static
269 int populate_stream_info(struct ctf_fs_ds_file_group *group,
270 struct bt_private_value *group_info,
271 struct range *stream_range)
272 {
273 int ret = 0;
274 size_t file_idx;
275 enum bt_value_status status;
276 struct bt_private_value *file_paths;
277
278 stream_range->begin_ns = INT64_MAX;
279 stream_range->end_ns = 0;
280
281 file_paths = bt_private_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 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
300 status = bt_private_value_array_append_string_element(file_paths,
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
328 status = bt_private_value_map_insert_entry(group_info, "paths",
329 bt_private_value_borrow_value(file_paths));
330 if (status != BT_VALUE_STATUS_OK) {
331 ret = -1;
332 goto end;
333 }
334
335 ret = add_stream_ids(group_info,
336 bt_private_stream_borrow_stream(group->stream));
337 if (ret) {
338 goto end;
339 }
340 end:
341 bt_object_put_ref(file_paths);
342 return ret;
343 }
344
345 static
346 int populate_trace_info(const char *trace_path, const char *trace_name,
347 struct bt_private_value *trace_info)
348 {
349 int ret = 0;
350 size_t group_idx;
351 struct ctf_fs_trace *trace = NULL;
352 enum bt_value_status status;
353 struct bt_private_value *file_groups;
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
365 file_groups = bt_private_value_array_create();
366 if (!file_groups) {
367 goto end;
368 }
369
370 status = bt_private_value_map_insert_string_entry(trace_info, "name",
371 trace_name);
372 if (status != BT_VALUE_STATUS_OK) {
373 ret = -1;
374 goto end;
375 }
376 status = bt_private_value_map_insert_string_entry(trace_info, "path",
377 trace_path);
378 if (status != BT_VALUE_STATUS_OK) {
379 ret = -1;
380 goto end;
381 }
382
383 trace = ctf_fs_trace_create(trace_path, trace_name, NULL);
384 if (!trace) {
385 BT_LOGE("Failed to create fs trace at \'%s\'", trace_path);
386 ret = -1;
387 goto end;
388 }
389
390 BT_ASSERT(trace->ds_file_groups);
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++) {
400 struct bt_private_value *group_info;
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
405 group_info = bt_private_value_map_create();
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) {
413 bt_object_put_ref(group_info);
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;
429 status = bt_private_value_array_append_element(
430 file_groups,
431 bt_private_value_borrow_value(group_info));
432 bt_object_put_ref(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_private_value_map_insert_entry(trace_info, "streams",
453 bt_private_value_borrow_value(file_groups));
454 BT_OBJECT_PUT_REF_AND_RESET(file_groups);
455 if (status != BT_VALUE_STATUS_OK) {
456 ret = -1;
457 goto end;
458 }
459
460 end:
461 bt_object_put_ref(file_groups);
462 ctf_fs_trace_destroy(trace);
463 return ret;
464 }
465
466 BT_HIDDEN
467 enum 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)
470 {
471 enum bt_query_status status = BT_QUERY_STATUS_OK;
472 struct bt_private_value *result = NULL;
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
482 BT_ASSERT(params);
483
484 if (!bt_value_is_map(params)) {
485 BT_LOGE("Query parameters is not a map value object.");
486 status = BT_QUERY_STATUS_INVALID_PARAMS;
487 goto error;
488 }
489
490 path_value = bt_value_map_borrow_entry_value(params, "path");
491 path = bt_value_string_get(path_value);
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 }
498 BT_ASSERT(path);
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
512 result = bt_private_value_array_create();
513 if (!result) {
514 status = BT_QUERY_STATUS_NOMEM;
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;
525 struct bt_private_value *trace_info;
526
527 trace_info = bt_private_value_map_create();
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) {
536 bt_object_put_ref(trace_info);
537 goto error;
538 }
539
540 status = bt_private_value_array_append_element(result,
541 bt_private_value_borrow_value(trace_info));
542 bt_object_put_ref(trace_info);
543 if (status != BT_VALUE_STATUS_OK) {
544 goto error;
545 }
546 }
547
548 goto end;
549
550 error:
551 BT_OBJECT_PUT_REF_AND_RESET(result);
552 result = NULL;
553
554 if (status >= 0) {
555 status = BT_QUERY_STATUS_ERROR;
556 }
557
558 end:
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 }
578
579 *user_result = bt_private_value_borrow_value(result);
580 return status;
581 }
This page took 0.044128 seconds and 4 git commands to generate.