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