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 | { |
94cf822e | 53 | return ds_file->mmap_valid_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 | ||
a555e971 | 65 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { |
55314f2a | 66 | BT_LOGE("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s", |
94cf822e PP |
67 | ds_file->mmap_addr, ds_file->mmap_len, |
68 | ds_file->file->path->str, ds_file->file->fp, | |
e98a2d6e | 69 | strerror(errno)); |
fc9a526c JG |
70 | ret = -1; |
71 | goto end; | |
e98a2d6e | 72 | } |
94cf822e PP |
73 | |
74 | ds_file->mmap_addr = NULL; | |
75 | ||
fc9a526c JG |
76 | end: |
77 | return ret; | |
e98a2d6e PP |
78 | } |
79 | ||
5b29e799 | 80 | static |
94cf822e PP |
81 | enum bt_ctf_notif_iter_medium_status ds_file_mmap_next( |
82 | struct ctf_fs_ds_file *ds_file) | |
e98a2d6e | 83 | { |
55314f2a | 84 | const size_t page_size = bt_common_get_page_size(); |
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 | ||
94cf822e PP |
94 | ds_file->mmap_offset += ds_file->mmap_valid_len; |
95 | ds_file->request_offset = 0; | |
e98a2d6e PP |
96 | } |
97 | ||
94cf822e PP |
98 | ds_file->mmap_valid_len = MIN(ds_file->file->size - ds_file->mmap_offset, |
99 | ds_file->mmap_max_len); | |
100 | if (ds_file->mmap_valid_len == 0) { | |
e0f6a64a JG |
101 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; |
102 | goto end; | |
103 | } | |
fc9a526c | 104 | /* Round up to next page, assuming page size being a power of 2. */ |
55314f2a JG |
105 | ds_file->mmap_len = (ds_file->mmap_valid_len + page_size - 1) |
106 | & ~(page_size - 1); | |
e98a2d6e | 107 | /* Map new region */ |
94cf822e | 108 | assert(ds_file->mmap_len); |
a555e971 | 109 | ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len, |
94cf822e PP |
110 | PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp), |
111 | ds_file->mmap_offset); | |
112 | if (ds_file->mmap_addr == MAP_FAILED) { | |
55314f2a | 113 | BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s", |
94cf822e PP |
114 | ds_file->mmap_len, ds_file->file->path->str, |
115 | ds_file->file->fp, ds_file->mmap_offset, | |
78586d8a | 116 | strerror(errno)); |
e98a2d6e PP |
117 | goto error; |
118 | } | |
119 | ||
120 | goto end; | |
e98a2d6e | 121 | error: |
94cf822e | 122 | ds_file_munmap(ds_file); |
e0f6a64a | 123 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
124 | end: |
125 | return ret; | |
126 | } | |
127 | ||
5b29e799 JG |
128 | static |
129 | enum bt_ctf_notif_iter_medium_status medop_request_bytes( | |
e98a2d6e PP |
130 | size_t request_sz, uint8_t **buffer_addr, |
131 | size_t *buffer_sz, void *data) | |
132 | { | |
133 | enum bt_ctf_notif_iter_medium_status status = | |
134 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
94cf822e | 135 | struct ctf_fs_ds_file *ds_file = data; |
e98a2d6e PP |
136 | |
137 | if (request_sz == 0) { | |
138 | goto end; | |
139 | } | |
140 | ||
e98a2d6e | 141 | /* Check if we have at least one memory-mapped byte left */ |
94cf822e | 142 | if (remaining_mmap_bytes(ds_file) == 0) { |
e98a2d6e | 143 | /* Are we at the end of the file? */ |
94cf822e | 144 | if (ds_file->mmap_offset >= ds_file->file->size) { |
55314f2a | 145 | BT_LOGD("Reached end of file \"%s\" (%p)", |
94cf822e | 146 | ds_file->file->path->str, ds_file->file->fp); |
e98a2d6e PP |
147 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; |
148 | goto end; | |
149 | } | |
150 | ||
94cf822e | 151 | status = ds_file_mmap_next(ds_file); |
e0f6a64a JG |
152 | switch (status) { |
153 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK: | |
154 | break; | |
155 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF: | |
156 | goto end; | |
157 | default: | |
55314f2a | 158 | BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)", |
94cf822e PP |
159 | ds_file->file->path->str, |
160 | ds_file->file->fp); | |
e98a2d6e PP |
161 | goto error; |
162 | } | |
163 | } | |
164 | ||
94cf822e PP |
165 | *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz); |
166 | *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset; | |
167 | ds_file->request_offset += *buffer_sz; | |
e98a2d6e PP |
168 | goto end; |
169 | ||
170 | error: | |
171 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; | |
172 | ||
173 | end: | |
174 | return status; | |
175 | } | |
176 | ||
5b29e799 JG |
177 | static |
178 | struct bt_ctf_stream *medop_get_stream( | |
e98a2d6e PP |
179 | struct bt_ctf_stream_class *stream_class, void *data) |
180 | { | |
94cf822e PP |
181 | struct ctf_fs_ds_file *ds_file = data; |
182 | struct bt_ctf_stream_class *ds_file_stream_class; | |
183 | struct bt_ctf_stream *stream = NULL; | |
184 | ||
185 | ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream); | |
186 | bt_put(ds_file_stream_class); | |
187 | ||
188 | if (stream_class != ds_file_stream_class) { | |
189 | /* | |
190 | * Not supported: two packets described by two different | |
191 | * stream classes within the same data stream file. | |
192 | */ | |
193 | goto end; | |
e98a2d6e PP |
194 | } |
195 | ||
94cf822e PP |
196 | stream = ds_file->stream; |
197 | ||
198 | end: | |
199 | return stream; | |
e98a2d6e PP |
200 | } |
201 | ||
202 | static struct bt_ctf_notif_iter_medium_ops medops = { | |
203 | .request_bytes = medop_request_bytes, | |
204 | .get_stream = medop_get_stream, | |
205 | }; | |
97ade20b JG |
206 | static |
207 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length) | |
208 | { | |
209 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); | |
210 | ||
211 | if (!index) { | |
212 | BT_LOGE_STR("Failed to allocate index"); | |
213 | goto error; | |
214 | } | |
215 | ||
216 | index->entries = g_array_sized_new(FALSE, TRUE, | |
217 | sizeof(struct ctf_fs_ds_index_entry), length); | |
218 | if (!index->entries) { | |
219 | BT_LOGE("Failed to allocate %zu index entries.", length); | |
220 | goto error; | |
221 | } | |
222 | g_array_set_size(index->entries, length); | |
223 | end: | |
224 | return index; | |
225 | error: | |
226 | ctf_fs_ds_index_destroy(index); | |
227 | goto end; | |
228 | } | |
e98a2d6e | 229 | |
b6c3dcb2 | 230 | static |
97ade20b JG |
231 | struct bt_ctf_clock_class *get_field_mapped_clock_class( |
232 | struct bt_ctf_field *field) | |
233 | { | |
234 | struct bt_ctf_field_type *field_type; | |
235 | struct bt_ctf_clock_class *clock_class = NULL; | |
236 | ||
237 | field_type = bt_ctf_field_get_type(field); | |
238 | if (!field_type) { | |
239 | goto end; | |
240 | } | |
241 | ||
242 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class( | |
243 | field_type); | |
244 | if (!clock_class) { | |
245 | goto end; | |
246 | } | |
247 | end: | |
248 | bt_put(field_type); | |
249 | return clock_class; | |
250 | } | |
251 | ||
252 | static | |
253 | int get_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file, | |
254 | struct bt_ctf_clock_class **_timestamp_begin_cc, | |
255 | struct bt_ctf_clock_class **_timestamp_end_cc) | |
256 | { | |
257 | int ret; | |
89b08333 | 258 | struct bt_ctf_field *timestamp_field = NULL; |
97ade20b JG |
259 | struct bt_ctf_field *packet_context_field = NULL; |
260 | struct bt_ctf_clock_class *timestamp_begin_cc = NULL; | |
261 | struct bt_ctf_clock_class *timestamp_end_cc = NULL; | |
262 | ||
263 | ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file, | |
264 | NULL, &packet_context_field); | |
265 | if (ret || !packet_context_field) { | |
266 | BT_LOGD("Cannot retrieve packet context field of stream \'%s\'", | |
267 | ds_file->file->path->str); | |
268 | ret = -1; | |
269 | goto end; | |
270 | } | |
271 | ||
272 | timestamp_field = bt_ctf_field_structure_get_field_by_name( | |
273 | packet_context_field, "timestamp_begin"); | |
274 | if (!timestamp_field) { | |
275 | BT_LOGD("Cannot retrieve timestamp_begin field in packet context of stream \'%s\'", | |
276 | ds_file->file->path->str); | |
277 | ret = -1; | |
278 | goto end; | |
279 | } | |
280 | ||
281 | timestamp_begin_cc = get_field_mapped_clock_class(timestamp_field); | |
282 | if (!timestamp_begin_cc) { | |
283 | BT_LOGD("Cannot retrieve the clock mapped to timestamp_begin of stream \'%s\'", | |
284 | ds_file->file->path->str); | |
285 | } | |
89b08333 | 286 | BT_PUT(timestamp_field); |
97ade20b JG |
287 | |
288 | timestamp_field = bt_ctf_field_structure_get_field_by_name( | |
289 | packet_context_field, "timestamp_end"); | |
290 | if (!timestamp_field) { | |
291 | BT_LOGD("Cannot retrieve timestamp_end field in packet context of stream \'%s\'", | |
292 | ds_file->file->path->str); | |
293 | ret = -1; | |
294 | goto end; | |
295 | } | |
296 | ||
297 | timestamp_end_cc = get_field_mapped_clock_class(timestamp_field); | |
298 | if (!timestamp_end_cc) { | |
299 | BT_LOGD("Cannot retrieve the clock mapped to timestamp_end in stream \'%s\'", | |
300 | ds_file->file->path->str); | |
301 | } | |
302 | ||
303 | if (_timestamp_begin_cc) { | |
304 | *_timestamp_begin_cc = bt_get(timestamp_begin_cc); | |
305 | } | |
306 | if (_timestamp_end_cc) { | |
307 | *_timestamp_end_cc = bt_get(timestamp_end_cc); | |
308 | } | |
309 | end: | |
310 | bt_put(packet_context_field); | |
311 | bt_put(timestamp_field); | |
312 | bt_put(timestamp_begin_cc); | |
313 | bt_put(timestamp_end_cc); | |
314 | return ret; | |
315 | } | |
316 | ||
317 | static | |
318 | int convert_cycles_to_ns(struct bt_ctf_clock_class *clock_class, | |
319 | uint64_t cycles, int64_t *ns) | |
b6c3dcb2 JG |
320 | { |
321 | int ret = 0; | |
97ade20b JG |
322 | struct bt_ctf_clock_value *clock_value; |
323 | ||
324 | assert(ns); | |
325 | clock_value = bt_ctf_clock_value_create(clock_class, cycles); | |
326 | if (!clock_value) { | |
327 | ret = -1; | |
328 | goto end; | |
329 | } | |
330 | ||
331 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns); | |
332 | if (ret) { | |
333 | goto end; | |
334 | } | |
335 | end: | |
336 | bt_put(clock_value); | |
337 | return ret; | |
338 | } | |
339 | ||
340 | static | |
341 | struct ctf_fs_ds_index *build_index_from_idx_file( | |
342 | struct ctf_fs_ds_file *ds_file) | |
343 | { | |
344 | int ret; | |
b6c3dcb2 JG |
345 | gchar *directory = NULL; |
346 | gchar *basename = NULL; | |
347 | GString *index_basename = NULL; | |
348 | gchar *index_file_path = NULL; | |
349 | GMappedFile *mapped_file = NULL; | |
350 | gsize filesize; | |
97ade20b JG |
351 | const char *mmap_begin = NULL, *file_pos = NULL; |
352 | const struct ctf_packet_index_file_hdr *header = NULL; | |
353 | struct ctf_fs_ds_index *index = NULL; | |
354 | struct ctf_fs_ds_index_entry *index_entry = NULL; | |
b6c3dcb2 JG |
355 | uint64_t total_packets_size = 0; |
356 | size_t file_index_entry_size; | |
357 | size_t file_entry_count; | |
358 | size_t i; | |
97ade20b JG |
359 | struct bt_ctf_clock_class *timestamp_begin_cc = NULL; |
360 | struct bt_ctf_clock_class *timestamp_end_cc = NULL; | |
361 | ||
362 | BT_LOGD("Building index from .idx file of stream file %s", | |
363 | ds_file->file->path->str); | |
364 | ||
365 | ret = get_ds_file_packet_bounds_clock_classes(ds_file, | |
366 | ×tamp_begin_cc, ×tamp_end_cc); | |
367 | if (ret) { | |
368 | BT_LOGD("Cannot get clock classes of \"timestamp_begin\" and \"timestamp_end\" fields"); | |
369 | goto error; | |
370 | } | |
b6c3dcb2 JG |
371 | |
372 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 373 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 374 | if (!basename) { |
97ade20b | 375 | BT_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 376 | ds_file->file->path->str); |
97ade20b | 377 | goto error; |
b6c3dcb2 JG |
378 | } |
379 | ||
94cf822e | 380 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 381 | if (!directory) { |
97ade20b | 382 | BT_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 383 | ds_file->file->path->str); |
97ade20b | 384 | goto error; |
b6c3dcb2 JG |
385 | } |
386 | ||
387 | index_basename = g_string_new(basename); | |
388 | if (!index_basename) { | |
97ade20b JG |
389 | BT_LOGE("Cannot allocate index file basename string"); |
390 | goto error; | |
b6c3dcb2 JG |
391 | } |
392 | ||
393 | g_string_append(index_basename, ".idx"); | |
394 | index_file_path = g_build_filename(directory, "index", | |
395 | index_basename->str, NULL); | |
396 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 397 | if (!mapped_file) { |
97ade20b | 398 | BT_LOGD("Cannot create new mapped file %s", |
55314f2a | 399 | index_file_path); |
97ade20b | 400 | goto error; |
06367fa3 | 401 | } |
65cbebef JG |
402 | |
403 | /* | |
404 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
405 | * Traces with such large indexes have never been seen in the wild, | |
406 | * but this would need to be adjusted to support them. | |
407 | */ | |
b6c3dcb2 JG |
408 | filesize = g_mapped_file_get_length(mapped_file); |
409 | if (filesize < sizeof(*header)) { | |
65cbebef JG |
410 | BT_LOGW("Invalid LTTng trace index file: " |
411 | "file size (%zu bytes) < header size (%zu bytes)", | |
412 | filesize, sizeof(*header)); | |
97ade20b | 413 | goto error; |
b6c3dcb2 JG |
414 | } |
415 | ||
416 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
417 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
418 | ||
419 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
420 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
65cbebef | 421 | BT_LOGW("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 422 | goto error; |
b6c3dcb2 | 423 | } |
b6c3dcb2 JG |
424 | |
425 | file_index_entry_size = be32toh(header->packet_index_len); | |
426 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; | |
97ade20b | 427 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
65cbebef JG |
428 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
429 | "(%zu bytes) is not a multiple of the index entry size " | |
430 | "(%zu bytes)", (filesize - sizeof(*header)), | |
431 | sizeof(*header)); | |
97ade20b | 432 | goto error; |
b6c3dcb2 JG |
433 | } |
434 | ||
97ade20b JG |
435 | index = ctf_fs_ds_index_create(file_entry_count); |
436 | if (!index) { | |
437 | goto error; | |
b6c3dcb2 | 438 | } |
97ade20b JG |
439 | |
440 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( | |
441 | index->entries, struct ctf_fs_ds_index_entry, 0); | |
b6c3dcb2 JG |
442 | for (i = 0; i < file_entry_count; i++) { |
443 | struct ctf_packet_index *file_index = | |
444 | (struct ctf_packet_index *) file_pos; | |
445 | uint64_t packet_size = be64toh(file_index->packet_size); | |
446 | ||
447 | if (packet_size % CHAR_BIT) { | |
65cbebef | 448 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 449 | goto error; |
b6c3dcb2 JG |
450 | } |
451 | ||
452 | /* Convert size in bits to bytes. */ | |
453 | packet_size /= CHAR_BIT; | |
97ade20b | 454 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 455 | |
97ade20b JG |
456 | index_entry->offset = be64toh(file_index->offset); |
457 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { | |
65cbebef JG |
458 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
459 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, | |
460 | (index_entry - 1)->offset, index_entry->offset); | |
97ade20b | 461 | goto error; |
b6c3dcb2 JG |
462 | } |
463 | ||
97ade20b JG |
464 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
465 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
466 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
65cbebef JG |
467 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
468 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, | |
469 | index_entry->timestamp_begin, | |
470 | index_entry->timestamp_end); | |
97ade20b JG |
471 | goto error; |
472 | } | |
473 | ||
474 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
475 | ret = convert_cycles_to_ns(timestamp_begin_cc, | |
476 | index_entry->timestamp_begin, | |
477 | &index_entry->timestamp_begin_ns); | |
478 | if (ret) { | |
65cbebef | 479 | BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
480 | goto error; |
481 | } | |
482 | ret = convert_cycles_to_ns(timestamp_end_cc, | |
483 | index_entry->timestamp_end, | |
484 | &index_entry->timestamp_end_ns); | |
485 | if (ret) { | |
65cbebef | 486 | BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 487 | goto error; |
b6c3dcb2 JG |
488 | } |
489 | ||
490 | total_packets_size += packet_size; | |
491 | file_pos += file_index_entry_size; | |
97ade20b | 492 | index_entry++; |
b6c3dcb2 JG |
493 | } |
494 | ||
495 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 496 | if (ds_file->file->size != total_packets_size) { |
65cbebef JG |
497 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
498 | "file_size=%" PRIu64 ", total_packets_size=%" PRIu64, | |
499 | ds_file->file->size, total_packets_size); | |
97ade20b | 500 | goto error; |
b6c3dcb2 JG |
501 | } |
502 | end: | |
503 | g_free(directory); | |
504 | g_free(basename); | |
505 | g_free(index_file_path); | |
06367fa3 JG |
506 | if (index_basename) { |
507 | g_string_free(index_basename, TRUE); | |
508 | } | |
509 | if (mapped_file) { | |
510 | g_mapped_file_unref(mapped_file); | |
511 | } | |
97ade20b JG |
512 | bt_put(timestamp_begin_cc); |
513 | bt_put(timestamp_end_cc); | |
514 | return index; | |
515 | error: | |
516 | ctf_fs_ds_index_destroy(index); | |
517 | index = NULL; | |
b6c3dcb2 JG |
518 | goto end; |
519 | } | |
520 | ||
e7a4393b | 521 | BT_HIDDEN |
94cf822e PP |
522 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
523 | struct ctf_fs_trace *ctf_fs_trace, | |
97ade20b | 524 | struct bt_ctf_stream *stream, const char *path) |
e98a2d6e | 525 | { |
b6c3dcb2 | 526 | int ret; |
55314f2a | 527 | const size_t page_size = bt_common_get_page_size(); |
94cf822e | 528 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 529 | |
94cf822e | 530 | if (!ds_file) { |
e98a2d6e PP |
531 | goto error; |
532 | } | |
533 | ||
55314f2a | 534 | ds_file->file = ctf_fs_file_create(); |
94cf822e | 535 | if (!ds_file->file) { |
4f1f88a6 PP |
536 | goto error; |
537 | } | |
538 | ||
94cf822e PP |
539 | ds_file->stream = bt_get(stream); |
540 | ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map); | |
541 | g_string_assign(ds_file->file->path, path); | |
55314f2a | 542 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
543 | if (ret) { |
544 | goto error; | |
545 | } | |
546 | ||
94cf822e | 547 | ds_file->notif_iter = bt_ctf_notif_iter_create( |
55314f2a | 548 | ctf_fs_trace->metadata->trace, page_size, medops, ds_file); |
94cf822e | 549 | if (!ds_file->notif_iter) { |
e98a2d6e PP |
550 | goto error; |
551 | } | |
5b29e799 | 552 | |
55314f2a | 553 | ds_file->mmap_max_len = page_size * 2048; |
94cf822e | 554 | |
e98a2d6e | 555 | goto end; |
1a9f7075 | 556 | |
e98a2d6e | 557 | error: |
78586d8a | 558 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
559 | ctf_fs_ds_file_destroy(ds_file); |
560 | ds_file = NULL; | |
1a9f7075 | 561 | |
e98a2d6e | 562 | end: |
94cf822e | 563 | return ds_file; |
e98a2d6e PP |
564 | } |
565 | ||
97ade20b JG |
566 | BT_HIDDEN |
567 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
568 | struct ctf_fs_ds_file *ds_file) | |
569 | { | |
570 | return build_index_from_idx_file(ds_file); | |
571 | } | |
572 | ||
5b29e799 | 573 | BT_HIDDEN |
94cf822e | 574 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 575 | { |
94cf822e | 576 | if (!ds_file) { |
5b29e799 | 577 | return; |
043e2020 JG |
578 | } |
579 | ||
94cf822e PP |
580 | bt_put(ds_file->cc_prio_map); |
581 | bt_put(ds_file->stream); | |
582 | (void) ds_file_munmap(ds_file); | |
0982a26d | 583 | |
94cf822e PP |
584 | if (ds_file->file) { |
585 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
586 | } |
587 | ||
94cf822e PP |
588 | if (ds_file->notif_iter) { |
589 | bt_ctf_notif_iter_destroy(ds_file->notif_iter); | |
043e2020 | 590 | } |
5b29e799 | 591 | |
94cf822e | 592 | g_free(ds_file); |
e98a2d6e | 593 | } |
4f1f88a6 PP |
594 | |
595 | BT_HIDDEN | |
94cf822e PP |
596 | struct bt_notification_iterator_next_return ctf_fs_ds_file_next( |
597 | struct ctf_fs_ds_file *ds_file) | |
4f1f88a6 PP |
598 | { |
599 | enum bt_ctf_notif_iter_status notif_iter_status; | |
600 | struct bt_notification_iterator_next_return ret = { | |
601 | .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR, | |
602 | .notification = NULL, | |
603 | }; | |
604 | ||
8915b7a7 | 605 | notif_iter_status = bt_ctf_notif_iter_get_next_notification( |
94cf822e | 606 | ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification); |
4f1f88a6 | 607 | |
4f1f88a6 PP |
608 | switch (notif_iter_status) { |
609 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
610 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
611 | break; | |
612 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
613 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
614 | break; | |
615 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
616 | /* | |
617 | * Should not make it this far as this is | |
618 | * medium-specific; there is nothing for the user to do | |
619 | * and it should have been handled upstream. | |
620 | */ | |
0fbb9a9f | 621 | abort(); |
4f1f88a6 PP |
622 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
623 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
624 | default: | |
625 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
626 | break; | |
627 | } | |
628 | ||
629 | return ret; | |
630 | } | |
94cf822e PP |
631 | |
632 | BT_HIDDEN | |
633 | int ctf_fs_ds_file_get_packet_header_context_fields( | |
97ade20b | 634 | struct ctf_fs_ds_file *ds_file, |
94cf822e PP |
635 | struct bt_ctf_field **packet_header_field, |
636 | struct bt_ctf_field **packet_context_field) | |
637 | { | |
638 | enum bt_ctf_notif_iter_status notif_iter_status; | |
94cf822e PP |
639 | int ret = 0; |
640 | ||
97ade20b | 641 | assert(ds_file); |
94cf822e PP |
642 | notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields( |
643 | ds_file->notif_iter, packet_header_field, packet_context_field); | |
644 | switch (notif_iter_status) { | |
645 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
646 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
647 | break; | |
648 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
0fbb9a9f | 649 | abort(); |
94cf822e PP |
650 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
651 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
652 | default: | |
653 | goto error; | |
654 | break; | |
655 | } | |
656 | ||
657 | goto end; | |
658 | ||
659 | error: | |
660 | ret = -1; | |
661 | ||
662 | if (packet_header_field) { | |
663 | bt_put(*packet_header_field); | |
664 | } | |
665 | ||
666 | if (packet_context_field) { | |
667 | bt_put(*packet_context_field); | |
668 | } | |
669 | ||
670 | end: | |
94cf822e PP |
671 | return ret; |
672 | } | |
97ade20b JG |
673 | |
674 | BT_HIDDEN | |
675 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
676 | { | |
677 | if (!index) { | |
678 | return; | |
679 | } | |
680 | ||
681 | if (index->entries) { | |
682 | g_array_free(index->entries, TRUE); | |
683 | } | |
684 | g_free(index); | |
685 | } |