Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | /* |
2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> | |
fc9a526c | 3 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
e98a2d6e PP |
4 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation |
5 | * | |
e98a2d6e PP |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdint.h> | |
27 | #include <stdlib.h> | |
28 | #include <stdbool.h> | |
29 | #include <glib.h> | |
30 | #include <inttypes.h> | |
31 | #include <sys/mman.h> | |
32 | #include <babeltrace/ctf-ir/stream.h> | |
b2e0c907 | 33 | #include <babeltrace/graph/notification-iterator.h> |
4f1f88a6 PP |
34 | #include <babeltrace/graph/notification-stream.h> |
35 | #include <babeltrace/graph/notification-event.h> | |
36 | #include <babeltrace/graph/notification-packet.h> | |
78586d8a JG |
37 | #include "file.h" |
38 | #include "metadata.h" | |
39 | #include "../common/notif-iter/notif-iter.h" | |
40 | #include <assert.h> | |
5b29e799 | 41 | #include "data-stream.h" |
e98a2d6e PP |
42 | |
43 | #define PRINT_ERR_STREAM ctf_fs->error_fp | |
44 | #define PRINT_PREFIX "ctf-fs-data-stream" | |
cd00e1d3 MD |
45 | #define PRINT_DBG_CHECK ctf_fs_debug |
46 | #include "../print.h" | |
e98a2d6e | 47 | |
4f1f88a6 | 48 | static inline |
5b29e799 | 49 | size_t remaining_mmap_bytes(struct ctf_fs_stream *stream) |
e98a2d6e | 50 | { |
fc9a526c | 51 | return stream->mmap_valid_len - stream->request_offset; |
e98a2d6e PP |
52 | } |
53 | ||
5b29e799 JG |
54 | static |
55 | int stream_munmap(struct ctf_fs_stream *stream) | |
e98a2d6e | 56 | { |
fc9a526c | 57 | int ret = 0; |
56a1cced | 58 | struct ctf_fs_component *ctf_fs = stream->file->ctf_fs; |
e98a2d6e PP |
59 | |
60 | if (munmap(stream->mmap_addr, stream->mmap_len)) { | |
61 | PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n", | |
62 | stream->mmap_addr, stream->mmap_len, | |
63 | stream->file->path->str, stream->file->fp, | |
64 | strerror(errno)); | |
fc9a526c JG |
65 | ret = -1; |
66 | goto end; | |
e98a2d6e | 67 | } |
fc9a526c JG |
68 | end: |
69 | return ret; | |
e98a2d6e PP |
70 | } |
71 | ||
5b29e799 | 72 | static |
e0f6a64a | 73 | enum bt_ctf_notif_iter_medium_status mmap_next(struct ctf_fs_stream *stream) |
e98a2d6e | 74 | { |
e0f6a64a JG |
75 | enum bt_ctf_notif_iter_medium_status ret = |
76 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
fc9a526c | 77 | struct ctf_fs_component *ctf_fs = stream->file->ctf_fs; |
e98a2d6e PP |
78 | |
79 | /* Unmap old region */ | |
80 | if (stream->mmap_addr) { | |
81 | if (stream_munmap(stream)) { | |
82 | goto error; | |
83 | } | |
84 | ||
fc9a526c JG |
85 | stream->mmap_offset += stream->mmap_valid_len; |
86 | stream->request_offset = 0; | |
e98a2d6e PP |
87 | } |
88 | ||
fc9a526c JG |
89 | stream->mmap_valid_len = MIN(stream->file->size - stream->mmap_offset, |
90 | stream->mmap_max_len); | |
e0f6a64a JG |
91 | if (stream->mmap_valid_len == 0) { |
92 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; | |
93 | goto end; | |
94 | } | |
fc9a526c JG |
95 | /* Round up to next page, assuming page size being a power of 2. */ |
96 | stream->mmap_len = (stream->mmap_valid_len + ctf_fs->page_size - 1) | |
97 | & ~(ctf_fs->page_size - 1); | |
e98a2d6e | 98 | /* Map new region */ |
e0f6a64a | 99 | assert(stream->mmap_len); |
e98a2d6e | 100 | stream->mmap_addr = mmap((void *) 0, stream->mmap_len, |
78586d8a JG |
101 | PROT_READ, MAP_PRIVATE, fileno(stream->file->fp), |
102 | stream->mmap_offset); | |
e98a2d6e PP |
103 | if (stream->mmap_addr == MAP_FAILED) { |
104 | PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n", | |
78586d8a JG |
105 | stream->mmap_len, stream->file->path->str, |
106 | stream->file->fp, stream->mmap_offset, | |
107 | strerror(errno)); | |
e98a2d6e PP |
108 | goto error; |
109 | } | |
110 | ||
111 | goto end; | |
e98a2d6e PP |
112 | error: |
113 | stream_munmap(stream); | |
e0f6a64a | 114 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
115 | end: |
116 | return ret; | |
117 | } | |
118 | ||
5b29e799 JG |
119 | static |
120 | enum bt_ctf_notif_iter_medium_status medop_request_bytes( | |
e98a2d6e PP |
121 | size_t request_sz, uint8_t **buffer_addr, |
122 | size_t *buffer_sz, void *data) | |
123 | { | |
124 | enum bt_ctf_notif_iter_medium_status status = | |
125 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
126 | struct ctf_fs_stream *stream = data; | |
56a1cced | 127 | struct ctf_fs_component *ctf_fs = stream->file->ctf_fs; |
e98a2d6e PP |
128 | |
129 | if (request_sz == 0) { | |
130 | goto end; | |
131 | } | |
132 | ||
e98a2d6e PP |
133 | /* Check if we have at least one memory-mapped byte left */ |
134 | if (remaining_mmap_bytes(stream) == 0) { | |
135 | /* Are we at the end of the file? */ | |
e0f6a64a | 136 | if (stream->mmap_offset >= stream->file->size) { |
e98a2d6e PP |
137 | PDBG("Reached end of file \"%s\" (%p)\n", |
138 | stream->file->path->str, stream->file->fp); | |
139 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; | |
140 | goto end; | |
141 | } | |
142 | ||
e0f6a64a JG |
143 | status = mmap_next(stream); |
144 | switch (status) { | |
145 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK: | |
146 | break; | |
147 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF: | |
148 | goto end; | |
149 | default: | |
e98a2d6e | 150 | PERR("Cannot memory-map next region of file \"%s\" (%p)\n", |
e0f6a64a JG |
151 | stream->file->path->str, |
152 | stream->file->fp); | |
e98a2d6e PP |
153 | goto error; |
154 | } | |
155 | } | |
156 | ||
157 | *buffer_sz = MIN(remaining_mmap_bytes(stream), request_sz); | |
fc9a526c | 158 | *buffer_addr = ((uint8_t *) stream->mmap_addr) + stream->request_offset; |
e98a2d6e PP |
159 | stream->request_offset += *buffer_sz; |
160 | goto end; | |
161 | ||
162 | error: | |
163 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; | |
164 | ||
165 | end: | |
166 | return status; | |
167 | } | |
168 | ||
5b29e799 JG |
169 | static |
170 | struct bt_ctf_stream *medop_get_stream( | |
e98a2d6e PP |
171 | struct bt_ctf_stream_class *stream_class, void *data) |
172 | { | |
173 | struct ctf_fs_stream *fs_stream = data; | |
56a1cced | 174 | struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs; |
e98a2d6e PP |
175 | |
176 | if (!fs_stream->stream) { | |
177 | int64_t id = bt_ctf_stream_class_get_id(stream_class); | |
178 | ||
179 | PDBG("Creating stream out of stream class %" PRId64 "\n", id); | |
180 | fs_stream->stream = bt_ctf_stream_create(stream_class, | |
fc9a526c | 181 | fs_stream->file->path->str); |
e98a2d6e PP |
182 | if (!fs_stream->stream) { |
183 | PERR("Cannot create stream (stream class %" PRId64 ")\n", | |
fc9a526c | 184 | id); |
e98a2d6e PP |
185 | } |
186 | } | |
187 | ||
188 | return fs_stream->stream; | |
189 | } | |
190 | ||
191 | static struct bt_ctf_notif_iter_medium_ops medops = { | |
192 | .request_bytes = medop_request_bytes, | |
193 | .get_stream = medop_get_stream, | |
194 | }; | |
195 | ||
b6c3dcb2 JG |
196 | static |
197 | int build_index_from_idx_file(struct ctf_fs_stream *stream) | |
198 | { | |
199 | int ret = 0; | |
200 | gchar *directory = NULL; | |
201 | gchar *basename = NULL; | |
202 | GString *index_basename = NULL; | |
203 | gchar *index_file_path = NULL; | |
204 | GMappedFile *mapped_file = NULL; | |
205 | gsize filesize; | |
206 | const struct ctf_packet_index_file_hdr *header; | |
207 | const char *mmap_begin, *file_pos; | |
208 | struct index_entry *index; | |
209 | uint64_t total_packets_size = 0; | |
210 | size_t file_index_entry_size; | |
211 | size_t file_entry_count; | |
212 | size_t i; | |
213 | ||
214 | /* Look for index file in relative path index/name.idx. */ | |
215 | basename = g_path_get_basename(stream->file->path->str); | |
216 | if (!basename) { | |
217 | ret = -1; | |
218 | goto end; | |
219 | } | |
220 | ||
221 | directory = g_path_get_dirname(stream->file->path->str); | |
222 | if (!directory) { | |
223 | ret = -1; | |
224 | goto end; | |
225 | } | |
226 | ||
227 | index_basename = g_string_new(basename); | |
228 | if (!index_basename) { | |
229 | ret = -1; | |
230 | goto end; | |
231 | } | |
232 | ||
233 | g_string_append(index_basename, ".idx"); | |
234 | index_file_path = g_build_filename(directory, "index", | |
235 | index_basename->str, NULL); | |
236 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 JG |
237 | if (!mapped_file) { |
238 | ret = -1; | |
239 | goto end; | |
240 | } | |
b6c3dcb2 JG |
241 | filesize = g_mapped_file_get_length(mapped_file); |
242 | if (filesize < sizeof(*header)) { | |
243 | printf_error("Invalid LTTng trace index: file size < header size"); | |
244 | ret = -1; | |
245 | goto end; | |
246 | } | |
247 | ||
248 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
249 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
250 | ||
251 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
252 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
253 | printf_error("Invalid LTTng trace index: \"magic\" validation failed"); | |
254 | ret = -1; | |
255 | goto end; | |
256 | } | |
b6c3dcb2 JG |
257 | |
258 | file_index_entry_size = be32toh(header->packet_index_len); | |
259 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
260 | if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) { | |
261 | printf_error("Invalid index file size; not a multiple of index entry size"); | |
262 | ret = -1; | |
263 | goto end; | |
264 | } | |
265 | ||
266 | stream->index.entries = g_array_sized_new(FALSE, TRUE, | |
267 | sizeof(struct index_entry), file_entry_count); | |
268 | if (!stream->index.entries) { | |
269 | ret = -1; | |
270 | goto end; | |
271 | } | |
272 | index = (struct index_entry *) stream->index.entries->data; | |
273 | for (i = 0; i < file_entry_count; i++) { | |
274 | struct ctf_packet_index *file_index = | |
275 | (struct ctf_packet_index *) file_pos; | |
276 | uint64_t packet_size = be64toh(file_index->packet_size); | |
277 | ||
278 | if (packet_size % CHAR_BIT) { | |
279 | ret = -1; | |
280 | printf_error("Invalid packet size encountered in index file"); | |
281 | goto invalid_index; | |
282 | } | |
283 | ||
284 | /* Convert size in bits to bytes. */ | |
285 | packet_size /= CHAR_BIT; | |
286 | index->packet_size = packet_size; | |
287 | ||
288 | index->offset = be64toh(file_index->offset); | |
289 | if (i != 0 && index->offset < (index - 1)->offset) { | |
290 | printf_error("Invalid, non-monotonic, packet offset encountered in index file"); | |
291 | ret = -1; | |
292 | goto invalid_index; | |
293 | } | |
294 | ||
295 | index->timestamp_begin = be64toh(file_index->timestamp_begin); | |
296 | index->timestamp_end = be64toh(file_index->timestamp_end); | |
297 | if (index->timestamp_end < index->timestamp_begin) { | |
298 | printf_error("Invalid packet time bounds encountered in index file"); | |
299 | ret = -1; | |
300 | goto invalid_index; | |
301 | } | |
302 | ||
303 | total_packets_size += packet_size; | |
304 | file_pos += file_index_entry_size; | |
305 | index++; | |
306 | } | |
307 | ||
308 | /* Validate that the index addresses the complete stream. */ | |
309 | if (stream->file->size != total_packets_size) { | |
310 | printf_error("Invalid index; indexed size != stream file size"); | |
311 | ret = -1; | |
312 | goto invalid_index; | |
313 | } | |
314 | end: | |
315 | g_free(directory); | |
316 | g_free(basename); | |
317 | g_free(index_file_path); | |
06367fa3 JG |
318 | if (index_basename) { |
319 | g_string_free(index_basename, TRUE); | |
320 | } | |
321 | if (mapped_file) { | |
322 | g_mapped_file_unref(mapped_file); | |
323 | } | |
b6c3dcb2 JG |
324 | return ret; |
325 | invalid_index: | |
326 | g_array_free(stream->index.entries, TRUE); | |
327 | goto end; | |
328 | } | |
329 | ||
330 | static | |
331 | int build_index_from_stream(struct ctf_fs_stream *stream) | |
332 | { | |
4f1f88a6 | 333 | return 0; |
b6c3dcb2 JG |
334 | } |
335 | ||
336 | static | |
337 | int init_stream_index(struct ctf_fs_stream *stream) | |
338 | { | |
339 | int ret; | |
340 | ||
341 | ret = build_index_from_idx_file(stream); | |
342 | if (!ret) { | |
343 | goto end; | |
344 | } | |
345 | ||
346 | ret = build_index_from_stream(stream); | |
347 | end: | |
348 | return ret; | |
349 | } | |
350 | ||
e7a4393b JG |
351 | BT_HIDDEN |
352 | struct ctf_fs_stream *ctf_fs_stream_create( | |
4f1f88a6 | 353 | struct ctf_fs_component *ctf_fs, const char *path) |
e98a2d6e | 354 | { |
b6c3dcb2 | 355 | int ret; |
e98a2d6e PP |
356 | struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1); |
357 | ||
358 | if (!stream) { | |
359 | goto error; | |
360 | } | |
361 | ||
4f1f88a6 PP |
362 | stream->file = ctf_fs_file_create(ctf_fs); |
363 | if (!stream->file) { | |
364 | goto error; | |
365 | } | |
366 | ||
0982a26d | 367 | stream->cc_prio_map = bt_get(ctf_fs->cc_prio_map); |
4f1f88a6 PP |
368 | g_string_assign(stream->file->path, path); |
369 | ret = ctf_fs_file_open(ctf_fs, stream->file, "rb"); | |
370 | if (ret) { | |
371 | goto error; | |
372 | } | |
373 | ||
5b29e799 | 374 | stream->notif_iter = bt_ctf_notif_iter_create(ctf_fs->metadata->trace, |
0982a26d | 375 | ctf_fs->page_size, medops, stream, |
599faa1c | 376 | ctf_fs->error_fp); |
e98a2d6e PP |
377 | if (!stream->notif_iter) { |
378 | goto error; | |
379 | } | |
5b29e799 | 380 | |
fc9a526c | 381 | stream->mmap_max_len = ctf_fs->page_size * 2048; |
b6c3dcb2 JG |
382 | ret = init_stream_index(stream); |
383 | if (ret) { | |
384 | goto error; | |
385 | } | |
e98a2d6e | 386 | goto end; |
e98a2d6e | 387 | error: |
78586d8a | 388 | /* Do not touch "borrowed" file. */ |
e98a2d6e PP |
389 | ctf_fs_stream_destroy(stream); |
390 | stream = NULL; | |
e98a2d6e PP |
391 | end: |
392 | return stream; | |
393 | } | |
394 | ||
5b29e799 JG |
395 | BT_HIDDEN |
396 | void ctf_fs_stream_destroy(struct ctf_fs_stream *stream) | |
e98a2d6e | 397 | { |
5b29e799 JG |
398 | if (!stream) { |
399 | return; | |
043e2020 JG |
400 | } |
401 | ||
0982a26d PP |
402 | bt_put(stream->cc_prio_map); |
403 | ||
5b29e799 JG |
404 | if (stream->file) { |
405 | ctf_fs_file_destroy(stream->file); | |
e98a2d6e PP |
406 | } |
407 | ||
5b29e799 JG |
408 | if (stream->stream) { |
409 | BT_PUT(stream->stream); | |
043e2020 | 410 | } |
5b29e799 JG |
411 | |
412 | if (stream->notif_iter) { | |
413 | bt_ctf_notif_iter_destroy(stream->notif_iter); | |
78586d8a | 414 | } |
5b29e799 | 415 | |
b6c3dcb2 JG |
416 | if (stream->index.entries) { |
417 | g_array_free(stream->index.entries, TRUE); | |
418 | } | |
419 | ||
5b29e799 | 420 | g_free(stream); |
e98a2d6e | 421 | } |
4f1f88a6 PP |
422 | |
423 | BT_HIDDEN | |
424 | struct bt_notification_iterator_next_return ctf_fs_stream_next( | |
425 | struct ctf_fs_stream *stream) | |
426 | { | |
427 | enum bt_ctf_notif_iter_status notif_iter_status; | |
428 | struct bt_notification_iterator_next_return ret = { | |
429 | .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR, | |
430 | .notification = NULL, | |
431 | }; | |
432 | ||
433 | if (stream->end_reached) { | |
434 | notif_iter_status = BT_CTF_NOTIF_ITER_STATUS_EOF; | |
435 | goto translate_status; | |
436 | } | |
437 | ||
8915b7a7 PP |
438 | notif_iter_status = bt_ctf_notif_iter_get_next_notification( |
439 | stream->notif_iter, stream->cc_prio_map, &ret.notification); | |
4f1f88a6 PP |
440 | if (notif_iter_status != BT_CTF_NOTIF_ITER_STATUS_OK && |
441 | notif_iter_status != BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
442 | goto translate_status; | |
443 | } | |
444 | ||
445 | /* Should be handled in bt_ctf_notif_iter_get_next_notification. */ | |
446 | if (notif_iter_status == BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
447 | ret.notification = bt_notification_stream_end_create( | |
448 | stream->stream); | |
449 | if (!ret.notification) { | |
450 | notif_iter_status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
451 | goto translate_status; | |
452 | } | |
453 | ||
454 | notif_iter_status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
455 | stream->end_reached = true; | |
456 | } | |
457 | ||
458 | translate_status: | |
459 | switch (notif_iter_status) { | |
460 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
461 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
462 | break; | |
463 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
464 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
465 | break; | |
466 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
467 | /* | |
468 | * Should not make it this far as this is | |
469 | * medium-specific; there is nothing for the user to do | |
470 | * and it should have been handled upstream. | |
471 | */ | |
472 | assert(false); | |
473 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: | |
474 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
475 | default: | |
476 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
477 | break; | |
478 | } | |
479 | ||
480 | return ret; | |
481 | } |