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 | 579 | enum bt_notification_iterator_status ctf_fs_iterator_init( |
91457551 PP |
580 | struct bt_private_notification_iterator *it, |
581 | struct bt_private_port *port) | |
e7a4393b | 582 | { |
5b29e799 JG |
583 | struct ctf_fs_iterator *ctf_it; |
584 | struct ctf_fs_component *ctf_fs; | |
91457551 PP |
585 | struct bt_private_component *source = |
586 | bt_private_notification_iterator_get_private_component(it); | |
d3eb6e8f | 587 | enum bt_notification_iterator_status ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
5b29e799 JG |
588 | |
589 | assert(source && it); | |
590 | ||
890882ef | 591 | ctf_fs = bt_private_component_get_user_data(source); |
5b29e799 | 592 | if (!ctf_fs) { |
d3eb6e8f | 593 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
5b29e799 JG |
594 | goto end; |
595 | } | |
596 | ||
597 | ctf_it = g_new0(struct ctf_fs_iterator, 1); | |
598 | if (!ctf_it) { | |
d3eb6e8f | 599 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
5b29e799 JG |
600 | goto end; |
601 | } | |
602 | ||
603 | ctf_it->stream_ht = g_hash_table_new_full(g_direct_hash, | |
604 | g_direct_equal, bt_put, stream_destroy); | |
605 | if (!ctf_it->stream_ht) { | |
606 | goto error; | |
607 | } | |
608 | ctf_it->pending_streams = g_ptr_array_new_with_free_func( | |
609 | stream_destroy); | |
610 | if (!ctf_it->pending_streams) { | |
611 | goto error; | |
612 | } | |
613 | ctf_it->pending_notifications = bt_notification_heap_create( | |
614 | compare_notifications, NULL); | |
615 | if (!ctf_it->pending_notifications) { | |
616 | goto error; | |
617 | } | |
618 | ||
619 | ret = open_trace_streams(ctf_fs, ctf_it); | |
620 | if (ret) { | |
621 | goto error; | |
622 | } | |
623 | ||
890882ef | 624 | ret = bt_private_notification_iterator_set_user_data(it, ctf_it); |
5b29e799 JG |
625 | if (ret) { |
626 | goto error; | |
627 | } | |
f48bc732 | 628 | |
91457551 PP |
629 | goto end; |
630 | ||
5b29e799 | 631 | error: |
890882ef | 632 | (void) bt_private_notification_iterator_set_user_data(it, NULL); |
5b29e799 | 633 | ctf_fs_iterator_destroy_data(ctf_it); |
91457551 PP |
634 | |
635 | end: | |
636 | bt_put(source); | |
637 | return ret; | |
5b29e799 JG |
638 | } |
639 | ||
640 | static | |
641 | void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs) | |
642 | { | |
fa5a772d JG |
643 | if (!ctf_fs) { |
644 | return; | |
645 | } | |
5b29e799 JG |
646 | if (ctf_fs->trace_path) { |
647 | g_string_free(ctf_fs->trace_path, TRUE); | |
648 | } | |
649 | if (ctf_fs->metadata) { | |
650 | ctf_fs_metadata_fini(ctf_fs->metadata); | |
651 | g_free(ctf_fs->metadata); | |
652 | } | |
653 | g_free(ctf_fs); | |
654 | } | |
655 | ||
64cadc66 | 656 | void ctf_fs_finalize(struct bt_private_component *component) |
5b29e799 | 657 | { |
890882ef | 658 | void *data = bt_private_component_get_user_data(component); |
5b29e799 JG |
659 | |
660 | ctf_fs_destroy_data(data); | |
e7a4393b JG |
661 | } |
662 | ||
56a1cced JG |
663 | static |
664 | struct ctf_fs_component *ctf_fs_create(struct bt_value *params) | |
665 | { | |
666 | struct ctf_fs_component *ctf_fs; | |
1ef09eb5 | 667 | struct bt_value *value = NULL; |
56a1cced JG |
668 | const char *path; |
669 | enum bt_value_status ret; | |
670 | ||
671 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
672 | if (!ctf_fs) { | |
673 | goto end; | |
674 | } | |
675 | ||
676 | /* FIXME: should probably look for a source URI */ | |
677 | value = bt_value_map_get(params, "path"); | |
678 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
679 | goto error; | |
680 | } | |
681 | ||
682 | ret = bt_value_string_get(value, &path); | |
683 | if (ret != BT_VALUE_STATUS_OK) { | |
684 | goto error; | |
685 | } | |
686 | ||
687 | ctf_fs->trace_path = g_string_new(path); | |
688 | if (!ctf_fs->trace_path) { | |
689 | goto error; | |
690 | } | |
56a1cced JG |
691 | ctf_fs->error_fp = stderr; |
692 | ctf_fs->page_size = (size_t) getpagesize(); | |
e7a4393b JG |
693 | |
694 | // FIXME: check error. | |
5b29e799 JG |
695 | ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1); |
696 | if (!ctf_fs->metadata) { | |
e7a4393b JG |
697 | goto error; |
698 | } | |
5b29e799 | 699 | ctf_fs_metadata_set_trace(ctf_fs); |
1ef09eb5 JG |
700 | goto end; |
701 | ||
56a1cced JG |
702 | error: |
703 | ctf_fs_destroy_data(ctf_fs); | |
e7a4393b | 704 | ctf_fs = NULL; |
1ef09eb5 JG |
705 | end: |
706 | BT_PUT(value); | |
56a1cced JG |
707 | return ctf_fs; |
708 | } | |
709 | ||
ea0b4b9e | 710 | BT_HIDDEN |
890882ef | 711 | enum bt_component_status ctf_fs_init(struct bt_private_component *source, |
7d61fa8e | 712 | struct bt_value *params, UNUSED_VAR void *init_method_data) |
ea0b4b9e JG |
713 | { |
714 | struct ctf_fs_component *ctf_fs; | |
715 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
716 | ||
717 | assert(source); | |
718 | ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0; | |
719 | ctf_fs = ctf_fs_create(params); | |
720 | if (!ctf_fs) { | |
721 | ret = BT_COMPONENT_STATUS_NOMEM; | |
722 | goto end; | |
723 | } | |
4c1456f0 | 724 | |
890882ef | 725 | ret = bt_private_component_set_user_data(source, ctf_fs); |
ea0b4b9e JG |
726 | if (ret != BT_COMPONENT_STATUS_OK) { |
727 | goto error; | |
728 | } | |
ea0b4b9e JG |
729 | end: |
730 | return ret; | |
731 | error: | |
890882ef | 732 | (void) bt_private_component_set_user_data(source, NULL); |
760051fa | 733 | ctf_fs_destroy_data(ctf_fs); |
ea0b4b9e JG |
734 | return ret; |
735 | } | |
33f93973 PP |
736 | |
737 | BT_HIDDEN | |
a67681c1 PP |
738 | struct bt_value *ctf_fs_query(struct bt_component_class *comp_class, |
739 | const char *object, struct bt_value *params) | |
33f93973 PP |
740 | { |
741 | struct bt_value *results = NULL; | |
742 | struct bt_value *path_value = NULL; | |
743 | char *metadata_text = NULL; | |
744 | FILE *metadata_fp = NULL; | |
745 | GString *g_metadata_text = NULL; | |
746 | ||
a67681c1 | 747 | if (strcmp(object, "metadata-info") == 0) { |
33f93973 PP |
748 | int ret; |
749 | int bo; | |
750 | const char *path; | |
751 | bool is_packetized; | |
752 | ||
753 | results = bt_value_map_create(); | |
754 | if (!results) { | |
755 | goto error; | |
756 | } | |
757 | ||
758 | if (!bt_value_is_map(params)) { | |
759 | fprintf(stderr, | |
a67681c1 | 760 | "Query parameters is not a map value object\n"); |
33f93973 PP |
761 | goto error; |
762 | } | |
763 | ||
764 | path_value = bt_value_map_get(params, "path"); | |
765 | ret = bt_value_string_get(path_value, &path); | |
766 | if (ret) { | |
767 | fprintf(stderr, | |
768 | "Cannot get `path` string parameter\n"); | |
769 | goto error; | |
770 | } | |
771 | ||
772 | assert(path); | |
773 | metadata_fp = ctf_fs_metadata_open_file(path); | |
774 | if (!metadata_fp) { | |
775 | fprintf(stderr, | |
776 | "Cannot open trace at path `%s`\n", path); | |
777 | goto error; | |
778 | } | |
779 | ||
780 | is_packetized = ctf_metadata_is_packetized(metadata_fp, &bo); | |
781 | ||
782 | if (is_packetized) { | |
783 | ret = ctf_metadata_packetized_file_to_buf(NULL, | |
784 | metadata_fp, (uint8_t **) &metadata_text, bo); | |
785 | if (ret) { | |
786 | fprintf(stderr, | |
787 | "Cannot decode packetized metadata file\n"); | |
788 | goto error; | |
789 | } | |
790 | } else { | |
791 | long filesize; | |
792 | ||
793 | fseek(metadata_fp, 0, SEEK_END); | |
794 | filesize = ftell(metadata_fp); | |
795 | rewind(metadata_fp); | |
796 | metadata_text = malloc(filesize + 1); | |
797 | if (!metadata_text) { | |
798 | fprintf(stderr, | |
799 | "Cannot allocate buffer for metadata text\n"); | |
800 | goto error; | |
801 | } | |
802 | ||
803 | if (fread(metadata_text, filesize, 1, metadata_fp) != | |
804 | 1) { | |
805 | fprintf(stderr, | |
806 | "Cannot read metadata file\n"); | |
807 | goto error; | |
808 | } | |
809 | ||
810 | metadata_text[filesize] = '\0'; | |
811 | } | |
812 | ||
813 | g_metadata_text = g_string_new(NULL); | |
814 | if (!g_metadata_text) { | |
815 | goto error; | |
816 | } | |
817 | ||
818 | if (strncmp(metadata_text, METADATA_TEXT_SIG, | |
819 | sizeof(METADATA_TEXT_SIG) - 1) != 0) { | |
820 | g_string_assign(g_metadata_text, METADATA_TEXT_SIG); | |
821 | g_string_append(g_metadata_text, " */\n\n"); | |
822 | } | |
823 | ||
824 | g_string_append(g_metadata_text, metadata_text); | |
825 | ||
826 | ret = bt_value_map_insert_string(results, "text", | |
827 | g_metadata_text->str); | |
828 | if (ret) { | |
829 | fprintf(stderr, "Cannot insert metadata text into results\n"); | |
830 | goto error; | |
831 | } | |
832 | ||
833 | ret = bt_value_map_insert_bool(results, "is-packetized", | |
834 | is_packetized); | |
835 | if (ret) { | |
836 | fprintf(stderr, "Cannot insert is packetized into results\n"); | |
837 | goto error; | |
838 | } | |
839 | } else { | |
a67681c1 | 840 | fprintf(stderr, "Unknown query object `%s`\n", object); |
33f93973 PP |
841 | goto error; |
842 | } | |
843 | ||
844 | goto end; | |
845 | ||
846 | error: | |
847 | BT_PUT(results); | |
848 | ||
849 | end: | |
850 | bt_put(path_value); | |
851 | free(metadata_text); | |
852 | ||
853 | if (g_metadata_text) { | |
854 | g_string_free(g_metadata_text, TRUE); | |
855 | } | |
856 | ||
857 | if (metadata_fp) { | |
858 | fclose(metadata_fp); | |
859 | } | |
860 | return results; | |
861 | } |