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