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