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