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