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