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