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> |
9d408fca | 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" | |
f6ccaed9 | 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 | { |
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 |
50842bdc | 78 | enum bt_notif_iter_medium_status ds_file_mmap_next( |
94cf822e | 79 | struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 80 | { |
50842bdc 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 | ||
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) { |
50842bdc | 102 | ret = BT_NOTIF_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); |
50842bdc | 121 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
122 | end: |
123 | return ret; | |
124 | } | |
125 | ||
5b29e799 | 126 | static |
50842bdc | 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 | { | |
50842bdc 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); |
50842bdc | 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) { |
50842bdc | 151 | case BT_NOTIF_ITER_MEDIUM_STATUS_OK: |
e0f6a64a | 152 | break; |
50842bdc | 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: | |
50842bdc | 169 | status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
170 | |
171 | end: | |
172 | return status; | |
173 | } | |
174 | ||
5b29e799 | 175 | static |
50842bdc PP |
176 | struct bt_stream *medop_get_stream( |
177 | struct bt_stream_class *stream_class, uint64_t stream_id, | |
b92735af | 178 | void *data) |
e98a2d6e | 179 | { |
94cf822e | 180 | struct ctf_fs_ds_file *ds_file = data; |
50842bdc PP |
181 | struct bt_stream_class *ds_file_stream_class; |
182 | struct bt_stream *stream = NULL; | |
94cf822e | 183 | |
0b29603d | 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 | ||
9e0c8dbb | 199 | static |
50842bdc PP |
200 | enum bt_notif_iter_medium_status medop_seek( |
201 | enum bt_notif_iter_seek_whence whence, off_t offset, | |
9e0c8dbb JG |
202 | void *data) |
203 | { | |
50842bdc PP |
204 | enum bt_notif_iter_medium_status ret = |
205 | BT_NOTIF_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 | ||
50842bdc | 209 | if (whence != BT_NOTIF_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); | |
50842bdc | 214 | ret = BT_NOTIF_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; | |
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 " | |
63489ca2 | 228 | "file mapping: offset=%jd, mmap-offset=%jd, " |
9e0c8dbb 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) { | |
50842bdc | 233 | ret = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
9e0c8dbb 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); | |
50842bdc | 240 | if (ret != BT_NOTIF_ITER_MEDIUM_STATUS_OK) { |
9e0c8dbb 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 | ||
6de92955 | 252 | BT_HIDDEN |
50842bdc | 253 | struct bt_notif_iter_medium_ops ctf_fs_ds_file_medops = { |
e98a2d6e PP |
254 | .request_bytes = medop_request_bytes, |
255 | .get_stream = medop_get_stream, | |
9e0c8dbb | 256 | .seek = medop_seek, |
e98a2d6e | 257 | }; |
6de92955 | 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 | |
9e0c8dbb 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 |
0b29603d | 294 | struct bt_clock_class *borrow_field_mapped_clock_class( |
50842bdc | 295 | struct bt_field *field) |
97ade20b | 296 | { |
50842bdc PP |
297 | struct bt_field_type *field_type; |
298 | struct bt_clock_class *clock_class = NULL; | |
97ade20b | 299 | |
0b29603d | 300 | field_type = bt_field_borrow_type(field); |
97ade20b JG |
301 | if (!field_type) { |
302 | goto end; | |
303 | } | |
304 | ||
0b29603d 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 | } | |
0b29603d | 310 | |
97ade20b | 311 | end: |
97ade20b JG |
312 | return clock_class; |
313 | } | |
314 | ||
315 | static | |
0b29603d | 316 | int borrow_packet_bounds_from_packet_context( |
50842bdc 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 | { |
9e0c8dbb | 323 | int ret = 0; |
50842bdc 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 | |
0b29603d | 329 | timestamp_begin = bt_field_structure_borrow_field_by_name( |
9e0c8dbb 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 | ||
0b29603d | 337 | timestamp_begin_cc = borrow_field_mapped_clock_class(timestamp_begin); |
97ade20b | 338 | if (!timestamp_begin_cc) { |
9e0c8dbb | 339 | BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_begin."); |
97ade20b | 340 | } |
97ade20b | 341 | |
0b29603d | 342 | timestamp_end = bt_field_structure_borrow_field_by_name( |
9e0c8dbb 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 | ||
0b29603d | 350 | timestamp_end_cc = borrow_field_mapped_clock_class(timestamp_end); |
97ade20b | 351 | if (!timestamp_end_cc) { |
9e0c8dbb | 352 | BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_end."); |
97ade20b JG |
353 | } |
354 | ||
355 | if (_timestamp_begin_cc) { | |
0b29603d | 356 | *_timestamp_begin_cc = timestamp_begin_cc; |
97ade20b | 357 | } |
9e0c8dbb | 358 | if (_timestamp_begin) { |
0b29603d | 359 | *_timestamp_begin = timestamp_begin; |
9e0c8dbb | 360 | } |
97ade20b | 361 | if (_timestamp_end_cc) { |
0b29603d | 362 | *_timestamp_end_cc = timestamp_end_cc; |
97ade20b | 363 | } |
9e0c8dbb | 364 | if (_timestamp_end) { |
0b29603d | 365 | *_timestamp_end = timestamp_end; |
9e0c8dbb | 366 | } |
97ade20b | 367 | end: |
9e0c8dbb JG |
368 | return ret; |
369 | } | |
370 | ||
371 | static | |
0b29603d | 372 | int borrow_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file, |
50842bdc PP |
373 | struct bt_clock_class **timestamp_begin_cc, |
374 | struct bt_clock_class **timestamp_end_cc) | |
9e0c8dbb | 375 | { |
50842bdc | 376 | struct bt_field *packet_context = NULL; |
9e0c8dbb JG |
377 | int ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file, |
378 | NULL, &packet_context); | |
379 | ||
380 | if (ret || !packet_context) { | |
381 | BT_LOGD("Cannot retrieve packet context field of stream \'%s\'", | |
382 | ds_file->file->path->str); | |
383 | ret = -1; | |
384 | goto end; | |
385 | } | |
386 | ||
0b29603d | 387 | ret = borrow_packet_bounds_from_packet_context(packet_context, |
9e0c8dbb JG |
388 | timestamp_begin_cc, NULL, |
389 | timestamp_end_cc, NULL); | |
0b29603d | 390 | |
9e0c8dbb JG |
391 | end: |
392 | bt_put(packet_context); | |
97ade20b JG |
393 | return ret; |
394 | } | |
395 | ||
396 | static | |
50842bdc | 397 | int convert_cycles_to_ns(struct bt_clock_class *clock_class, |
97ade20b | 398 | uint64_t cycles, int64_t *ns) |
b6c3dcb2 JG |
399 | { |
400 | int ret = 0; | |
50842bdc | 401 | struct bt_clock_value *clock_value; |
97ade20b | 402 | |
f6ccaed9 | 403 | BT_ASSERT(ns); |
50842bdc | 404 | clock_value = bt_clock_value_create(clock_class, cycles); |
97ade20b JG |
405 | if (!clock_value) { |
406 | ret = -1; | |
407 | goto end; | |
408 | } | |
409 | ||
50842bdc | 410 | ret = bt_clock_value_get_value_ns_from_epoch(clock_value, ns); |
97ade20b JG |
411 | if (ret) { |
412 | goto end; | |
413 | } | |
414 | end: | |
415 | bt_put(clock_value); | |
416 | return ret; | |
417 | } | |
418 | ||
419 | static | |
420 | struct ctf_fs_ds_index *build_index_from_idx_file( | |
421 | struct ctf_fs_ds_file *ds_file) | |
422 | { | |
423 | int ret; | |
b6c3dcb2 JG |
424 | gchar *directory = NULL; |
425 | gchar *basename = NULL; | |
426 | GString *index_basename = NULL; | |
427 | gchar *index_file_path = NULL; | |
428 | GMappedFile *mapped_file = NULL; | |
429 | gsize filesize; | |
97ade20b JG |
430 | const char *mmap_begin = NULL, *file_pos = NULL; |
431 | const struct ctf_packet_index_file_hdr *header = NULL; | |
432 | struct ctf_fs_ds_index *index = NULL; | |
433 | struct ctf_fs_ds_index_entry *index_entry = NULL; | |
b6c3dcb2 JG |
434 | uint64_t total_packets_size = 0; |
435 | size_t file_index_entry_size; | |
436 | size_t file_entry_count; | |
437 | size_t i; | |
50842bdc PP |
438 | struct bt_clock_class *timestamp_begin_cc = NULL; |
439 | struct bt_clock_class *timestamp_end_cc = NULL; | |
97ade20b JG |
440 | |
441 | BT_LOGD("Building index from .idx file of stream file %s", | |
442 | ds_file->file->path->str); | |
443 | ||
0b29603d | 444 | ret = borrow_ds_file_packet_bounds_clock_classes(ds_file, |
97ade20b JG |
445 | ×tamp_begin_cc, ×tamp_end_cc); |
446 | if (ret) { | |
9e0c8dbb JG |
447 | BT_LOGD_STR("Cannot get clock classes of \"timestamp_begin\" " |
448 | "and \"timestamp_end\" fields"); | |
97ade20b JG |
449 | goto error; |
450 | } | |
b6c3dcb2 JG |
451 | |
452 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 453 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 454 | if (!basename) { |
97ade20b | 455 | BT_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 456 | ds_file->file->path->str); |
97ade20b | 457 | goto error; |
b6c3dcb2 JG |
458 | } |
459 | ||
94cf822e | 460 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 461 | if (!directory) { |
97ade20b | 462 | BT_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 463 | ds_file->file->path->str); |
97ade20b | 464 | goto error; |
b6c3dcb2 JG |
465 | } |
466 | ||
467 | index_basename = g_string_new(basename); | |
468 | if (!index_basename) { | |
9e0c8dbb | 469 | BT_LOGE_STR("Cannot allocate index file basename string"); |
97ade20b | 470 | goto error; |
b6c3dcb2 JG |
471 | } |
472 | ||
473 | g_string_append(index_basename, ".idx"); | |
474 | index_file_path = g_build_filename(directory, "index", | |
475 | index_basename->str, NULL); | |
476 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 477 | if (!mapped_file) { |
97ade20b | 478 | BT_LOGD("Cannot create new mapped file %s", |
55314f2a | 479 | index_file_path); |
97ade20b | 480 | goto error; |
06367fa3 | 481 | } |
d14940a7 JG |
482 | |
483 | /* | |
484 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
485 | * Traces with such large indexes have never been seen in the wild, | |
486 | * but this would need to be adjusted to support them. | |
487 | */ | |
b6c3dcb2 JG |
488 | filesize = g_mapped_file_get_length(mapped_file); |
489 | if (filesize < sizeof(*header)) { | |
d14940a7 JG |
490 | BT_LOGW("Invalid LTTng trace index file: " |
491 | "file size (%zu bytes) < header size (%zu bytes)", | |
492 | filesize, sizeof(*header)); | |
97ade20b | 493 | goto error; |
b6c3dcb2 JG |
494 | } |
495 | ||
496 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
497 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
498 | ||
499 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
500 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
9e0c8dbb | 501 | BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 502 | goto error; |
b6c3dcb2 | 503 | } |
b6c3dcb2 JG |
504 | |
505 | file_index_entry_size = be32toh(header->packet_index_len); | |
506 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
97ade20b | 507 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
d14940a7 JG |
508 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
509 | "(%zu bytes) is not a multiple of the index entry size " | |
510 | "(%zu bytes)", (filesize - sizeof(*header)), | |
511 | sizeof(*header)); | |
97ade20b | 512 | goto error; |
b6c3dcb2 JG |
513 | } |
514 | ||
97ade20b JG |
515 | index = ctf_fs_ds_index_create(file_entry_count); |
516 | if (!index) { | |
517 | goto error; | |
b6c3dcb2 | 518 | } |
97ade20b JG |
519 | |
520 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( | |
521 | index->entries, struct ctf_fs_ds_index_entry, 0); | |
b6c3dcb2 JG |
522 | for (i = 0; i < file_entry_count; i++) { |
523 | struct ctf_packet_index *file_index = | |
524 | (struct ctf_packet_index *) file_pos; | |
525 | uint64_t packet_size = be64toh(file_index->packet_size); | |
526 | ||
527 | if (packet_size % CHAR_BIT) { | |
d14940a7 | 528 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 529 | goto error; |
b6c3dcb2 JG |
530 | } |
531 | ||
532 | /* Convert size in bits to bytes. */ | |
533 | packet_size /= CHAR_BIT; | |
97ade20b | 534 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 535 | |
97ade20b JG |
536 | index_entry->offset = be64toh(file_index->offset); |
537 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { | |
d14940a7 JG |
538 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
539 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, | |
540 | (index_entry - 1)->offset, index_entry->offset); | |
97ade20b | 541 | goto error; |
b6c3dcb2 JG |
542 | } |
543 | ||
97ade20b JG |
544 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
545 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
546 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
d14940a7 JG |
547 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
548 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, | |
549 | index_entry->timestamp_begin, | |
550 | index_entry->timestamp_end); | |
97ade20b JG |
551 | goto error; |
552 | } | |
553 | ||
554 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
555 | ret = convert_cycles_to_ns(timestamp_begin_cc, | |
556 | index_entry->timestamp_begin, | |
557 | &index_entry->timestamp_begin_ns); | |
558 | if (ret) { | |
9e0c8dbb | 559 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
560 | goto error; |
561 | } | |
562 | ret = convert_cycles_to_ns(timestamp_end_cc, | |
563 | index_entry->timestamp_end, | |
564 | &index_entry->timestamp_end_ns); | |
565 | if (ret) { | |
9e0c8dbb | 566 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 567 | goto error; |
b6c3dcb2 JG |
568 | } |
569 | ||
570 | total_packets_size += packet_size; | |
571 | file_pos += file_index_entry_size; | |
97ade20b | 572 | index_entry++; |
b6c3dcb2 JG |
573 | } |
574 | ||
575 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 576 | if (ds_file->file->size != total_packets_size) { |
d14940a7 | 577 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
9e0c8dbb | 578 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
d14940a7 | 579 | ds_file->file->size, total_packets_size); |
97ade20b | 580 | goto error; |
b6c3dcb2 JG |
581 | } |
582 | end: | |
583 | g_free(directory); | |
584 | g_free(basename); | |
585 | g_free(index_file_path); | |
06367fa3 JG |
586 | if (index_basename) { |
587 | g_string_free(index_basename, TRUE); | |
588 | } | |
589 | if (mapped_file) { | |
590 | g_mapped_file_unref(mapped_file); | |
591 | } | |
97ade20b JG |
592 | return index; |
593 | error: | |
594 | ctf_fs_ds_index_destroy(index); | |
595 | index = NULL; | |
b6c3dcb2 JG |
596 | goto end; |
597 | } | |
598 | ||
9e0c8dbb JG |
599 | static |
600 | int init_index_entry(struct ctf_fs_ds_index_entry *entry, | |
50842bdc | 601 | struct bt_field *packet_context, off_t packet_size, |
9e0c8dbb JG |
602 | off_t packet_offset) |
603 | { | |
604 | int ret; | |
50842bdc PP |
605 | struct bt_field *timestamp_begin = NULL; |
606 | struct bt_field *timestamp_end = NULL; | |
607 | struct bt_clock_class *timestamp_begin_cc = NULL; | |
608 | struct bt_clock_class *timestamp_end_cc = NULL; | |
9e0c8dbb | 609 | |
0b29603d | 610 | ret = borrow_packet_bounds_from_packet_context(packet_context, |
9e0c8dbb JG |
611 | ×tamp_begin_cc, ×tamp_begin, |
612 | ×tamp_end_cc, ×tamp_end); | |
613 | if (ret || !timestamp_begin_cc || !timestamp_begin || | |
614 | !timestamp_end_cc || ! timestamp_end) { | |
615 | BT_LOGD_STR("Failed to determine time bound fields of packet."); | |
616 | goto end; | |
617 | } | |
618 | ||
f6ccaed9 | 619 | BT_ASSERT(packet_offset >= 0); |
9e0c8dbb JG |
620 | entry->offset = packet_offset; |
621 | ||
f6ccaed9 | 622 | BT_ASSERT(packet_size >= 0); |
9e0c8dbb JG |
623 | entry->packet_size = packet_size; |
624 | ||
3dca2276 | 625 | ret = bt_field_integer_unsigned_get_value(timestamp_begin, |
9e0c8dbb JG |
626 | &entry->timestamp_begin); |
627 | if (ret) { | |
628 | goto end; | |
629 | } | |
3dca2276 | 630 | ret = bt_field_integer_unsigned_get_value(timestamp_end, |
9e0c8dbb JG |
631 | &entry->timestamp_end); |
632 | if (ret) { | |
633 | goto end; | |
634 | } | |
635 | ||
636 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
637 | ret = convert_cycles_to_ns(timestamp_begin_cc, | |
638 | entry->timestamp_begin, | |
639 | &entry->timestamp_begin_ns); | |
640 | if (ret) { | |
641 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
642 | goto end; | |
643 | } | |
644 | ||
645 | ret = convert_cycles_to_ns(timestamp_end_cc, | |
646 | entry->timestamp_end, | |
647 | &entry->timestamp_end_ns); | |
648 | if (ret) { | |
649 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
650 | goto end; | |
651 | } | |
0b29603d | 652 | |
9e0c8dbb | 653 | end: |
9e0c8dbb JG |
654 | return ret; |
655 | } | |
656 | ||
657 | static | |
658 | struct ctf_fs_ds_index *build_index_from_stream_file( | |
659 | struct ctf_fs_ds_file *ds_file) | |
660 | { | |
661 | int ret; | |
662 | struct ctf_fs_ds_index *index = NULL; | |
50842bdc PP |
663 | enum bt_notif_iter_status iter_status; |
664 | struct bt_field *packet_context = NULL; | |
9e0c8dbb JG |
665 | |
666 | BT_LOGD("Indexing stream file %s", ds_file->file->path->str); | |
667 | ||
668 | index = ctf_fs_ds_index_create(0); | |
669 | if (!index) { | |
670 | goto error; | |
671 | } | |
672 | ||
673 | do { | |
674 | off_t current_packet_offset; | |
675 | off_t next_packet_offset; | |
676 | off_t current_packet_size, current_packet_size_bytes; | |
677 | struct ctf_fs_ds_index_entry *entry; | |
678 | ||
50842bdc | 679 | iter_status = bt_notif_iter_get_packet_header_context_fields( |
9e0c8dbb | 680 | ds_file->notif_iter, NULL, &packet_context); |
50842bdc PP |
681 | if (iter_status != BT_NOTIF_ITER_STATUS_OK) { |
682 | if (iter_status == BT_NOTIF_ITER_STATUS_EOF) { | |
9e0c8dbb JG |
683 | break; |
684 | } | |
685 | goto error; | |
686 | } | |
687 | current_packet_offset = | |
50842bdc | 688 | bt_notif_iter_get_current_packet_offset( |
9e0c8dbb JG |
689 | ds_file->notif_iter); |
690 | if (current_packet_offset < 0) { | |
691 | BT_LOGE_STR("Cannot get the current packet's offset."); | |
692 | goto error; | |
693 | } | |
694 | ||
50842bdc | 695 | current_packet_size = bt_notif_iter_get_current_packet_size( |
9e0c8dbb JG |
696 | ds_file->notif_iter); |
697 | if (current_packet_size < 0) { | |
698 | BT_LOGE("Cannot get packet size: packet-offset=%jd", | |
699 | current_packet_offset); | |
700 | goto error; | |
701 | } | |
702 | current_packet_size_bytes = | |
703 | ((current_packet_size + 7) & ~7) / CHAR_BIT; | |
704 | ||
705 | if (current_packet_offset + current_packet_size_bytes > | |
706 | ds_file->file->size) { | |
707 | BT_LOGW("Invalid packet size reported in file: stream=\"%s\", " | |
708 | "packet-offset=%jd, packet-size-bytes=%jd, " | |
709 | "file-size=%jd", | |
710 | ds_file->file->path->str, | |
711 | current_packet_offset, | |
712 | current_packet_size_bytes, | |
713 | ds_file->file->size); | |
714 | goto error; | |
715 | } | |
716 | ||
717 | next_packet_offset = current_packet_offset + | |
718 | current_packet_size_bytes; | |
719 | BT_LOGD("Seeking to next packet: current-packet-offset=%jd, " | |
720 | "next-packet-offset=%jd", current_packet_offset, | |
721 | next_packet_offset); | |
722 | ||
723 | entry = ctf_fs_ds_index_add_new_entry(index); | |
724 | if (!entry) { | |
725 | BT_LOGE_STR("Failed to allocate a new index entry."); | |
726 | goto error; | |
727 | } | |
728 | ||
729 | ret = init_index_entry(entry, packet_context, | |
730 | current_packet_size_bytes, | |
731 | current_packet_offset); | |
732 | if (ret) { | |
733 | goto error; | |
734 | } | |
735 | ||
50842bdc | 736 | iter_status = bt_notif_iter_seek(ds_file->notif_iter, |
9e0c8dbb JG |
737 | next_packet_offset); |
738 | BT_PUT(packet_context); | |
50842bdc | 739 | } while (iter_status == BT_NOTIF_ITER_STATUS_OK); |
9e0c8dbb | 740 | |
50842bdc | 741 | if (iter_status != BT_NOTIF_ITER_STATUS_EOF) { |
9e0c8dbb JG |
742 | goto error; |
743 | } | |
744 | end: | |
745 | bt_put(packet_context); | |
746 | return index; | |
747 | error: | |
748 | ctf_fs_ds_index_destroy(index); | |
749 | index = NULL; | |
750 | goto end; | |
751 | } | |
752 | ||
e7a4393b | 753 | BT_HIDDEN |
94cf822e PP |
754 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
755 | struct ctf_fs_trace *ctf_fs_trace, | |
50842bdc PP |
756 | struct bt_notif_iter *notif_iter, |
757 | struct bt_stream *stream, const char *path) | |
e98a2d6e | 758 | { |
b6c3dcb2 | 759 | int ret; |
55314f2a | 760 | const size_t page_size = bt_common_get_page_size(); |
94cf822e | 761 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 762 | |
94cf822e | 763 | if (!ds_file) { |
e98a2d6e PP |
764 | goto error; |
765 | } | |
766 | ||
55314f2a | 767 | ds_file->file = ctf_fs_file_create(); |
94cf822e | 768 | if (!ds_file->file) { |
4f1f88a6 PP |
769 | goto error; |
770 | } | |
771 | ||
94cf822e PP |
772 | ds_file->stream = bt_get(stream); |
773 | ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map); | |
774 | g_string_assign(ds_file->file->path, path); | |
55314f2a | 775 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
776 | if (ret) { |
777 | goto error; | |
778 | } | |
779 | ||
6de92955 | 780 | ds_file->notif_iter = notif_iter; |
50842bdc | 781 | bt_notif_iter_set_medops_data(ds_file->notif_iter, ds_file); |
94cf822e | 782 | if (!ds_file->notif_iter) { |
e98a2d6e PP |
783 | goto error; |
784 | } | |
5b29e799 | 785 | |
55314f2a | 786 | ds_file->mmap_max_len = page_size * 2048; |
94cf822e | 787 | |
e98a2d6e | 788 | goto end; |
1a9f7075 | 789 | |
e98a2d6e | 790 | error: |
78586d8a | 791 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
792 | ctf_fs_ds_file_destroy(ds_file); |
793 | ds_file = NULL; | |
1a9f7075 | 794 | |
e98a2d6e | 795 | end: |
94cf822e | 796 | return ds_file; |
e98a2d6e PP |
797 | } |
798 | ||
97ade20b JG |
799 | BT_HIDDEN |
800 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
801 | struct ctf_fs_ds_file *ds_file) | |
802 | { | |
9e0c8dbb JG |
803 | struct ctf_fs_ds_index *index; |
804 | ||
805 | index = build_index_from_idx_file(ds_file); | |
806 | if (index) { | |
807 | goto end; | |
808 | } | |
809 | ||
810 | BT_LOGD("Failed to build index from .index file; " | |
811 | "falling back to stream indexing."); | |
812 | index = build_index_from_stream_file(ds_file); | |
813 | end: | |
814 | return index; | |
97ade20b JG |
815 | } |
816 | ||
5b29e799 | 817 | BT_HIDDEN |
94cf822e | 818 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 819 | { |
94cf822e | 820 | if (!ds_file) { |
5b29e799 | 821 | return; |
043e2020 JG |
822 | } |
823 | ||
94cf822e PP |
824 | bt_put(ds_file->cc_prio_map); |
825 | bt_put(ds_file->stream); | |
826 | (void) ds_file_munmap(ds_file); | |
0982a26d | 827 | |
94cf822e PP |
828 | if (ds_file->file) { |
829 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
830 | } |
831 | ||
94cf822e | 832 | g_free(ds_file); |
e98a2d6e | 833 | } |
4f1f88a6 PP |
834 | |
835 | BT_HIDDEN | |
90157d89 | 836 | struct bt_notification_iterator_next_method_return ctf_fs_ds_file_next( |
94cf822e | 837 | struct ctf_fs_ds_file *ds_file) |
4f1f88a6 | 838 | { |
50842bdc | 839 | enum bt_notif_iter_status notif_iter_status; |
90157d89 | 840 | struct bt_notification_iterator_next_method_return ret = { |
4f1f88a6 PP |
841 | .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR, |
842 | .notification = NULL, | |
843 | }; | |
844 | ||
50842bdc | 845 | notif_iter_status = bt_notif_iter_get_next_notification( |
94cf822e | 846 | ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification); |
4f1f88a6 | 847 | |
4f1f88a6 | 848 | switch (notif_iter_status) { |
50842bdc | 849 | case BT_NOTIF_ITER_STATUS_EOF: |
4f1f88a6 PP |
850 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
851 | break; | |
50842bdc | 852 | case BT_NOTIF_ITER_STATUS_OK: |
4f1f88a6 PP |
853 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
854 | break; | |
50842bdc | 855 | case BT_NOTIF_ITER_STATUS_AGAIN: |
4f1f88a6 PP |
856 | /* |
857 | * Should not make it this far as this is | |
858 | * medium-specific; there is nothing for the user to do | |
859 | * and it should have been handled upstream. | |
860 | */ | |
0fbb9a9f | 861 | abort(); |
50842bdc PP |
862 | case BT_NOTIF_ITER_STATUS_INVAL: |
863 | case BT_NOTIF_ITER_STATUS_ERROR: | |
4f1f88a6 PP |
864 | default: |
865 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
866 | break; | |
867 | } | |
868 | ||
869 | return ret; | |
870 | } | |
94cf822e PP |
871 | |
872 | BT_HIDDEN | |
873 | int ctf_fs_ds_file_get_packet_header_context_fields( | |
97ade20b | 874 | struct ctf_fs_ds_file *ds_file, |
50842bdc PP |
875 | struct bt_field **packet_header_field, |
876 | struct bt_field **packet_context_field) | |
94cf822e | 877 | { |
50842bdc | 878 | enum bt_notif_iter_status notif_iter_status; |
94cf822e PP |
879 | int ret = 0; |
880 | ||
f6ccaed9 | 881 | BT_ASSERT(ds_file); |
50842bdc | 882 | notif_iter_status = bt_notif_iter_get_packet_header_context_fields( |
94cf822e PP |
883 | ds_file->notif_iter, packet_header_field, packet_context_field); |
884 | switch (notif_iter_status) { | |
50842bdc PP |
885 | case BT_NOTIF_ITER_STATUS_EOF: |
886 | case BT_NOTIF_ITER_STATUS_OK: | |
94cf822e | 887 | break; |
50842bdc | 888 | case BT_NOTIF_ITER_STATUS_AGAIN: |
0fbb9a9f | 889 | abort(); |
50842bdc PP |
890 | case BT_NOTIF_ITER_STATUS_INVAL: |
891 | case BT_NOTIF_ITER_STATUS_ERROR: | |
94cf822e PP |
892 | default: |
893 | goto error; | |
894 | break; | |
895 | } | |
896 | ||
897 | goto end; | |
898 | ||
899 | error: | |
900 | ret = -1; | |
901 | ||
902 | if (packet_header_field) { | |
903 | bt_put(*packet_header_field); | |
904 | } | |
905 | ||
906 | if (packet_context_field) { | |
907 | bt_put(*packet_context_field); | |
908 | } | |
909 | ||
910 | end: | |
94cf822e PP |
911 | return ret; |
912 | } | |
97ade20b JG |
913 | |
914 | BT_HIDDEN | |
915 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
916 | { | |
917 | if (!index) { | |
918 | return; | |
919 | } | |
920 | ||
921 | if (index->entries) { | |
922 | g_array_free(index->entries, TRUE); | |
923 | } | |
924 | g_free(index); | |
925 | } |