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> |
5b29e799 | 29 | #include <babeltrace/ctf-ir/packet.h> |
ac0c6bdd | 30 | #include <babeltrace/ctf-ir/clock-class.h> |
94cf822e PP |
31 | #include <babeltrace/ctf-ir/stream.h> |
32 | #include <babeltrace/ctf-ir/fields.h> | |
4f1f88a6 | 33 | #include <babeltrace/graph/private-port.h> |
b2e0c907 | 34 | #include <babeltrace/graph/private-component.h> |
4f1f88a6 PP |
35 | #include <babeltrace/graph/private-component-source.h> |
36 | #include <babeltrace/graph/private-notification-iterator.h> | |
b2e0c907 PP |
37 | #include <babeltrace/graph/component.h> |
38 | #include <babeltrace/graph/notification-iterator.h> | |
599faa1c | 39 | #include <babeltrace/graph/clock-class-priority-map.h> |
7d61fa8e | 40 | #include <plugins-common.h> |
ea0b4b9e JG |
41 | #include <glib.h> |
42 | #include <assert.h> | |
94cf822e | 43 | #include <inttypes.h> |
c55a9f58 | 44 | #include <stdbool.h> |
56a1cced | 45 | #include "fs.h" |
413bc2c4 | 46 | #include "metadata.h" |
94cf822e | 47 | #include "data-stream-file.h" |
e7a4393b | 48 | #include "file.h" |
1e649dff | 49 | #include "../common/metadata/decoder.h" |
6de92955 | 50 | #include "../common/notif-iter/notif-iter.h" |
04c0ba87 | 51 | #include "query.h" |
e7a4393b | 52 | |
55314f2a JG |
53 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC" |
54 | #include "logging.h" | |
ea0b4b9e | 55 | |
94cf822e PP |
56 | static |
57 | int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data *notif_iter_data) | |
58 | { | |
59 | struct ctf_fs_ds_file_info *ds_file_info; | |
60 | int ret = 0; | |
61 | ||
62 | assert(notif_iter_data->ds_file_info_index < | |
63 | notif_iter_data->ds_file_group->ds_file_infos->len); | |
64 | ds_file_info = g_ptr_array_index( | |
65 | notif_iter_data->ds_file_group->ds_file_infos, | |
66 | notif_iter_data->ds_file_info_index); | |
67 | ||
68 | ctf_fs_ds_file_destroy(notif_iter_data->ds_file); | |
69 | notif_iter_data->ds_file = ctf_fs_ds_file_create( | |
70 | notif_iter_data->ds_file_group->ctf_fs_trace, | |
6de92955 | 71 | notif_iter_data->notif_iter, |
94cf822e | 72 | notif_iter_data->ds_file_group->stream, |
97ade20b | 73 | ds_file_info->path->str); |
94cf822e PP |
74 | if (!notif_iter_data->ds_file) { |
75 | ret = -1; | |
76 | } | |
77 | ||
78 | return ret; | |
79 | } | |
80 | ||
81 | static | |
82 | void ctf_fs_notif_iter_data_destroy( | |
83 | struct ctf_fs_notif_iter_data *notif_iter_data) | |
84 | { | |
85 | if (!notif_iter_data) { | |
86 | return; | |
87 | } | |
88 | ||
89 | ctf_fs_ds_file_destroy(notif_iter_data->ds_file); | |
6de92955 PP |
90 | |
91 | if (notif_iter_data->notif_iter) { | |
92 | bt_ctf_notif_iter_destroy(notif_iter_data->notif_iter); | |
93 | } | |
94 | ||
94cf822e PP |
95 | g_free(notif_iter_data); |
96 | } | |
97 | ||
41a2b7ae | 98 | struct bt_notification_iterator_next_return ctf_fs_iterator_next( |
4f1f88a6 | 99 | struct bt_private_notification_iterator *iterator) |
ea0b4b9e | 100 | { |
94cf822e PP |
101 | struct bt_notification_iterator_next_return next_ret; |
102 | struct ctf_fs_notif_iter_data *notif_iter_data = | |
4f1f88a6 | 103 | bt_private_notification_iterator_get_user_data(iterator); |
94cf822e PP |
104 | int ret; |
105 | ||
106 | assert(notif_iter_data->ds_file); | |
107 | next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file); | |
108 | if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_END) { | |
109 | assert(!next_ret.notification); | |
110 | notif_iter_data->ds_file_info_index++; | |
111 | ||
112 | if (notif_iter_data->ds_file_info_index == | |
113 | notif_iter_data->ds_file_group->ds_file_infos->len) { | |
114 | /* | |
115 | * No more stream files to read: we reached the | |
116 | * real end. | |
117 | */ | |
118 | goto end; | |
119 | } | |
120 | ||
121 | /* | |
122 | * Open and start reading the next stream file within | |
123 | * our stream file group. | |
124 | */ | |
125 | ret = notif_iter_data_set_current_ds_file(notif_iter_data); | |
126 | if (ret) { | |
127 | next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
128 | goto end; | |
129 | } | |
130 | ||
131 | next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file); | |
132 | ||
133 | /* | |
134 | * We should not get BT_NOTIFICATION_ITERATOR_STATUS_END | |
135 | * with a brand new stream file because empty stream | |
136 | * files are not even part of stream file groups, which | |
137 | * means we're sure to get at least one pair of "packet | |
138 | * begin" and "packet end" notifications in the case of | |
139 | * a single, empty packet. | |
140 | */ | |
141 | assert(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_END); | |
142 | } | |
d01e0f33 | 143 | |
94cf822e PP |
144 | end: |
145 | return next_ret; | |
ea0b4b9e | 146 | } |
bfd20a42 | 147 | |
4f1f88a6 | 148 | void ctf_fs_iterator_finalize(struct bt_private_notification_iterator *it) |
760051fa | 149 | { |
94cf822e | 150 | void *notif_iter_data = |
4f1f88a6 | 151 | bt_private_notification_iterator_get_user_data(it); |
760051fa | 152 | |
94cf822e | 153 | ctf_fs_notif_iter_data_destroy(notif_iter_data); |
760051fa JG |
154 | } |
155 | ||
4f1f88a6 PP |
156 | enum bt_notification_iterator_status ctf_fs_iterator_init( |
157 | struct bt_private_notification_iterator *it, | |
158 | struct bt_private_port *port) | |
4c1456f0 | 159 | { |
4f1f88a6 | 160 | struct ctf_fs_port_data *port_data; |
94cf822e | 161 | struct ctf_fs_notif_iter_data *notif_iter_data = NULL; |
4f1f88a6 PP |
162 | enum bt_notification_iterator_status ret = |
163 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
94cf822e | 164 | int iret; |
760051fa | 165 | |
4f1f88a6 PP |
166 | port_data = bt_private_port_get_user_data(port); |
167 | if (!port_data) { | |
fe8ad2b6 | 168 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID; |
4f1f88a6 PP |
169 | goto error; |
170 | } | |
5b29e799 | 171 | |
94cf822e PP |
172 | notif_iter_data = g_new0(struct ctf_fs_notif_iter_data, 1); |
173 | if (!notif_iter_data) { | |
174 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
175 | goto error; | |
176 | } | |
177 | ||
6de92955 PP |
178 | notif_iter_data->notif_iter = bt_ctf_notif_iter_create( |
179 | port_data->ds_file_group->ctf_fs_trace->metadata->trace, | |
180 | bt_common_get_page_size() * 8, | |
181 | ctf_fs_ds_file_medops, NULL); | |
182 | if (!notif_iter_data->notif_iter) { | |
183 | BT_LOGE_STR("Cannot create a CTF notification iterator."); | |
184 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
185 | goto error; | |
186 | } | |
187 | ||
94cf822e PP |
188 | notif_iter_data->ds_file_group = port_data->ds_file_group; |
189 | iret = notif_iter_data_set_current_ds_file(notif_iter_data); | |
190 | if (iret) { | |
191 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
4f1f88a6 | 192 | goto error; |
760051fa JG |
193 | } |
194 | ||
94cf822e | 195 | ret = bt_private_notification_iterator_set_user_data(it, notif_iter_data); |
4f1f88a6 PP |
196 | if (ret) { |
197 | goto error; | |
760051fa JG |
198 | } |
199 | ||
94cf822e | 200 | notif_iter_data = NULL; |
4f1f88a6 | 201 | goto end; |
5b29e799 | 202 | |
4f1f88a6 PP |
203 | error: |
204 | (void) bt_private_notification_iterator_set_user_data(it, NULL); | |
205 | ||
206 | if (ret == BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
207 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
760051fa | 208 | } |
5b29e799 | 209 | |
760051fa | 210 | end: |
94cf822e | 211 | ctf_fs_notif_iter_data_destroy(notif_iter_data); |
760051fa JG |
212 | return ret; |
213 | } | |
214 | ||
760051fa | 215 | static |
1a9f7075 | 216 | void ctf_fs_destroy(struct ctf_fs_component *ctf_fs) |
760051fa | 217 | { |
4f1f88a6 | 218 | if (!ctf_fs) { |
8fa760ba JG |
219 | return; |
220 | } | |
4f1f88a6 | 221 | |
1a9f7075 PP |
222 | if (ctf_fs->traces) { |
223 | g_ptr_array_free(ctf_fs->traces, TRUE); | |
56a1cced | 224 | } |
4f1f88a6 PP |
225 | |
226 | if (ctf_fs->port_data) { | |
227 | g_ptr_array_free(ctf_fs->port_data, TRUE); | |
c14d7e26 | 228 | } |
760051fa | 229 | |
1a9f7075 PP |
230 | g_free(ctf_fs); |
231 | } | |
232 | ||
97ade20b JG |
233 | BT_HIDDEN |
234 | void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace) | |
1a9f7075 | 235 | { |
1a9f7075 PP |
236 | if (!ctf_fs_trace) { |
237 | return; | |
238 | } | |
239 | ||
94cf822e PP |
240 | if (ctf_fs_trace->ds_file_groups) { |
241 | g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE); | |
242 | } | |
243 | ||
1a9f7075 PP |
244 | if (ctf_fs_trace->path) { |
245 | g_string_free(ctf_fs_trace->path, TRUE); | |
4f1f88a6 | 246 | } |
760051fa | 247 | |
1a9f7075 PP |
248 | if (ctf_fs_trace->name) { |
249 | g_string_free(ctf_fs_trace->name, TRUE); | |
250 | } | |
251 | ||
252 | if (ctf_fs_trace->metadata) { | |
253 | ctf_fs_metadata_fini(ctf_fs_trace->metadata); | |
254 | g_free(ctf_fs_trace->metadata); | |
255 | } | |
256 | ||
257 | bt_put(ctf_fs_trace->cc_prio_map); | |
258 | g_free(ctf_fs_trace); | |
4c1456f0 JG |
259 | } |
260 | ||
97ade20b JG |
261 | static |
262 | void ctf_fs_trace_destroy_notifier(void *data) | |
263 | { | |
264 | struct ctf_fs_trace *trace = data; | |
265 | ctf_fs_trace_destroy(trace); | |
266 | } | |
267 | ||
4f1f88a6 | 268 | void ctf_fs_finalize(struct bt_private_component *component) |
a4792757 | 269 | { |
4f1f88a6 PP |
270 | void *data = bt_private_component_get_user_data(component); |
271 | ||
1a9f7075 | 272 | ctf_fs_destroy(data); |
a4792757 JG |
273 | } |
274 | ||
e7a4393b | 275 | static |
4f1f88a6 PP |
276 | void port_data_destroy(void *data) { |
277 | struct ctf_fs_port_data *port_data = data; | |
278 | ||
279 | if (!port_data) { | |
280 | return; | |
281 | } | |
282 | ||
4f1f88a6 | 283 | g_free(port_data); |
5b29e799 JG |
284 | } |
285 | ||
547eacf1 PP |
286 | static |
287 | GString *get_stream_instance_unique_name( | |
288 | struct ctf_fs_ds_file_group *ds_file_group) | |
289 | { | |
290 | GString *name; | |
291 | struct ctf_fs_ds_file_info *ds_file_info; | |
292 | ||
293 | name = g_string_new(NULL); | |
294 | if (!name) { | |
295 | goto end; | |
296 | } | |
297 | ||
298 | /* | |
299 | * If there's more than one stream file in the stream file | |
300 | * group, the first (earliest) stream file's path is used as | |
301 | * the stream's unique name. | |
302 | */ | |
303 | assert(ds_file_group->ds_file_infos->len > 0); | |
304 | ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0); | |
305 | g_string_assign(name, ds_file_info->path->str); | |
306 | ||
307 | end: | |
308 | return name; | |
309 | } | |
310 | ||
5b29e799 | 311 | static |
55314f2a JG |
312 | int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, |
313 | struct ctf_fs_trace *ctf_fs_trace, | |
94cf822e | 314 | struct ctf_fs_ds_file_group *ds_file_group) |
5b29e799 | 315 | { |
4f1f88a6 | 316 | int ret = 0; |
4f1f88a6 PP |
317 | struct ctf_fs_port_data *port_data = NULL; |
318 | GString *port_name = NULL; | |
319 | ||
547eacf1 | 320 | port_name = get_stream_instance_unique_name(ds_file_group); |
4f1f88a6 PP |
321 | if (!port_name) { |
322 | goto error; | |
323 | } | |
324 | ||
55314f2a | 325 | BT_LOGD("Creating one port named `%s`", port_name->str); |
4f1f88a6 PP |
326 | |
327 | /* Create output port for this file */ | |
4f1f88a6 PP |
328 | port_data = g_new0(struct ctf_fs_port_data, 1); |
329 | if (!port_data) { | |
330 | goto error; | |
331 | } | |
332 | ||
94cf822e | 333 | port_data->ds_file_group = ds_file_group; |
147337a3 PP |
334 | ret = bt_private_component_source_add_output_private_port( |
335 | ctf_fs->priv_comp, port_name->str, port_data, NULL); | |
336 | if (ret) { | |
4f1f88a6 PP |
337 | goto error; |
338 | } | |
339 | ||
340 | g_ptr_array_add(ctf_fs->port_data, port_data); | |
341 | port_data = NULL; | |
342 | goto end; | |
343 | ||
344 | error: | |
345 | ret = -1; | |
346 | ||
347 | end: | |
348 | if (port_name) { | |
349 | g_string_free(port_name, TRUE); | |
350 | } | |
351 | ||
4f1f88a6 PP |
352 | port_data_destroy(port_data); |
353 | return ret; | |
5b29e799 JG |
354 | } |
355 | ||
356 | static | |
55314f2a JG |
357 | int create_ports_for_trace(struct ctf_fs_component *ctf_fs, |
358 | struct ctf_fs_trace *ctf_fs_trace) | |
94cf822e PP |
359 | { |
360 | int ret = 0; | |
94cf822e PP |
361 | size_t i; |
362 | ||
363 | /* Create one output port for each stream file group */ | |
364 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
365 | struct ctf_fs_ds_file_group *ds_file_group = | |
366 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
367 | ||
55314f2a JG |
368 | ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace, |
369 | ds_file_group); | |
94cf822e | 370 | if (ret) { |
55314f2a | 371 | BT_LOGE("Cannot create output port."); |
94cf822e PP |
372 | goto end; |
373 | } | |
374 | } | |
375 | ||
376 | end: | |
377 | return ret; | |
378 | } | |
379 | ||
380 | uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace *ctf_fs_trace, | |
381 | struct bt_ctf_field *packet_header_field) | |
382 | { | |
383 | struct bt_ctf_field *stream_instance_id_field = NULL; | |
384 | uint64_t stream_instance_id = -1ULL; | |
385 | int ret; | |
386 | ||
387 | if (!packet_header_field) { | |
388 | goto end; | |
389 | } | |
390 | ||
391 | stream_instance_id_field = bt_ctf_field_structure_get_field_by_name( | |
392 | packet_header_field, "stream_instance_id"); | |
393 | if (!stream_instance_id_field) { | |
394 | goto end; | |
395 | } | |
396 | ||
397 | ret = bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field, | |
398 | &stream_instance_id); | |
399 | if (ret) { | |
400 | stream_instance_id = -1ULL; | |
401 | goto end; | |
402 | } | |
403 | ||
404 | end: | |
405 | bt_put(stream_instance_id_field); | |
406 | return stream_instance_id; | |
407 | } | |
408 | ||
409 | struct bt_ctf_stream_class *stream_class_from_packet_header( | |
410 | struct ctf_fs_trace *ctf_fs_trace, | |
411 | struct bt_ctf_field *packet_header_field) | |
412 | { | |
413 | struct bt_ctf_field *stream_id_field = NULL; | |
414 | struct bt_ctf_stream_class *stream_class = NULL; | |
415 | uint64_t stream_id = -1ULL; | |
416 | int ret; | |
417 | ||
418 | if (!packet_header_field) { | |
419 | goto single_stream_class; | |
420 | } | |
421 | ||
422 | stream_id_field = bt_ctf_field_structure_get_field_by_name( | |
423 | packet_header_field, "stream_id"); | |
424 | if (!stream_id_field) { | |
89b08333 | 425 | goto single_stream_class; |
94cf822e PP |
426 | } |
427 | ||
428 | ret = bt_ctf_field_unsigned_integer_get_value(stream_id_field, | |
429 | &stream_id); | |
430 | if (ret) { | |
431 | stream_id = -1ULL; | |
432 | } | |
433 | ||
434 | if (stream_id == -1ULL) { | |
435 | single_stream_class: | |
436 | /* Single stream class */ | |
437 | if (bt_ctf_trace_get_stream_class_count( | |
438 | ctf_fs_trace->metadata->trace) == 0) { | |
439 | goto end; | |
440 | } | |
441 | ||
442 | stream_class = bt_ctf_trace_get_stream_class_by_index( | |
443 | ctf_fs_trace->metadata->trace, 0); | |
444 | } else { | |
445 | stream_class = bt_ctf_trace_get_stream_class_by_id( | |
446 | ctf_fs_trace->metadata->trace, stream_id); | |
447 | } | |
448 | ||
449 | end: | |
450 | bt_put(stream_id_field); | |
451 | return stream_class; | |
452 | } | |
453 | ||
454 | uint64_t get_packet_context_timestamp_begin_ns( | |
455 | struct ctf_fs_trace *ctf_fs_trace, | |
456 | struct bt_ctf_field *packet_context_field) | |
457 | { | |
458 | int ret; | |
459 | struct bt_ctf_field *timestamp_begin_field = NULL; | |
460 | struct bt_ctf_field_type *timestamp_begin_ft = NULL; | |
461 | uint64_t timestamp_begin_raw_value = -1ULL; | |
462 | uint64_t timestamp_begin_ns = -1ULL; | |
463 | int64_t timestamp_begin_ns_signed; | |
464 | struct bt_ctf_clock_class *timestamp_begin_clock_class = NULL; | |
465 | struct bt_ctf_clock_value *clock_value = NULL; | |
466 | ||
467 | if (!packet_context_field) { | |
468 | goto end; | |
469 | } | |
470 | ||
471 | timestamp_begin_field = bt_ctf_field_structure_get_field_by_name( | |
472 | packet_context_field, "timestamp_begin"); | |
473 | if (!timestamp_begin_field) { | |
474 | goto end; | |
475 | } | |
476 | ||
477 | timestamp_begin_ft = bt_ctf_field_get_type(timestamp_begin_field); | |
478 | assert(timestamp_begin_ft); | |
479 | timestamp_begin_clock_class = | |
480 | bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft); | |
481 | if (!timestamp_begin_clock_class) { | |
482 | goto end; | |
483 | } | |
484 | ||
485 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field, | |
486 | ×tamp_begin_raw_value); | |
487 | if (ret) { | |
488 | goto end; | |
489 | } | |
490 | ||
491 | clock_value = bt_ctf_clock_value_create(timestamp_begin_clock_class, | |
492 | timestamp_begin_raw_value); | |
493 | if (!clock_value) { | |
494 | goto end; | |
495 | } | |
496 | ||
497 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, | |
498 | ×tamp_begin_ns_signed); | |
499 | if (ret) { | |
500 | goto end; | |
501 | } | |
502 | ||
503 | timestamp_begin_ns = (uint64_t) timestamp_begin_ns_signed; | |
504 | ||
505 | end: | |
506 | bt_put(timestamp_begin_field); | |
507 | bt_put(timestamp_begin_ft); | |
508 | bt_put(timestamp_begin_clock_class); | |
509 | bt_put(clock_value); | |
510 | return timestamp_begin_ns; | |
511 | } | |
512 | ||
513 | static | |
514 | void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info) | |
515 | { | |
516 | if (!ds_file_info) { | |
517 | return; | |
518 | } | |
519 | ||
520 | if (ds_file_info->path) { | |
521 | g_string_free(ds_file_info->path, TRUE); | |
522 | } | |
523 | ||
97ade20b | 524 | ctf_fs_ds_index_destroy(ds_file_info->index); |
94cf822e PP |
525 | g_free(ds_file_info); |
526 | } | |
527 | ||
528 | static | |
529 | struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path, | |
97ade20b | 530 | uint64_t begin_ns, struct ctf_fs_ds_index *index) |
94cf822e PP |
531 | { |
532 | struct ctf_fs_ds_file_info *ds_file_info; | |
533 | ||
534 | ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1); | |
535 | if (!ds_file_info) { | |
536 | goto end; | |
537 | } | |
538 | ||
539 | ds_file_info->path = g_string_new(path); | |
540 | if (!ds_file_info->path) { | |
541 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
542 | ds_file_info = NULL; | |
543 | goto end; | |
544 | } | |
545 | ||
546 | ds_file_info->begin_ns = begin_ns; | |
97ade20b JG |
547 | ds_file_info->index = index; |
548 | index = NULL; | |
94cf822e PP |
549 | |
550 | end: | |
97ade20b | 551 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
552 | return ds_file_info; |
553 | } | |
554 | ||
555 | static | |
556 | void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group) | |
557 | { | |
558 | if (!ds_file_group) { | |
559 | return; | |
560 | } | |
561 | ||
562 | if (ds_file_group->ds_file_infos) { | |
563 | g_ptr_array_free(ds_file_group->ds_file_infos, TRUE); | |
564 | } | |
565 | ||
566 | bt_put(ds_file_group->stream); | |
547eacf1 | 567 | bt_put(ds_file_group->stream_class); |
94cf822e PP |
568 | g_free(ds_file_group); |
569 | } | |
570 | ||
571 | static | |
572 | struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create( | |
573 | struct ctf_fs_trace *ctf_fs_trace, | |
574 | struct bt_ctf_stream_class *stream_class, | |
575 | uint64_t stream_instance_id) | |
576 | { | |
577 | struct ctf_fs_ds_file_group *ds_file_group; | |
578 | ||
94cf822e PP |
579 | ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1); |
580 | if (!ds_file_group) { | |
581 | goto error; | |
582 | } | |
583 | ||
584 | ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func( | |
585 | (GDestroyNotify) ctf_fs_ds_file_info_destroy); | |
586 | if (!ds_file_group->ds_file_infos) { | |
587 | goto error; | |
588 | } | |
589 | ||
547eacf1 PP |
590 | ds_file_group->stream_id = stream_instance_id; |
591 | assert(stream_class); | |
592 | ds_file_group->stream_class = bt_get(stream_class); | |
94cf822e | 593 | ds_file_group->ctf_fs_trace = ctf_fs_trace; |
94cf822e PP |
594 | goto end; |
595 | ||
596 | error: | |
597 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
598 | ds_file_group = NULL; | |
599 | ||
600 | end: | |
601 | return ds_file_group; | |
602 | } | |
603 | ||
604 | static | |
605 | int ctf_fs_ds_file_group_add_ds_file_info( | |
606 | struct ctf_fs_ds_file_group *ds_file_group, | |
97ade20b JG |
607 | const char *path, uint64_t begin_ns, |
608 | struct ctf_fs_ds_index *index) | |
94cf822e PP |
609 | { |
610 | struct ctf_fs_ds_file_info *ds_file_info; | |
611 | gint i = 0; | |
612 | int ret = 0; | |
613 | ||
97ade20b JG |
614 | /* Onwership of index is transferred. */ |
615 | ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns, index); | |
616 | index = NULL; | |
94cf822e PP |
617 | if (!ds_file_info) { |
618 | goto error; | |
619 | } | |
620 | ||
621 | /* Find a spot to insert this one */ | |
622 | for (i = 0; i < ds_file_group->ds_file_infos->len; i++) { | |
623 | struct ctf_fs_ds_file_info *other_ds_file_info = | |
624 | g_ptr_array_index(ds_file_group->ds_file_infos, i); | |
625 | ||
626 | if (begin_ns < other_ds_file_info->begin_ns) { | |
627 | break; | |
628 | } | |
629 | } | |
630 | ||
631 | if (i == ds_file_group->ds_file_infos->len) { | |
632 | /* Append instead */ | |
633 | i = -1; | |
634 | } | |
635 | ||
636 | g_ptr_array_insert(ds_file_group->ds_file_infos, i, ds_file_info); | |
637 | ds_file_info = NULL; | |
638 | goto end; | |
639 | ||
640 | error: | |
641 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
97ade20b | 642 | ctf_fs_ds_index_destroy(index); |
94cf822e | 643 | ret = -1; |
94cf822e PP |
644 | end: |
645 | return ret; | |
646 | } | |
647 | ||
648 | static | |
649 | int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, | |
650 | const char *path) | |
651 | { | |
652 | struct bt_ctf_field *packet_header_field = NULL; | |
653 | struct bt_ctf_field *packet_context_field = NULL; | |
654 | struct bt_ctf_stream_class *stream_class = NULL; | |
94cf822e PP |
655 | uint64_t stream_instance_id = -1ULL; |
656 | uint64_t begin_ns = -1ULL; | |
657 | struct ctf_fs_ds_file_group *ds_file_group = NULL; | |
658 | bool add_group = false; | |
659 | int ret; | |
660 | size_t i; | |
6de92955 | 661 | struct ctf_fs_ds_file *ds_file = NULL; |
97ade20b | 662 | struct ctf_fs_ds_index *index = NULL; |
6de92955 | 663 | struct bt_ctf_notif_iter *notif_iter = NULL; |
94cf822e | 664 | |
6de92955 PP |
665 | notif_iter = bt_ctf_notif_iter_create(ctf_fs_trace->metadata->trace, |
666 | bt_common_get_page_size() * 8, ctf_fs_ds_file_medops, NULL); | |
667 | if (!notif_iter) { | |
668 | BT_LOGE_STR("Cannot create a CTF notification iterator."); | |
669 | goto error; | |
670 | } | |
671 | ||
672 | ds_file = ctf_fs_ds_file_create(ctf_fs_trace, notif_iter, NULL, path); | |
97ade20b JG |
673 | if (!ds_file) { |
674 | goto error; | |
675 | } | |
676 | ||
677 | ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file, | |
678 | &packet_header_field, &packet_context_field); | |
94cf822e | 679 | if (ret) { |
55314f2a | 680 | BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).", |
94cf822e PP |
681 | path); |
682 | goto error; | |
683 | } | |
684 | ||
685 | stream_instance_id = get_packet_header_stream_instance_id(ctf_fs_trace, | |
686 | packet_header_field); | |
687 | begin_ns = get_packet_context_timestamp_begin_ns(ctf_fs_trace, | |
688 | packet_context_field); | |
689 | stream_class = stream_class_from_packet_header(ctf_fs_trace, | |
690 | packet_header_field); | |
691 | if (!stream_class) { | |
692 | goto error; | |
693 | } | |
694 | ||
97ade20b JG |
695 | index = ctf_fs_ds_file_build_index(ds_file); |
696 | if (!index) { | |
697 | BT_LOGW("Failed to index CTF stream file \'%s\'", | |
698 | ds_file->file->path->str); | |
699 | } | |
700 | ||
94cf822e PP |
701 | if (begin_ns == -1ULL) { |
702 | /* | |
703 | * No beggining timestamp to sort the stream files | |
704 | * within a stream file group, so consider that this | |
705 | * file must be the only one within its group. | |
706 | */ | |
707 | stream_instance_id = -1ULL; | |
708 | } | |
709 | ||
710 | if (stream_instance_id == -1ULL) { | |
711 | /* | |
712 | * No stream instance ID or no beginning timestamp: | |
713 | * create a unique stream file group for this stream | |
714 | * file because, even if there's a stream instance ID, | |
715 | * there's no timestamp to order the file within its | |
716 | * group. | |
717 | */ | |
94cf822e PP |
718 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, |
719 | stream_class, stream_instance_id); | |
720 | if (!ds_file_group) { | |
721 | goto error; | |
722 | } | |
723 | ||
724 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, | |
547eacf1 | 725 | path, begin_ns, index); |
97ade20b JG |
726 | /* Ownership of index is transferred. */ |
727 | index = NULL; | |
94cf822e PP |
728 | if (ret) { |
729 | goto error; | |
730 | } | |
731 | ||
732 | add_group = true; | |
733 | goto end; | |
734 | } | |
735 | ||
736 | assert(stream_instance_id != -1ULL); | |
737 | assert(begin_ns != -1ULL); | |
738 | ||
739 | /* Find an existing stream file group with this ID */ | |
740 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
94cf822e PP |
741 | ds_file_group = g_ptr_array_index( |
742 | ctf_fs_trace->ds_file_groups, i); | |
94cf822e | 743 | |
547eacf1 PP |
744 | if (ds_file_group->stream_class == stream_class && |
745 | ds_file_group->stream_id == | |
746 | stream_instance_id) { | |
94cf822e PP |
747 | break; |
748 | } | |
749 | ||
750 | ds_file_group = NULL; | |
751 | } | |
752 | ||
753 | if (!ds_file_group) { | |
754 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, | |
755 | stream_class, stream_instance_id); | |
756 | if (!ds_file_group) { | |
757 | goto error; | |
758 | } | |
759 | ||
760 | add_group = true; | |
761 | } | |
762 | ||
547eacf1 PP |
763 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path, |
764 | begin_ns, index); | |
97ade20b | 765 | index = NULL; |
94cf822e PP |
766 | if (ret) { |
767 | goto error; | |
768 | } | |
769 | ||
770 | goto end; | |
771 | ||
772 | error: | |
773 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
774 | ret = -1; | |
775 | ||
776 | end: | |
777 | if (add_group && ds_file_group) { | |
778 | g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group); | |
779 | } | |
547eacf1 | 780 | |
97ade20b | 781 | ctf_fs_ds_file_destroy(ds_file); |
6de92955 PP |
782 | |
783 | if (notif_iter) { | |
784 | bt_ctf_notif_iter_destroy(notif_iter); | |
785 | } | |
786 | ||
97ade20b | 787 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
788 | bt_put(packet_header_field); |
789 | bt_put(packet_context_field); | |
790 | bt_put(stream_class); | |
791 | return ret; | |
792 | } | |
793 | ||
794 | static | |
795 | int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace) | |
e7a4393b JG |
796 | { |
797 | int ret = 0; | |
4f1f88a6 | 798 | const char *basename; |
e7a4393b | 799 | GError *error = NULL; |
4f1f88a6 | 800 | GDir *dir = NULL; |
547eacf1 | 801 | size_t i; |
e7a4393b | 802 | |
94cf822e | 803 | /* Check each file in the path directory, except specific ones */ |
1a9f7075 | 804 | dir = g_dir_open(ctf_fs_trace->path->str, 0, &error); |
e7a4393b | 805 | if (!dir) { |
55314f2a | 806 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 | 807 | ctf_fs_trace->path->str, error->message, |
4f1f88a6 | 808 | error->code); |
e7a4393b JG |
809 | goto error; |
810 | } | |
811 | ||
4f1f88a6 | 812 | while ((basename = g_dir_read_name(dir))) { |
94cf822e PP |
813 | struct ctf_fs_file *file; |
814 | ||
4f1f88a6 | 815 | if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) { |
e7a4393b | 816 | /* Ignore the metadata stream. */ |
55314f2a | 817 | BT_LOGD("Ignoring metadata file `%s/%s`", |
1a9f7075 | 818 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
819 | continue; |
820 | } | |
821 | ||
4f1f88a6 | 822 | if (basename[0] == '.') { |
55314f2a | 823 | BT_LOGD("Ignoring hidden file `%s/%s`", |
1a9f7075 | 824 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
825 | continue; |
826 | } | |
827 | ||
828 | /* Create the file. */ | |
55314f2a | 829 | file = ctf_fs_file_create(); |
e7a4393b | 830 | if (!file) { |
55314f2a | 831 | BT_LOGE("Cannot create stream file object for file `%s/%s`", |
1a9f7075 | 832 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
833 | goto error; |
834 | } | |
835 | ||
836 | /* Create full path string. */ | |
837 | g_string_append_printf(file->path, "%s/%s", | |
1a9f7075 | 838 | ctf_fs_trace->path->str, basename); |
e7a4393b | 839 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
55314f2a | 840 | BT_LOGD("Ignoring non-regular file `%s`", |
1a9f7075 | 841 | file->path->str); |
e7a4393b | 842 | ctf_fs_file_destroy(file); |
4f1f88a6 | 843 | file = NULL; |
e7a4393b JG |
844 | continue; |
845 | } | |
846 | ||
55314f2a | 847 | ret = ctf_fs_file_open(file, "rb"); |
4f1f88a6 | 848 | if (ret) { |
55314f2a | 849 | BT_LOGE("Cannot open stream file `%s`", file->path->str); |
e7a4393b JG |
850 | goto error; |
851 | } | |
852 | ||
9fa0891a JG |
853 | if (file->size == 0) { |
854 | /* Skip empty stream. */ | |
55314f2a | 855 | BT_LOGD("Ignoring empty file `%s`", file->path->str); |
9fa0891a JG |
856 | ctf_fs_file_destroy(file); |
857 | continue; | |
858 | } | |
859 | ||
94cf822e PP |
860 | ret = add_ds_file_to_ds_file_group(ctf_fs_trace, |
861 | file->path->str); | |
4f1f88a6 | 862 | if (ret) { |
89b08333 | 863 | BT_LOGE("Cannot add stream file `%s` to stream file group", |
1a9f7075 | 864 | file->path->str); |
94cf822e | 865 | ctf_fs_file_destroy(file); |
e7a4393b JG |
866 | goto error; |
867 | } | |
868 | ||
4f1f88a6 | 869 | ctf_fs_file_destroy(file); |
e7a4393b JG |
870 | } |
871 | ||
547eacf1 PP |
872 | /* |
873 | * At this point, DS file groupes are created, but their | |
874 | * associated stream objects do not exist yet. This is because | |
875 | * we need to name the created stream object with the data | |
876 | * stream file's path. We have everything we need here to do | |
877 | * this. | |
878 | */ | |
879 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
880 | struct ctf_fs_ds_file_group *ds_file_group = | |
881 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
882 | GString *name = get_stream_instance_unique_name(ds_file_group); | |
883 | ||
884 | if (!name) { | |
885 | goto error; | |
886 | } | |
887 | ||
888 | if (ds_file_group->stream_id == -1ULL) { | |
889 | /* No stream ID */ | |
890 | ds_file_group->stream = bt_ctf_stream_create( | |
891 | ds_file_group->stream_class, name->str); | |
892 | } else { | |
893 | /* Specific stream ID */ | |
894 | ds_file_group->stream = bt_ctf_stream_create_with_id( | |
895 | ds_file_group->stream_class, name->str, | |
896 | ds_file_group->stream_id); | |
897 | } | |
898 | ||
899 | g_string_free(name, TRUE); | |
900 | ||
901 | if (!ds_file_group) { | |
902 | BT_LOGE("Cannot create stream for DS file group: " | |
903 | "addr=%p, stream-name=\"%s\"", | |
904 | ds_file_group, name->str); | |
905 | goto error; | |
906 | } | |
907 | } | |
908 | ||
e7a4393b | 909 | goto end; |
4f1f88a6 | 910 | |
e7a4393b JG |
911 | error: |
912 | ret = -1; | |
4f1f88a6 | 913 | |
e7a4393b JG |
914 | end: |
915 | if (dir) { | |
916 | g_dir_close(dir); | |
917 | dir = NULL; | |
918 | } | |
4f1f88a6 | 919 | |
e7a4393b JG |
920 | if (error) { |
921 | g_error_free(error); | |
922 | } | |
5b29e799 | 923 | |
91457551 | 924 | return ret; |
5b29e799 JG |
925 | } |
926 | ||
599faa1c | 927 | static |
1a9f7075 | 928 | int create_cc_prio_map(struct ctf_fs_trace *ctf_fs_trace) |
599faa1c PP |
929 | { |
930 | int ret = 0; | |
931 | size_t i; | |
932 | int count; | |
933 | ||
1a9f7075 PP |
934 | assert(ctf_fs_trace); |
935 | ctf_fs_trace->cc_prio_map = bt_clock_class_priority_map_create(); | |
936 | if (!ctf_fs_trace->cc_prio_map) { | |
599faa1c PP |
937 | ret = -1; |
938 | goto end; | |
939 | } | |
940 | ||
1a9f7075 PP |
941 | count = bt_ctf_trace_get_clock_class_count( |
942 | ctf_fs_trace->metadata->trace); | |
599faa1c PP |
943 | assert(count >= 0); |
944 | ||
945 | for (i = 0; i < count; i++) { | |
946 | struct bt_ctf_clock_class *clock_class = | |
9ac68eb1 | 947 | bt_ctf_trace_get_clock_class_by_index( |
1a9f7075 | 948 | ctf_fs_trace->metadata->trace, i); |
599faa1c PP |
949 | |
950 | assert(clock_class); | |
951 | ret = bt_clock_class_priority_map_add_clock_class( | |
1a9f7075 | 952 | ctf_fs_trace->cc_prio_map, clock_class, 0); |
599faa1c PP |
953 | BT_PUT(clock_class); |
954 | ||
955 | if (ret) { | |
956 | goto end; | |
957 | } | |
958 | } | |
959 | ||
960 | end: | |
961 | return ret; | |
962 | } | |
963 | ||
97ade20b | 964 | BT_HIDDEN |
55314f2a | 965 | struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name, |
a2a54545 | 966 | struct ctf_fs_metadata_config *metadata_config) |
1a9f7075 PP |
967 | { |
968 | struct ctf_fs_trace *ctf_fs_trace; | |
969 | int ret; | |
970 | ||
971 | ctf_fs_trace = g_new0(struct ctf_fs_trace, 1); | |
972 | if (!ctf_fs_trace) { | |
973 | goto end; | |
974 | } | |
975 | ||
1a9f7075 PP |
976 | ctf_fs_trace->path = g_string_new(path); |
977 | if (!ctf_fs_trace->path) { | |
978 | goto error; | |
979 | } | |
980 | ||
981 | ctf_fs_trace->name = g_string_new(name); | |
982 | if (!ctf_fs_trace->name) { | |
983 | goto error; | |
984 | } | |
985 | ||
986 | ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1); | |
987 | if (!ctf_fs_trace->metadata) { | |
988 | goto error; | |
989 | } | |
990 | ||
94cf822e PP |
991 | ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func( |
992 | (GDestroyNotify) ctf_fs_ds_file_group_destroy); | |
993 | if (!ctf_fs_trace->ds_file_groups) { | |
994 | goto error; | |
995 | } | |
996 | ||
a2a54545 | 997 | ret = ctf_fs_metadata_set_trace(ctf_fs_trace, metadata_config); |
1a9f7075 PP |
998 | if (ret) { |
999 | goto error; | |
1000 | } | |
1001 | ||
94cf822e PP |
1002 | ret = create_ds_file_groups(ctf_fs_trace); |
1003 | if (ret) { | |
1004 | goto error; | |
1005 | } | |
1006 | ||
1a9f7075 PP |
1007 | ret = create_cc_prio_map(ctf_fs_trace); |
1008 | if (ret) { | |
1009 | goto error; | |
1010 | } | |
1011 | ||
27d1a78b PP |
1012 | /* |
1013 | * create_ds_file_groups() created all the streams that this | |
1014 | * trace needs. There won't be any more. Therefore it is safe to | |
1015 | * make this trace static. | |
1016 | */ | |
1017 | (void) bt_ctf_trace_set_is_static(ctf_fs_trace->metadata->trace); | |
1018 | ||
1a9f7075 PP |
1019 | goto end; |
1020 | ||
1021 | error: | |
1022 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1023 | ctf_fs_trace = NULL; | |
1024 | end: | |
1025 | return ctf_fs_trace; | |
1026 | } | |
1027 | ||
1028 | static | |
1029 | int path_is_ctf_trace(const char *path) | |
1030 | { | |
1031 | GString *metadata_path = g_string_new(NULL); | |
1032 | int ret = 0; | |
1033 | ||
1034 | if (!metadata_path) { | |
1035 | ret = -1; | |
1036 | goto end; | |
1037 | } | |
1038 | ||
1039 | g_string_printf(metadata_path, "%s/%s", path, CTF_FS_METADATA_FILENAME); | |
1040 | ||
1041 | if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) { | |
1042 | ret = 1; | |
1043 | goto end; | |
1044 | } | |
1045 | ||
1046 | end: | |
1047 | g_string_free(metadata_path, TRUE); | |
1048 | return ret; | |
1049 | } | |
1050 | ||
1051 | static | |
55314f2a | 1052 | int add_trace_path(GList **trace_paths, const char *path) |
1a9f7075 | 1053 | { |
4bd72b60 | 1054 | GString *norm_path = NULL; |
1a9f7075 | 1055 | int ret = 0; |
1a9f7075 | 1056 | |
4bd72b60 PP |
1057 | norm_path = bt_common_normalize_path(path, NULL); |
1058 | if (!norm_path) { | |
55314f2a | 1059 | BT_LOGE("Failed to normalize path `%s`.", path); |
1a9f7075 PP |
1060 | ret = -1; |
1061 | goto end; | |
1062 | } | |
1063 | ||
4bd72b60 | 1064 | if (strcmp(norm_path->str, "/") == 0) { |
55314f2a | 1065 | BT_LOGE("Opening a trace in `/` is not supported."); |
1a9f7075 PP |
1066 | ret = -1; |
1067 | goto end; | |
1068 | } | |
1069 | ||
4bd72b60 | 1070 | *trace_paths = g_list_prepend(*trace_paths, norm_path); |
1a9f7075 | 1071 | assert(*trace_paths); |
4bd72b60 | 1072 | norm_path = NULL; |
1a9f7075 PP |
1073 | |
1074 | end: | |
4bd72b60 PP |
1075 | if (norm_path) { |
1076 | g_string_free(norm_path, TRUE); | |
1077 | } | |
1078 | ||
1a9f7075 PP |
1079 | return ret; |
1080 | } | |
1081 | ||
55314f2a JG |
1082 | BT_HIDDEN |
1083 | int ctf_fs_find_traces(GList **trace_paths, const char *start_path) | |
1a9f7075 PP |
1084 | { |
1085 | int ret; | |
1086 | GError *error = NULL; | |
1087 | GDir *dir = NULL; | |
1088 | const char *basename = NULL; | |
1089 | ||
1090 | /* Check if the starting path is a CTF trace itself */ | |
1091 | ret = path_is_ctf_trace(start_path); | |
1092 | if (ret < 0) { | |
1093 | goto end; | |
1094 | } | |
1095 | ||
1096 | if (ret) { | |
1097 | /* | |
4bd72b60 PP |
1098 | * Stop recursion: a CTF trace cannot contain another |
1099 | * CTF trace. | |
1a9f7075 | 1100 | */ |
55314f2a | 1101 | ret = add_trace_path(trace_paths, start_path); |
1a9f7075 PP |
1102 | goto end; |
1103 | } | |
1104 | ||
1105 | /* Look for subdirectories */ | |
1106 | if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) { | |
1107 | /* Starting path is not a directory: end of recursion */ | |
1108 | goto end; | |
1109 | } | |
1110 | ||
1111 | dir = g_dir_open(start_path, 0, &error); | |
1112 | if (!dir) { | |
1113 | if (error->code == G_FILE_ERROR_ACCES) { | |
55314f2a | 1114 | BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing", |
1a9f7075 PP |
1115 | start_path, error->message, error->code); |
1116 | goto end; | |
1117 | } | |
1118 | ||
55314f2a | 1119 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 PP |
1120 | start_path, error->message, error->code); |
1121 | ret = -1; | |
1122 | goto end; | |
1123 | } | |
1124 | ||
1125 | while ((basename = g_dir_read_name(dir))) { | |
1126 | GString *sub_path = g_string_new(NULL); | |
1127 | ||
1128 | if (!sub_path) { | |
1129 | ret = -1; | |
1130 | goto end; | |
1131 | } | |
1132 | ||
1133 | g_string_printf(sub_path, "%s/%s", start_path, basename); | |
55314f2a | 1134 | ret = ctf_fs_find_traces(trace_paths, sub_path->str); |
1a9f7075 PP |
1135 | g_string_free(sub_path, TRUE); |
1136 | if (ret) { | |
1137 | goto end; | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | end: | |
1142 | if (dir) { | |
1143 | g_dir_close(dir); | |
1144 | } | |
1145 | ||
1146 | if (error) { | |
1147 | g_error_free(error); | |
1148 | } | |
1149 | ||
1150 | return ret; | |
1151 | } | |
1152 | ||
55314f2a JG |
1153 | BT_HIDDEN |
1154 | GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) { | |
1a9f7075 | 1155 | GList *trace_names = NULL; |
1a9f7075 | 1156 | GList *node; |
4bd72b60 PP |
1157 | const char *last_sep; |
1158 | size_t base_dist; | |
1a9f7075 PP |
1159 | |
1160 | /* | |
4bd72b60 PP |
1161 | * At this point we know that all the trace paths are |
1162 | * normalized, and so is the base path. This means that | |
1163 | * they are absolute and they don't end with a separator. | |
1164 | * We can simply find the location of the last separator | |
1165 | * in the base path, which gives us the name of the actual | |
1166 | * directory to look into, and use this location as the | |
1167 | * start of each trace name within each trace path. | |
1168 | * | |
1169 | * For example: | |
1170 | * | |
1171 | * Base path: /home/user/my-traces/some-trace | |
1172 | * Trace paths: | |
1173 | * - /home/user/my-traces/some-trace/host1/trace1 | |
1174 | * - /home/user/my-traces/some-trace/host1/trace2 | |
1175 | * - /home/user/my-traces/some-trace/host2/trace | |
1176 | * - /home/user/my-traces/some-trace/other-trace | |
1177 | * | |
1178 | * In this case the trace names are: | |
1179 | * | |
1180 | * - some-trace/host1/trace1 | |
1181 | * - some-trace/host1/trace2 | |
1182 | * - some-trace/host2/trace | |
1183 | * - some-trace/other-trace | |
1a9f7075 | 1184 | */ |
4bd72b60 | 1185 | last_sep = strrchr(base_path, G_DIR_SEPARATOR); |
1a9f7075 | 1186 | |
4bd72b60 PP |
1187 | /* We know there's at least one separator */ |
1188 | assert(last_sep); | |
1a9f7075 | 1189 | |
4bd72b60 PP |
1190 | /* Distance to base */ |
1191 | base_dist = last_sep - base_path + 1; | |
1a9f7075 PP |
1192 | |
1193 | /* Create the trace names */ | |
1194 | for (node = trace_paths; node; node = g_list_next(node)) { | |
1195 | GString *trace_name = g_string_new(NULL); | |
1196 | GString *trace_path = node->data; | |
1197 | ||
4bd72b60 PP |
1198 | assert(trace_name); |
1199 | g_string_assign(trace_name, &trace_path->str[base_dist]); | |
1a9f7075 PP |
1200 | trace_names = g_list_append(trace_names, trace_name); |
1201 | } | |
1202 | ||
1203 | return trace_names; | |
1204 | } | |
1205 | ||
1206 | static | |
1207 | int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs, | |
1208 | const char *path_param) | |
1209 | { | |
1210 | struct ctf_fs_trace *ctf_fs_trace = NULL; | |
1211 | int ret = 0; | |
4bd72b60 | 1212 | GString *norm_path = NULL; |
1a9f7075 PP |
1213 | GList *trace_paths = NULL; |
1214 | GList *trace_names = NULL; | |
1215 | GList *tp_node; | |
1216 | GList *tn_node; | |
1217 | ||
4bd72b60 PP |
1218 | norm_path = bt_common_normalize_path(path_param, NULL); |
1219 | if (!norm_path) { | |
55314f2a | 1220 | BT_LOGE("Failed to normalize path: `%s`.", |
4bd72b60 PP |
1221 | path_param); |
1222 | goto error; | |
1223 | } | |
1224 | ||
55314f2a | 1225 | ret = ctf_fs_find_traces(&trace_paths, norm_path->str); |
1a9f7075 PP |
1226 | if (ret) { |
1227 | goto error; | |
1228 | } | |
1229 | ||
1230 | if (!trace_paths) { | |
55314f2a | 1231 | BT_LOGE("No CTF traces recursively found in `%s`.", |
1a9f7075 PP |
1232 | path_param); |
1233 | goto error; | |
1234 | } | |
1235 | ||
55314f2a | 1236 | trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str); |
1a9f7075 | 1237 | if (!trace_names) { |
55314f2a | 1238 | BT_LOGE("Cannot create trace names from trace paths."); |
1a9f7075 PP |
1239 | goto error; |
1240 | } | |
1241 | ||
1242 | for (tp_node = trace_paths, tn_node = trace_names; tp_node; | |
1243 | tp_node = g_list_next(tp_node), | |
1244 | tn_node = g_list_next(tn_node)) { | |
1245 | GString *trace_path = tp_node->data; | |
1246 | GString *trace_name = tn_node->data; | |
1247 | ||
55314f2a | 1248 | ctf_fs_trace = ctf_fs_trace_create(trace_path->str, |
a2a54545 | 1249 | trace_name->str, &ctf_fs->metadata_config); |
1a9f7075 | 1250 | if (!ctf_fs_trace) { |
55314f2a | 1251 | BT_LOGE("Cannot create trace for `%s`.", |
1a9f7075 PP |
1252 | trace_path->str); |
1253 | goto error; | |
1254 | } | |
52c5fe74 | 1255 | |
55314f2a JG |
1256 | ret = create_ports_for_trace(ctf_fs, ctf_fs_trace); |
1257 | if (ret) { | |
1258 | goto error; | |
1259 | } | |
1260 | ||
52c5fe74 PP |
1261 | g_ptr_array_add(ctf_fs->traces, ctf_fs_trace); |
1262 | ctf_fs_trace = NULL; | |
1a9f7075 PP |
1263 | } |
1264 | ||
1a9f7075 PP |
1265 | goto end; |
1266 | ||
1267 | error: | |
1268 | ret = -1; | |
1269 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1270 | ||
1271 | end: | |
1272 | for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) { | |
1273 | if (tp_node->data) { | |
1274 | g_string_free(tp_node->data, TRUE); | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) { | |
1279 | if (tn_node->data) { | |
1280 | g_string_free(tn_node->data, TRUE); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | if (trace_paths) { | |
1285 | g_list_free(trace_paths); | |
1286 | } | |
1287 | ||
1288 | if (trace_names) { | |
1289 | g_list_free(trace_names); | |
1290 | } | |
1291 | ||
4bd72b60 PP |
1292 | if (norm_path) { |
1293 | g_string_free(norm_path, TRUE); | |
1294 | } | |
1295 | ||
1a9f7075 PP |
1296 | return ret; |
1297 | } | |
1298 | ||
5b29e799 | 1299 | static |
4f1f88a6 PP |
1300 | struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp, |
1301 | struct bt_value *params) | |
56a1cced JG |
1302 | { |
1303 | struct ctf_fs_component *ctf_fs; | |
1ef09eb5 | 1304 | struct bt_value *value = NULL; |
1a9f7075 | 1305 | const char *path_param; |
4f1f88a6 | 1306 | int ret; |
56a1cced JG |
1307 | |
1308 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
1309 | if (!ctf_fs) { | |
1310 | goto end; | |
1311 | } | |
1312 | ||
1a9f7075 PP |
1313 | ret = bt_private_component_set_user_data(priv_comp, ctf_fs); |
1314 | assert(ret == 0); | |
1315 | ||
4f1f88a6 PP |
1316 | /* |
1317 | * We don't need to get a new reference here because as long as | |
1318 | * our private ctf_fs_component object exists, the containing | |
1319 | * private component should also exist. | |
1320 | */ | |
1321 | ctf_fs->priv_comp = priv_comp; | |
56a1cced | 1322 | value = bt_value_map_get(params, "path"); |
1a9f7075 | 1323 | if (!bt_value_is_string(value)) { |
56a1cced JG |
1324 | goto error; |
1325 | } | |
1326 | ||
1a9f7075 PP |
1327 | ret = bt_value_string_get(value, &path_param); |
1328 | assert(ret == 0); | |
1329 | BT_PUT(value); | |
a2a54545 | 1330 | value = bt_value_map_get(params, "clock-class-offset-s"); |
92540773 | 1331 | if (value) { |
a2a54545 PP |
1332 | if (!bt_value_is_integer(value)) { |
1333 | BT_LOGE("clock-class-offset-s should be an integer"); | |
1334 | goto error; | |
1335 | } | |
1336 | ret = bt_value_integer_get(value, | |
1337 | &ctf_fs->metadata_config.clock_class_offset_s); | |
1338 | assert(ret == 0); | |
1339 | BT_PUT(value); | |
1340 | } | |
92540773 | 1341 | |
a2a54545 PP |
1342 | value = bt_value_map_get(params, "clock-class-offset-ns"); |
1343 | if (value) { | |
92540773 | 1344 | if (!bt_value_is_integer(value)) { |
a2a54545 | 1345 | BT_LOGE("clock-class-offset-ns should be an integer"); |
92540773 JD |
1346 | goto error; |
1347 | } | |
a2a54545 PP |
1348 | ret = bt_value_integer_get(value, |
1349 | &ctf_fs->metadata_config.clock_class_offset_ns); | |
1a9f7075 | 1350 | assert(ret == 0); |
1a9f7075 | 1351 | BT_PUT(value); |
92540773 JD |
1352 | } |
1353 | ||
1a9f7075 PP |
1354 | ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy); |
1355 | if (!ctf_fs->port_data) { | |
4f1f88a6 PP |
1356 | goto error; |
1357 | } | |
1358 | ||
97ade20b JG |
1359 | ctf_fs->traces = g_ptr_array_new_with_free_func( |
1360 | ctf_fs_trace_destroy_notifier); | |
1a9f7075 | 1361 | if (!ctf_fs->traces) { |
599faa1c PP |
1362 | goto error; |
1363 | } | |
1364 | ||
1a9f7075 | 1365 | ret = create_ctf_fs_traces(ctf_fs, path_param); |
4f1f88a6 PP |
1366 | if (ret) { |
1367 | goto error; | |
1368 | } | |
1369 | ||
1ef09eb5 JG |
1370 | goto end; |
1371 | ||
56a1cced | 1372 | error: |
1a9f7075 | 1373 | ctf_fs_destroy(ctf_fs); |
e7a4393b | 1374 | ctf_fs = NULL; |
1a9f7075 PP |
1375 | ret = bt_private_component_set_user_data(priv_comp, NULL); |
1376 | assert(ret == 0); | |
1377 | ||
1ef09eb5 | 1378 | end: |
1a9f7075 | 1379 | bt_put(value); |
56a1cced JG |
1380 | return ctf_fs; |
1381 | } | |
1382 | ||
ea0b4b9e | 1383 | BT_HIDDEN |
4f1f88a6 | 1384 | enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp, |
7d61fa8e | 1385 | struct bt_value *params, UNUSED_VAR void *init_method_data) |
ea0b4b9e JG |
1386 | { |
1387 | struct ctf_fs_component *ctf_fs; | |
1388 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
1389 | ||
4f1f88a6 | 1390 | ctf_fs = ctf_fs_create(priv_comp, params); |
ea0b4b9e | 1391 | if (!ctf_fs) { |
1a9f7075 | 1392 | ret = BT_COMPONENT_STATUS_ERROR; |
ea0b4b9e | 1393 | } |
4c1456f0 | 1394 | |
ea0b4b9e JG |
1395 | return ret; |
1396 | } | |
33f93973 PP |
1397 | |
1398 | BT_HIDDEN | |
a67681c1 PP |
1399 | struct bt_value *ctf_fs_query(struct bt_component_class *comp_class, |
1400 | const char *object, struct bt_value *params) | |
33f93973 | 1401 | { |
04c0ba87 | 1402 | struct bt_value *result = NULL; |
33f93973 | 1403 | |
04c0ba87 JG |
1404 | if (!strcmp(object, "metadata-info")) { |
1405 | result = metadata_info_query(comp_class, params); | |
97ade20b JG |
1406 | } else if (!strcmp(object, "trace-info")) { |
1407 | result = trace_info_query(comp_class, params); | |
33f93973 | 1408 | } else { |
55314f2a | 1409 | BT_LOGE("Unknown query object `%s`", object); |
04c0ba87 | 1410 | goto end; |
33f93973 | 1411 | } |
33f93973 | 1412 | end: |
04c0ba87 | 1413 | return result; |
33f93973 | 1414 | } |