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