Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
ea0b4b9e | 2 | * fs.c |
7a278c8e | 3 | * |
ea0b4b9e | 4 | * Babeltrace CTF file system Reader Component |
7a278c8e | 5 | * |
1a9f7075 | 6 | * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com> |
f3bc2010 | 7 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7a278c8e | 8 | * |
7a278c8e JG |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
4bd72b60 | 28 | #include <babeltrace/common-internal.h> |
9d408fca | 29 | #include <babeltrace/babeltrace.h> |
7d61fa8e | 30 | #include <plugins-common.h> |
ea0b4b9e | 31 | #include <glib.h> |
f6ccaed9 | 32 | #include <babeltrace/assert-internal.h> |
94cf822e | 33 | #include <inttypes.h> |
c55a9f58 | 34 | #include <stdbool.h> |
56a1cced | 35 | #include "fs.h" |
413bc2c4 | 36 | #include "metadata.h" |
94cf822e | 37 | #include "data-stream-file.h" |
e7a4393b | 38 | #include "file.h" |
1e649dff | 39 | #include "../common/metadata/decoder.h" |
d6e69534 | 40 | #include "../common/msg-iter/msg-iter.h" |
a429c321 | 41 | #include "../common/utils/utils.h" |
04c0ba87 | 42 | #include "query.h" |
e7a4393b | 43 | |
55314f2a JG |
44 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC" |
45 | #include "logging.h" | |
ea0b4b9e | 46 | |
94cf822e | 47 | static |
d6e69534 | 48 | int msg_iter_data_set_current_ds_file(struct ctf_fs_msg_iter_data *msg_iter_data) |
94cf822e PP |
49 | { |
50 | struct ctf_fs_ds_file_info *ds_file_info; | |
51 | int ret = 0; | |
52 | ||
d6e69534 PP |
53 | BT_ASSERT(msg_iter_data->ds_file_info_index < |
54 | msg_iter_data->ds_file_group->ds_file_infos->len); | |
94cf822e | 55 | ds_file_info = g_ptr_array_index( |
d6e69534 PP |
56 | msg_iter_data->ds_file_group->ds_file_infos, |
57 | msg_iter_data->ds_file_info_index); | |
58 | ||
59 | ctf_fs_ds_file_destroy(msg_iter_data->ds_file); | |
60 | msg_iter_data->ds_file = ctf_fs_ds_file_create( | |
61 | msg_iter_data->ds_file_group->ctf_fs_trace, | |
62 | msg_iter_data->pc_msg_iter, | |
63 | msg_iter_data->msg_iter, | |
64 | msg_iter_data->ds_file_group->stream, | |
97ade20b | 65 | ds_file_info->path->str); |
d6e69534 | 66 | if (!msg_iter_data->ds_file) { |
94cf822e PP |
67 | ret = -1; |
68 | } | |
69 | ||
70 | return ret; | |
71 | } | |
72 | ||
73 | static | |
d6e69534 PP |
74 | void ctf_fs_msg_iter_data_destroy( |
75 | struct ctf_fs_msg_iter_data *msg_iter_data) | |
94cf822e | 76 | { |
d6e69534 | 77 | if (!msg_iter_data) { |
94cf822e PP |
78 | return; |
79 | } | |
80 | ||
d6e69534 | 81 | ctf_fs_ds_file_destroy(msg_iter_data->ds_file); |
6de92955 | 82 | |
d6e69534 PP |
83 | if (msg_iter_data->msg_iter) { |
84 | bt_msg_iter_destroy(msg_iter_data->msg_iter); | |
6de92955 PP |
85 | } |
86 | ||
d6e69534 | 87 | g_free(msg_iter_data); |
94cf822e PP |
88 | } |
89 | ||
fc917f65 PP |
90 | static |
91 | void set_msg_iter_emits_stream_beginning_end_messages( | |
92 | struct ctf_fs_msg_iter_data *msg_iter_data) | |
93 | { | |
94 | bt_msg_iter_set_emit_stream_beginning_message( | |
95 | msg_iter_data->ds_file->msg_iter, | |
96 | msg_iter_data->ds_file_info_index == 0); | |
97 | bt_msg_iter_set_emit_stream_end_message( | |
98 | msg_iter_data->ds_file->msg_iter, | |
99 | msg_iter_data->ds_file_info_index == | |
100 | msg_iter_data->ds_file_group->ds_file_infos->len - 1); | |
101 | } | |
102 | ||
d4393e08 | 103 | static |
4cdfc5e8 | 104 | bt_self_message_iterator_status ctf_fs_iterator_next_one( |
d6e69534 | 105 | struct ctf_fs_msg_iter_data *msg_iter_data, |
fc917f65 | 106 | const bt_message **out_msg) |
ea0b4b9e | 107 | { |
4cdfc5e8 | 108 | bt_self_message_iterator_status status; |
94cf822e | 109 | |
d6e69534 | 110 | BT_ASSERT(msg_iter_data->ds_file); |
f42867e2 | 111 | |
fc917f65 PP |
112 | while (true) { |
113 | bt_message *msg; | |
114 | ||
115 | status = ctf_fs_ds_file_next(msg_iter_data->ds_file, &msg); | |
116 | switch (status) { | |
117 | case BT_SELF_MESSAGE_ITERATOR_STATUS_OK: | |
118 | *out_msg = msg; | |
119 | msg = NULL; | |
f42867e2 | 120 | goto end; |
fc917f65 PP |
121 | case BT_SELF_MESSAGE_ITERATOR_STATUS_END: |
122 | { | |
123 | int ret; | |
f42867e2 | 124 | |
fc917f65 PP |
125 | if (msg_iter_data->ds_file_info_index == |
126 | msg_iter_data->ds_file_group->ds_file_infos->len - 1) { | |
127 | /* End of all group's stream files */ | |
128 | goto end; | |
129 | } | |
130 | ||
131 | msg_iter_data->ds_file_info_index++; | |
495490c5 PP |
132 | bt_msg_iter_reset_for_next_stream_file( |
133 | msg_iter_data->msg_iter); | |
fc917f65 PP |
134 | set_msg_iter_emits_stream_beginning_end_messages( |
135 | msg_iter_data); | |
94cf822e | 136 | |
94cf822e | 137 | /* |
fc917f65 PP |
138 | * Open and start reading the next stream file |
139 | * within our stream file group. | |
94cf822e | 140 | */ |
fc917f65 PP |
141 | ret = msg_iter_data_set_current_ds_file(msg_iter_data); |
142 | if (ret) { | |
143 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
144 | goto end; | |
145 | } | |
146 | ||
147 | /* Continue the loop to get the next message */ | |
148 | break; | |
94cf822e | 149 | } |
fc917f65 | 150 | default: |
94cf822e PP |
151 | goto end; |
152 | } | |
94cf822e | 153 | } |
d01e0f33 | 154 | |
94cf822e | 155 | end: |
d4393e08 PP |
156 | return status; |
157 | } | |
158 | ||
159 | BT_HIDDEN | |
4cdfc5e8 | 160 | bt_self_message_iterator_status ctf_fs_iterator_next( |
d6e69534 PP |
161 | bt_self_message_iterator *iterator, |
162 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 PP |
163 | uint64_t *count) |
164 | { | |
4cdfc5e8 | 165 | bt_self_message_iterator_status status = |
d6e69534 PP |
166 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
167 | struct ctf_fs_msg_iter_data *msg_iter_data = | |
168 | bt_self_message_iterator_get_data(iterator); | |
d4393e08 PP |
169 | uint64_t i = 0; |
170 | ||
d6e69534 PP |
171 | while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
172 | status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]); | |
173 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
d4393e08 PP |
174 | i++; |
175 | } | |
176 | } | |
177 | ||
178 | if (i > 0) { | |
179 | /* | |
180 | * Even if ctf_fs_iterator_next_one() returned something | |
d6e69534 PP |
181 | * else than BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we |
182 | * accumulated message objects in the output | |
183 | * message array, so we need to return | |
184 | * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they are | |
d4393e08 | 185 | * transfered to downstream. This other status occurs |
d6e69534 | 186 | * again the next time muxer_msg_iter_do_next() is |
d4393e08 | 187 | * called, possibly without any accumulated |
d6e69534 | 188 | * message, in which case we'll return it. |
d4393e08 PP |
189 | */ |
190 | *count = i; | |
d6e69534 | 191 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
192 | } |
193 | ||
194 | return status; | |
ea0b4b9e | 195 | } |
bfd20a42 | 196 | |
6a9bb5e9 PP |
197 | static |
198 | int ctf_fs_iterator_reset(struct ctf_fs_msg_iter_data *msg_iter_data) | |
199 | { | |
200 | int ret; | |
201 | ||
202 | msg_iter_data->ds_file_info_index = 0; | |
203 | ret = msg_iter_data_set_current_ds_file(msg_iter_data); | |
204 | if (ret) { | |
205 | goto end; | |
206 | } | |
207 | ||
208 | bt_msg_iter_reset(msg_iter_data->msg_iter); | |
209 | set_msg_iter_emits_stream_beginning_end_messages(msg_iter_data); | |
210 | ||
211 | end: | |
212 | return ret; | |
213 | } | |
214 | ||
215 | BT_HIDDEN | |
216 | bt_self_message_iterator_status ctf_fs_iterator_seek_beginning( | |
217 | bt_self_message_iterator *it) | |
218 | { | |
219 | struct ctf_fs_msg_iter_data *msg_iter_data = | |
220 | bt_self_message_iterator_get_data(it); | |
221 | bt_self_message_iterator_status status = | |
222 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
223 | ||
224 | BT_ASSERT(msg_iter_data); | |
225 | if (ctf_fs_iterator_reset(msg_iter_data)) { | |
226 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
227 | } | |
228 | ||
229 | return status; | |
230 | } | |
231 | ||
232 | BT_HIDDEN | |
d6e69534 | 233 | void ctf_fs_iterator_finalize(bt_self_message_iterator *it) |
760051fa | 234 | { |
d6e69534 PP |
235 | ctf_fs_msg_iter_data_destroy( |
236 | bt_self_message_iterator_get_data(it)); | |
760051fa JG |
237 | } |
238 | ||
6a9bb5e9 | 239 | BT_HIDDEN |
4cdfc5e8 | 240 | bt_self_message_iterator_status ctf_fs_iterator_init( |
d6e69534 | 241 | bt_self_message_iterator *self_msg_iter, |
b19ff26f PP |
242 | bt_self_component_source *self_comp, |
243 | bt_self_component_port_output *self_port) | |
4c1456f0 | 244 | { |
4f1f88a6 | 245 | struct ctf_fs_port_data *port_data; |
d6e69534 | 246 | struct ctf_fs_msg_iter_data *msg_iter_data = NULL; |
4cdfc5e8 | 247 | bt_self_message_iterator_status ret = |
d6e69534 | 248 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
760051fa | 249 | |
d94d92ac | 250 | port_data = bt_self_component_port_get_data( |
707b7d35 | 251 | bt_self_component_port_output_as_self_component_port( |
d94d92ac PP |
252 | self_port)); |
253 | BT_ASSERT(port_data); | |
d6e69534 PP |
254 | msg_iter_data = g_new0(struct ctf_fs_msg_iter_data, 1); |
255 | if (!msg_iter_data) { | |
256 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM; | |
94cf822e PP |
257 | goto error; |
258 | } | |
259 | ||
d6e69534 PP |
260 | msg_iter_data->pc_msg_iter = self_msg_iter; |
261 | msg_iter_data->msg_iter = bt_msg_iter_create( | |
44c440bc | 262 | port_data->ds_file_group->ctf_fs_trace->metadata->tc, |
6de92955 PP |
263 | bt_common_get_page_size() * 8, |
264 | ctf_fs_ds_file_medops, NULL); | |
d6e69534 PP |
265 | if (!msg_iter_data->msg_iter) { |
266 | BT_LOGE_STR("Cannot create a CTF message iterator."); | |
267 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM; | |
6de92955 PP |
268 | goto error; |
269 | } | |
270 | ||
d6e69534 | 271 | msg_iter_data->ds_file_group = port_data->ds_file_group; |
6a9bb5e9 | 272 | if (ctf_fs_iterator_reset(msg_iter_data)) { |
d6e69534 | 273 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
4f1f88a6 | 274 | goto error; |
760051fa JG |
275 | } |
276 | ||
d6e69534 PP |
277 | bt_self_message_iterator_set_data(self_msg_iter, |
278 | msg_iter_data); | |
279 | if (ret != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
4f1f88a6 | 280 | goto error; |
760051fa JG |
281 | } |
282 | ||
d6e69534 | 283 | msg_iter_data = NULL; |
4f1f88a6 | 284 | goto end; |
5b29e799 | 285 | |
4f1f88a6 | 286 | error: |
d6e69534 | 287 | bt_self_message_iterator_set_data(self_msg_iter, NULL); |
4f1f88a6 | 288 | |
760051fa | 289 | end: |
d6e69534 | 290 | ctf_fs_msg_iter_data_destroy(msg_iter_data); |
760051fa JG |
291 | return ret; |
292 | } | |
293 | ||
f280892e | 294 | BT_HIDDEN |
1a9f7075 | 295 | void ctf_fs_destroy(struct ctf_fs_component *ctf_fs) |
760051fa | 296 | { |
4f1f88a6 | 297 | if (!ctf_fs) { |
8fa760ba JG |
298 | return; |
299 | } | |
4f1f88a6 | 300 | |
1a9f7075 PP |
301 | if (ctf_fs->traces) { |
302 | g_ptr_array_free(ctf_fs->traces, TRUE); | |
56a1cced | 303 | } |
4f1f88a6 PP |
304 | |
305 | if (ctf_fs->port_data) { | |
306 | g_ptr_array_free(ctf_fs->port_data, TRUE); | |
c14d7e26 | 307 | } |
760051fa | 308 | |
1a9f7075 PP |
309 | g_free(ctf_fs); |
310 | } | |
311 | ||
f280892e SM |
312 | static |
313 | void port_data_destroy(struct ctf_fs_port_data *port_data) | |
314 | { | |
315 | if (!port_data) { | |
316 | return; | |
317 | } | |
318 | ||
319 | g_free(port_data); | |
320 | } | |
321 | ||
322 | static | |
323 | void port_data_destroy_notifier(void *data) { | |
324 | port_data_destroy(data); | |
325 | } | |
326 | ||
327 | static | |
97ade20b | 328 | void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace) |
1a9f7075 | 329 | { |
1a9f7075 PP |
330 | if (!ctf_fs_trace) { |
331 | return; | |
332 | } | |
333 | ||
94cf822e PP |
334 | if (ctf_fs_trace->ds_file_groups) { |
335 | g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE); | |
336 | } | |
337 | ||
c5b9b441 | 338 | BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace->trace); |
862ca4ed | 339 | |
1a9f7075 PP |
340 | if (ctf_fs_trace->path) { |
341 | g_string_free(ctf_fs_trace->path, TRUE); | |
4f1f88a6 | 342 | } |
760051fa | 343 | |
1a9f7075 PP |
344 | if (ctf_fs_trace->name) { |
345 | g_string_free(ctf_fs_trace->name, TRUE); | |
346 | } | |
347 | ||
348 | if (ctf_fs_trace->metadata) { | |
349 | ctf_fs_metadata_fini(ctf_fs_trace->metadata); | |
350 | g_free(ctf_fs_trace->metadata); | |
351 | } | |
352 | ||
1a9f7075 | 353 | g_free(ctf_fs_trace); |
4c1456f0 JG |
354 | } |
355 | ||
97ade20b | 356 | static |
56a924f4 | 357 | void ctf_fs_trace_destroy_notifier(void *data) |
97ade20b JG |
358 | { |
359 | struct ctf_fs_trace *trace = data; | |
360 | ctf_fs_trace_destroy(trace); | |
361 | } | |
362 | ||
f280892e | 363 | struct ctf_fs_component *ctf_fs_component_create(void) |
a4792757 | 364 | { |
f280892e | 365 | struct ctf_fs_component *ctf_fs; |
a4792757 | 366 | |
f280892e SM |
367 | ctf_fs = g_new0(struct ctf_fs_component, 1); |
368 | if (!ctf_fs) { | |
369 | goto error; | |
370 | } | |
4f1f88a6 | 371 | |
f280892e SM |
372 | ctf_fs->port_data = |
373 | g_ptr_array_new_with_free_func(port_data_destroy_notifier); | |
374 | if (!ctf_fs->port_data) { | |
375 | goto error; | |
4f1f88a6 PP |
376 | } |
377 | ||
f280892e SM |
378 | ctf_fs->traces = |
379 | g_ptr_array_new_with_free_func(ctf_fs_trace_destroy_notifier); | |
380 | if (!ctf_fs->traces) { | |
381 | goto error; | |
382 | } | |
383 | ||
384 | goto end; | |
385 | ||
386 | error: | |
387 | if (ctf_fs) { | |
388 | ctf_fs_destroy(ctf_fs); | |
389 | } | |
390 | ||
391 | end: | |
392 | return ctf_fs; | |
393 | } | |
394 | ||
395 | void ctf_fs_finalize(bt_self_component_source *component) | |
396 | { | |
397 | ctf_fs_destroy(bt_self_component_get_data( | |
398 | bt_self_component_source_as_self_component(component))); | |
5b29e799 JG |
399 | } |
400 | ||
547eacf1 PP |
401 | static |
402 | GString *get_stream_instance_unique_name( | |
403 | struct ctf_fs_ds_file_group *ds_file_group) | |
404 | { | |
405 | GString *name; | |
406 | struct ctf_fs_ds_file_info *ds_file_info; | |
407 | ||
408 | name = g_string_new(NULL); | |
409 | if (!name) { | |
410 | goto end; | |
411 | } | |
412 | ||
413 | /* | |
414 | * If there's more than one stream file in the stream file | |
415 | * group, the first (earliest) stream file's path is used as | |
416 | * the stream's unique name. | |
417 | */ | |
f6ccaed9 | 418 | BT_ASSERT(ds_file_group->ds_file_infos->len > 0); |
547eacf1 PP |
419 | ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0); |
420 | g_string_assign(name, ds_file_info->path->str); | |
421 | ||
422 | end: | |
423 | return name; | |
424 | } | |
425 | ||
5b29e799 | 426 | static |
55314f2a JG |
427 | int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, |
428 | struct ctf_fs_trace *ctf_fs_trace, | |
94cf822e | 429 | struct ctf_fs_ds_file_group *ds_file_group) |
5b29e799 | 430 | { |
4f1f88a6 | 431 | int ret = 0; |
4f1f88a6 PP |
432 | struct ctf_fs_port_data *port_data = NULL; |
433 | GString *port_name = NULL; | |
434 | ||
547eacf1 | 435 | port_name = get_stream_instance_unique_name(ds_file_group); |
4f1f88a6 PP |
436 | if (!port_name) { |
437 | goto error; | |
438 | } | |
439 | ||
55314f2a | 440 | BT_LOGD("Creating one port named `%s`", port_name->str); |
4f1f88a6 PP |
441 | |
442 | /* Create output port for this file */ | |
4f1f88a6 PP |
443 | port_data = g_new0(struct ctf_fs_port_data, 1); |
444 | if (!port_data) { | |
445 | goto error; | |
446 | } | |
447 | ||
5c563278 | 448 | port_data->ctf_fs = ctf_fs; |
94cf822e | 449 | port_data->ds_file_group = ds_file_group; |
d94d92ac PP |
450 | ret = bt_self_component_source_add_output_port( |
451 | ctf_fs->self_comp, port_name->str, port_data, NULL); | |
147337a3 | 452 | if (ret) { |
4f1f88a6 PP |
453 | goto error; |
454 | } | |
455 | ||
456 | g_ptr_array_add(ctf_fs->port_data, port_data); | |
457 | port_data = NULL; | |
458 | goto end; | |
459 | ||
460 | error: | |
461 | ret = -1; | |
462 | ||
463 | end: | |
464 | if (port_name) { | |
465 | g_string_free(port_name, TRUE); | |
466 | } | |
467 | ||
4f1f88a6 PP |
468 | port_data_destroy(port_data); |
469 | return ret; | |
5b29e799 JG |
470 | } |
471 | ||
472 | static | |
55314f2a JG |
473 | int create_ports_for_trace(struct ctf_fs_component *ctf_fs, |
474 | struct ctf_fs_trace *ctf_fs_trace) | |
94cf822e PP |
475 | { |
476 | int ret = 0; | |
94cf822e PP |
477 | size_t i; |
478 | ||
479 | /* Create one output port for each stream file group */ | |
480 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
481 | struct ctf_fs_ds_file_group *ds_file_group = | |
482 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
483 | ||
55314f2a JG |
484 | ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace, |
485 | ds_file_group); | |
94cf822e | 486 | if (ret) { |
55314f2a | 487 | BT_LOGE("Cannot create output port."); |
94cf822e PP |
488 | goto end; |
489 | } | |
490 | } | |
491 | ||
492 | end: | |
493 | return ret; | |
494 | } | |
495 | ||
94cf822e PP |
496 | static |
497 | void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info) | |
498 | { | |
499 | if (!ds_file_info) { | |
500 | return; | |
501 | } | |
502 | ||
503 | if (ds_file_info->path) { | |
504 | g_string_free(ds_file_info->path, TRUE); | |
505 | } | |
506 | ||
97ade20b | 507 | ctf_fs_ds_index_destroy(ds_file_info->index); |
94cf822e PP |
508 | g_free(ds_file_info); |
509 | } | |
510 | ||
511 | static | |
512 | struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path, | |
44c440bc | 513 | int64_t begin_ns, struct ctf_fs_ds_index *index) |
94cf822e PP |
514 | { |
515 | struct ctf_fs_ds_file_info *ds_file_info; | |
516 | ||
517 | ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1); | |
518 | if (!ds_file_info) { | |
519 | goto end; | |
520 | } | |
521 | ||
522 | ds_file_info->path = g_string_new(path); | |
523 | if (!ds_file_info->path) { | |
524 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
525 | ds_file_info = NULL; | |
526 | goto end; | |
527 | } | |
528 | ||
529 | ds_file_info->begin_ns = begin_ns; | |
97ade20b JG |
530 | ds_file_info->index = index; |
531 | index = NULL; | |
94cf822e PP |
532 | |
533 | end: | |
97ade20b | 534 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
535 | return ds_file_info; |
536 | } | |
537 | ||
538 | static | |
539 | void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group) | |
540 | { | |
541 | if (!ds_file_group) { | |
542 | return; | |
543 | } | |
544 | ||
545 | if (ds_file_group->ds_file_infos) { | |
546 | g_ptr_array_free(ds_file_group->ds_file_infos, TRUE); | |
547 | } | |
548 | ||
c5b9b441 | 549 | bt_stream_put_ref(ds_file_group->stream); |
94cf822e PP |
550 | g_free(ds_file_group); |
551 | } | |
552 | ||
553 | static | |
554 | struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create( | |
555 | struct ctf_fs_trace *ctf_fs_trace, | |
60363f2f | 556 | struct ctf_stream_class *sc, |
94cf822e PP |
557 | uint64_t stream_instance_id) |
558 | { | |
559 | struct ctf_fs_ds_file_group *ds_file_group; | |
560 | ||
94cf822e PP |
561 | ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1); |
562 | if (!ds_file_group) { | |
563 | goto error; | |
564 | } | |
565 | ||
566 | ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func( | |
567 | (GDestroyNotify) ctf_fs_ds_file_info_destroy); | |
568 | if (!ds_file_group->ds_file_infos) { | |
569 | goto error; | |
570 | } | |
571 | ||
547eacf1 | 572 | ds_file_group->stream_id = stream_instance_id; |
60363f2f PP |
573 | BT_ASSERT(sc); |
574 | ds_file_group->sc = sc; | |
94cf822e | 575 | ds_file_group->ctf_fs_trace = ctf_fs_trace; |
94cf822e PP |
576 | goto end; |
577 | ||
578 | error: | |
579 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
580 | ds_file_group = NULL; | |
581 | ||
582 | end: | |
583 | return ds_file_group; | |
584 | } | |
585 | ||
8bf7105e MJ |
586 | /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */ |
587 | static | |
588 | void array_insert(GPtrArray *array, gpointer element, size_t pos) | |
589 | { | |
590 | size_t original_array_len = array->len; | |
591 | ||
592 | /* Allocate an unused element at the end of the array. */ | |
593 | g_ptr_array_add(array, NULL); | |
594 | ||
595 | /* If we are not inserting at the end, move the elements by one. */ | |
596 | if (pos < original_array_len) { | |
597 | memmove(&(array->pdata[pos + 1]), | |
598 | &(array->pdata[pos]), | |
599 | (original_array_len - pos) * sizeof(gpointer)); | |
600 | } | |
601 | ||
602 | /* Insert the value and bump the array len */ | |
603 | array->pdata[pos] = element; | |
604 | } | |
605 | ||
94cf822e PP |
606 | static |
607 | int ctf_fs_ds_file_group_add_ds_file_info( | |
608 | struct ctf_fs_ds_file_group *ds_file_group, | |
44c440bc | 609 | const char *path, int64_t begin_ns, |
97ade20b | 610 | struct ctf_fs_ds_index *index) |
94cf822e PP |
611 | { |
612 | struct ctf_fs_ds_file_info *ds_file_info; | |
613 | gint i = 0; | |
614 | int ret = 0; | |
615 | ||
97ade20b JG |
616 | /* Onwership of index is transferred. */ |
617 | ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns, index); | |
618 | index = NULL; | |
94cf822e PP |
619 | if (!ds_file_info) { |
620 | goto error; | |
621 | } | |
622 | ||
623 | /* Find a spot to insert this one */ | |
624 | for (i = 0; i < ds_file_group->ds_file_infos->len; i++) { | |
625 | struct ctf_fs_ds_file_info *other_ds_file_info = | |
626 | g_ptr_array_index(ds_file_group->ds_file_infos, i); | |
627 | ||
628 | if (begin_ns < other_ds_file_info->begin_ns) { | |
629 | break; | |
630 | } | |
631 | } | |
632 | ||
8bf7105e | 633 | array_insert(ds_file_group->ds_file_infos, ds_file_info, i); |
94cf822e PP |
634 | ds_file_info = NULL; |
635 | goto end; | |
636 | ||
637 | error: | |
638 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
97ade20b | 639 | ctf_fs_ds_index_destroy(index); |
94cf822e | 640 | ret = -1; |
94cf822e PP |
641 | end: |
642 | return ret; | |
643 | } | |
644 | ||
645 | static | |
646 | int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, | |
e5be10ef | 647 | const char *path) |
94cf822e | 648 | { |
44c440bc PP |
649 | int64_t stream_instance_id = -1; |
650 | int64_t begin_ns = -1; | |
94cf822e PP |
651 | struct ctf_fs_ds_file_group *ds_file_group = NULL; |
652 | bool add_group = false; | |
653 | int ret; | |
654 | size_t i; | |
6de92955 | 655 | struct ctf_fs_ds_file *ds_file = NULL; |
97ade20b | 656 | struct ctf_fs_ds_index *index = NULL; |
d6e69534 | 657 | struct bt_msg_iter *msg_iter = NULL; |
44c440bc | 658 | struct ctf_stream_class *sc = NULL; |
d6e69534 | 659 | struct bt_msg_iter_packet_properties props; |
94cf822e | 660 | |
d6e69534 | 661 | msg_iter = bt_msg_iter_create(ctf_fs_trace->metadata->tc, |
6de92955 | 662 | bt_common_get_page_size() * 8, ctf_fs_ds_file_medops, NULL); |
d6e69534 PP |
663 | if (!msg_iter) { |
664 | BT_LOGE_STR("Cannot create a CTF message iterator."); | |
6de92955 PP |
665 | goto error; |
666 | } | |
667 | ||
d6e69534 | 668 | ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, msg_iter, |
5c563278 | 669 | NULL, path); |
97ade20b JG |
670 | if (!ds_file) { |
671 | goto error; | |
672 | } | |
673 | ||
41693723 | 674 | ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); |
94cf822e | 675 | if (ret) { |
55314f2a | 676 | BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).", |
94cf822e PP |
677 | path); |
678 | goto error; | |
679 | } | |
680 | ||
44c440bc PP |
681 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
682 | props.stream_class_id); | |
683 | BT_ASSERT(sc); | |
44c440bc PP |
684 | stream_instance_id = props.data_stream_id; |
685 | ||
686 | if (props.snapshots.beginning_clock != UINT64_C(-1)) { | |
687 | BT_ASSERT(sc->default_clock_class); | |
0f2d58c9 PP |
688 | ret = bt_util_clock_cycles_to_ns_from_origin( |
689 | props.snapshots.beginning_clock, | |
690 | sc->default_clock_class->frequency, | |
691 | sc->default_clock_class->offset_seconds, | |
692 | sc->default_clock_class->offset_cycles, &begin_ns); | |
44c440bc PP |
693 | if (ret) { |
694 | BT_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).", | |
695 | path); | |
696 | goto error; | |
697 | } | |
94cf822e PP |
698 | } |
699 | ||
97ade20b JG |
700 | index = ctf_fs_ds_file_build_index(ds_file); |
701 | if (!index) { | |
702 | BT_LOGW("Failed to index CTF stream file \'%s\'", | |
703 | ds_file->file->path->str); | |
704 | } | |
705 | ||
44c440bc | 706 | if (begin_ns == -1) { |
94cf822e PP |
707 | /* |
708 | * No beggining timestamp to sort the stream files | |
709 | * within a stream file group, so consider that this | |
710 | * file must be the only one within its group. | |
711 | */ | |
44c440bc | 712 | stream_instance_id = -1; |
94cf822e PP |
713 | } |
714 | ||
44c440bc | 715 | if (stream_instance_id == -1) { |
94cf822e PP |
716 | /* |
717 | * No stream instance ID or no beginning timestamp: | |
718 | * create a unique stream file group for this stream | |
719 | * file because, even if there's a stream instance ID, | |
720 | * there's no timestamp to order the file within its | |
721 | * group. | |
722 | */ | |
94cf822e | 723 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, |
60363f2f | 724 | sc, UINT64_C(-1)); |
94cf822e PP |
725 | if (!ds_file_group) { |
726 | goto error; | |
727 | } | |
728 | ||
729 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, | |
547eacf1 | 730 | path, begin_ns, index); |
97ade20b JG |
731 | /* Ownership of index is transferred. */ |
732 | index = NULL; | |
94cf822e PP |
733 | if (ret) { |
734 | goto error; | |
735 | } | |
736 | ||
737 | add_group = true; | |
738 | goto end; | |
739 | } | |
740 | ||
44c440bc PP |
741 | BT_ASSERT(stream_instance_id != -1); |
742 | BT_ASSERT(begin_ns != -1); | |
94cf822e PP |
743 | |
744 | /* Find an existing stream file group with this ID */ | |
745 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
94cf822e PP |
746 | ds_file_group = g_ptr_array_index( |
747 | ctf_fs_trace->ds_file_groups, i); | |
94cf822e | 748 | |
60363f2f | 749 | if (ds_file_group->sc == sc && |
547eacf1 PP |
750 | ds_file_group->stream_id == |
751 | stream_instance_id) { | |
94cf822e PP |
752 | break; |
753 | } | |
754 | ||
755 | ds_file_group = NULL; | |
756 | } | |
757 | ||
758 | if (!ds_file_group) { | |
759 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, | |
60363f2f | 760 | sc, stream_instance_id); |
94cf822e PP |
761 | if (!ds_file_group) { |
762 | goto error; | |
763 | } | |
764 | ||
765 | add_group = true; | |
766 | } | |
767 | ||
547eacf1 PP |
768 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path, |
769 | begin_ns, index); | |
97ade20b | 770 | index = NULL; |
94cf822e PP |
771 | if (ret) { |
772 | goto error; | |
773 | } | |
774 | ||
775 | goto end; | |
776 | ||
777 | error: | |
778 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
779 | ret = -1; | |
780 | ||
781 | end: | |
782 | if (add_group && ds_file_group) { | |
783 | g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group); | |
784 | } | |
547eacf1 | 785 | |
97ade20b | 786 | ctf_fs_ds_file_destroy(ds_file); |
6de92955 | 787 | |
d6e69534 PP |
788 | if (msg_iter) { |
789 | bt_msg_iter_destroy(msg_iter); | |
6de92955 PP |
790 | } |
791 | ||
97ade20b | 792 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
793 | return ret; |
794 | } | |
795 | ||
796 | static | |
e5be10ef | 797 | int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace) |
e7a4393b JG |
798 | { |
799 | int ret = 0; | |
4f1f88a6 | 800 | const char *basename; |
e7a4393b | 801 | GError *error = NULL; |
4f1f88a6 | 802 | GDir *dir = NULL; |
e7a4393b | 803 | |
94cf822e | 804 | /* Check each file in the path directory, except specific ones */ |
1a9f7075 | 805 | dir = g_dir_open(ctf_fs_trace->path->str, 0, &error); |
e7a4393b | 806 | if (!dir) { |
55314f2a | 807 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 | 808 | ctf_fs_trace->path->str, error->message, |
4f1f88a6 | 809 | error->code); |
e7a4393b JG |
810 | goto error; |
811 | } | |
812 | ||
4f1f88a6 | 813 | while ((basename = g_dir_read_name(dir))) { |
94cf822e PP |
814 | struct ctf_fs_file *file; |
815 | ||
4f1f88a6 | 816 | if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) { |
e7a4393b | 817 | /* Ignore the metadata stream. */ |
3743a302 | 818 | BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 819 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
820 | continue; |
821 | } | |
822 | ||
4f1f88a6 | 823 | if (basename[0] == '.') { |
3743a302 | 824 | BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 825 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
826 | continue; |
827 | } | |
828 | ||
829 | /* Create the file. */ | |
55314f2a | 830 | file = ctf_fs_file_create(); |
e7a4393b | 831 | if (!file) { |
3743a302 | 832 | BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 833 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
834 | goto error; |
835 | } | |
836 | ||
837 | /* Create full path string. */ | |
3743a302 | 838 | g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s", |
1a9f7075 | 839 | ctf_fs_trace->path->str, basename); |
e7a4393b | 840 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
55314f2a | 841 | BT_LOGD("Ignoring non-regular file `%s`", |
1a9f7075 | 842 | file->path->str); |
e7a4393b | 843 | ctf_fs_file_destroy(file); |
4f1f88a6 | 844 | file = NULL; |
e7a4393b JG |
845 | continue; |
846 | } | |
847 | ||
55314f2a | 848 | ret = ctf_fs_file_open(file, "rb"); |
4f1f88a6 | 849 | if (ret) { |
55314f2a | 850 | BT_LOGE("Cannot open stream file `%s`", file->path->str); |
e7a4393b JG |
851 | goto error; |
852 | } | |
853 | ||
9fa0891a JG |
854 | if (file->size == 0) { |
855 | /* Skip empty stream. */ | |
55314f2a | 856 | BT_LOGD("Ignoring empty file `%s`", file->path->str); |
9fa0891a JG |
857 | ctf_fs_file_destroy(file); |
858 | continue; | |
859 | } | |
860 | ||
e5be10ef | 861 | ret = add_ds_file_to_ds_file_group(ctf_fs_trace, |
94cf822e | 862 | file->path->str); |
4f1f88a6 | 863 | if (ret) { |
89b08333 | 864 | BT_LOGE("Cannot add stream file `%s` to stream file group", |
1a9f7075 | 865 | file->path->str); |
94cf822e | 866 | ctf_fs_file_destroy(file); |
e7a4393b JG |
867 | goto error; |
868 | } | |
869 | ||
4f1f88a6 | 870 | ctf_fs_file_destroy(file); |
e7a4393b JG |
871 | } |
872 | ||
873 | goto end; | |
4f1f88a6 | 874 | |
e7a4393b JG |
875 | error: |
876 | ret = -1; | |
4f1f88a6 | 877 | |
e7a4393b JG |
878 | end: |
879 | if (dir) { | |
880 | g_dir_close(dir); | |
881 | dir = NULL; | |
882 | } | |
4f1f88a6 | 883 | |
e7a4393b JG |
884 | if (error) { |
885 | g_error_free(error); | |
886 | } | |
5b29e799 | 887 | |
91457551 | 888 | return ret; |
5b29e799 JG |
889 | } |
890 | ||
862ca4ed | 891 | static |
b19ff26f | 892 | int set_trace_name(bt_trace *trace, const char *name_suffix) |
862ca4ed PP |
893 | { |
894 | int ret = 0; | |
b19ff26f PP |
895 | const bt_trace_class *tc = bt_trace_borrow_class_const(trace); |
896 | const bt_value *val; | |
862ca4ed PP |
897 | GString *name; |
898 | ||
899 | name = g_string_new(NULL); | |
900 | if (!name) { | |
901 | BT_LOGE_STR("Failed to allocate a GString."); | |
902 | ret = -1; | |
903 | goto end; | |
904 | } | |
905 | ||
906 | /* | |
907 | * Check if we have a trace environment string value named `hostname`. | |
908 | * If so, use it as the trace name's prefix. | |
909 | */ | |
910 | val = bt_trace_class_borrow_environment_entry_value_by_name_const( | |
911 | tc, "hostname"); | |
912 | if (val && bt_value_is_string(val)) { | |
913 | g_string_append(name, bt_value_string_get(val)); | |
914 | ||
915 | if (name_suffix) { | |
916 | g_string_append_c(name, G_DIR_SEPARATOR); | |
917 | } | |
918 | } | |
919 | ||
920 | if (name_suffix) { | |
921 | g_string_append(name, name_suffix); | |
922 | } | |
923 | ||
924 | ret = bt_trace_set_name(trace, name->str); | |
925 | if (ret) { | |
926 | goto end; | |
927 | } | |
928 | ||
929 | goto end; | |
930 | ||
931 | end: | |
932 | if (name) { | |
933 | g_string_free(name, TRUE); | |
934 | } | |
935 | ||
936 | return ret; | |
937 | } | |
938 | ||
f280892e | 939 | static |
41693723 PP |
940 | struct ctf_fs_trace *ctf_fs_trace_create(bt_self_component_source *self_comp, |
941 | const char *path, const char *name, | |
e5be10ef | 942 | struct ctf_fs_metadata_config *metadata_config) |
1a9f7075 PP |
943 | { |
944 | struct ctf_fs_trace *ctf_fs_trace; | |
945 | int ret; | |
946 | ||
947 | ctf_fs_trace = g_new0(struct ctf_fs_trace, 1); | |
948 | if (!ctf_fs_trace) { | |
949 | goto end; | |
950 | } | |
951 | ||
1a9f7075 PP |
952 | ctf_fs_trace->path = g_string_new(path); |
953 | if (!ctf_fs_trace->path) { | |
954 | goto error; | |
955 | } | |
956 | ||
957 | ctf_fs_trace->name = g_string_new(name); | |
958 | if (!ctf_fs_trace->name) { | |
959 | goto error; | |
960 | } | |
961 | ||
962 | ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1); | |
963 | if (!ctf_fs_trace->metadata) { | |
964 | goto error; | |
965 | } | |
966 | ||
44c440bc | 967 | ctf_fs_metadata_init(ctf_fs_trace->metadata); |
94cf822e PP |
968 | ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func( |
969 | (GDestroyNotify) ctf_fs_ds_file_group_destroy); | |
970 | if (!ctf_fs_trace->ds_file_groups) { | |
971 | goto error; | |
972 | } | |
973 | ||
41693723 PP |
974 | ret = ctf_fs_metadata_set_trace_class(self_comp, |
975 | ctf_fs_trace, metadata_config); | |
862ca4ed PP |
976 | if (ret) { |
977 | goto error; | |
978 | } | |
979 | ||
41693723 PP |
980 | if (ctf_fs_trace->metadata->trace_class) { |
981 | ctf_fs_trace->trace = | |
982 | bt_trace_create(ctf_fs_trace->metadata->trace_class); | |
983 | if (!ctf_fs_trace->trace) { | |
984 | goto error; | |
985 | } | |
862ca4ed PP |
986 | } |
987 | ||
41693723 PP |
988 | if (ctf_fs_trace->trace) { |
989 | ret = set_trace_name(ctf_fs_trace->trace, name); | |
990 | if (ret) { | |
991 | goto error; | |
992 | } | |
1a9f7075 PP |
993 | } |
994 | ||
e5be10ef | 995 | ret = create_ds_file_groups(ctf_fs_trace); |
94cf822e PP |
996 | if (ret) { |
997 | goto error; | |
998 | } | |
999 | ||
1a9f7075 PP |
1000 | goto end; |
1001 | ||
1002 | error: | |
1003 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1004 | ctf_fs_trace = NULL; | |
44c440bc | 1005 | |
1a9f7075 PP |
1006 | end: |
1007 | return ctf_fs_trace; | |
1008 | } | |
1009 | ||
1010 | static | |
1011 | int path_is_ctf_trace(const char *path) | |
1012 | { | |
1013 | GString *metadata_path = g_string_new(NULL); | |
1014 | int ret = 0; | |
1015 | ||
1016 | if (!metadata_path) { | |
1017 | ret = -1; | |
1018 | goto end; | |
1019 | } | |
1020 | ||
3743a302 | 1021 | g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME); |
1a9f7075 PP |
1022 | |
1023 | if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) { | |
1024 | ret = 1; | |
1025 | goto end; | |
1026 | } | |
1027 | ||
1028 | end: | |
1029 | g_string_free(metadata_path, TRUE); | |
1030 | return ret; | |
1031 | } | |
1032 | ||
1033 | static | |
55314f2a | 1034 | int add_trace_path(GList **trace_paths, const char *path) |
1a9f7075 | 1035 | { |
4bd72b60 | 1036 | GString *norm_path = NULL; |
1a9f7075 | 1037 | int ret = 0; |
1a9f7075 | 1038 | |
4bd72b60 PP |
1039 | norm_path = bt_common_normalize_path(path, NULL); |
1040 | if (!norm_path) { | |
55314f2a | 1041 | BT_LOGE("Failed to normalize path `%s`.", path); |
1a9f7075 PP |
1042 | ret = -1; |
1043 | goto end; | |
1044 | } | |
1045 | ||
3743a302 | 1046 | // FIXME: Remove or ifdef for __MINGW32__ |
4bd72b60 | 1047 | if (strcmp(norm_path->str, "/") == 0) { |
55314f2a | 1048 | BT_LOGE("Opening a trace in `/` is not supported."); |
1a9f7075 PP |
1049 | ret = -1; |
1050 | goto end; | |
1051 | } | |
1052 | ||
4bd72b60 | 1053 | *trace_paths = g_list_prepend(*trace_paths, norm_path); |
f6ccaed9 | 1054 | BT_ASSERT(*trace_paths); |
4bd72b60 | 1055 | norm_path = NULL; |
1a9f7075 PP |
1056 | |
1057 | end: | |
4bd72b60 PP |
1058 | if (norm_path) { |
1059 | g_string_free(norm_path, TRUE); | |
1060 | } | |
1061 | ||
1a9f7075 PP |
1062 | return ret; |
1063 | } | |
1064 | ||
f280892e | 1065 | static |
55314f2a | 1066 | int ctf_fs_find_traces(GList **trace_paths, const char *start_path) |
1a9f7075 PP |
1067 | { |
1068 | int ret; | |
1069 | GError *error = NULL; | |
1070 | GDir *dir = NULL; | |
1071 | const char *basename = NULL; | |
1072 | ||
1073 | /* Check if the starting path is a CTF trace itself */ | |
1074 | ret = path_is_ctf_trace(start_path); | |
1075 | if (ret < 0) { | |
1076 | goto end; | |
1077 | } | |
1078 | ||
1079 | if (ret) { | |
1080 | /* | |
4bd72b60 PP |
1081 | * Stop recursion: a CTF trace cannot contain another |
1082 | * CTF trace. | |
1a9f7075 | 1083 | */ |
55314f2a | 1084 | ret = add_trace_path(trace_paths, start_path); |
1a9f7075 PP |
1085 | goto end; |
1086 | } | |
1087 | ||
1088 | /* Look for subdirectories */ | |
1089 | if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) { | |
1090 | /* Starting path is not a directory: end of recursion */ | |
1091 | goto end; | |
1092 | } | |
1093 | ||
1094 | dir = g_dir_open(start_path, 0, &error); | |
1095 | if (!dir) { | |
1096 | if (error->code == G_FILE_ERROR_ACCES) { | |
55314f2a | 1097 | BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing", |
1a9f7075 PP |
1098 | start_path, error->message, error->code); |
1099 | goto end; | |
1100 | } | |
1101 | ||
55314f2a | 1102 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 PP |
1103 | start_path, error->message, error->code); |
1104 | ret = -1; | |
1105 | goto end; | |
1106 | } | |
1107 | ||
1108 | while ((basename = g_dir_read_name(dir))) { | |
1109 | GString *sub_path = g_string_new(NULL); | |
1110 | ||
1111 | if (!sub_path) { | |
1112 | ret = -1; | |
1113 | goto end; | |
1114 | } | |
1115 | ||
3743a302 | 1116 | g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename); |
55314f2a | 1117 | ret = ctf_fs_find_traces(trace_paths, sub_path->str); |
1a9f7075 PP |
1118 | g_string_free(sub_path, TRUE); |
1119 | if (ret) { | |
1120 | goto end; | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | end: | |
1125 | if (dir) { | |
1126 | g_dir_close(dir); | |
1127 | } | |
1128 | ||
1129 | if (error) { | |
1130 | g_error_free(error); | |
1131 | } | |
1132 | ||
1133 | return ret; | |
1134 | } | |
1135 | ||
f280892e | 1136 | static |
55314f2a | 1137 | GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) { |
1a9f7075 | 1138 | GList *trace_names = NULL; |
1a9f7075 | 1139 | GList *node; |
4bd72b60 PP |
1140 | const char *last_sep; |
1141 | size_t base_dist; | |
1a9f7075 PP |
1142 | |
1143 | /* | |
4bd72b60 PP |
1144 | * At this point we know that all the trace paths are |
1145 | * normalized, and so is the base path. This means that | |
1146 | * they are absolute and they don't end with a separator. | |
1147 | * We can simply find the location of the last separator | |
1148 | * in the base path, which gives us the name of the actual | |
1149 | * directory to look into, and use this location as the | |
1150 | * start of each trace name within each trace path. | |
1151 | * | |
1152 | * For example: | |
1153 | * | |
1154 | * Base path: /home/user/my-traces/some-trace | |
1155 | * Trace paths: | |
1156 | * - /home/user/my-traces/some-trace/host1/trace1 | |
1157 | * - /home/user/my-traces/some-trace/host1/trace2 | |
1158 | * - /home/user/my-traces/some-trace/host2/trace | |
1159 | * - /home/user/my-traces/some-trace/other-trace | |
1160 | * | |
1161 | * In this case the trace names are: | |
1162 | * | |
1163 | * - some-trace/host1/trace1 | |
1164 | * - some-trace/host1/trace2 | |
1165 | * - some-trace/host2/trace | |
1166 | * - some-trace/other-trace | |
1a9f7075 | 1167 | */ |
4bd72b60 | 1168 | last_sep = strrchr(base_path, G_DIR_SEPARATOR); |
1a9f7075 | 1169 | |
4bd72b60 | 1170 | /* We know there's at least one separator */ |
f6ccaed9 | 1171 | BT_ASSERT(last_sep); |
1a9f7075 | 1172 | |
4bd72b60 PP |
1173 | /* Distance to base */ |
1174 | base_dist = last_sep - base_path + 1; | |
1a9f7075 PP |
1175 | |
1176 | /* Create the trace names */ | |
1177 | for (node = trace_paths; node; node = g_list_next(node)) { | |
1178 | GString *trace_name = g_string_new(NULL); | |
1179 | GString *trace_path = node->data; | |
1180 | ||
f6ccaed9 | 1181 | BT_ASSERT(trace_name); |
4bd72b60 | 1182 | g_string_assign(trace_name, &trace_path->str[base_dist]); |
1a9f7075 PP |
1183 | trace_names = g_list_append(trace_names, trace_name); |
1184 | } | |
1185 | ||
1186 | return trace_names; | |
1187 | } | |
1188 | ||
f280892e SM |
1189 | /* Helper for ctf_fs_component_create_ctf_fs_traces, to handle a single path/root. */ |
1190 | ||
1a9f7075 | 1191 | static |
f280892e | 1192 | int ctf_fs_component_create_ctf_fs_traces_one_root(bt_self_component_source *self_comp, |
41693723 | 1193 | struct ctf_fs_component *ctf_fs, |
1a9f7075 PP |
1194 | const char *path_param) |
1195 | { | |
1196 | struct ctf_fs_trace *ctf_fs_trace = NULL; | |
1197 | int ret = 0; | |
4bd72b60 | 1198 | GString *norm_path = NULL; |
1a9f7075 PP |
1199 | GList *trace_paths = NULL; |
1200 | GList *trace_names = NULL; | |
1201 | GList *tp_node; | |
1202 | GList *tn_node; | |
1203 | ||
4bd72b60 PP |
1204 | norm_path = bt_common_normalize_path(path_param, NULL); |
1205 | if (!norm_path) { | |
55314f2a | 1206 | BT_LOGE("Failed to normalize path: `%s`.", |
4bd72b60 PP |
1207 | path_param); |
1208 | goto error; | |
1209 | } | |
1210 | ||
55314f2a | 1211 | ret = ctf_fs_find_traces(&trace_paths, norm_path->str); |
1a9f7075 PP |
1212 | if (ret) { |
1213 | goto error; | |
1214 | } | |
1215 | ||
1216 | if (!trace_paths) { | |
55314f2a | 1217 | BT_LOGE("No CTF traces recursively found in `%s`.", |
1a9f7075 PP |
1218 | path_param); |
1219 | goto error; | |
1220 | } | |
1221 | ||
55314f2a | 1222 | trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str); |
1a9f7075 | 1223 | if (!trace_names) { |
55314f2a | 1224 | BT_LOGE("Cannot create trace names from trace paths."); |
1a9f7075 PP |
1225 | goto error; |
1226 | } | |
1227 | ||
1228 | for (tp_node = trace_paths, tn_node = trace_names; tp_node; | |
1229 | tp_node = g_list_next(tp_node), | |
1230 | tn_node = g_list_next(tn_node)) { | |
1231 | GString *trace_path = tp_node->data; | |
1232 | GString *trace_name = tn_node->data; | |
1233 | ||
41693723 PP |
1234 | ctf_fs_trace = ctf_fs_trace_create(self_comp, |
1235 | trace_path->str, trace_name->str, | |
1236 | &ctf_fs->metadata_config); | |
1a9f7075 | 1237 | if (!ctf_fs_trace) { |
55314f2a | 1238 | BT_LOGE("Cannot create trace for `%s`.", |
1a9f7075 PP |
1239 | trace_path->str); |
1240 | goto error; | |
1241 | } | |
52c5fe74 PP |
1242 | |
1243 | g_ptr_array_add(ctf_fs->traces, ctf_fs_trace); | |
1244 | ctf_fs_trace = NULL; | |
1a9f7075 PP |
1245 | } |
1246 | ||
1a9f7075 PP |
1247 | goto end; |
1248 | ||
1249 | error: | |
1250 | ret = -1; | |
1251 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1252 | ||
1253 | end: | |
1254 | for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) { | |
1255 | if (tp_node->data) { | |
1256 | g_string_free(tp_node->data, TRUE); | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) { | |
1261 | if (tn_node->data) { | |
1262 | g_string_free(tn_node->data, TRUE); | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | if (trace_paths) { | |
1267 | g_list_free(trace_paths); | |
1268 | } | |
1269 | ||
1270 | if (trace_names) { | |
1271 | g_list_free(trace_names); | |
1272 | } | |
1273 | ||
4bd72b60 PP |
1274 | if (norm_path) { |
1275 | g_string_free(norm_path, TRUE); | |
1276 | } | |
1277 | ||
1a9f7075 PP |
1278 | return ret; |
1279 | } | |
1280 | ||
f280892e SM |
1281 | int ctf_fs_component_create_ctf_fs_traces(bt_self_component_source *self_comp, |
1282 | struct ctf_fs_component *ctf_fs, | |
1283 | const bt_value *paths_value) | |
1284 | { | |
1285 | int ret = 0; | |
1286 | uint64_t i; | |
1287 | ||
1288 | for (i = 0; i < bt_value_array_get_size(paths_value); i++) { | |
1289 | const bt_value *path_value = bt_value_array_borrow_element_by_index_const(paths_value, i); | |
1290 | const char *path = bt_value_string_get(path_value); | |
1291 | ||
1292 | ret = ctf_fs_component_create_ctf_fs_traces_one_root(self_comp, ctf_fs, path); | |
1293 | if (ret) { | |
1294 | goto end; | |
1295 | } | |
1296 | } | |
1297 | ||
1298 | end: | |
1299 | return ret; | |
1300 | } | |
1301 | ||
1302 | /* Create the IR stream objects for ctf_fs_trace. */ | |
1303 | ||
1304 | static | |
1305 | int create_streams_for_trace(struct ctf_fs_trace *ctf_fs_trace) | |
1306 | { | |
1307 | int ret; | |
1308 | GString *name = NULL; | |
1309 | guint i; | |
1310 | ||
1311 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
1312 | struct ctf_fs_ds_file_group *ds_file_group = | |
1313 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
1314 | name = get_stream_instance_unique_name(ds_file_group); | |
1315 | ||
1316 | if (!name) { | |
1317 | goto error; | |
1318 | } | |
1319 | ||
1320 | if (ds_file_group->sc->ir_sc) { | |
1321 | BT_ASSERT(ctf_fs_trace->trace); | |
1322 | ||
1323 | if (ds_file_group->stream_id == UINT64_C(-1)) { | |
1324 | /* No stream ID: use 0 */ | |
1325 | ds_file_group->stream = bt_stream_create_with_id( | |
1326 | ds_file_group->sc->ir_sc, | |
1327 | ctf_fs_trace->trace, | |
1328 | ctf_fs_trace->next_stream_id); | |
1329 | ctf_fs_trace->next_stream_id++; | |
1330 | } else { | |
1331 | /* Specific stream ID */ | |
1332 | ds_file_group->stream = bt_stream_create_with_id( | |
1333 | ds_file_group->sc->ir_sc, | |
1334 | ctf_fs_trace->trace, | |
1335 | (uint64_t) ds_file_group->stream_id); | |
1336 | } | |
1337 | } else { | |
1338 | ds_file_group->stream = NULL; | |
1339 | } | |
1340 | ||
1341 | if (!ds_file_group->stream) { | |
1342 | BT_LOGE("Cannot create stream for DS file group: " | |
1343 | "addr=%p, stream-name=\"%s\"", | |
1344 | ds_file_group, name->str); | |
1345 | goto error; | |
1346 | } | |
1347 | ||
1348 | ret = bt_stream_set_name(ds_file_group->stream, | |
1349 | name->str); | |
1350 | if (ret) { | |
1351 | BT_LOGE("Cannot set stream's name: " | |
1352 | "addr=%p, stream-name=\"%s\"", | |
1353 | ds_file_group->stream, name->str); | |
1354 | goto error; | |
1355 | } | |
1356 | ||
1357 | g_string_free(name, TRUE); | |
1358 | name = NULL; | |
1359 | } | |
1360 | ||
1361 | ret = 0; | |
1362 | goto end; | |
1363 | ||
1364 | error: | |
1365 | ret = -1; | |
1366 | ||
1367 | end: | |
1368 | ||
1369 | if (name) { | |
1370 | g_string_free(name, TRUE); | |
1371 | } | |
1372 | return ret; | |
1373 | } | |
1374 | ||
1375 | bool validate_paths_parameter(const bt_value *paths) | |
1376 | { | |
1377 | bool ret; | |
1378 | bt_value_type type; | |
1379 | uint64_t i; | |
1380 | ||
1381 | if (!paths) { | |
1382 | BT_LOGE("missing \"paths\" parameter"); | |
1383 | goto error; | |
1384 | } | |
1385 | ||
1386 | type = bt_value_get_type(paths); | |
1387 | if (type != BT_VALUE_TYPE_ARRAY) { | |
1388 | BT_LOGE("`paths` parameter: expecting array value: type=%s", | |
1389 | bt_common_value_type_string(type)); | |
1390 | goto error; | |
1391 | } | |
1392 | ||
1393 | for (i = 0; i < bt_value_array_get_size(paths); i++) { | |
1394 | const bt_value *elem; | |
1395 | ||
1396 | elem = bt_value_array_borrow_element_by_index_const(paths, i); | |
1397 | type = bt_value_get_type(elem); | |
1398 | if (type != BT_VALUE_TYPE_STRING) { | |
1399 | BT_LOGE("`paths` parameter: expecting string value: index=%" PRIu64 ", type=%s", | |
1400 | i, bt_common_value_type_string(type)); | |
1401 | goto error; | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | ret = true; | |
1406 | goto end; | |
1407 | ||
1408 | error: | |
1409 | ret = false; | |
1410 | ||
1411 | end: | |
1412 | return ret; | |
1413 | } | |
1414 | ||
5b29e799 | 1415 | static |
d94d92ac | 1416 | struct ctf_fs_component *ctf_fs_create( |
b19ff26f PP |
1417 | bt_self_component_source *self_comp, |
1418 | const bt_value *params) | |
56a1cced | 1419 | { |
f280892e | 1420 | struct ctf_fs_component *ctf_fs = NULL; |
b19ff26f | 1421 | const bt_value *value = NULL; |
f280892e SM |
1422 | guint i; |
1423 | const bt_value *paths_value; | |
56a1cced | 1424 | |
f280892e SM |
1425 | paths_value = bt_value_map_borrow_entry_value_const(params, "paths"); |
1426 | if (!validate_paths_parameter(paths_value)) { | |
1427 | goto error; | |
1428 | } | |
1429 | ||
1430 | ctf_fs = ctf_fs_component_create(); | |
56a1cced | 1431 | if (!ctf_fs) { |
f280892e | 1432 | goto error; |
56a1cced JG |
1433 | } |
1434 | ||
d94d92ac | 1435 | bt_self_component_set_data( |
707b7d35 | 1436 | bt_self_component_source_as_self_component(self_comp), |
d94d92ac | 1437 | ctf_fs); |
1a9f7075 | 1438 | |
4f1f88a6 PP |
1439 | /* |
1440 | * We don't need to get a new reference here because as long as | |
1441 | * our private ctf_fs_component object exists, the containing | |
1442 | * private component should also exist. | |
1443 | */ | |
d94d92ac | 1444 | ctf_fs->self_comp = self_comp; |
56a1cced | 1445 | |
05e21286 PP |
1446 | value = bt_value_map_borrow_entry_value_const(params, |
1447 | "clock-class-offset-s"); | |
92540773 | 1448 | if (value) { |
a2a54545 PP |
1449 | if (!bt_value_is_integer(value)) { |
1450 | BT_LOGE("clock-class-offset-s should be an integer"); | |
1451 | goto error; | |
1452 | } | |
601b0d3c | 1453 | ctf_fs->metadata_config.clock_class_offset_s = bt_value_integer_get(value); |
a2a54545 | 1454 | } |
92540773 | 1455 | |
05e21286 PP |
1456 | value = bt_value_map_borrow_entry_value_const(params, |
1457 | "clock-class-offset-ns"); | |
a2a54545 | 1458 | if (value) { |
92540773 | 1459 | if (!bt_value_is_integer(value)) { |
a2a54545 | 1460 | BT_LOGE("clock-class-offset-ns should be an integer"); |
92540773 JD |
1461 | goto error; |
1462 | } | |
601b0d3c | 1463 | ctf_fs->metadata_config.clock_class_offset_ns = bt_value_integer_get(value); |
92540773 JD |
1464 | } |
1465 | ||
f280892e | 1466 | if (ctf_fs_component_create_ctf_fs_traces(self_comp, ctf_fs, paths_value)) { |
4f1f88a6 PP |
1467 | goto error; |
1468 | } | |
1469 | ||
f280892e SM |
1470 | for (i = 0; i < ctf_fs->traces->len; i++) { |
1471 | struct ctf_fs_trace *trace = g_ptr_array_index(ctf_fs->traces, i); | |
599faa1c | 1472 | |
f280892e SM |
1473 | if (create_streams_for_trace(trace)) { |
1474 | goto error; | |
1475 | } | |
1476 | ||
1477 | if (create_ports_for_trace(ctf_fs, trace)) { | |
1478 | goto error; | |
1479 | } | |
4f1f88a6 PP |
1480 | } |
1481 | ||
1ef09eb5 JG |
1482 | goto end; |
1483 | ||
56a1cced | 1484 | error: |
1a9f7075 | 1485 | ctf_fs_destroy(ctf_fs); |
e7a4393b | 1486 | ctf_fs = NULL; |
d94d92ac | 1487 | bt_self_component_set_data( |
707b7d35 | 1488 | bt_self_component_source_as_self_component(self_comp), |
d94d92ac | 1489 | NULL); |
1a9f7075 | 1490 | |
1ef09eb5 | 1491 | end: |
56a1cced JG |
1492 | return ctf_fs; |
1493 | } | |
1494 | ||
ea0b4b9e | 1495 | BT_HIDDEN |
4cdfc5e8 | 1496 | bt_self_component_status ctf_fs_init( |
b19ff26f PP |
1497 | bt_self_component_source *self_comp, |
1498 | const bt_value *params, UNUSED_VAR void *init_method_data) | |
ea0b4b9e JG |
1499 | { |
1500 | struct ctf_fs_component *ctf_fs; | |
4cdfc5e8 | 1501 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
ea0b4b9e | 1502 | |
d94d92ac | 1503 | ctf_fs = ctf_fs_create(self_comp, params); |
ea0b4b9e | 1504 | if (!ctf_fs) { |
d94d92ac | 1505 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
ea0b4b9e | 1506 | } |
4c1456f0 | 1507 | |
ea0b4b9e JG |
1508 | return ret; |
1509 | } | |
33f93973 PP |
1510 | |
1511 | BT_HIDDEN | |
4cdfc5e8 | 1512 | bt_query_status ctf_fs_query( |
b19ff26f PP |
1513 | bt_self_component_class_source *comp_class, |
1514 | const bt_query_executor *query_exec, | |
1515 | const char *object, const bt_value *params, | |
1516 | const bt_value **result) | |
33f93973 | 1517 | { |
4cdfc5e8 | 1518 | bt_query_status status = BT_QUERY_STATUS_OK; |
33f93973 | 1519 | |
04c0ba87 | 1520 | if (!strcmp(object, "metadata-info")) { |
d94d92ac | 1521 | status = metadata_info_query(comp_class, params, result); |
97ade20b | 1522 | } else if (!strcmp(object, "trace-info")) { |
d94d92ac | 1523 | status = trace_info_query(comp_class, params, result); |
33f93973 | 1524 | } else { |
55314f2a | 1525 | BT_LOGE("Unknown query object `%s`", object); |
d94d92ac | 1526 | status = BT_QUERY_STATUS_INVALID_OBJECT; |
04c0ba87 | 1527 | goto end; |
33f93973 | 1528 | } |
33f93973 | 1529 | end: |
d94d92ac | 1530 | return status; |
33f93973 | 1531 | } |