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