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 | ||
d7bc0973 SM |
25 | #define BT_COMP_LOG_SELF_COMP (self_comp) |
26 | #define BT_LOG_OUTPUT_LEVEL (log_level) | |
3d731ea9 | 27 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS" |
3fa1b6a3 | 28 | #include "logging/comp-logging.h" |
3d731ea9 | 29 | |
0fbb9a9f | 30 | #include <stdlib.h> |
e98a2d6e PP |
31 | #include <stdio.h> |
32 | #include <stdint.h> | |
33 | #include <stdlib.h> | |
e98a2d6e PP |
34 | #include <glib.h> |
35 | #include <inttypes.h> | |
57952005 MJ |
36 | #include "compat/mman.h" |
37 | #include "compat/endian.h" | |
71c5da58 | 38 | #include <babeltrace2/babeltrace.h> |
57952005 | 39 | #include "common/common.h" |
78586d8a JG |
40 | #include "file.h" |
41 | #include "metadata.h" | |
b09a5592 | 42 | #include "../common/msg-iter/msg-iter.h" |
57952005 | 43 | #include "common/assert.h" |
94cf822e | 44 | #include "data-stream-file.h" |
e9383dfd | 45 | #include <string.h> |
e98a2d6e | 46 | |
4f1f88a6 | 47 | static inline |
94cf822e | 48 | size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 49 | { |
43b07461 SM |
50 | BT_ASSERT_DBG(ds_file->mmap_len >= ds_file->request_offset_in_mapping); |
51 | return ds_file->mmap_len - ds_file->request_offset_in_mapping; | |
e98a2d6e PP |
52 | } |
53 | ||
0eb6c8ee SM |
54 | /* |
55 | * Return true if `offset_in_file` is in the current mapping. | |
56 | */ | |
57 | ||
58 | static | |
59 | bool offset_ist_mapped(struct ctf_fs_ds_file *ds_file, off_t offset_in_file) | |
60 | { | |
61 | return offset_in_file >= ds_file->mmap_offset_in_file && | |
62 | offset_in_file < (ds_file->mmap_offset_in_file + ds_file->mmap_len); | |
63 | } | |
64 | ||
5b29e799 | 65 | static |
3baf7da0 SM |
66 | enum ctf_msg_iter_medium_status ds_file_munmap( |
67 | struct ctf_fs_ds_file *ds_file) | |
e98a2d6e | 68 | { |
3baf7da0 | 69 | enum ctf_msg_iter_medium_status status; |
d7bc0973 SM |
70 | bt_self_component *self_comp = ds_file->self_comp; |
71 | bt_logging_level log_level = ds_file->log_level; | |
e98a2d6e | 72 | |
4bbea805 SM |
73 | BT_ASSERT(ds_file); |
74 | ||
75 | if (!ds_file->mmap_addr) { | |
3baf7da0 | 76 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
94cf822e PP |
77 | goto end; |
78 | } | |
79 | ||
a555e971 | 80 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { |
f67894f9 | 81 | BT_COMP_LOGE_ERRNO("Cannot memory-unmap file", |
2a48e5ae | 82 | ": address=%p, size=%zu, file_path=\"%s\", file=%p", |
94cf822e | 83 | ds_file->mmap_addr, ds_file->mmap_len, |
bb230c9b | 84 | ds_file->file ? ds_file->file->path->str : "NULL", |
2a48e5ae | 85 | ds_file->file ? ds_file->file->fp : NULL); |
3baf7da0 | 86 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
fc9a526c | 87 | goto end; |
e98a2d6e | 88 | } |
94cf822e PP |
89 | |
90 | ds_file->mmap_addr = NULL; | |
91 | ||
3baf7da0 | 92 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
fc9a526c | 93 | end: |
3baf7da0 | 94 | return status; |
e98a2d6e PP |
95 | } |
96 | ||
0eb6c8ee SM |
97 | /* |
98 | * mmap a region of `ds_file` such that `requested_offset_in_file` is in the | |
99 | * mapping. If the currently mmap-ed region already contains | |
100 | * `requested_offset_in_file`, the mapping is kept. | |
101 | * | |
148f02cb SM |
102 | * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`, |
103 | * such that the next call to `request_bytes` will return bytes starting at that | |
104 | * position. | |
0eb6c8ee SM |
105 | * |
106 | * `requested_offset_in_file` must be a valid offset in the file. | |
107 | */ | |
5b29e799 | 108 | static |
0eb6c8ee SM |
109 | enum ctf_msg_iter_medium_status ds_file_mmap( |
110 | struct ctf_fs_ds_file *ds_file, off_t requested_offset_in_file) | |
e98a2d6e | 111 | { |
3baf7da0 | 112 | enum ctf_msg_iter_medium_status status; |
d7bc0973 SM |
113 | bt_self_component *self_comp = ds_file->self_comp; |
114 | bt_logging_level log_level = ds_file->log_level; | |
e98a2d6e | 115 | |
0eb6c8ee SM |
116 | /* Ensure the requested offset is in the file range. */ |
117 | BT_ASSERT(requested_offset_in_file >= 0); | |
118 | BT_ASSERT(requested_offset_in_file < ds_file->file->size); | |
e98a2d6e | 119 | |
0eb6c8ee SM |
120 | /* |
121 | * If the mapping already contains the requested offset, just adjust | |
122 | * requested_offset_in_mapping. | |
123 | */ | |
124 | if (offset_ist_mapped(ds_file, requested_offset_in_file)) { | |
125 | ds_file->request_offset_in_mapping = | |
126 | requested_offset_in_file - ds_file->mmap_offset_in_file; | |
127 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; | |
128 | goto end; | |
e98a2d6e PP |
129 | } |
130 | ||
0eb6c8ee SM |
131 | /* Unmap old region */ |
132 | status = ds_file_munmap(ds_file); | |
133 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { | |
e0f6a64a JG |
134 | goto end; |
135 | } | |
0eb6c8ee SM |
136 | |
137 | /* | |
138 | * Compute a mapping that has the required alignment properties and | |
139 | * contains `requested_offset_in_file`. | |
140 | */ | |
141 | ds_file->request_offset_in_mapping = | |
142 | requested_offset_in_file % bt_mmap_get_offset_align_size(ds_file->log_level); | |
143 | ds_file->mmap_offset_in_file = | |
144 | requested_offset_in_file - ds_file->request_offset_in_mapping; | |
145 | ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset_in_file, | |
146 | ds_file->mmap_max_len); | |
147 | ||
148 | BT_ASSERT(ds_file->mmap_len > 0); | |
149 | ||
a555e971 | 150 | ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len, |
94cf822e | 151 | PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp), |
43b07461 | 152 | ds_file->mmap_offset_in_file, ds_file->log_level); |
94cf822e | 153 | if (ds_file->mmap_addr == MAP_FAILED) { |
f67894f9 | 154 | BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s", |
94cf822e | 155 | ds_file->mmap_len, ds_file->file->path->str, |
43b07461 | 156 | ds_file->file->fp, (intmax_t) ds_file->mmap_offset_in_file, |
78586d8a | 157 | strerror(errno)); |
3baf7da0 SM |
158 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
159 | goto end; | |
e98a2d6e PP |
160 | } |
161 | ||
3baf7da0 | 162 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
0eb6c8ee SM |
163 | |
164 | end: | |
165 | return status; | |
166 | } | |
167 | ||
168 | /* | |
169 | * Change the mapping of the file to read the region that follows the current | |
170 | * mapping. | |
171 | * | |
172 | * If the file hasn't been mapped yet, then everything (mmap_offset_in_file, | |
173 | * mmap_len, request_offset_in_mapping) should have the value 0, which will | |
174 | * result in the beginning of the file getting mapped. | |
175 | * | |
176 | * return _EOF if the current mapping is the end of the file. | |
177 | */ | |
178 | ||
179 | static | |
180 | enum ctf_msg_iter_medium_status ds_file_mmap_next( | |
181 | struct ctf_fs_ds_file *ds_file) | |
182 | { | |
183 | enum ctf_msg_iter_medium_status status; | |
184 | ||
185 | /* | |
186 | * If we're called, it's because more bytes are requested but we have | |
187 | * given all the bytes of the current mapping. | |
188 | */ | |
189 | BT_ASSERT(ds_file->request_offset_in_mapping == ds_file->mmap_len); | |
190 | ||
191 | /* | |
192 | * If the current mapping coincides with the end of the file, there is | |
193 | * no next mapping. | |
194 | */ | |
195 | if (ds_file->mmap_offset_in_file + ds_file->mmap_len == ds_file->file->size) { | |
196 | status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; | |
197 | goto end; | |
198 | } | |
199 | ||
200 | status = ds_file_mmap(ds_file, | |
201 | ds_file->mmap_offset_in_file + ds_file->mmap_len); | |
202 | ||
e98a2d6e | 203 | end: |
3baf7da0 | 204 | return status; |
e98a2d6e PP |
205 | } |
206 | ||
5b29e799 | 207 | static |
5d452e1f | 208 | enum ctf_msg_iter_medium_status medop_request_bytes( |
e98a2d6e PP |
209 | size_t request_sz, uint8_t **buffer_addr, |
210 | size_t *buffer_sz, void *data) | |
211 | { | |
5d452e1f SM |
212 | enum ctf_msg_iter_medium_status status = |
213 | CTF_MSG_ITER_MEDIUM_STATUS_OK; | |
94cf822e | 214 | struct ctf_fs_ds_file *ds_file = data; |
d7bc0973 SM |
215 | bt_self_component *self_comp = ds_file->self_comp; |
216 | bt_logging_level log_level = ds_file->log_level; | |
e98a2d6e | 217 | |
640a46fd | 218 | BT_ASSERT(request_sz > 0); |
e98a2d6e | 219 | |
f102bfaa FD |
220 | /* |
221 | * Check if we have at least one memory-mapped byte left. If we don't, | |
222 | * mmap the next file. | |
223 | */ | |
94cf822e | 224 | if (remaining_mmap_bytes(ds_file) == 0) { |
e98a2d6e | 225 | /* Are we at the end of the file? */ |
43b07461 | 226 | if (ds_file->mmap_offset_in_file >= ds_file->file->size) { |
f67894f9 | 227 | BT_COMP_LOGD("Reached end of file \"%s\" (%p)", |
94cf822e | 228 | ds_file->file->path->str, ds_file->file->fp); |
5d452e1f | 229 | status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
e98a2d6e PP |
230 | goto end; |
231 | } | |
232 | ||
94cf822e | 233 | status = ds_file_mmap_next(ds_file); |
e0f6a64a | 234 | switch (status) { |
5d452e1f | 235 | case CTF_MSG_ITER_MEDIUM_STATUS_OK: |
e0f6a64a | 236 | break; |
5d452e1f | 237 | case CTF_MSG_ITER_MEDIUM_STATUS_EOF: |
e0f6a64a JG |
238 | goto end; |
239 | default: | |
f67894f9 | 240 | BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)", |
94cf822e PP |
241 | ds_file->file->path->str, |
242 | ds_file->file->fp); | |
e98a2d6e PP |
243 | goto error; |
244 | } | |
245 | } | |
246 | ||
640a46fd | 247 | BT_ASSERT(remaining_mmap_bytes(ds_file) > 0); |
94cf822e | 248 | *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz); |
640a46fd | 249 | |
f102bfaa | 250 | BT_ASSERT(ds_file->mmap_addr); |
43b07461 | 251 | *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping; |
640a46fd | 252 | |
43b07461 | 253 | ds_file->request_offset_in_mapping += *buffer_sz; |
e98a2d6e PP |
254 | goto end; |
255 | ||
256 | error: | |
5d452e1f | 257 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
e98a2d6e PP |
258 | |
259 | end: | |
260 | return status; | |
261 | } | |
262 | ||
5b29e799 | 263 | static |
bbbd35bf | 264 | bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t stream_id, |
0659c536 | 265 | void *data) |
e98a2d6e | 266 | { |
94cf822e | 267 | struct ctf_fs_ds_file *ds_file = data; |
8eee8ea2 PP |
268 | bt_stream_class *ds_file_stream_class; |
269 | bt_stream *stream = NULL; | |
9e550e5f | 270 | |
78cf9df6 | 271 | ds_file_stream_class = bt_stream_borrow_class( |
9e550e5f | 272 | ds_file->stream); |
94cf822e | 273 | |
94cf822e PP |
274 | if (stream_class != ds_file_stream_class) { |
275 | /* | |
276 | * Not supported: two packets described by two different | |
277 | * stream classes within the same data stream file. | |
278 | */ | |
279 | goto end; | |
e98a2d6e PP |
280 | } |
281 | ||
94cf822e PP |
282 | stream = ds_file->stream; |
283 | ||
284 | end: | |
285 | return stream; | |
e98a2d6e PP |
286 | } |
287 | ||
bb230c9b | 288 | static |
9a4c10a7 | 289 | enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data) |
bb230c9b | 290 | { |
bb230c9b | 291 | struct ctf_fs_ds_file *ds_file = data; |
bb230c9b | 292 | |
ebe4c684 | 293 | BT_ASSERT(offset >= 0); |
0eb6c8ee | 294 | BT_ASSERT(offset < ds_file->file->size); |
bb230c9b | 295 | |
0eb6c8ee | 296 | return ds_file_mmap(ds_file, offset); |
bb230c9b JG |
297 | } |
298 | ||
55fa6491 | 299 | BT_HIDDEN |
5d452e1f | 300 | struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = { |
e98a2d6e | 301 | .request_bytes = medop_request_bytes, |
a6918753 | 302 | .borrow_stream = medop_borrow_stream, |
bb230c9b | 303 | .seek = medop_seek, |
e98a2d6e | 304 | }; |
55fa6491 | 305 | |
148f02cb SM |
306 | struct ctf_fs_ds_group_medops_data { |
307 | /* Weak, set once at creation time. */ | |
308 | struct ctf_fs_ds_file_group *ds_file_group; | |
309 | ||
310 | /* | |
311 | * Index (as in element rank) of the index entry of ds_file_groups' | |
312 | * index we will read next (so, the one after the one we are reading | |
313 | * right now). | |
314 | */ | |
315 | guint next_index_entry_index; | |
316 | ||
317 | /* | |
318 | * File we are currently reading. Changes whenever we switch to | |
319 | * reading another data file. | |
320 | * | |
321 | * Owned by this. | |
322 | */ | |
323 | struct ctf_fs_ds_file *file; | |
324 | ||
325 | /* Weak, for context / logging / appending causes. */ | |
326 | bt_self_message_iterator *self_msg_iter; | |
327 | bt_logging_level log_level; | |
328 | }; | |
329 | ||
330 | static | |
331 | enum ctf_msg_iter_medium_status medop_group_request_bytes( | |
332 | size_t request_sz, | |
333 | uint8_t **buffer_addr, | |
334 | size_t *buffer_sz, | |
335 | void *void_data) | |
336 | { | |
337 | struct ctf_fs_ds_group_medops_data *data = void_data; | |
338 | ||
339 | /* Return bytes from the current file. */ | |
340 | return medop_request_bytes(request_sz, buffer_addr, buffer_sz, data->file); | |
341 | } | |
342 | ||
343 | static | |
344 | bt_stream *medop_group_borrow_stream( | |
345 | bt_stream_class *stream_class, | |
346 | int64_t stream_id, | |
347 | void *void_data) | |
348 | { | |
349 | struct ctf_fs_ds_group_medops_data *data = void_data; | |
350 | ||
351 | return medop_borrow_stream(stream_class, stream_id, data->file); | |
352 | } | |
353 | ||
354 | /* | |
355 | * Set `data->file` to prepare it to read the packet described | |
356 | * by `index_entry`. | |
357 | */ | |
358 | ||
359 | static | |
360 | enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_set_file( | |
361 | struct ctf_fs_ds_group_medops_data *data, | |
362 | struct ctf_fs_ds_index_entry *index_entry, | |
363 | bt_self_message_iterator *self_msg_iter, | |
364 | bt_logging_level log_level) | |
365 | { | |
366 | enum ctf_msg_iter_medium_status status; | |
367 | ||
368 | BT_ASSERT(data); | |
369 | BT_ASSERT(index_entry); | |
370 | ||
371 | /* Check if that file is already the one mapped. */ | |
372 | if (!data->file || strcmp(index_entry->path, data->file->file->path->str) != 0) { | |
373 | /* Destroy the previously used file. */ | |
374 | ctf_fs_ds_file_destroy(data->file); | |
375 | ||
376 | /* Create the new file. */ | |
377 | data->file = ctf_fs_ds_file_create( | |
378 | data->ds_file_group->ctf_fs_trace, | |
379 | self_msg_iter, | |
380 | data->ds_file_group->stream, | |
381 | index_entry->path, | |
382 | log_level); | |
383 | if (!data->file) { | |
384 | BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter, | |
385 | "failed to create ctf_fs_ds_file."); | |
386 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; | |
387 | goto end; | |
388 | } | |
389 | } | |
390 | ||
391 | /* | |
392 | * Ensure the right portion of the file will be returned on the next | |
393 | * request_bytes call. | |
394 | */ | |
395 | status = ds_file_mmap(data->file, index_entry->offset); | |
396 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { | |
397 | goto end; | |
398 | } | |
399 | ||
400 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; | |
401 | ||
402 | end: | |
403 | return status; | |
404 | } | |
405 | ||
406 | static | |
407 | enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data) | |
408 | { | |
409 | struct ctf_fs_ds_group_medops_data *data = void_data; | |
410 | struct ctf_fs_ds_index_entry *index_entry; | |
411 | enum ctf_msg_iter_medium_status status; | |
412 | ||
413 | /* If we have gone through all index entries, we are done. */ | |
414 | if (data->next_index_entry_index >= | |
415 | data->ds_file_group->index->entries->len) { | |
416 | status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; | |
417 | goto end; | |
418 | } | |
419 | ||
420 | /* | |
421 | * Otherwise, look up the next index entry / packet and prepare it | |
422 | * for reading. | |
423 | */ | |
424 | index_entry = g_ptr_array_index( | |
425 | data->ds_file_group->index->entries, | |
426 | data->next_index_entry_index); | |
427 | ||
428 | status = ctf_fs_ds_group_medops_set_file( | |
429 | data, index_entry, data->self_msg_iter, data->log_level); | |
430 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { | |
431 | goto end; | |
432 | } | |
433 | ||
434 | data->next_index_entry_index++; | |
435 | ||
436 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; | |
437 | end: | |
438 | return status; | |
439 | } | |
440 | ||
441 | BT_HIDDEN | |
442 | void ctf_fs_ds_group_medops_data_destroy( | |
443 | struct ctf_fs_ds_group_medops_data *data) | |
444 | { | |
445 | if (!data) { | |
446 | goto end; | |
447 | } | |
448 | ||
449 | ctf_fs_ds_file_destroy(data->file); | |
450 | ||
451 | g_free(data); | |
452 | ||
453 | end: | |
454 | return; | |
455 | } | |
456 | ||
457 | enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create( | |
458 | struct ctf_fs_ds_file_group *ds_file_group, | |
459 | bt_self_message_iterator *self_msg_iter, | |
460 | bt_logging_level log_level, | |
461 | struct ctf_fs_ds_group_medops_data **out) | |
462 | { | |
463 | struct ctf_fs_ds_group_medops_data *data; | |
464 | enum ctf_msg_iter_medium_status status; | |
465 | ||
466 | BT_ASSERT(self_msg_iter); | |
467 | BT_ASSERT(ds_file_group); | |
468 | BT_ASSERT(ds_file_group->index); | |
469 | BT_ASSERT(ds_file_group->index->entries->len > 0); | |
470 | ||
471 | data = g_new0(struct ctf_fs_ds_group_medops_data, 1); | |
472 | if (!data) { | |
473 | BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter, | |
474 | "Failed to allocate a struct ctf_fs_ds_group_medops_data"); | |
475 | status = CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR; | |
476 | goto error; | |
477 | } | |
478 | ||
479 | data->ds_file_group = ds_file_group; | |
480 | data->self_msg_iter = self_msg_iter; | |
481 | data->log_level = log_level; | |
482 | ||
483 | /* | |
484 | * No need to prepare the first file. ctf_msg_iter will call | |
485 | * switch_packet before reading the first packet, it will be | |
486 | * done then. | |
487 | */ | |
488 | ||
489 | *out = data; | |
490 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; | |
491 | goto end; | |
492 | ||
493 | error: | |
494 | ctf_fs_ds_group_medops_data_destroy(data); | |
495 | ||
496 | end: | |
497 | return status; | |
498 | } | |
499 | ||
500 | void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data) | |
501 | { | |
502 | data->next_index_entry_index = 0; | |
503 | } | |
504 | ||
505 | struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = { | |
506 | .request_bytes = medop_group_request_bytes, | |
507 | .borrow_stream = medop_group_borrow_stream, | |
508 | .switch_packet = medop_group_switch_packet, | |
509 | ||
510 | /* | |
511 | * We don't support seeking using this medops. It would probably be | |
512 | * possible, but it's not needed at the moment. | |
513 | */ | |
514 | .seek = NULL, | |
515 | }; | |
516 | ||
408c8c19 SM |
517 | static |
518 | struct ctf_fs_ds_index_entry *ctf_fs_ds_index_entry_create( | |
519 | bt_self_component *self_comp, bt_logging_level log_level) | |
520 | { | |
521 | struct ctf_fs_ds_index_entry *entry; | |
522 | ||
523 | entry = g_new0(struct ctf_fs_ds_index_entry, 1); | |
524 | if (!entry) { | |
525 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a ctf_fs_ds_index_entry."); | |
526 | goto end; | |
527 | } | |
528 | ||
529 | entry->packet_seq_num = UINT64_MAX; | |
530 | ||
531 | end: | |
532 | return entry; | |
533 | } | |
534 | ||
97ade20b | 535 | static |
35fa110e | 536 | int convert_cycles_to_ns(struct ctf_clock_class *clock_class, |
97ade20b | 537 | uint64_t cycles, int64_t *ns) |
b6c3dcb2 | 538 | { |
35fa110e PP |
539 | return bt_util_clock_cycles_to_ns_from_origin(cycles, |
540 | clock_class->frequency, clock_class->offset_seconds, | |
541 | clock_class->offset_cycles, ns); | |
97ade20b JG |
542 | } |
543 | ||
544 | static | |
545 | struct ctf_fs_ds_index *build_index_from_idx_file( | |
13772304 | 546 | struct ctf_fs_ds_file *ds_file, |
3b89958b SM |
547 | struct ctf_fs_ds_file_info *file_info, |
548 | struct ctf_msg_iter *msg_iter) | |
97ade20b JG |
549 | { |
550 | int ret; | |
b6c3dcb2 JG |
551 | gchar *directory = NULL; |
552 | gchar *basename = NULL; | |
553 | GString *index_basename = NULL; | |
554 | gchar *index_file_path = NULL; | |
555 | GMappedFile *mapped_file = NULL; | |
556 | gsize filesize; | |
97ade20b JG |
557 | const char *mmap_begin = NULL, *file_pos = NULL; |
558 | const struct ctf_packet_index_file_hdr *header = NULL; | |
559 | struct ctf_fs_ds_index *index = NULL; | |
e58dfd9c | 560 | struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL; |
b6c3dcb2 JG |
561 | uint64_t total_packets_size = 0; |
562 | size_t file_index_entry_size; | |
563 | size_t file_entry_count; | |
564 | size_t i; | |
7b33a0e0 | 565 | struct ctf_stream_class *sc; |
5d452e1f | 566 | struct ctf_msg_iter_packet_properties props; |
2068d50b | 567 | uint32_t version_major, version_minor; |
d7bc0973 SM |
568 | bt_self_component *self_comp = ds_file->self_comp; |
569 | bt_logging_level log_level = ds_file->log_level; | |
97ade20b | 570 | |
f67894f9 | 571 | BT_COMP_LOGI("Building index from .idx file of stream file %s", |
97ade20b | 572 | ds_file->file->path->str); |
3b89958b | 573 | ret = ctf_msg_iter_get_packet_properties(msg_iter, &props); |
97ade20b | 574 | if (ret) { |
f67894f9 | 575 | BT_COMP_LOGI_STR("Cannot read first packet's header and context fields."); |
7b33a0e0 PP |
576 | goto error; |
577 | } | |
578 | ||
7b33a0e0 PP |
579 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
580 | props.stream_class_id); | |
581 | BT_ASSERT(sc); | |
582 | if (!sc->default_clock_class) { | |
f67894f9 | 583 | BT_COMP_LOGI_STR("Cannot find stream class's default clock class."); |
97ade20b JG |
584 | goto error; |
585 | } | |
b6c3dcb2 JG |
586 | |
587 | /* Look for index file in relative path index/name.idx. */ | |
94cf822e | 588 | basename = g_path_get_basename(ds_file->file->path->str); |
b6c3dcb2 | 589 | if (!basename) { |
f67894f9 | 590 | BT_COMP_LOGE("Cannot get the basename of datastream file %s", |
55314f2a | 591 | ds_file->file->path->str); |
97ade20b | 592 | goto error; |
b6c3dcb2 JG |
593 | } |
594 | ||
94cf822e | 595 | directory = g_path_get_dirname(ds_file->file->path->str); |
b6c3dcb2 | 596 | if (!directory) { |
f67894f9 | 597 | BT_COMP_LOGE("Cannot get dirname of datastream file %s", |
55314f2a | 598 | ds_file->file->path->str); |
97ade20b | 599 | goto error; |
b6c3dcb2 JG |
600 | } |
601 | ||
602 | index_basename = g_string_new(basename); | |
603 | if (!index_basename) { | |
f67894f9 | 604 | BT_COMP_LOGE_STR("Cannot allocate index file basename string"); |
97ade20b | 605 | goto error; |
b6c3dcb2 JG |
606 | } |
607 | ||
608 | g_string_append(index_basename, ".idx"); | |
609 | index_file_path = g_build_filename(directory, "index", | |
610 | index_basename->str, NULL); | |
611 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); | |
06367fa3 | 612 | if (!mapped_file) { |
f67894f9 | 613 | BT_COMP_LOGD("Cannot create new mapped file %s", |
55314f2a | 614 | index_file_path); |
97ade20b | 615 | goto error; |
06367fa3 | 616 | } |
65cbebef JG |
617 | |
618 | /* | |
619 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
620 | * Traces with such large indexes have never been seen in the wild, | |
621 | * but this would need to be adjusted to support them. | |
622 | */ | |
b6c3dcb2 JG |
623 | filesize = g_mapped_file_get_length(mapped_file); |
624 | if (filesize < sizeof(*header)) { | |
f67894f9 | 625 | BT_COMP_LOGW("Invalid LTTng trace index file: " |
65cbebef JG |
626 | "file size (%zu bytes) < header size (%zu bytes)", |
627 | filesize, sizeof(*header)); | |
97ade20b | 628 | goto error; |
b6c3dcb2 JG |
629 | } |
630 | ||
631 | mmap_begin = g_mapped_file_get_contents(mapped_file); | |
632 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; | |
633 | ||
634 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); | |
635 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { | |
f67894f9 | 636 | BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed"); |
97ade20b | 637 | goto error; |
b6c3dcb2 | 638 | } |
b6c3dcb2 | 639 | |
2068d50b SM |
640 | version_major = be32toh(header->index_major); |
641 | version_minor = be32toh(header->index_minor); | |
642 | if (version_major != 1) { | |
643 | BT_COMP_LOGW( | |
644 | "Unknown LTTng trace index version: " | |
645 | "major=%" PRIu32 ", minor=%" PRIu32, | |
646 | version_major, version_minor); | |
647 | goto error; | |
648 | } | |
649 | ||
b6c3dcb2 | 650 | file_index_entry_size = be32toh(header->packet_index_len); |
c0b480d3 JG |
651 | if (file_index_entry_size < CTF_INDEX_1_0_SIZE) { |
652 | BT_COMP_LOGW("Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): " | |
653 | "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu", | |
654 | file_index_entry_size, CTF_INDEX_1_0_SIZE); | |
655 | goto error; | |
656 | } | |
657 | ||
b6c3dcb2 | 658 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; |
97ade20b | 659 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
f67894f9 | 660 | BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header " |
65cbebef JG |
661 | "(%zu bytes) is not a multiple of the index entry size " |
662 | "(%zu bytes)", (filesize - sizeof(*header)), | |
663 | sizeof(*header)); | |
97ade20b | 664 | goto error; |
b6c3dcb2 JG |
665 | } |
666 | ||
f67894f9 | 667 | index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp); |
97ade20b JG |
668 | if (!index) { |
669 | goto error; | |
b6c3dcb2 | 670 | } |
97ade20b | 671 | |
b6c3dcb2 JG |
672 | for (i = 0; i < file_entry_count; i++) { |
673 | struct ctf_packet_index *file_index = | |
674 | (struct ctf_packet_index *) file_pos; | |
675 | uint64_t packet_size = be64toh(file_index->packet_size); | |
676 | ||
677 | if (packet_size % CHAR_BIT) { | |
f67894f9 | 678 | BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file"); |
97ade20b | 679 | goto error; |
b6c3dcb2 JG |
680 | } |
681 | ||
408c8c19 SM |
682 | index_entry = ctf_fs_ds_index_entry_create( |
683 | ds_file->self_comp, ds_file->log_level); | |
779f4a23 | 684 | if (!index_entry) { |
408c8c19 SM |
685 | BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp, |
686 | "Failed to create a ctf_fs_ds_index_entry."); | |
779f4a23 FD |
687 | goto error; |
688 | } | |
689 | ||
13772304 FD |
690 | /* Set path to stream file. */ |
691 | index_entry->path = file_info->path->str; | |
692 | ||
b6c3dcb2 JG |
693 | /* Convert size in bits to bytes. */ |
694 | packet_size /= CHAR_BIT; | |
97ade20b | 695 | index_entry->packet_size = packet_size; |
b6c3dcb2 | 696 | |
97ade20b | 697 | index_entry->offset = be64toh(file_index->offset); |
e58dfd9c | 698 | if (i != 0 && index_entry->offset < prev_index_entry->offset) { |
f67894f9 | 699 | BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
65cbebef | 700 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, |
6cef4a7d | 701 | prev_index_entry->offset, index_entry->offset); |
97ade20b | 702 | goto error; |
b6c3dcb2 JG |
703 | } |
704 | ||
97ade20b JG |
705 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
706 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); | |
707 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { | |
f67894f9 | 708 | BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
65cbebef JG |
709 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, |
710 | index_entry->timestamp_begin, | |
711 | index_entry->timestamp_end); | |
97ade20b JG |
712 | goto error; |
713 | } | |
714 | ||
715 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
7b33a0e0 | 716 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
717 | index_entry->timestamp_begin, |
718 | &index_entry->timestamp_begin_ns); | |
719 | if (ret) { | |
f67894f9 | 720 | BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
97ade20b JG |
721 | goto error; |
722 | } | |
7b33a0e0 | 723 | ret = convert_cycles_to_ns(sc->default_clock_class, |
97ade20b JG |
724 | index_entry->timestamp_end, |
725 | &index_entry->timestamp_end_ns); | |
726 | if (ret) { | |
f67894f9 | 727 | BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
97ade20b | 728 | goto error; |
b6c3dcb2 JG |
729 | } |
730 | ||
408c8c19 SM |
731 | if (version_minor >= 1) { |
732 | index_entry->packet_seq_num = be64toh(file_index->packet_seq_num); | |
733 | } | |
734 | ||
b6c3dcb2 JG |
735 | total_packets_size += packet_size; |
736 | file_pos += file_index_entry_size; | |
779f4a23 | 737 | |
e58dfd9c | 738 | prev_index_entry = index_entry; |
c0ba90e9 SM |
739 | |
740 | /* Give ownership of `index_entry` to `index->entries`. */ | |
741 | g_ptr_array_add(index->entries, index_entry); | |
742 | index_entry = NULL; | |
b6c3dcb2 JG |
743 | } |
744 | ||
745 | /* Validate that the index addresses the complete stream. */ | |
94cf822e | 746 | if (ds_file->file->size != total_packets_size) { |
f67894f9 | 747 | BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
bb230c9b | 748 | "file-size=%" PRIu64 ", total-packets-size=%" PRIu64, |
65cbebef | 749 | ds_file->file->size, total_packets_size); |
97ade20b | 750 | goto error; |
b6c3dcb2 JG |
751 | } |
752 | end: | |
753 | g_free(directory); | |
754 | g_free(basename); | |
755 | g_free(index_file_path); | |
06367fa3 JG |
756 | if (index_basename) { |
757 | g_string_free(index_basename, TRUE); | |
758 | } | |
759 | if (mapped_file) { | |
760 | g_mapped_file_unref(mapped_file); | |
761 | } | |
97ade20b JG |
762 | return index; |
763 | error: | |
764 | ctf_fs_ds_index_destroy(index); | |
779f4a23 | 765 | g_free(index_entry); |
97ade20b | 766 | index = NULL; |
b6c3dcb2 JG |
767 | goto end; |
768 | } | |
769 | ||
bb230c9b JG |
770 | static |
771 | int init_index_entry(struct ctf_fs_ds_index_entry *entry, | |
7b33a0e0 | 772 | struct ctf_fs_ds_file *ds_file, |
5d452e1f | 773 | struct ctf_msg_iter_packet_properties *props, |
7b33a0e0 | 774 | off_t packet_size, off_t packet_offset) |
bb230c9b | 775 | { |
414ee0f4 | 776 | int ret = 0; |
7b33a0e0 | 777 | struct ctf_stream_class *sc; |
d7bc0973 SM |
778 | bt_self_component *self_comp = ds_file->self_comp; |
779 | bt_logging_level log_level = ds_file->log_level; | |
bb230c9b | 780 | |
7b33a0e0 PP |
781 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
782 | props->stream_class_id); | |
783 | BT_ASSERT(sc); | |
8b45963b | 784 | BT_ASSERT(packet_offset >= 0); |
bb230c9b | 785 | entry->offset = packet_offset; |
8b45963b | 786 | BT_ASSERT(packet_size >= 0); |
bb230c9b JG |
787 | entry->packet_size = packet_size; |
788 | ||
0476f6f7 | 789 | if (props->snapshots.beginning_clock != UINT64_C(-1)) { |
c2f37699 FD |
790 | entry->timestamp_begin = props->snapshots.beginning_clock; |
791 | ||
0476f6f7 PP |
792 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
793 | ret = convert_cycles_to_ns(sc->default_clock_class, | |
794 | props->snapshots.beginning_clock, | |
795 | &entry->timestamp_begin_ns); | |
796 | if (ret) { | |
f67894f9 | 797 | BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); |
0476f6f7 PP |
798 | goto end; |
799 | } | |
800 | } else { | |
c2f37699 | 801 | entry->timestamp_begin = UINT64_C(-1); |
0476f6f7 | 802 | entry->timestamp_begin_ns = UINT64_C(-1); |
bb230c9b JG |
803 | } |
804 | ||
0476f6f7 | 805 | if (props->snapshots.end_clock != UINT64_C(-1)) { |
c2f37699 FD |
806 | entry->timestamp_end = props->snapshots.end_clock; |
807 | ||
808 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
0476f6f7 PP |
809 | ret = convert_cycles_to_ns(sc->default_clock_class, |
810 | props->snapshots.end_clock, | |
811 | &entry->timestamp_end_ns); | |
812 | if (ret) { | |
f67894f9 | 813 | BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch."); |
0476f6f7 PP |
814 | goto end; |
815 | } | |
816 | } else { | |
c2f37699 | 817 | entry->timestamp_end = UINT64_C(-1); |
0476f6f7 | 818 | entry->timestamp_end_ns = UINT64_C(-1); |
bb230c9b | 819 | } |
c43f6ee2 | 820 | |
bb230c9b | 821 | end: |
bb230c9b JG |
822 | return ret; |
823 | } | |
824 | ||
825 | static | |
826 | struct ctf_fs_ds_index *build_index_from_stream_file( | |
13772304 | 827 | struct ctf_fs_ds_file *ds_file, |
3b89958b SM |
828 | struct ctf_fs_ds_file_info *file_info, |
829 | struct ctf_msg_iter *msg_iter) | |
bb230c9b JG |
830 | { |
831 | int ret; | |
832 | struct ctf_fs_ds_index *index = NULL; | |
5d452e1f | 833 | enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK; |
bbbd35bf | 834 | off_t current_packet_offset_bytes = 0; |
d7bc0973 SM |
835 | bt_self_component *self_comp = ds_file->self_comp; |
836 | bt_logging_level log_level = ds_file->log_level; | |
bb230c9b | 837 | |
f67894f9 | 838 | BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str); |
bb230c9b | 839 | |
f67894f9 | 840 | index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp); |
bb230c9b JG |
841 | if (!index) { |
842 | goto error; | |
843 | } | |
844 | ||
5eb3f5e6 | 845 | while (true) { |
7b33a0e0 | 846 | off_t current_packet_size_bytes; |
779f4a23 | 847 | struct ctf_fs_ds_index_entry *index_entry; |
5d452e1f | 848 | struct ctf_msg_iter_packet_properties props; |
bb230c9b | 849 | |
bbbd35bf | 850 | if (current_packet_offset_bytes < 0) { |
f67894f9 | 851 | BT_COMP_LOGE_STR("Cannot get the current packet's offset."); |
bbbd35bf PP |
852 | goto error; |
853 | } else if (current_packet_offset_bytes > ds_file->file->size) { | |
f67894f9 | 854 | BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file)."); |
bbbd35bf PP |
855 | goto error; |
856 | } else if (current_packet_offset_bytes == ds_file->file->size) { | |
857 | /* No more data */ | |
858 | break; | |
859 | } | |
860 | ||
3b89958b | 861 | iter_status = ctf_msg_iter_seek(msg_iter, |
bbbd35bf | 862 | current_packet_offset_bytes); |
5d452e1f | 863 | if (iter_status != CTF_MSG_ITER_STATUS_OK) { |
bb230c9b JG |
864 | goto error; |
865 | } | |
a6918753 | 866 | |
5d452e1f | 867 | iter_status = ctf_msg_iter_get_packet_properties( |
3b89958b | 868 | msg_iter, &props); |
5d452e1f | 869 | if (iter_status != CTF_MSG_ITER_STATUS_OK) { |
bb230c9b JG |
870 | goto error; |
871 | } | |
872 | ||
bbbd35bf PP |
873 | if (props.exp_packet_total_size >= 0) { |
874 | current_packet_size_bytes = | |
875 | (uint64_t) props.exp_packet_total_size / 8; | |
876 | } else { | |
877 | current_packet_size_bytes = ds_file->file->size; | |
878 | } | |
bb230c9b | 879 | |
bbbd35bf | 880 | if (current_packet_offset_bytes + current_packet_size_bytes > |
bb230c9b | 881 | ds_file->file->size) { |
f67894f9 | 882 | BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", " |
bb230c9b JG |
883 | "packet-offset=%jd, packet-size-bytes=%jd, " |
884 | "file-size=%jd", | |
885 | ds_file->file->path->str, | |
2655783b JR |
886 | (intmax_t) current_packet_offset_bytes, |
887 | (intmax_t) current_packet_size_bytes, | |
888 | (intmax_t) ds_file->file->size); | |
bb230c9b JG |
889 | goto error; |
890 | } | |
891 | ||
408c8c19 SM |
892 | index_entry = ctf_fs_ds_index_entry_create( |
893 | ds_file->self_comp, ds_file->log_level); | |
779f4a23 | 894 | if (!index_entry) { |
408c8c19 SM |
895 | BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp, |
896 | "Failed to create a ctf_fs_ds_index_entry."); | |
bb230c9b JG |
897 | goto error; |
898 | } | |
899 | ||
13772304 FD |
900 | /* Set path to stream file. */ |
901 | index_entry->path = file_info->path->str; | |
902 | ||
779f4a23 | 903 | ret = init_index_entry(index_entry, ds_file, &props, |
bbbd35bf | 904 | current_packet_size_bytes, current_packet_offset_bytes); |
bb230c9b | 905 | if (ret) { |
779f4a23 | 906 | g_free(index_entry); |
bb230c9b JG |
907 | goto error; |
908 | } | |
779f4a23 FD |
909 | |
910 | g_ptr_array_add(index->entries, index_entry); | |
911 | ||
9502dc00 | 912 | current_packet_offset_bytes += current_packet_size_bytes; |
f67894f9 | 913 | BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, " |
9502dc00 | 914 | "next-packet-offset=%jd", |
2655783b JR |
915 | (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes), |
916 | (intmax_t) current_packet_offset_bytes); | |
bb230c9b | 917 | } |
a6918753 | 918 | |
bb230c9b | 919 | end: |
bb230c9b | 920 | return index; |
a6918753 | 921 | |
bb230c9b JG |
922 | error: |
923 | ctf_fs_ds_index_destroy(index); | |
924 | index = NULL; | |
925 | goto end; | |
926 | } | |
927 | ||
e7a4393b | 928 | BT_HIDDEN |
94cf822e PP |
929 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
930 | struct ctf_fs_trace *ctf_fs_trace, | |
c7d8bfa1 | 931 | bt_self_message_iterator *self_msg_iter, |
3d731ea9 PP |
932 | bt_stream *stream, const char *path, |
933 | bt_logging_level log_level) | |
e98a2d6e | 934 | { |
b6c3dcb2 | 935 | int ret; |
c8ebf034 | 936 | const size_t offset_align = bt_mmap_get_offset_align_size(log_level); |
94cf822e | 937 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
e98a2d6e | 938 | |
94cf822e | 939 | if (!ds_file) { |
e98a2d6e PP |
940 | goto error; |
941 | } | |
942 | ||
3d731ea9 | 943 | ds_file->log_level = log_level; |
f67894f9 | 944 | ds_file->self_comp = ctf_fs_trace->self_comp; |
c7d8bfa1 | 945 | ds_file->self_msg_iter = self_msg_iter; |
f67894f9 | 946 | ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp); |
94cf822e | 947 | if (!ds_file->file) { |
4f1f88a6 PP |
948 | goto error; |
949 | } | |
950 | ||
4b70020d | 951 | ds_file->stream = stream; |
8c6884d9 | 952 | bt_stream_get_ref(ds_file->stream); |
7b33a0e0 | 953 | ds_file->metadata = ctf_fs_trace->metadata; |
94cf822e | 954 | g_string_assign(ds_file->file->path, path); |
55314f2a | 955 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
4f1f88a6 PP |
956 | if (ret) { |
957 | goto error; | |
958 | } | |
959 | ||
c8ebf034 | 960 | ds_file->mmap_max_len = offset_align * 2048; |
94cf822e | 961 | |
e98a2d6e | 962 | goto end; |
1a9f7075 | 963 | |
e98a2d6e | 964 | error: |
78586d8a | 965 | /* Do not touch "borrowed" file. */ |
94cf822e PP |
966 | ctf_fs_ds_file_destroy(ds_file); |
967 | ds_file = NULL; | |
1a9f7075 | 968 | |
e98a2d6e | 969 | end: |
94cf822e | 970 | return ds_file; |
e98a2d6e PP |
971 | } |
972 | ||
97ade20b JG |
973 | BT_HIDDEN |
974 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( | |
13772304 | 975 | struct ctf_fs_ds_file *ds_file, |
3b89958b SM |
976 | struct ctf_fs_ds_file_info *file_info, |
977 | struct ctf_msg_iter *msg_iter) | |
97ade20b | 978 | { |
bb230c9b | 979 | struct ctf_fs_ds_index *index; |
d7bc0973 SM |
980 | bt_self_component *self_comp = ds_file->self_comp; |
981 | bt_logging_level log_level = ds_file->log_level; | |
bb230c9b | 982 | |
3b89958b | 983 | index = build_index_from_idx_file(ds_file, file_info, msg_iter); |
bb230c9b JG |
984 | if (index) { |
985 | goto end; | |
986 | } | |
987 | ||
f67894f9 | 988 | BT_COMP_LOGI("Failed to build index from .index file; " |
bb230c9b | 989 | "falling back to stream indexing."); |
3b89958b | 990 | index = build_index_from_stream_file(ds_file, file_info, msg_iter); |
bb230c9b JG |
991 | end: |
992 | return index; | |
97ade20b JG |
993 | } |
994 | ||
779f4a23 | 995 | BT_HIDDEN |
f67894f9 PP |
996 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level, |
997 | bt_self_component *self_comp) | |
779f4a23 FD |
998 | { |
999 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); | |
1000 | ||
1001 | if (!index) { | |
f67894f9 | 1002 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
3d731ea9 | 1003 | "Failed to allocate index"); |
779f4a23 FD |
1004 | goto error; |
1005 | } | |
1006 | ||
1007 | index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free); | |
1008 | if (!index->entries) { | |
f67894f9 | 1009 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
3d731ea9 | 1010 | "Failed to allocate index entries."); |
779f4a23 FD |
1011 | goto error; |
1012 | } | |
1013 | ||
1014 | goto end; | |
1015 | ||
1016 | error: | |
1017 | ctf_fs_ds_index_destroy(index); | |
1018 | index = NULL; | |
1019 | end: | |
1020 | return index; | |
1021 | } | |
1022 | ||
5b29e799 | 1023 | BT_HIDDEN |
94cf822e | 1024 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 1025 | { |
94cf822e | 1026 | if (!ds_file) { |
5b29e799 | 1027 | return; |
043e2020 JG |
1028 | } |
1029 | ||
8c6884d9 | 1030 | bt_stream_put_ref(ds_file->stream); |
94cf822e | 1031 | (void) ds_file_munmap(ds_file); |
0982a26d | 1032 | |
94cf822e PP |
1033 | if (ds_file->file) { |
1034 | ctf_fs_file_destroy(ds_file->file); | |
e98a2d6e PP |
1035 | } |
1036 | ||
94cf822e | 1037 | g_free(ds_file); |
e98a2d6e | 1038 | } |
4f1f88a6 | 1039 | |
97ade20b JG |
1040 | BT_HIDDEN |
1041 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) | |
1042 | { | |
1043 | if (!index) { | |
1044 | return; | |
1045 | } | |
1046 | ||
1047 | if (index->entries) { | |
779f4a23 | 1048 | g_ptr_array_free(index->entries, TRUE); |
97ade20b JG |
1049 | } |
1050 | g_free(index); | |
1051 | } |