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