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