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