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