Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
ea0b4b9e | 2 | * fs.c |
7a278c8e | 3 | * |
ea0b4b9e | 4 | * Babeltrace CTF file system Reader Component |
7a278c8e | 5 | * |
1a9f7075 | 6 | * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com> |
f3bc2010 | 7 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7a278c8e | 8 | * |
7a278c8e JG |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
578e048b | 28 | #include "common/common.h" |
3fadfbc0 | 29 | #include <babeltrace2/babeltrace.h> |
578e048b | 30 | #include "compat/uuid.h" |
ea0b4b9e | 31 | #include <glib.h> |
578e048b | 32 | #include "common/assert.h" |
94cf822e | 33 | #include <inttypes.h> |
c55a9f58 | 34 | #include <stdbool.h> |
56a1cced | 35 | #include "fs.h" |
413bc2c4 | 36 | #include "metadata.h" |
94cf822e | 37 | #include "data-stream-file.h" |
e7a4393b | 38 | #include "file.h" |
1e649dff | 39 | #include "../common/metadata/decoder.h" |
d6e69534 | 40 | #include "../common/msg-iter/msg-iter.h" |
a429c321 | 41 | #include "../common/utils/utils.h" |
04c0ba87 | 42 | #include "query.h" |
e7a4393b | 43 | |
350ad6c1 | 44 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS" |
55314f2a | 45 | #include "logging.h" |
ea0b4b9e | 46 | |
94cf822e | 47 | static |
d6e69534 | 48 | int msg_iter_data_set_current_ds_file(struct ctf_fs_msg_iter_data *msg_iter_data) |
94cf822e PP |
49 | { |
50 | struct ctf_fs_ds_file_info *ds_file_info; | |
51 | int ret = 0; | |
52 | ||
d6e69534 PP |
53 | BT_ASSERT(msg_iter_data->ds_file_info_index < |
54 | msg_iter_data->ds_file_group->ds_file_infos->len); | |
94cf822e | 55 | ds_file_info = g_ptr_array_index( |
d6e69534 PP |
56 | msg_iter_data->ds_file_group->ds_file_infos, |
57 | msg_iter_data->ds_file_info_index); | |
58 | ||
59 | ctf_fs_ds_file_destroy(msg_iter_data->ds_file); | |
60 | msg_iter_data->ds_file = ctf_fs_ds_file_create( | |
61 | msg_iter_data->ds_file_group->ctf_fs_trace, | |
62 | msg_iter_data->pc_msg_iter, | |
63 | msg_iter_data->msg_iter, | |
64 | msg_iter_data->ds_file_group->stream, | |
97ade20b | 65 | ds_file_info->path->str); |
d6e69534 | 66 | if (!msg_iter_data->ds_file) { |
94cf822e PP |
67 | ret = -1; |
68 | } | |
69 | ||
70 | return ret; | |
71 | } | |
72 | ||
73 | static | |
d6e69534 PP |
74 | void ctf_fs_msg_iter_data_destroy( |
75 | struct ctf_fs_msg_iter_data *msg_iter_data) | |
94cf822e | 76 | { |
d6e69534 | 77 | if (!msg_iter_data) { |
94cf822e PP |
78 | return; |
79 | } | |
80 | ||
d6e69534 | 81 | ctf_fs_ds_file_destroy(msg_iter_data->ds_file); |
6de92955 | 82 | |
d6e69534 PP |
83 | if (msg_iter_data->msg_iter) { |
84 | bt_msg_iter_destroy(msg_iter_data->msg_iter); | |
6de92955 PP |
85 | } |
86 | ||
d6e69534 | 87 | g_free(msg_iter_data); |
94cf822e PP |
88 | } |
89 | ||
fc917f65 PP |
90 | static |
91 | void set_msg_iter_emits_stream_beginning_end_messages( | |
92 | struct ctf_fs_msg_iter_data *msg_iter_data) | |
93 | { | |
94 | bt_msg_iter_set_emit_stream_beginning_message( | |
95 | msg_iter_data->ds_file->msg_iter, | |
96 | msg_iter_data->ds_file_info_index == 0); | |
97 | bt_msg_iter_set_emit_stream_end_message( | |
98 | msg_iter_data->ds_file->msg_iter, | |
99 | msg_iter_data->ds_file_info_index == | |
100 | msg_iter_data->ds_file_group->ds_file_infos->len - 1); | |
101 | } | |
102 | ||
d4393e08 | 103 | static |
4cdfc5e8 | 104 | bt_self_message_iterator_status ctf_fs_iterator_next_one( |
d6e69534 | 105 | struct ctf_fs_msg_iter_data *msg_iter_data, |
fc917f65 | 106 | const bt_message **out_msg) |
ea0b4b9e | 107 | { |
4cdfc5e8 | 108 | bt_self_message_iterator_status status; |
94cf822e | 109 | |
d6e69534 | 110 | BT_ASSERT(msg_iter_data->ds_file); |
f42867e2 | 111 | |
fc917f65 PP |
112 | while (true) { |
113 | bt_message *msg; | |
114 | ||
115 | status = ctf_fs_ds_file_next(msg_iter_data->ds_file, &msg); | |
116 | switch (status) { | |
117 | case BT_SELF_MESSAGE_ITERATOR_STATUS_OK: | |
118 | *out_msg = msg; | |
119 | msg = NULL; | |
f42867e2 | 120 | goto end; |
fc917f65 PP |
121 | case BT_SELF_MESSAGE_ITERATOR_STATUS_END: |
122 | { | |
123 | int ret; | |
f42867e2 | 124 | |
fc917f65 PP |
125 | if (msg_iter_data->ds_file_info_index == |
126 | msg_iter_data->ds_file_group->ds_file_infos->len - 1) { | |
127 | /* End of all group's stream files */ | |
128 | goto end; | |
129 | } | |
130 | ||
131 | msg_iter_data->ds_file_info_index++; | |
495490c5 PP |
132 | bt_msg_iter_reset_for_next_stream_file( |
133 | msg_iter_data->msg_iter); | |
fc917f65 PP |
134 | set_msg_iter_emits_stream_beginning_end_messages( |
135 | msg_iter_data); | |
94cf822e | 136 | |
94cf822e | 137 | /* |
fc917f65 PP |
138 | * Open and start reading the next stream file |
139 | * within our stream file group. | |
94cf822e | 140 | */ |
fc917f65 PP |
141 | ret = msg_iter_data_set_current_ds_file(msg_iter_data); |
142 | if (ret) { | |
143 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
144 | goto end; | |
145 | } | |
146 | ||
147 | /* Continue the loop to get the next message */ | |
148 | break; | |
94cf822e | 149 | } |
fc917f65 | 150 | default: |
94cf822e PP |
151 | goto end; |
152 | } | |
94cf822e | 153 | } |
d01e0f33 | 154 | |
94cf822e | 155 | end: |
d4393e08 PP |
156 | return status; |
157 | } | |
158 | ||
159 | BT_HIDDEN | |
4cdfc5e8 | 160 | bt_self_message_iterator_status ctf_fs_iterator_next( |
d6e69534 PP |
161 | bt_self_message_iterator *iterator, |
162 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 PP |
163 | uint64_t *count) |
164 | { | |
4cdfc5e8 | 165 | bt_self_message_iterator_status status = |
d6e69534 PP |
166 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
167 | struct ctf_fs_msg_iter_data *msg_iter_data = | |
168 | bt_self_message_iterator_get_data(iterator); | |
d4393e08 PP |
169 | uint64_t i = 0; |
170 | ||
d6e69534 PP |
171 | while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
172 | status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]); | |
173 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
d4393e08 PP |
174 | i++; |
175 | } | |
176 | } | |
177 | ||
178 | if (i > 0) { | |
179 | /* | |
180 | * Even if ctf_fs_iterator_next_one() returned something | |
d6e69534 PP |
181 | * else than BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we |
182 | * accumulated message objects in the output | |
183 | * message array, so we need to return | |
184 | * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they are | |
d4393e08 | 185 | * transfered to downstream. This other status occurs |
d6e69534 | 186 | * again the next time muxer_msg_iter_do_next() is |
d4393e08 | 187 | * called, possibly without any accumulated |
d6e69534 | 188 | * message, in which case we'll return it. |
d4393e08 PP |
189 | */ |
190 | *count = i; | |
d6e69534 | 191 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
192 | } |
193 | ||
194 | return status; | |
ea0b4b9e | 195 | } |
bfd20a42 | 196 | |
6a9bb5e9 PP |
197 | static |
198 | int ctf_fs_iterator_reset(struct ctf_fs_msg_iter_data *msg_iter_data) | |
199 | { | |
200 | int ret; | |
201 | ||
202 | msg_iter_data->ds_file_info_index = 0; | |
203 | ret = msg_iter_data_set_current_ds_file(msg_iter_data); | |
204 | if (ret) { | |
205 | goto end; | |
206 | } | |
207 | ||
208 | bt_msg_iter_reset(msg_iter_data->msg_iter); | |
209 | set_msg_iter_emits_stream_beginning_end_messages(msg_iter_data); | |
210 | ||
211 | end: | |
212 | return ret; | |
213 | } | |
214 | ||
215 | BT_HIDDEN | |
216 | bt_self_message_iterator_status ctf_fs_iterator_seek_beginning( | |
217 | bt_self_message_iterator *it) | |
218 | { | |
219 | struct ctf_fs_msg_iter_data *msg_iter_data = | |
220 | bt_self_message_iterator_get_data(it); | |
221 | bt_self_message_iterator_status status = | |
222 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
223 | ||
224 | BT_ASSERT(msg_iter_data); | |
225 | if (ctf_fs_iterator_reset(msg_iter_data)) { | |
226 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
227 | } | |
228 | ||
229 | return status; | |
230 | } | |
231 | ||
232 | BT_HIDDEN | |
d6e69534 | 233 | void ctf_fs_iterator_finalize(bt_self_message_iterator *it) |
760051fa | 234 | { |
d6e69534 PP |
235 | ctf_fs_msg_iter_data_destroy( |
236 | bt_self_message_iterator_get_data(it)); | |
760051fa JG |
237 | } |
238 | ||
6a9bb5e9 | 239 | BT_HIDDEN |
4cdfc5e8 | 240 | bt_self_message_iterator_status ctf_fs_iterator_init( |
d6e69534 | 241 | bt_self_message_iterator *self_msg_iter, |
b19ff26f PP |
242 | bt_self_component_source *self_comp, |
243 | bt_self_component_port_output *self_port) | |
4c1456f0 | 244 | { |
4f1f88a6 | 245 | struct ctf_fs_port_data *port_data; |
d6e69534 | 246 | struct ctf_fs_msg_iter_data *msg_iter_data = NULL; |
4cdfc5e8 | 247 | bt_self_message_iterator_status ret = |
d6e69534 | 248 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
760051fa | 249 | |
d94d92ac | 250 | port_data = bt_self_component_port_get_data( |
707b7d35 | 251 | bt_self_component_port_output_as_self_component_port( |
d94d92ac PP |
252 | self_port)); |
253 | BT_ASSERT(port_data); | |
d6e69534 PP |
254 | msg_iter_data = g_new0(struct ctf_fs_msg_iter_data, 1); |
255 | if (!msg_iter_data) { | |
256 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM; | |
94cf822e PP |
257 | goto error; |
258 | } | |
259 | ||
d6e69534 PP |
260 | msg_iter_data->pc_msg_iter = self_msg_iter; |
261 | msg_iter_data->msg_iter = bt_msg_iter_create( | |
44c440bc | 262 | port_data->ds_file_group->ctf_fs_trace->metadata->tc, |
86d8b7b8 | 263 | bt_common_get_page_size(BT_LOG_OUTPUT_LEVEL) * 8, |
ea14c7dd | 264 | ctf_fs_ds_file_medops, NULL, BT_LOG_OUTPUT_LEVEL); |
d6e69534 PP |
265 | if (!msg_iter_data->msg_iter) { |
266 | BT_LOGE_STR("Cannot create a CTF message iterator."); | |
267 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM; | |
6de92955 PP |
268 | goto error; |
269 | } | |
270 | ||
d6e69534 | 271 | msg_iter_data->ds_file_group = port_data->ds_file_group; |
6a9bb5e9 | 272 | if (ctf_fs_iterator_reset(msg_iter_data)) { |
d6e69534 | 273 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
4f1f88a6 | 274 | goto error; |
760051fa JG |
275 | } |
276 | ||
d6e69534 PP |
277 | bt_self_message_iterator_set_data(self_msg_iter, |
278 | msg_iter_data); | |
279 | if (ret != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
4f1f88a6 | 280 | goto error; |
760051fa JG |
281 | } |
282 | ||
d6e69534 | 283 | msg_iter_data = NULL; |
4f1f88a6 | 284 | goto end; |
5b29e799 | 285 | |
4f1f88a6 | 286 | error: |
d6e69534 | 287 | bt_self_message_iterator_set_data(self_msg_iter, NULL); |
4f1f88a6 | 288 | |
760051fa | 289 | end: |
d6e69534 | 290 | ctf_fs_msg_iter_data_destroy(msg_iter_data); |
760051fa JG |
291 | return ret; |
292 | } | |
293 | ||
f280892e | 294 | BT_HIDDEN |
1a9f7075 | 295 | void ctf_fs_destroy(struct ctf_fs_component *ctf_fs) |
760051fa | 296 | { |
4f1f88a6 | 297 | if (!ctf_fs) { |
8fa760ba JG |
298 | return; |
299 | } | |
4f1f88a6 | 300 | |
1a9f7075 PP |
301 | if (ctf_fs->traces) { |
302 | g_ptr_array_free(ctf_fs->traces, TRUE); | |
56a1cced | 303 | } |
4f1f88a6 PP |
304 | |
305 | if (ctf_fs->port_data) { | |
306 | g_ptr_array_free(ctf_fs->port_data, TRUE); | |
c14d7e26 | 307 | } |
760051fa | 308 | |
1a9f7075 PP |
309 | g_free(ctf_fs); |
310 | } | |
311 | ||
f280892e SM |
312 | static |
313 | void port_data_destroy(struct ctf_fs_port_data *port_data) | |
314 | { | |
315 | if (!port_data) { | |
316 | return; | |
317 | } | |
318 | ||
319 | g_free(port_data); | |
320 | } | |
321 | ||
322 | static | |
323 | void port_data_destroy_notifier(void *data) { | |
324 | port_data_destroy(data); | |
325 | } | |
326 | ||
327 | static | |
97ade20b | 328 | void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace) |
1a9f7075 | 329 | { |
1a9f7075 PP |
330 | if (!ctf_fs_trace) { |
331 | return; | |
332 | } | |
333 | ||
94cf822e PP |
334 | if (ctf_fs_trace->ds_file_groups) { |
335 | g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE); | |
336 | } | |
337 | ||
c5b9b441 | 338 | BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace->trace); |
862ca4ed | 339 | |
1a9f7075 PP |
340 | if (ctf_fs_trace->path) { |
341 | g_string_free(ctf_fs_trace->path, TRUE); | |
4f1f88a6 | 342 | } |
760051fa | 343 | |
1a9f7075 PP |
344 | if (ctf_fs_trace->name) { |
345 | g_string_free(ctf_fs_trace->name, TRUE); | |
346 | } | |
347 | ||
348 | if (ctf_fs_trace->metadata) { | |
349 | ctf_fs_metadata_fini(ctf_fs_trace->metadata); | |
350 | g_free(ctf_fs_trace->metadata); | |
351 | } | |
352 | ||
1a9f7075 | 353 | g_free(ctf_fs_trace); |
4c1456f0 JG |
354 | } |
355 | ||
97ade20b | 356 | static |
56a924f4 | 357 | void ctf_fs_trace_destroy_notifier(void *data) |
97ade20b JG |
358 | { |
359 | struct ctf_fs_trace *trace = data; | |
360 | ctf_fs_trace_destroy(trace); | |
361 | } | |
362 | ||
f280892e | 363 | struct ctf_fs_component *ctf_fs_component_create(void) |
a4792757 | 364 | { |
f280892e | 365 | struct ctf_fs_component *ctf_fs; |
a4792757 | 366 | |
f280892e SM |
367 | ctf_fs = g_new0(struct ctf_fs_component, 1); |
368 | if (!ctf_fs) { | |
369 | goto error; | |
370 | } | |
4f1f88a6 | 371 | |
f280892e SM |
372 | ctf_fs->port_data = |
373 | g_ptr_array_new_with_free_func(port_data_destroy_notifier); | |
374 | if (!ctf_fs->port_data) { | |
375 | goto error; | |
4f1f88a6 PP |
376 | } |
377 | ||
f280892e SM |
378 | ctf_fs->traces = |
379 | g_ptr_array_new_with_free_func(ctf_fs_trace_destroy_notifier); | |
380 | if (!ctf_fs->traces) { | |
381 | goto error; | |
382 | } | |
383 | ||
384 | goto end; | |
385 | ||
386 | error: | |
387 | if (ctf_fs) { | |
388 | ctf_fs_destroy(ctf_fs); | |
389 | } | |
390 | ||
391 | end: | |
392 | return ctf_fs; | |
393 | } | |
394 | ||
395 | void ctf_fs_finalize(bt_self_component_source *component) | |
396 | { | |
397 | ctf_fs_destroy(bt_self_component_get_data( | |
398 | bt_self_component_source_as_self_component(component))); | |
5b29e799 JG |
399 | } |
400 | ||
a38d7650 | 401 | gchar *ctf_fs_make_port_name(struct ctf_fs_ds_file_group *ds_file_group) |
547eacf1 | 402 | { |
a38d7650 | 403 | GString *name = g_string_new(NULL); |
547eacf1 | 404 | |
a38d7650 SM |
405 | /* |
406 | * The unique port name is generated by concatenating unique identifiers | |
407 | * for: | |
408 | * | |
409 | * - the trace | |
410 | * - the stream class | |
411 | * - the stream | |
412 | */ | |
413 | ||
414 | /* For the trace, use the uuid if present, else the path. */ | |
415 | if (ds_file_group->ctf_fs_trace->metadata->tc->is_uuid_set) { | |
416 | char uuid_str[BABELTRACE_UUID_STR_LEN]; | |
417 | ||
418 | bt_uuid_unparse(ds_file_group->ctf_fs_trace->metadata->tc->uuid, uuid_str); | |
419 | g_string_assign(name, uuid_str); | |
420 | } else { | |
421 | g_string_assign(name, ds_file_group->ctf_fs_trace->path->str); | |
547eacf1 PP |
422 | } |
423 | ||
424 | /* | |
a38d7650 SM |
425 | * For the stream class, use the id if present. We can omit this field |
426 | * otherwise, as there will only be a single stream class. | |
547eacf1 | 427 | */ |
a38d7650 SM |
428 | if (ds_file_group->sc->id != UINT64_C(-1)) { |
429 | g_string_append_printf(name, " | %" PRIu64, ds_file_group->sc->id); | |
430 | } | |
547eacf1 | 431 | |
a38d7650 SM |
432 | /* For the stream, use the id if present, else, use the path. */ |
433 | if (ds_file_group->stream_id != UINT64_C(-1)) { | |
434 | g_string_append_printf(name, " | %" PRIu64, ds_file_group->stream_id); | |
435 | } else { | |
436 | BT_ASSERT(ds_file_group->ds_file_infos->len == 1); | |
437 | struct ctf_fs_ds_file_info *ds_file_info = | |
438 | g_ptr_array_index(ds_file_group->ds_file_infos, 0); | |
439 | g_string_append_printf(name, " | %s", ds_file_info->path->str); | |
440 | } | |
441 | ||
442 | return g_string_free(name, FALSE); | |
547eacf1 PP |
443 | } |
444 | ||
5b29e799 | 445 | static |
55314f2a JG |
446 | int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, |
447 | struct ctf_fs_trace *ctf_fs_trace, | |
94cf822e | 448 | struct ctf_fs_ds_file_group *ds_file_group) |
5b29e799 | 449 | { |
4f1f88a6 | 450 | int ret = 0; |
4f1f88a6 | 451 | struct ctf_fs_port_data *port_data = NULL; |
a38d7650 | 452 | gchar *port_name; |
4f1f88a6 | 453 | |
a38d7650 | 454 | port_name = ctf_fs_make_port_name(ds_file_group); |
4f1f88a6 PP |
455 | if (!port_name) { |
456 | goto error; | |
457 | } | |
458 | ||
3f7d4d90 | 459 | BT_LOGI("Creating one port named `%s`", port_name); |
4f1f88a6 PP |
460 | |
461 | /* Create output port for this file */ | |
4f1f88a6 PP |
462 | port_data = g_new0(struct ctf_fs_port_data, 1); |
463 | if (!port_data) { | |
464 | goto error; | |
465 | } | |
466 | ||
5c563278 | 467 | port_data->ctf_fs = ctf_fs; |
94cf822e | 468 | port_data->ds_file_group = ds_file_group; |
d94d92ac | 469 | ret = bt_self_component_source_add_output_port( |
a38d7650 | 470 | ctf_fs->self_comp, port_name, port_data, NULL); |
147337a3 | 471 | if (ret) { |
4f1f88a6 PP |
472 | goto error; |
473 | } | |
474 | ||
475 | g_ptr_array_add(ctf_fs->port_data, port_data); | |
476 | port_data = NULL; | |
477 | goto end; | |
478 | ||
479 | error: | |
480 | ret = -1; | |
481 | ||
482 | end: | |
483 | if (port_name) { | |
a38d7650 | 484 | g_free(port_name); |
4f1f88a6 PP |
485 | } |
486 | ||
4f1f88a6 PP |
487 | port_data_destroy(port_data); |
488 | return ret; | |
5b29e799 JG |
489 | } |
490 | ||
491 | static | |
55314f2a JG |
492 | int create_ports_for_trace(struct ctf_fs_component *ctf_fs, |
493 | struct ctf_fs_trace *ctf_fs_trace) | |
94cf822e PP |
494 | { |
495 | int ret = 0; | |
94cf822e PP |
496 | size_t i; |
497 | ||
498 | /* Create one output port for each stream file group */ | |
499 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
500 | struct ctf_fs_ds_file_group *ds_file_group = | |
501 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
502 | ||
55314f2a JG |
503 | ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace, |
504 | ds_file_group); | |
94cf822e | 505 | if (ret) { |
55314f2a | 506 | BT_LOGE("Cannot create output port."); |
94cf822e PP |
507 | goto end; |
508 | } | |
509 | } | |
510 | ||
511 | end: | |
512 | return ret; | |
513 | } | |
514 | ||
94cf822e PP |
515 | static |
516 | void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info) | |
517 | { | |
518 | if (!ds_file_info) { | |
519 | return; | |
520 | } | |
521 | ||
522 | if (ds_file_info->path) { | |
523 | g_string_free(ds_file_info->path, TRUE); | |
524 | } | |
525 | ||
526 | g_free(ds_file_info); | |
527 | } | |
528 | ||
529 | static | |
530 | struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path, | |
7ed5243a | 531 | int64_t begin_ns) |
94cf822e PP |
532 | { |
533 | struct ctf_fs_ds_file_info *ds_file_info; | |
534 | ||
535 | ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1); | |
536 | if (!ds_file_info) { | |
537 | goto end; | |
538 | } | |
539 | ||
540 | ds_file_info->path = g_string_new(path); | |
541 | if (!ds_file_info->path) { | |
542 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
543 | ds_file_info = NULL; | |
544 | goto end; | |
545 | } | |
546 | ||
547 | ds_file_info->begin_ns = begin_ns; | |
548 | ||
549 | end: | |
550 | return ds_file_info; | |
551 | } | |
552 | ||
553 | static | |
554 | void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group) | |
555 | { | |
556 | if (!ds_file_group) { | |
557 | return; | |
558 | } | |
559 | ||
560 | if (ds_file_group->ds_file_infos) { | |
561 | g_ptr_array_free(ds_file_group->ds_file_infos, TRUE); | |
562 | } | |
563 | ||
7ed5243a FD |
564 | if (ds_file_group->index) { |
565 | if (ds_file_group->index->entries) { | |
566 | g_ptr_array_free(ds_file_group->index->entries, TRUE); | |
567 | } | |
568 | g_free(ds_file_group->index); | |
569 | } | |
570 | ||
c5b9b441 | 571 | bt_stream_put_ref(ds_file_group->stream); |
94cf822e PP |
572 | g_free(ds_file_group); |
573 | } | |
574 | ||
575 | static | |
576 | struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create( | |
577 | struct ctf_fs_trace *ctf_fs_trace, | |
60363f2f | 578 | struct ctf_stream_class *sc, |
7ed5243a FD |
579 | uint64_t stream_instance_id, |
580 | struct ctf_fs_ds_index *index) | |
94cf822e PP |
581 | { |
582 | struct ctf_fs_ds_file_group *ds_file_group; | |
583 | ||
94cf822e PP |
584 | ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1); |
585 | if (!ds_file_group) { | |
586 | goto error; | |
587 | } | |
588 | ||
589 | ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func( | |
590 | (GDestroyNotify) ctf_fs_ds_file_info_destroy); | |
591 | if (!ds_file_group->ds_file_infos) { | |
592 | goto error; | |
593 | } | |
594 | ||
7ed5243a FD |
595 | ds_file_group->index = index; |
596 | ||
547eacf1 | 597 | ds_file_group->stream_id = stream_instance_id; |
60363f2f PP |
598 | BT_ASSERT(sc); |
599 | ds_file_group->sc = sc; | |
94cf822e | 600 | ds_file_group->ctf_fs_trace = ctf_fs_trace; |
94cf822e PP |
601 | goto end; |
602 | ||
603 | error: | |
604 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
7ed5243a | 605 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
606 | ds_file_group = NULL; |
607 | ||
608 | end: | |
609 | return ds_file_group; | |
610 | } | |
611 | ||
8bf7105e MJ |
612 | /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */ |
613 | static | |
614 | void array_insert(GPtrArray *array, gpointer element, size_t pos) | |
615 | { | |
616 | size_t original_array_len = array->len; | |
617 | ||
618 | /* Allocate an unused element at the end of the array. */ | |
619 | g_ptr_array_add(array, NULL); | |
620 | ||
621 | /* If we are not inserting at the end, move the elements by one. */ | |
622 | if (pos < original_array_len) { | |
623 | memmove(&(array->pdata[pos + 1]), | |
624 | &(array->pdata[pos]), | |
625 | (original_array_len - pos) * sizeof(gpointer)); | |
626 | } | |
627 | ||
f897d50c | 628 | /* Insert the value. */ |
8bf7105e MJ |
629 | array->pdata[pos] = element; |
630 | } | |
631 | ||
41a65f30 SM |
632 | /* |
633 | * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right | |
634 | * place to keep it sorted. | |
635 | */ | |
636 | ||
637 | static | |
638 | void ds_file_group_insert_ds_file_info_sorted( | |
639 | struct ctf_fs_ds_file_group *ds_file_group, | |
640 | struct ctf_fs_ds_file_info *ds_file_info) | |
641 | { | |
642 | guint i; | |
643 | ||
644 | /* Find the spot where to insert this ds_file_info. */ | |
645 | for (i = 0; i < ds_file_group->ds_file_infos->len; i++) { | |
646 | struct ctf_fs_ds_file_info *other_ds_file_info = | |
647 | g_ptr_array_index(ds_file_group->ds_file_infos, i); | |
648 | ||
649 | if (ds_file_info->begin_ns < other_ds_file_info->begin_ns) { | |
650 | break; | |
651 | } | |
652 | } | |
653 | ||
654 | array_insert(ds_file_group->ds_file_infos, ds_file_info, i); | |
655 | } | |
656 | ||
7ed5243a FD |
657 | static |
658 | void ds_file_group_insert_ds_index_entry_sorted( | |
659 | struct ctf_fs_ds_file_group *ds_file_group, | |
660 | struct ctf_fs_ds_index_entry *entry) | |
661 | { | |
662 | guint i; | |
663 | ||
664 | /* Find the spot where to insert this index entry. */ | |
665 | for (i = 0; i < ds_file_group->index->entries->len; i++) { | |
666 | struct ctf_fs_ds_index_entry *other_entry = g_ptr_array_index( | |
667 | ds_file_group->index->entries, i); | |
668 | ||
669 | if (entry->timestamp_begin_ns < other_entry->timestamp_begin_ns) { | |
670 | break; | |
671 | } | |
672 | } | |
673 | ||
674 | array_insert(ds_file_group->index->entries, entry, i); | |
675 | } | |
676 | ||
41a65f30 SM |
677 | /* |
678 | * Create a new ds_file_info using the provided path, begin_ns and index, then | |
679 | * add it to ds_file_group's list of ds_file_infos. | |
680 | */ | |
681 | ||
94cf822e PP |
682 | static |
683 | int ctf_fs_ds_file_group_add_ds_file_info( | |
684 | struct ctf_fs_ds_file_group *ds_file_group, | |
7ed5243a | 685 | const char *path, int64_t begin_ns) |
94cf822e PP |
686 | { |
687 | struct ctf_fs_ds_file_info *ds_file_info; | |
94cf822e PP |
688 | int ret = 0; |
689 | ||
7ed5243a | 690 | ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns); |
94cf822e PP |
691 | if (!ds_file_info) { |
692 | goto error; | |
693 | } | |
694 | ||
41a65f30 | 695 | ds_file_group_insert_ds_file_info_sorted(ds_file_group, ds_file_info); |
94cf822e | 696 | |
94cf822e PP |
697 | ds_file_info = NULL; |
698 | goto end; | |
699 | ||
700 | error: | |
701 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
702 | ret = -1; | |
94cf822e PP |
703 | end: |
704 | return ret; | |
705 | } | |
706 | ||
707 | static | |
708 | int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, | |
e5be10ef | 709 | const char *path) |
94cf822e | 710 | { |
44c440bc PP |
711 | int64_t stream_instance_id = -1; |
712 | int64_t begin_ns = -1; | |
94cf822e PP |
713 | struct ctf_fs_ds_file_group *ds_file_group = NULL; |
714 | bool add_group = false; | |
715 | int ret; | |
716 | size_t i; | |
6de92955 | 717 | struct ctf_fs_ds_file *ds_file = NULL; |
97ade20b | 718 | struct ctf_fs_ds_index *index = NULL; |
d6e69534 | 719 | struct bt_msg_iter *msg_iter = NULL; |
44c440bc | 720 | struct ctf_stream_class *sc = NULL; |
d6e69534 | 721 | struct bt_msg_iter_packet_properties props; |
94cf822e | 722 | |
d6e69534 | 723 | msg_iter = bt_msg_iter_create(ctf_fs_trace->metadata->tc, |
86d8b7b8 | 724 | bt_common_get_page_size(BT_LOG_OUTPUT_LEVEL) * 8, |
ea14c7dd | 725 | ctf_fs_ds_file_medops, NULL, BT_LOG_OUTPUT_LEVEL); |
d6e69534 PP |
726 | if (!msg_iter) { |
727 | BT_LOGE_STR("Cannot create a CTF message iterator."); | |
6de92955 PP |
728 | goto error; |
729 | } | |
730 | ||
d6e69534 | 731 | ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, msg_iter, |
5c563278 | 732 | NULL, path); |
97ade20b JG |
733 | if (!ds_file) { |
734 | goto error; | |
735 | } | |
736 | ||
41693723 | 737 | ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); |
94cf822e | 738 | if (ret) { |
55314f2a | 739 | BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).", |
94cf822e PP |
740 | path); |
741 | goto error; | |
742 | } | |
743 | ||
44c440bc PP |
744 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
745 | props.stream_class_id); | |
746 | BT_ASSERT(sc); | |
44c440bc PP |
747 | stream_instance_id = props.data_stream_id; |
748 | ||
749 | if (props.snapshots.beginning_clock != UINT64_C(-1)) { | |
750 | BT_ASSERT(sc->default_clock_class); | |
0f2d58c9 PP |
751 | ret = bt_util_clock_cycles_to_ns_from_origin( |
752 | props.snapshots.beginning_clock, | |
753 | sc->default_clock_class->frequency, | |
754 | sc->default_clock_class->offset_seconds, | |
755 | sc->default_clock_class->offset_cycles, &begin_ns); | |
44c440bc PP |
756 | if (ret) { |
757 | BT_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).", | |
758 | path); | |
759 | goto error; | |
760 | } | |
94cf822e PP |
761 | } |
762 | ||
97ade20b JG |
763 | index = ctf_fs_ds_file_build_index(ds_file); |
764 | if (!index) { | |
765 | BT_LOGW("Failed to index CTF stream file \'%s\'", | |
766 | ds_file->file->path->str); | |
767 | } | |
768 | ||
44c440bc | 769 | if (begin_ns == -1) { |
94cf822e PP |
770 | /* |
771 | * No beggining timestamp to sort the stream files | |
772 | * within a stream file group, so consider that this | |
773 | * file must be the only one within its group. | |
774 | */ | |
44c440bc | 775 | stream_instance_id = -1; |
94cf822e PP |
776 | } |
777 | ||
44c440bc | 778 | if (stream_instance_id == -1) { |
94cf822e PP |
779 | /* |
780 | * No stream instance ID or no beginning timestamp: | |
781 | * create a unique stream file group for this stream | |
782 | * file because, even if there's a stream instance ID, | |
783 | * there's no timestamp to order the file within its | |
784 | * group. | |
785 | */ | |
94cf822e | 786 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, |
7ed5243a FD |
787 | sc, UINT64_C(-1), index); |
788 | /* Ownership of index is transferred. */ | |
789 | index = NULL; | |
790 | ||
94cf822e PP |
791 | if (!ds_file_group) { |
792 | goto error; | |
793 | } | |
794 | ||
795 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, | |
7ed5243a | 796 | path, begin_ns); |
94cf822e PP |
797 | if (ret) { |
798 | goto error; | |
799 | } | |
800 | ||
801 | add_group = true; | |
802 | goto end; | |
803 | } | |
804 | ||
44c440bc PP |
805 | BT_ASSERT(stream_instance_id != -1); |
806 | BT_ASSERT(begin_ns != -1); | |
94cf822e PP |
807 | |
808 | /* Find an existing stream file group with this ID */ | |
809 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
94cf822e PP |
810 | ds_file_group = g_ptr_array_index( |
811 | ctf_fs_trace->ds_file_groups, i); | |
94cf822e | 812 | |
60363f2f | 813 | if (ds_file_group->sc == sc && |
547eacf1 PP |
814 | ds_file_group->stream_id == |
815 | stream_instance_id) { | |
94cf822e PP |
816 | break; |
817 | } | |
818 | ||
819 | ds_file_group = NULL; | |
820 | } | |
821 | ||
822 | if (!ds_file_group) { | |
823 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, | |
7ed5243a FD |
824 | sc, stream_instance_id, index); |
825 | /* Ownership of index is transferred. */ | |
826 | index = NULL; | |
94cf822e PP |
827 | if (!ds_file_group) { |
828 | goto error; | |
829 | } | |
830 | ||
831 | add_group = true; | |
832 | } | |
833 | ||
547eacf1 | 834 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path, |
7ed5243a | 835 | begin_ns); |
94cf822e PP |
836 | if (ret) { |
837 | goto error; | |
838 | } | |
839 | ||
840 | goto end; | |
841 | ||
842 | error: | |
843 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
90e1eb09 | 844 | ds_file_group = NULL; |
94cf822e PP |
845 | ret = -1; |
846 | ||
847 | end: | |
848 | if (add_group && ds_file_group) { | |
849 | g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group); | |
850 | } | |
547eacf1 | 851 | |
97ade20b | 852 | ctf_fs_ds_file_destroy(ds_file); |
6de92955 | 853 | |
d6e69534 PP |
854 | if (msg_iter) { |
855 | bt_msg_iter_destroy(msg_iter); | |
6de92955 PP |
856 | } |
857 | ||
97ade20b | 858 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
859 | return ret; |
860 | } | |
861 | ||
862 | static | |
e5be10ef | 863 | int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace) |
e7a4393b JG |
864 | { |
865 | int ret = 0; | |
4f1f88a6 | 866 | const char *basename; |
e7a4393b | 867 | GError *error = NULL; |
4f1f88a6 | 868 | GDir *dir = NULL; |
e7a4393b | 869 | |
94cf822e | 870 | /* Check each file in the path directory, except specific ones */ |
1a9f7075 | 871 | dir = g_dir_open(ctf_fs_trace->path->str, 0, &error); |
e7a4393b | 872 | if (!dir) { |
55314f2a | 873 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 | 874 | ctf_fs_trace->path->str, error->message, |
4f1f88a6 | 875 | error->code); |
e7a4393b JG |
876 | goto error; |
877 | } | |
878 | ||
4f1f88a6 | 879 | while ((basename = g_dir_read_name(dir))) { |
94cf822e PP |
880 | struct ctf_fs_file *file; |
881 | ||
4f1f88a6 | 882 | if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) { |
e7a4393b | 883 | /* Ignore the metadata stream. */ |
3f7d4d90 | 884 | BT_LOGI("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 885 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
886 | continue; |
887 | } | |
888 | ||
4f1f88a6 | 889 | if (basename[0] == '.') { |
3f7d4d90 | 890 | BT_LOGI("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 891 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
892 | continue; |
893 | } | |
894 | ||
895 | /* Create the file. */ | |
55314f2a | 896 | file = ctf_fs_file_create(); |
e7a4393b | 897 | if (!file) { |
3743a302 | 898 | BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 899 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
900 | goto error; |
901 | } | |
902 | ||
903 | /* Create full path string. */ | |
3743a302 | 904 | g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s", |
1a9f7075 | 905 | ctf_fs_trace->path->str, basename); |
e7a4393b | 906 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
3f7d4d90 | 907 | BT_LOGI("Ignoring non-regular file `%s`", |
1a9f7075 | 908 | file->path->str); |
e7a4393b | 909 | ctf_fs_file_destroy(file); |
4f1f88a6 | 910 | file = NULL; |
e7a4393b JG |
911 | continue; |
912 | } | |
913 | ||
55314f2a | 914 | ret = ctf_fs_file_open(file, "rb"); |
4f1f88a6 | 915 | if (ret) { |
55314f2a | 916 | BT_LOGE("Cannot open stream file `%s`", file->path->str); |
e7a4393b JG |
917 | goto error; |
918 | } | |
919 | ||
9fa0891a JG |
920 | if (file->size == 0) { |
921 | /* Skip empty stream. */ | |
3f7d4d90 | 922 | BT_LOGI("Ignoring empty file `%s`", file->path->str); |
9fa0891a JG |
923 | ctf_fs_file_destroy(file); |
924 | continue; | |
925 | } | |
926 | ||
e5be10ef | 927 | ret = add_ds_file_to_ds_file_group(ctf_fs_trace, |
94cf822e | 928 | file->path->str); |
4f1f88a6 | 929 | if (ret) { |
89b08333 | 930 | BT_LOGE("Cannot add stream file `%s` to stream file group", |
1a9f7075 | 931 | file->path->str); |
94cf822e | 932 | ctf_fs_file_destroy(file); |
e7a4393b JG |
933 | goto error; |
934 | } | |
935 | ||
4f1f88a6 | 936 | ctf_fs_file_destroy(file); |
e7a4393b JG |
937 | } |
938 | ||
939 | goto end; | |
4f1f88a6 | 940 | |
e7a4393b JG |
941 | error: |
942 | ret = -1; | |
4f1f88a6 | 943 | |
e7a4393b JG |
944 | end: |
945 | if (dir) { | |
946 | g_dir_close(dir); | |
947 | dir = NULL; | |
948 | } | |
4f1f88a6 | 949 | |
e7a4393b JG |
950 | if (error) { |
951 | g_error_free(error); | |
952 | } | |
5b29e799 | 953 | |
91457551 | 954 | return ret; |
5b29e799 JG |
955 | } |
956 | ||
862ca4ed | 957 | static |
b19ff26f | 958 | int set_trace_name(bt_trace *trace, const char *name_suffix) |
862ca4ed PP |
959 | { |
960 | int ret = 0; | |
b19ff26f PP |
961 | const bt_trace_class *tc = bt_trace_borrow_class_const(trace); |
962 | const bt_value *val; | |
862ca4ed PP |
963 | GString *name; |
964 | ||
965 | name = g_string_new(NULL); | |
966 | if (!name) { | |
967 | BT_LOGE_STR("Failed to allocate a GString."); | |
968 | ret = -1; | |
969 | goto end; | |
970 | } | |
971 | ||
972 | /* | |
973 | * Check if we have a trace environment string value named `hostname`. | |
974 | * If so, use it as the trace name's prefix. | |
975 | */ | |
976 | val = bt_trace_class_borrow_environment_entry_value_by_name_const( | |
977 | tc, "hostname"); | |
978 | if (val && bt_value_is_string(val)) { | |
979 | g_string_append(name, bt_value_string_get(val)); | |
980 | ||
981 | if (name_suffix) { | |
982 | g_string_append_c(name, G_DIR_SEPARATOR); | |
983 | } | |
984 | } | |
985 | ||
986 | if (name_suffix) { | |
987 | g_string_append(name, name_suffix); | |
988 | } | |
989 | ||
990 | ret = bt_trace_set_name(trace, name->str); | |
991 | if (ret) { | |
992 | goto end; | |
993 | } | |
994 | ||
995 | goto end; | |
996 | ||
997 | end: | |
998 | if (name) { | |
999 | g_string_free(name, TRUE); | |
1000 | } | |
1001 | ||
1002 | return ret; | |
1003 | } | |
1004 | ||
f280892e | 1005 | static |
41693723 PP |
1006 | struct ctf_fs_trace *ctf_fs_trace_create(bt_self_component_source *self_comp, |
1007 | const char *path, const char *name, | |
e5be10ef | 1008 | struct ctf_fs_metadata_config *metadata_config) |
1a9f7075 PP |
1009 | { |
1010 | struct ctf_fs_trace *ctf_fs_trace; | |
1011 | int ret; | |
1012 | ||
1013 | ctf_fs_trace = g_new0(struct ctf_fs_trace, 1); | |
1014 | if (!ctf_fs_trace) { | |
1015 | goto end; | |
1016 | } | |
1017 | ||
1a9f7075 PP |
1018 | ctf_fs_trace->path = g_string_new(path); |
1019 | if (!ctf_fs_trace->path) { | |
1020 | goto error; | |
1021 | } | |
1022 | ||
1023 | ctf_fs_trace->name = g_string_new(name); | |
1024 | if (!ctf_fs_trace->name) { | |
1025 | goto error; | |
1026 | } | |
1027 | ||
1028 | ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1); | |
1029 | if (!ctf_fs_trace->metadata) { | |
1030 | goto error; | |
1031 | } | |
1032 | ||
44c440bc | 1033 | ctf_fs_metadata_init(ctf_fs_trace->metadata); |
94cf822e PP |
1034 | ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func( |
1035 | (GDestroyNotify) ctf_fs_ds_file_group_destroy); | |
1036 | if (!ctf_fs_trace->ds_file_groups) { | |
1037 | goto error; | |
1038 | } | |
1039 | ||
41693723 PP |
1040 | ret = ctf_fs_metadata_set_trace_class(self_comp, |
1041 | ctf_fs_trace, metadata_config); | |
862ca4ed PP |
1042 | if (ret) { |
1043 | goto error; | |
1044 | } | |
1045 | ||
41693723 PP |
1046 | if (ctf_fs_trace->metadata->trace_class) { |
1047 | ctf_fs_trace->trace = | |
1048 | bt_trace_create(ctf_fs_trace->metadata->trace_class); | |
1049 | if (!ctf_fs_trace->trace) { | |
1050 | goto error; | |
1051 | } | |
862ca4ed PP |
1052 | } |
1053 | ||
41693723 PP |
1054 | if (ctf_fs_trace->trace) { |
1055 | ret = set_trace_name(ctf_fs_trace->trace, name); | |
1056 | if (ret) { | |
1057 | goto error; | |
1058 | } | |
1a9f7075 PP |
1059 | } |
1060 | ||
e5be10ef | 1061 | ret = create_ds_file_groups(ctf_fs_trace); |
94cf822e PP |
1062 | if (ret) { |
1063 | goto error; | |
1064 | } | |
1065 | ||
1a9f7075 PP |
1066 | goto end; |
1067 | ||
1068 | error: | |
1069 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1070 | ctf_fs_trace = NULL; | |
44c440bc | 1071 | |
1a9f7075 PP |
1072 | end: |
1073 | return ctf_fs_trace; | |
1074 | } | |
1075 | ||
1076 | static | |
1077 | int path_is_ctf_trace(const char *path) | |
1078 | { | |
1079 | GString *metadata_path = g_string_new(NULL); | |
1080 | int ret = 0; | |
1081 | ||
1082 | if (!metadata_path) { | |
1083 | ret = -1; | |
1084 | goto end; | |
1085 | } | |
1086 | ||
3743a302 | 1087 | g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME); |
1a9f7075 PP |
1088 | |
1089 | if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) { | |
1090 | ret = 1; | |
1091 | goto end; | |
1092 | } | |
1093 | ||
1094 | end: | |
1095 | g_string_free(metadata_path, TRUE); | |
1096 | return ret; | |
1097 | } | |
1098 | ||
1099 | static | |
55314f2a | 1100 | int add_trace_path(GList **trace_paths, const char *path) |
1a9f7075 | 1101 | { |
4bd72b60 | 1102 | GString *norm_path = NULL; |
1a9f7075 | 1103 | int ret = 0; |
1a9f7075 | 1104 | |
4bd72b60 PP |
1105 | norm_path = bt_common_normalize_path(path, NULL); |
1106 | if (!norm_path) { | |
55314f2a | 1107 | BT_LOGE("Failed to normalize path `%s`.", path); |
1a9f7075 PP |
1108 | ret = -1; |
1109 | goto end; | |
1110 | } | |
1111 | ||
3743a302 | 1112 | // FIXME: Remove or ifdef for __MINGW32__ |
4bd72b60 | 1113 | if (strcmp(norm_path->str, "/") == 0) { |
55314f2a | 1114 | BT_LOGE("Opening a trace in `/` is not supported."); |
1a9f7075 PP |
1115 | ret = -1; |
1116 | goto end; | |
1117 | } | |
1118 | ||
4bd72b60 | 1119 | *trace_paths = g_list_prepend(*trace_paths, norm_path); |
f6ccaed9 | 1120 | BT_ASSERT(*trace_paths); |
4bd72b60 | 1121 | norm_path = NULL; |
1a9f7075 PP |
1122 | |
1123 | end: | |
4bd72b60 PP |
1124 | if (norm_path) { |
1125 | g_string_free(norm_path, TRUE); | |
1126 | } | |
1127 | ||
1a9f7075 PP |
1128 | return ret; |
1129 | } | |
1130 | ||
f280892e | 1131 | static |
55314f2a | 1132 | int ctf_fs_find_traces(GList **trace_paths, const char *start_path) |
1a9f7075 PP |
1133 | { |
1134 | int ret; | |
1135 | GError *error = NULL; | |
1136 | GDir *dir = NULL; | |
1137 | const char *basename = NULL; | |
1138 | ||
1139 | /* Check if the starting path is a CTF trace itself */ | |
1140 | ret = path_is_ctf_trace(start_path); | |
1141 | if (ret < 0) { | |
1142 | goto end; | |
1143 | } | |
1144 | ||
1145 | if (ret) { | |
1146 | /* | |
4bd72b60 PP |
1147 | * Stop recursion: a CTF trace cannot contain another |
1148 | * CTF trace. | |
1a9f7075 | 1149 | */ |
55314f2a | 1150 | ret = add_trace_path(trace_paths, start_path); |
1a9f7075 PP |
1151 | goto end; |
1152 | } | |
1153 | ||
1154 | /* Look for subdirectories */ | |
1155 | if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) { | |
1156 | /* Starting path is not a directory: end of recursion */ | |
1157 | goto end; | |
1158 | } | |
1159 | ||
1160 | dir = g_dir_open(start_path, 0, &error); | |
1161 | if (!dir) { | |
1162 | if (error->code == G_FILE_ERROR_ACCES) { | |
3f7d4d90 | 1163 | BT_LOGI("Cannot open directory `%s`: %s (code %d): continuing", |
1a9f7075 PP |
1164 | start_path, error->message, error->code); |
1165 | goto end; | |
1166 | } | |
1167 | ||
55314f2a | 1168 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 PP |
1169 | start_path, error->message, error->code); |
1170 | ret = -1; | |
1171 | goto end; | |
1172 | } | |
1173 | ||
1174 | while ((basename = g_dir_read_name(dir))) { | |
1175 | GString *sub_path = g_string_new(NULL); | |
1176 | ||
1177 | if (!sub_path) { | |
1178 | ret = -1; | |
1179 | goto end; | |
1180 | } | |
1181 | ||
3743a302 | 1182 | g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename); |
55314f2a | 1183 | ret = ctf_fs_find_traces(trace_paths, sub_path->str); |
1a9f7075 PP |
1184 | g_string_free(sub_path, TRUE); |
1185 | if (ret) { | |
1186 | goto end; | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | end: | |
1191 | if (dir) { | |
1192 | g_dir_close(dir); | |
1193 | } | |
1194 | ||
1195 | if (error) { | |
1196 | g_error_free(error); | |
1197 | } | |
1198 | ||
1199 | return ret; | |
1200 | } | |
1201 | ||
f280892e | 1202 | static |
55314f2a | 1203 | GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) { |
1a9f7075 | 1204 | GList *trace_names = NULL; |
1a9f7075 | 1205 | GList *node; |
4bd72b60 PP |
1206 | const char *last_sep; |
1207 | size_t base_dist; | |
1a9f7075 PP |
1208 | |
1209 | /* | |
4bd72b60 PP |
1210 | * At this point we know that all the trace paths are |
1211 | * normalized, and so is the base path. This means that | |
1212 | * they are absolute and they don't end with a separator. | |
1213 | * We can simply find the location of the last separator | |
1214 | * in the base path, which gives us the name of the actual | |
1215 | * directory to look into, and use this location as the | |
1216 | * start of each trace name within each trace path. | |
1217 | * | |
1218 | * For example: | |
1219 | * | |
1220 | * Base path: /home/user/my-traces/some-trace | |
1221 | * Trace paths: | |
1222 | * - /home/user/my-traces/some-trace/host1/trace1 | |
1223 | * - /home/user/my-traces/some-trace/host1/trace2 | |
1224 | * - /home/user/my-traces/some-trace/host2/trace | |
1225 | * - /home/user/my-traces/some-trace/other-trace | |
1226 | * | |
1227 | * In this case the trace names are: | |
1228 | * | |
1229 | * - some-trace/host1/trace1 | |
1230 | * - some-trace/host1/trace2 | |
1231 | * - some-trace/host2/trace | |
1232 | * - some-trace/other-trace | |
1a9f7075 | 1233 | */ |
4bd72b60 | 1234 | last_sep = strrchr(base_path, G_DIR_SEPARATOR); |
1a9f7075 | 1235 | |
4bd72b60 | 1236 | /* We know there's at least one separator */ |
f6ccaed9 | 1237 | BT_ASSERT(last_sep); |
1a9f7075 | 1238 | |
4bd72b60 PP |
1239 | /* Distance to base */ |
1240 | base_dist = last_sep - base_path + 1; | |
1a9f7075 PP |
1241 | |
1242 | /* Create the trace names */ | |
1243 | for (node = trace_paths; node; node = g_list_next(node)) { | |
1244 | GString *trace_name = g_string_new(NULL); | |
1245 | GString *trace_path = node->data; | |
1246 | ||
f6ccaed9 | 1247 | BT_ASSERT(trace_name); |
4bd72b60 | 1248 | g_string_assign(trace_name, &trace_path->str[base_dist]); |
1a9f7075 PP |
1249 | trace_names = g_list_append(trace_names, trace_name); |
1250 | } | |
1251 | ||
1252 | return trace_names; | |
1253 | } | |
1254 | ||
f280892e SM |
1255 | /* Helper for ctf_fs_component_create_ctf_fs_traces, to handle a single path/root. */ |
1256 | ||
1a9f7075 | 1257 | static |
f280892e | 1258 | int ctf_fs_component_create_ctf_fs_traces_one_root(bt_self_component_source *self_comp, |
41693723 | 1259 | struct ctf_fs_component *ctf_fs, |
1a9f7075 PP |
1260 | const char *path_param) |
1261 | { | |
1262 | struct ctf_fs_trace *ctf_fs_trace = NULL; | |
1263 | int ret = 0; | |
4bd72b60 | 1264 | GString *norm_path = NULL; |
1a9f7075 PP |
1265 | GList *trace_paths = NULL; |
1266 | GList *trace_names = NULL; | |
1267 | GList *tp_node; | |
1268 | GList *tn_node; | |
1269 | ||
4bd72b60 PP |
1270 | norm_path = bt_common_normalize_path(path_param, NULL); |
1271 | if (!norm_path) { | |
55314f2a | 1272 | BT_LOGE("Failed to normalize path: `%s`.", |
4bd72b60 PP |
1273 | path_param); |
1274 | goto error; | |
1275 | } | |
1276 | ||
55314f2a | 1277 | ret = ctf_fs_find_traces(&trace_paths, norm_path->str); |
1a9f7075 PP |
1278 | if (ret) { |
1279 | goto error; | |
1280 | } | |
1281 | ||
1282 | if (!trace_paths) { | |
55314f2a | 1283 | BT_LOGE("No CTF traces recursively found in `%s`.", |
1a9f7075 PP |
1284 | path_param); |
1285 | goto error; | |
1286 | } | |
1287 | ||
55314f2a | 1288 | trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str); |
1a9f7075 | 1289 | if (!trace_names) { |
55314f2a | 1290 | BT_LOGE("Cannot create trace names from trace paths."); |
1a9f7075 PP |
1291 | goto error; |
1292 | } | |
1293 | ||
1294 | for (tp_node = trace_paths, tn_node = trace_names; tp_node; | |
1295 | tp_node = g_list_next(tp_node), | |
1296 | tn_node = g_list_next(tn_node)) { | |
1297 | GString *trace_path = tp_node->data; | |
1298 | GString *trace_name = tn_node->data; | |
1299 | ||
41693723 PP |
1300 | ctf_fs_trace = ctf_fs_trace_create(self_comp, |
1301 | trace_path->str, trace_name->str, | |
1302 | &ctf_fs->metadata_config); | |
1a9f7075 | 1303 | if (!ctf_fs_trace) { |
55314f2a | 1304 | BT_LOGE("Cannot create trace for `%s`.", |
1a9f7075 PP |
1305 | trace_path->str); |
1306 | goto error; | |
1307 | } | |
52c5fe74 PP |
1308 | |
1309 | g_ptr_array_add(ctf_fs->traces, ctf_fs_trace); | |
1310 | ctf_fs_trace = NULL; | |
1a9f7075 PP |
1311 | } |
1312 | ||
1a9f7075 PP |
1313 | goto end; |
1314 | ||
1315 | error: | |
1316 | ret = -1; | |
1317 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1318 | ||
1319 | end: | |
1320 | for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) { | |
1321 | if (tp_node->data) { | |
1322 | g_string_free(tp_node->data, TRUE); | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) { | |
1327 | if (tn_node->data) { | |
1328 | g_string_free(tn_node->data, TRUE); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | if (trace_paths) { | |
1333 | g_list_free(trace_paths); | |
1334 | } | |
1335 | ||
1336 | if (trace_names) { | |
1337 | g_list_free(trace_names); | |
1338 | } | |
1339 | ||
4bd72b60 PP |
1340 | if (norm_path) { |
1341 | g_string_free(norm_path, TRUE); | |
1342 | } | |
1343 | ||
1a9f7075 PP |
1344 | return ret; |
1345 | } | |
1346 | ||
41a65f30 SM |
1347 | /* GCompareFunc to sort traces by UUID. */ |
1348 | ||
1349 | static | |
1350 | gint sort_traces_by_uuid(gconstpointer a, gconstpointer b) | |
1351 | { | |
1352 | const struct ctf_fs_trace *trace_a = *((const struct ctf_fs_trace **) a); | |
1353 | const struct ctf_fs_trace *trace_b = *((const struct ctf_fs_trace **) b); | |
1354 | ||
1355 | bool trace_a_has_uuid = trace_a->metadata->tc->is_uuid_set; | |
1356 | bool trace_b_has_uuid = trace_b->metadata->tc->is_uuid_set; | |
1357 | gint ret; | |
1358 | ||
1359 | /* Order traces without uuid first. */ | |
1360 | if (!trace_a_has_uuid && trace_b_has_uuid) { | |
1361 | ret = -1; | |
1362 | } else if (trace_a_has_uuid && !trace_b_has_uuid) { | |
1363 | ret = 1; | |
1364 | } else if (!trace_a_has_uuid && !trace_b_has_uuid) { | |
1365 | ret = 0; | |
1366 | } else { | |
1367 | ret = bt_uuid_compare(trace_a->metadata->tc->uuid, trace_b->metadata->tc->uuid); | |
1368 | } | |
1369 | ||
1370 | return ret; | |
1371 | } | |
1372 | ||
1373 | /* | |
1374 | * Count the number of stream and event classes defined by this trace's metadata. | |
1375 | * | |
1376 | * This is used to determine which metadata is the "latest", out of multiple | |
1377 | * traces sharing the same UUID. It is assumed that amongst all these metadatas, | |
1378 | * a bigger metadata is a superset of a smaller metadata. Therefore, it is | |
1379 | * enough to just count the classes. | |
1380 | */ | |
1381 | ||
1382 | static | |
1383 | unsigned int metadata_count_stream_and_event_classes(struct ctf_fs_trace *trace) | |
1384 | { | |
1385 | unsigned int num = trace->metadata->tc->stream_classes->len; | |
1386 | guint i; | |
1387 | ||
1388 | for (i = 0; i < trace->metadata->tc->stream_classes->len; i++) { | |
1389 | struct ctf_stream_class *sc = trace->metadata->tc->stream_classes->pdata[i]; | |
1390 | num += sc->event_classes->len; | |
1391 | } | |
1392 | ||
1393 | return num; | |
1394 | } | |
1395 | ||
1396 | /* | |
1397 | * Merge the src ds_file_group into dest. This consists of merging their | |
1398 | * ds_file_infos, making sure to keep the result sorted. | |
1399 | */ | |
1400 | ||
1401 | static | |
1402 | void merge_ctf_fs_ds_file_groups(struct ctf_fs_ds_file_group *dest, struct ctf_fs_ds_file_group *src) | |
1403 | { | |
1404 | guint i; | |
1405 | ||
1406 | for (i = 0; i < src->ds_file_infos->len; i++) { | |
1407 | struct ctf_fs_ds_file_info *ds_file_info = | |
1408 | g_ptr_array_index(src->ds_file_infos, i); | |
1409 | ||
1410 | /* Ownership of the ds_file_info is transferred to dest. */ | |
1411 | g_ptr_array_index(src->ds_file_infos, i) = NULL; | |
1412 | ||
1413 | ds_file_group_insert_ds_file_info_sorted(dest, ds_file_info); | |
1414 | } | |
41a65f30 | 1415 | |
7ed5243a FD |
1416 | /* Merge both indexes. */ |
1417 | for (i = 0; i < src->index->entries->len; i++) { | |
1418 | struct ctf_fs_ds_index_entry *entry = g_ptr_array_index( | |
1419 | src->index->entries, i); | |
1420 | ||
1421 | /* | |
1422 | * Ownership of the ctf_fs_ds_index_entry is transferred to | |
1423 | * dest. | |
1424 | */ | |
1425 | g_ptr_array_index(src->index->entries, i) = NULL; | |
1426 | ||
1427 | ds_file_group_insert_ds_index_entry_sorted(dest, entry); | |
1428 | } | |
1429 | } | |
41a65f30 SM |
1430 | /* Merge src_trace's data stream file groups into dest_trace's. */ |
1431 | ||
1432 | static | |
54ef52bd | 1433 | int merge_matching_ctf_fs_ds_file_groups( |
41a65f30 SM |
1434 | struct ctf_fs_trace *dest_trace, |
1435 | struct ctf_fs_trace *src_trace) | |
1436 | { | |
1437 | ||
1438 | GPtrArray *dest = dest_trace->ds_file_groups; | |
1439 | GPtrArray *src = src_trace->ds_file_groups; | |
1440 | guint s_i; | |
54ef52bd | 1441 | int ret = 0; |
41a65f30 SM |
1442 | |
1443 | /* | |
1444 | * Save the initial length of dest: we only want to check against the | |
1445 | * original elements in the inner loop. | |
1446 | */ | |
1447 | const guint dest_len = dest->len; | |
1448 | ||
1449 | for (s_i = 0; s_i < src->len; s_i++) { | |
1450 | struct ctf_fs_ds_file_group *src_group = g_ptr_array_index(src, s_i); | |
1451 | struct ctf_fs_ds_file_group *dest_group = NULL; | |
1452 | ||
1453 | /* A stream instance without ID can't match a stream in the other trace. */ | |
1454 | if (src_group->stream_id != -1) { | |
1455 | guint d_i; | |
1456 | ||
1457 | /* Let's search for a matching ds_file_group in the destination. */ | |
1458 | for (d_i = 0; d_i < dest_len; d_i++) { | |
1459 | struct ctf_fs_ds_file_group *candidate_dest = g_ptr_array_index(dest, d_i); | |
1460 | ||
1461 | /* Can't match a stream instance without ID. */ | |
1462 | if (candidate_dest->stream_id == -1) { | |
1463 | continue; | |
1464 | } | |
1465 | ||
1466 | /* | |
1467 | * If the two groups have the same stream instance id | |
1468 | * and belong to the same stream class (stream instance | |
1469 | * ids are per-stream class), they represent the same | |
1470 | * stream instance. | |
1471 | */ | |
1472 | if (candidate_dest->stream_id != src_group->stream_id || | |
1473 | candidate_dest->sc->id != src_group->sc->id) { | |
1474 | continue; | |
1475 | } | |
1476 | ||
1477 | dest_group = candidate_dest; | |
1478 | break; | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | /* | |
1483 | * Didn't find a friend in dest to merge our src_group into? | |
7ed5243a FD |
1484 | * Create a new empty one. This can happen if a stream was |
1485 | * active in the source trace chunk but not in the destination | |
1486 | * trace chunk. | |
41a65f30 SM |
1487 | */ |
1488 | if (!dest_group) { | |
1489 | struct ctf_stream_class *sc; | |
7ed5243a | 1490 | struct ctf_fs_ds_index *index; |
41a65f30 SM |
1491 | |
1492 | sc = ctf_trace_class_borrow_stream_class_by_id( | |
1493 | dest_trace->metadata->tc, src_group->sc->id); | |
1494 | BT_ASSERT(sc); | |
1495 | ||
7ed5243a FD |
1496 | index = ctf_fs_ds_index_create(); |
1497 | if (!index) { | |
1498 | ret = -1; | |
1499 | goto end; | |
1500 | } | |
1501 | ||
41a65f30 | 1502 | dest_group = ctf_fs_ds_file_group_create(dest_trace, sc, |
7ed5243a FD |
1503 | src_group->stream_id, index); |
1504 | /* Ownership of index is transferred. */ | |
1505 | index = NULL; | |
54ef52bd FD |
1506 | if (!dest_group) { |
1507 | ret = -1; | |
1508 | goto end; | |
1509 | } | |
41a65f30 SM |
1510 | |
1511 | g_ptr_array_add(dest_trace->ds_file_groups, dest_group); | |
1512 | } | |
1513 | ||
1514 | BT_ASSERT(dest_group); | |
1515 | merge_ctf_fs_ds_file_groups(dest_group, src_group); | |
1516 | } | |
54ef52bd FD |
1517 | |
1518 | end: | |
1519 | return ret; | |
41a65f30 SM |
1520 | } |
1521 | ||
1522 | /* | |
1523 | * Collapse the given traces, which must all share the same UUID, in a single | |
1524 | * one. | |
1525 | * | |
1526 | * The trace with the most expansive metadata is chosen and all other traces | |
1527 | * are merged into that one. The array slots of all the traces that get merged | |
1528 | * in the chosen one are set to NULL, so only the slot of the chosen trace | |
1529 | * remains non-NULL. | |
1530 | */ | |
1531 | ||
1532 | static | |
54ef52bd | 1533 | int merge_ctf_fs_traces(struct ctf_fs_trace **traces, unsigned int num_traces) |
41a65f30 SM |
1534 | { |
1535 | unsigned int winner_count; | |
1536 | struct ctf_fs_trace *winner; | |
1537 | guint i; | |
54ef52bd | 1538 | int ret = 0; |
41a65f30 SM |
1539 | char uuid_str[BABELTRACE_UUID_STR_LEN]; |
1540 | ||
1541 | BT_ASSERT(num_traces >= 2); | |
1542 | ||
1543 | winner_count = metadata_count_stream_and_event_classes(traces[0]); | |
1544 | winner = traces[0]; | |
1545 | ||
1546 | /* Find the trace with the largest metadata. */ | |
1547 | for (i = 1; i < num_traces; i++) { | |
1548 | struct ctf_fs_trace *candidate; | |
1549 | unsigned int candidate_count; | |
1550 | ||
1551 | candidate = traces[i]; | |
1552 | ||
1553 | /* A bit of sanity check. */ | |
1554 | BT_ASSERT(bt_uuid_compare(winner->metadata->tc->uuid, candidate->metadata->tc->uuid) == 0); | |
1555 | ||
1556 | candidate_count = metadata_count_stream_and_event_classes(candidate); | |
1557 | ||
1558 | if (candidate_count > winner_count) { | |
1559 | winner_count = candidate_count; | |
1560 | winner = candidate; | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | /* Merge all the other traces in the winning trace. */ | |
1565 | for (i = 0; i < num_traces; i++) { | |
1566 | struct ctf_fs_trace *trace = traces[i]; | |
1567 | ||
1568 | /* Don't merge the winner into itself. */ | |
1569 | if (trace == winner) { | |
1570 | continue; | |
1571 | } | |
1572 | ||
1573 | /* Merge trace's data stream file groups into winner's. */ | |
54ef52bd FD |
1574 | ret = merge_matching_ctf_fs_ds_file_groups(winner, trace); |
1575 | if (ret) { | |
1576 | goto end; | |
1577 | } | |
41a65f30 SM |
1578 | |
1579 | /* Free the trace that got merged into winner, clear the slot in the array. */ | |
1580 | ctf_fs_trace_destroy(trace); | |
1581 | traces[i] = NULL; | |
1582 | } | |
1583 | ||
1584 | /* Use the string representation of the UUID as the trace name. */ | |
1585 | bt_uuid_unparse(winner->metadata->tc->uuid, uuid_str); | |
1586 | g_string_printf(winner->name, "%s", uuid_str); | |
54ef52bd FD |
1587 | |
1588 | end: | |
1589 | return ret; | |
41a65f30 SM |
1590 | } |
1591 | ||
1592 | /* | |
1593 | * Merge all traces of `ctf_fs` that share the same UUID in a single trace. | |
1594 | * Traces with no UUID are not merged. | |
1595 | */ | |
1596 | ||
1597 | static | |
54ef52bd | 1598 | int merge_traces_with_same_uuid(struct ctf_fs_component *ctf_fs) |
41a65f30 SM |
1599 | { |
1600 | GPtrArray *traces = ctf_fs->traces; | |
1601 | guint range_start_idx = 0; | |
1602 | unsigned int num_traces = 0; | |
1603 | guint i; | |
54ef52bd | 1604 | int ret = 0; |
41a65f30 SM |
1605 | |
1606 | /* Sort the traces by uuid, then collapse traces with the same uuid in a single one. */ | |
1607 | g_ptr_array_sort(traces, sort_traces_by_uuid); | |
1608 | ||
1609 | /* Find ranges of consecutive traces that share the same UUID. */ | |
1610 | while (range_start_idx < traces->len) { | |
1611 | guint range_len; | |
1612 | struct ctf_fs_trace *range_start_trace = g_ptr_array_index(traces, range_start_idx); | |
1613 | ||
1614 | /* Exclusive end of range. */ | |
1615 | guint range_end_exc_idx = range_start_idx + 1; | |
1616 | ||
1617 | while (range_end_exc_idx < traces->len) { | |
1618 | struct ctf_fs_trace *this_trace = g_ptr_array_index(traces, range_end_exc_idx); | |
1619 | ||
1620 | if (!range_start_trace->metadata->tc->is_uuid_set || | |
1621 | (bt_uuid_compare(range_start_trace->metadata->tc->uuid, this_trace->metadata->tc->uuid) != 0)) { | |
1622 | break; | |
1623 | } | |
1624 | ||
1625 | range_end_exc_idx++; | |
1626 | } | |
1627 | ||
1628 | /* If we have two or more traces with matching UUIDs, merge them. */ | |
1629 | range_len = range_end_exc_idx - range_start_idx; | |
1630 | if (range_len > 1) { | |
1631 | struct ctf_fs_trace **range_start = (struct ctf_fs_trace **) &traces->pdata[range_start_idx]; | |
54ef52bd FD |
1632 | ret = merge_ctf_fs_traces(range_start, range_len); |
1633 | if (ret) { | |
1634 | goto end; | |
1635 | } | |
41a65f30 SM |
1636 | } |
1637 | ||
1638 | num_traces++; | |
1639 | range_start_idx = range_end_exc_idx; | |
1640 | } | |
1641 | ||
1642 | /* Clear any NULL slot (traces that got merged in another one) in the array. */ | |
1643 | for (i = 0; i < traces->len;) { | |
1644 | if (g_ptr_array_index(traces, i) == NULL) { | |
1645 | g_ptr_array_remove_index_fast(traces, i); | |
1646 | } else { | |
1647 | i++; | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | BT_ASSERT(num_traces == traces->len); | |
54ef52bd FD |
1652 | |
1653 | end: | |
1654 | return ret; | |
41a65f30 SM |
1655 | } |
1656 | ||
f280892e SM |
1657 | int ctf_fs_component_create_ctf_fs_traces(bt_self_component_source *self_comp, |
1658 | struct ctf_fs_component *ctf_fs, | |
1659 | const bt_value *paths_value) | |
1660 | { | |
1661 | int ret = 0; | |
1662 | uint64_t i; | |
1663 | ||
1664 | for (i = 0; i < bt_value_array_get_size(paths_value); i++) { | |
1665 | const bt_value *path_value = bt_value_array_borrow_element_by_index_const(paths_value, i); | |
1666 | const char *path = bt_value_string_get(path_value); | |
1667 | ||
1668 | ret = ctf_fs_component_create_ctf_fs_traces_one_root(self_comp, ctf_fs, path); | |
1669 | if (ret) { | |
1670 | goto end; | |
1671 | } | |
1672 | } | |
1673 | ||
54ef52bd | 1674 | ret = merge_traces_with_same_uuid(ctf_fs); |
41a65f30 | 1675 | |
f280892e SM |
1676 | end: |
1677 | return ret; | |
1678 | } | |
1679 | ||
a38d7650 SM |
1680 | static |
1681 | GString *get_stream_instance_unique_name( | |
1682 | struct ctf_fs_ds_file_group *ds_file_group) | |
1683 | { | |
1684 | GString *name; | |
1685 | struct ctf_fs_ds_file_info *ds_file_info; | |
1686 | ||
1687 | name = g_string_new(NULL); | |
1688 | if (!name) { | |
1689 | goto end; | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * If there's more than one stream file in the stream file | |
1694 | * group, the first (earliest) stream file's path is used as | |
1695 | * the stream's unique name. | |
1696 | */ | |
1697 | BT_ASSERT(ds_file_group->ds_file_infos->len > 0); | |
1698 | ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0); | |
1699 | g_string_assign(name, ds_file_info->path->str); | |
1700 | ||
1701 | end: | |
1702 | return name; | |
1703 | } | |
1704 | ||
f280892e SM |
1705 | /* Create the IR stream objects for ctf_fs_trace. */ |
1706 | ||
1707 | static | |
1708 | int create_streams_for_trace(struct ctf_fs_trace *ctf_fs_trace) | |
1709 | { | |
1710 | int ret; | |
1711 | GString *name = NULL; | |
1712 | guint i; | |
1713 | ||
1714 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
1715 | struct ctf_fs_ds_file_group *ds_file_group = | |
1716 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
1717 | name = get_stream_instance_unique_name(ds_file_group); | |
1718 | ||
1719 | if (!name) { | |
1720 | goto error; | |
1721 | } | |
1722 | ||
1723 | if (ds_file_group->sc->ir_sc) { | |
1724 | BT_ASSERT(ctf_fs_trace->trace); | |
1725 | ||
1726 | if (ds_file_group->stream_id == UINT64_C(-1)) { | |
1727 | /* No stream ID: use 0 */ | |
1728 | ds_file_group->stream = bt_stream_create_with_id( | |
1729 | ds_file_group->sc->ir_sc, | |
1730 | ctf_fs_trace->trace, | |
1731 | ctf_fs_trace->next_stream_id); | |
1732 | ctf_fs_trace->next_stream_id++; | |
1733 | } else { | |
1734 | /* Specific stream ID */ | |
1735 | ds_file_group->stream = bt_stream_create_with_id( | |
1736 | ds_file_group->sc->ir_sc, | |
1737 | ctf_fs_trace->trace, | |
1738 | (uint64_t) ds_file_group->stream_id); | |
1739 | } | |
1740 | } else { | |
1741 | ds_file_group->stream = NULL; | |
1742 | } | |
1743 | ||
1744 | if (!ds_file_group->stream) { | |
1745 | BT_LOGE("Cannot create stream for DS file group: " | |
1746 | "addr=%p, stream-name=\"%s\"", | |
1747 | ds_file_group, name->str); | |
1748 | goto error; | |
1749 | } | |
1750 | ||
1751 | ret = bt_stream_set_name(ds_file_group->stream, | |
1752 | name->str); | |
1753 | if (ret) { | |
1754 | BT_LOGE("Cannot set stream's name: " | |
1755 | "addr=%p, stream-name=\"%s\"", | |
1756 | ds_file_group->stream, name->str); | |
1757 | goto error; | |
1758 | } | |
1759 | ||
1760 | g_string_free(name, TRUE); | |
1761 | name = NULL; | |
1762 | } | |
1763 | ||
1764 | ret = 0; | |
1765 | goto end; | |
1766 | ||
1767 | error: | |
1768 | ret = -1; | |
1769 | ||
1770 | end: | |
1771 | ||
1772 | if (name) { | |
1773 | g_string_free(name, TRUE); | |
1774 | } | |
1775 | return ret; | |
1776 | } | |
1777 | ||
d907165c SM |
1778 | /* |
1779 | * Validate the "paths" parameter passed to this component. It must be | |
1780 | * present, and it must be an array of strings. | |
1781 | */ | |
1782 | ||
1783 | static | |
f280892e SM |
1784 | bool validate_paths_parameter(const bt_value *paths) |
1785 | { | |
1786 | bool ret; | |
1787 | bt_value_type type; | |
1788 | uint64_t i; | |
1789 | ||
1790 | if (!paths) { | |
1791 | BT_LOGE("missing \"paths\" parameter"); | |
1792 | goto error; | |
1793 | } | |
1794 | ||
1795 | type = bt_value_get_type(paths); | |
1796 | if (type != BT_VALUE_TYPE_ARRAY) { | |
1797 | BT_LOGE("`paths` parameter: expecting array value: type=%s", | |
1798 | bt_common_value_type_string(type)); | |
1799 | goto error; | |
1800 | } | |
1801 | ||
1802 | for (i = 0; i < bt_value_array_get_size(paths); i++) { | |
1803 | const bt_value *elem; | |
1804 | ||
1805 | elem = bt_value_array_borrow_element_by_index_const(paths, i); | |
1806 | type = bt_value_get_type(elem); | |
1807 | if (type != BT_VALUE_TYPE_STRING) { | |
1808 | BT_LOGE("`paths` parameter: expecting string value: index=%" PRIu64 ", type=%s", | |
1809 | i, bt_common_value_type_string(type)); | |
1810 | goto error; | |
1811 | } | |
1812 | } | |
1813 | ||
1814 | ret = true; | |
1815 | goto end; | |
1816 | ||
1817 | error: | |
1818 | ret = false; | |
1819 | ||
1820 | end: | |
1821 | return ret; | |
1822 | } | |
1823 | ||
d907165c SM |
1824 | bool read_src_fs_parameters(const bt_value *params, |
1825 | const bt_value **paths, struct ctf_fs_component *ctf_fs) { | |
1826 | bool ret; | |
1827 | const bt_value *value; | |
1828 | ||
1829 | /* paths parameter */ | |
1830 | *paths = bt_value_map_borrow_entry_value_const(params, "paths"); | |
1831 | if (!validate_paths_parameter(*paths)) { | |
1832 | goto error; | |
1833 | } | |
1834 | ||
1835 | /* clock-class-offset-s parameter */ | |
1836 | value = bt_value_map_borrow_entry_value_const(params, | |
1837 | "clock-class-offset-s"); | |
1838 | if (value) { | |
fdd3a2da | 1839 | if (!bt_value_is_signed_integer(value)) { |
d907165c SM |
1840 | BT_LOGE("clock-class-offset-s must be an integer"); |
1841 | goto error; | |
1842 | } | |
1843 | ctf_fs->metadata_config.clock_class_offset_s = | |
fdd3a2da | 1844 | bt_value_signed_integer_get(value); |
d907165c SM |
1845 | } |
1846 | ||
1847 | /* clock-class-offset-ns parameter */ | |
1848 | value = bt_value_map_borrow_entry_value_const(params, | |
1849 | "clock-class-offset-ns"); | |
1850 | if (value) { | |
fdd3a2da | 1851 | if (!bt_value_is_signed_integer(value)) { |
d907165c SM |
1852 | BT_LOGE("clock-class-offset-ns must be an integer"); |
1853 | goto error; | |
1854 | } | |
1855 | ctf_fs->metadata_config.clock_class_offset_ns = | |
fdd3a2da | 1856 | bt_value_signed_integer_get(value); |
d907165c SM |
1857 | } |
1858 | ||
1859 | ||
1860 | ret = true; | |
1861 | goto end; | |
1862 | ||
1863 | error: | |
1864 | ret = false; | |
1865 | ||
1866 | end: | |
1867 | return ret; | |
1868 | } | |
1869 | ||
5b29e799 | 1870 | static |
d94d92ac | 1871 | struct ctf_fs_component *ctf_fs_create( |
b19ff26f PP |
1872 | bt_self_component_source *self_comp, |
1873 | const bt_value *params) | |
56a1cced | 1874 | { |
f280892e | 1875 | struct ctf_fs_component *ctf_fs = NULL; |
f280892e SM |
1876 | guint i; |
1877 | const bt_value *paths_value; | |
56a1cced | 1878 | |
d907165c SM |
1879 | ctf_fs = ctf_fs_component_create(); |
1880 | if (!ctf_fs) { | |
f280892e SM |
1881 | goto error; |
1882 | } | |
1883 | ||
d907165c | 1884 | if (!read_src_fs_parameters(params, &paths_value, ctf_fs)) { |
f280892e | 1885 | goto error; |
56a1cced JG |
1886 | } |
1887 | ||
d94d92ac | 1888 | bt_self_component_set_data( |
707b7d35 | 1889 | bt_self_component_source_as_self_component(self_comp), |
d94d92ac | 1890 | ctf_fs); |
1a9f7075 | 1891 | |
4f1f88a6 PP |
1892 | /* |
1893 | * We don't need to get a new reference here because as long as | |
1894 | * our private ctf_fs_component object exists, the containing | |
1895 | * private component should also exist. | |
1896 | */ | |
d94d92ac | 1897 | ctf_fs->self_comp = self_comp; |
56a1cced | 1898 | |
f280892e | 1899 | if (ctf_fs_component_create_ctf_fs_traces(self_comp, ctf_fs, paths_value)) { |
4f1f88a6 PP |
1900 | goto error; |
1901 | } | |
1902 | ||
f280892e SM |
1903 | for (i = 0; i < ctf_fs->traces->len; i++) { |
1904 | struct ctf_fs_trace *trace = g_ptr_array_index(ctf_fs->traces, i); | |
599faa1c | 1905 | |
f280892e SM |
1906 | if (create_streams_for_trace(trace)) { |
1907 | goto error; | |
1908 | } | |
1909 | ||
1910 | if (create_ports_for_trace(ctf_fs, trace)) { | |
1911 | goto error; | |
1912 | } | |
4f1f88a6 PP |
1913 | } |
1914 | ||
1ef09eb5 JG |
1915 | goto end; |
1916 | ||
56a1cced | 1917 | error: |
1a9f7075 | 1918 | ctf_fs_destroy(ctf_fs); |
e7a4393b | 1919 | ctf_fs = NULL; |
d94d92ac | 1920 | bt_self_component_set_data( |
707b7d35 | 1921 | bt_self_component_source_as_self_component(self_comp), |
d94d92ac | 1922 | NULL); |
1a9f7075 | 1923 | |
1ef09eb5 | 1924 | end: |
56a1cced JG |
1925 | return ctf_fs; |
1926 | } | |
1927 | ||
ea0b4b9e | 1928 | BT_HIDDEN |
4cdfc5e8 | 1929 | bt_self_component_status ctf_fs_init( |
b19ff26f | 1930 | bt_self_component_source *self_comp, |
c88dd1cb | 1931 | const bt_value *params, __attribute__((unused)) void *init_method_data) |
ea0b4b9e JG |
1932 | { |
1933 | struct ctf_fs_component *ctf_fs; | |
4cdfc5e8 | 1934 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
ea0b4b9e | 1935 | |
d94d92ac | 1936 | ctf_fs = ctf_fs_create(self_comp, params); |
ea0b4b9e | 1937 | if (!ctf_fs) { |
d94d92ac | 1938 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
ea0b4b9e | 1939 | } |
4c1456f0 | 1940 | |
ea0b4b9e JG |
1941 | return ret; |
1942 | } | |
33f93973 PP |
1943 | |
1944 | BT_HIDDEN | |
4cdfc5e8 | 1945 | bt_query_status ctf_fs_query( |
b19ff26f PP |
1946 | bt_self_component_class_source *comp_class, |
1947 | const bt_query_executor *query_exec, | |
1948 | const char *object, const bt_value *params, | |
f4e38e70 | 1949 | __attribute__((unused)) bt_logging_level log_level, |
b19ff26f | 1950 | const bt_value **result) |
33f93973 | 1951 | { |
4cdfc5e8 | 1952 | bt_query_status status = BT_QUERY_STATUS_OK; |
33f93973 | 1953 | |
04c0ba87 | 1954 | if (!strcmp(object, "metadata-info")) { |
d94d92ac | 1955 | status = metadata_info_query(comp_class, params, result); |
97ade20b | 1956 | } else if (!strcmp(object, "trace-info")) { |
d94d92ac | 1957 | status = trace_info_query(comp_class, params, result); |
33f93973 | 1958 | } else { |
55314f2a | 1959 | BT_LOGE("Unknown query object `%s`", object); |
d94d92ac | 1960 | status = BT_QUERY_STATUS_INVALID_OBJECT; |
04c0ba87 | 1961 | goto end; |
33f93973 | 1962 | } |
33f93973 | 1963 | end: |
d94d92ac | 1964 | return status; |
33f93973 | 1965 | } |