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> | |
8f76831a | 32 | #include <babeltrace/compat/mman-internal.h> |
e9383dfd | 33 | #include <babeltrace/endian-internal.h> |
e0831b38 | 34 | #include <babeltrace/babeltrace.h> |
55314f2a | 35 | #include <babeltrace/common-internal.h> |
78586d8a JG |
36 | #include "file.h" |
37 | #include "metadata.h" | |
38 | #include "../common/notif-iter/notif-iter.h" | |
8b45963b | 39 | #include <babeltrace/assert-internal.h> |
94cf822e | 40 | #include "data-stream-file.h" |
e9383dfd | 41 | #include <string.h> |
e98a2d6e | 42 | |
55314f2a JG |
43 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS" |
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 | { |
52eb1a72 | 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 | ||
a555e971 | 61 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { |
2a48e5ae 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, |
bb230c9b | 65 | ds_file->file ? ds_file->file->path->str : "NULL", |
2a48e5ae | 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 |
839d52a5 | 78 | enum bt_notif_iter_medium_status ds_file_mmap_next( |
94cf822e | 79 | struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 80 | { |
839d52a5 PP |
81 | enum bt_notif_iter_medium_status ret = |
82 | BT_NOTIF_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 | ||
7b7d8091 | 90 | /* |
52eb1a72 | 91 | * mmap_len is guaranteed to be page-aligned except on the |
7b7d8091 JG |
92 | * last mapping where it may not be possible (since the file's |
93 | * size itself may not be a page multiple). | |
94 | */ | |
52eb1a72 | 95 | ds_file->mmap_offset += ds_file->mmap_len; |
94cf822e | 96 | ds_file->request_offset = 0; |
e98a2d6e PP |
97 | } |
98 | ||
52eb1a72 | 99 | ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset, |
94cf822e | 100 | ds_file->mmap_max_len); |
52eb1a72 | 101 | if (ds_file->mmap_len == 0) { |
839d52a5 | 102 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_EOF; |
e0f6a64a JG |
103 | goto end; |
104 | } | |
e98a2d6e | 105 | /* Map new region */ |
8b45963b | 106 | BT_ASSERT(ds_file->mmap_len); |
a555e971 | 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) { | |
41342c71 | 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, |
41342c71 | 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); |
839d52a5 | 121 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
122 | end: |
123 | return ret; | |
124 | } | |
125 | ||
5b29e799 | 126 | static |
839d52a5 | 127 | enum bt_notif_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 | { | |
839d52a5 PP |
131 | enum bt_notif_iter_medium_status status = |
132 | BT_NOTIF_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); |
839d52a5 | 145 | status = BT_NOTIF_ITER_MEDIUM_STATUS_EOF; |
e98a2d6e PP |
146 | goto end; |
147 | } | |
148 | ||
94cf822e | 149 | status = ds_file_mmap_next(ds_file); |
e0f6a64a | 150 | switch (status) { |
839d52a5 | 151 | case BT_NOTIF_ITER_MEDIUM_STATUS_OK: |
e0f6a64a | 152 | break; |
839d52a5 | 153 | case BT_NOTIF_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: | |
839d52a5 | 169 | status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
170 | |
171 | end: | |
172 | return status; | |
173 | } | |
174 | ||
5b29e799 | 175 | static |
a6918753 | 176 | struct bt_stream *medop_borrow_stream( |
839d52a5 | 177 | struct bt_stream_class *stream_class, uint64_t stream_id, |
0659c536 | 178 | void *data) |
e98a2d6e | 179 | { |
94cf822e | 180 | struct ctf_fs_ds_file *ds_file = data; |
839d52a5 PP |
181 | struct bt_stream_class *ds_file_stream_class; |
182 | struct bt_stream *stream = NULL; | |
94cf822e | 183 | |
c43f6ee2 | 184 | ds_file_stream_class = bt_stream_borrow_class(ds_file->stream); |
94cf822e PP |
185 | if (stream_class != ds_file_stream_class) { |
186 | /* | |
187 | * Not supported: two packets described by two different | |
188 | * stream classes within the same data stream file. | |
189 | */ | |
190 | goto end; | |
e98a2d6e PP |
191 | } |
192 | ||
94cf822e PP |
193 | stream = ds_file->stream; |
194 | ||
195 | end: | |
196 | return stream; | |
e98a2d6e PP |
197 | } |
198 | ||
bb230c9b | 199 | static |
839d52a5 PP |
200 | enum bt_notif_iter_medium_status medop_seek( |
201 | enum bt_notif_iter_seek_whence whence, off_t offset, | |
bb230c9b JG |
202 | void *data) |
203 | { | |
839d52a5 PP |
204 | enum bt_notif_iter_medium_status ret = |
205 | BT_NOTIF_ITER_MEDIUM_STATUS_OK; | |
bb230c9b JG |
206 | struct ctf_fs_ds_file *ds_file = data; |
207 | off_t file_size = ds_file->file->size; | |
208 | ||
839d52a5 | 209 | if (whence != BT_NOTIF_ITER_SEEK_WHENCE_SET || |
bb230c9b 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); | |
839d52a5 | 214 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_INVAL; |
bb230c9b 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; | |
225 | off_t offset_in_mapping = offset % bt_common_get_page_size(); | |
226 | ||
227 | BT_LOGD("Medium seek request cannot be accomodated by the current " | |
af5e91e4 | 228 | "file mapping: offset=%jd, mmap-offset=%jd, " |
bb230c9b JG |
229 | "mmap-len=%zu", offset, ds_file->mmap_offset, |
230 | ds_file->mmap_len); | |
231 | unmap_ret = ds_file_munmap(ds_file); | |
232 | if (unmap_ret) { | |
839d52a5 | 233 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
bb230c9b JG |
234 | goto end; |
235 | } | |
236 | ||
237 | ds_file->mmap_offset = offset - offset_in_mapping; | |
238 | ds_file->request_offset = offset_in_mapping; | |
239 | ret = ds_file_mmap_next(ds_file); | |
839d52a5 | 240 | if (ret != BT_NOTIF_ITER_MEDIUM_STATUS_OK) { |
bb230c9b JG |
241 | goto end; |
242 | } | |
243 | } else { | |
244 | ds_file->request_offset = offset - ds_file->mmap_offset; | |
245 | } | |
246 | ||
247 | ds_file->end_reached = (offset == file_size); | |
248 | end: | |
249 | return ret; | |
250 | } | |
251 | ||
55fa6491 | 252 | BT_HIDDEN |
839d52a5 | 253 | struct bt_notif_iter_medium_ops ctf_fs_ds_file_medops = { |
e98a2d6e | 254 | .request_bytes = medop_request_bytes, |
a6918753 | 255 | .borrow_stream = medop_borrow_stream, |
bb230c9b | 256 | .seek = medop_seek, |
e98a2d6e | 257 | }; |
55fa6491 | 258 | |
97ade20b JG |
259 | static |
260 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length) | |
261 | { | |
262 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); | |
263 | ||
264 | if (!index) { | |
265 | BT_LOGE_STR("Failed to allocate index"); | |
266 | goto error; | |
267 | } | |
268 | ||
269 | index->entries = g_array_sized_new(FALSE, TRUE, | |
270 | sizeof(struct ctf_fs_ds_index_entry), length); | |
271 | if (!index->entries) { | |
272 | BT_LOGE("Failed to allocate %zu index entries.", length); | |
273 | goto error; | |
274 | } | |
275 | g_array_set_size(index->entries, length); | |
276 | end: | |
277 | return index; | |
278 | error: | |
279 | ctf_fs_ds_index_destroy(index); | |
280 | goto end; | |
281 | } | |
e98a2d6e | 282 | |
bb230c9b JG |
283 | /* Returns a new, zeroed, index entry. */ |
284 | static | |
285 | struct ctf_fs_ds_index_entry *ctf_fs_ds_index_add_new_entry( | |
286 | struct ctf_fs_ds_index *index) | |
287 | { | |
288 | g_array_set_size(index->entries, index->entries->len + 1); | |
289 | return &g_array_index(index->entries, struct ctf_fs_ds_index_entry, | |
290 | index->entries->len - 1); | |
291 | } | |
292 | ||
b6c3dcb2 | 293 | static |
c43f6ee2 | 294 | struct bt_clock_class *borrow_field_mapped_clock_class( |
839d52a5 | 295 | struct bt_field *field) |
97ade20b | 296 | { |
839d52a5 PP |
297 | struct bt_field_type *field_type; |
298 | struct bt_clock_class *clock_class = NULL; | |
97ade20b | 299 | |
c43f6ee2 | 300 | field_type = bt_field_borrow_type(field); |
97ade20b JG |
301 | if (!field_type) { |
302 | goto end; | |
303 | } | |
304 | ||
c43f6ee2 PP |
305 | clock_class = bt_field_type_integer_borrow_mapped_clock_class( |
306 | field_type); | |
97ade20b JG |
307 | if (!clock_class) { |
308 | goto end; | |
309 | } | |
c43f6ee2 | 310 | |
97ade20b | 311 | end: |
97ade20b JG |
312 | return clock_class; |
313 | } | |
314 | ||
315 | static | |
c43f6ee2 | 316 | int borrow_packet_bounds_from_packet_context( |
839d52a5 PP |
317 | struct bt_field *packet_context, |
318 | struct bt_clock_class **_timestamp_begin_cc, | |
319 | struct bt_field **_timestamp_begin, | |
320 | struct bt_clock_class **_timestamp_end_cc, | |
321 | struct bt_field **_timestamp_end) | |
97ade20b | 322 | { |
bb230c9b | 323 | int ret = 0; |
839d52a5 PP |
324 | struct bt_clock_class *timestamp_begin_cc = NULL; |
325 | struct bt_clock_class *timestamp_end_cc = NULL; | |
326 | struct bt_field *timestamp_begin = NULL; | |
327 | struct bt_field *timestamp_end = NULL; | |
97ade20b | 328 | |
c43f6ee2 | 329 | timestamp_begin = bt_field_structure_borrow_field_by_name( |
bb230c9b JG |
330 | packet_context, "timestamp_begin"); |
331 | if (!timestamp_begin) { | |
332 | BT_LOGD_STR("Cannot retrieve timestamp_begin field in packet context."); | |
97ade20b JG |
333 | ret = -1; |
334 | goto end; | |
335 | } | |
336 | ||
c43f6ee2 | 337 | timestamp_begin_cc = borrow_field_mapped_clock_class(timestamp_begin); |
97ade20b | 338 | if (!timestamp_begin_cc) { |
bb230c9b | 339 | BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_begin."); |
97ade20b | 340 | } |
97ade20b | 341 | |
c43f6ee2 | 342 | timestamp_end = bt_field_structure_borrow_field_by_name( |
bb230c9b JG |
343 | packet_context, "timestamp_end"); |
344 | if (!timestamp_end) { | |
345 | BT_LOGD_STR("Cannot retrieve timestamp_end field in packet context."); | |
97ade20b JG |
346 | ret = -1; |
347 | goto end; | |
348 | } | |
349 | ||
c43f6ee2 | 350 | timestamp_end_cc = borrow_field_mapped_clock_class(timestamp_end); |
97ade20b | 351 | if (!timestamp_end_cc) { |
bb230c9b | 352 | BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_end."); |
97ade20b JG |
353 | } |
354 | ||
355 | if (_timestamp_begin_cc) { | |
c43f6ee2 | 356 | *_timestamp_begin_cc = timestamp_begin_cc; |
97ade20b | 357 | } |
bb230c9b | 358 | if (_timestamp_begin) { |
c43f6ee2 | 359 | *_timestamp_begin = timestamp_begin; |
bb230c9b | 360 | } |
97ade20b | 361 | if (_timestamp_end_cc) { |
c43f6ee2 | 362 | *_timestamp_end_cc = timestamp_end_cc; |
97ade20b | 363 | } |
bb230c9b | 364 | if (_timestamp_end) { |
c43f6ee2 | 365 | *_timestamp_end = timestamp_end; |
bb230c9b | 366 | } |
97ade20b | 367 | end: |
bb230c9b JG |
368 | return ret; |
369 | } | |
370 | ||
371 | static | |
c43f6ee2 | 372 | int borrow_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file, |
839d52a5 PP |
373 | struct bt_clock_class **timestamp_begin_cc, |
374 | struct bt_clock_class **timestamp_end_cc) | |
bb230c9b | 375 | { |
839d52a5 | 376 | struct bt_field *packet_context = NULL; |
a6918753 PP |
377 | int ret = ctf_fs_ds_file_borrow_packet_header_context_fields(ds_file, |
378 | NULL, &packet_context); | |
bb230c9b JG |
379 | |
380 | if (ret || !packet_context) { | |
a6918753 | 381 | BT_LOGD("Cannot retrieve packet context field: ds-file-path=\"%s\"", |
bb230c9b JG |
382 | ds_file->file->path->str); |
383 | ret = -1; | |
384 | goto end; | |
385 | } | |
386 | ||
c43f6ee2 | 387 | ret = borrow_packet_bounds_from_packet_context(packet_context, |
bb230c9b JG |
388 | timestamp_begin_cc, NULL, |
389 | timestamp_end_cc, NULL); | |
c43f6ee2 | 390 | |
bb230c9b | 391 | end: |
97ade20b JG |
392 | return ret; |
393 | } | |
394 | ||
395 | static | |
839d52a5 | 396 | int convert_cycles_to_ns(struct bt_clock_class *clock_class, |
97ade20b | 397 | uint64_t cycles, int64_t *ns) |
b6c3dcb2 | 398 | { |
a6918753 | 399 | return bt_clock_class_cycles_to_ns(clock_class, cycles, ns); |
97ade20b JG |
400 | } |
401 | ||
402 | static | |
403 | struct ctf_fs_ds_index *build_index_from_idx_file( | |
404 | struct ctf_fs_ds_file *ds_file) | |
405 | { | |
406 | int ret; | |
b6c3dcb2 JG |
407 | gchar *directory = NULL; |
408 | gchar *basename = NULL; | |
409 | GString *index_basename = NULL; | |
410 | gchar *index_file_path = NULL; | |
411 | GMappedFile *mapped_file = NULL; | |
412 | gsize filesize; | |
97ade20b JG |
413 | const char *mmap_begin = NULL, *file_pos = NULL; |
414 | const struct ctf_packet_index_file_hdr *header = NULL; | |
415 | struct ctf_fs_ds_index *index = NULL; | |
416 | struct ctf_fs_ds_index_entry *index_entry = NULL; | |
b6c3dcb2 JG |
417 | uint64_t total_packets_size = 0; |
418 | size_t file_index_entry_size; | |
419 | size_t file_entry_count; | |
420 | size_t i; | |
839d52a5 PP |
421 | struct bt_clock_class *timestamp_begin_cc = NULL; |
422 | struct bt_clock_class *timestamp_end_cc = NULL; | |
97ade20b JG |
423 | |
424 | BT_LOGD("Building index from .idx file of stream file %s", | |
425 | ds_file->file->path->str); | |
426 | ||
c43f6ee2 | 427 | ret = borrow_ds_file_packet_bounds_clock_classes(ds_file, |
97ade20b JG |
428 | ×tamp_begin_cc, ×tamp_end_cc); |
429 | if (ret) { | |
bb230c9b JG |
430 | BT_LOGD_STR("Cannot get clock classes of \"timestamp_begin\" " |
431 | "and \"timestamp_end\" fields"); | |
97ade20b JG |
432 | goto error; |
433 | } | |
b6c3dcb2 JG |
434 | |
435 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 436 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 437 | if (!basename) { |
97ade20b | 438 | BT_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 439 | ds_file->file->path->str); |
97ade20b | 440 | goto error; |
b6c3dcb2 JG |
441 | } |
442 | ||
94cf822e | 443 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 444 | if (!directory) { |
97ade20b | 445 | BT_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 446 | ds_file->file->path->str); |
97ade20b | 447 | goto error; |
b6c3dcb2 JG |
448 | } |
449 | ||
450 | index_basename = g_string_new(basename); | |
451 | if (!index_basename) { | |
bb230c9b | 452 | BT_LOGE_STR("Cannot allocate index file basename string"); |
97ade20b | 453 | goto error; |
b6c3dcb2 JG |
454 | } |
455 | ||
456 | g_string_append(index_basename, ".idx"); | |
457 | index_file_path = g_build_filename(directory, "index", | |
458 | index_basename->str, NULL); | |
459 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 460 | if (!mapped_file) { |
97ade20b | 461 | BT_LOGD("Cannot create new mapped file %s", |
55314f2a | 462 | index_file_path); |
97ade20b | 463 | goto error; |
06367fa3 | 464 | } |
65cbebef JG |
465 | |
466 | /* | |
467 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
468 | * Traces with such large indexes have never been seen in the wild, | |
469 | * but this would need to be adjusted to support them. | |
470 | */ | |
b6c3dcb2 JG |
471 | filesize = g_mapped_file_get_length(mapped_file); |
472 | if (filesize < sizeof(*header)) { | |
65cbebef JG |
473 | BT_LOGW("Invalid LTTng trace index file: " |
474 | "file size (%zu bytes) < header size (%zu bytes)", | |
475 | filesize, sizeof(*header)); | |
97ade20b | 476 | goto error; |
b6c3dcb2 JG |
477 | } |
478 | ||
479 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
480 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
481 | ||
482 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
483 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
bb230c9b | 484 | BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 485 | goto error; |
b6c3dcb2 | 486 | } |
b6c3dcb2 JG |
487 | |
488 | file_index_entry_size = be32toh(header->packet_index_len); | |
489 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
97ade20b | 490 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
65cbebef JG |
491 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
492 | "(%zu bytes) is not a multiple of the index entry size " | |
493 | "(%zu bytes)", (filesize - sizeof(*header)), | |
494 | sizeof(*header)); | |
97ade20b | 495 | goto error; |
b6c3dcb2 JG |
496 | } |
497 | ||
97ade20b JG |
498 | index = ctf_fs_ds_index_create(file_entry_count); |
499 | if (!index) { | |
500 | goto error; | |
b6c3dcb2 | 501 | } |
97ade20b JG |
502 | |
503 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( | |
504 | index->entries, struct ctf_fs_ds_index_entry, 0); | |
b6c3dcb2 JG |
505 | for (i = 0; i < file_entry_count; i++) { |
506 | struct ctf_packet_index *file_index = | |
507 | (struct ctf_packet_index *) file_pos; | |
508 | uint64_t packet_size = be64toh(file_index->packet_size); | |
509 | ||
510 | if (packet_size % CHAR_BIT) { | |
65cbebef | 511 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 512 | goto error; |
b6c3dcb2 JG |
513 | } |
514 | ||
515 | /* Convert size in bits to bytes. */ | |
516 | packet_size /= CHAR_BIT; | |
97ade20b | 517 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 518 | |
97ade20b JG |
519 | index_entry->offset = be64toh(file_index->offset); |
520 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { | |
65cbebef JG |
521 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
522 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, | |
523 | (index_entry - 1)->offset, index_entry->offset); | |
97ade20b | 524 | goto error; |
b6c3dcb2 JG |
525 | } |
526 | ||
97ade20b JG |
527 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
528 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
529 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
65cbebef JG |
530 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
531 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, | |
532 | index_entry->timestamp_begin, | |
533 | index_entry->timestamp_end); | |
97ade20b JG |
534 | goto error; |
535 | } | |
536 | ||
537 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
538 | ret = convert_cycles_to_ns(timestamp_begin_cc, | |
539 | index_entry->timestamp_begin, | |
540 | &index_entry->timestamp_begin_ns); | |
541 | if (ret) { | |
bb230c9b | 542 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
543 | goto error; |
544 | } | |
545 | ret = convert_cycles_to_ns(timestamp_end_cc, | |
546 | index_entry->timestamp_end, | |
547 | &index_entry->timestamp_end_ns); | |
548 | if (ret) { | |
bb230c9b | 549 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 550 | goto error; |
b6c3dcb2 JG |
551 | } |
552 | ||
553 | total_packets_size += packet_size; | |
554 | file_pos += file_index_entry_size; | |
97ade20b | 555 | index_entry++; |
b6c3dcb2 JG |
556 | } |
557 | ||
558 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 559 | if (ds_file->file->size != total_packets_size) { |
65cbebef | 560 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
bb230c9b | 561 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
65cbebef | 562 | ds_file->file->size, total_packets_size); |
97ade20b | 563 | goto error; |
b6c3dcb2 JG |
564 | } |
565 | end: | |
566 | g_free(directory); | |
567 | g_free(basename); | |
568 | g_free(index_file_path); | |
06367fa3 JG |
569 | if (index_basename) { |
570 | g_string_free(index_basename, TRUE); | |
571 | } | |
572 | if (mapped_file) { | |
573 | g_mapped_file_unref(mapped_file); | |
574 | } | |
97ade20b JG |
575 | return index; |
576 | error: | |
577 | ctf_fs_ds_index_destroy(index); | |
578 | index = NULL; | |
b6c3dcb2 JG |
579 | goto end; |
580 | } | |
581 | ||
bb230c9b JG |
582 | static |
583 | int init_index_entry(struct ctf_fs_ds_index_entry *entry, | |
839d52a5 | 584 | struct bt_field *packet_context, off_t packet_size, |
bb230c9b JG |
585 | off_t packet_offset) |
586 | { | |
587 | int ret; | |
839d52a5 PP |
588 | struct bt_field *timestamp_begin = NULL; |
589 | struct bt_field *timestamp_end = NULL; | |
590 | struct bt_clock_class *timestamp_begin_cc = NULL; | |
591 | struct bt_clock_class *timestamp_end_cc = NULL; | |
bb230c9b | 592 | |
c43f6ee2 | 593 | ret = borrow_packet_bounds_from_packet_context(packet_context, |
bb230c9b JG |
594 | ×tamp_begin_cc, ×tamp_begin, |
595 | ×tamp_end_cc, ×tamp_end); | |
596 | if (ret || !timestamp_begin_cc || !timestamp_begin || | |
597 | !timestamp_end_cc || ! timestamp_end) { | |
598 | BT_LOGD_STR("Failed to determine time bound fields of packet."); | |
599 | goto end; | |
600 | } | |
601 | ||
8b45963b | 602 | BT_ASSERT(packet_offset >= 0); |
bb230c9b JG |
603 | entry->offset = packet_offset; |
604 | ||
8b45963b | 605 | BT_ASSERT(packet_size >= 0); |
bb230c9b JG |
606 | entry->packet_size = packet_size; |
607 | ||
8deee039 | 608 | ret = bt_field_integer_unsigned_get_value(timestamp_begin, |
bb230c9b JG |
609 | &entry->timestamp_begin); |
610 | if (ret) { | |
611 | goto end; | |
612 | } | |
8deee039 | 613 | ret = bt_field_integer_unsigned_get_value(timestamp_end, |
bb230c9b JG |
614 | &entry->timestamp_end); |
615 | if (ret) { | |
616 | goto end; | |
617 | } | |
618 | ||
619 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
620 | ret = convert_cycles_to_ns(timestamp_begin_cc, | |
621 | entry->timestamp_begin, | |
622 | &entry->timestamp_begin_ns); | |
623 | if (ret) { | |
624 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
625 | goto end; | |
626 | } | |
627 | ||
628 | ret = convert_cycles_to_ns(timestamp_end_cc, | |
629 | entry->timestamp_end, | |
630 | &entry->timestamp_end_ns); | |
631 | if (ret) { | |
632 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
633 | goto end; | |
634 | } | |
c43f6ee2 | 635 | |
bb230c9b | 636 | end: |
bb230c9b JG |
637 | return ret; |
638 | } | |
639 | ||
640 | static | |
641 | struct ctf_fs_ds_index *build_index_from_stream_file( | |
642 | struct ctf_fs_ds_file *ds_file) | |
643 | { | |
644 | int ret; | |
645 | struct ctf_fs_ds_index *index = NULL; | |
839d52a5 | 646 | enum bt_notif_iter_status iter_status; |
bb230c9b JG |
647 | |
648 | BT_LOGD("Indexing stream file %s", ds_file->file->path->str); | |
649 | ||
650 | index = ctf_fs_ds_index_create(0); | |
651 | if (!index) { | |
652 | goto error; | |
653 | } | |
654 | ||
655 | do { | |
656 | off_t current_packet_offset; | |
657 | off_t next_packet_offset; | |
658 | off_t current_packet_size, current_packet_size_bytes; | |
659 | struct ctf_fs_ds_index_entry *entry; | |
a6918753 | 660 | struct bt_field *packet_context = NULL; |
bb230c9b | 661 | |
a6918753 PP |
662 | iter_status = bt_notif_iter_borrow_packet_header_context_fields( |
663 | ds_file->notif_iter, NULL, &packet_context); | |
839d52a5 PP |
664 | if (iter_status != BT_NOTIF_ITER_STATUS_OK) { |
665 | if (iter_status == BT_NOTIF_ITER_STATUS_EOF) { | |
bb230c9b JG |
666 | break; |
667 | } | |
668 | goto error; | |
669 | } | |
a6918753 | 670 | |
bb230c9b | 671 | current_packet_offset = |
a6918753 | 672 | bt_notif_iter_get_current_packet_offset( |
bb230c9b JG |
673 | ds_file->notif_iter); |
674 | if (current_packet_offset < 0) { | |
675 | BT_LOGE_STR("Cannot get the current packet's offset."); | |
676 | goto error; | |
677 | } | |
678 | ||
839d52a5 | 679 | current_packet_size = bt_notif_iter_get_current_packet_size( |
a6918753 | 680 | ds_file->notif_iter); |
bb230c9b JG |
681 | if (current_packet_size < 0) { |
682 | BT_LOGE("Cannot get packet size: packet-offset=%jd", | |
683 | current_packet_offset); | |
684 | goto error; | |
685 | } | |
686 | current_packet_size_bytes = | |
a6918753 | 687 | ((current_packet_size + 7) & ~7) / CHAR_BIT; |
bb230c9b JG |
688 | |
689 | if (current_packet_offset + current_packet_size_bytes > | |
690 | ds_file->file->size) { | |
691 | BT_LOGW("Invalid packet size reported in file: stream=\"%s\", " | |
692 | "packet-offset=%jd, packet-size-bytes=%jd, " | |
693 | "file-size=%jd", | |
694 | ds_file->file->path->str, | |
695 | current_packet_offset, | |
696 | current_packet_size_bytes, | |
697 | ds_file->file->size); | |
698 | goto error; | |
699 | } | |
700 | ||
701 | next_packet_offset = current_packet_offset + | |
a6918753 | 702 | current_packet_size_bytes; |
bb230c9b | 703 | BT_LOGD("Seeking to next packet: current-packet-offset=%jd, " |
a6918753 PP |
704 | "next-packet-offset=%jd", current_packet_offset, |
705 | next_packet_offset); | |
bb230c9b JG |
706 | |
707 | entry = ctf_fs_ds_index_add_new_entry(index); | |
708 | if (!entry) { | |
709 | BT_LOGE_STR("Failed to allocate a new index entry."); | |
710 | goto error; | |
711 | } | |
712 | ||
713 | ret = init_index_entry(entry, packet_context, | |
a6918753 PP |
714 | current_packet_size_bytes, |
715 | current_packet_offset); | |
bb230c9b JG |
716 | if (ret) { |
717 | goto error; | |
718 | } | |
719 | ||
839d52a5 | 720 | iter_status = bt_notif_iter_seek(ds_file->notif_iter, |
bb230c9b | 721 | next_packet_offset); |
839d52a5 | 722 | } while (iter_status == BT_NOTIF_ITER_STATUS_OK); |
bb230c9b | 723 | |
839d52a5 | 724 | if (iter_status != BT_NOTIF_ITER_STATUS_EOF) { |
bb230c9b JG |
725 | goto error; |
726 | } | |
a6918753 | 727 | |
bb230c9b | 728 | end: |
bb230c9b | 729 | return index; |
a6918753 | 730 | |
bb230c9b JG |
731 | error: |
732 | ctf_fs_ds_index_destroy(index); | |
733 | index = NULL; | |
734 | goto end; | |
735 | } | |
736 | ||
e7a4393b | 737 | BT_HIDDEN |
94cf822e PP |
738 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
739 | struct ctf_fs_trace *ctf_fs_trace, | |
839d52a5 PP |
740 | struct bt_notif_iter *notif_iter, |
741 | struct bt_stream *stream, const char *path) | |
e98a2d6e | 742 | { |
b6c3dcb2 | 743 | int ret; |
55314f2a | 744 | const size_t page_size = bt_common_get_page_size(); |
94cf822e | 745 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 746 | |
94cf822e | 747 | if (!ds_file) { |
e98a2d6e PP |
748 | goto error; |
749 | } | |
750 | ||
55314f2a | 751 | ds_file->file = ctf_fs_file_create(); |
94cf822e | 752 | if (!ds_file->file) { |
4f1f88a6 PP |
753 | goto error; |
754 | } | |
755 | ||
94cf822e PP |
756 | ds_file->stream = bt_get(stream); |
757 | ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map); | |
758 | g_string_assign(ds_file->file->path, path); | |
55314f2a | 759 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
760 | if (ret) { |
761 | goto error; | |
762 | } | |
763 | ||
55fa6491 | 764 | ds_file->notif_iter = notif_iter; |
839d52a5 | 765 | bt_notif_iter_set_medops_data(ds_file->notif_iter, ds_file); |
94cf822e | 766 | if (!ds_file->notif_iter) { |
e98a2d6e PP |
767 | goto error; |
768 | } | |
5b29e799 | 769 | |
55314f2a | 770 | ds_file->mmap_max_len = page_size * 2048; |
94cf822e | 771 | |
e98a2d6e | 772 | goto end; |
1a9f7075 | 773 | |
e98a2d6e | 774 | error: |
78586d8a | 775 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
776 | ctf_fs_ds_file_destroy(ds_file); |
777 | ds_file = NULL; | |
1a9f7075 | 778 | |
e98a2d6e | 779 | end: |
94cf822e | 780 | return ds_file; |
e98a2d6e PP |
781 | } |
782 | ||
97ade20b JG |
783 | BT_HIDDEN |
784 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
785 | struct ctf_fs_ds_file *ds_file) | |
786 | { | |
bb230c9b JG |
787 | struct ctf_fs_ds_index *index; |
788 | ||
789 | index = build_index_from_idx_file(ds_file); | |
790 | if (index) { | |
791 | goto end; | |
792 | } | |
793 | ||
794 | BT_LOGD("Failed to build index from .index file; " | |
795 | "falling back to stream indexing."); | |
796 | index = build_index_from_stream_file(ds_file); | |
797 | end: | |
798 | return index; | |
97ade20b JG |
799 | } |
800 | ||
5b29e799 | 801 | BT_HIDDEN |
94cf822e | 802 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 803 | { |
94cf822e | 804 | if (!ds_file) { |
5b29e799 | 805 | return; |
043e2020 JG |
806 | } |
807 | ||
94cf822e PP |
808 | bt_put(ds_file->cc_prio_map); |
809 | bt_put(ds_file->stream); | |
810 | (void) ds_file_munmap(ds_file); | |
0982a26d | 811 | |
94cf822e PP |
812 | if (ds_file->file) { |
813 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
814 | } |
815 | ||
94cf822e | 816 | g_free(ds_file); |
e98a2d6e | 817 | } |
4f1f88a6 PP |
818 | |
819 | BT_HIDDEN | |
fe7265b5 | 820 | struct bt_notification_iterator_next_method_return ctf_fs_ds_file_next( |
94cf822e | 821 | struct ctf_fs_ds_file *ds_file) |
4f1f88a6 | 822 | { |
839d52a5 | 823 | enum bt_notif_iter_status notif_iter_status; |
fe7265b5 | 824 | struct bt_notification_iterator_next_method_return ret = { |
4f1f88a6 PP |
825 | .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR, |
826 | .notification = NULL, | |
827 | }; | |
828 | ||
839d52a5 | 829 | notif_iter_status = bt_notif_iter_get_next_notification( |
94cf822e | 830 | ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification); |
4f1f88a6 | 831 | |
4f1f88a6 | 832 | switch (notif_iter_status) { |
839d52a5 | 833 | case BT_NOTIF_ITER_STATUS_EOF: |
4f1f88a6 PP |
834 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
835 | break; | |
839d52a5 | 836 | case BT_NOTIF_ITER_STATUS_OK: |
4f1f88a6 PP |
837 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
838 | break; | |
839d52a5 | 839 | case BT_NOTIF_ITER_STATUS_AGAIN: |
4f1f88a6 PP |
840 | /* |
841 | * Should not make it this far as this is | |
842 | * medium-specific; there is nothing for the user to do | |
843 | * and it should have been handled upstream. | |
844 | */ | |
0fbb9a9f | 845 | abort(); |
839d52a5 PP |
846 | case BT_NOTIF_ITER_STATUS_INVAL: |
847 | case BT_NOTIF_ITER_STATUS_ERROR: | |
4f1f88a6 PP |
848 | default: |
849 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
850 | break; | |
851 | } | |
852 | ||
853 | return ret; | |
854 | } | |
94cf822e PP |
855 | |
856 | BT_HIDDEN | |
a6918753 | 857 | int ctf_fs_ds_file_borrow_packet_header_context_fields( |
97ade20b | 858 | struct ctf_fs_ds_file *ds_file, |
839d52a5 PP |
859 | struct bt_field **packet_header_field, |
860 | struct bt_field **packet_context_field) | |
94cf822e | 861 | { |
839d52a5 | 862 | enum bt_notif_iter_status notif_iter_status; |
94cf822e PP |
863 | int ret = 0; |
864 | ||
8b45963b | 865 | BT_ASSERT(ds_file); |
a6918753 | 866 | notif_iter_status = bt_notif_iter_borrow_packet_header_context_fields( |
94cf822e PP |
867 | ds_file->notif_iter, packet_header_field, packet_context_field); |
868 | switch (notif_iter_status) { | |
839d52a5 PP |
869 | case BT_NOTIF_ITER_STATUS_EOF: |
870 | case BT_NOTIF_ITER_STATUS_OK: | |
94cf822e | 871 | break; |
839d52a5 | 872 | case BT_NOTIF_ITER_STATUS_AGAIN: |
0fbb9a9f | 873 | abort(); |
839d52a5 PP |
874 | case BT_NOTIF_ITER_STATUS_INVAL: |
875 | case BT_NOTIF_ITER_STATUS_ERROR: | |
94cf822e PP |
876 | default: |
877 | goto error; | |
878 | break; | |
879 | } | |
880 | ||
881 | goto end; | |
882 | ||
883 | error: | |
884 | ret = -1; | |
885 | ||
94cf822e | 886 | end: |
94cf822e PP |
887 | return ret; |
888 | } | |
97ade20b JG |
889 | |
890 | BT_HIDDEN | |
891 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
892 | { | |
893 | if (!index) { | |
894 | return; | |
895 | } | |
896 | ||
897 | if (index->entries) { | |
898 | g_array_free(index->entries, TRUE); | |
899 | } | |
900 | g_free(index); | |
901 | } |