1 #ifndef BABELTRACE_CTFSER_INTERNAL_H
2 #define BABELTRACE_CTFSER_INTERNAL_H
5 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017-2019 Philippe Proulx <pproulx@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * The Common Trace Format (CTF) Specification is available at
28 * http://www.efficios.com/ctf
34 #include "compat/mman.h"
35 #include <sys/types.h>
38 #include "common/align.h"
39 #include "compat/endian.h"
40 #include "common/common.h"
41 #include "common/mmap-align.h"
42 #include <babeltrace2/types.h>
43 #include "common/assert.h"
44 #include "compat/bitfield.h"
48 /* Stream file's descriptor */
51 /* Offset (bytes) of memory map (current packet) in the stream file */
54 /* Offset (bytes) of packet's first byte in the memory map */
55 off_t mmap_base_offset
;
57 /* Current offset (bits) within current packet */
58 uint64_t offset_in_cur_packet_bits
;
60 /* Current packet size (bytes) */
61 uint64_t cur_packet_size_bytes
;
63 /* Previous packet size (bytes) */
64 uint64_t prev_packet_size_bytes
;
66 /* Current stream size (bytes) */
67 uint64_t stream_size_bytes
;
69 /* Memory map base address */
70 struct mmap_align
*base_mma
;
72 /* Stream file's path (for debugging) */
77 * Initializes a CTF serializer.
79 * This function opens the file `path` for writing.
82 int bt_ctfser_init(struct bt_ctfser
*ctfser
, const char *path
);
85 * Finalizes a CTF serializer.
87 * This function truncates the stream file so that there's no extra
88 * padding after the last packet, and then closes the file.
91 int bt_ctfser_fini(struct bt_ctfser
*ctfser
);
96 * All the next writing functions are performed within this new packet.
99 int bt_ctfser_open_packet(struct bt_ctfser
*ctfser
);
102 * Closes the current packet, making its size `packet_size_bytes`.
105 void bt_ctfser_close_current_packet(struct bt_ctfser
*ctfser
,
106 uint64_t packet_size_bytes
);
109 int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser
*ctfser
);
112 uint64_t _bt_ctfser_cur_packet_size_bits(struct bt_ctfser
*ctfser
)
114 return ctfser
->cur_packet_size_bytes
* 8;
118 uint64_t _bt_ctfser_prev_packet_size_bits(struct bt_ctfser
*ctfser
)
120 return ctfser
->prev_packet_size_bytes
* 8;
124 uint64_t _bt_ctfser_offset_bytes(struct bt_ctfser
*ctfser
)
126 return ctfser
->offset_in_cur_packet_bits
/ 8;
130 uint8_t *_bt_ctfser_get_addr(struct bt_ctfser
*ctfser
)
132 /* Only makes sense to get the address after aligning on byte */
133 BT_ASSERT(ctfser
->offset_in_cur_packet_bits
% 8 == 0);
134 return ((uint8_t *) mmap_align_addr(ctfser
->base_mma
)) +
135 ctfser
->mmap_base_offset
+ _bt_ctfser_offset_bytes(ctfser
);
139 bool _bt_ctfser_has_space_left(struct bt_ctfser
*ctfser
, uint64_t size_bits
)
141 bool has_space_left
= true;
143 if (unlikely((ctfser
->offset_in_cur_packet_bits
+ size_bits
>
144 _bt_ctfser_cur_packet_size_bits(ctfser
)))) {
145 has_space_left
= false;
149 if (unlikely(size_bits
> UINT64_MAX
- ctfser
->offset_in_cur_packet_bits
)) {
150 has_space_left
= false;
155 return has_space_left
;
159 void _bt_ctfser_incr_offset(struct bt_ctfser
*ctfser
, uint64_t size_bits
)
161 BT_ASSERT(_bt_ctfser_has_space_left(ctfser
, size_bits
));
162 ctfser
->offset_in_cur_packet_bits
+= size_bits
;
166 * Aligns the current offset within the current packet to
167 * `alignment_bits` bits (power of two, > 0).
170 int bt_ctfser_align_offset_in_current_packet(struct bt_ctfser
*ctfser
,
171 uint64_t alignment_bits
)
174 uint64_t align_size_bits
;
176 BT_ASSERT(alignment_bits
> 0);
177 align_size_bits
= ALIGN(ctfser
->offset_in_cur_packet_bits
,
178 alignment_bits
) - ctfser
->offset_in_cur_packet_bits
;
180 if (unlikely(!_bt_ctfser_has_space_left(ctfser
, align_size_bits
))) {
181 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
187 _bt_ctfser_incr_offset(ctfser
, align_size_bits
);
194 int _bt_ctfser_write_byte_aligned_unsigned_int_no_align(
195 struct bt_ctfser
*ctfser
, uint64_t value
,
196 unsigned int size_bits
, int byte_order
)
200 /* Reverse byte order? */
201 bool rbo
= byte_order
!= BYTE_ORDER
;
203 BT_ASSERT(size_bits
% 8 == 0);
204 BT_ASSERT(_bt_ctfser_has_space_left(ctfser
, size_bits
));
209 uint8_t v
= (uint8_t) value
;
211 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
216 uint16_t v
= (uint16_t) value
;
219 v
= GUINT16_SWAP_LE_BE(v
);
222 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
227 uint32_t v
= (uint32_t) value
;
230 v
= GUINT32_SWAP_LE_BE(v
);
233 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
238 uint64_t v
= (uint64_t) value
;
241 v
= GUINT64_SWAP_LE_BE(v
);
244 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
251 _bt_ctfser_incr_offset(ctfser
, size_bits
);
256 int _bt_ctfser_write_byte_aligned_signed_int_no_align(
257 struct bt_ctfser
*ctfser
, int64_t value
,
258 unsigned int size_bits
, int byte_order
)
262 /* Reverse byte order? */
263 bool rbo
= byte_order
!= BYTE_ORDER
;
265 BT_ASSERT(size_bits
% 8 == 0);
266 BT_ASSERT(_bt_ctfser_has_space_left(ctfser
, size_bits
));
271 int8_t v
= (int8_t) value
;
273 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
278 int16_t v
= (int16_t) value
;
281 v
= GUINT16_SWAP_LE_BE(v
);
284 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
289 int32_t v
= (int32_t) value
;
292 v
= GUINT32_SWAP_LE_BE(v
);
295 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
300 int64_t v
= (int64_t) value
;
303 v
= GUINT64_SWAP_LE_BE(v
);
306 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
313 _bt_ctfser_incr_offset(ctfser
, size_bits
);
318 * Writes an unsigned integer known to have a size that is a multiple of
319 * 8 and an alignment that is >= 8 at the current offset within the
323 int bt_ctfser_write_byte_aligned_unsigned_int(struct bt_ctfser
*ctfser
,
324 uint64_t value
, unsigned int alignment_bits
,
325 unsigned int size_bits
, int byte_order
)
329 BT_ASSERT(alignment_bits
% 8 == 0);
330 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
335 if (unlikely(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
336 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
342 ret
= _bt_ctfser_write_byte_aligned_unsigned_int_no_align(ctfser
, value
,
343 size_bits
, byte_order
);
353 * Writes a signed integer known to have a size that is a multiple of 8
354 * and an alignment that is >= 8 at the current offset within the
358 int bt_ctfser_write_byte_aligned_signed_int(struct bt_ctfser
*ctfser
,
359 int64_t value
, unsigned int alignment_bits
,
360 unsigned int size_bits
, int byte_order
)
364 BT_ASSERT(alignment_bits
% 8 == 0);
365 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
370 if (unlikely(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
371 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
377 ret
= _bt_ctfser_write_byte_aligned_signed_int_no_align(ctfser
, value
,
378 size_bits
, byte_order
);
388 * Writes an unsigned integer at the current offset within the current
392 int bt_ctfser_write_unsigned_int(struct bt_ctfser
*ctfser
, uint64_t value
,
393 unsigned int alignment_bits
, unsigned int size_bits
,
398 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
403 if (unlikely(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
404 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
410 if (alignment_bits
% 8 == 0 && size_bits
% 8 == 0) {
411 ret
= _bt_ctfser_write_byte_aligned_unsigned_int_no_align(
412 ctfser
, value
, size_bits
, byte_order
);
416 if (byte_order
== LITTLE_ENDIAN
) {
417 bt_bitfield_write_le(mmap_align_addr(ctfser
->base_mma
) +
418 ctfser
->mmap_base_offset
, uint8_t,
419 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
421 bt_bitfield_write_be(mmap_align_addr(ctfser
->base_mma
) +
422 ctfser
->mmap_base_offset
, uint8_t,
423 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
426 _bt_ctfser_incr_offset(ctfser
, size_bits
);
433 * Writes a signed integer at the current offset within the current
437 int bt_ctfser_write_signed_int(struct bt_ctfser
*ctfser
, int64_t value
,
438 unsigned int alignment_bits
, unsigned int size_bits
,
443 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
448 if (unlikely(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
449 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
455 if (alignment_bits
% 8 == 0 && size_bits
% 8 == 0) {
456 ret
= _bt_ctfser_write_byte_aligned_signed_int_no_align(
457 ctfser
, value
, size_bits
, byte_order
);
461 if (byte_order
== LITTLE_ENDIAN
) {
462 bt_bitfield_write_le(mmap_align_addr(ctfser
->base_mma
) +
463 ctfser
->mmap_base_offset
, uint8_t,
464 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
466 bt_bitfield_write_be(mmap_align_addr(ctfser
->base_mma
) +
467 ctfser
->mmap_base_offset
, uint8_t,
468 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
471 _bt_ctfser_incr_offset(ctfser
, size_bits
);
478 * Writes a 32-bit floating point number at the current offset within
479 * the current packet.
482 int bt_ctfser_write_float32(struct bt_ctfser
*ctfser
, double value
,
483 unsigned int alignment_bits
, int byte_order
)
490 u32f
.f
= (float) value
;
491 return bt_ctfser_write_unsigned_int(ctfser
, (uint64_t) u32f
.u
,
492 alignment_bits
, 32, byte_order
);
496 * Writes a 64-bit floating point number at the current offset within
497 * the current packet.
500 int bt_ctfser_write_float64(struct bt_ctfser
*ctfser
, double value
,
501 unsigned int alignment_bits
, int byte_order
)
509 return bt_ctfser_write_unsigned_int(ctfser
, u64f
.u
, alignment_bits
,
514 * Writes a C string, including the terminating null character, at the
515 * current offset within the current packet.
518 int bt_ctfser_write_string(struct bt_ctfser
*ctfser
, const char *value
)
521 const char *at
= value
;
523 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, 8);
529 if (unlikely(!_bt_ctfser_has_space_left(ctfser
, 8))) {
530 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
536 memcpy(_bt_ctfser_get_addr(ctfser
), at
, sizeof(*at
));
537 _bt_ctfser_incr_offset(ctfser
, 8);
539 if (unlikely(*at
== '\0')) {
551 * Returns the current offset within the current packet (bits).
554 uint64_t bt_ctfser_get_offset_in_current_packet_bits(struct bt_ctfser
*ctfser
)
556 return ctfser
->offset_in_cur_packet_bits
;
560 * Sets the current offset within the current packet (bits).
563 void bt_ctfser_set_offset_in_current_packet_bits(struct bt_ctfser
*ctfser
,
564 uint64_t offset_bits
)
566 BT_ASSERT(offset_bits
<= _bt_ctfser_cur_packet_size_bits(ctfser
));
567 ctfser
->offset_in_cur_packet_bits
= offset_bits
;
571 const char *bt_ctfser_get_file_path(struct bt_ctfser
*ctfser
)
573 return ctfser
->path
->str
;
576 #endif /* BABELTRACE_CTFSER_INTERNAL_H */