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