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 "lib/lttng-ust/events.h"
14 #include <lttng/urcu/pointer.h>
15 #include "common/bitfield.h"
16 #include "common/align.h"
18 #include "context-internal.h"
19 #include "lttng-tracer.h"
20 #include "common/ringbuffer/frontend_types.h"
21 #include <urcu/tls-compat.h>
23 #define LTTNG_COMPACT_EVENT_BITS 5
24 #define LTTNG_COMPACT_TSC_BITS 27
27 * Keep the natural field alignment for _each field_ within this structure if
28 * you ever add/remove a field from this header. Packed attribute is not used
29 * because gcc generates poor code on at least powerpc and mips. Don't ever
30 * let gcc add padding between the structure elements.
33 struct packet_header
{
34 /* Trace packet header */
37 * contains endianness information.
39 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
41 uint64_t stream_instance_id
;
44 /* Stream packet context */
45 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
46 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
47 uint64_t content_size
; /* Size of data in subbuffer */
48 uint64_t packet_size
; /* Subbuffer size (include padding) */
49 uint64_t packet_seq_num
; /* Packet sequence number */
50 unsigned long events_discarded
; /*
51 * Events lost in this subbuffer since
52 * the beginning of the trace.
55 uint32_t cpu_id
; /* CPU id associated with stream */
56 uint8_t header_end
; /* End of header */
60 struct lttng_client_ctx
{
61 size_t packet_context_len
;
62 size_t event_context_len
;
63 struct lttng_ust_ctx
*chan_ctx
;
64 struct lttng_ust_ctx
*event_ctx
;
68 * Indexed by lib_ring_buffer_nesting_count().
70 typedef struct lttng_ust_ring_buffer_ctx_private private_ctx_stack_t
[LIB_RING_BUFFER_MAX_NESTING
];
71 static DEFINE_URCU_TLS(private_ctx_stack_t
, private_ctx_stack
);
74 * Force a read (imply TLS allocation for dlopen) of TLS variables.
76 void RING_BUFFER_MODE_TEMPLATE_ALLOC_TLS(void)
78 asm volatile ("" : : "m" (URCU_TLS(private_ctx_stack
)));
81 static inline uint64_t lib_ring_buffer_clock_read(
82 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)))
84 return trace_clock_read64();
88 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ust_ctx
*ctx
,
91 size_t orig_offset
= offset
;
95 offset
+= lttng_ust_ring_buffer_align(offset
, ctx
->largest_align
);
97 return offset
- orig_offset
;
101 void ctx_get_struct_size(struct lttng_ust_ring_buffer_ctx
*bufctx
,
102 struct lttng_ust_ctx
*ctx
, size_t *ctx_len
)
107 if (caa_likely(!ctx
)) {
111 for (i
= 0; i
< ctx
->nr_fields
; i
++)
112 offset
+= ctx
->fields
[i
].get_size(ctx
->fields
[i
].priv
, bufctx
->probe_ctx
, offset
);
117 void ctx_record(struct lttng_ust_ring_buffer_ctx
*bufctx
,
118 struct lttng_ust_channel_buffer
*chan
,
119 struct lttng_ust_ctx
*ctx
)
123 if (caa_likely(!ctx
))
125 lttng_ust_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
126 for (i
= 0; i
< ctx
->nr_fields
; i
++)
127 ctx
->fields
[i
].record(ctx
->fields
[i
].priv
, bufctx
->probe_ctx
, bufctx
, chan
);
131 * record_header_size - Calculate the header size and padding necessary.
132 * @config: ring buffer instance configuration
134 * @offset: offset in the write buffer
135 * @pre_header_padding: padding to add before the header (output)
136 * @ctx: reservation context
138 * Returns the event header size (including padding).
140 * The payload must itself determine its own alignment from the biggest type it
144 size_t record_header_size(
145 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
146 struct lttng_ust_ring_buffer_channel
*chan
,
148 size_t *pre_header_padding
,
149 struct lttng_ust_ring_buffer_ctx
*ctx
,
150 struct lttng_client_ctx
*client_ctx
)
152 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
153 size_t orig_offset
= offset
;
156 switch (lttng_chan
->priv
->header_type
) {
157 case 1: /* compact */
158 padding
= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
160 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
161 offset
+= sizeof(uint32_t); /* id and timestamp */
163 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
164 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
165 /* Align extended struct on largest member */
166 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
167 offset
+= sizeof(uint32_t); /* id */
168 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
169 offset
+= sizeof(uint64_t); /* timestamp */
173 padding
= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint16_t));
175 offset
+= sizeof(uint16_t);
176 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
177 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
178 offset
+= sizeof(uint32_t); /* timestamp */
180 /* Align extended struct on largest member */
181 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
182 offset
+= sizeof(uint32_t); /* id */
183 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
184 offset
+= sizeof(uint64_t); /* timestamp */
191 offset
+= ctx_get_aligned_size(offset
, client_ctx
->chan_ctx
,
192 client_ctx
->packet_context_len
);
193 offset
+= ctx_get_aligned_size(offset
, client_ctx
->event_ctx
,
194 client_ctx
->event_context_len
);
195 *pre_header_padding
= padding
;
196 return offset
- orig_offset
;
199 #include "common/ringbuffer/api.h"
200 #include "lttng-rb-clients.h"
203 void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config
*config
,
204 struct lttng_ust_ring_buffer_ctx
*ctx
,
205 struct lttng_client_ctx
*client_ctx
,
209 * lttng_write_event_header
211 * Writes the event header to the offset (already aligned on 32-bits).
213 * @config: ring buffer instance configuration
214 * @ctx: reservation context
215 * @event_id: event ID
218 void lttng_write_event_header(const struct lttng_ust_ring_buffer_config
*config
,
219 struct lttng_ust_ring_buffer_ctx
*ctx
,
220 struct lttng_client_ctx
*client_ctx
,
223 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
225 if (caa_unlikely(ctx
->priv
->rflags
))
228 switch (lttng_chan
->priv
->header_type
) {
229 case 1: /* compact */
231 uint32_t id_time
= 0;
233 bt_bitfield_write(&id_time
, uint32_t,
235 LTTNG_COMPACT_EVENT_BITS
,
237 bt_bitfield_write(&id_time
, uint32_t,
238 LTTNG_COMPACT_EVENT_BITS
,
239 LTTNG_COMPACT_TSC_BITS
,
241 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
246 uint32_t timestamp
= (uint32_t) ctx
->priv
->tsc
;
247 uint16_t id
= event_id
;
249 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
250 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
251 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
258 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
);
259 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
);
260 lttng_ust_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
265 lttng_write_event_header_slow(config
, ctx
, client_ctx
, event_id
);
269 void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config
*config
,
270 struct lttng_ust_ring_buffer_ctx
*ctx
,
271 struct lttng_client_ctx
*client_ctx
,
274 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
275 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
277 switch (lttng_chan
->priv
->header_type
) {
278 case 1: /* compact */
279 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
280 uint32_t id_time
= 0;
282 bt_bitfield_write(&id_time
, uint32_t,
284 LTTNG_COMPACT_EVENT_BITS
,
286 bt_bitfield_write(&id_time
, uint32_t,
287 LTTNG_COMPACT_EVENT_BITS
,
288 LTTNG_COMPACT_TSC_BITS
,
290 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
293 uint64_t timestamp
= ctx_private
->tsc
;
295 bt_bitfield_write(&id
, uint8_t,
297 LTTNG_COMPACT_EVENT_BITS
,
299 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
300 /* Align extended struct on largest member */
301 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
302 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
303 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
304 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
309 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
310 uint32_t timestamp
= (uint32_t) ctx_private
->tsc
;
311 uint16_t id
= event_id
;
313 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
314 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
315 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
318 uint64_t timestamp
= ctx_private
->tsc
;
320 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
321 /* Align extended struct on largest member */
322 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
323 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
324 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
325 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
332 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
);
333 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
);
334 lttng_ust_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
337 static const struct lttng_ust_ring_buffer_config client_config
;
339 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_ring_buffer_channel
*chan
)
341 return lib_ring_buffer_clock_read(chan
);
345 size_t client_record_header_size(const struct lttng_ust_ring_buffer_config
*config
,
346 struct lttng_ust_ring_buffer_channel
*chan
,
348 size_t *pre_header_padding
,
349 struct lttng_ust_ring_buffer_ctx
*ctx
,
352 return record_header_size(config
, chan
, offset
,
353 pre_header_padding
, ctx
, client_ctx
);
357 * client_packet_header_size - called on buffer-switch to a new sub-buffer
359 * Return header size without padding after the structure. Don't use packed
360 * structure because gcc generates inefficient code on some architectures
363 static size_t client_packet_header_size(void)
365 return offsetof(struct packet_header
, ctx
.header_end
);
368 static void client_buffer_begin(struct lttng_ust_ring_buffer
*buf
, uint64_t tsc
,
369 unsigned int subbuf_idx
,
370 struct lttng_ust_shm_handle
*handle
)
372 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
373 struct packet_header
*header
=
374 (struct packet_header
*)
375 lib_ring_buffer_offset_address(&buf
->backend
,
376 subbuf_idx
* chan
->backend
.subbuf_size
,
378 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
379 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
384 header
->magic
= CTF_MAGIC_NUMBER
;
385 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
386 header
->stream_id
= lttng_chan
->priv
->id
;
387 header
->stream_instance_id
= buf
->backend
.cpu
;
388 header
->ctx
.timestamp_begin
= tsc
;
389 header
->ctx
.timestamp_end
= 0;
390 header
->ctx
.content_size
= ~0ULL; /* for debugging */
391 header
->ctx
.packet_size
= ~0ULL;
392 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
393 header
->ctx
.events_discarded
= 0;
394 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
398 * offset is assumed to never be 0 here : never deliver a completely empty
399 * subbuffer. data_size is between 1 and subbuf_size.
401 static void client_buffer_end(struct lttng_ust_ring_buffer
*buf
, uint64_t tsc
,
402 unsigned int subbuf_idx
, unsigned long data_size
,
403 struct lttng_ust_shm_handle
*handle
)
405 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
406 struct packet_header
*header
=
407 (struct packet_header
*)
408 lib_ring_buffer_offset_address(&buf
->backend
,
409 subbuf_idx
* chan
->backend
.subbuf_size
,
411 unsigned long records_lost
= 0;
416 header
->ctx
.timestamp_end
= tsc
;
417 header
->ctx
.content_size
=
418 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
419 header
->ctx
.packet_size
=
420 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
422 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
423 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
424 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
425 header
->ctx
.events_discarded
= records_lost
;
428 static int client_buffer_create(
429 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
430 void *priv
__attribute__((unused
)),
431 int cpu
__attribute__((unused
)),
432 const char *name
__attribute__((unused
)),
433 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
438 static void client_buffer_finalize(
439 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
440 void *priv
__attribute__((unused
)),
441 int cpu
__attribute__((unused
)),
442 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
446 static void client_content_size_field(
447 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
448 size_t *offset
, size_t *length
)
450 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
451 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
454 static void client_packet_size_field(
455 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
456 size_t *offset
, size_t *length
)
458 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
459 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
462 static struct packet_header
*client_packet_header(struct lttng_ust_ring_buffer
*buf
,
463 struct lttng_ust_shm_handle
*handle
)
465 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
468 static int client_timestamp_begin(struct lttng_ust_ring_buffer
*buf
,
469 struct lttng_ust_ring_buffer_channel
*chan
,
470 uint64_t *timestamp_begin
)
472 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
473 struct packet_header
*header
;
475 header
= client_packet_header(buf
, handle
);
478 *timestamp_begin
= header
->ctx
.timestamp_begin
;
482 static int client_timestamp_end(struct lttng_ust_ring_buffer
*buf
,
483 struct lttng_ust_ring_buffer_channel
*chan
,
484 uint64_t *timestamp_end
)
486 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
487 struct packet_header
*header
;
489 header
= client_packet_header(buf
, handle
);
492 *timestamp_end
= header
->ctx
.timestamp_end
;
496 static int client_events_discarded(struct lttng_ust_ring_buffer
*buf
,
497 struct lttng_ust_ring_buffer_channel
*chan
,
498 uint64_t *events_discarded
)
500 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
501 struct packet_header
*header
;
503 header
= client_packet_header(buf
, handle
);
506 *events_discarded
= header
->ctx
.events_discarded
;
510 static int client_content_size(struct lttng_ust_ring_buffer
*buf
,
511 struct lttng_ust_ring_buffer_channel
*chan
,
512 uint64_t *content_size
)
514 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
515 struct packet_header
*header
;
517 header
= client_packet_header(buf
, handle
);
520 *content_size
= header
->ctx
.content_size
;
524 static int client_packet_size(struct lttng_ust_ring_buffer
*buf
,
525 struct lttng_ust_ring_buffer_channel
*chan
,
526 uint64_t *packet_size
)
528 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
529 struct packet_header
*header
;
531 header
= client_packet_header(buf
, handle
);
534 *packet_size
= header
->ctx
.packet_size
;
538 static int client_stream_id(struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
539 struct lttng_ust_ring_buffer_channel
*chan
,
542 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
544 *stream_id
= lttng_chan
->priv
->id
;
549 static int client_current_timestamp(
550 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
551 struct lttng_ust_ring_buffer_channel
*chan
,
554 *ts
= client_ring_buffer_clock_read(chan
);
559 static int client_sequence_number(struct lttng_ust_ring_buffer
*buf
,
560 struct lttng_ust_ring_buffer_channel
*chan
,
563 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
564 struct packet_header
*header
;
566 header
= client_packet_header(buf
, handle
);
569 *seq
= header
->ctx
.packet_seq_num
;
573 static int client_instance_id(struct lttng_ust_ring_buffer
*buf
,
574 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
577 *id
= buf
->backend
.cpu
;
583 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
585 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
586 .record_header_size
= client_record_header_size
,
587 .subbuffer_header_size
= client_packet_header_size
,
588 .buffer_begin
= client_buffer_begin
,
589 .buffer_end
= client_buffer_end
,
590 .buffer_create
= client_buffer_create
,
591 .buffer_finalize
= client_buffer_finalize
,
592 .content_size_field
= client_content_size_field
,
593 .packet_size_field
= client_packet_size_field
,
595 .timestamp_begin
= client_timestamp_begin
,
596 .timestamp_end
= client_timestamp_end
,
597 .events_discarded
= client_events_discarded
,
598 .content_size
= client_content_size
,
599 .packet_size
= client_packet_size
,
600 .stream_id
= client_stream_id
,
601 .current_timestamp
= client_current_timestamp
,
602 .sequence_number
= client_sequence_number
,
603 .instance_id
= client_instance_id
,
606 static const struct lttng_ust_ring_buffer_config client_config
= {
607 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
608 .cb
.record_header_size
= client_record_header_size
,
609 .cb
.subbuffer_header_size
= client_packet_header_size
,
610 .cb
.buffer_begin
= client_buffer_begin
,
611 .cb
.buffer_end
= client_buffer_end
,
612 .cb
.buffer_create
= client_buffer_create
,
613 .cb
.buffer_finalize
= client_buffer_finalize
,
614 .cb
.content_size_field
= client_content_size_field
,
615 .cb
.packet_size_field
= client_packet_size_field
,
617 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
618 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
619 .sync
= RING_BUFFER_SYNC_GLOBAL
,
620 .mode
= RING_BUFFER_MODE_TEMPLATE
,
621 .backend
= RING_BUFFER_PAGE
,
622 .output
= RING_BUFFER_MMAP
,
623 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
624 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
625 .wakeup
= LTTNG_CLIENT_WAKEUP
,
626 .client_type
= LTTNG_CLIENT_TYPE
,
628 .cb_ptr
= &client_cb
.parent
,
632 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
634 size_t subbuf_size
, size_t num_subbuf
,
635 unsigned int switch_timer_interval
,
636 unsigned int read_timer_interval
,
639 const int *stream_fds
, int nr_stream_fds
,
640 int64_t blocking_timeout
)
642 struct lttng_ust_abi_channel_config chan_priv_init
;
643 struct lttng_ust_shm_handle
*handle
;
644 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
646 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
649 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
650 lttng_chan_buf
->priv
->id
= chan_id
;
652 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
653 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
654 chan_priv_init
.id
= chan_id
;
656 handle
= channel_create(&client_config
, name
,
657 __alignof__(struct lttng_ust_abi_channel_config
),
658 sizeof(struct lttng_ust_abi_channel_config
),
660 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
661 switch_timer_interval
, read_timer_interval
,
662 stream_fds
, nr_stream_fds
, blocking_timeout
);
665 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
666 return lttng_chan_buf
;
669 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
674 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
676 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
677 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
681 int lttng_event_reserve(struct lttng_ust_ring_buffer_ctx
*ctx
)
683 struct lttng_ust_event_recorder
*event_recorder
= ctx
->client_priv
;
684 struct lttng_ust_channel_buffer
*lttng_chan
= event_recorder
->chan
;
685 struct lttng_client_ctx client_ctx
;
687 struct lttng_ust_ring_buffer_ctx_private
*private_ctx
;
690 event_id
= event_recorder
->priv
->id
;
691 client_ctx
.chan_ctx
= lttng_ust_rcu_dereference(lttng_chan
->priv
->ctx
);
692 client_ctx
.event_ctx
= lttng_ust_rcu_dereference(event_recorder
->priv
->ctx
);
693 /* Compute internal size of context structures. */
694 ctx_get_struct_size(ctx
, client_ctx
.chan_ctx
, &client_ctx
.packet_context_len
);
695 ctx_get_struct_size(ctx
, client_ctx
.event_ctx
, &client_ctx
.event_context_len
);
697 nesting
= lib_ring_buffer_nesting_inc(&client_config
);
701 private_ctx
= &URCU_TLS(private_ctx_stack
)[nesting
];
702 memset(private_ctx
, 0, sizeof(*private_ctx
));
703 private_ctx
->pub
= ctx
;
704 private_ctx
->chan
= lttng_chan
->priv
->rb_chan
;
706 ctx
->priv
= private_ctx
;
708 switch (lttng_chan
->priv
->header_type
) {
709 case 1: /* compact */
711 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
714 if (event_id
> 65534)
715 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
721 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, &client_ctx
);
722 if (caa_unlikely(ret
))
724 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
725 &private_ctx
->backend_pages
)) {
729 lttng_write_event_header(&client_config
, ctx
, &client_ctx
, event_id
);
732 lib_ring_buffer_nesting_dec(&client_config
);
737 void lttng_event_commit(struct lttng_ust_ring_buffer_ctx
*ctx
)
739 lib_ring_buffer_commit(&client_config
, ctx
);
740 lib_ring_buffer_nesting_dec(&client_config
);
744 void lttng_event_write(struct lttng_ust_ring_buffer_ctx
*ctx
,
745 const void *src
, size_t len
, size_t alignment
)
747 lttng_ust_ring_buffer_align_ctx(ctx
, alignment
);
748 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
752 void lttng_event_strcpy(struct lttng_ust_ring_buffer_ctx
*ctx
,
753 const char *src
, size_t len
)
755 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
759 void lttng_event_pstrcpy_pad(struct lttng_ust_ring_buffer_ctx
*ctx
,
760 const char *src
, size_t len
)
762 lib_ring_buffer_pstrcpy(&client_config
, ctx
, src
, len
, '\0');
766 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
768 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
770 return lib_ring_buffer_channel_is_finalized(rb_chan
);
774 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
776 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
778 return lib_ring_buffer_channel_is_disabled(rb_chan
);
782 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
784 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
785 struct lttng_ust_ring_buffer
*buf
;
788 for_each_channel_cpu(cpu
, rb_chan
) {
789 int shm_fd
, wait_fd
, wakeup_fd
;
790 uint64_t memory_map_size
;
792 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
793 cpu
, rb_chan
->handle
, &shm_fd
, &wait_fd
,
794 &wakeup_fd
, &memory_map_size
);
795 lib_ring_buffer_switch(&client_config
, buf
,
796 SWITCH_ACTIVE
, rb_chan
->handle
);
801 static struct lttng_transport lttng_relay_transport
= {
802 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
804 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
805 .priv
= LTTNG_UST_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
806 .pub
= <tng_relay_transport
.ops
,
807 .channel_create
= _channel_create
,
808 .channel_destroy
= lttng_channel_destroy
,
809 .packet_avail_size
= NULL
, /* Would be racy anyway */
810 .is_finalized
= lttng_is_finalized
,
811 .is_disabled
= lttng_is_disabled
,
812 .flush_buffer
= lttng_flush_buffer
,
814 .event_reserve
= lttng_event_reserve
,
815 .event_commit
= lttng_event_commit
,
816 .event_write
= lttng_event_write
,
817 .event_strcpy
= lttng_event_strcpy
,
818 .event_pstrcpy_pad
= lttng_event_pstrcpy_pad
,
820 .client_config
= &client_config
,
823 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
825 DBG("LTT : ltt ring buffer client \"%s\" init\n",
826 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
827 lttng_transport_register(<tng_relay_transport
);
830 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
832 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
833 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
834 lttng_transport_unregister(<tng_relay_transport
);