Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
ea0b4b9e | 2 | * fs.c |
7a278c8e | 3 | * |
ea0b4b9e | 4 | * Babeltrace CTF file system Reader Component |
7a278c8e | 5 | * |
f3bc2010 | 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7a278c8e JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
5b29e799 | 29 | #include <babeltrace/ctf-ir/packet.h> |
ac0c6bdd | 30 | #include <babeltrace/ctf-ir/clock-class.h> |
33b34c43 PP |
31 | #include <babeltrace/component/notification/iterator.h> |
32 | #include <babeltrace/component/notification/stream.h> | |
33 | #include <babeltrace/component/notification/event.h> | |
34 | #include <babeltrace/component/notification/packet.h> | |
35 | #include <babeltrace/component/notification/heap.h> | |
7d61fa8e | 36 | #include <plugins-common.h> |
ea0b4b9e JG |
37 | #include <glib.h> |
38 | #include <assert.h> | |
56a1cced JG |
39 | #include <unistd.h> |
40 | #include "fs.h" | |
413bc2c4 JG |
41 | #include "metadata.h" |
42 | #include "data-stream.h" | |
e7a4393b JG |
43 | #include "file.h" |
44 | ||
45 | #define PRINT_ERR_STREAM ctf_fs->error_fp | |
46 | #define PRINT_PREFIX "ctf-fs" | |
47 | #include "print.h" | |
ea0b4b9e | 48 | |
78bb6992 MD |
49 | BT_HIDDEN |
50 | bool ctf_fs_debug; | |
ea0b4b9e | 51 | |
f48bc732 JG |
52 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
53 | struct bt_notification_iterator *iterator); | |
54 | ||
760051fa JG |
55 | struct bt_notification *ctf_fs_iterator_get( |
56 | struct bt_notification_iterator *iterator) | |
ea0b4b9e | 57 | { |
5b29e799 JG |
58 | struct ctf_fs_iterator *ctf_it = |
59 | bt_notification_iterator_get_private_data(iterator); | |
d01e0f33 | 60 | |
f48bc732 JG |
61 | if (!ctf_it->current_notification) { |
62 | (void) ctf_fs_iterator_next(iterator); | |
63 | } | |
64 | ||
5b29e799 | 65 | return bt_get(ctf_it->current_notification); |
ea0b4b9e JG |
66 | } |
67 | ||
68 | static | |
5b29e799 JG |
69 | enum bt_notification_iterator_status ctf_fs_iterator_get_next_notification( |
70 | struct ctf_fs_iterator *it, | |
71 | struct ctf_fs_stream *stream, | |
72 | struct bt_notification **notification) | |
ea0b4b9e | 73 | { |
5b29e799 | 74 | enum bt_ctf_notif_iter_status status; |
d01e0f33 | 75 | enum bt_notification_iterator_status ret; |
d01e0f33 | 76 | |
5b29e799 JG |
77 | if (stream->end_reached) { |
78 | status = BT_CTF_NOTIF_ITER_STATUS_EOF; | |
d01e0f33 JG |
79 | goto end; |
80 | } | |
81 | ||
5b29e799 JG |
82 | status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter, |
83 | notification); | |
84 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK && | |
85 | status != BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
d01e0f33 JG |
86 | goto end; |
87 | } | |
88 | ||
5b29e799 JG |
89 | /* Should be handled in bt_ctf_notif_iter_get_next_notification. */ |
90 | if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
91 | *notification = bt_notification_stream_end_create( | |
92 | stream->stream); | |
93 | if (!*notification) { | |
94 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
95 | } | |
96 | status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
97 | stream->end_reached = true; | |
98 | } | |
d01e0f33 | 99 | end: |
5b29e799 JG |
100 | switch (status) { |
101 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
102 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
103 | break; | |
104 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
105 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
106 | break; | |
107 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
108 | /* | |
109 | * Should not make it this far as this is medium-specific; | |
110 | * there is nothing for the user to do and it should have been | |
111 | * handled upstream. | |
112 | */ | |
113 | assert(0); | |
114 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: | |
115 | /* No argument provided by the user, so don't return INVAL. */ | |
116 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
117 | default: | |
118 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
119 | break; | |
120 | } | |
043e2020 | 121 | return ret; |
ea0b4b9e | 122 | } |
bfd20a42 | 123 | |
5b29e799 JG |
124 | /* |
125 | * Remove me. This is a temporary work-around due to our inhability to use | |
126 | * libbabeltrace-ctf from libbabeltrace-plugin. | |
127 | */ | |
760051fa | 128 | static |
5b29e799 JG |
129 | struct bt_ctf_stream *internal_bt_notification_get_stream( |
130 | struct bt_notification *notification) | |
760051fa | 131 | { |
5b29e799 JG |
132 | struct bt_ctf_stream *stream = NULL; |
133 | ||
134 | assert(notification); | |
135 | switch (bt_notification_get_type(notification)) { | |
136 | case BT_NOTIFICATION_TYPE_EVENT: | |
137 | { | |
138 | struct bt_ctf_event *event; | |
139 | ||
140 | event = bt_notification_event_get_event(notification); | |
141 | stream = bt_ctf_event_get_stream(event); | |
142 | bt_put(event); | |
143 | break; | |
144 | } | |
ea0e619e | 145 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
5b29e799 JG |
146 | { |
147 | struct bt_ctf_packet *packet; | |
148 | ||
ea0e619e | 149 | packet = bt_notification_packet_begin_get_packet(notification); |
5b29e799 JG |
150 | stream = bt_ctf_packet_get_stream(packet); |
151 | bt_put(packet); | |
152 | break; | |
153 | } | |
154 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
155 | { | |
156 | struct bt_ctf_packet *packet; | |
157 | ||
158 | packet = bt_notification_packet_end_get_packet(notification); | |
159 | stream = bt_ctf_packet_get_stream(packet); | |
160 | bt_put(packet); | |
161 | break; | |
162 | } | |
163 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
164 | stream = bt_notification_stream_end_get_stream(notification); | |
165 | break; | |
166 | default: | |
167 | goto end; | |
168 | } | |
169 | end: | |
170 | return stream; | |
760051fa JG |
171 | } |
172 | ||
173 | static | |
5b29e799 | 174 | enum bt_notification_iterator_status populate_heap(struct ctf_fs_iterator *it) |
760051fa | 175 | { |
5b29e799 JG |
176 | size_t i, pending_streams_count = it->pending_streams->len; |
177 | enum bt_notification_iterator_status ret = | |
178 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
179 | ||
180 | /* Insert one stream-associated notification for each stream. */ | |
181 | for (i = 0; i < pending_streams_count; i++) { | |
182 | struct bt_notification *notification; | |
183 | struct ctf_fs_stream *fs_stream; | |
184 | struct bt_ctf_stream *stream; | |
185 | size_t pending_stream_index = pending_streams_count - 1 - i; | |
186 | ||
187 | fs_stream = g_ptr_array_index(it->pending_streams, | |
188 | pending_stream_index); | |
189 | ||
190 | do { | |
191 | int heap_ret; | |
192 | ||
193 | ret = ctf_fs_iterator_get_next_notification( | |
194 | it, fs_stream, ¬ification); | |
195 | if (ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END) { | |
196 | printf_debug("Failed to populate heap at stream %zu\n", | |
197 | pending_stream_index); | |
198 | goto end; | |
199 | } | |
200 | ||
201 | stream = internal_bt_notification_get_stream( | |
202 | notification); | |
203 | if (stream) { | |
204 | gboolean inserted; | |
205 | ||
206 | /* | |
207 | * Associate pending ctf_fs_stream to | |
208 | * bt_ctf_stream. Ownership of stream | |
209 | * is passed to the stream ht. | |
210 | */ | |
211 | inserted = g_hash_table_insert(it->stream_ht, | |
212 | stream, fs_stream); | |
213 | if (!inserted) { | |
214 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
215 | printf_debug("Failed to associate fs stream to ctf stream\n"); | |
216 | goto end; | |
217 | } | |
218 | } | |
219 | ||
220 | heap_ret = bt_notification_heap_insert( | |
221 | it->pending_notifications, | |
222 | notification); | |
223 | bt_put(notification); | |
224 | if (heap_ret) { | |
225 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
226 | printf_debug("Failed to insert notification in heap\n"); | |
227 | goto end; | |
228 | } | |
229 | } while (!stream && ret != BT_NOTIFICATION_ITERATOR_STATUS_END); | |
230 | /* | |
231 | * Set NULL so the destruction callback registered with the | |
232 | * array is not invoked on the stream (its ownership was | |
233 | * transferred to the streams hashtable). | |
234 | */ | |
235 | g_ptr_array_index(it->pending_streams, | |
236 | pending_stream_index) = NULL; | |
237 | g_ptr_array_remove_index(it->pending_streams, | |
238 | pending_stream_index); | |
239 | } | |
760051fa | 240 | |
5b29e799 JG |
241 | g_ptr_array_free(it->pending_streams, TRUE); |
242 | it->pending_streams = NULL; | |
243 | end: | |
244 | return ret; | |
760051fa JG |
245 | } |
246 | ||
5b29e799 JG |
247 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
248 | struct bt_notification_iterator *iterator) | |
4c1456f0 | 249 | { |
5b29e799 | 250 | int heap_ret; |
a11bd504 | 251 | struct bt_ctf_stream *stream = NULL; |
5b29e799 JG |
252 | struct ctf_fs_stream *fs_stream; |
253 | struct bt_notification *notification; | |
254 | struct bt_notification *next_stream_notification; | |
255 | enum bt_notification_iterator_status ret = | |
256 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
257 | struct ctf_fs_iterator *ctf_it = | |
258 | bt_notification_iterator_get_private_data(iterator); | |
259 | ||
260 | notification = bt_notification_heap_pop(ctf_it->pending_notifications); | |
261 | if (!notification && !ctf_it->pending_streams) { | |
262 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
760051fa JG |
263 | goto end; |
264 | } | |
265 | ||
5b29e799 JG |
266 | if (!notification && ctf_it->pending_streams) { |
267 | /* | |
268 | * Insert at one notification per stream in the heap and pop | |
269 | * one. | |
270 | */ | |
271 | ret = populate_heap(ctf_it); | |
272 | if (ret) { | |
273 | goto end; | |
274 | } | |
275 | ||
276 | notification = bt_notification_heap_pop( | |
277 | ctf_it->pending_notifications); | |
278 | if (!notification) { | |
279 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
280 | goto end; | |
281 | } | |
760051fa JG |
282 | } |
283 | ||
5b29e799 JG |
284 | /* notification is set from here. */ |
285 | ||
286 | stream = internal_bt_notification_get_stream(notification); | |
287 | if (!stream) { | |
288 | /* | |
289 | * The current notification is not associated to a particular | |
290 | * stream, there is no need to insert a new notification from | |
291 | * a stream in the heap. | |
292 | */ | |
293 | goto end; | |
760051fa JG |
294 | } |
295 | ||
5b29e799 JG |
296 | fs_stream = g_hash_table_lookup(ctf_it->stream_ht, stream); |
297 | if (!fs_stream) { | |
298 | /* We have reached this stream's end. */ | |
299 | goto end; | |
760051fa JG |
300 | } |
301 | ||
5b29e799 JG |
302 | ret = ctf_fs_iterator_get_next_notification(ctf_it, fs_stream, |
303 | &next_stream_notification); | |
304 | if ((ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END)) { | |
305 | heap_ret = bt_notification_heap_insert( | |
306 | ctf_it->pending_notifications, notification); | |
307 | ||
308 | assert(!next_stream_notification); | |
309 | if (heap_ret) { | |
310 | /* | |
311 | * We're dropping the most recent notification, but at | |
312 | * this point, something is seriously wrong... | |
313 | */ | |
314 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
315 | } | |
316 | BT_PUT(notification); | |
317 | goto end; | |
318 | } | |
319 | ||
320 | if (ret == BT_NOTIFICATION_ITERATOR_STATUS_END) { | |
321 | gboolean success; | |
322 | ||
323 | /* Remove stream. */ | |
324 | success = g_hash_table_remove(ctf_it->stream_ht, stream); | |
325 | assert(success); | |
326 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
327 | } else { | |
328 | heap_ret = bt_notification_heap_insert(ctf_it->pending_notifications, | |
329 | next_stream_notification); | |
330 | BT_PUT(next_stream_notification); | |
331 | if (heap_ret) { | |
332 | /* | |
333 | * We're dropping the most recent notification... | |
334 | */ | |
335 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
336 | } | |
760051fa | 337 | } |
5b29e799 JG |
338 | |
339 | /* | |
340 | * Ensure that the stream is removed from both pending_streams and | |
341 | * the streams hashtable on reception of the "end of stream" | |
342 | * notification. | |
343 | */ | |
760051fa | 344 | end: |
5b29e799 | 345 | BT_MOVE(ctf_it->current_notification, notification); |
33bf10b7 | 346 | bt_put(stream); |
760051fa JG |
347 | return ret; |
348 | } | |
349 | ||
760051fa | 350 | static |
5b29e799 | 351 | void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it) |
760051fa | 352 | { |
5b29e799 JG |
353 | bt_put(ctf_it->current_notification); |
354 | bt_put(ctf_it->pending_notifications); | |
355 | if (ctf_it->pending_streams) { | |
356 | g_ptr_array_free(ctf_it->pending_streams, TRUE); | |
56a1cced | 357 | } |
5b29e799 JG |
358 | if (ctf_it->stream_ht) { |
359 | g_hash_table_destroy(ctf_it->stream_ht); | |
c14d7e26 | 360 | } |
5b29e799 | 361 | g_free(ctf_it); |
760051fa JG |
362 | } |
363 | ||
5b29e799 | 364 | void ctf_fs_iterator_destroy(struct bt_notification_iterator *it) |
760051fa | 365 | { |
5b29e799 | 366 | void *data = bt_notification_iterator_get_private_data(it); |
760051fa | 367 | |
5b29e799 | 368 | ctf_fs_iterator_destroy_data(data); |
4c1456f0 JG |
369 | } |
370 | ||
a4792757 JG |
371 | static |
372 | bool compare_event_notifications(struct bt_notification *a, | |
373 | struct bt_notification *b) | |
374 | { | |
375 | int ret; | |
ac0c6bdd | 376 | struct bt_ctf_clock_class *clock_class; |
a4792757 JG |
377 | struct bt_ctf_clock_value *a_clock_value, *b_clock_value; |
378 | struct bt_ctf_stream_class *a_stream_class; | |
379 | struct bt_ctf_stream *a_stream; | |
380 | struct bt_ctf_event *a_event, *b_event; | |
381 | struct bt_ctf_trace *trace; | |
382 | int64_t a_ts, b_ts; | |
383 | ||
384 | // FIXME - assumes only one clock | |
385 | a_event = bt_notification_event_get_event(a); | |
386 | b_event = bt_notification_event_get_event(b); | |
387 | assert(a_event); | |
388 | assert(b_event); | |
389 | ||
390 | a_stream = bt_ctf_event_get_stream(a_event); | |
391 | assert(a_stream); | |
392 | a_stream_class = bt_ctf_stream_get_class(a_stream); | |
393 | assert(a_stream_class); | |
394 | trace = bt_ctf_stream_class_get_trace(a_stream_class); | |
395 | assert(trace); | |
396 | ||
ac0c6bdd PP |
397 | clock_class = bt_ctf_trace_get_clock_class(trace, 0); |
398 | a_clock_value = bt_ctf_event_get_clock_value(a_event, clock_class); | |
399 | b_clock_value = bt_ctf_event_get_clock_value(b_event, clock_class); | |
a4792757 JG |
400 | assert(a_clock_value); |
401 | assert(b_clock_value); | |
402 | ||
403 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value, &a_ts); | |
404 | assert(!ret); | |
405 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value, &b_ts); | |
406 | assert(!ret); | |
407 | ||
408 | bt_put(a_event); | |
409 | bt_put(b_event); | |
410 | bt_put(a_clock_value); | |
411 | bt_put(b_clock_value); | |
412 | bt_put(a_stream); | |
413 | bt_put(a_stream_class); | |
ac0c6bdd | 414 | bt_put(clock_class); |
a4792757 JG |
415 | bt_put(trace); |
416 | return a_ts < b_ts; | |
417 | } | |
418 | ||
e7a4393b | 419 | static |
5b29e799 JG |
420 | bool compare_notifications(struct bt_notification *a, struct bt_notification *b, |
421 | void *unused) | |
422 | { | |
a4792757 JG |
423 | static int notification_priorities[] = { |
424 | [BT_NOTIFICATION_TYPE_NEW_TRACE] = 0, | |
425 | [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS] = 1, | |
426 | [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS] = 2, | |
ea0e619e | 427 | [BT_NOTIFICATION_TYPE_PACKET_BEGIN] = 3, |
a4792757 JG |
428 | [BT_NOTIFICATION_TYPE_PACKET_END] = 4, |
429 | [BT_NOTIFICATION_TYPE_EVENT] = 5, | |
430 | [BT_NOTIFICATION_TYPE_END_OF_TRACE] = 6, | |
431 | }; | |
432 | int a_prio, b_prio; | |
433 | enum bt_notification_type a_type, b_type; | |
434 | ||
435 | assert(a && b); | |
436 | a_type = bt_notification_get_type(a); | |
437 | b_type = bt_notification_get_type(b); | |
438 | assert(a_type > BT_NOTIFICATION_TYPE_ALL); | |
439 | assert(a_type < BT_NOTIFICATION_TYPE_NR); | |
440 | assert(b_type > BT_NOTIFICATION_TYPE_ALL); | |
441 | assert(b_type < BT_NOTIFICATION_TYPE_NR); | |
442 | ||
443 | a_prio = notification_priorities[a_type]; | |
444 | b_prio = notification_priorities[b_type]; | |
445 | ||
446 | if (likely((a_type == b_type) && a_type == BT_NOTIFICATION_TYPE_EVENT)) { | |
447 | return compare_event_notifications(a, b); | |
448 | } | |
449 | ||
450 | if (unlikely(a_prio != b_prio)) { | |
451 | return a_prio < b_prio; | |
452 | } | |
453 | ||
454 | /* Notification types are equal, but not of type "event". */ | |
455 | switch (a_type) { | |
ea0e619e | 456 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
a4792757 JG |
457 | case BT_NOTIFICATION_TYPE_PACKET_END: |
458 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
459 | { | |
460 | int64_t a_sc_id, b_sc_id; | |
461 | struct bt_ctf_stream *a_stream, *b_stream; | |
462 | struct bt_ctf_stream_class *a_sc, *b_sc; | |
463 | ||
464 | a_stream = internal_bt_notification_get_stream(a); | |
465 | b_stream = internal_bt_notification_get_stream(b); | |
466 | assert(a_stream && b_stream); | |
467 | ||
468 | a_sc = bt_ctf_stream_get_class(a_stream); | |
469 | b_sc = bt_ctf_stream_get_class(b_stream); | |
470 | assert(a_sc && b_sc); | |
471 | ||
472 | a_sc_id = bt_ctf_stream_class_get_id(a_sc); | |
473 | b_sc_id = bt_ctf_stream_class_get_id(b_sc); | |
474 | assert(a_sc_id >= 0 && b_sc_id >= 0); | |
475 | bt_put(a_sc); | |
476 | bt_put(a_stream); | |
477 | bt_put(b_sc); | |
478 | bt_put(b_stream); | |
479 | return a_sc_id < b_sc_id; | |
480 | } | |
481 | case BT_NOTIFICATION_TYPE_NEW_TRACE: | |
482 | case BT_NOTIFICATION_TYPE_END_OF_TRACE: | |
483 | /* Impossible to have two separate traces. */ | |
484 | default: | |
485 | assert(0); | |
486 | } | |
487 | ||
488 | assert(0); | |
5b29e799 JG |
489 | return a < b; |
490 | } | |
491 | ||
492 | static | |
493 | void stream_destroy(void *stream) | |
494 | { | |
495 | ctf_fs_stream_destroy((struct ctf_fs_stream *) stream); | |
496 | } | |
497 | ||
498 | static | |
499 | int open_trace_streams(struct ctf_fs_component *ctf_fs, | |
500 | struct ctf_fs_iterator *ctf_it) | |
e7a4393b JG |
501 | { |
502 | int ret = 0; | |
503 | const char *name; | |
504 | GError *error = NULL; | |
505 | GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error); | |
506 | ||
507 | if (!dir) { | |
508 | PERR("Cannot open directory \"%s\": %s (code %d)\n", | |
509 | ctf_fs->trace_path->str, error->message, | |
510 | error->code); | |
511 | goto error; | |
512 | } | |
513 | ||
514 | while ((name = g_dir_read_name(dir))) { | |
515 | struct ctf_fs_file *file = NULL; | |
516 | struct ctf_fs_stream *stream = NULL; | |
517 | ||
518 | if (!strcmp(name, CTF_FS_METADATA_FILENAME)) { | |
519 | /* Ignore the metadata stream. */ | |
520 | PDBG("Ignoring metadata file \"%s\"\n", | |
521 | name); | |
522 | continue; | |
523 | } | |
524 | ||
525 | if (name[0] == '.') { | |
526 | PDBG("Ignoring hidden file \"%s\"\n", | |
527 | name); | |
528 | continue; | |
529 | } | |
530 | ||
531 | /* Create the file. */ | |
532 | file = ctf_fs_file_create(ctf_fs); | |
533 | if (!file) { | |
534 | PERR("Cannot create stream file object\n"); | |
535 | goto error; | |
536 | } | |
537 | ||
538 | /* Create full path string. */ | |
539 | g_string_append_printf(file->path, "%s/%s", | |
540 | ctf_fs->trace_path->str, name); | |
541 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { | |
542 | PDBG("Ignoring non-regular file \"%s\"\n", name); | |
543 | ctf_fs_file_destroy(file); | |
544 | continue; | |
545 | } | |
546 | ||
547 | /* Open the file. */ | |
548 | if (ctf_fs_file_open(ctf_fs, file, "rb")) { | |
549 | ctf_fs_file_destroy(file); | |
550 | goto error; | |
551 | } | |
552 | ||
9fa0891a JG |
553 | if (file->size == 0) { |
554 | /* Skip empty stream. */ | |
555 | ctf_fs_file_destroy(file); | |
556 | continue; | |
557 | } | |
558 | ||
e7a4393b JG |
559 | /* Create a private stream; file ownership is passed to it. */ |
560 | stream = ctf_fs_stream_create(ctf_fs, file); | |
561 | if (!stream) { | |
562 | ctf_fs_file_destroy(file); | |
563 | goto error; | |
564 | } | |
565 | ||
5b29e799 | 566 | g_ptr_array_add(ctf_it->pending_streams, stream); |
e7a4393b JG |
567 | } |
568 | ||
569 | goto end; | |
570 | error: | |
571 | ret = -1; | |
572 | end: | |
573 | if (dir) { | |
574 | g_dir_close(dir); | |
575 | dir = NULL; | |
576 | } | |
577 | if (error) { | |
578 | g_error_free(error); | |
579 | } | |
580 | return ret; | |
581 | } | |
582 | ||
d3eb6e8f | 583 | enum bt_notification_iterator_status ctf_fs_iterator_init(struct bt_component *source, |
5b29e799 | 584 | struct bt_notification_iterator *it) |
e7a4393b | 585 | { |
5b29e799 JG |
586 | struct ctf_fs_iterator *ctf_it; |
587 | struct ctf_fs_component *ctf_fs; | |
d3eb6e8f | 588 | enum bt_notification_iterator_status ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
5b29e799 JG |
589 | |
590 | assert(source && it); | |
591 | ||
592 | ctf_fs = bt_component_get_private_data(source); | |
593 | if (!ctf_fs) { | |
d3eb6e8f | 594 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
5b29e799 JG |
595 | goto end; |
596 | } | |
597 | ||
598 | ctf_it = g_new0(struct ctf_fs_iterator, 1); | |
599 | if (!ctf_it) { | |
d3eb6e8f | 600 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
5b29e799 JG |
601 | goto end; |
602 | } | |
603 | ||
604 | ctf_it->stream_ht = g_hash_table_new_full(g_direct_hash, | |
605 | g_direct_equal, bt_put, stream_destroy); | |
606 | if (!ctf_it->stream_ht) { | |
607 | goto error; | |
608 | } | |
609 | ctf_it->pending_streams = g_ptr_array_new_with_free_func( | |
610 | stream_destroy); | |
611 | if (!ctf_it->pending_streams) { | |
612 | goto error; | |
613 | } | |
614 | ctf_it->pending_notifications = bt_notification_heap_create( | |
615 | compare_notifications, NULL); | |
616 | if (!ctf_it->pending_notifications) { | |
617 | goto error; | |
618 | } | |
619 | ||
620 | ret = open_trace_streams(ctf_fs, ctf_it); | |
621 | if (ret) { | |
622 | goto error; | |
623 | } | |
624 | ||
5b29e799 JG |
625 | ret = bt_notification_iterator_set_private_data(it, ctf_it); |
626 | if (ret) { | |
627 | goto error; | |
628 | } | |
f48bc732 | 629 | |
5b29e799 JG |
630 | end: |
631 | return ret; | |
632 | error: | |
633 | (void) bt_notification_iterator_set_private_data(it, NULL); | |
634 | ctf_fs_iterator_destroy_data(ctf_it); | |
635 | goto end; | |
636 | } | |
637 | ||
638 | static | |
639 | void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs) | |
640 | { | |
fa5a772d JG |
641 | if (!ctf_fs) { |
642 | return; | |
643 | } | |
5b29e799 JG |
644 | if (ctf_fs->trace_path) { |
645 | g_string_free(ctf_fs->trace_path, TRUE); | |
646 | } | |
647 | if (ctf_fs->metadata) { | |
648 | ctf_fs_metadata_fini(ctf_fs->metadata); | |
649 | g_free(ctf_fs->metadata); | |
650 | } | |
651 | g_free(ctf_fs); | |
652 | } | |
653 | ||
5b29e799 JG |
654 | void ctf_fs_destroy(struct bt_component *component) |
655 | { | |
656 | void *data = bt_component_get_private_data(component); | |
657 | ||
658 | ctf_fs_destroy_data(data); | |
e7a4393b JG |
659 | } |
660 | ||
56a1cced JG |
661 | static |
662 | struct ctf_fs_component *ctf_fs_create(struct bt_value *params) | |
663 | { | |
664 | struct ctf_fs_component *ctf_fs; | |
1ef09eb5 | 665 | struct bt_value *value = NULL; |
56a1cced JG |
666 | const char *path; |
667 | enum bt_value_status ret; | |
668 | ||
669 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
670 | if (!ctf_fs) { | |
671 | goto end; | |
672 | } | |
673 | ||
674 | /* FIXME: should probably look for a source URI */ | |
675 | value = bt_value_map_get(params, "path"); | |
676 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
677 | goto error; | |
678 | } | |
679 | ||
680 | ret = bt_value_string_get(value, &path); | |
681 | if (ret != BT_VALUE_STATUS_OK) { | |
682 | goto error; | |
683 | } | |
684 | ||
685 | ctf_fs->trace_path = g_string_new(path); | |
686 | if (!ctf_fs->trace_path) { | |
687 | goto error; | |
688 | } | |
56a1cced JG |
689 | ctf_fs->error_fp = stderr; |
690 | ctf_fs->page_size = (size_t) getpagesize(); | |
e7a4393b JG |
691 | |
692 | // FIXME: check error. | |
5b29e799 JG |
693 | ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1); |
694 | if (!ctf_fs->metadata) { | |
e7a4393b JG |
695 | goto error; |
696 | } | |
5b29e799 | 697 | ctf_fs_metadata_set_trace(ctf_fs); |
1ef09eb5 JG |
698 | goto end; |
699 | ||
56a1cced JG |
700 | error: |
701 | ctf_fs_destroy_data(ctf_fs); | |
e7a4393b | 702 | ctf_fs = NULL; |
1ef09eb5 JG |
703 | end: |
704 | BT_PUT(value); | |
56a1cced JG |
705 | return ctf_fs; |
706 | } | |
707 | ||
ea0b4b9e JG |
708 | BT_HIDDEN |
709 | enum bt_component_status ctf_fs_init(struct bt_component *source, | |
7d61fa8e | 710 | struct bt_value *params, UNUSED_VAR void *init_method_data) |
ea0b4b9e JG |
711 | { |
712 | struct ctf_fs_component *ctf_fs; | |
713 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
714 | ||
715 | assert(source); | |
716 | ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0; | |
717 | ctf_fs = ctf_fs_create(params); | |
718 | if (!ctf_fs) { | |
719 | ret = BT_COMPONENT_STATUS_NOMEM; | |
720 | goto end; | |
721 | } | |
4c1456f0 | 722 | |
ea0b4b9e JG |
723 | ret = bt_component_set_private_data(source, ctf_fs); |
724 | if (ret != BT_COMPONENT_STATUS_OK) { | |
725 | goto error; | |
726 | } | |
ea0b4b9e JG |
727 | end: |
728 | return ret; | |
729 | error: | |
730 | (void) bt_component_set_private_data(source, NULL); | |
760051fa | 731 | ctf_fs_destroy_data(ctf_fs); |
ea0b4b9e JG |
732 | return ret; |
733 | } |