| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | */ |
| 8 | |
| 9 | #include <glib.h> |
| 10 | #include <inttypes.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <babeltrace2/babeltrace.h> |
| 17 | |
| 18 | #define BT_COMP_LOG_SELF_COMP (self_comp) |
| 19 | #define BT_LOG_OUTPUT_LEVEL (log_level) |
| 20 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS" |
| 21 | #include "logging/comp-logging.h" |
| 22 | |
| 23 | #include "common/assert.h" |
| 24 | #include "common/common.h" |
| 25 | #include "compat/endian.h" |
| 26 | #include "compat/mman.h" |
| 27 | |
| 28 | #include "../common/msg-iter/msg-iter.hpp" |
| 29 | #include "data-stream-file.hpp" |
| 30 | #include "file.hpp" |
| 31 | #include "metadata.hpp" |
| 32 | |
| 33 | static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file) |
| 34 | { |
| 35 | BT_ASSERT_DBG(ds_file->mmap_len >= ds_file->request_offset_in_mapping); |
| 36 | return ds_file->mmap_len - ds_file->request_offset_in_mapping; |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Return true if `offset_in_file` is in the current mapping. |
| 41 | */ |
| 42 | |
| 43 | static bool offset_ist_mapped(struct ctf_fs_ds_file *ds_file, off_t offset_in_file) |
| 44 | { |
| 45 | return offset_in_file >= ds_file->mmap_offset_in_file && |
| 46 | offset_in_file < (ds_file->mmap_offset_in_file + ds_file->mmap_len); |
| 47 | } |
| 48 | |
| 49 | static enum ctf_msg_iter_medium_status ds_file_munmap(struct ctf_fs_ds_file *ds_file) |
| 50 | { |
| 51 | enum ctf_msg_iter_medium_status status; |
| 52 | bt_self_component *self_comp = ds_file->self_comp; |
| 53 | bt_logging_level log_level = ds_file->log_level; |
| 54 | |
| 55 | BT_ASSERT(ds_file); |
| 56 | |
| 57 | if (!ds_file->mmap_addr) { |
| 58 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 59 | goto end; |
| 60 | } |
| 61 | |
| 62 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { |
| 63 | BT_COMP_LOGE_ERRNO("Cannot memory-unmap file", |
| 64 | ": address=%p, size=%zu, file_path=\"%s\", file=%p", ds_file->mmap_addr, |
| 65 | ds_file->mmap_len, ds_file->file ? ds_file->file->path->str : "NULL", |
| 66 | ds_file->file ? ds_file->file->fp : NULL); |
| 67 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 68 | goto end; |
| 69 | } |
| 70 | |
| 71 | ds_file->mmap_addr = NULL; |
| 72 | |
| 73 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 74 | end: |
| 75 | return status; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * mmap a region of `ds_file` such that `requested_offset_in_file` is in the |
| 80 | * mapping. If the currently mmap-ed region already contains |
| 81 | * `requested_offset_in_file`, the mapping is kept. |
| 82 | * |
| 83 | * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`, |
| 84 | * such that the next call to `request_bytes` will return bytes starting at that |
| 85 | * position. |
| 86 | * |
| 87 | * `requested_offset_in_file` must be a valid offset in the file. |
| 88 | */ |
| 89 | static enum ctf_msg_iter_medium_status ds_file_mmap(struct ctf_fs_ds_file *ds_file, |
| 90 | off_t requested_offset_in_file) |
| 91 | { |
| 92 | enum ctf_msg_iter_medium_status status; |
| 93 | bt_self_component *self_comp = ds_file->self_comp; |
| 94 | bt_logging_level log_level = ds_file->log_level; |
| 95 | |
| 96 | /* Ensure the requested offset is in the file range. */ |
| 97 | BT_ASSERT(requested_offset_in_file >= 0); |
| 98 | BT_ASSERT(requested_offset_in_file < ds_file->file->size); |
| 99 | |
| 100 | /* |
| 101 | * If the mapping already contains the requested offset, just adjust |
| 102 | * requested_offset_in_mapping. |
| 103 | */ |
| 104 | if (offset_ist_mapped(ds_file, requested_offset_in_file)) { |
| 105 | ds_file->request_offset_in_mapping = |
| 106 | requested_offset_in_file - ds_file->mmap_offset_in_file; |
| 107 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | /* Unmap old region */ |
| 112 | status = ds_file_munmap(ds_file); |
| 113 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Compute a mapping that has the required alignment properties and |
| 119 | * contains `requested_offset_in_file`. |
| 120 | */ |
| 121 | ds_file->request_offset_in_mapping = |
| 122 | requested_offset_in_file % bt_mmap_get_offset_align_size(ds_file->log_level); |
| 123 | ds_file->mmap_offset_in_file = requested_offset_in_file - ds_file->request_offset_in_mapping; |
| 124 | ds_file->mmap_len = |
| 125 | MIN(ds_file->file->size - ds_file->mmap_offset_in_file, ds_file->mmap_max_len); |
| 126 | |
| 127 | BT_ASSERT(ds_file->mmap_len > 0); |
| 128 | |
| 129 | ds_file->mmap_addr = |
| 130 | bt_mmap(ds_file->mmap_len, PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp), |
| 131 | ds_file->mmap_offset_in_file, ds_file->log_level); |
| 132 | if (ds_file->mmap_addr == MAP_FAILED) { |
| 133 | BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s", |
| 134 | ds_file->mmap_len, ds_file->file->path->str, ds_file->file->fp, |
| 135 | (intmax_t) ds_file->mmap_offset_in_file, strerror(errno)); |
| 136 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 141 | |
| 142 | end: |
| 143 | return status; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Change the mapping of the file to read the region that follows the current |
| 148 | * mapping. |
| 149 | * |
| 150 | * If the file hasn't been mapped yet, then everything (mmap_offset_in_file, |
| 151 | * mmap_len, request_offset_in_mapping) should have the value 0, which will |
| 152 | * result in the beginning of the file getting mapped. |
| 153 | * |
| 154 | * return _EOF if the current mapping is the end of the file. |
| 155 | */ |
| 156 | |
| 157 | static enum ctf_msg_iter_medium_status ds_file_mmap_next(struct ctf_fs_ds_file *ds_file) |
| 158 | { |
| 159 | enum ctf_msg_iter_medium_status status; |
| 160 | |
| 161 | /* |
| 162 | * If we're called, it's because more bytes are requested but we have |
| 163 | * given all the bytes of the current mapping. |
| 164 | */ |
| 165 | BT_ASSERT(ds_file->request_offset_in_mapping == ds_file->mmap_len); |
| 166 | |
| 167 | /* |
| 168 | * If the current mapping coincides with the end of the file, there is |
| 169 | * no next mapping. |
| 170 | */ |
| 171 | if (ds_file->mmap_offset_in_file + ds_file->mmap_len == ds_file->file->size) { |
| 172 | status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
| 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | status = ds_file_mmap(ds_file, ds_file->mmap_offset_in_file + ds_file->mmap_len); |
| 177 | |
| 178 | end: |
| 179 | return status; |
| 180 | } |
| 181 | |
| 182 | static enum ctf_msg_iter_medium_status medop_request_bytes(size_t request_sz, uint8_t **buffer_addr, |
| 183 | size_t *buffer_sz, void *data) |
| 184 | { |
| 185 | enum ctf_msg_iter_medium_status status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 186 | struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data; |
| 187 | bt_self_component *self_comp = ds_file->self_comp; |
| 188 | bt_logging_level log_level = ds_file->log_level; |
| 189 | |
| 190 | BT_ASSERT(request_sz > 0); |
| 191 | |
| 192 | /* |
| 193 | * Check if we have at least one memory-mapped byte left. If we don't, |
| 194 | * mmap the next file. |
| 195 | */ |
| 196 | if (remaining_mmap_bytes(ds_file) == 0) { |
| 197 | /* Are we at the end of the file? */ |
| 198 | if (ds_file->mmap_offset_in_file >= ds_file->file->size) { |
| 199 | BT_COMP_LOGD("Reached end of file \"%s\" (%p)", ds_file->file->path->str, |
| 200 | ds_file->file->fp); |
| 201 | status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
| 202 | goto end; |
| 203 | } |
| 204 | |
| 205 | status = ds_file_mmap_next(ds_file); |
| 206 | switch (status) { |
| 207 | case CTF_MSG_ITER_MEDIUM_STATUS_OK: |
| 208 | break; |
| 209 | case CTF_MSG_ITER_MEDIUM_STATUS_EOF: |
| 210 | goto end; |
| 211 | default: |
| 212 | BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)", |
| 213 | ds_file->file->path->str, ds_file->file->fp); |
| 214 | goto error; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | BT_ASSERT(remaining_mmap_bytes(ds_file) > 0); |
| 219 | *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz); |
| 220 | |
| 221 | BT_ASSERT(ds_file->mmap_addr); |
| 222 | *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping; |
| 223 | |
| 224 | ds_file->request_offset_in_mapping += *buffer_sz; |
| 225 | goto end; |
| 226 | |
| 227 | error: |
| 228 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 229 | |
| 230 | end: |
| 231 | return status; |
| 232 | } |
| 233 | |
| 234 | static bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t, void *data) |
| 235 | { |
| 236 | struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data; |
| 237 | bt_stream_class *ds_file_stream_class; |
| 238 | bt_stream *stream = NULL; |
| 239 | |
| 240 | ds_file_stream_class = bt_stream_borrow_class(ds_file->stream); |
| 241 | |
| 242 | if (stream_class != ds_file_stream_class) { |
| 243 | /* |
| 244 | * Not supported: two packets described by two different |
| 245 | * stream classes within the same data stream file. |
| 246 | */ |
| 247 | goto end; |
| 248 | } |
| 249 | |
| 250 | stream = ds_file->stream; |
| 251 | |
| 252 | end: |
| 253 | return stream; |
| 254 | } |
| 255 | |
| 256 | static enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data) |
| 257 | { |
| 258 | struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data; |
| 259 | |
| 260 | BT_ASSERT(offset >= 0); |
| 261 | BT_ASSERT(offset < ds_file->file->size); |
| 262 | |
| 263 | return ds_file_mmap(ds_file, offset); |
| 264 | } |
| 265 | |
| 266 | struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = { |
| 267 | medop_request_bytes, |
| 268 | medop_seek, |
| 269 | nullptr, |
| 270 | medop_borrow_stream, |
| 271 | }; |
| 272 | |
| 273 | struct ctf_fs_ds_group_medops_data |
| 274 | { |
| 275 | /* Weak, set once at creation time. */ |
| 276 | struct ctf_fs_ds_file_group *ds_file_group; |
| 277 | |
| 278 | /* |
| 279 | * Index (as in element rank) of the index entry of ds_file_groups' |
| 280 | * index we will read next (so, the one after the one we are reading |
| 281 | * right now). |
| 282 | */ |
| 283 | guint next_index_entry_index; |
| 284 | |
| 285 | /* |
| 286 | * File we are currently reading. Changes whenever we switch to |
| 287 | * reading another data file. |
| 288 | * |
| 289 | * Owned by this. |
| 290 | */ |
| 291 | struct ctf_fs_ds_file *file; |
| 292 | |
| 293 | /* Weak, for context / logging / appending causes. */ |
| 294 | bt_self_message_iterator *self_msg_iter; |
| 295 | bt_logging_level log_level; |
| 296 | }; |
| 297 | |
| 298 | static enum ctf_msg_iter_medium_status medop_group_request_bytes(size_t request_sz, |
| 299 | uint8_t **buffer_addr, |
| 300 | size_t *buffer_sz, void *void_data) |
| 301 | { |
| 302 | struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data; |
| 303 | |
| 304 | /* Return bytes from the current file. */ |
| 305 | return medop_request_bytes(request_sz, buffer_addr, buffer_sz, data->file); |
| 306 | } |
| 307 | |
| 308 | static bt_stream *medop_group_borrow_stream(bt_stream_class *stream_class, int64_t stream_id, |
| 309 | void *void_data) |
| 310 | { |
| 311 | struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data; |
| 312 | |
| 313 | return medop_borrow_stream(stream_class, stream_id, data->file); |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Set `data->file` to prepare it to read the packet described |
| 318 | * by `index_entry`. |
| 319 | */ |
| 320 | |
| 321 | static enum ctf_msg_iter_medium_status |
| 322 | ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data *data, |
| 323 | struct ctf_fs_ds_index_entry *index_entry, |
| 324 | bt_self_message_iterator *self_msg_iter, bt_logging_level log_level) |
| 325 | { |
| 326 | enum ctf_msg_iter_medium_status status; |
| 327 | |
| 328 | BT_ASSERT(data); |
| 329 | BT_ASSERT(index_entry); |
| 330 | |
| 331 | /* Check if that file is already the one mapped. */ |
| 332 | if (!data->file || strcmp(index_entry->path, data->file->file->path->str) != 0) { |
| 333 | /* Destroy the previously used file. */ |
| 334 | ctf_fs_ds_file_destroy(data->file); |
| 335 | |
| 336 | /* Create the new file. */ |
| 337 | data->file = |
| 338 | ctf_fs_ds_file_create(data->ds_file_group->ctf_fs_trace, data->ds_file_group->stream, |
| 339 | index_entry->path, log_level); |
| 340 | if (!data->file) { |
| 341 | BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter, "failed to create ctf_fs_ds_file."); |
| 342 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 343 | goto end; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Ensure the right portion of the file will be returned on the next |
| 349 | * request_bytes call. |
| 350 | */ |
| 351 | status = ds_file_mmap(data->file, index_entry->offset); |
| 352 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { |
| 353 | goto end; |
| 354 | } |
| 355 | |
| 356 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 357 | |
| 358 | end: |
| 359 | return status; |
| 360 | } |
| 361 | |
| 362 | static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data) |
| 363 | { |
| 364 | struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data; |
| 365 | struct ctf_fs_ds_index_entry *index_entry; |
| 366 | enum ctf_msg_iter_medium_status status; |
| 367 | |
| 368 | /* If we have gone through all index entries, we are done. */ |
| 369 | if (data->next_index_entry_index >= data->ds_file_group->index->entries->len) { |
| 370 | status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
| 371 | goto end; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * Otherwise, look up the next index entry / packet and prepare it |
| 376 | * for reading. |
| 377 | */ |
| 378 | index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( |
| 379 | data->ds_file_group->index->entries, data->next_index_entry_index); |
| 380 | |
| 381 | status = |
| 382 | ctf_fs_ds_group_medops_set_file(data, index_entry, data->self_msg_iter, data->log_level); |
| 383 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { |
| 384 | goto end; |
| 385 | } |
| 386 | |
| 387 | data->next_index_entry_index++; |
| 388 | |
| 389 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 390 | end: |
| 391 | return status; |
| 392 | } |
| 393 | |
| 394 | void ctf_fs_ds_group_medops_data_destroy(struct ctf_fs_ds_group_medops_data *data) |
| 395 | { |
| 396 | if (!data) { |
| 397 | goto end; |
| 398 | } |
| 399 | |
| 400 | ctf_fs_ds_file_destroy(data->file); |
| 401 | |
| 402 | g_free(data); |
| 403 | |
| 404 | end: |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create( |
| 409 | struct ctf_fs_ds_file_group *ds_file_group, bt_self_message_iterator *self_msg_iter, |
| 410 | bt_logging_level log_level, struct ctf_fs_ds_group_medops_data **out) |
| 411 | { |
| 412 | struct ctf_fs_ds_group_medops_data *data; |
| 413 | enum ctf_msg_iter_medium_status status; |
| 414 | |
| 415 | BT_ASSERT(self_msg_iter); |
| 416 | BT_ASSERT(ds_file_group); |
| 417 | BT_ASSERT(ds_file_group->index); |
| 418 | BT_ASSERT(ds_file_group->index->entries->len > 0); |
| 419 | |
| 420 | data = g_new0(struct ctf_fs_ds_group_medops_data, 1); |
| 421 | if (!data) { |
| 422 | BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter, |
| 423 | "Failed to allocate a struct ctf_fs_ds_group_medops_data"); |
| 424 | status = CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR; |
| 425 | goto error; |
| 426 | } |
| 427 | |
| 428 | data->ds_file_group = ds_file_group; |
| 429 | data->self_msg_iter = self_msg_iter; |
| 430 | data->log_level = log_level; |
| 431 | |
| 432 | /* |
| 433 | * No need to prepare the first file. ctf_msg_iter will call |
| 434 | * switch_packet before reading the first packet, it will be |
| 435 | * done then. |
| 436 | */ |
| 437 | |
| 438 | *out = data; |
| 439 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 440 | goto end; |
| 441 | |
| 442 | error: |
| 443 | ctf_fs_ds_group_medops_data_destroy(data); |
| 444 | |
| 445 | end: |
| 446 | return status; |
| 447 | } |
| 448 | |
| 449 | void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data) |
| 450 | { |
| 451 | data->next_index_entry_index = 0; |
| 452 | } |
| 453 | |
| 454 | struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = { |
| 455 | .request_bytes = medop_group_request_bytes, |
| 456 | |
| 457 | /* |
| 458 | * We don't support seeking using this medops. It would probably be |
| 459 | * possible, but it's not needed at the moment. |
| 460 | */ |
| 461 | .seek = NULL, |
| 462 | |
| 463 | .switch_packet = medop_group_switch_packet, |
| 464 | .borrow_stream = medop_group_borrow_stream, |
| 465 | }; |
| 466 | |
| 467 | static struct ctf_fs_ds_index_entry *ctf_fs_ds_index_entry_create(bt_self_component *self_comp, |
| 468 | bt_logging_level log_level) |
| 469 | { |
| 470 | struct ctf_fs_ds_index_entry *entry; |
| 471 | |
| 472 | entry = g_new0(struct ctf_fs_ds_index_entry, 1); |
| 473 | if (!entry) { |
| 474 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a ctf_fs_ds_index_entry."); |
| 475 | goto end; |
| 476 | } |
| 477 | |
| 478 | entry->packet_seq_num = UINT64_MAX; |
| 479 | |
| 480 | end: |
| 481 | return entry; |
| 482 | } |
| 483 | |
| 484 | static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cycles, int64_t *ns) |
| 485 | { |
| 486 | return bt_util_clock_cycles_to_ns_from_origin(cycles, clock_class->frequency, |
| 487 | clock_class->offset_seconds, |
| 488 | clock_class->offset_cycles, ns); |
| 489 | } |
| 490 | |
| 491 | static struct ctf_fs_ds_index *build_index_from_idx_file(struct ctf_fs_ds_file *ds_file, |
| 492 | struct ctf_fs_ds_file_info *file_info, |
| 493 | struct ctf_msg_iter *msg_iter) |
| 494 | { |
| 495 | int ret; |
| 496 | gchar *directory = NULL; |
| 497 | gchar *basename = NULL; |
| 498 | GString *index_basename = NULL; |
| 499 | gchar *index_file_path = NULL; |
| 500 | GMappedFile *mapped_file = NULL; |
| 501 | gsize filesize; |
| 502 | const char *mmap_begin = NULL, *file_pos = NULL; |
| 503 | const struct ctf_packet_index_file_hdr *header = NULL; |
| 504 | struct ctf_fs_ds_index *index = NULL; |
| 505 | struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL; |
| 506 | uint64_t total_packets_size = 0; |
| 507 | size_t file_index_entry_size; |
| 508 | size_t file_entry_count; |
| 509 | size_t i; |
| 510 | struct ctf_stream_class *sc; |
| 511 | struct ctf_msg_iter_packet_properties props; |
| 512 | uint32_t version_major, version_minor; |
| 513 | bt_self_component *self_comp = ds_file->self_comp; |
| 514 | bt_logging_level log_level = ds_file->log_level; |
| 515 | |
| 516 | BT_COMP_LOGI("Building index from .idx file of stream file %s", ds_file->file->path->str); |
| 517 | ret = ctf_msg_iter_get_packet_properties(msg_iter, &props); |
| 518 | if (ret) { |
| 519 | BT_COMP_LOGI_STR("Cannot read first packet's header and context fields."); |
| 520 | goto error; |
| 521 | } |
| 522 | |
| 523 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id); |
| 524 | BT_ASSERT(sc); |
| 525 | if (!sc->default_clock_class) { |
| 526 | BT_COMP_LOGI_STR("Cannot find stream class's default clock class."); |
| 527 | goto error; |
| 528 | } |
| 529 | |
| 530 | /* Look for index file in relative path index/name.idx. */ |
| 531 | basename = g_path_get_basename(ds_file->file->path->str); |
| 532 | if (!basename) { |
| 533 | BT_COMP_LOGE("Cannot get the basename of datastream file %s", ds_file->file->path->str); |
| 534 | goto error; |
| 535 | } |
| 536 | |
| 537 | directory = g_path_get_dirname(ds_file->file->path->str); |
| 538 | if (!directory) { |
| 539 | BT_COMP_LOGE("Cannot get dirname of datastream file %s", ds_file->file->path->str); |
| 540 | goto error; |
| 541 | } |
| 542 | |
| 543 | index_basename = g_string_new(basename); |
| 544 | if (!index_basename) { |
| 545 | BT_COMP_LOGE_STR("Cannot allocate index file basename string"); |
| 546 | goto error; |
| 547 | } |
| 548 | |
| 549 | g_string_append(index_basename, ".idx"); |
| 550 | index_file_path = g_build_filename(directory, "index", index_basename->str, NULL); |
| 551 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); |
| 552 | if (!mapped_file) { |
| 553 | BT_COMP_LOGD("Cannot create new mapped file %s", index_file_path); |
| 554 | goto error; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * The g_mapped_file API limits us to 4GB files on 32-bit. |
| 559 | * Traces with such large indexes have never been seen in the wild, |
| 560 | * but this would need to be adjusted to support them. |
| 561 | */ |
| 562 | filesize = g_mapped_file_get_length(mapped_file); |
| 563 | if (filesize < sizeof(*header)) { |
| 564 | BT_COMP_LOGW("Invalid LTTng trace index file: " |
| 565 | "file size (%zu bytes) < header size (%zu bytes)", |
| 566 | filesize, sizeof(*header)); |
| 567 | goto error; |
| 568 | } |
| 569 | |
| 570 | mmap_begin = g_mapped_file_get_contents(mapped_file); |
| 571 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; |
| 572 | |
| 573 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); |
| 574 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { |
| 575 | BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
| 576 | goto error; |
| 577 | } |
| 578 | |
| 579 | version_major = be32toh(header->index_major); |
| 580 | version_minor = be32toh(header->index_minor); |
| 581 | if (version_major != 1) { |
| 582 | BT_COMP_LOGW("Unknown LTTng trace index version: " |
| 583 | "major=%" PRIu32 ", minor=%" PRIu32, |
| 584 | version_major, version_minor); |
| 585 | goto error; |
| 586 | } |
| 587 | |
| 588 | file_index_entry_size = be32toh(header->packet_index_len); |
| 589 | if (file_index_entry_size < CTF_INDEX_1_0_SIZE) { |
| 590 | BT_COMP_LOGW( |
| 591 | "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): " |
| 592 | "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu", |
| 593 | file_index_entry_size, CTF_INDEX_1_0_SIZE); |
| 594 | goto error; |
| 595 | } |
| 596 | |
| 597 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; |
| 598 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
| 599 | BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header " |
| 600 | "(%zu bytes) is not a multiple of the index entry size " |
| 601 | "(%zu bytes)", |
| 602 | (filesize - sizeof(*header)), sizeof(*header)); |
| 603 | goto error; |
| 604 | } |
| 605 | |
| 606 | index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp); |
| 607 | if (!index) { |
| 608 | goto error; |
| 609 | } |
| 610 | |
| 611 | for (i = 0; i < file_entry_count; i++) { |
| 612 | struct ctf_packet_index *file_index = (struct ctf_packet_index *) file_pos; |
| 613 | uint64_t packet_size = be64toh(file_index->packet_size); |
| 614 | |
| 615 | if (packet_size % CHAR_BIT) { |
| 616 | BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file"); |
| 617 | goto error; |
| 618 | } |
| 619 | |
| 620 | index_entry = ctf_fs_ds_index_entry_create(ds_file->self_comp, ds_file->log_level); |
| 621 | if (!index_entry) { |
| 622 | BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp, |
| 623 | "Failed to create a ctf_fs_ds_index_entry."); |
| 624 | goto error; |
| 625 | } |
| 626 | |
| 627 | /* Set path to stream file. */ |
| 628 | index_entry->path = file_info->path->str; |
| 629 | |
| 630 | /* Convert size in bits to bytes. */ |
| 631 | packet_size /= CHAR_BIT; |
| 632 | index_entry->packet_size = packet_size; |
| 633 | |
| 634 | index_entry->offset = be64toh(file_index->offset); |
| 635 | if (i != 0 && index_entry->offset < prev_index_entry->offset) { |
| 636 | BT_COMP_LOGW( |
| 637 | "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
| 638 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, |
| 639 | prev_index_entry->offset, index_entry->offset); |
| 640 | goto error; |
| 641 | } |
| 642 | |
| 643 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
| 644 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); |
| 645 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { |
| 646 | BT_COMP_LOGW( |
| 647 | "Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
| 648 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, |
| 649 | index_entry->timestamp_begin, index_entry->timestamp_end); |
| 650 | goto error; |
| 651 | } |
| 652 | |
| 653 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
| 654 | ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_begin, |
| 655 | &index_entry->timestamp_begin_ns); |
| 656 | if (ret) { |
| 657 | BT_COMP_LOGI_STR( |
| 658 | "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
| 659 | goto error; |
| 660 | } |
| 661 | ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_end, |
| 662 | &index_entry->timestamp_end_ns); |
| 663 | if (ret) { |
| 664 | BT_COMP_LOGI_STR( |
| 665 | "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
| 666 | goto error; |
| 667 | } |
| 668 | |
| 669 | if (version_minor >= 1) { |
| 670 | index_entry->packet_seq_num = be64toh(file_index->packet_seq_num); |
| 671 | } |
| 672 | |
| 673 | total_packets_size += packet_size; |
| 674 | file_pos += file_index_entry_size; |
| 675 | |
| 676 | prev_index_entry = index_entry; |
| 677 | |
| 678 | /* Give ownership of `index_entry` to `index->entries`. */ |
| 679 | g_ptr_array_add(index->entries, index_entry); |
| 680 | index_entry = NULL; |
| 681 | } |
| 682 | |
| 683 | /* Validate that the index addresses the complete stream. */ |
| 684 | if (ds_file->file->size != total_packets_size) { |
| 685 | BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
| 686 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
| 687 | ds_file->file->size, total_packets_size); |
| 688 | goto error; |
| 689 | } |
| 690 | end: |
| 691 | g_free(directory); |
| 692 | g_free(basename); |
| 693 | g_free(index_file_path); |
| 694 | if (index_basename) { |
| 695 | g_string_free(index_basename, TRUE); |
| 696 | } |
| 697 | if (mapped_file) { |
| 698 | g_mapped_file_unref(mapped_file); |
| 699 | } |
| 700 | return index; |
| 701 | error: |
| 702 | ctf_fs_ds_index_destroy(index); |
| 703 | g_free(index_entry); |
| 704 | index = NULL; |
| 705 | goto end; |
| 706 | } |
| 707 | |
| 708 | static int init_index_entry(struct ctf_fs_ds_index_entry *entry, struct ctf_fs_ds_file *ds_file, |
| 709 | struct ctf_msg_iter_packet_properties *props, off_t packet_size, |
| 710 | off_t packet_offset) |
| 711 | { |
| 712 | int ret = 0; |
| 713 | struct ctf_stream_class *sc; |
| 714 | bt_self_component *self_comp = ds_file->self_comp; |
| 715 | bt_logging_level log_level = ds_file->log_level; |
| 716 | |
| 717 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props->stream_class_id); |
| 718 | BT_ASSERT(sc); |
| 719 | BT_ASSERT(packet_offset >= 0); |
| 720 | entry->offset = packet_offset; |
| 721 | BT_ASSERT(packet_size >= 0); |
| 722 | entry->packet_size = packet_size; |
| 723 | |
| 724 | if (props->snapshots.beginning_clock != UINT64_C(-1)) { |
| 725 | entry->timestamp_begin = props->snapshots.beginning_clock; |
| 726 | |
| 727 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
| 728 | ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.beginning_clock, |
| 729 | &entry->timestamp_begin_ns); |
| 730 | if (ret) { |
| 731 | BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); |
| 732 | goto end; |
| 733 | } |
| 734 | } else { |
| 735 | entry->timestamp_begin = UINT64_C(-1); |
| 736 | entry->timestamp_begin_ns = UINT64_C(-1); |
| 737 | } |
| 738 | |
| 739 | if (props->snapshots.end_clock != UINT64_C(-1)) { |
| 740 | entry->timestamp_end = props->snapshots.end_clock; |
| 741 | |
| 742 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
| 743 | ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.end_clock, |
| 744 | &entry->timestamp_end_ns); |
| 745 | if (ret) { |
| 746 | BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); |
| 747 | goto end; |
| 748 | } |
| 749 | } else { |
| 750 | entry->timestamp_end = UINT64_C(-1); |
| 751 | entry->timestamp_end_ns = UINT64_C(-1); |
| 752 | } |
| 753 | |
| 754 | end: |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | static struct ctf_fs_ds_index *build_index_from_stream_file(struct ctf_fs_ds_file *ds_file, |
| 759 | struct ctf_fs_ds_file_info *file_info, |
| 760 | struct ctf_msg_iter *msg_iter) |
| 761 | { |
| 762 | int ret; |
| 763 | struct ctf_fs_ds_index *index = NULL; |
| 764 | enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK; |
| 765 | off_t current_packet_offset_bytes = 0; |
| 766 | bt_self_component *self_comp = ds_file->self_comp; |
| 767 | bt_logging_level log_level = ds_file->log_level; |
| 768 | |
| 769 | BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str); |
| 770 | |
| 771 | index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp); |
| 772 | if (!index) { |
| 773 | goto error; |
| 774 | } |
| 775 | |
| 776 | while (true) { |
| 777 | off_t current_packet_size_bytes; |
| 778 | struct ctf_fs_ds_index_entry *index_entry; |
| 779 | struct ctf_msg_iter_packet_properties props; |
| 780 | |
| 781 | if (current_packet_offset_bytes < 0) { |
| 782 | BT_COMP_LOGE_STR("Cannot get the current packet's offset."); |
| 783 | goto error; |
| 784 | } else if (current_packet_offset_bytes > ds_file->file->size) { |
| 785 | BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file)."); |
| 786 | goto error; |
| 787 | } else if (current_packet_offset_bytes == ds_file->file->size) { |
| 788 | /* No more data */ |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | iter_status = ctf_msg_iter_seek(msg_iter, current_packet_offset_bytes); |
| 793 | if (iter_status != CTF_MSG_ITER_STATUS_OK) { |
| 794 | goto error; |
| 795 | } |
| 796 | |
| 797 | iter_status = ctf_msg_iter_get_packet_properties(msg_iter, &props); |
| 798 | if (iter_status != CTF_MSG_ITER_STATUS_OK) { |
| 799 | goto error; |
| 800 | } |
| 801 | |
| 802 | if (props.exp_packet_total_size >= 0) { |
| 803 | current_packet_size_bytes = (uint64_t) props.exp_packet_total_size / 8; |
| 804 | } else { |
| 805 | current_packet_size_bytes = ds_file->file->size; |
| 806 | } |
| 807 | |
| 808 | if (current_packet_offset_bytes + current_packet_size_bytes > ds_file->file->size) { |
| 809 | BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", " |
| 810 | "packet-offset=%jd, packet-size-bytes=%jd, " |
| 811 | "file-size=%jd", |
| 812 | ds_file->file->path->str, (intmax_t) current_packet_offset_bytes, |
| 813 | (intmax_t) current_packet_size_bytes, (intmax_t) ds_file->file->size); |
| 814 | goto error; |
| 815 | } |
| 816 | |
| 817 | index_entry = ctf_fs_ds_index_entry_create(ds_file->self_comp, ds_file->log_level); |
| 818 | if (!index_entry) { |
| 819 | BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp, |
| 820 | "Failed to create a ctf_fs_ds_index_entry."); |
| 821 | goto error; |
| 822 | } |
| 823 | |
| 824 | /* Set path to stream file. */ |
| 825 | index_entry->path = file_info->path->str; |
| 826 | |
| 827 | ret = init_index_entry(index_entry, ds_file, &props, current_packet_size_bytes, |
| 828 | current_packet_offset_bytes); |
| 829 | if (ret) { |
| 830 | g_free(index_entry); |
| 831 | goto error; |
| 832 | } |
| 833 | |
| 834 | g_ptr_array_add(index->entries, index_entry); |
| 835 | |
| 836 | current_packet_offset_bytes += current_packet_size_bytes; |
| 837 | BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, " |
| 838 | "next-packet-offset=%jd", |
| 839 | (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes), |
| 840 | (intmax_t) current_packet_offset_bytes); |
| 841 | } |
| 842 | |
| 843 | end: |
| 844 | return index; |
| 845 | |
| 846 | error: |
| 847 | ctf_fs_ds_index_destroy(index); |
| 848 | index = NULL; |
| 849 | goto end; |
| 850 | } |
| 851 | |
| 852 | struct ctf_fs_ds_file *ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace, bt_stream *stream, |
| 853 | const char *path, bt_logging_level log_level) |
| 854 | { |
| 855 | int ret; |
| 856 | const size_t offset_align = bt_mmap_get_offset_align_size(log_level); |
| 857 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
| 858 | |
| 859 | if (!ds_file) { |
| 860 | goto error; |
| 861 | } |
| 862 | |
| 863 | ds_file->log_level = log_level; |
| 864 | ds_file->self_comp = ctf_fs_trace->self_comp; |
| 865 | ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp); |
| 866 | if (!ds_file->file) { |
| 867 | goto error; |
| 868 | } |
| 869 | |
| 870 | ds_file->stream = stream; |
| 871 | bt_stream_get_ref(ds_file->stream); |
| 872 | ds_file->metadata = ctf_fs_trace->metadata; |
| 873 | g_string_assign(ds_file->file->path, path); |
| 874 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
| 875 | if (ret) { |
| 876 | goto error; |
| 877 | } |
| 878 | |
| 879 | ds_file->mmap_max_len = offset_align * 2048; |
| 880 | |
| 881 | goto end; |
| 882 | |
| 883 | error: |
| 884 | /* Do not touch "borrowed" file. */ |
| 885 | ctf_fs_ds_file_destroy(ds_file); |
| 886 | ds_file = NULL; |
| 887 | |
| 888 | end: |
| 889 | return ds_file; |
| 890 | } |
| 891 | |
| 892 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file, |
| 893 | struct ctf_fs_ds_file_info *file_info, |
| 894 | struct ctf_msg_iter *msg_iter) |
| 895 | { |
| 896 | struct ctf_fs_ds_index *index; |
| 897 | bt_self_component *self_comp = ds_file->self_comp; |
| 898 | bt_logging_level log_level = ds_file->log_level; |
| 899 | |
| 900 | index = build_index_from_idx_file(ds_file, file_info, msg_iter); |
| 901 | if (index) { |
| 902 | goto end; |
| 903 | } |
| 904 | |
| 905 | BT_COMP_LOGI("Failed to build index from .index file; " |
| 906 | "falling back to stream indexing."); |
| 907 | index = build_index_from_stream_file(ds_file, file_info, msg_iter); |
| 908 | end: |
| 909 | return index; |
| 910 | } |
| 911 | |
| 912 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level, |
| 913 | bt_self_component *self_comp) |
| 914 | { |
| 915 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); |
| 916 | |
| 917 | if (!index) { |
| 918 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, "Failed to allocate index"); |
| 919 | goto error; |
| 920 | } |
| 921 | |
| 922 | index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free); |
| 923 | if (!index->entries) { |
| 924 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
| 925 | "Failed to allocate index entries."); |
| 926 | goto error; |
| 927 | } |
| 928 | |
| 929 | goto end; |
| 930 | |
| 931 | error: |
| 932 | ctf_fs_ds_index_destroy(index); |
| 933 | index = NULL; |
| 934 | end: |
| 935 | return index; |
| 936 | } |
| 937 | |
| 938 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
| 939 | { |
| 940 | if (!ds_file) { |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | bt_stream_put_ref(ds_file->stream); |
| 945 | (void) ds_file_munmap(ds_file); |
| 946 | |
| 947 | if (ds_file->file) { |
| 948 | ctf_fs_file_destroy(ds_file->file); |
| 949 | } |
| 950 | |
| 951 | g_free(ds_file); |
| 952 | } |
| 953 | |
| 954 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) |
| 955 | { |
| 956 | if (!index) { |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | if (index->entries) { |
| 961 | g_ptr_array_free(index->entries, TRUE); |
| 962 | } |
| 963 | g_free(index); |
| 964 | } |