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> |
e98a2d6e | 34 | #include <babeltrace/ctf-ir/stream.h> |
b2e0c907 | 35 | #include <babeltrace/graph/notification-iterator.h> |
4f1f88a6 PP |
36 | #include <babeltrace/graph/notification-stream.h> |
37 | #include <babeltrace/graph/notification-event.h> | |
38 | #include <babeltrace/graph/notification-packet.h> | |
55314f2a | 39 | #include <babeltrace/common-internal.h> |
78586d8a JG |
40 | #include "file.h" |
41 | #include "metadata.h" | |
42 | #include "../common/notif-iter/notif-iter.h" | |
43 | #include <assert.h> | |
94cf822e | 44 | #include "data-stream-file.h" |
e9383dfd | 45 | #include <string.h> |
e98a2d6e | 46 | |
55314f2a JG |
47 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS" |
48 | #include "logging.h" | |
e98a2d6e | 49 | |
4f1f88a6 | 50 | static inline |
94cf822e | 51 | size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 52 | { |
2c701ca6 | 53 | return ds_file->mmap_len - ds_file->request_offset; |
e98a2d6e PP |
54 | } |
55 | ||
5b29e799 | 56 | static |
94cf822e | 57 | int ds_file_munmap(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 58 | { |
fc9a526c | 59 | int ret = 0; |
e98a2d6e | 60 | |
d238196d | 61 | if (!ds_file || !ds_file->mmap_addr) { |
94cf822e PP |
62 | goto end; |
63 | } | |
64 | ||
04394229 | 65 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { |
cd232764 JG |
66 | BT_LOGE_ERRNO("Cannot memory-unmap file", |
67 | ": address=%p, size=%zu, file_path=\"%s\", file=%p", | |
94cf822e | 68 | ds_file->mmap_addr, ds_file->mmap_len, |
9e0c8dbb | 69 | ds_file->file ? ds_file->file->path->str : "NULL", |
cd232764 | 70 | ds_file->file ? ds_file->file->fp : NULL); |
fc9a526c JG |
71 | ret = -1; |
72 | goto end; | |
e98a2d6e | 73 | } |
94cf822e PP |
74 | |
75 | ds_file->mmap_addr = NULL; | |
76 | ||
fc9a526c JG |
77 | end: |
78 | return ret; | |
e98a2d6e PP |
79 | } |
80 | ||
5b29e799 | 81 | static |
94cf822e PP |
82 | enum bt_ctf_notif_iter_medium_status ds_file_mmap_next( |
83 | struct ctf_fs_ds_file *ds_file) | |
e98a2d6e | 84 | { |
e0f6a64a JG |
85 | enum bt_ctf_notif_iter_medium_status ret = |
86 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
e98a2d6e PP |
87 | |
88 | /* Unmap old region */ | |
94cf822e PP |
89 | if (ds_file->mmap_addr) { |
90 | if (ds_file_munmap(ds_file)) { | |
e98a2d6e PP |
91 | goto error; |
92 | } | |
93 | ||
2f5a009b | 94 | /* |
2c701ca6 | 95 | * mmap_len is guaranteed to be page-aligned except on the |
2f5a009b JG |
96 | * last mapping where it may not be possible (since the file's |
97 | * size itself may not be a page multiple). | |
98 | */ | |
2c701ca6 | 99 | ds_file->mmap_offset += ds_file->mmap_len; |
94cf822e | 100 | ds_file->request_offset = 0; |
e98a2d6e PP |
101 | } |
102 | ||
2c701ca6 | 103 | ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset, |
94cf822e | 104 | ds_file->mmap_max_len); |
2c701ca6 | 105 | if (ds_file->mmap_len == 0) { |
e0f6a64a JG |
106 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; |
107 | goto end; | |
108 | } | |
e98a2d6e | 109 | /* Map new region */ |
94cf822e | 110 | assert(ds_file->mmap_len); |
04394229 | 111 | ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len, |
94cf822e PP |
112 | PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp), |
113 | ds_file->mmap_offset); | |
114 | if (ds_file->mmap_addr == MAP_FAILED) { | |
1974687e | 115 | BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s", |
94cf822e | 116 | ds_file->mmap_len, ds_file->file->path->str, |
1974687e | 117 | ds_file->file->fp, (intmax_t) ds_file->mmap_offset, |
78586d8a | 118 | strerror(errno)); |
e98a2d6e PP |
119 | goto error; |
120 | } | |
121 | ||
122 | goto end; | |
e98a2d6e | 123 | error: |
94cf822e | 124 | ds_file_munmap(ds_file); |
e0f6a64a | 125 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
126 | end: |
127 | return ret; | |
128 | } | |
129 | ||
5b29e799 JG |
130 | static |
131 | enum bt_ctf_notif_iter_medium_status medop_request_bytes( | |
e98a2d6e PP |
132 | size_t request_sz, uint8_t **buffer_addr, |
133 | size_t *buffer_sz, void *data) | |
134 | { | |
135 | enum bt_ctf_notif_iter_medium_status status = | |
136 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
94cf822e | 137 | struct ctf_fs_ds_file *ds_file = data; |
e98a2d6e PP |
138 | |
139 | if (request_sz == 0) { | |
140 | goto end; | |
141 | } | |
142 | ||
e98a2d6e | 143 | /* Check if we have at least one memory-mapped byte left */ |
94cf822e | 144 | if (remaining_mmap_bytes(ds_file) == 0) { |
e98a2d6e | 145 | /* Are we at the end of the file? */ |
94cf822e | 146 | if (ds_file->mmap_offset >= ds_file->file->size) { |
55314f2a | 147 | BT_LOGD("Reached end of file \"%s\" (%p)", |
94cf822e | 148 | ds_file->file->path->str, ds_file->file->fp); |
e98a2d6e PP |
149 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; |
150 | goto end; | |
151 | } | |
152 | ||
94cf822e | 153 | status = ds_file_mmap_next(ds_file); |
e0f6a64a JG |
154 | switch (status) { |
155 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK: | |
156 | break; | |
157 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF: | |
158 | goto end; | |
159 | default: | |
55314f2a | 160 | BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)", |
94cf822e PP |
161 | ds_file->file->path->str, |
162 | ds_file->file->fp); | |
e98a2d6e PP |
163 | goto error; |
164 | } | |
165 | } | |
166 | ||
94cf822e PP |
167 | *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz); |
168 | *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset; | |
169 | ds_file->request_offset += *buffer_sz; | |
e98a2d6e PP |
170 | goto end; |
171 | ||
172 | error: | |
173 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; | |
174 | ||
175 | end: | |
176 | return status; | |
177 | } | |
178 | ||
5b29e799 JG |
179 | static |
180 | struct bt_ctf_stream *medop_get_stream( | |
b92735af PP |
181 | struct bt_ctf_stream_class *stream_class, uint64_t stream_id, |
182 | void *data) | |
e98a2d6e | 183 | { |
94cf822e PP |
184 | struct ctf_fs_ds_file *ds_file = data; |
185 | struct bt_ctf_stream_class *ds_file_stream_class; | |
186 | struct bt_ctf_stream *stream = NULL; | |
187 | ||
188 | ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream); | |
189 | bt_put(ds_file_stream_class); | |
190 | ||
191 | if (stream_class != ds_file_stream_class) { | |
192 | /* | |
193 | * Not supported: two packets described by two different | |
194 | * stream classes within the same data stream file. | |
195 | */ | |
196 | goto end; | |
e98a2d6e PP |
197 | } |
198 | ||
94cf822e PP |
199 | stream = ds_file->stream; |
200 | ||
201 | end: | |
202 | return stream; | |
e98a2d6e PP |
203 | } |
204 | ||
9e0c8dbb JG |
205 | static |
206 | enum bt_ctf_notif_iter_medium_status medop_seek( | |
207 | enum bt_ctf_notif_iter_seek_whence whence, off_t offset, | |
208 | void *data) | |
209 | { | |
210 | enum bt_ctf_notif_iter_medium_status ret = | |
211 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
212 | struct ctf_fs_ds_file *ds_file = data; | |
213 | off_t file_size = ds_file->file->size; | |
214 | ||
215 | if (whence != BT_CTF_NOTIF_ITER_SEEK_WHENCE_SET || | |
216 | offset < 0 || offset > file_size) { | |
217 | BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, " | |
218 | "file-size=%jd", (int) whence, offset, | |
219 | file_size); | |
220 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL; | |
221 | goto end; | |
222 | } | |
223 | ||
224 | /* | |
225 | * Determine whether or not the destination is contained within the | |
226 | * current mapping. | |
227 | */ | |
228 | if (ds_file->mmap_addr && (offset < ds_file->mmap_offset || | |
229 | offset >= ds_file->mmap_offset + ds_file->mmap_len)) { | |
230 | int unmap_ret; | |
231 | off_t offset_in_mapping = offset % bt_common_get_page_size(); | |
232 | ||
233 | BT_LOGD("Medium seek request cannot be accomodated by the current " | |
63489ca2 | 234 | "file mapping: offset=%jd, mmap-offset=%jd, " |
9e0c8dbb JG |
235 | "mmap-len=%zu", offset, ds_file->mmap_offset, |
236 | ds_file->mmap_len); | |
237 | unmap_ret = ds_file_munmap(ds_file); | |
238 | if (unmap_ret) { | |
239 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; | |
240 | goto end; | |
241 | } | |
242 | ||
243 | ds_file->mmap_offset = offset - offset_in_mapping; | |
244 | ds_file->request_offset = offset_in_mapping; | |
245 | ret = ds_file_mmap_next(ds_file); | |
246 | if (ret != BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK) { | |
247 | goto end; | |
248 | } | |
249 | } else { | |
250 | ds_file->request_offset = offset - ds_file->mmap_offset; | |
251 | } | |
252 | ||
253 | ds_file->end_reached = (offset == file_size); | |
254 | end: | |
255 | return ret; | |
256 | } | |
257 | ||
6de92955 PP |
258 | BT_HIDDEN |
259 | struct bt_ctf_notif_iter_medium_ops ctf_fs_ds_file_medops = { | |
e98a2d6e PP |
260 | .request_bytes = medop_request_bytes, |
261 | .get_stream = medop_get_stream, | |
9e0c8dbb | 262 | .seek = medop_seek, |
e98a2d6e | 263 | }; |
6de92955 | 264 | |
97ade20b JG |
265 | static |
266 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length) | |
267 | { | |
268 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); | |
269 | ||
270 | if (!index) { | |
271 | BT_LOGE_STR("Failed to allocate index"); | |
272 | goto error; | |
273 | } | |
274 | ||
275 | index->entries = g_array_sized_new(FALSE, TRUE, | |
276 | sizeof(struct ctf_fs_ds_index_entry), length); | |
277 | if (!index->entries) { | |
278 | BT_LOGE("Failed to allocate %zu index entries.", length); | |
279 | goto error; | |
280 | } | |
281 | g_array_set_size(index->entries, length); | |
282 | end: | |
283 | return index; | |
284 | error: | |
285 | ctf_fs_ds_index_destroy(index); | |
286 | goto end; | |
287 | } | |
e98a2d6e | 288 | |
9e0c8dbb JG |
289 | /* Returns a new, zeroed, index entry. */ |
290 | static | |
291 | struct ctf_fs_ds_index_entry *ctf_fs_ds_index_add_new_entry( | |
292 | struct ctf_fs_ds_index *index) | |
293 | { | |
294 | g_array_set_size(index->entries, index->entries->len + 1); | |
295 | return &g_array_index(index->entries, struct ctf_fs_ds_index_entry, | |
296 | index->entries->len - 1); | |
297 | } | |
298 | ||
b6c3dcb2 | 299 | static |
97ade20b JG |
300 | struct bt_ctf_clock_class *get_field_mapped_clock_class( |
301 | struct bt_ctf_field *field) | |
302 | { | |
303 | struct bt_ctf_field_type *field_type; | |
304 | struct bt_ctf_clock_class *clock_class = NULL; | |
305 | ||
306 | field_type = bt_ctf_field_get_type(field); | |
307 | if (!field_type) { | |
308 | goto end; | |
309 | } | |
310 | ||
311 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class( | |
312 | field_type); | |
313 | if (!clock_class) { | |
314 | goto end; | |
315 | } | |
316 | end: | |
317 | bt_put(field_type); | |
318 | return clock_class; | |
319 | } | |
320 | ||
321 | static | |
9e0c8dbb JG |
322 | int get_packet_bounds_from_packet_context( |
323 | struct bt_ctf_field *packet_context, | |
97ade20b | 324 | struct bt_ctf_clock_class **_timestamp_begin_cc, |
9e0c8dbb JG |
325 | struct bt_ctf_field **_timestamp_begin, |
326 | struct bt_ctf_clock_class **_timestamp_end_cc, | |
327 | struct bt_ctf_field **_timestamp_end) | |
97ade20b | 328 | { |
9e0c8dbb | 329 | int ret = 0; |
97ade20b JG |
330 | struct bt_ctf_clock_class *timestamp_begin_cc = NULL; |
331 | struct bt_ctf_clock_class *timestamp_end_cc = NULL; | |
9e0c8dbb JG |
332 | struct bt_ctf_field *timestamp_begin = NULL; |
333 | struct bt_ctf_field *timestamp_end = NULL; | |
97ade20b | 334 | |
9e0c8dbb JG |
335 | timestamp_begin = bt_ctf_field_structure_get_field_by_name( |
336 | packet_context, "timestamp_begin"); | |
337 | if (!timestamp_begin) { | |
338 | BT_LOGD_STR("Cannot retrieve timestamp_begin field in packet context."); | |
97ade20b JG |
339 | ret = -1; |
340 | goto end; | |
341 | } | |
342 | ||
9e0c8dbb | 343 | timestamp_begin_cc = get_field_mapped_clock_class(timestamp_begin); |
97ade20b | 344 | if (!timestamp_begin_cc) { |
9e0c8dbb | 345 | BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_begin."); |
97ade20b | 346 | } |
97ade20b | 347 | |
9e0c8dbb JG |
348 | timestamp_end = bt_ctf_field_structure_get_field_by_name( |
349 | packet_context, "timestamp_end"); | |
350 | if (!timestamp_end) { | |
351 | BT_LOGD_STR("Cannot retrieve timestamp_end field in packet context."); | |
97ade20b JG |
352 | ret = -1; |
353 | goto end; | |
354 | } | |
355 | ||
9e0c8dbb | 356 | timestamp_end_cc = get_field_mapped_clock_class(timestamp_end); |
97ade20b | 357 | if (!timestamp_end_cc) { |
9e0c8dbb | 358 | BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_end."); |
97ade20b JG |
359 | } |
360 | ||
361 | if (_timestamp_begin_cc) { | |
362 | *_timestamp_begin_cc = bt_get(timestamp_begin_cc); | |
363 | } | |
9e0c8dbb JG |
364 | if (_timestamp_begin) { |
365 | *_timestamp_begin = bt_get(timestamp_begin); | |
366 | } | |
97ade20b JG |
367 | if (_timestamp_end_cc) { |
368 | *_timestamp_end_cc = bt_get(timestamp_end_cc); | |
369 | } | |
9e0c8dbb JG |
370 | if (_timestamp_end) { |
371 | *_timestamp_end = bt_get(timestamp_end); | |
372 | } | |
97ade20b | 373 | end: |
97ade20b JG |
374 | bt_put(timestamp_begin_cc); |
375 | bt_put(timestamp_end_cc); | |
9e0c8dbb JG |
376 | bt_put(timestamp_begin); |
377 | bt_put(timestamp_end); | |
378 | return ret; | |
379 | } | |
380 | ||
381 | static | |
382 | int get_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file, | |
383 | struct bt_ctf_clock_class **timestamp_begin_cc, | |
384 | struct bt_ctf_clock_class **timestamp_end_cc) | |
385 | { | |
386 | struct bt_ctf_field *packet_context = NULL; | |
387 | int ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file, | |
388 | NULL, &packet_context); | |
389 | ||
390 | if (ret || !packet_context) { | |
391 | BT_LOGD("Cannot retrieve packet context field of stream \'%s\'", | |
392 | ds_file->file->path->str); | |
393 | ret = -1; | |
394 | goto end; | |
395 | } | |
396 | ||
397 | ret = get_packet_bounds_from_packet_context(packet_context, | |
398 | timestamp_begin_cc, NULL, | |
399 | timestamp_end_cc, NULL); | |
400 | end: | |
401 | bt_put(packet_context); | |
97ade20b JG |
402 | return ret; |
403 | } | |
404 | ||
405 | static | |
406 | int convert_cycles_to_ns(struct bt_ctf_clock_class *clock_class, | |
407 | uint64_t cycles, int64_t *ns) | |
b6c3dcb2 JG |
408 | { |
409 | int ret = 0; | |
97ade20b JG |
410 | struct bt_ctf_clock_value *clock_value; |
411 | ||
412 | assert(ns); | |
413 | clock_value = bt_ctf_clock_value_create(clock_class, cycles); | |
414 | if (!clock_value) { | |
415 | ret = -1; | |
416 | goto end; | |
417 | } | |
418 | ||
419 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns); | |
420 | if (ret) { | |
421 | goto end; | |
422 | } | |
423 | end: | |
424 | bt_put(clock_value); | |
425 | return ret; | |
426 | } | |
427 | ||
428 | static | |
429 | struct ctf_fs_ds_index *build_index_from_idx_file( | |
430 | struct ctf_fs_ds_file *ds_file) | |
431 | { | |
432 | int ret; | |
b6c3dcb2 JG |
433 | gchar *directory = NULL; |
434 | gchar *basename = NULL; | |
435 | GString *index_basename = NULL; | |
436 | gchar *index_file_path = NULL; | |
437 | GMappedFile *mapped_file = NULL; | |
438 | gsize filesize; | |
97ade20b JG |
439 | const char *mmap_begin = NULL, *file_pos = NULL; |
440 | const struct ctf_packet_index_file_hdr *header = NULL; | |
441 | struct ctf_fs_ds_index *index = NULL; | |
442 | struct ctf_fs_ds_index_entry *index_entry = NULL; | |
b6c3dcb2 JG |
443 | uint64_t total_packets_size = 0; |
444 | size_t file_index_entry_size; | |
445 | size_t file_entry_count; | |
446 | size_t i; | |
97ade20b JG |
447 | struct bt_ctf_clock_class *timestamp_begin_cc = NULL; |
448 | struct bt_ctf_clock_class *timestamp_end_cc = NULL; | |
449 | ||
450 | BT_LOGD("Building index from .idx file of stream file %s", | |
451 | ds_file->file->path->str); | |
452 | ||
453 | ret = get_ds_file_packet_bounds_clock_classes(ds_file, | |
454 | ×tamp_begin_cc, ×tamp_end_cc); | |
455 | if (ret) { | |
9e0c8dbb JG |
456 | BT_LOGD_STR("Cannot get clock classes of \"timestamp_begin\" " |
457 | "and \"timestamp_end\" fields"); | |
97ade20b JG |
458 | goto error; |
459 | } | |
b6c3dcb2 JG |
460 | |
461 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 462 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 463 | if (!basename) { |
97ade20b | 464 | BT_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 465 | ds_file->file->path->str); |
97ade20b | 466 | goto error; |
b6c3dcb2 JG |
467 | } |
468 | ||
94cf822e | 469 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 470 | if (!directory) { |
97ade20b | 471 | BT_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 472 | ds_file->file->path->str); |
97ade20b | 473 | goto error; |
b6c3dcb2 JG |
474 | } |
475 | ||
476 | index_basename = g_string_new(basename); | |
477 | if (!index_basename) { | |
9e0c8dbb | 478 | BT_LOGE_STR("Cannot allocate index file basename string"); |
97ade20b | 479 | goto error; |
b6c3dcb2 JG |
480 | } |
481 | ||
482 | g_string_append(index_basename, ".idx"); | |
483 | index_file_path = g_build_filename(directory, "index", | |
484 | index_basename->str, NULL); | |
485 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 486 | if (!mapped_file) { |
97ade20b | 487 | BT_LOGD("Cannot create new mapped file %s", |
55314f2a | 488 | index_file_path); |
97ade20b | 489 | goto error; |
06367fa3 | 490 | } |
d14940a7 JG |
491 | |
492 | /* | |
493 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
494 | * Traces with such large indexes have never been seen in the wild, | |
495 | * but this would need to be adjusted to support them. | |
496 | */ | |
b6c3dcb2 JG |
497 | filesize = g_mapped_file_get_length(mapped_file); |
498 | if (filesize < sizeof(*header)) { | |
d14940a7 JG |
499 | BT_LOGW("Invalid LTTng trace index file: " |
500 | "file size (%zu bytes) < header size (%zu bytes)", | |
501 | filesize, sizeof(*header)); | |
97ade20b | 502 | goto error; |
b6c3dcb2 JG |
503 | } |
504 | ||
505 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
506 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
507 | ||
508 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
509 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
9e0c8dbb | 510 | BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 511 | goto error; |
b6c3dcb2 | 512 | } |
b6c3dcb2 JG |
513 | |
514 | file_index_entry_size = be32toh(header->packet_index_len); | |
515 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
97ade20b | 516 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
d14940a7 JG |
517 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
518 | "(%zu bytes) is not a multiple of the index entry size " | |
519 | "(%zu bytes)", (filesize - sizeof(*header)), | |
520 | sizeof(*header)); | |
97ade20b | 521 | goto error; |
b6c3dcb2 JG |
522 | } |
523 | ||
97ade20b JG |
524 | index = ctf_fs_ds_index_create(file_entry_count); |
525 | if (!index) { | |
526 | goto error; | |
b6c3dcb2 | 527 | } |
97ade20b JG |
528 | |
529 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( | |
530 | index->entries, struct ctf_fs_ds_index_entry, 0); | |
b6c3dcb2 JG |
531 | for (i = 0; i < file_entry_count; i++) { |
532 | struct ctf_packet_index *file_index = | |
533 | (struct ctf_packet_index *) file_pos; | |
534 | uint64_t packet_size = be64toh(file_index->packet_size); | |
535 | ||
536 | if (packet_size % CHAR_BIT) { | |
d14940a7 | 537 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 538 | goto error; |
b6c3dcb2 JG |
539 | } |
540 | ||
541 | /* Convert size in bits to bytes. */ | |
542 | packet_size /= CHAR_BIT; | |
97ade20b | 543 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 544 | |
97ade20b JG |
545 | index_entry->offset = be64toh(file_index->offset); |
546 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { | |
d14940a7 JG |
547 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
548 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, | |
549 | (index_entry - 1)->offset, index_entry->offset); | |
97ade20b | 550 | goto error; |
b6c3dcb2 JG |
551 | } |
552 | ||
97ade20b JG |
553 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
554 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
555 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
d14940a7 JG |
556 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
557 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, | |
558 | index_entry->timestamp_begin, | |
559 | index_entry->timestamp_end); | |
97ade20b JG |
560 | goto error; |
561 | } | |
562 | ||
563 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
564 | ret = convert_cycles_to_ns(timestamp_begin_cc, | |
565 | index_entry->timestamp_begin, | |
566 | &index_entry->timestamp_begin_ns); | |
567 | if (ret) { | |
9e0c8dbb | 568 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
569 | goto error; |
570 | } | |
571 | ret = convert_cycles_to_ns(timestamp_end_cc, | |
572 | index_entry->timestamp_end, | |
573 | &index_entry->timestamp_end_ns); | |
574 | if (ret) { | |
9e0c8dbb | 575 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 576 | goto error; |
b6c3dcb2 JG |
577 | } |
578 | ||
579 | total_packets_size += packet_size; | |
580 | file_pos += file_index_entry_size; | |
97ade20b | 581 | index_entry++; |
b6c3dcb2 JG |
582 | } |
583 | ||
584 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 585 | if (ds_file->file->size != total_packets_size) { |
d14940a7 | 586 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
9e0c8dbb | 587 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
d14940a7 | 588 | ds_file->file->size, total_packets_size); |
97ade20b | 589 | goto error; |
b6c3dcb2 JG |
590 | } |
591 | end: | |
592 | g_free(directory); | |
593 | g_free(basename); | |
594 | g_free(index_file_path); | |
06367fa3 JG |
595 | if (index_basename) { |
596 | g_string_free(index_basename, TRUE); | |
597 | } | |
598 | if (mapped_file) { | |
599 | g_mapped_file_unref(mapped_file); | |
600 | } | |
97ade20b JG |
601 | bt_put(timestamp_begin_cc); |
602 | bt_put(timestamp_end_cc); | |
603 | return index; | |
604 | error: | |
605 | ctf_fs_ds_index_destroy(index); | |
606 | index = NULL; | |
b6c3dcb2 JG |
607 | goto end; |
608 | } | |
609 | ||
9e0c8dbb JG |
610 | static |
611 | int init_index_entry(struct ctf_fs_ds_index_entry *entry, | |
612 | struct bt_ctf_field *packet_context, off_t packet_size, | |
613 | off_t packet_offset) | |
614 | { | |
615 | int ret; | |
616 | struct bt_ctf_field *timestamp_begin = NULL; | |
617 | struct bt_ctf_field *timestamp_end = NULL; | |
618 | struct bt_ctf_clock_class *timestamp_begin_cc = NULL; | |
619 | struct bt_ctf_clock_class *timestamp_end_cc = NULL; | |
620 | ||
621 | ret = get_packet_bounds_from_packet_context(packet_context, | |
622 | ×tamp_begin_cc, ×tamp_begin, | |
623 | ×tamp_end_cc, ×tamp_end); | |
624 | if (ret || !timestamp_begin_cc || !timestamp_begin || | |
625 | !timestamp_end_cc || ! timestamp_end) { | |
626 | BT_LOGD_STR("Failed to determine time bound fields of packet."); | |
627 | goto end; | |
628 | } | |
629 | ||
630 | assert(packet_offset >= 0); | |
631 | entry->offset = packet_offset; | |
632 | ||
633 | assert(packet_size >= 0); | |
634 | entry->packet_size = packet_size; | |
635 | ||
636 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_begin, | |
637 | &entry->timestamp_begin); | |
638 | if (ret) { | |
639 | goto end; | |
640 | } | |
641 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_end, | |
642 | &entry->timestamp_end); | |
643 | if (ret) { | |
644 | goto end; | |
645 | } | |
646 | ||
647 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
648 | ret = convert_cycles_to_ns(timestamp_begin_cc, | |
649 | entry->timestamp_begin, | |
650 | &entry->timestamp_begin_ns); | |
651 | if (ret) { | |
652 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
653 | goto end; | |
654 | } | |
655 | ||
656 | ret = convert_cycles_to_ns(timestamp_end_cc, | |
657 | entry->timestamp_end, | |
658 | &entry->timestamp_end_ns); | |
659 | if (ret) { | |
660 | BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); | |
661 | goto end; | |
662 | } | |
663 | end: | |
664 | bt_put(timestamp_begin); | |
665 | bt_put(timestamp_begin_cc); | |
666 | bt_put(timestamp_end); | |
667 | bt_put(timestamp_end_cc); | |
668 | return ret; | |
669 | } | |
670 | ||
671 | static | |
672 | struct ctf_fs_ds_index *build_index_from_stream_file( | |
673 | struct ctf_fs_ds_file *ds_file) | |
674 | { | |
675 | int ret; | |
676 | struct ctf_fs_ds_index *index = NULL; | |
677 | enum bt_ctf_notif_iter_status iter_status; | |
678 | struct bt_ctf_field *packet_context = NULL; | |
679 | ||
680 | BT_LOGD("Indexing stream file %s", ds_file->file->path->str); | |
681 | ||
682 | index = ctf_fs_ds_index_create(0); | |
683 | if (!index) { | |
684 | goto error; | |
685 | } | |
686 | ||
687 | do { | |
688 | off_t current_packet_offset; | |
689 | off_t next_packet_offset; | |
690 | off_t current_packet_size, current_packet_size_bytes; | |
691 | struct ctf_fs_ds_index_entry *entry; | |
692 | ||
693 | iter_status = bt_ctf_notif_iter_get_packet_header_context_fields( | |
694 | ds_file->notif_iter, NULL, &packet_context); | |
695 | if (iter_status != BT_CTF_NOTIF_ITER_STATUS_OK) { | |
696 | if (iter_status == BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
697 | break; | |
698 | } | |
699 | goto error; | |
700 | } | |
701 | current_packet_offset = | |
702 | bt_ctf_notif_iter_get_current_packet_offset( | |
703 | ds_file->notif_iter); | |
704 | if (current_packet_offset < 0) { | |
705 | BT_LOGE_STR("Cannot get the current packet's offset."); | |
706 | goto error; | |
707 | } | |
708 | ||
709 | current_packet_size = bt_ctf_notif_iter_get_current_packet_size( | |
710 | ds_file->notif_iter); | |
711 | if (current_packet_size < 0) { | |
712 | BT_LOGE("Cannot get packet size: packet-offset=%jd", | |
713 | current_packet_offset); | |
714 | goto error; | |
715 | } | |
716 | current_packet_size_bytes = | |
717 | ((current_packet_size + 7) & ~7) / CHAR_BIT; | |
718 | ||
719 | if (current_packet_offset + current_packet_size_bytes > | |
720 | ds_file->file->size) { | |
721 | BT_LOGW("Invalid packet size reported in file: stream=\"%s\", " | |
722 | "packet-offset=%jd, packet-size-bytes=%jd, " | |
723 | "file-size=%jd", | |
724 | ds_file->file->path->str, | |
725 | current_packet_offset, | |
726 | current_packet_size_bytes, | |
727 | ds_file->file->size); | |
728 | goto error; | |
729 | } | |
730 | ||
731 | next_packet_offset = current_packet_offset + | |
732 | current_packet_size_bytes; | |
733 | BT_LOGD("Seeking to next packet: current-packet-offset=%jd, " | |
734 | "next-packet-offset=%jd", current_packet_offset, | |
735 | next_packet_offset); | |
736 | ||
737 | entry = ctf_fs_ds_index_add_new_entry(index); | |
738 | if (!entry) { | |
739 | BT_LOGE_STR("Failed to allocate a new index entry."); | |
740 | goto error; | |
741 | } | |
742 | ||
743 | ret = init_index_entry(entry, packet_context, | |
744 | current_packet_size_bytes, | |
745 | current_packet_offset); | |
746 | if (ret) { | |
747 | goto error; | |
748 | } | |
749 | ||
750 | iter_status = bt_ctf_notif_iter_seek(ds_file->notif_iter, | |
751 | next_packet_offset); | |
752 | BT_PUT(packet_context); | |
753 | } while (iter_status == BT_CTF_NOTIF_ITER_STATUS_OK); | |
754 | ||
755 | if (iter_status != BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
756 | goto error; | |
757 | } | |
758 | end: | |
759 | bt_put(packet_context); | |
760 | return index; | |
761 | error: | |
762 | ctf_fs_ds_index_destroy(index); | |
763 | index = NULL; | |
764 | goto end; | |
765 | } | |
766 | ||
e7a4393b | 767 | BT_HIDDEN |
94cf822e PP |
768 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
769 | struct ctf_fs_trace *ctf_fs_trace, | |
6de92955 | 770 | struct bt_ctf_notif_iter *notif_iter, |
97ade20b | 771 | struct bt_ctf_stream *stream, const char *path) |
e98a2d6e | 772 | { |
b6c3dcb2 | 773 | int ret; |
55314f2a | 774 | const size_t page_size = bt_common_get_page_size(); |
94cf822e | 775 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 776 | |
94cf822e | 777 | if (!ds_file) { |
e98a2d6e PP |
778 | goto error; |
779 | } | |
780 | ||
55314f2a | 781 | ds_file->file = ctf_fs_file_create(); |
94cf822e | 782 | if (!ds_file->file) { |
4f1f88a6 PP |
783 | goto error; |
784 | } | |
785 | ||
94cf822e PP |
786 | ds_file->stream = bt_get(stream); |
787 | ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map); | |
788 | g_string_assign(ds_file->file->path, path); | |
55314f2a | 789 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
790 | if (ret) { |
791 | goto error; | |
792 | } | |
793 | ||
6de92955 PP |
794 | ds_file->notif_iter = notif_iter; |
795 | bt_ctf_notif_iter_set_medops_data(ds_file->notif_iter, ds_file); | |
94cf822e | 796 | if (!ds_file->notif_iter) { |
e98a2d6e PP |
797 | goto error; |
798 | } | |
5b29e799 | 799 | |
55314f2a | 800 | ds_file->mmap_max_len = page_size * 2048; |
94cf822e | 801 | |
e98a2d6e | 802 | goto end; |
1a9f7075 | 803 | |
e98a2d6e | 804 | error: |
78586d8a | 805 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
806 | ctf_fs_ds_file_destroy(ds_file); |
807 | ds_file = NULL; | |
1a9f7075 | 808 | |
e98a2d6e | 809 | end: |
94cf822e | 810 | return ds_file; |
e98a2d6e PP |
811 | } |
812 | ||
97ade20b JG |
813 | BT_HIDDEN |
814 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
815 | struct ctf_fs_ds_file *ds_file) | |
816 | { | |
9e0c8dbb JG |
817 | struct ctf_fs_ds_index *index; |
818 | ||
819 | index = build_index_from_idx_file(ds_file); | |
820 | if (index) { | |
821 | goto end; | |
822 | } | |
823 | ||
824 | BT_LOGD("Failed to build index from .index file; " | |
825 | "falling back to stream indexing."); | |
826 | index = build_index_from_stream_file(ds_file); | |
827 | end: | |
828 | return index; | |
97ade20b JG |
829 | } |
830 | ||
5b29e799 | 831 | BT_HIDDEN |
94cf822e | 832 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 833 | { |
94cf822e | 834 | if (!ds_file) { |
5b29e799 | 835 | return; |
043e2020 JG |
836 | } |
837 | ||
94cf822e PP |
838 | bt_put(ds_file->cc_prio_map); |
839 | bt_put(ds_file->stream); | |
840 | (void) ds_file_munmap(ds_file); | |
0982a26d | 841 | |
94cf822e PP |
842 | if (ds_file->file) { |
843 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
844 | } |
845 | ||
94cf822e | 846 | g_free(ds_file); |
e98a2d6e | 847 | } |
4f1f88a6 PP |
848 | |
849 | BT_HIDDEN | |
90157d89 | 850 | struct bt_notification_iterator_next_method_return ctf_fs_ds_file_next( |
94cf822e | 851 | struct ctf_fs_ds_file *ds_file) |
4f1f88a6 PP |
852 | { |
853 | enum bt_ctf_notif_iter_status notif_iter_status; | |
90157d89 | 854 | struct bt_notification_iterator_next_method_return ret = { |
4f1f88a6 PP |
855 | .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR, |
856 | .notification = NULL, | |
857 | }; | |
858 | ||
8915b7a7 | 859 | notif_iter_status = bt_ctf_notif_iter_get_next_notification( |
94cf822e | 860 | ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification); |
4f1f88a6 | 861 | |
4f1f88a6 PP |
862 | switch (notif_iter_status) { |
863 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
864 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
865 | break; | |
866 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
867 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
868 | break; | |
869 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
870 | /* | |
871 | * Should not make it this far as this is | |
872 | * medium-specific; there is nothing for the user to do | |
873 | * and it should have been handled upstream. | |
874 | */ | |
0fbb9a9f | 875 | abort(); |
4f1f88a6 PP |
876 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
877 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
878 | default: | |
879 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
880 | break; | |
881 | } | |
882 | ||
883 | return ret; | |
884 | } | |
94cf822e PP |
885 | |
886 | BT_HIDDEN | |
887 | int ctf_fs_ds_file_get_packet_header_context_fields( | |
97ade20b | 888 | struct ctf_fs_ds_file *ds_file, |
94cf822e PP |
889 | struct bt_ctf_field **packet_header_field, |
890 | struct bt_ctf_field **packet_context_field) | |
891 | { | |
892 | enum bt_ctf_notif_iter_status notif_iter_status; | |
94cf822e PP |
893 | int ret = 0; |
894 | ||
97ade20b | 895 | assert(ds_file); |
94cf822e PP |
896 | notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields( |
897 | ds_file->notif_iter, packet_header_field, packet_context_field); | |
898 | switch (notif_iter_status) { | |
899 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
900 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
901 | break; | |
902 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
0fbb9a9f | 903 | abort(); |
94cf822e PP |
904 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
905 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
906 | default: | |
907 | goto error; | |
908 | break; | |
909 | } | |
910 | ||
911 | goto end; | |
912 | ||
913 | error: | |
914 | ret = -1; | |
915 | ||
916 | if (packet_header_field) { | |
917 | bt_put(*packet_header_field); | |
918 | } | |
919 | ||
920 | if (packet_context_field) { | |
921 | bt_put(*packet_context_field); | |
922 | } | |
923 | ||
924 | end: | |
94cf822e PP |
925 | return ret; |
926 | } | |
97ade20b JG |
927 | |
928 | BT_HIDDEN | |
929 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
930 | { | |
931 | if (!index) { | |
932 | return; | |
933 | } | |
934 | ||
935 | if (index->entries) { | |
936 | g_array_free(index->entries, TRUE); | |
937 | } | |
938 | g_free(index); | |
939 | } |