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 |
312c056a | 176 | struct bt_stream *medop_borrow_stream( |
44c440bc | 177 | struct 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; |
50842bdc PP |
181 | struct bt_stream_class *ds_file_stream_class; |
182 | struct bt_stream *stream = NULL; | |
94cf822e | 183 | |
0b29603d | 184 | ds_file_stream_class = bt_stream_borrow_class(ds_file->stream); |
94cf822e PP |
185 | if (stream_class != ds_file_stream_class) { |
186 | /* | |
187 | * Not supported: two packets described by two different | |
188 | * stream classes within the same data stream file. | |
189 | */ | |
190 | goto end; | |
e98a2d6e PP |
191 | } |
192 | ||
94cf822e PP |
193 | stream = ds_file->stream; |
194 | ||
195 | end: | |
196 | return stream; | |
e98a2d6e PP |
197 | } |
198 | ||
9e0c8dbb | 199 | static |
50842bdc PP |
200 | enum bt_notif_iter_medium_status medop_seek( |
201 | enum bt_notif_iter_seek_whence whence, off_t offset, | |
9e0c8dbb JG |
202 | void *data) |
203 | { | |
50842bdc PP |
204 | enum bt_notif_iter_medium_status ret = |
205 | BT_NOTIF_ITER_MEDIUM_STATUS_OK; | |
9e0c8dbb JG |
206 | struct ctf_fs_ds_file *ds_file = data; |
207 | off_t file_size = ds_file->file->size; | |
208 | ||
50842bdc | 209 | if (whence != BT_NOTIF_ITER_SEEK_WHENCE_SET || |
9e0c8dbb JG |
210 | offset < 0 || offset > file_size) { |
211 | BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, " | |
212 | "file-size=%jd", (int) whence, offset, | |
213 | file_size); | |
50842bdc | 214 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_INVAL; |
9e0c8dbb JG |
215 | goto end; |
216 | } | |
217 | ||
218 | /* | |
219 | * Determine whether or not the destination is contained within the | |
220 | * current mapping. | |
221 | */ | |
222 | if (ds_file->mmap_addr && (offset < ds_file->mmap_offset || | |
223 | offset >= ds_file->mmap_offset + ds_file->mmap_len)) { | |
224 | int unmap_ret; | |
225 | off_t offset_in_mapping = offset % bt_common_get_page_size(); | |
226 | ||
227 | BT_LOGD("Medium seek request cannot be accomodated by the current " | |
63489ca2 | 228 | "file mapping: offset=%jd, mmap-offset=%jd, " |
9e0c8dbb JG |
229 | "mmap-len=%zu", offset, ds_file->mmap_offset, |
230 | ds_file->mmap_len); | |
231 | unmap_ret = ds_file_munmap(ds_file); | |
232 | if (unmap_ret) { | |
50842bdc | 233 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
9e0c8dbb JG |
234 | goto end; |
235 | } | |
236 | ||
237 | ds_file->mmap_offset = offset - offset_in_mapping; | |
238 | ds_file->request_offset = offset_in_mapping; | |
239 | ret = ds_file_mmap_next(ds_file); | |
50842bdc | 240 | if (ret != BT_NOTIF_ITER_MEDIUM_STATUS_OK) { |
9e0c8dbb JG |
241 | goto end; |
242 | } | |
243 | } else { | |
244 | ds_file->request_offset = offset - ds_file->mmap_offset; | |
245 | } | |
246 | ||
247 | ds_file->end_reached = (offset == file_size); | |
248 | end: | |
249 | return ret; | |
250 | } | |
251 | ||
6de92955 | 252 | BT_HIDDEN |
50842bdc | 253 | struct bt_notif_iter_medium_ops ctf_fs_ds_file_medops = { |
e98a2d6e | 254 | .request_bytes = medop_request_bytes, |
312c056a | 255 | .borrow_stream = medop_borrow_stream, |
9e0c8dbb | 256 | .seek = medop_seek, |
e98a2d6e | 257 | }; |
6de92955 | 258 | |
97ade20b JG |
259 | static |
260 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length) | |
261 | { | |
262 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); | |
263 | ||
264 | if (!index) { | |
265 | BT_LOGE_STR("Failed to allocate index"); | |
266 | goto error; | |
267 | } | |
268 | ||
269 | index->entries = g_array_sized_new(FALSE, TRUE, | |
270 | sizeof(struct ctf_fs_ds_index_entry), length); | |
271 | if (!index->entries) { | |
272 | BT_LOGE("Failed to allocate %zu index entries.", length); | |
273 | goto error; | |
274 | } | |
275 | g_array_set_size(index->entries, length); | |
276 | end: | |
277 | return index; | |
278 | error: | |
279 | ctf_fs_ds_index_destroy(index); | |
280 | goto end; | |
281 | } | |
e98a2d6e | 282 | |
9e0c8dbb JG |
283 | /* Returns a new, zeroed, index entry. */ |
284 | static | |
285 | struct ctf_fs_ds_index_entry *ctf_fs_ds_index_add_new_entry( | |
286 | struct ctf_fs_ds_index *index) | |
287 | { | |
288 | g_array_set_size(index->entries, index->entries->len + 1); | |
289 | return &g_array_index(index->entries, struct ctf_fs_ds_index_entry, | |
290 | index->entries->len - 1); | |
291 | } | |
292 | ||
97ade20b | 293 | static |
50842bdc | 294 | int convert_cycles_to_ns(struct bt_clock_class *clock_class, |
97ade20b | 295 | uint64_t cycles, int64_t *ns) |
b6c3dcb2 | 296 | { |
44c440bc | 297 | return bt_clock_class_cycles_to_ns_from_origin(clock_class, cycles, ns); |
97ade20b JG |
298 | } |
299 | ||
300 | static | |
301 | struct ctf_fs_ds_index *build_index_from_idx_file( | |
302 | struct ctf_fs_ds_file *ds_file) | |
303 | { | |
304 | int ret; | |
b6c3dcb2 JG |
305 | gchar *directory = NULL; |
306 | gchar *basename = NULL; | |
307 | GString *index_basename = NULL; | |
308 | gchar *index_file_path = NULL; | |
309 | GMappedFile *mapped_file = NULL; | |
310 | gsize filesize; | |
97ade20b JG |
311 | const char *mmap_begin = NULL, *file_pos = NULL; |
312 | const struct ctf_packet_index_file_hdr *header = NULL; | |
313 | struct ctf_fs_ds_index *index = NULL; | |
314 | struct ctf_fs_ds_index_entry *index_entry = NULL; | |
b6c3dcb2 JG |
315 | uint64_t total_packets_size = 0; |
316 | size_t file_index_entry_size; | |
317 | size_t file_entry_count; | |
318 | size_t i; | |
44c440bc PP |
319 | struct ctf_stream_class *sc; |
320 | struct bt_notif_iter_packet_properties props; | |
97ade20b JG |
321 | |
322 | BT_LOGD("Building index from .idx file of stream file %s", | |
323 | ds_file->file->path->str); | |
324 | ||
44c440bc PP |
325 | ret = bt_notif_iter_borrow_packet_header_context_fields( |
326 | ds_file->notif_iter, NULL, NULL); | |
97ade20b | 327 | if (ret) { |
44c440bc PP |
328 | BT_LOGD_STR("Cannot borrow first packet's header and context " |
329 | "fields."); | |
330 | goto error; | |
331 | } | |
332 | ||
333 | ret = bt_notif_iter_get_packet_properties(ds_file->notif_iter, &props); | |
334 | BT_ASSERT(ret == 0); | |
335 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, | |
336 | props.stream_class_id); | |
337 | BT_ASSERT(sc); | |
338 | if (!sc->default_clock_class) { | |
339 | BT_LOGD_STR("Cannot find stream class's default clock class."); | |
97ade20b JG |
340 | goto error; |
341 | } | |
b6c3dcb2 JG |
342 | |
343 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 344 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 345 | if (!basename) { |
97ade20b | 346 | BT_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 347 | ds_file->file->path->str); |
97ade20b | 348 | goto error; |
b6c3dcb2 JG |
349 | } |
350 | ||
94cf822e | 351 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 352 | if (!directory) { |
97ade20b | 353 | BT_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 354 | ds_file->file->path->str); |
97ade20b | 355 | goto error; |
b6c3dcb2 JG |
356 | } |
357 | ||
358 | index_basename = g_string_new(basename); | |
359 | if (!index_basename) { | |
9e0c8dbb | 360 | BT_LOGE_STR("Cannot allocate index file basename string"); |
97ade20b | 361 | goto error; |
b6c3dcb2 JG |
362 | } |
363 | ||
364 | g_string_append(index_basename, ".idx"); | |
365 | index_file_path = g_build_filename(directory, "index", | |
366 | index_basename->str, NULL); | |
367 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 368 | if (!mapped_file) { |
97ade20b | 369 | BT_LOGD("Cannot create new mapped file %s", |
55314f2a | 370 | index_file_path); |
97ade20b | 371 | goto error; |
06367fa3 | 372 | } |
d14940a7 JG |
373 | |
374 | /* | |
375 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
376 | * Traces with such large indexes have never been seen in the wild, | |
377 | * but this would need to be adjusted to support them. | |
378 | */ | |
b6c3dcb2 JG |
379 | filesize = g_mapped_file_get_length(mapped_file); |
380 | if (filesize < sizeof(*header)) { | |
d14940a7 JG |
381 | BT_LOGW("Invalid LTTng trace index file: " |
382 | "file size (%zu bytes) < header size (%zu bytes)", | |
383 | filesize, sizeof(*header)); | |
97ade20b | 384 | goto error; |
b6c3dcb2 JG |
385 | } |
386 | ||
387 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
388 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
389 | ||
390 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
391 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
9e0c8dbb | 392 | BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 393 | goto error; |
b6c3dcb2 | 394 | } |
b6c3dcb2 JG |
395 | |
396 | file_index_entry_size = be32toh(header->packet_index_len); | |
397 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
97ade20b | 398 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
d14940a7 JG |
399 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
400 | "(%zu bytes) is not a multiple of the index entry size " | |
401 | "(%zu bytes)", (filesize - sizeof(*header)), | |
402 | sizeof(*header)); | |
97ade20b | 403 | goto error; |
b6c3dcb2 JG |
404 | } |
405 | ||
97ade20b JG |
406 | index = ctf_fs_ds_index_create(file_entry_count); |
407 | if (!index) { | |
408 | goto error; | |
b6c3dcb2 | 409 | } |
97ade20b JG |
410 | |
411 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( | |
412 | index->entries, struct ctf_fs_ds_index_entry, 0); | |
b6c3dcb2 JG |
413 | for (i = 0; i < file_entry_count; i++) { |
414 | struct ctf_packet_index *file_index = | |
415 | (struct ctf_packet_index *) file_pos; | |
416 | uint64_t packet_size = be64toh(file_index->packet_size); | |
417 | ||
418 | if (packet_size % CHAR_BIT) { | |
d14940a7 | 419 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 420 | goto error; |
b6c3dcb2 JG |
421 | } |
422 | ||
423 | /* Convert size in bits to bytes. */ | |
424 | packet_size /= CHAR_BIT; | |
97ade20b | 425 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 426 | |
97ade20b JG |
427 | index_entry->offset = be64toh(file_index->offset); |
428 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { | |
d14940a7 JG |
429 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
430 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, | |
431 | (index_entry - 1)->offset, index_entry->offset); | |
97ade20b | 432 | goto error; |
b6c3dcb2 JG |
433 | } |
434 | ||
97ade20b JG |
435 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
436 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
437 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
d14940a7 JG |
438 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
439 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, | |
440 | index_entry->timestamp_begin, | |
441 | index_entry->timestamp_end); | |
97ade20b JG |
442 | goto error; |
443 | } | |
444 | ||
445 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
44c440bc | 446 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
447 | index_entry->timestamp_begin, |
448 | &index_entry->timestamp_begin_ns); | |
449 | if (ret) { | |
9e0c8dbb | 450 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
451 | goto error; |
452 | } | |
44c440bc | 453 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
454 | index_entry->timestamp_end, |
455 | &index_entry->timestamp_end_ns); | |
456 | if (ret) { | |
9e0c8dbb | 457 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 458 | goto error; |
b6c3dcb2 JG |
459 | } |
460 | ||
461 | total_packets_size += packet_size; | |
462 | file_pos += file_index_entry_size; | |
97ade20b | 463 | index_entry++; |
b6c3dcb2 JG |
464 | } |
465 | ||
466 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 467 | if (ds_file->file->size != total_packets_size) { |
d14940a7 | 468 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
9e0c8dbb | 469 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
d14940a7 | 470 | ds_file->file->size, total_packets_size); |
97ade20b | 471 | goto error; |
b6c3dcb2 JG |
472 | } |
473 | end: | |
474 | g_free(directory); | |
475 | g_free(basename); | |
476 | g_free(index_file_path); | |
06367fa3 JG |
477 | if (index_basename) { |
478 | g_string_free(index_basename, TRUE); | |
479 | } | |
480 | if (mapped_file) { | |
481 | g_mapped_file_unref(mapped_file); | |
482 | } | |
97ade20b JG |
483 | return index; |
484 | error: | |
485 | ctf_fs_ds_index_destroy(index); | |
486 | index = NULL; | |
b6c3dcb2 JG |
487 | goto end; |
488 | } | |
489 | ||
9e0c8dbb JG |
490 | static |
491 | int init_index_entry(struct ctf_fs_ds_index_entry *entry, | |
44c440bc PP |
492 | struct ctf_fs_ds_file *ds_file, |
493 | struct bt_notif_iter_packet_properties *props, | |
494 | off_t packet_size, off_t packet_offset) | |
9e0c8dbb JG |
495 | { |
496 | int ret; | |
44c440bc | 497 | struct ctf_stream_class *sc; |
9e0c8dbb | 498 | |
44c440bc PP |
499 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
500 | props->stream_class_id); | |
501 | BT_ASSERT(sc); | |
f6ccaed9 | 502 | BT_ASSERT(packet_offset >= 0); |
9e0c8dbb | 503 | entry->offset = packet_offset; |
f6ccaed9 | 504 | BT_ASSERT(packet_size >= 0); |
9e0c8dbb JG |
505 | entry->packet_size = packet_size; |
506 | ||
9e0c8dbb | 507 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
44c440bc PP |
508 | ret = convert_cycles_to_ns(sc->default_clock_class, |
509 | props->snapshots.beginning_clock, | |
9e0c8dbb JG |
510 | &entry->timestamp_begin_ns); |
511 | if (ret) { | |
512 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
513 | goto end; | |
514 | } | |
515 | ||
44c440bc PP |
516 | ret = convert_cycles_to_ns(sc->default_clock_class, |
517 | props->snapshots.end_clock, | |
9e0c8dbb JG |
518 | &entry->timestamp_end_ns); |
519 | if (ret) { | |
520 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
521 | goto end; | |
522 | } | |
0b29603d | 523 | |
9e0c8dbb | 524 | end: |
9e0c8dbb JG |
525 | return ret; |
526 | } | |
527 | ||
528 | static | |
529 | struct ctf_fs_ds_index *build_index_from_stream_file( | |
530 | struct ctf_fs_ds_file *ds_file) | |
531 | { | |
532 | int ret; | |
533 | struct ctf_fs_ds_index *index = NULL; | |
50842bdc | 534 | enum bt_notif_iter_status iter_status; |
9e0c8dbb JG |
535 | |
536 | BT_LOGD("Indexing stream file %s", ds_file->file->path->str); | |
537 | ||
538 | index = ctf_fs_ds_index_create(0); | |
539 | if (!index) { | |
540 | goto error; | |
541 | } | |
542 | ||
543 | do { | |
544 | off_t current_packet_offset; | |
545 | off_t next_packet_offset; | |
44c440bc | 546 | off_t current_packet_size_bytes; |
9e0c8dbb | 547 | struct ctf_fs_ds_index_entry *entry; |
44c440bc | 548 | struct bt_notif_iter_packet_properties props; |
9e0c8dbb | 549 | |
312c056a | 550 | iter_status = bt_notif_iter_borrow_packet_header_context_fields( |
44c440bc | 551 | ds_file->notif_iter, NULL, NULL); |
50842bdc PP |
552 | if (iter_status != BT_NOTIF_ITER_STATUS_OK) { |
553 | if (iter_status == BT_NOTIF_ITER_STATUS_EOF) { | |
9e0c8dbb JG |
554 | break; |
555 | } | |
556 | goto error; | |
557 | } | |
312c056a | 558 | |
44c440bc PP |
559 | ret = bt_notif_iter_get_packet_properties(ds_file->notif_iter, |
560 | &props); | |
561 | BT_ASSERT(ret == 0); | |
562 | ||
9e0c8dbb | 563 | current_packet_offset = |
312c056a | 564 | bt_notif_iter_get_current_packet_offset( |
9e0c8dbb JG |
565 | ds_file->notif_iter); |
566 | if (current_packet_offset < 0) { | |
567 | BT_LOGE_STR("Cannot get the current packet's offset."); | |
568 | goto error; | |
569 | } | |
570 | ||
9e0c8dbb | 571 | current_packet_size_bytes = |
44c440bc | 572 | ((props.exp_packet_total_size + 7) & ~7) / CHAR_BIT; |
9e0c8dbb JG |
573 | |
574 | if (current_packet_offset + current_packet_size_bytes > | |
575 | ds_file->file->size) { | |
576 | BT_LOGW("Invalid packet size reported in file: stream=\"%s\", " | |
577 | "packet-offset=%jd, packet-size-bytes=%jd, " | |
578 | "file-size=%jd", | |
579 | ds_file->file->path->str, | |
580 | current_packet_offset, | |
581 | current_packet_size_bytes, | |
582 | ds_file->file->size); | |
583 | goto error; | |
584 | } | |
585 | ||
586 | next_packet_offset = current_packet_offset + | |
312c056a | 587 | current_packet_size_bytes; |
9e0c8dbb | 588 | BT_LOGD("Seeking to next packet: current-packet-offset=%jd, " |
312c056a PP |
589 | "next-packet-offset=%jd", current_packet_offset, |
590 | next_packet_offset); | |
9e0c8dbb JG |
591 | |
592 | entry = ctf_fs_ds_index_add_new_entry(index); | |
593 | if (!entry) { | |
594 | BT_LOGE_STR("Failed to allocate a new index entry."); | |
595 | goto error; | |
596 | } | |
597 | ||
44c440bc PP |
598 | ret = init_index_entry(entry, ds_file, &props, |
599 | current_packet_size_bytes, current_packet_offset); | |
9e0c8dbb JG |
600 | if (ret) { |
601 | goto error; | |
602 | } | |
603 | ||
50842bdc | 604 | iter_status = bt_notif_iter_seek(ds_file->notif_iter, |
9e0c8dbb | 605 | next_packet_offset); |
50842bdc | 606 | } while (iter_status == BT_NOTIF_ITER_STATUS_OK); |
9e0c8dbb | 607 | |
50842bdc | 608 | if (iter_status != BT_NOTIF_ITER_STATUS_EOF) { |
9e0c8dbb JG |
609 | goto error; |
610 | } | |
312c056a | 611 | |
9e0c8dbb | 612 | end: |
9e0c8dbb | 613 | return index; |
312c056a | 614 | |
9e0c8dbb JG |
615 | error: |
616 | ctf_fs_ds_index_destroy(index); | |
617 | index = NULL; | |
618 | goto end; | |
619 | } | |
620 | ||
e7a4393b | 621 | BT_HIDDEN |
94cf822e PP |
622 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
623 | struct ctf_fs_trace *ctf_fs_trace, | |
f0010051 PP |
624 | struct bt_private_connection_private_notification_iterator *pc_notif_iter, |
625 | struct bt_notif_iter *notif_iter, | |
50842bdc | 626 | struct bt_stream *stream, const char *path) |
e98a2d6e | 627 | { |
b6c3dcb2 | 628 | int ret; |
55314f2a | 629 | const size_t page_size = bt_common_get_page_size(); |
94cf822e | 630 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 631 | |
94cf822e | 632 | if (!ds_file) { |
e98a2d6e PP |
633 | goto error; |
634 | } | |
635 | ||
f0010051 | 636 | ds_file->pc_notif_iter = pc_notif_iter; |
55314f2a | 637 | ds_file->file = ctf_fs_file_create(); |
94cf822e | 638 | if (!ds_file->file) { |
4f1f88a6 PP |
639 | goto error; |
640 | } | |
641 | ||
65300d60 | 642 | ds_file->stream = bt_object_get_ref(stream); |
44c440bc | 643 | ds_file->metadata = ctf_fs_trace->metadata; |
94cf822e | 644 | g_string_assign(ds_file->file->path, path); |
55314f2a | 645 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
646 | if (ret) { |
647 | goto error; | |
648 | } | |
649 | ||
6de92955 | 650 | ds_file->notif_iter = notif_iter; |
50842bdc | 651 | bt_notif_iter_set_medops_data(ds_file->notif_iter, ds_file); |
94cf822e | 652 | if (!ds_file->notif_iter) { |
e98a2d6e PP |
653 | goto error; |
654 | } | |
5b29e799 | 655 | |
55314f2a | 656 | ds_file->mmap_max_len = page_size * 2048; |
94cf822e | 657 | |
e98a2d6e | 658 | goto end; |
1a9f7075 | 659 | |
e98a2d6e | 660 | error: |
78586d8a | 661 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
662 | ctf_fs_ds_file_destroy(ds_file); |
663 | ds_file = NULL; | |
1a9f7075 | 664 | |
e98a2d6e | 665 | end: |
94cf822e | 666 | return ds_file; |
e98a2d6e PP |
667 | } |
668 | ||
97ade20b JG |
669 | BT_HIDDEN |
670 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
671 | struct ctf_fs_ds_file *ds_file) | |
672 | { | |
9e0c8dbb JG |
673 | struct ctf_fs_ds_index *index; |
674 | ||
675 | index = build_index_from_idx_file(ds_file); | |
676 | if (index) { | |
677 | goto end; | |
678 | } | |
679 | ||
680 | BT_LOGD("Failed to build index from .index file; " | |
681 | "falling back to stream indexing."); | |
682 | index = build_index_from_stream_file(ds_file); | |
683 | end: | |
684 | return index; | |
97ade20b JG |
685 | } |
686 | ||
5b29e799 | 687 | BT_HIDDEN |
94cf822e | 688 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 689 | { |
94cf822e | 690 | if (!ds_file) { |
5b29e799 | 691 | return; |
043e2020 JG |
692 | } |
693 | ||
65300d60 PP |
694 | bt_object_put_ref(ds_file->cc_prio_map); |
695 | bt_object_put_ref(ds_file->stream); | |
94cf822e | 696 | (void) ds_file_munmap(ds_file); |
0982a26d | 697 | |
94cf822e PP |
698 | if (ds_file->file) { |
699 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
700 | } |
701 | ||
94cf822e | 702 | g_free(ds_file); |
e98a2d6e | 703 | } |
4f1f88a6 PP |
704 | |
705 | BT_HIDDEN | |
d4393e08 PP |
706 | enum bt_notification_iterator_status ctf_fs_ds_file_next( |
707 | struct ctf_fs_ds_file *ds_file, | |
708 | struct bt_notification **notif) | |
4f1f88a6 | 709 | { |
50842bdc | 710 | enum bt_notif_iter_status notif_iter_status; |
d4393e08 | 711 | enum bt_notification_iterator_status status; |
4f1f88a6 | 712 | |
50842bdc | 713 | notif_iter_status = bt_notif_iter_get_next_notification( |
f0010051 | 714 | ds_file->notif_iter, ds_file->pc_notif_iter, notif); |
4f1f88a6 | 715 | |
4f1f88a6 | 716 | switch (notif_iter_status) { |
50842bdc | 717 | case BT_NOTIF_ITER_STATUS_EOF: |
d4393e08 | 718 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
4f1f88a6 | 719 | break; |
50842bdc | 720 | case BT_NOTIF_ITER_STATUS_OK: |
d4393e08 | 721 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
4f1f88a6 | 722 | break; |
50842bdc | 723 | case BT_NOTIF_ITER_STATUS_AGAIN: |
4f1f88a6 PP |
724 | /* |
725 | * Should not make it this far as this is | |
726 | * medium-specific; there is nothing for the user to do | |
727 | * and it should have been handled upstream. | |
728 | */ | |
0fbb9a9f | 729 | abort(); |
50842bdc PP |
730 | case BT_NOTIF_ITER_STATUS_INVAL: |
731 | case BT_NOTIF_ITER_STATUS_ERROR: | |
4f1f88a6 | 732 | default: |
d4393e08 | 733 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
4f1f88a6 PP |
734 | break; |
735 | } | |
d4393e08 | 736 | return status; |
4f1f88a6 | 737 | } |
94cf822e PP |
738 | |
739 | BT_HIDDEN | |
312c056a | 740 | int ctf_fs_ds_file_borrow_packet_header_context_fields( |
97ade20b | 741 | struct ctf_fs_ds_file *ds_file, |
50842bdc PP |
742 | struct bt_field **packet_header_field, |
743 | struct bt_field **packet_context_field) | |
94cf822e | 744 | { |
50842bdc | 745 | enum bt_notif_iter_status notif_iter_status; |
94cf822e PP |
746 | int ret = 0; |
747 | ||
f6ccaed9 | 748 | BT_ASSERT(ds_file); |
312c056a | 749 | notif_iter_status = bt_notif_iter_borrow_packet_header_context_fields( |
94cf822e PP |
750 | ds_file->notif_iter, packet_header_field, packet_context_field); |
751 | switch (notif_iter_status) { | |
50842bdc PP |
752 | case BT_NOTIF_ITER_STATUS_EOF: |
753 | case BT_NOTIF_ITER_STATUS_OK: | |
94cf822e | 754 | break; |
50842bdc | 755 | case BT_NOTIF_ITER_STATUS_AGAIN: |
0fbb9a9f | 756 | abort(); |
50842bdc PP |
757 | case BT_NOTIF_ITER_STATUS_INVAL: |
758 | case BT_NOTIF_ITER_STATUS_ERROR: | |
94cf822e PP |
759 | default: |
760 | goto error; | |
761 | break; | |
762 | } | |
763 | ||
764 | goto end; | |
765 | ||
766 | error: | |
767 | ret = -1; | |
768 | ||
94cf822e | 769 | end: |
94cf822e PP |
770 | return ret; |
771 | } | |
97ade20b JG |
772 | |
773 | BT_HIDDEN | |
774 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
775 | { | |
776 | if (!index) { | |
777 | return; | |
778 | } | |
779 | ||
780 | if (index->entries) { | |
781 | g_array_free(index->entries, TRUE); | |
782 | } | |
783 | g_free(index); | |
784 | } |