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