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