Commit | Line | Data |
---|---|---|
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 | ||
98903a3e PP |
27 | #define BT_LOG_OUTPUT_LEVEL log_level |
28 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY" | |
29 | #include "logging/log.h" | |
30 | ||
04c0ba87 JG |
31 | #include "query.h" |
32 | #include <stdbool.h> | |
578e048b | 33 | #include "common/assert.h" |
04c0ba87 JG |
34 | #include "metadata.h" |
35 | #include "../common/metadata/decoder.h" | |
578e048b | 36 | #include "common/common.h" |
91d81473 | 37 | #include "common/macros.h" |
3fadfbc0 | 38 | #include <babeltrace2/babeltrace.h> |
97ade20b | 39 | #include "fs.h" |
d23b766e | 40 | #include "logging/comp-logging.h" |
55314f2a | 41 | |
04c0ba87 JG |
42 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
43 | ||
97ade20b JG |
44 | struct range { |
45 | int64_t begin_ns; | |
46 | int64_t end_ns; | |
47 | bool set; | |
48 | }; | |
49 | ||
04c0ba87 | 50 | BT_HIDDEN |
d24d5663 | 51 | bt_component_class_query_method_status metadata_info_query( |
d23b766e | 52 | bt_self_component_class_source *self_comp_class_src, |
98903a3e | 53 | const bt_value *params, bt_logging_level log_level, |
b19ff26f | 54 | const bt_value **user_result) |
04c0ba87 | 55 | { |
d24d5663 PP |
56 | bt_component_class_query_method_status status = |
57 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
d23b766e SM |
58 | bt_self_component_class *self_comp_class = |
59 | bt_self_component_class_source_as_self_component_class(self_comp_class_src); | |
b19ff26f PP |
60 | bt_value *result = NULL; |
61 | const bt_value *path_value = NULL; | |
04c0ba87 JG |
62 | char *metadata_text = NULL; |
63 | FILE *metadata_fp = NULL; | |
64 | GString *g_metadata_text = NULL; | |
65 | int ret; | |
66 | int bo; | |
67 | const char *path; | |
68 | bool is_packetized; | |
69 | ||
05e21286 | 70 | result = bt_value_map_create(); |
da91b29a | 71 | if (!result) { |
d24d5663 | 72 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
04c0ba87 JG |
73 | goto error; |
74 | } | |
75 | ||
f6ccaed9 PP |
76 | BT_ASSERT(params); |
77 | ||
04c0ba87 | 78 | if (!bt_value_is_map(params)) { |
d23b766e SM |
79 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
80 | "Query parameters is not a map value object."); | |
a635e507 | 81 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
04c0ba87 JG |
82 | goto error; |
83 | } | |
84 | ||
05e21286 | 85 | path_value = bt_value_map_borrow_entry_value_const(params, "path"); |
07330b50 | 86 | if (!path_value) { |
d23b766e SM |
87 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
88 | "Mandatory `path` parameter missing"); | |
a635e507 | 89 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
07330b50 FD |
90 | goto error; |
91 | } | |
92 | ||
93 | if (!bt_value_is_string(path_value)) { | |
d23b766e SM |
94 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
95 | "`path` parameter is required to be a string value"); | |
a635e507 | 96 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
07330b50 FD |
97 | goto error; |
98 | } | |
99 | ||
601b0d3c | 100 | path = bt_value_string_get(path_value); |
04c0ba87 | 101 | |
f6ccaed9 | 102 | BT_ASSERT(path); |
04c0ba87 JG |
103 | metadata_fp = ctf_fs_metadata_open_file(path); |
104 | if (!metadata_fp) { | |
d23b766e SM |
105 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
106 | "Cannot open trace metadata: path=\"%s\".", path); | |
04c0ba87 JG |
107 | goto error; |
108 | } | |
109 | ||
110 | is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp, | |
98903a3e | 111 | &bo, log_level, NULL); |
04c0ba87 JG |
112 | |
113 | if (is_packetized) { | |
114 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf( | |
3c8252a5 PP |
115 | metadata_fp, &metadata_text, bo, NULL, NULL, |
116 | log_level, NULL); | |
04c0ba87 | 117 | if (ret) { |
d23b766e SM |
118 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
119 | "Cannot decode packetized metadata file: path=\"%s\"", | |
f7d0e29c | 120 | path); |
04c0ba87 JG |
121 | goto error; |
122 | } | |
123 | } else { | |
124 | long filesize; | |
125 | ||
677e1a21 MD |
126 | ret = fseek(metadata_fp, 0, SEEK_END); |
127 | if (ret) { | |
d23b766e SM |
128 | BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class, |
129 | "Failed to seek to the end of the metadata file", | |
f7d0e29c | 130 | ": path=\"%s\"", path); |
677e1a21 MD |
131 | goto error; |
132 | } | |
04c0ba87 | 133 | filesize = ftell(metadata_fp); |
677e1a21 | 134 | if (filesize < 0) { |
d23b766e SM |
135 | BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class, |
136 | "Failed to get the current position in the metadata file", | |
f7d0e29c | 137 | ": path=\"%s\"", path); |
677e1a21 MD |
138 | goto error; |
139 | } | |
04c0ba87 JG |
140 | rewind(metadata_fp); |
141 | metadata_text = malloc(filesize + 1); | |
142 | if (!metadata_text) { | |
d23b766e SM |
143 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
144 | "Cannot allocate buffer for metadata text."); | |
04c0ba87 JG |
145 | goto error; |
146 | } | |
147 | ||
148 | if (fread(metadata_text, filesize, 1, metadata_fp) != 1) { | |
d23b766e SM |
149 | BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class, |
150 | "Cannot read metadata file", ": path=\"%s\"", | |
f7d0e29c | 151 | path); |
04c0ba87 JG |
152 | goto error; |
153 | } | |
154 | ||
155 | metadata_text[filesize] = '\0'; | |
156 | } | |
157 | ||
158 | g_metadata_text = g_string_new(NULL); | |
159 | if (!g_metadata_text) { | |
160 | goto error; | |
161 | } | |
162 | ||
163 | if (strncmp(metadata_text, METADATA_TEXT_SIG, | |
164 | sizeof(METADATA_TEXT_SIG) - 1) != 0) { | |
165 | g_string_assign(g_metadata_text, METADATA_TEXT_SIG); | |
166 | g_string_append(g_metadata_text, " */\n\n"); | |
167 | } | |
168 | ||
169 | g_string_append(g_metadata_text, metadata_text); | |
170 | ||
05e21286 | 171 | ret = bt_value_map_insert_string_entry(result, "text", |
04c0ba87 JG |
172 | g_metadata_text->str); |
173 | if (ret) { | |
d23b766e SM |
174 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
175 | "Cannot insert metadata text into query result."); | |
04c0ba87 JG |
176 | goto error; |
177 | } | |
178 | ||
05e21286 | 179 | ret = bt_value_map_insert_bool_entry(result, "is-packetized", |
04c0ba87 JG |
180 | is_packetized); |
181 | if (ret) { | |
d23b766e SM |
182 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
183 | "Cannot insert \"is-packetized\" attribute into query result."); | |
04c0ba87 JG |
184 | goto error; |
185 | } | |
186 | ||
187 | goto end; | |
188 | ||
189 | error: | |
c5b9b441 | 190 | BT_VALUE_PUT_REF_AND_RESET(result); |
d94d92ac | 191 | result = NULL; |
c7eee084 | 192 | |
d94d92ac | 193 | if (status >= 0) { |
d24d5663 | 194 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
c7eee084 | 195 | } |
04c0ba87 JG |
196 | |
197 | end: | |
04c0ba87 JG |
198 | free(metadata_text); |
199 | ||
200 | if (g_metadata_text) { | |
201 | g_string_free(g_metadata_text, TRUE); | |
202 | } | |
203 | ||
204 | if (metadata_fp) { | |
205 | fclose(metadata_fp); | |
206 | } | |
c7eee084 | 207 | |
05e21286 | 208 | *user_result = result; |
d94d92ac | 209 | return status; |
97ade20b | 210 | } |
9ec238a8 | 211 | |
97ade20b | 212 | static |
b19ff26f | 213 | int add_range(bt_value *info, struct range *range, |
97ade20b JG |
214 | const char *range_name) |
215 | { | |
216 | int ret = 0; | |
d24d5663 | 217 | bt_value_map_insert_entry_status status; |
b19ff26f | 218 | bt_value *range_map = NULL; |
97ade20b JG |
219 | |
220 | if (!range->set) { | |
221 | /* Not an error. */ | |
222 | goto end; | |
223 | } | |
224 | ||
05e21286 | 225 | range_map = bt_value_map_create(); |
97ade20b JG |
226 | if (!range_map) { |
227 | ret = -1; | |
228 | goto end; | |
229 | } | |
230 | ||
fdd3a2da | 231 | status = bt_value_map_insert_signed_integer_entry(range_map, "begin", |
97ade20b | 232 | range->begin_ns); |
d24d5663 | 233 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
234 | ret = -1; |
235 | goto end; | |
236 | } | |
237 | ||
fdd3a2da | 238 | status = bt_value_map_insert_signed_integer_entry(range_map, "end", |
97ade20b | 239 | range->end_ns); |
d24d5663 | 240 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
241 | ret = -1; |
242 | goto end; | |
243 | } | |
244 | ||
05e21286 PP |
245 | status = bt_value_map_insert_entry(info, range_name, |
246 | range_map); | |
d24d5663 | 247 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
248 | ret = -1; |
249 | goto end; | |
250 | } | |
da91b29a | 251 | |
97ade20b | 252 | end: |
c5b9b441 | 253 | bt_value_put_ref(range_map); |
97ade20b JG |
254 | return ret; |
255 | } | |
256 | ||
257 | static | |
60363f2f | 258 | int add_stream_ids(bt_value *info, struct ctf_fs_ds_file_group *ds_file_group) |
97ade20b JG |
259 | { |
260 | int ret = 0; | |
d24d5663 | 261 | bt_value_map_insert_entry_status status; |
97ade20b | 262 | |
60363f2f | 263 | if (ds_file_group->stream_id != UINT64_C(-1)) { |
fdd3a2da PP |
264 | status = bt_value_map_insert_unsigned_integer_entry(info, "id", |
265 | ds_file_group->stream_id); | |
d24d5663 | 266 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
267 | ret = -1; |
268 | goto end; | |
269 | } | |
270 | } | |
271 | ||
fdd3a2da PP |
272 | status = bt_value_map_insert_unsigned_integer_entry(info, "class-id", |
273 | ds_file_group->sc->id); | |
d24d5663 | 274 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
275 | ret = -1; |
276 | goto end; | |
277 | } | |
0b29603d | 278 | |
97ade20b | 279 | end: |
97ade20b JG |
280 | return ret; |
281 | } | |
282 | ||
283 | static | |
284 | int populate_stream_info(struct ctf_fs_ds_file_group *group, | |
b19ff26f | 285 | bt_value *group_info, struct range *stream_range) |
97ade20b JG |
286 | { |
287 | int ret = 0; | |
288 | size_t file_idx; | |
d24d5663 PP |
289 | bt_value_map_insert_entry_status insert_status; |
290 | bt_value_array_append_element_status append_status; | |
b19ff26f | 291 | bt_value *file_paths; |
b1c001b6 | 292 | struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry; |
a38d7650 | 293 | gchar *port_name = NULL; |
97ade20b | 294 | |
05e21286 | 295 | file_paths = bt_value_array_create(); |
97ade20b JG |
296 | if (!file_paths) { |
297 | ret = -1; | |
298 | goto end; | |
299 | } | |
300 | ||
301 | for (file_idx = 0; file_idx < group->ds_file_infos->len; file_idx++) { | |
97ade20b | 302 | struct ctf_fs_ds_file_info *info = |
60363f2f PP |
303 | g_ptr_array_index(group->ds_file_infos, |
304 | file_idx); | |
97ade20b | 305 | |
d24d5663 | 306 | append_status = bt_value_array_append_string_element(file_paths, |
97ade20b | 307 | info->path->str); |
d24d5663 | 308 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
97ade20b JG |
309 | ret = -1; |
310 | goto end; | |
311 | } | |
97ade20b JG |
312 | } |
313 | ||
b1c001b6 | 314 | /* |
7ed5243a FD |
315 | * Since each `struct ctf_fs_ds_file_group` has a sorted array of |
316 | * `struct ctf_fs_ds_index_entry`, we can compute the stream range from | |
317 | * the timestamp_begin of the first index entry and the timestamp_end | |
318 | * of the last index entry. | |
b1c001b6 | 319 | */ |
7ed5243a FD |
320 | BT_ASSERT(group->index); |
321 | BT_ASSERT(group->index->entries); | |
322 | BT_ASSERT(group->index->entries->len > 0); | |
b1c001b6 | 323 | |
7ed5243a FD |
324 | /* First entry. */ |
325 | first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( | |
326 | group->index->entries, 0); | |
b1c001b6 | 327 | |
7ed5243a FD |
328 | /* Last entry. */ |
329 | last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( | |
330 | group->index->entries, group->index->entries->len - 1); | |
b1c001b6 FD |
331 | |
332 | stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns; | |
333 | stream_range->end_ns = last_ds_index_entry->timestamp_end_ns; | |
b1c001b6 | 334 | |
1d4ac4b6 FD |
335 | /* |
336 | * If any of the begin and end timestamps is not set it means that | |
337 | * packets don't include `timestamp_begin` _and_ `timestamp_end` fields | |
338 | * in their packet context so we can't set the range. | |
339 | */ | |
340 | stream_range->set = stream_range->begin_ns != UINT64_C(-1) && | |
341 | stream_range->end_ns != UINT64_C(-1); | |
342 | ||
343 | ret = add_range(group_info, stream_range, "range-ns"); | |
344 | if (ret) { | |
345 | goto end; | |
97ade20b JG |
346 | } |
347 | ||
d24d5663 | 348 | insert_status = bt_value_map_insert_entry(group_info, "paths", |
05e21286 | 349 | file_paths); |
d24d5663 | 350 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
351 | ret = -1; |
352 | goto end; | |
353 | } | |
354 | ||
60363f2f | 355 | ret = add_stream_ids(group_info, group); |
97ade20b JG |
356 | if (ret) { |
357 | goto end; | |
358 | } | |
a38d7650 SM |
359 | |
360 | port_name = ctf_fs_make_port_name(group); | |
361 | if (!port_name) { | |
362 | ret = -1; | |
363 | goto end; | |
364 | } | |
365 | ||
d24d5663 PP |
366 | insert_status = bt_value_map_insert_string_entry(group_info, |
367 | "port-name", port_name); | |
368 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
a38d7650 SM |
369 | ret = -1; |
370 | goto end; | |
371 | } | |
372 | ||
97ade20b | 373 | end: |
19bbdc9b | 374 | g_free(port_name); |
c5b9b441 | 375 | bt_value_put_ref(file_paths); |
97ade20b JG |
376 | return ret; |
377 | } | |
378 | ||
379 | static | |
f280892e | 380 | int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info) |
97ade20b JG |
381 | { |
382 | int ret = 0; | |
383 | size_t group_idx; | |
d24d5663 PP |
384 | bt_value_map_insert_entry_status insert_status; |
385 | bt_value_array_append_element_status append_status; | |
f280892e | 386 | bt_value *file_groups = NULL; |
97ade20b JG |
387 | struct range trace_range = { |
388 | .begin_ns = INT64_MAX, | |
389 | .end_ns = 0, | |
390 | .set = false, | |
391 | }; | |
392 | struct range trace_intersection = { | |
393 | .begin_ns = 0, | |
394 | .end_ns = INT64_MAX, | |
395 | .set = false, | |
396 | }; | |
397 | ||
f280892e SM |
398 | BT_ASSERT(trace->ds_file_groups); |
399 | /* Add trace range info only if it contains streams. */ | |
400 | if (trace->ds_file_groups->len == 0) { | |
401 | ret = -1; | |
402 | goto end; | |
403 | } | |
404 | ||
05e21286 | 405 | file_groups = bt_value_array_create(); |
97ade20b JG |
406 | if (!file_groups) { |
407 | goto end; | |
408 | } | |
409 | ||
d24d5663 | 410 | insert_status = bt_value_map_insert_string_entry(trace_info, "name", |
f280892e | 411 | trace->name->str); |
d24d5663 | 412 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
413 | ret = -1; |
414 | goto end; | |
415 | } | |
d24d5663 | 416 | insert_status = bt_value_map_insert_string_entry(trace_info, "path", |
f280892e | 417 | trace->path->str); |
d24d5663 | 418 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
419 | ret = -1; |
420 | goto end; | |
421 | } | |
422 | ||
97ade20b JG |
423 | /* Find range of all stream groups, and of the trace. */ |
424 | for (group_idx = 0; group_idx < trace->ds_file_groups->len; | |
425 | group_idx++) { | |
b19ff26f | 426 | bt_value *group_info; |
97ade20b JG |
427 | struct range group_range = { .set = false }; |
428 | struct ctf_fs_ds_file_group *group = g_ptr_array_index( | |
429 | trace->ds_file_groups, group_idx); | |
430 | ||
05e21286 | 431 | group_info = bt_value_map_create(); |
97ade20b JG |
432 | if (!group_info) { |
433 | ret = -1; | |
434 | goto end; | |
435 | } | |
436 | ||
437 | ret = populate_stream_info(group, group_info, &group_range); | |
438 | if (ret) { | |
c5b9b441 | 439 | bt_value_put_ref(group_info); |
97ade20b JG |
440 | goto end; |
441 | } | |
442 | ||
d24d5663 PP |
443 | append_status = bt_value_array_append_element(file_groups, |
444 | group_info); | |
7233204d | 445 | bt_value_put_ref(group_info); |
d24d5663 | 446 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
7233204d FD |
447 | goto end; |
448 | } | |
449 | ||
97ade20b | 450 | if (group_range.set) { |
91d81473 | 451 | trace_range.begin_ns = MIN(trace_range.begin_ns, |
97ade20b | 452 | group_range.begin_ns); |
91d81473 | 453 | trace_range.end_ns = MAX(trace_range.end_ns, |
97ade20b JG |
454 | group_range.end_ns); |
455 | trace_range.set = true; | |
456 | ||
91d81473 | 457 | trace_intersection.begin_ns = MAX(trace_intersection.begin_ns, |
97ade20b | 458 | group_range.begin_ns); |
91d81473 | 459 | trace_intersection.end_ns = MIN(trace_intersection.end_ns, |
97ade20b JG |
460 | group_range.end_ns); |
461 | trace_intersection.set = true; | |
97ade20b JG |
462 | } |
463 | } | |
464 | ||
465 | ret = add_range(trace_info, &trace_range, "range-ns"); | |
466 | if (ret) { | |
467 | goto end; | |
468 | } | |
c6daf8b2 PP |
469 | |
470 | if (trace_intersection.begin_ns < trace_intersection.end_ns) { | |
471 | ret = add_range(trace_info, &trace_intersection, | |
472 | "intersection-range-ns"); | |
473 | if (ret) { | |
474 | goto end; | |
475 | } | |
97ade20b JG |
476 | } |
477 | ||
d24d5663 | 478 | insert_status = bt_value_map_insert_entry(trace_info, "streams", |
05e21286 | 479 | file_groups); |
c5b9b441 | 480 | BT_VALUE_PUT_REF_AND_RESET(file_groups); |
d24d5663 | 481 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
482 | ret = -1; |
483 | goto end; | |
484 | } | |
485 | ||
486 | end: | |
c5b9b441 | 487 | bt_value_put_ref(file_groups); |
97ade20b JG |
488 | return ret; |
489 | } | |
490 | ||
491 | BT_HIDDEN | |
d24d5663 | 492 | bt_component_class_query_method_status trace_info_query( |
d23b766e | 493 | bt_self_component_class_source *self_comp_class_src, |
98903a3e | 494 | const bt_value *params, bt_logging_level log_level, |
b19ff26f | 495 | const bt_value **user_result) |
97ade20b | 496 | { |
f280892e | 497 | struct ctf_fs_component *ctf_fs = NULL; |
d24d5663 PP |
498 | bt_component_class_query_method_status status = |
499 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
d23b766e SM |
500 | bt_self_component_class *self_comp_class = |
501 | bt_self_component_class_source_as_self_component_class( | |
502 | self_comp_class_src); | |
b19ff26f | 503 | bt_value *result = NULL; |
73760435 | 504 | const bt_value *inputs_value = NULL; |
97ade20b | 505 | int ret = 0; |
f280892e | 506 | guint i; |
97ade20b | 507 | |
f6ccaed9 PP |
508 | BT_ASSERT(params); |
509 | ||
97ade20b | 510 | if (!bt_value_is_map(params)) { |
d23b766e SM |
511 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
512 | "Query parameters is not a map value object."); | |
a635e507 | 513 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
97ade20b JG |
514 | goto error; |
515 | } | |
516 | ||
4c65a157 | 517 | ctf_fs = ctf_fs_component_create(log_level, NULL); |
d907165c | 518 | if (!ctf_fs) { |
97ade20b JG |
519 | goto error; |
520 | } | |
97ade20b | 521 | |
d23b766e SM |
522 | if (!read_src_fs_parameters(params, &inputs_value, ctf_fs, NULL, |
523 | self_comp_class)) { | |
a635e507 | 524 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
97ade20b JG |
525 | goto error; |
526 | } | |
527 | ||
d23b766e SM |
528 | if (ctf_fs_component_create_ctf_fs_traces(ctf_fs, inputs_value, NULL, |
529 | self_comp_class)) { | |
97ade20b JG |
530 | goto error; |
531 | } | |
532 | ||
05e21286 | 533 | result = bt_value_array_create(); |
da91b29a | 534 | if (!result) { |
d24d5663 | 535 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
97ade20b JG |
536 | goto error; |
537 | } | |
538 | ||
f280892e SM |
539 | for (i = 0; i < ctf_fs->traces->len; i++) { |
540 | struct ctf_fs_trace *trace; | |
b19ff26f | 541 | bt_value *trace_info; |
d24d5663 | 542 | bt_value_array_append_element_status append_status; |
f280892e SM |
543 | |
544 | trace = g_ptr_array_index(ctf_fs->traces, i); | |
545 | BT_ASSERT(trace); | |
97ade20b | 546 | |
05e21286 | 547 | trace_info = bt_value_map_create(); |
97ade20b | 548 | if (!trace_info) { |
d23b766e SM |
549 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
550 | "Failed to create trace info map."); | |
97ade20b JG |
551 | goto error; |
552 | } | |
553 | ||
f280892e | 554 | ret = populate_trace_info(trace, trace_info); |
97ade20b | 555 | if (ret) { |
c5b9b441 | 556 | bt_value_put_ref(trace_info); |
97ade20b JG |
557 | goto error; |
558 | } | |
559 | ||
d24d5663 PP |
560 | append_status = bt_value_array_append_element(result, |
561 | trace_info); | |
c5b9b441 | 562 | bt_value_put_ref(trace_info); |
d24d5663 | 563 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
97ade20b JG |
564 | goto error; |
565 | } | |
566 | } | |
567 | ||
568 | goto end; | |
569 | ||
570 | error: | |
c5b9b441 | 571 | BT_VALUE_PUT_REF_AND_RESET(result); |
d94d92ac | 572 | result = NULL; |
c7eee084 | 573 | |
d94d92ac | 574 | if (status >= 0) { |
d24d5663 | 575 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
c7eee084 PP |
576 | } |
577 | ||
97ade20b | 578 | end: |
f280892e SM |
579 | if (ctf_fs) { |
580 | ctf_fs_destroy(ctf_fs); | |
581 | ctf_fs = NULL; | |
97ade20b | 582 | } |
d94d92ac | 583 | |
05e21286 | 584 | *user_result = result; |
d94d92ac | 585 | return status; |
04c0ba87 | 586 | } |
73760435 SM |
587 | |
588 | BT_HIDDEN | |
589 | bt_component_class_query_method_status support_info_query( | |
590 | bt_self_component_class_source *comp_class, | |
591 | const bt_value *params, bt_logging_level log_level, | |
592 | const bt_value **user_result) | |
593 | { | |
594 | const bt_value *input_type_value; | |
595 | const char *input_type; | |
596 | bt_component_class_query_method_status status; | |
597 | double weight = 0; | |
598 | gchar *metadata_path = NULL; | |
599 | bt_value *result = NULL; | |
600 | ||
601 | input_type_value = bt_value_map_borrow_entry_value_const(params, "type"); | |
602 | BT_ASSERT(input_type_value); | |
603 | BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING); | |
604 | input_type = bt_value_string_get(input_type_value); | |
605 | ||
606 | result = bt_value_map_create(); | |
607 | if (!result) { | |
608 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
609 | goto end; | |
610 | } | |
611 | ||
612 | if (strcmp(input_type, "directory") == 0) { | |
613 | const bt_value *input_value; | |
614 | const char *path; | |
615 | ||
616 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); | |
617 | BT_ASSERT(input_value); | |
618 | BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING); | |
619 | path = bt_value_string_get(input_value); | |
620 | ||
621 | metadata_path = g_build_filename(path, CTF_FS_METADATA_FILENAME, NULL); | |
622 | if (!metadata_path) { | |
623 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
624 | goto end; | |
625 | } | |
626 | ||
627 | /* | |
628 | * If the metadata file exists in this directory, consider it to | |
629 | * be a CTF trace. | |
630 | */ | |
631 | if (g_file_test(metadata_path, G_FILE_TEST_EXISTS)) { | |
632 | weight = 0.5; | |
633 | } | |
634 | } | |
635 | ||
636 | if (bt_value_map_insert_real_entry(result, "weight", weight) != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
637 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
638 | goto end; | |
639 | } | |
640 | ||
641 | /* | |
642 | * Use the arbitrary constant string "ctf" as the group, such that all | |
643 | * found ctf traces are passed to the same instance of src.ctf.fs. | |
644 | */ | |
645 | if (bt_value_map_insert_string_entry(result, "group", "ctf") != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
646 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
647 | goto end; | |
648 | } | |
649 | ||
650 | *user_result = result; | |
651 | result = NULL; | |
652 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
653 | ||
654 | end: | |
655 | g_free(metadata_path); | |
656 | bt_value_put_ref(result); | |
657 | ||
658 | return status; | |
659 | } |