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