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