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