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 "common/macros.h"
45 #include "compat/bitfield.h"
49 /* Stream file's descriptor */
52 /* Offset (bytes) of memory map (current packet) in the stream file */
55 /* Offset (bytes) of packet's first byte in the memory map */
56 off_t mmap_base_offset
;
58 /* Current offset (bits) within current packet */
59 uint64_t offset_in_cur_packet_bits
;
61 /* Current packet size (bytes) */
62 uint64_t cur_packet_size_bytes
;
64 /* Previous packet size (bytes) */
65 uint64_t prev_packet_size_bytes
;
67 /* Current stream size (bytes) */
68 uint64_t stream_size_bytes
;
70 /* Memory map base address */
71 struct mmap_align
*base_mma
;
73 /* Stream file's path (for debugging) */
76 /* Serializer's log level */
81 * Initializes a CTF serializer.
83 * This function opens the file `path` for writing.
86 int bt_ctfser_init(struct bt_ctfser
*ctfser
, const char *path
,
90 * Finalizes a CTF serializer.
92 * This function truncates the stream file so that there's no extra
93 * padding after the last packet, and then closes the file.
96 int bt_ctfser_fini(struct bt_ctfser
*ctfser
);
101 * All the next writing functions are performed within this new packet.
104 int bt_ctfser_open_packet(struct bt_ctfser
*ctfser
);
107 * Closes the current packet, making its size `packet_size_bytes`.
110 void bt_ctfser_close_current_packet(struct bt_ctfser
*ctfser
,
111 uint64_t packet_size_bytes
);
114 int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser
*ctfser
);
117 uint64_t _bt_ctfser_cur_packet_size_bits(struct bt_ctfser
*ctfser
)
119 return ctfser
->cur_packet_size_bytes
* 8;
123 uint64_t _bt_ctfser_prev_packet_size_bits(struct bt_ctfser
*ctfser
)
125 return ctfser
->prev_packet_size_bytes
* 8;
129 uint64_t _bt_ctfser_offset_bytes(struct bt_ctfser
*ctfser
)
131 return ctfser
->offset_in_cur_packet_bits
/ 8;
135 uint8_t *_bt_ctfser_get_addr(struct bt_ctfser
*ctfser
)
137 /* Only makes sense to get the address after aligning on byte */
138 BT_ASSERT(ctfser
->offset_in_cur_packet_bits
% 8 == 0);
139 return ((uint8_t *) mmap_align_addr(ctfser
->base_mma
)) +
140 ctfser
->mmap_base_offset
+ _bt_ctfser_offset_bytes(ctfser
);
144 bool _bt_ctfser_has_space_left(struct bt_ctfser
*ctfser
, uint64_t size_bits
)
146 bool has_space_left
= true;
148 if (G_UNLIKELY((ctfser
->offset_in_cur_packet_bits
+ size_bits
>
149 _bt_ctfser_cur_packet_size_bits(ctfser
)))) {
150 has_space_left
= false;
154 if (G_UNLIKELY(size_bits
> UINT64_MAX
- ctfser
->offset_in_cur_packet_bits
)) {
155 has_space_left
= false;
160 return has_space_left
;
164 void _bt_ctfser_incr_offset(struct bt_ctfser
*ctfser
, uint64_t size_bits
)
166 BT_ASSERT(_bt_ctfser_has_space_left(ctfser
, size_bits
));
167 ctfser
->offset_in_cur_packet_bits
+= size_bits
;
171 * Aligns the current offset within the current packet to
172 * `alignment_bits` bits (power of two, > 0).
175 int bt_ctfser_align_offset_in_current_packet(struct bt_ctfser
*ctfser
,
176 uint64_t alignment_bits
)
179 uint64_t align_size_bits
;
181 BT_ASSERT(alignment_bits
> 0);
182 align_size_bits
= ALIGN(ctfser
->offset_in_cur_packet_bits
,
183 alignment_bits
) - ctfser
->offset_in_cur_packet_bits
;
185 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, align_size_bits
))) {
186 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
187 if (G_UNLIKELY(ret
)) {
192 _bt_ctfser_incr_offset(ctfser
, align_size_bits
);
199 int _bt_ctfser_write_byte_aligned_unsigned_int_no_align(
200 struct bt_ctfser
*ctfser
, uint64_t value
,
201 unsigned int size_bits
, int byte_order
)
205 /* Reverse byte order? */
206 bool rbo
= byte_order
!= BYTE_ORDER
;
208 BT_ASSERT(size_bits
% 8 == 0);
209 BT_ASSERT(_bt_ctfser_has_space_left(ctfser
, size_bits
));
214 uint8_t v
= (uint8_t) value
;
216 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
221 uint16_t v
= (uint16_t) value
;
224 v
= GUINT16_SWAP_LE_BE(v
);
227 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
232 uint32_t v
= (uint32_t) value
;
235 v
= GUINT32_SWAP_LE_BE(v
);
238 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
243 uint64_t v
= (uint64_t) value
;
246 v
= GUINT64_SWAP_LE_BE(v
);
249 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
256 _bt_ctfser_incr_offset(ctfser
, size_bits
);
261 int _bt_ctfser_write_byte_aligned_signed_int_no_align(
262 struct bt_ctfser
*ctfser
, int64_t value
,
263 unsigned int size_bits
, int byte_order
)
267 /* Reverse byte order? */
268 bool rbo
= byte_order
!= BYTE_ORDER
;
270 BT_ASSERT(size_bits
% 8 == 0);
271 BT_ASSERT(_bt_ctfser_has_space_left(ctfser
, size_bits
));
276 int8_t v
= (int8_t) value
;
278 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
283 int16_t v
= (int16_t) value
;
286 v
= GUINT16_SWAP_LE_BE(v
);
289 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
294 int32_t v
= (int32_t) value
;
297 v
= GUINT32_SWAP_LE_BE(v
);
300 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
305 int64_t v
= (int64_t) value
;
308 v
= GUINT64_SWAP_LE_BE(v
);
311 memcpy(_bt_ctfser_get_addr(ctfser
), &v
, sizeof(v
));
318 _bt_ctfser_incr_offset(ctfser
, size_bits
);
323 * Writes an unsigned integer known to have a size that is a multiple of
324 * 8 and an alignment that is >= 8 at the current offset within the
328 int bt_ctfser_write_byte_aligned_unsigned_int(struct bt_ctfser
*ctfser
,
329 uint64_t value
, unsigned int alignment_bits
,
330 unsigned int size_bits
, int byte_order
)
334 BT_ASSERT(alignment_bits
% 8 == 0);
335 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
336 if (G_UNLIKELY(ret
)) {
340 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
341 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
342 if (G_UNLIKELY(ret
)) {
347 ret
= _bt_ctfser_write_byte_aligned_unsigned_int_no_align(ctfser
, value
,
348 size_bits
, byte_order
);
349 if (G_UNLIKELY(ret
)) {
358 * Writes a signed integer known to have a size that is a multiple of 8
359 * and an alignment that is >= 8 at the current offset within the
363 int bt_ctfser_write_byte_aligned_signed_int(struct bt_ctfser
*ctfser
,
364 int64_t value
, unsigned int alignment_bits
,
365 unsigned int size_bits
, int byte_order
)
369 BT_ASSERT(alignment_bits
% 8 == 0);
370 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
371 if (G_UNLIKELY(ret
)) {
375 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
376 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
377 if (G_UNLIKELY(ret
)) {
382 ret
= _bt_ctfser_write_byte_aligned_signed_int_no_align(ctfser
, value
,
383 size_bits
, byte_order
);
384 if (G_UNLIKELY(ret
)) {
393 * Writes an unsigned integer at the current offset within the current
397 int bt_ctfser_write_unsigned_int(struct bt_ctfser
*ctfser
, uint64_t value
,
398 unsigned int alignment_bits
, unsigned int size_bits
,
403 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
404 if (G_UNLIKELY(ret
)) {
408 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
409 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
410 if (G_UNLIKELY(ret
)) {
415 if (alignment_bits
% 8 == 0 && size_bits
% 8 == 0) {
416 ret
= _bt_ctfser_write_byte_aligned_unsigned_int_no_align(
417 ctfser
, value
, size_bits
, byte_order
);
421 if (byte_order
== LITTLE_ENDIAN
) {
422 bt_bitfield_write_le(mmap_align_addr(ctfser
->base_mma
) +
423 ctfser
->mmap_base_offset
, uint8_t,
424 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
426 bt_bitfield_write_be(mmap_align_addr(ctfser
->base_mma
) +
427 ctfser
->mmap_base_offset
, uint8_t,
428 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
431 _bt_ctfser_incr_offset(ctfser
, size_bits
);
438 * Writes a signed integer at the current offset within the current
442 int bt_ctfser_write_signed_int(struct bt_ctfser
*ctfser
, int64_t value
,
443 unsigned int alignment_bits
, unsigned int size_bits
,
448 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, alignment_bits
);
449 if (G_UNLIKELY(ret
)) {
453 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, size_bits
))) {
454 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
455 if (G_UNLIKELY(ret
)) {
460 if (alignment_bits
% 8 == 0 && size_bits
% 8 == 0) {
461 ret
= _bt_ctfser_write_byte_aligned_signed_int_no_align(
462 ctfser
, value
, size_bits
, byte_order
);
466 if (byte_order
== LITTLE_ENDIAN
) {
467 bt_bitfield_write_le(mmap_align_addr(ctfser
->base_mma
) +
468 ctfser
->mmap_base_offset
, uint8_t,
469 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
471 bt_bitfield_write_be(mmap_align_addr(ctfser
->base_mma
) +
472 ctfser
->mmap_base_offset
, uint8_t,
473 ctfser
->offset_in_cur_packet_bits
, size_bits
, value
);
476 _bt_ctfser_incr_offset(ctfser
, size_bits
);
483 * Writes a 32-bit floating point number at the current offset within
484 * the current packet.
487 int bt_ctfser_write_float32(struct bt_ctfser
*ctfser
, double value
,
488 unsigned int alignment_bits
, int byte_order
)
495 u32f
.f
= (float) value
;
496 return bt_ctfser_write_unsigned_int(ctfser
, (uint64_t) u32f
.u
,
497 alignment_bits
, 32, byte_order
);
501 * Writes a 64-bit floating point number at the current offset within
502 * the current packet.
505 int bt_ctfser_write_float64(struct bt_ctfser
*ctfser
, double value
,
506 unsigned int alignment_bits
, int byte_order
)
514 return bt_ctfser_write_unsigned_int(ctfser
, u64f
.u
, alignment_bits
,
519 * Writes a C string, including the terminating null character, at the
520 * current offset within the current packet.
523 int bt_ctfser_write_string(struct bt_ctfser
*ctfser
, const char *value
)
526 const char *at
= value
;
528 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
, 8);
529 if (G_UNLIKELY(ret
)) {
534 if (G_UNLIKELY(!_bt_ctfser_has_space_left(ctfser
, 8))) {
535 ret
= _bt_ctfser_increase_cur_packet_size(ctfser
);
536 if (G_UNLIKELY(ret
)) {
541 memcpy(_bt_ctfser_get_addr(ctfser
), at
, sizeof(*at
));
542 _bt_ctfser_incr_offset(ctfser
, 8);
544 if (G_UNLIKELY(*at
== '\0')) {
556 * Returns the current offset within the current packet (bits).
559 uint64_t bt_ctfser_get_offset_in_current_packet_bits(struct bt_ctfser
*ctfser
)
561 return ctfser
->offset_in_cur_packet_bits
;
565 * Sets the current offset within the current packet (bits).
568 void bt_ctfser_set_offset_in_current_packet_bits(struct bt_ctfser
*ctfser
,
569 uint64_t offset_bits
)
571 BT_ASSERT(offset_bits
<= _bt_ctfser_cur_packet_size_bits(ctfser
));
572 ctfser
->offset_in_cur_packet_bits
= offset_bits
;
576 const char *bt_ctfser_get_file_path(struct bt_ctfser
*ctfser
)
578 return ctfser
->path
->str
;
581 #endif /* BABELTRACE_CTFSER_INTERNAL_H */