2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng lib ring buffer client template.
13 #include <ust-events-internal.h>
14 #include <lttng/urcu/pointer.h>
15 #include "ust-bitfield.h"
16 #include "ust-compat.h"
18 #include "context-internal.h"
19 #include "lttng-tracer.h"
20 #include "../libringbuffer/frontend_types.h"
21 #include <urcu/tls-compat.h>
23 #define LTTNG_COMPACT_EVENT_BITS 5
24 #define LTTNG_COMPACT_TSC_BITS 27
32 * Keep the natural field alignment for _each field_ within this structure if
33 * you ever add/remove a field from this header. Packed attribute is not used
34 * because gcc generates poor code on at least powerpc and mips. Don't ever
35 * let gcc add padding between the structure elements.
38 struct packet_header
{
39 /* Trace packet header */
42 * contains endianness information.
44 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
46 uint64_t stream_instance_id
;
49 /* Stream packet context */
50 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
51 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
52 uint64_t content_size
; /* Size of data in subbuffer */
53 uint64_t packet_size
; /* Subbuffer size (include padding) */
54 uint64_t packet_seq_num
; /* Packet sequence number */
55 unsigned long events_discarded
; /*
56 * Events lost in this subbuffer since
57 * the beginning of the trace.
60 uint32_t cpu_id
; /* CPU id associated with stream */
61 uint8_t header_end
; /* End of header */
65 struct lttng_client_ctx
{
66 size_t packet_context_len
;
67 size_t event_context_len
;
68 struct lttng_ust_ctx
*chan_ctx
;
69 struct lttng_ust_ctx
*event_ctx
;
73 * Indexed by lib_ring_buffer_nesting_count().
75 typedef struct lttng_ust_lib_ring_buffer_ctx_private private_ctx_stack_t
[LIB_RING_BUFFER_MAX_NESTING
];
76 static DEFINE_URCU_TLS(private_ctx_stack_t
, private_ctx_stack
);
79 * Force a read (imply TLS fixup for dlopen) of TLS variables.
82 void lttng_fixup_rb_client_tls(void)
84 asm volatile ("" : : "m" (URCU_TLS(private_ctx_stack
)));
87 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
89 return trace_clock_read64();
93 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ust_ctx
*ctx
,
96 size_t orig_offset
= offset
;
100 offset
+= lttng_ust_lib_ring_buffer_align(offset
, ctx
->largest_align
);
102 return offset
- orig_offset
;
106 void ctx_get_struct_size(struct lttng_ust_ctx
*ctx
, size_t *ctx_len
,
107 enum app_ctx_mode mode
)
112 if (caa_likely(!ctx
)) {
116 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
117 if (mode
== APP_CTX_ENABLED
) {
118 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
120 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
122 * Before UST 2.8, we cannot use the
123 * application context, because we
124 * cannot trust that the handler used
125 * for get_size is the same used for
126 * ctx_record, which would result in
127 * corrupted traces when tracing
128 * concurrently with application context
129 * register/unregister.
131 offset
+= lttng_ust_dummy_get_size(ctx
->fields
[i
], offset
);
133 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
141 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
142 struct lttng_ust_channel_buffer
*chan
,
143 struct lttng_ust_ctx
*ctx
,
144 enum app_ctx_mode mode
)
148 if (caa_likely(!ctx
))
150 lttng_ust_lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
151 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
152 if (mode
== APP_CTX_ENABLED
) {
153 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
155 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
157 * Before UST 2.8, we cannot use the
158 * application context, because we
159 * cannot trust that the handler used
160 * for get_size is the same used for
161 * ctx_record, which would result in
162 * corrupted traces when tracing
163 * concurrently with application context
164 * register/unregister.
166 lttng_ust_dummy_record(ctx
->fields
[i
], bufctx
, chan
);
168 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
175 * record_header_size - Calculate the header size and padding necessary.
176 * @config: ring buffer instance configuration
178 * @offset: offset in the write buffer
179 * @pre_header_padding: padding to add before the header (output)
180 * @ctx: reservation context
182 * Returns the event header size (including padding).
184 * The payload must itself determine its own alignment from the biggest type it
188 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
189 struct lttng_ust_lib_ring_buffer_channel
*chan
,
191 size_t *pre_header_padding
,
192 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
193 struct lttng_client_ctx
*client_ctx
)
195 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
196 size_t orig_offset
= offset
;
199 switch (lttng_chan
->priv
->header_type
) {
200 case 1: /* compact */
201 padding
= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
203 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
204 offset
+= sizeof(uint32_t); /* id and timestamp */
206 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
207 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
208 /* Align extended struct on largest member */
209 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
210 offset
+= sizeof(uint32_t); /* id */
211 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
212 offset
+= sizeof(uint64_t); /* timestamp */
216 padding
= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint16_t));
218 offset
+= sizeof(uint16_t);
219 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
220 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
221 offset
+= sizeof(uint32_t); /* timestamp */
223 /* Align extended struct on largest member */
224 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
225 offset
+= sizeof(uint32_t); /* id */
226 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
227 offset
+= sizeof(uint64_t); /* timestamp */
234 offset
+= ctx_get_aligned_size(offset
, client_ctx
->chan_ctx
,
235 client_ctx
->packet_context_len
);
236 offset
+= ctx_get_aligned_size(offset
, client_ctx
->event_ctx
,
237 client_ctx
->event_context_len
);
238 *pre_header_padding
= padding
;
239 return offset
- orig_offset
;
242 #include "../libringbuffer/api.h"
243 #include "lttng-rb-clients.h"
246 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
247 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
248 struct lttng_client_ctx
*client_ctx
,
252 * lttng_write_event_header
254 * Writes the event header to the offset (already aligned on 32-bits).
256 * @config: ring buffer instance configuration
257 * @ctx: reservation context
258 * @event_id: event ID
261 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
262 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
263 struct lttng_client_ctx
*client_ctx
,
266 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
268 if (caa_unlikely(ctx
->priv
->rflags
))
271 switch (lttng_chan
->priv
->header_type
) {
272 case 1: /* compact */
274 uint32_t id_time
= 0;
276 bt_bitfield_write(&id_time
, uint32_t,
278 LTTNG_COMPACT_EVENT_BITS
,
280 bt_bitfield_write(&id_time
, uint32_t,
281 LTTNG_COMPACT_EVENT_BITS
,
282 LTTNG_COMPACT_TSC_BITS
,
284 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
289 uint32_t timestamp
= (uint32_t) ctx
->priv
->tsc
;
290 uint16_t id
= event_id
;
292 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
293 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
294 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
301 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
, APP_CTX_ENABLED
);
302 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
, APP_CTX_ENABLED
);
303 lttng_ust_lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
308 lttng_write_event_header_slow(config
, ctx
, client_ctx
, event_id
);
312 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
313 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
314 struct lttng_client_ctx
*client_ctx
,
317 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
318 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
320 switch (lttng_chan
->priv
->header_type
) {
321 case 1: /* compact */
322 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
323 uint32_t id_time
= 0;
325 bt_bitfield_write(&id_time
, uint32_t,
327 LTTNG_COMPACT_EVENT_BITS
,
329 bt_bitfield_write(&id_time
, uint32_t,
330 LTTNG_COMPACT_EVENT_BITS
,
331 LTTNG_COMPACT_TSC_BITS
,
333 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
336 uint64_t timestamp
= ctx_private
->tsc
;
338 bt_bitfield_write(&id
, uint8_t,
340 LTTNG_COMPACT_EVENT_BITS
,
342 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
343 /* Align extended struct on largest member */
344 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
345 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
346 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
347 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
352 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
353 uint32_t timestamp
= (uint32_t) ctx_private
->tsc
;
354 uint16_t id
= event_id
;
356 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
357 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
358 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
361 uint64_t timestamp
= ctx_private
->tsc
;
363 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
364 /* Align extended struct on largest member */
365 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
366 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
367 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
368 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
375 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
, APP_CTX_ENABLED
);
376 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
, APP_CTX_ENABLED
);
377 lttng_ust_lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
380 static const struct lttng_ust_lib_ring_buffer_config client_config
;
382 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
384 return lib_ring_buffer_clock_read(chan
);
388 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
389 struct lttng_ust_lib_ring_buffer_channel
*chan
,
391 size_t *pre_header_padding
,
392 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
395 return record_header_size(config
, chan
, offset
,
396 pre_header_padding
, ctx
, client_ctx
);
400 * client_packet_header_size - called on buffer-switch to a new sub-buffer
402 * Return header size without padding after the structure. Don't use packed
403 * structure because gcc generates inefficient code on some architectures
406 static size_t client_packet_header_size(void)
408 return offsetof(struct packet_header
, ctx
.header_end
);
411 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
412 unsigned int subbuf_idx
,
413 struct lttng_ust_shm_handle
*handle
)
415 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
416 struct packet_header
*header
=
417 (struct packet_header
*)
418 lib_ring_buffer_offset_address(&buf
->backend
,
419 subbuf_idx
* chan
->backend
.subbuf_size
,
421 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
422 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
427 header
->magic
= CTF_MAGIC_NUMBER
;
428 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
429 header
->stream_id
= lttng_chan
->priv
->id
;
430 header
->stream_instance_id
= buf
->backend
.cpu
;
431 header
->ctx
.timestamp_begin
= tsc
;
432 header
->ctx
.timestamp_end
= 0;
433 header
->ctx
.content_size
= ~0ULL; /* for debugging */
434 header
->ctx
.packet_size
= ~0ULL;
435 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
436 header
->ctx
.events_discarded
= 0;
437 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
441 * offset is assumed to never be 0 here : never deliver a completely empty
442 * subbuffer. data_size is between 1 and subbuf_size.
444 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
445 unsigned int subbuf_idx
, unsigned long data_size
,
446 struct lttng_ust_shm_handle
*handle
)
448 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
449 struct packet_header
*header
=
450 (struct packet_header
*)
451 lib_ring_buffer_offset_address(&buf
->backend
,
452 subbuf_idx
* chan
->backend
.subbuf_size
,
454 unsigned long records_lost
= 0;
459 header
->ctx
.timestamp_end
= tsc
;
460 header
->ctx
.content_size
=
461 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
462 header
->ctx
.packet_size
=
463 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
465 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
466 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
467 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
468 header
->ctx
.events_discarded
= records_lost
;
471 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
472 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
477 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
481 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
482 size_t *offset
, size_t *length
)
484 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
485 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
488 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
489 size_t *offset
, size_t *length
)
491 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
492 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
495 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
496 struct lttng_ust_shm_handle
*handle
)
498 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
501 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
502 struct lttng_ust_lib_ring_buffer_channel
*chan
,
503 uint64_t *timestamp_begin
)
505 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
506 struct packet_header
*header
;
508 header
= client_packet_header(buf
, handle
);
511 *timestamp_begin
= header
->ctx
.timestamp_begin
;
515 static int client_timestamp_end(struct lttng_ust_lib_ring_buffer
*buf
,
516 struct lttng_ust_lib_ring_buffer_channel
*chan
,
517 uint64_t *timestamp_end
)
519 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
520 struct packet_header
*header
;
522 header
= client_packet_header(buf
, handle
);
525 *timestamp_end
= header
->ctx
.timestamp_end
;
529 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
530 struct lttng_ust_lib_ring_buffer_channel
*chan
,
531 uint64_t *events_discarded
)
533 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
534 struct packet_header
*header
;
536 header
= client_packet_header(buf
, handle
);
539 *events_discarded
= header
->ctx
.events_discarded
;
543 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
544 struct lttng_ust_lib_ring_buffer_channel
*chan
,
545 uint64_t *content_size
)
547 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
548 struct packet_header
*header
;
550 header
= client_packet_header(buf
, handle
);
553 *content_size
= header
->ctx
.content_size
;
557 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
558 struct lttng_ust_lib_ring_buffer_channel
*chan
,
559 uint64_t *packet_size
)
561 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
562 struct packet_header
*header
;
564 header
= client_packet_header(buf
, handle
);
567 *packet_size
= header
->ctx
.packet_size
;
571 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
572 struct lttng_ust_lib_ring_buffer_channel
*chan
,
575 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
577 *stream_id
= lttng_chan
->priv
->id
;
582 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
583 struct lttng_ust_lib_ring_buffer_channel
*chan
,
586 *ts
= client_ring_buffer_clock_read(chan
);
591 static int client_sequence_number(struct lttng_ust_lib_ring_buffer
*buf
,
592 struct lttng_ust_lib_ring_buffer_channel
*chan
,
595 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
596 struct packet_header
*header
;
598 header
= client_packet_header(buf
, handle
);
601 *seq
= header
->ctx
.packet_seq_num
;
605 static int client_instance_id(struct lttng_ust_lib_ring_buffer
*buf
,
606 struct lttng_ust_lib_ring_buffer_channel
*chan
,
609 *id
= buf
->backend
.cpu
;
615 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
617 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
618 .record_header_size
= client_record_header_size
,
619 .subbuffer_header_size
= client_packet_header_size
,
620 .buffer_begin
= client_buffer_begin
,
621 .buffer_end
= client_buffer_end
,
622 .buffer_create
= client_buffer_create
,
623 .buffer_finalize
= client_buffer_finalize
,
624 .content_size_field
= client_content_size_field
,
625 .packet_size_field
= client_packet_size_field
,
627 .timestamp_begin
= client_timestamp_begin
,
628 .timestamp_end
= client_timestamp_end
,
629 .events_discarded
= client_events_discarded
,
630 .content_size
= client_content_size
,
631 .packet_size
= client_packet_size
,
632 .stream_id
= client_stream_id
,
633 .current_timestamp
= client_current_timestamp
,
634 .sequence_number
= client_sequence_number
,
635 .instance_id
= client_instance_id
,
638 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
639 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
640 .cb
.record_header_size
= client_record_header_size
,
641 .cb
.subbuffer_header_size
= client_packet_header_size
,
642 .cb
.buffer_begin
= client_buffer_begin
,
643 .cb
.buffer_end
= client_buffer_end
,
644 .cb
.buffer_create
= client_buffer_create
,
645 .cb
.buffer_finalize
= client_buffer_finalize
,
646 .cb
.content_size_field
= client_content_size_field
,
647 .cb
.packet_size_field
= client_packet_size_field
,
649 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
650 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
651 .sync
= RING_BUFFER_SYNC_GLOBAL
,
652 .mode
= RING_BUFFER_MODE_TEMPLATE
,
653 .backend
= RING_BUFFER_PAGE
,
654 .output
= RING_BUFFER_MMAP
,
655 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
656 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
657 .wakeup
= LTTNG_CLIENT_WAKEUP
,
658 .client_type
= LTTNG_CLIENT_TYPE
,
660 .cb_ptr
= &client_cb
.parent
,
664 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
666 size_t subbuf_size
, size_t num_subbuf
,
667 unsigned int switch_timer_interval
,
668 unsigned int read_timer_interval
,
671 const int *stream_fds
, int nr_stream_fds
,
672 int64_t blocking_timeout
)
674 struct lttng_ust_abi_channel_config chan_priv_init
;
675 struct lttng_ust_shm_handle
*handle
;
676 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
678 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
681 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
682 lttng_chan_buf
->priv
->id
= chan_id
;
684 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
685 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
686 chan_priv_init
.id
= chan_id
;
688 handle
= channel_create(&client_config
, name
,
689 __alignof__(struct lttng_ust_abi_channel_config
),
690 sizeof(struct lttng_ust_abi_channel_config
),
692 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
693 switch_timer_interval
, read_timer_interval
,
694 stream_fds
, nr_stream_fds
, blocking_timeout
);
697 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
698 return lttng_chan_buf
;
701 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
706 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
708 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
709 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
713 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
715 struct lttng_ust_event_recorder
*event_recorder
= ctx
->client_priv
;
716 struct lttng_ust_channel_buffer
*lttng_chan
= event_recorder
->chan
;
717 struct lttng_client_ctx client_ctx
;
719 struct lttng_ust_lib_ring_buffer_ctx_private
*private_ctx
;
722 event_id
= event_recorder
->priv
->id
;
723 client_ctx
.chan_ctx
= lttng_ust_rcu_dereference(lttng_chan
->priv
->ctx
);
724 client_ctx
.event_ctx
= lttng_ust_rcu_dereference(event_recorder
->priv
->ctx
);
725 /* Compute internal size of context structures. */
726 ctx_get_struct_size(client_ctx
.chan_ctx
, &client_ctx
.packet_context_len
,
728 ctx_get_struct_size(client_ctx
.event_ctx
, &client_ctx
.event_context_len
,
731 nesting
= lib_ring_buffer_nesting_inc(&client_config
);
735 private_ctx
= &private_ctx_stack
[nesting
];
736 memset(private_ctx
, 0, sizeof(*private_ctx
));
737 private_ctx
->pub
= ctx
;
738 private_ctx
->chan
= lttng_chan
->priv
->rb_chan
;
740 ctx
->priv
= private_ctx
;
742 switch (lttng_chan
->priv
->header_type
) {
743 case 1: /* compact */
745 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
748 if (event_id
> 65534)
749 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
755 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, &client_ctx
);
756 if (caa_unlikely(ret
))
758 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
759 &private_ctx
->backend_pages
)) {
763 lttng_write_event_header(&client_config
, ctx
, &client_ctx
, event_id
);
766 lib_ring_buffer_nesting_dec(&client_config
);
771 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
773 lib_ring_buffer_commit(&client_config
, ctx
);
774 lib_ring_buffer_nesting_dec(&client_config
);
778 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
779 const void *src
, size_t len
, size_t alignment
)
781 lttng_ust_lib_ring_buffer_align_ctx(ctx
, alignment
);
782 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
786 void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
787 const char *src
, size_t len
)
789 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
793 void lttng_event_pstrcpy_pad(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
794 const char *src
, size_t len
)
796 lib_ring_buffer_pstrcpy(&client_config
, ctx
, src
, len
, '\0');
800 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
802 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
804 return lib_ring_buffer_channel_is_finalized(rb_chan
);
808 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
810 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
812 return lib_ring_buffer_channel_is_disabled(rb_chan
);
816 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
818 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
819 struct lttng_ust_lib_ring_buffer
*buf
;
822 for_each_channel_cpu(cpu
, rb_chan
) {
823 int shm_fd
, wait_fd
, wakeup_fd
;
824 uint64_t memory_map_size
;
826 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
827 cpu
, rb_chan
->handle
, &shm_fd
, &wait_fd
,
828 &wakeup_fd
, &memory_map_size
);
829 lib_ring_buffer_switch(&client_config
, buf
,
830 SWITCH_ACTIVE
, rb_chan
->handle
);
835 static struct lttng_transport lttng_relay_transport
= {
836 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
838 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
839 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
840 .pub
= <tng_relay_transport
.ops
,
841 .channel_create
= _channel_create
,
842 .channel_destroy
= lttng_channel_destroy
,
843 .packet_avail_size
= NULL
, /* Would be racy anyway */
844 .is_finalized
= lttng_is_finalized
,
845 .is_disabled
= lttng_is_disabled
,
846 .flush_buffer
= lttng_flush_buffer
,
848 .event_reserve
= lttng_event_reserve
,
849 .event_commit
= lttng_event_commit
,
850 .event_write
= lttng_event_write
,
851 .event_strcpy
= lttng_event_strcpy
,
852 .event_pstrcpy_pad
= lttng_event_pstrcpy_pad
,
854 .client_config
= &client_config
,
857 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
859 DBG("LTT : ltt ring buffer client \"%s\" init\n",
860 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
861 lttng_fixup_rb_client_tls();
862 lttng_transport_register(<tng_relay_transport
);
865 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
867 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
868 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
869 lttng_transport_unregister(<tng_relay_transport
);