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" | |
38 | #include "../common/notif-iter/notif-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 |
50842bdc | 78 | enum bt_notif_iter_medium_status ds_file_mmap_next( |
94cf822e | 79 | struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 80 | { |
50842bdc PP |
81 | enum bt_notif_iter_medium_status ret = |
82 | BT_NOTIF_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) { |
50842bdc | 102 | ret = BT_NOTIF_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); |
50842bdc | 121 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
122 | end: |
123 | return ret; | |
124 | } | |
125 | ||
5b29e799 | 126 | static |
50842bdc | 127 | enum bt_notif_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 | { | |
50842bdc PP |
131 | enum bt_notif_iter_medium_status status = |
132 | BT_NOTIF_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); |
50842bdc | 145 | status = BT_NOTIF_ITER_MEDIUM_STATUS_EOF; |
e98a2d6e PP |
146 | goto end; |
147 | } | |
148 | ||
94cf822e | 149 | status = ds_file_mmap_next(ds_file); |
e0f6a64a | 150 | switch (status) { |
50842bdc | 151 | case BT_NOTIF_ITER_MEDIUM_STATUS_OK: |
e0f6a64a | 152 | break; |
50842bdc | 153 | case BT_NOTIF_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: | |
50842bdc | 169 | status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
170 | |
171 | end: | |
172 | return status; | |
173 | } | |
174 | ||
5b29e799 | 175 | static |
e5be10ef PP |
176 | struct bt_private_stream *medop_borrow_stream( |
177 | struct bt_private_stream_class *stream_class, int64_t stream_id, | |
b92735af | 178 | void *data) |
e98a2d6e | 179 | { |
94cf822e | 180 | struct ctf_fs_ds_file *ds_file = data; |
e5be10ef PP |
181 | struct bt_private_stream_class *ds_file_stream_class; |
182 | struct bt_private_stream *stream = NULL; | |
183 | ||
28e6ca8b | 184 | ds_file_stream_class = bt_private_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 |
50842bdc PP |
202 | enum bt_notif_iter_medium_status medop_seek( |
203 | enum bt_notif_iter_seek_whence whence, off_t offset, | |
9e0c8dbb JG |
204 | void *data) |
205 | { | |
50842bdc PP |
206 | enum bt_notif_iter_medium_status ret = |
207 | BT_NOTIF_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 | ||
50842bdc | 211 | if (whence != BT_NOTIF_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); | |
50842bdc | 216 | ret = BT_NOTIF_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) { | |
50842bdc | 235 | ret = BT_NOTIF_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); | |
50842bdc | 242 | if (ret != BT_NOTIF_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 |
50842bdc | 255 | struct bt_notif_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 |
e5be10ef | 296 | int convert_cycles_to_ns(struct bt_private_clock_class *clock_class, |
97ade20b | 297 | uint64_t cycles, int64_t *ns) |
b6c3dcb2 | 298 | { |
e5be10ef | 299 | return bt_clock_class_cycles_to_ns_from_origin( |
d94d92ac | 300 | bt_private_clock_class_borrow_clock_class(clock_class), cycles, 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 PP |
322 | struct ctf_stream_class *sc; |
323 | struct bt_notif_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); | |
327 | ||
44c440bc PP |
328 | ret = bt_notif_iter_borrow_packet_header_context_fields( |
329 | ds_file->notif_iter, NULL, NULL); | |
97ade20b | 330 | if (ret) { |
44c440bc PP |
331 | BT_LOGD_STR("Cannot borrow first packet's header and context " |
332 | "fields."); | |
333 | goto error; | |
334 | } | |
335 | ||
336 | ret = bt_notif_iter_get_packet_properties(ds_file->notif_iter, &props); | |
337 | BT_ASSERT(ret == 0); | |
338 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, | |
339 | props.stream_class_id); | |
340 | BT_ASSERT(sc); | |
341 | if (!sc->default_clock_class) { | |
342 | BT_LOGD_STR("Cannot find stream class's default clock class."); | |
97ade20b JG |
343 | goto error; |
344 | } | |
b6c3dcb2 JG |
345 | |
346 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 347 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 348 | if (!basename) { |
97ade20b | 349 | BT_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 350 | ds_file->file->path->str); |
97ade20b | 351 | goto error; |
b6c3dcb2 JG |
352 | } |
353 | ||
94cf822e | 354 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 355 | if (!directory) { |
97ade20b | 356 | BT_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 357 | ds_file->file->path->str); |
97ade20b | 358 | goto error; |
b6c3dcb2 JG |
359 | } |
360 | ||
361 | index_basename = g_string_new(basename); | |
362 | if (!index_basename) { | |
9e0c8dbb | 363 | BT_LOGE_STR("Cannot allocate index file basename string"); |
97ade20b | 364 | goto error; |
b6c3dcb2 JG |
365 | } |
366 | ||
367 | g_string_append(index_basename, ".idx"); | |
368 | index_file_path = g_build_filename(directory, "index", | |
369 | index_basename->str, NULL); | |
370 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 371 | if (!mapped_file) { |
97ade20b | 372 | BT_LOGD("Cannot create new mapped file %s", |
55314f2a | 373 | index_file_path); |
97ade20b | 374 | goto error; |
06367fa3 | 375 | } |
d14940a7 JG |
376 | |
377 | /* | |
378 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
379 | * Traces with such large indexes have never been seen in the wild, | |
380 | * but this would need to be adjusted to support them. | |
381 | */ | |
b6c3dcb2 JG |
382 | filesize = g_mapped_file_get_length(mapped_file); |
383 | if (filesize < sizeof(*header)) { | |
d14940a7 JG |
384 | BT_LOGW("Invalid LTTng trace index file: " |
385 | "file size (%zu bytes) < header size (%zu bytes)", | |
386 | filesize, sizeof(*header)); | |
97ade20b | 387 | goto error; |
b6c3dcb2 JG |
388 | } |
389 | ||
390 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
391 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
392 | ||
393 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
394 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
9e0c8dbb | 395 | BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 396 | goto error; |
b6c3dcb2 | 397 | } |
b6c3dcb2 JG |
398 | |
399 | file_index_entry_size = be32toh(header->packet_index_len); | |
400 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
97ade20b | 401 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
d14940a7 JG |
402 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
403 | "(%zu bytes) is not a multiple of the index entry size " | |
404 | "(%zu bytes)", (filesize - sizeof(*header)), | |
405 | sizeof(*header)); | |
97ade20b | 406 | goto error; |
b6c3dcb2 JG |
407 | } |
408 | ||
97ade20b JG |
409 | index = ctf_fs_ds_index_create(file_entry_count); |
410 | if (!index) { | |
411 | goto error; | |
b6c3dcb2 | 412 | } |
97ade20b JG |
413 | |
414 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( | |
415 | index->entries, struct ctf_fs_ds_index_entry, 0); | |
b6c3dcb2 JG |
416 | for (i = 0; i < file_entry_count; i++) { |
417 | struct ctf_packet_index *file_index = | |
418 | (struct ctf_packet_index *) file_pos; | |
419 | uint64_t packet_size = be64toh(file_index->packet_size); | |
420 | ||
421 | if (packet_size % CHAR_BIT) { | |
d14940a7 | 422 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 423 | goto error; |
b6c3dcb2 JG |
424 | } |
425 | ||
426 | /* Convert size in bits to bytes. */ | |
427 | packet_size /= CHAR_BIT; | |
97ade20b | 428 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 429 | |
97ade20b JG |
430 | index_entry->offset = be64toh(file_index->offset); |
431 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { | |
d14940a7 JG |
432 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
433 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, | |
434 | (index_entry - 1)->offset, index_entry->offset); | |
97ade20b | 435 | goto error; |
b6c3dcb2 JG |
436 | } |
437 | ||
97ade20b JG |
438 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
439 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
440 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
d14940a7 JG |
441 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
442 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, | |
443 | index_entry->timestamp_begin, | |
444 | index_entry->timestamp_end); | |
97ade20b JG |
445 | goto error; |
446 | } | |
447 | ||
448 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
44c440bc | 449 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
450 | index_entry->timestamp_begin, |
451 | &index_entry->timestamp_begin_ns); | |
452 | if (ret) { | |
9e0c8dbb | 453 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
454 | goto error; |
455 | } | |
44c440bc | 456 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
457 | index_entry->timestamp_end, |
458 | &index_entry->timestamp_end_ns); | |
459 | if (ret) { | |
9e0c8dbb | 460 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 461 | goto error; |
b6c3dcb2 JG |
462 | } |
463 | ||
464 | total_packets_size += packet_size; | |
465 | file_pos += file_index_entry_size; | |
97ade20b | 466 | index_entry++; |
b6c3dcb2 JG |
467 | } |
468 | ||
469 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 470 | if (ds_file->file->size != total_packets_size) { |
d14940a7 | 471 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
9e0c8dbb | 472 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
d14940a7 | 473 | ds_file->file->size, total_packets_size); |
97ade20b | 474 | goto error; |
b6c3dcb2 JG |
475 | } |
476 | end: | |
477 | g_free(directory); | |
478 | g_free(basename); | |
479 | g_free(index_file_path); | |
06367fa3 JG |
480 | if (index_basename) { |
481 | g_string_free(index_basename, TRUE); | |
482 | } | |
483 | if (mapped_file) { | |
484 | g_mapped_file_unref(mapped_file); | |
485 | } | |
97ade20b JG |
486 | return index; |
487 | error: | |
488 | ctf_fs_ds_index_destroy(index); | |
489 | index = NULL; | |
b6c3dcb2 JG |
490 | goto end; |
491 | } | |
492 | ||
9e0c8dbb JG |
493 | static |
494 | int init_index_entry(struct ctf_fs_ds_index_entry *entry, | |
44c440bc PP |
495 | struct ctf_fs_ds_file *ds_file, |
496 | struct bt_notif_iter_packet_properties *props, | |
497 | off_t packet_size, off_t packet_offset) | |
9e0c8dbb JG |
498 | { |
499 | int ret; | |
44c440bc | 500 | struct ctf_stream_class *sc; |
9e0c8dbb | 501 | |
44c440bc PP |
502 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
503 | props->stream_class_id); | |
504 | BT_ASSERT(sc); | |
f6ccaed9 | 505 | BT_ASSERT(packet_offset >= 0); |
9e0c8dbb | 506 | entry->offset = packet_offset; |
f6ccaed9 | 507 | BT_ASSERT(packet_size >= 0); |
9e0c8dbb JG |
508 | entry->packet_size = packet_size; |
509 | ||
9e0c8dbb | 510 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
44c440bc PP |
511 | ret = convert_cycles_to_ns(sc->default_clock_class, |
512 | props->snapshots.beginning_clock, | |
9e0c8dbb JG |
513 | &entry->timestamp_begin_ns); |
514 | if (ret) { | |
515 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
516 | goto end; | |
517 | } | |
518 | ||
44c440bc PP |
519 | ret = convert_cycles_to_ns(sc->default_clock_class, |
520 | props->snapshots.end_clock, | |
9e0c8dbb JG |
521 | &entry->timestamp_end_ns); |
522 | if (ret) { | |
523 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
524 | goto end; | |
525 | } | |
0b29603d | 526 | |
9e0c8dbb | 527 | end: |
9e0c8dbb JG |
528 | return ret; |
529 | } | |
530 | ||
531 | static | |
532 | struct ctf_fs_ds_index *build_index_from_stream_file( | |
533 | struct ctf_fs_ds_file *ds_file) | |
534 | { | |
535 | int ret; | |
536 | struct ctf_fs_ds_index *index = NULL; | |
50842bdc | 537 | enum bt_notif_iter_status iter_status; |
9e0c8dbb JG |
538 | |
539 | BT_LOGD("Indexing stream file %s", ds_file->file->path->str); | |
540 | ||
541 | index = ctf_fs_ds_index_create(0); | |
542 | if (!index) { | |
543 | goto error; | |
544 | } | |
545 | ||
546 | do { | |
547 | off_t current_packet_offset; | |
548 | off_t next_packet_offset; | |
44c440bc | 549 | off_t current_packet_size_bytes; |
9e0c8dbb | 550 | struct ctf_fs_ds_index_entry *entry; |
44c440bc | 551 | struct bt_notif_iter_packet_properties props; |
9e0c8dbb | 552 | |
312c056a | 553 | iter_status = bt_notif_iter_borrow_packet_header_context_fields( |
44c440bc | 554 | ds_file->notif_iter, NULL, NULL); |
50842bdc PP |
555 | if (iter_status != BT_NOTIF_ITER_STATUS_OK) { |
556 | if (iter_status == BT_NOTIF_ITER_STATUS_EOF) { | |
9e0c8dbb JG |
557 | break; |
558 | } | |
559 | goto error; | |
560 | } | |
312c056a | 561 | |
44c440bc PP |
562 | ret = bt_notif_iter_get_packet_properties(ds_file->notif_iter, |
563 | &props); | |
564 | BT_ASSERT(ret == 0); | |
565 | ||
9e0c8dbb | 566 | current_packet_offset = |
312c056a | 567 | bt_notif_iter_get_current_packet_offset( |
9e0c8dbb JG |
568 | ds_file->notif_iter); |
569 | if (current_packet_offset < 0) { | |
570 | BT_LOGE_STR("Cannot get the current packet's offset."); | |
571 | goto error; | |
572 | } | |
573 | ||
9e0c8dbb | 574 | current_packet_size_bytes = |
44c440bc | 575 | ((props.exp_packet_total_size + 7) & ~7) / CHAR_BIT; |
9e0c8dbb JG |
576 | |
577 | if (current_packet_offset + current_packet_size_bytes > | |
578 | ds_file->file->size) { | |
579 | BT_LOGW("Invalid packet size reported in file: stream=\"%s\", " | |
580 | "packet-offset=%jd, packet-size-bytes=%jd, " | |
581 | "file-size=%jd", | |
582 | ds_file->file->path->str, | |
583 | current_packet_offset, | |
584 | current_packet_size_bytes, | |
585 | ds_file->file->size); | |
586 | goto error; | |
587 | } | |
588 | ||
589 | next_packet_offset = current_packet_offset + | |
312c056a | 590 | current_packet_size_bytes; |
9e0c8dbb | 591 | BT_LOGD("Seeking to next packet: current-packet-offset=%jd, " |
312c056a PP |
592 | "next-packet-offset=%jd", current_packet_offset, |
593 | next_packet_offset); | |
9e0c8dbb JG |
594 | |
595 | entry = ctf_fs_ds_index_add_new_entry(index); | |
596 | if (!entry) { | |
597 | BT_LOGE_STR("Failed to allocate a new index entry."); | |
598 | goto error; | |
599 | } | |
600 | ||
44c440bc PP |
601 | ret = init_index_entry(entry, ds_file, &props, |
602 | current_packet_size_bytes, current_packet_offset); | |
9e0c8dbb JG |
603 | if (ret) { |
604 | goto error; | |
605 | } | |
606 | ||
50842bdc | 607 | iter_status = bt_notif_iter_seek(ds_file->notif_iter, |
9e0c8dbb | 608 | next_packet_offset); |
50842bdc | 609 | } while (iter_status == BT_NOTIF_ITER_STATUS_OK); |
9e0c8dbb | 610 | |
50842bdc | 611 | if (iter_status != BT_NOTIF_ITER_STATUS_EOF) { |
9e0c8dbb JG |
612 | goto error; |
613 | } | |
312c056a | 614 | |
9e0c8dbb | 615 | end: |
9e0c8dbb | 616 | return index; |
312c056a | 617 | |
9e0c8dbb JG |
618 | error: |
619 | ctf_fs_ds_index_destroy(index); | |
620 | index = NULL; | |
621 | goto end; | |
622 | } | |
623 | ||
e7a4393b | 624 | BT_HIDDEN |
94cf822e PP |
625 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
626 | struct ctf_fs_trace *ctf_fs_trace, | |
d94d92ac | 627 | struct bt_self_notification_iterator *pc_notif_iter, |
f0010051 | 628 | struct bt_notif_iter *notif_iter, |
e5be10ef | 629 | struct bt_private_stream *stream, const char *path) |
e98a2d6e | 630 | { |
b6c3dcb2 | 631 | int ret; |
55314f2a | 632 | const size_t page_size = bt_common_get_page_size(); |
94cf822e | 633 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 634 | |
94cf822e | 635 | if (!ds_file) { |
e98a2d6e PP |
636 | goto error; |
637 | } | |
638 | ||
f0010051 | 639 | ds_file->pc_notif_iter = pc_notif_iter; |
55314f2a | 640 | ds_file->file = ctf_fs_file_create(); |
94cf822e | 641 | if (!ds_file->file) { |
4f1f88a6 PP |
642 | goto error; |
643 | } | |
644 | ||
65300d60 | 645 | ds_file->stream = bt_object_get_ref(stream); |
44c440bc | 646 | ds_file->metadata = ctf_fs_trace->metadata; |
94cf822e | 647 | g_string_assign(ds_file->file->path, path); |
55314f2a | 648 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
649 | if (ret) { |
650 | goto error; | |
651 | } | |
652 | ||
6de92955 | 653 | ds_file->notif_iter = notif_iter; |
50842bdc | 654 | bt_notif_iter_set_medops_data(ds_file->notif_iter, ds_file); |
94cf822e | 655 | if (!ds_file->notif_iter) { |
e98a2d6e PP |
656 | goto error; |
657 | } | |
5b29e799 | 658 | |
55314f2a | 659 | ds_file->mmap_max_len = page_size * 2048; |
94cf822e | 660 | |
e98a2d6e | 661 | goto end; |
1a9f7075 | 662 | |
e98a2d6e | 663 | error: |
78586d8a | 664 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
665 | ctf_fs_ds_file_destroy(ds_file); |
666 | ds_file = NULL; | |
1a9f7075 | 667 | |
e98a2d6e | 668 | end: |
94cf822e | 669 | return ds_file; |
e98a2d6e PP |
670 | } |
671 | ||
97ade20b JG |
672 | BT_HIDDEN |
673 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
674 | struct ctf_fs_ds_file *ds_file) | |
675 | { | |
9e0c8dbb JG |
676 | struct ctf_fs_ds_index *index; |
677 | ||
678 | index = build_index_from_idx_file(ds_file); | |
679 | if (index) { | |
680 | goto end; | |
681 | } | |
682 | ||
683 | BT_LOGD("Failed to build index from .index file; " | |
684 | "falling back to stream indexing."); | |
685 | index = build_index_from_stream_file(ds_file); | |
686 | end: | |
687 | return index; | |
97ade20b JG |
688 | } |
689 | ||
5b29e799 | 690 | BT_HIDDEN |
94cf822e | 691 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 692 | { |
94cf822e | 693 | if (!ds_file) { |
5b29e799 | 694 | return; |
043e2020 JG |
695 | } |
696 | ||
65300d60 | 697 | bt_object_put_ref(ds_file->stream); |
94cf822e | 698 | (void) ds_file_munmap(ds_file); |
0982a26d | 699 | |
94cf822e PP |
700 | if (ds_file->file) { |
701 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
702 | } |
703 | ||
94cf822e | 704 | g_free(ds_file); |
e98a2d6e | 705 | } |
4f1f88a6 PP |
706 | |
707 | BT_HIDDEN | |
d4393e08 PP |
708 | enum bt_notification_iterator_status ctf_fs_ds_file_next( |
709 | struct ctf_fs_ds_file *ds_file, | |
e5be10ef | 710 | struct bt_private_notification **notif) |
4f1f88a6 | 711 | { |
50842bdc | 712 | enum bt_notif_iter_status notif_iter_status; |
d4393e08 | 713 | enum bt_notification_iterator_status status; |
4f1f88a6 | 714 | |
50842bdc | 715 | notif_iter_status = bt_notif_iter_get_next_notification( |
f0010051 | 716 | ds_file->notif_iter, ds_file->pc_notif_iter, notif); |
4f1f88a6 | 717 | |
4f1f88a6 | 718 | switch (notif_iter_status) { |
50842bdc | 719 | case BT_NOTIF_ITER_STATUS_EOF: |
d4393e08 | 720 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
4f1f88a6 | 721 | break; |
50842bdc | 722 | case BT_NOTIF_ITER_STATUS_OK: |
d4393e08 | 723 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
4f1f88a6 | 724 | break; |
50842bdc | 725 | case BT_NOTIF_ITER_STATUS_AGAIN: |
4f1f88a6 PP |
726 | /* |
727 | * Should not make it this far as this is | |
728 | * medium-specific; there is nothing for the user to do | |
729 | * and it should have been handled upstream. | |
730 | */ | |
0fbb9a9f | 731 | abort(); |
50842bdc PP |
732 | case BT_NOTIF_ITER_STATUS_INVAL: |
733 | case BT_NOTIF_ITER_STATUS_ERROR: | |
4f1f88a6 | 734 | default: |
d4393e08 | 735 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
4f1f88a6 PP |
736 | break; |
737 | } | |
d4393e08 | 738 | return status; |
4f1f88a6 | 739 | } |
94cf822e PP |
740 | |
741 | BT_HIDDEN | |
312c056a | 742 | int ctf_fs_ds_file_borrow_packet_header_context_fields( |
97ade20b | 743 | struct ctf_fs_ds_file *ds_file, |
e5be10ef PP |
744 | struct bt_private_field **packet_header_field, |
745 | struct bt_private_field **packet_context_field) | |
94cf822e | 746 | { |
50842bdc | 747 | enum bt_notif_iter_status notif_iter_status; |
94cf822e PP |
748 | int ret = 0; |
749 | ||
f6ccaed9 | 750 | BT_ASSERT(ds_file); |
312c056a | 751 | notif_iter_status = bt_notif_iter_borrow_packet_header_context_fields( |
94cf822e PP |
752 | ds_file->notif_iter, packet_header_field, packet_context_field); |
753 | switch (notif_iter_status) { | |
50842bdc PP |
754 | case BT_NOTIF_ITER_STATUS_EOF: |
755 | case BT_NOTIF_ITER_STATUS_OK: | |
94cf822e | 756 | break; |
50842bdc | 757 | case BT_NOTIF_ITER_STATUS_AGAIN: |
0fbb9a9f | 758 | abort(); |
50842bdc PP |
759 | case BT_NOTIF_ITER_STATUS_INVAL: |
760 | case BT_NOTIF_ITER_STATUS_ERROR: | |
94cf822e PP |
761 | default: |
762 | goto error; | |
763 | break; | |
764 | } | |
765 | ||
766 | goto end; | |
767 | ||
768 | error: | |
769 | ret = -1; | |
770 | ||
94cf822e | 771 | end: |
94cf822e PP |
772 | return ret; |
773 | } | |
97ade20b JG |
774 | |
775 | BT_HIDDEN | |
776 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
777 | { | |
778 | if (!index) { | |
779 | return; | |
780 | } | |
781 | ||
782 | if (index->entries) { | |
783 | g_array_free(index->entries, TRUE); | |
784 | } | |
785 | g_free(index); | |
786 | } |