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