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