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