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
35 #include "compat/mman.h"
36 #include <sys/types.h>
39 #include "common/align.h"
40 #include "compat/endian.h"
41 #include "common/common.h"
42 #include "common/mmap-align.h"
43 #include <babeltrace2/types.h>
44 #include "common/assert.h"
45 #include "common/macros.h"
46 #include "compat/bitfield.h"
50 /* Stream file's descriptor */
53 /* Offset (bytes) of memory map (current packet) in the stream file */
56 /* Offset (bytes) of packet's first byte in the memory map */
57 off_t mmap_base_offset
;
59 /* Current offset (bits) within current packet */
60 uint64_t offset_in_cur_packet_bits
;
62 /* Current packet size (bytes) */
63 uint64_t cur_packet_size_bytes
;
65 /* Previous packet size (bytes) */
66 uint64_t prev_packet_size_bytes
;
68 /* Current stream size (bytes) */
69 uint64_t stream_size_bytes
;
71 /* Memory map base address */
72 struct mmap_align
*base_mma
;
74 /* Stream file's path (for debugging) */
77 /* Serializer's log level */
82 * Initializes a CTF serializer.
84 * This function opens the file `path` for writing.
87 int bt_ctfser_init(struct bt_ctfser
*ctfser
, const char *path
,
91 * Finalizes a CTF serializer.
93 * This function truncates the stream file so that there's no extra
94 * padding after the last packet, and then closes the file.
97 int bt_ctfser_fini(struct bt_ctfser
*ctfser
);
100 * Opens a new packet.
102 * All the next writing functions are performed within this new packet.
105 int bt_ctfser_open_packet(struct bt_ctfser
*ctfser
);
108 * Closes the current packet, making its size `packet_size_bytes`.
111 void bt_ctfser_close_current_packet(struct bt_ctfser
*ctfser
,
112 uint64_t packet_size_bytes
);
115 int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser
*ctfser
);
118 uint64_t _bt_ctfser_cur_packet_size_bits(struct bt_ctfser
*ctfser
)
120 return ctfser
->cur_packet_size_bytes
* 8;
124 uint64_t _bt_ctfser_prev_packet_size_bits(struct bt_ctfser
*ctfser
)
126 return ctfser
->prev_packet_size_bytes
* 8;
130 uint64_t _bt_ctfser_offset_bytes(struct bt_ctfser
*ctfser
)
132 return ctfser
->offset_in_cur_packet_bits
/ 8;
136 uint8_t *_bt_ctfser_get_addr(struct bt_ctfser
*ctfser
)
138 /* Only makes sense to get the address after aligning on byte */
139 BT_ASSERT_DBG(ctfser
->offset_in_cur_packet_bits
% 8 == 0);
140 return ((uint8_t *) mmap_align_addr(ctfser
->base_mma
)) +
141 ctfser
->mmap_base_offset
+ _bt_ctfser_offset_bytes(ctfser
);
145 bool _bt_ctfser_has_space_left(struct bt_ctfser
*ctfser
, uint64_t size_bits
)
147 bool has_space_left
= true;
149 if (G_UNLIKELY((ctfser
->offset_in_cur_packet_bits
+ size_bits
>
150 _bt_ctfser_cur_packet_size_bits(ctfser
)))) {
151 has_space_left
= false;
155 if (G_UNLIKELY(size_bits
> UINT64_MAX
- ctfser
->offset_in_cur_packet_bits
)) {
156 has_space_left
= false;
161 return has_space_left
;
165 void _bt_ctfser_incr_offset(struct bt_ctfser
*ctfser
, uint64_t size_bits
)
167 BT_ASSERT_DBG(_bt_ctfser_has_space_left(ctfser
, size_bits
));
168 ctfser
->offset_in_cur_packet_bits
+= size_bits
;
172 * Aligns the current offset within the current packet to
173 * `alignment_bits` bits (power of two, > 0).
176 int bt_ctfser_align_offset_in_current_packet(struct bt_ctfser
*ctfser
,
177 uint64_t alignment_bits
)
180 uint64_t align_size_bits
;
182 BT_ASSERT_DBG(alignment_bits
> 0);
183 align_size_bits
= ALIGN(ctfser
->offset_in_cur_packet_bits
,
184 alignment_bits
) - ctfser
->offset_in_cur_packet_bits
;
186 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, align_size_bits
))) {
187 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
188 if (G_UNLIKELY(ret
)) {
193 _bt_ctfser_incr_offset(ctfser
, align_size_bits
);
200 int _bt_ctfser_write_byte_aligned_unsigned_int_no_align(
201 struct bt_ctfser
*ctfser
, uint64_t value
,
202 unsigned int size_bits
, int byte_order
)
206 /* Reverse byte order? */
207 bool rbo
= byte_order
!= BYTE_ORDER
;
209 BT_ASSERT_DBG(size_bits
% 8 == 0);
210 BT_ASSERT_DBG(_bt_ctfser_has_space_left(ctfser
, size_bits
));
215 uint8_t v
= (uint8_t) value
;
217 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
222 uint16_t v
= (uint16_t) value
;
225 v
= GUINT16_SWAP_LE_BE(v
);
228 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
233 uint32_t v
= (uint32_t) value
;
236 v
= GUINT32_SWAP_LE_BE(v
);
239 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
244 uint64_t v
= (uint64_t) value
;
247 v
= GUINT64_SWAP_LE_BE(v
);
250 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
257 _bt_ctfser_incr_offset(ctfser
, size_bits
);
262 int _bt_ctfser_write_byte_aligned_signed_int_no_align(
263 struct bt_ctfser
*ctfser
, int64_t value
,
264 unsigned int size_bits
, int byte_order
)
268 /* Reverse byte order? */
269 bool rbo
= byte_order
!= BYTE_ORDER
;
271 BT_ASSERT_DBG(size_bits
% 8 == 0);
272 BT_ASSERT_DBG(_bt_ctfser_has_space_left(ctfser
, size_bits
));
277 int8_t v
= (int8_t) value
;
279 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
284 int16_t v
= (int16_t) value
;
287 v
= GUINT16_SWAP_LE_BE(v
);
290 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
295 int32_t v
= (int32_t) value
;
298 v
= GUINT32_SWAP_LE_BE(v
);
301 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
306 int64_t v
= (int64_t) value
;
309 v
= GUINT64_SWAP_LE_BE(v
);
312 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
319 _bt_ctfser_incr_offset(ctfser
, size_bits
);
324 * Writes an unsigned integer known to have a size that is a multiple of
325 * 8 and an alignment that is >= 8 at the current offset within the
329 int bt_ctfser_write_byte_aligned_unsigned_int(struct bt_ctfser
*ctfser
,
330 uint64_t value
, unsigned int alignment_bits
,
331 unsigned int size_bits
, int byte_order
)
335 BT_ASSERT_DBG(alignment_bits
% 8 == 0);
336 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
337 if (G_UNLIKELY(ret
)) {
341 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
342 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
343 if (G_UNLIKELY(ret
)) {
348 ret
= _bt_ctfser_write_byte_aligned_unsigned_int_no_align(ctfser
, value
,
349 size_bits
, byte_order
);
350 if (G_UNLIKELY(ret
)) {
359 * Writes a signed integer known to have a size that is a multiple of 8
360 * and an alignment that is >= 8 at the current offset within the
364 int bt_ctfser_write_byte_aligned_signed_int(struct bt_ctfser
*ctfser
,
365 int64_t value
, unsigned int alignment_bits
,
366 unsigned int size_bits
, int byte_order
)
370 BT_ASSERT_DBG(alignment_bits
% 8 == 0);
371 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
372 if (G_UNLIKELY(ret
)) {
376 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
377 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
378 if (G_UNLIKELY(ret
)) {
383 ret
= _bt_ctfser_write_byte_aligned_signed_int_no_align(ctfser
, value
,
384 size_bits
, byte_order
);
385 if (G_UNLIKELY(ret
)) {
394 * Writes an unsigned integer at the current offset within the current
398 int bt_ctfser_write_unsigned_int(struct bt_ctfser
*ctfser
, uint64_t value
,
399 unsigned int alignment_bits
, unsigned int size_bits
,
404 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
405 if (G_UNLIKELY(ret
)) {
409 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
410 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
411 if (G_UNLIKELY(ret
)) {
416 if (alignment_bits
% 8 == 0 && size_bits
% 8 == 0) {
417 ret
= _bt_ctfser_write_byte_aligned_unsigned_int_no_align(
418 ctfser
, value
, size_bits
, byte_order
);
422 if (byte_order
== LITTLE_ENDIAN
) {
423 bt_bitfield_write_le(mmap_align_addr(ctfser
->base_mma
) +
424 ctfser
->mmap_base_offset
, uint8_t,
425 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
427 bt_bitfield_write_be(mmap_align_addr(ctfser
->base_mma
) +
428 ctfser
->mmap_base_offset
, uint8_t,
429 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
432 _bt_ctfser_incr_offset(ctfser
, size_bits
);
439 * Writes a signed integer at the current offset within the current
443 int bt_ctfser_write_signed_int(struct bt_ctfser
*ctfser
, int64_t value
,
444 unsigned int alignment_bits
, unsigned int size_bits
,
449 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
450 if (G_UNLIKELY(ret
)) {
454 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
455 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
456 if (G_UNLIKELY(ret
)) {
461 if (alignment_bits
% 8 == 0 && size_bits
% 8 == 0) {
462 ret
= _bt_ctfser_write_byte_aligned_signed_int_no_align(
463 ctfser
, value
, size_bits
, byte_order
);
467 if (byte_order
== LITTLE_ENDIAN
) {
468 bt_bitfield_write_le(mmap_align_addr(ctfser
->base_mma
) +
469 ctfser
->mmap_base_offset
, uint8_t,
470 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
472 bt_bitfield_write_be(mmap_align_addr(ctfser
->base_mma
) +
473 ctfser
->mmap_base_offset
, uint8_t,
474 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
477 _bt_ctfser_incr_offset(ctfser
, size_bits
);
484 * Writes a 32-bit floating point number at the current offset within
485 * the current packet.
488 int bt_ctfser_write_float32(struct bt_ctfser
*ctfser
, double value
,
489 unsigned int alignment_bits
, int byte_order
)
496 u32f
.f
= (float) value
;
497 return bt_ctfser_write_unsigned_int(ctfser
, (uint64_t) u32f
.u
,
498 alignment_bits
, 32, byte_order
);
502 * Writes a 64-bit floating point number at the current offset within
503 * the current packet.
506 int bt_ctfser_write_float64(struct bt_ctfser
*ctfser
, double value
,
507 unsigned int alignment_bits
, int byte_order
)
515 return bt_ctfser_write_unsigned_int(ctfser
, u64f
.u
, alignment_bits
,
520 * Writes a C string, including the terminating null character, at the
521 * current offset within the current packet.
524 int bt_ctfser_write_string(struct bt_ctfser
*ctfser
, const char *value
)
527 const char *at
= value
;
529 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, 8);
530 if (G_UNLIKELY(ret
)) {
535 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, 8))) {
536 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
537 if (G_UNLIKELY(ret
)) {
542 memcpy(_bt_ctfser_get_addr(ctfser
), at
, sizeof(*at
));
543 _bt_ctfser_incr_offset(ctfser
, 8);
545 if (G_UNLIKELY(*at
== '\0')) {
557 * Returns the current offset within the current packet (bits).
560 uint64_t bt_ctfser_get_offset_in_current_packet_bits(struct bt_ctfser
*ctfser
)
562 return ctfser
->offset_in_cur_packet_bits
;
566 * Sets the current offset within the current packet (bits).
569 void bt_ctfser_set_offset_in_current_packet_bits(struct bt_ctfser
*ctfser
,
570 uint64_t offset_bits
)
572 BT_ASSERT_DBG(offset_bits
<= _bt_ctfser_cur_packet_size_bits(ctfser
));
573 ctfser
->offset_in_cur_packet_bits
= offset_bits
;
577 const char *bt_ctfser_get_file_path(struct bt_ctfser
*ctfser
)
579 return ctfser
->path
->str
;
582 #endif /* BABELTRACE_CTFSER_INTERNAL_H */