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