Commit | Line | Data |
---|---|---|
e98a2d6e | 1 | /* |
94cf822e | 2 | * Copyright 2016-2017 - 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 | ||
0fbb9a9f | 25 | #include <stdlib.h> |
e98a2d6e PP |
26 | #include <stdio.h> |
27 | #include <stdint.h> | |
28 | #include <stdlib.h> | |
29 | #include <stdbool.h> | |
30 | #include <glib.h> | |
31 | #include <inttypes.h> | |
8f76831a | 32 | #include <babeltrace/compat/mman-internal.h> |
e9383dfd | 33 | #include <babeltrace/endian-internal.h> |
9d408fca | 34 | #include <babeltrace/babeltrace.h> |
55314f2a | 35 | #include <babeltrace/common-internal.h> |
78586d8a JG |
36 | #include "file.h" |
37 | #include "metadata.h" | |
d6e69534 | 38 | #include "../common/msg-iter/msg-iter.h" |
f6ccaed9 | 39 | #include <babeltrace/assert-internal.h> |
94cf822e | 40 | #include "data-stream-file.h" |
e9383dfd | 41 | #include <string.h> |
e98a2d6e | 42 | |
55314f2a JG |
43 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS" |
44 | #include "logging.h" | |
e98a2d6e | 45 | |
4f1f88a6 | 46 | static inline |
94cf822e | 47 | size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 48 | { |
2c701ca6 | 49 | return ds_file->mmap_len - ds_file->request_offset; |
e98a2d6e PP |
50 | } |
51 | ||
5b29e799 | 52 | static |
94cf822e | 53 | int ds_file_munmap(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 54 | { |
fc9a526c | 55 | int ret = 0; |
e98a2d6e | 56 | |
d238196d | 57 | if (!ds_file || !ds_file->mmap_addr) { |
94cf822e PP |
58 | goto end; |
59 | } | |
60 | ||
04394229 | 61 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { |
cd232764 JG |
62 | BT_LOGE_ERRNO("Cannot memory-unmap file", |
63 | ": address=%p, size=%zu, file_path=\"%s\", file=%p", | |
94cf822e | 64 | ds_file->mmap_addr, ds_file->mmap_len, |
9e0c8dbb | 65 | ds_file->file ? ds_file->file->path->str : "NULL", |
cd232764 | 66 | ds_file->file ? ds_file->file->fp : NULL); |
fc9a526c JG |
67 | ret = -1; |
68 | goto end; | |
e98a2d6e | 69 | } |
94cf822e PP |
70 | |
71 | ds_file->mmap_addr = NULL; | |
72 | ||
fc9a526c JG |
73 | end: |
74 | return ret; | |
e98a2d6e PP |
75 | } |
76 | ||
5b29e799 | 77 | static |
d6e69534 | 78 | enum bt_msg_iter_medium_status ds_file_mmap_next( |
94cf822e | 79 | struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 80 | { |
d6e69534 PP |
81 | enum bt_msg_iter_medium_status ret = |
82 | BT_MSG_ITER_MEDIUM_STATUS_OK; | |
e98a2d6e PP |
83 | |
84 | /* Unmap old region */ | |
94cf822e PP |
85 | if (ds_file->mmap_addr) { |
86 | if (ds_file_munmap(ds_file)) { | |
e98a2d6e PP |
87 | goto error; |
88 | } | |
89 | ||
2f5a009b | 90 | /* |
2c701ca6 | 91 | * mmap_len is guaranteed to be page-aligned except on the |
2f5a009b JG |
92 | * last mapping where it may not be possible (since the file's |
93 | * size itself may not be a page multiple). | |
94 | */ | |
2c701ca6 | 95 | ds_file->mmap_offset += ds_file->mmap_len; |
94cf822e | 96 | ds_file->request_offset = 0; |
e98a2d6e PP |
97 | } |
98 | ||
2c701ca6 | 99 | ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset, |
94cf822e | 100 | ds_file->mmap_max_len); |
2c701ca6 | 101 | if (ds_file->mmap_len == 0) { |
d6e69534 | 102 | ret = BT_MSG_ITER_MEDIUM_STATUS_EOF; |
e0f6a64a JG |
103 | goto end; |
104 | } | |
e98a2d6e | 105 | /* Map new region */ |
f6ccaed9 | 106 | BT_ASSERT(ds_file->mmap_len); |
04394229 | 107 | ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len, |
94cf822e PP |
108 | PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp), |
109 | ds_file->mmap_offset); | |
110 | if (ds_file->mmap_addr == MAP_FAILED) { | |
1974687e | 111 | BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s", |
94cf822e | 112 | ds_file->mmap_len, ds_file->file->path->str, |
1974687e | 113 | ds_file->file->fp, (intmax_t) ds_file->mmap_offset, |
78586d8a | 114 | strerror(errno)); |
e98a2d6e PP |
115 | goto error; |
116 | } | |
117 | ||
118 | goto end; | |
e98a2d6e | 119 | error: |
94cf822e | 120 | ds_file_munmap(ds_file); |
d6e69534 | 121 | ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
122 | end: |
123 | return ret; | |
124 | } | |
125 | ||
5b29e799 | 126 | static |
d6e69534 | 127 | enum bt_msg_iter_medium_status medop_request_bytes( |
e98a2d6e PP |
128 | size_t request_sz, uint8_t **buffer_addr, |
129 | size_t *buffer_sz, void *data) | |
130 | { | |
d6e69534 PP |
131 | enum bt_msg_iter_medium_status status = |
132 | BT_MSG_ITER_MEDIUM_STATUS_OK; | |
94cf822e | 133 | struct ctf_fs_ds_file *ds_file = data; |
e98a2d6e PP |
134 | |
135 | if (request_sz == 0) { | |
136 | goto end; | |
137 | } | |
138 | ||
e98a2d6e | 139 | /* Check if we have at least one memory-mapped byte left */ |
94cf822e | 140 | if (remaining_mmap_bytes(ds_file) == 0) { |
e98a2d6e | 141 | /* Are we at the end of the file? */ |
94cf822e | 142 | if (ds_file->mmap_offset >= ds_file->file->size) { |
55314f2a | 143 | BT_LOGD("Reached end of file \"%s\" (%p)", |
94cf822e | 144 | ds_file->file->path->str, ds_file->file->fp); |
d6e69534 | 145 | status = BT_MSG_ITER_MEDIUM_STATUS_EOF; |
e98a2d6e PP |
146 | goto end; |
147 | } | |
148 | ||
94cf822e | 149 | status = ds_file_mmap_next(ds_file); |
e0f6a64a | 150 | switch (status) { |
d6e69534 | 151 | case BT_MSG_ITER_MEDIUM_STATUS_OK: |
e0f6a64a | 152 | break; |
d6e69534 | 153 | case BT_MSG_ITER_MEDIUM_STATUS_EOF: |
e0f6a64a JG |
154 | goto end; |
155 | default: | |
55314f2a | 156 | BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)", |
94cf822e PP |
157 | ds_file->file->path->str, |
158 | ds_file->file->fp); | |
e98a2d6e PP |
159 | goto error; |
160 | } | |
161 | } | |
162 | ||
94cf822e PP |
163 | *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz); |
164 | *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset; | |
165 | ds_file->request_offset += *buffer_sz; | |
e98a2d6e PP |
166 | goto end; |
167 | ||
168 | error: | |
d6e69534 | 169 | status = BT_MSG_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
170 | |
171 | end: | |
172 | return status; | |
173 | } | |
174 | ||
5b29e799 | 175 | static |
b19ff26f PP |
176 | bt_stream *medop_borrow_stream( |
177 | bt_stream_class *stream_class, int64_t stream_id, | |
b92735af | 178 | void *data) |
e98a2d6e | 179 | { |
94cf822e | 180 | struct ctf_fs_ds_file *ds_file = data; |
b19ff26f PP |
181 | bt_stream_class *ds_file_stream_class; |
182 | bt_stream *stream = NULL; | |
e5be10ef | 183 | |
40f4ba76 | 184 | ds_file_stream_class = bt_stream_borrow_class( |
e5be10ef | 185 | ds_file->stream); |
94cf822e | 186 | |
94cf822e PP |
187 | if (stream_class != ds_file_stream_class) { |
188 | /* | |
189 | * Not supported: two packets described by two different | |
190 | * stream classes within the same data stream file. | |
191 | */ | |
192 | goto end; | |
e98a2d6e PP |
193 | } |
194 | ||
94cf822e PP |
195 | stream = ds_file->stream; |
196 | ||
197 | end: | |
198 | return stream; | |
e98a2d6e PP |
199 | } |
200 | ||
9e0c8dbb | 201 | static |
d6e69534 PP |
202 | enum bt_msg_iter_medium_status medop_seek( |
203 | enum bt_msg_iter_seek_whence whence, off_t offset, | |
9e0c8dbb JG |
204 | void *data) |
205 | { | |
d6e69534 PP |
206 | enum bt_msg_iter_medium_status ret = |
207 | BT_MSG_ITER_MEDIUM_STATUS_OK; | |
9e0c8dbb JG |
208 | struct ctf_fs_ds_file *ds_file = data; |
209 | off_t file_size = ds_file->file->size; | |
210 | ||
d6e69534 | 211 | if (whence != BT_MSG_ITER_SEEK_WHENCE_SET || |
9e0c8dbb JG |
212 | offset < 0 || offset > file_size) { |
213 | BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, " | |
214 | "file-size=%jd", (int) whence, offset, | |
215 | file_size); | |
d6e69534 | 216 | ret = BT_MSG_ITER_MEDIUM_STATUS_INVAL; |
9e0c8dbb JG |
217 | goto end; |
218 | } | |
219 | ||
220 | /* | |
221 | * Determine whether or not the destination is contained within the | |
222 | * current mapping. | |
223 | */ | |
224 | if (ds_file->mmap_addr && (offset < ds_file->mmap_offset || | |
225 | offset >= ds_file->mmap_offset + ds_file->mmap_len)) { | |
226 | int unmap_ret; | |
227 | off_t offset_in_mapping = offset % bt_common_get_page_size(); | |
228 | ||
229 | BT_LOGD("Medium seek request cannot be accomodated by the current " | |
63489ca2 | 230 | "file mapping: offset=%jd, mmap-offset=%jd, " |
9e0c8dbb JG |
231 | "mmap-len=%zu", offset, ds_file->mmap_offset, |
232 | ds_file->mmap_len); | |
233 | unmap_ret = ds_file_munmap(ds_file); | |
234 | if (unmap_ret) { | |
d6e69534 | 235 | ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR; |
9e0c8dbb JG |
236 | goto end; |
237 | } | |
238 | ||
239 | ds_file->mmap_offset = offset - offset_in_mapping; | |
240 | ds_file->request_offset = offset_in_mapping; | |
241 | ret = ds_file_mmap_next(ds_file); | |
d6e69534 | 242 | if (ret != BT_MSG_ITER_MEDIUM_STATUS_OK) { |
9e0c8dbb JG |
243 | goto end; |
244 | } | |
245 | } else { | |
246 | ds_file->request_offset = offset - ds_file->mmap_offset; | |
247 | } | |
248 | ||
249 | ds_file->end_reached = (offset == file_size); | |
250 | end: | |
251 | return ret; | |
252 | } | |
253 | ||
6de92955 | 254 | BT_HIDDEN |
d6e69534 | 255 | struct bt_msg_iter_medium_ops ctf_fs_ds_file_medops = { |
e98a2d6e | 256 | .request_bytes = medop_request_bytes, |
312c056a | 257 | .borrow_stream = medop_borrow_stream, |
9e0c8dbb | 258 | .seek = medop_seek, |
e98a2d6e | 259 | }; |
6de92955 | 260 | |
97ade20b JG |
261 | static |
262 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length) | |
263 | { | |
264 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); | |
265 | ||
266 | if (!index) { | |
267 | BT_LOGE_STR("Failed to allocate index"); | |
268 | goto error; | |
269 | } | |
270 | ||
271 | index->entries = g_array_sized_new(FALSE, TRUE, | |
272 | sizeof(struct ctf_fs_ds_index_entry), length); | |
273 | if (!index->entries) { | |
274 | BT_LOGE("Failed to allocate %zu index entries.", length); | |
275 | goto error; | |
276 | } | |
277 | g_array_set_size(index->entries, length); | |
278 | end: | |
279 | return index; | |
280 | error: | |
281 | ctf_fs_ds_index_destroy(index); | |
282 | goto end; | |
283 | } | |
e98a2d6e | 284 | |
9e0c8dbb JG |
285 | /* Returns a new, zeroed, index entry. */ |
286 | static | |
287 | struct ctf_fs_ds_index_entry *ctf_fs_ds_index_add_new_entry( | |
288 | struct ctf_fs_ds_index *index) | |
289 | { | |
290 | g_array_set_size(index->entries, index->entries->len + 1); | |
291 | return &g_array_index(index->entries, struct ctf_fs_ds_index_entry, | |
292 | index->entries->len - 1); | |
293 | } | |
294 | ||
97ade20b | 295 | static |
b19ff26f | 296 | int convert_cycles_to_ns(bt_clock_class *clock_class, |
97ade20b | 297 | uint64_t cycles, int64_t *ns) |
b6c3dcb2 | 298 | { |
40f4ba76 PP |
299 | return bt_clock_class_cycles_to_ns_from_origin(clock_class, cycles, |
300 | ns); | |
97ade20b JG |
301 | } |
302 | ||
303 | static | |
304 | struct ctf_fs_ds_index *build_index_from_idx_file( | |
305 | struct ctf_fs_ds_file *ds_file) | |
306 | { | |
307 | int ret; | |
b6c3dcb2 JG |
308 | gchar *directory = NULL; |
309 | gchar *basename = NULL; | |
310 | GString *index_basename = NULL; | |
311 | gchar *index_file_path = NULL; | |
312 | GMappedFile *mapped_file = NULL; | |
313 | gsize filesize; | |
97ade20b JG |
314 | const char *mmap_begin = NULL, *file_pos = NULL; |
315 | const struct ctf_packet_index_file_hdr *header = NULL; | |
316 | struct ctf_fs_ds_index *index = NULL; | |
317 | struct ctf_fs_ds_index_entry *index_entry = NULL; | |
b6c3dcb2 JG |
318 | uint64_t total_packets_size = 0; |
319 | size_t file_index_entry_size; | |
320 | size_t file_entry_count; | |
321 | size_t i; | |
44c440bc | 322 | struct ctf_stream_class *sc; |
d6e69534 | 323 | struct bt_msg_iter_packet_properties props; |
97ade20b JG |
324 | |
325 | BT_LOGD("Building index from .idx file of stream file %s", | |
326 | ds_file->file->path->str); | |
41693723 | 327 | ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); |
97ade20b | 328 | if (ret) { |
41693723 | 329 | BT_LOGD_STR("Cannot read first packet's header and context fields."); |
44c440bc PP |
330 | goto error; |
331 | } | |
332 | ||
44c440bc PP |
333 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
334 | props.stream_class_id); | |
335 | BT_ASSERT(sc); | |
336 | if (!sc->default_clock_class) { | |
337 | BT_LOGD_STR("Cannot find stream class's default clock class."); | |
97ade20b JG |
338 | goto error; |
339 | } | |
b6c3dcb2 JG |
340 | |
341 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 342 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 343 | if (!basename) { |
97ade20b | 344 | BT_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 345 | ds_file->file->path->str); |
97ade20b | 346 | goto error; |
b6c3dcb2 JG |
347 | } |
348 | ||
94cf822e | 349 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 350 | if (!directory) { |
97ade20b | 351 | BT_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 352 | ds_file->file->path->str); |
97ade20b | 353 | goto error; |
b6c3dcb2 JG |
354 | } |
355 | ||
356 | index_basename = g_string_new(basename); | |
357 | if (!index_basename) { | |
9e0c8dbb | 358 | BT_LOGE_STR("Cannot allocate index file basename string"); |
97ade20b | 359 | goto error; |
b6c3dcb2 JG |
360 | } |
361 | ||
362 | g_string_append(index_basename, ".idx"); | |
363 | index_file_path = g_build_filename(directory, "index", | |
364 | index_basename->str, NULL); | |
365 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 366 | if (!mapped_file) { |
97ade20b | 367 | BT_LOGD("Cannot create new mapped file %s", |
55314f2a | 368 | index_file_path); |
97ade20b | 369 | goto error; |
06367fa3 | 370 | } |
d14940a7 JG |
371 | |
372 | /* | |
373 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
374 | * Traces with such large indexes have never been seen in the wild, | |
375 | * but this would need to be adjusted to support them. | |
376 | */ | |
b6c3dcb2 JG |
377 | filesize = g_mapped_file_get_length(mapped_file); |
378 | if (filesize < sizeof(*header)) { | |
d14940a7 JG |
379 | BT_LOGW("Invalid LTTng trace index file: " |
380 | "file size (%zu bytes) < header size (%zu bytes)", | |
381 | filesize, sizeof(*header)); | |
97ade20b | 382 | goto error; |
b6c3dcb2 JG |
383 | } |
384 | ||
385 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
386 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
387 | ||
388 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
389 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
9e0c8dbb | 390 | BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 391 | goto error; |
b6c3dcb2 | 392 | } |
b6c3dcb2 JG |
393 | |
394 | file_index_entry_size = be32toh(header->packet_index_len); | |
395 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
97ade20b | 396 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
d14940a7 JG |
397 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
398 | "(%zu bytes) is not a multiple of the index entry size " | |
399 | "(%zu bytes)", (filesize - sizeof(*header)), | |
400 | sizeof(*header)); | |
97ade20b | 401 | goto error; |
b6c3dcb2 JG |
402 | } |
403 | ||
97ade20b JG |
404 | index = ctf_fs_ds_index_create(file_entry_count); |
405 | if (!index) { | |
406 | goto error; | |
b6c3dcb2 | 407 | } |
97ade20b JG |
408 | |
409 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( | |
410 | index->entries, struct ctf_fs_ds_index_entry, 0); | |
b6c3dcb2 JG |
411 | for (i = 0; i < file_entry_count; i++) { |
412 | struct ctf_packet_index *file_index = | |
413 | (struct ctf_packet_index *) file_pos; | |
414 | uint64_t packet_size = be64toh(file_index->packet_size); | |
415 | ||
416 | if (packet_size % CHAR_BIT) { | |
d14940a7 | 417 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 418 | goto error; |
b6c3dcb2 JG |
419 | } |
420 | ||
421 | /* Convert size in bits to bytes. */ | |
422 | packet_size /= CHAR_BIT; | |
97ade20b | 423 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 424 | |
97ade20b JG |
425 | index_entry->offset = be64toh(file_index->offset); |
426 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { | |
d14940a7 JG |
427 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
428 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, | |
429 | (index_entry - 1)->offset, index_entry->offset); | |
97ade20b | 430 | goto error; |
b6c3dcb2 JG |
431 | } |
432 | ||
97ade20b JG |
433 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
434 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
435 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
d14940a7 JG |
436 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
437 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, | |
438 | index_entry->timestamp_begin, | |
439 | index_entry->timestamp_end); | |
97ade20b JG |
440 | goto error; |
441 | } | |
442 | ||
443 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
44c440bc | 444 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
445 | index_entry->timestamp_begin, |
446 | &index_entry->timestamp_begin_ns); | |
447 | if (ret) { | |
9e0c8dbb | 448 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
449 | goto error; |
450 | } | |
44c440bc | 451 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
452 | index_entry->timestamp_end, |
453 | &index_entry->timestamp_end_ns); | |
454 | if (ret) { | |
9e0c8dbb | 455 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 456 | goto error; |
b6c3dcb2 JG |
457 | } |
458 | ||
459 | total_packets_size += packet_size; | |
460 | file_pos += file_index_entry_size; | |
97ade20b | 461 | index_entry++; |
b6c3dcb2 JG |
462 | } |
463 | ||
464 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 465 | if (ds_file->file->size != total_packets_size) { |
d14940a7 | 466 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
9e0c8dbb | 467 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
d14940a7 | 468 | ds_file->file->size, total_packets_size); |
97ade20b | 469 | goto error; |
b6c3dcb2 JG |
470 | } |
471 | end: | |
472 | g_free(directory); | |
473 | g_free(basename); | |
474 | g_free(index_file_path); | |
06367fa3 JG |
475 | if (index_basename) { |
476 | g_string_free(index_basename, TRUE); | |
477 | } | |
478 | if (mapped_file) { | |
479 | g_mapped_file_unref(mapped_file); | |
480 | } | |
97ade20b JG |
481 | return index; |
482 | error: | |
483 | ctf_fs_ds_index_destroy(index); | |
484 | index = NULL; | |
b6c3dcb2 JG |
485 | goto end; |
486 | } | |
487 | ||
9e0c8dbb JG |
488 | static |
489 | int init_index_entry(struct ctf_fs_ds_index_entry *entry, | |
44c440bc | 490 | struct ctf_fs_ds_file *ds_file, |
d6e69534 | 491 | struct bt_msg_iter_packet_properties *props, |
44c440bc | 492 | off_t packet_size, off_t packet_offset) |
9e0c8dbb JG |
493 | { |
494 | int ret; | |
44c440bc | 495 | struct ctf_stream_class *sc; |
9e0c8dbb | 496 | |
44c440bc PP |
497 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
498 | props->stream_class_id); | |
499 | BT_ASSERT(sc); | |
f6ccaed9 | 500 | BT_ASSERT(packet_offset >= 0); |
9e0c8dbb | 501 | entry->offset = packet_offset; |
f6ccaed9 | 502 | BT_ASSERT(packet_size >= 0); |
9e0c8dbb JG |
503 | entry->packet_size = packet_size; |
504 | ||
9e0c8dbb | 505 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
44c440bc PP |
506 | ret = convert_cycles_to_ns(sc->default_clock_class, |
507 | props->snapshots.beginning_clock, | |
9e0c8dbb JG |
508 | &entry->timestamp_begin_ns); |
509 | if (ret) { | |
510 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
511 | goto end; | |
512 | } | |
513 | ||
44c440bc PP |
514 | ret = convert_cycles_to_ns(sc->default_clock_class, |
515 | props->snapshots.end_clock, | |
9e0c8dbb JG |
516 | &entry->timestamp_end_ns); |
517 | if (ret) { | |
518 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
519 | goto end; | |
520 | } | |
0b29603d | 521 | |
9e0c8dbb | 522 | end: |
9e0c8dbb JG |
523 | return ret; |
524 | } | |
525 | ||
526 | static | |
527 | struct ctf_fs_ds_index *build_index_from_stream_file( | |
528 | struct ctf_fs_ds_file *ds_file) | |
529 | { | |
530 | int ret; | |
531 | struct ctf_fs_ds_index *index = NULL; | |
d6e69534 | 532 | enum bt_msg_iter_status iter_status; |
9e0c8dbb JG |
533 | |
534 | BT_LOGD("Indexing stream file %s", ds_file->file->path->str); | |
535 | ||
536 | index = ctf_fs_ds_index_create(0); | |
537 | if (!index) { | |
538 | goto error; | |
539 | } | |
540 | ||
541 | do { | |
542 | off_t current_packet_offset; | |
543 | off_t next_packet_offset; | |
44c440bc | 544 | off_t current_packet_size_bytes; |
9e0c8dbb | 545 | struct ctf_fs_ds_index_entry *entry; |
d6e69534 | 546 | struct bt_msg_iter_packet_properties props; |
9e0c8dbb | 547 | |
41693723 PP |
548 | iter_status = bt_msg_iter_get_packet_properties( |
549 | ds_file->msg_iter, &props); | |
d6e69534 PP |
550 | if (iter_status != BT_MSG_ITER_STATUS_OK) { |
551 | if (iter_status == BT_MSG_ITER_STATUS_EOF) { | |
9e0c8dbb JG |
552 | break; |
553 | } | |
554 | goto error; | |
555 | } | |
312c056a | 556 | |
9e0c8dbb | 557 | current_packet_offset = |
d6e69534 PP |
558 | bt_msg_iter_get_current_packet_offset( |
559 | ds_file->msg_iter); | |
9e0c8dbb JG |
560 | if (current_packet_offset < 0) { |
561 | BT_LOGE_STR("Cannot get the current packet's offset."); | |
562 | goto error; | |
563 | } | |
564 | ||
9e0c8dbb | 565 | current_packet_size_bytes = |
44c440bc | 566 | ((props.exp_packet_total_size + 7) & ~7) / CHAR_BIT; |
9e0c8dbb JG |
567 | |
568 | if (current_packet_offset + current_packet_size_bytes > | |
569 | ds_file->file->size) { | |
570 | BT_LOGW("Invalid packet size reported in file: stream=\"%s\", " | |
571 | "packet-offset=%jd, packet-size-bytes=%jd, " | |
572 | "file-size=%jd", | |
573 | ds_file->file->path->str, | |
574 | current_packet_offset, | |
575 | current_packet_size_bytes, | |
576 | ds_file->file->size); | |
577 | goto error; | |
578 | } | |
579 | ||
580 | next_packet_offset = current_packet_offset + | |
312c056a | 581 | current_packet_size_bytes; |
9e0c8dbb | 582 | BT_LOGD("Seeking to next packet: current-packet-offset=%jd, " |
312c056a PP |
583 | "next-packet-offset=%jd", current_packet_offset, |
584 | next_packet_offset); | |
9e0c8dbb JG |
585 | |
586 | entry = ctf_fs_ds_index_add_new_entry(index); | |
587 | if (!entry) { | |
588 | BT_LOGE_STR("Failed to allocate a new index entry."); | |
589 | goto error; | |
590 | } | |
591 | ||
44c440bc PP |
592 | ret = init_index_entry(entry, ds_file, &props, |
593 | current_packet_size_bytes, current_packet_offset); | |
9e0c8dbb JG |
594 | if (ret) { |
595 | goto error; | |
596 | } | |
597 | ||
d6e69534 | 598 | iter_status = bt_msg_iter_seek(ds_file->msg_iter, |
9e0c8dbb | 599 | next_packet_offset); |
d6e69534 | 600 | } while (iter_status == BT_MSG_ITER_STATUS_OK); |
9e0c8dbb | 601 | |
d6e69534 | 602 | if (iter_status != BT_MSG_ITER_STATUS_EOF) { |
9e0c8dbb JG |
603 | goto error; |
604 | } | |
312c056a | 605 | |
9e0c8dbb | 606 | end: |
9e0c8dbb | 607 | return index; |
312c056a | 608 | |
9e0c8dbb JG |
609 | error: |
610 | ctf_fs_ds_index_destroy(index); | |
611 | index = NULL; | |
612 | goto end; | |
613 | } | |
614 | ||
e7a4393b | 615 | BT_HIDDEN |
94cf822e PP |
616 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
617 | struct ctf_fs_trace *ctf_fs_trace, | |
d6e69534 PP |
618 | bt_self_message_iterator *pc_msg_iter, |
619 | struct bt_msg_iter *msg_iter, | |
b19ff26f | 620 | bt_stream *stream, const char *path) |
e98a2d6e | 621 | { |
b6c3dcb2 | 622 | int ret; |
55314f2a | 623 | const size_t page_size = bt_common_get_page_size(); |
94cf822e | 624 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 625 | |
94cf822e | 626 | if (!ds_file) { |
e98a2d6e PP |
627 | goto error; |
628 | } | |
629 | ||
d6e69534 | 630 | ds_file->pc_msg_iter = pc_msg_iter; |
55314f2a | 631 | ds_file->file = ctf_fs_file_create(); |
94cf822e | 632 | if (!ds_file->file) { |
4f1f88a6 PP |
633 | goto error; |
634 | } | |
635 | ||
398454ed | 636 | ds_file->stream = stream; |
c5b9b441 | 637 | bt_stream_get_ref(ds_file->stream); |
44c440bc | 638 | ds_file->metadata = ctf_fs_trace->metadata; |
94cf822e | 639 | g_string_assign(ds_file->file->path, path); |
55314f2a | 640 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
641 | if (ret) { |
642 | goto error; | |
643 | } | |
644 | ||
d6e69534 PP |
645 | ds_file->msg_iter = msg_iter; |
646 | bt_msg_iter_set_medops_data(ds_file->msg_iter, ds_file); | |
647 | if (!ds_file->msg_iter) { | |
e98a2d6e PP |
648 | goto error; |
649 | } | |
5b29e799 | 650 | |
55314f2a | 651 | ds_file->mmap_max_len = page_size * 2048; |
94cf822e | 652 | |
e98a2d6e | 653 | goto end; |
1a9f7075 | 654 | |
e98a2d6e | 655 | error: |
78586d8a | 656 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
657 | ctf_fs_ds_file_destroy(ds_file); |
658 | ds_file = NULL; | |
1a9f7075 | 659 | |
e98a2d6e | 660 | end: |
94cf822e | 661 | return ds_file; |
e98a2d6e PP |
662 | } |
663 | ||
97ade20b JG |
664 | BT_HIDDEN |
665 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
666 | struct ctf_fs_ds_file *ds_file) | |
667 | { | |
9e0c8dbb JG |
668 | struct ctf_fs_ds_index *index; |
669 | ||
670 | index = build_index_from_idx_file(ds_file); | |
671 | if (index) { | |
672 | goto end; | |
673 | } | |
674 | ||
675 | BT_LOGD("Failed to build index from .index file; " | |
676 | "falling back to stream indexing."); | |
677 | index = build_index_from_stream_file(ds_file); | |
678 | end: | |
679 | return index; | |
97ade20b JG |
680 | } |
681 | ||
5b29e799 | 682 | BT_HIDDEN |
94cf822e | 683 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 684 | { |
94cf822e | 685 | if (!ds_file) { |
5b29e799 | 686 | return; |
043e2020 JG |
687 | } |
688 | ||
c5b9b441 | 689 | bt_stream_put_ref(ds_file->stream); |
94cf822e | 690 | (void) ds_file_munmap(ds_file); |
0982a26d | 691 | |
94cf822e PP |
692 | if (ds_file->file) { |
693 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
694 | } |
695 | ||
94cf822e | 696 | g_free(ds_file); |
e98a2d6e | 697 | } |
4f1f88a6 PP |
698 | |
699 | BT_HIDDEN | |
4cdfc5e8 | 700 | bt_message_iterator_status ctf_fs_ds_file_next( |
d4393e08 | 701 | struct ctf_fs_ds_file *ds_file, |
d6e69534 | 702 | bt_message **msg) |
4f1f88a6 | 703 | { |
d6e69534 | 704 | enum bt_msg_iter_status msg_iter_status; |
4cdfc5e8 | 705 | bt_message_iterator_status status; |
4f1f88a6 | 706 | |
d6e69534 PP |
707 | msg_iter_status = bt_msg_iter_get_next_message( |
708 | ds_file->msg_iter, ds_file->pc_msg_iter, msg); | |
4f1f88a6 | 709 | |
d6e69534 PP |
710 | switch (msg_iter_status) { |
711 | case BT_MSG_ITER_STATUS_EOF: | |
712 | status = BT_MESSAGE_ITERATOR_STATUS_END; | |
4f1f88a6 | 713 | break; |
d6e69534 PP |
714 | case BT_MSG_ITER_STATUS_OK: |
715 | status = BT_MESSAGE_ITERATOR_STATUS_OK; | |
4f1f88a6 | 716 | break; |
d6e69534 | 717 | case BT_MSG_ITER_STATUS_AGAIN: |
4f1f88a6 PP |
718 | /* |
719 | * Should not make it this far as this is | |
720 | * medium-specific; there is nothing for the user to do | |
721 | * and it should have been handled upstream. | |
722 | */ | |
0fbb9a9f | 723 | abort(); |
d6e69534 PP |
724 | case BT_MSG_ITER_STATUS_INVAL: |
725 | case BT_MSG_ITER_STATUS_ERROR: | |
4f1f88a6 | 726 | default: |
d6e69534 | 727 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
4f1f88a6 PP |
728 | break; |
729 | } | |
d4393e08 | 730 | return status; |
4f1f88a6 | 731 | } |
94cf822e | 732 | |
97ade20b JG |
733 | BT_HIDDEN |
734 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
735 | { | |
736 | if (!index) { | |
737 | return; | |
738 | } | |
739 | ||
740 | if (index->entries) { | |
741 | g_array_free(index->entries, TRUE); | |
742 | } | |
743 | g_free(index); | |
744 | } |