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 <lttng/urcu/pointer.h>
14 #include <urcu/tls-compat.h>
16 #include "common/events.h"
17 #include "common/bitfield.h"
18 #include "common/align.h"
19 #include "common/clock.h"
20 #include "common/ringbuffer/frontend_types.h"
22 #define LTTNG_COMPACT_EVENT_BITS 5
23 #define LTTNG_COMPACT_TSC_BITS 27
26 * Keep the natural field alignment for _each field_ within this structure if
27 * you ever add/remove a field from this header. Packed attribute is not used
28 * because gcc generates poor code on at least powerpc and mips. Don't ever
29 * let gcc add padding between the structure elements.
32 struct packet_header
{
33 /* Trace packet header */
36 * contains endianness information.
38 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
40 uint64_t stream_instance_id
;
43 /* Stream packet context */
44 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
45 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
46 uint64_t content_size
; /* Size of data in subbuffer */
47 uint64_t packet_size
; /* Subbuffer size (include padding) */
48 uint64_t packet_seq_num
; /* Packet sequence number */
49 unsigned long events_discarded
; /*
50 * Events lost in this subbuffer since
51 * the beginning of the trace.
54 uint32_t cpu_id
; /* CPU id associated with stream */
55 uint8_t header_end
; /* End of header */
59 struct lttng_client_ctx
{
60 size_t packet_context_len
;
61 size_t event_context_len
;
62 struct lttng_ust_ctx
*chan_ctx
;
63 struct lttng_ust_ctx
*event_ctx
;
67 * Indexed by lib_ring_buffer_nesting_count().
69 typedef struct lttng_ust_ring_buffer_ctx_private private_ctx_stack_t
[LIB_RING_BUFFER_MAX_NESTING
];
70 static DEFINE_URCU_TLS(private_ctx_stack_t
, private_ctx_stack
);
73 * Force a read (imply TLS allocation for dlopen) of TLS variables.
75 void RING_BUFFER_MODE_TEMPLATE_ALLOC_TLS(void)
77 asm volatile ("" : : "m" (URCU_TLS(private_ctx_stack
)));
80 static inline uint64_t lib_ring_buffer_clock_read(
81 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)))
83 return trace_clock_read64();
87 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ust_ctx
*ctx
,
90 size_t orig_offset
= offset
;
94 offset
+= lttng_ust_ring_buffer_align(offset
, ctx
->largest_align
);
96 return offset
- orig_offset
;
100 void ctx_get_struct_size(struct lttng_ust_ring_buffer_ctx
*bufctx
,
101 struct lttng_ust_ctx
*ctx
, size_t *ctx_len
)
106 if (caa_likely(!ctx
)) {
110 for (i
= 0; i
< ctx
->nr_fields
; i
++)
111 offset
+= ctx
->fields
[i
].get_size(ctx
->fields
[i
].priv
, bufctx
->probe_ctx
, offset
);
116 void ctx_record(struct lttng_ust_ring_buffer_ctx
*bufctx
,
117 struct lttng_ust_channel_buffer
*chan
,
118 struct lttng_ust_ctx
*ctx
)
122 if (caa_likely(!ctx
))
124 lttng_ust_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
125 for (i
= 0; i
< ctx
->nr_fields
; i
++)
126 ctx
->fields
[i
].record(ctx
->fields
[i
].priv
, bufctx
->probe_ctx
, bufctx
, chan
);
130 * record_header_size - Calculate the header size and padding necessary.
131 * @config: ring buffer instance configuration
133 * @offset: offset in the write buffer
134 * @pre_header_padding: padding to add before the header (output)
135 * @ctx: reservation context
137 * Returns the event header size (including padding).
139 * The payload must itself determine its own alignment from the biggest type it
143 size_t record_header_size(
144 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
145 struct lttng_ust_ring_buffer_channel
*chan
,
147 size_t *pre_header_padding
,
148 struct lttng_ust_ring_buffer_ctx
*ctx
,
149 struct lttng_client_ctx
*client_ctx
)
151 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
152 size_t orig_offset
= offset
;
155 switch (lttng_chan
->priv
->header_type
) {
156 case 1: /* compact */
157 padding
= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
159 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
160 offset
+= sizeof(uint32_t); /* id and timestamp */
162 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
163 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
164 /* Align extended struct on largest member */
165 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
166 offset
+= sizeof(uint32_t); /* id */
167 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
168 offset
+= sizeof(uint64_t); /* timestamp */
172 padding
= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint16_t));
174 offset
+= sizeof(uint16_t);
175 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
176 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
177 offset
+= sizeof(uint32_t); /* timestamp */
179 /* Align extended struct on largest member */
180 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
181 offset
+= sizeof(uint32_t); /* id */
182 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
183 offset
+= sizeof(uint64_t); /* timestamp */
190 offset
+= ctx_get_aligned_size(offset
, client_ctx
->chan_ctx
,
191 client_ctx
->packet_context_len
);
192 offset
+= ctx_get_aligned_size(offset
, client_ctx
->event_ctx
,
193 client_ctx
->event_context_len
);
194 *pre_header_padding
= padding
;
195 return offset
- orig_offset
;
198 #include "common/ringbuffer/api.h"
199 #include "common/ringbuffer-clients/clients.h"
202 void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config
*config
,
203 struct lttng_ust_ring_buffer_ctx
*ctx
,
204 struct lttng_client_ctx
*client_ctx
,
208 * lttng_write_event_header
210 * Writes the event header to the offset (already aligned on 32-bits).
212 * @config: ring buffer instance configuration
213 * @ctx: reservation context
214 * @event_id: event ID
217 void lttng_write_event_header(const struct lttng_ust_ring_buffer_config
*config
,
218 struct lttng_ust_ring_buffer_ctx
*ctx
,
219 struct lttng_client_ctx
*client_ctx
,
222 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
224 if (caa_unlikely(ctx
->priv
->rflags
))
227 switch (lttng_chan
->priv
->header_type
) {
228 case 1: /* compact */
230 uint32_t id_time
= 0;
232 bt_bitfield_write(&id_time
, uint32_t,
234 LTTNG_COMPACT_EVENT_BITS
,
236 bt_bitfield_write(&id_time
, uint32_t,
237 LTTNG_COMPACT_EVENT_BITS
,
238 LTTNG_COMPACT_TSC_BITS
,
240 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
245 uint32_t timestamp
= (uint32_t) ctx
->priv
->tsc
;
246 uint16_t id
= event_id
;
248 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
249 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
250 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
257 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
);
258 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
);
259 lttng_ust_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
264 lttng_write_event_header_slow(config
, ctx
, client_ctx
, event_id
);
268 void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config
*config
,
269 struct lttng_ust_ring_buffer_ctx
*ctx
,
270 struct lttng_client_ctx
*client_ctx
,
273 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
274 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
276 switch (lttng_chan
->priv
->header_type
) {
277 case 1: /* compact */
278 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
279 uint32_t id_time
= 0;
281 bt_bitfield_write(&id_time
, uint32_t,
283 LTTNG_COMPACT_EVENT_BITS
,
285 bt_bitfield_write(&id_time
, uint32_t,
286 LTTNG_COMPACT_EVENT_BITS
,
287 LTTNG_COMPACT_TSC_BITS
,
289 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
292 uint64_t timestamp
= ctx_private
->tsc
;
294 bt_bitfield_write(&id
, uint8_t,
296 LTTNG_COMPACT_EVENT_BITS
,
298 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
299 /* Align extended struct on largest member */
300 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
301 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
302 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
303 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
308 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
309 uint32_t timestamp
= (uint32_t) ctx_private
->tsc
;
310 uint16_t id
= event_id
;
312 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
313 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
314 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
317 uint64_t timestamp
= ctx_private
->tsc
;
319 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
320 /* Align extended struct on largest member */
321 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
322 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
323 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
324 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
331 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
);
332 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
);
333 lttng_ust_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
336 static const struct lttng_ust_ring_buffer_config client_config
;
338 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_ring_buffer_channel
*chan
)
340 return lib_ring_buffer_clock_read(chan
);
344 size_t client_record_header_size(const struct lttng_ust_ring_buffer_config
*config
,
345 struct lttng_ust_ring_buffer_channel
*chan
,
347 size_t *pre_header_padding
,
348 struct lttng_ust_ring_buffer_ctx
*ctx
,
351 return record_header_size(config
, chan
, offset
,
352 pre_header_padding
, ctx
, client_ctx
);
356 * client_packet_header_size - called on buffer-switch to a new sub-buffer
358 * Return header size without padding after the structure. Don't use packed
359 * structure because gcc generates inefficient code on some architectures
362 static size_t client_packet_header_size(void)
364 return offsetof(struct packet_header
, ctx
.header_end
);
367 static void client_buffer_begin(struct lttng_ust_ring_buffer
*buf
, uint64_t tsc
,
368 unsigned int subbuf_idx
,
369 struct lttng_ust_shm_handle
*handle
)
371 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
372 struct packet_header
*header
=
373 (struct packet_header
*)
374 lib_ring_buffer_offset_address(&buf
->backend
,
375 subbuf_idx
* chan
->backend
.subbuf_size
,
377 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
378 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
383 header
->magic
= CTF_MAGIC_NUMBER
;
384 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
385 header
->stream_id
= lttng_chan
->priv
->id
;
386 header
->stream_instance_id
= buf
->backend
.cpu
;
387 header
->ctx
.timestamp_begin
= tsc
;
388 header
->ctx
.timestamp_end
= 0;
389 header
->ctx
.content_size
= ~0ULL; /* for debugging */
390 header
->ctx
.packet_size
= ~0ULL;
391 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
392 header
->ctx
.events_discarded
= 0;
393 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
397 * offset is assumed to never be 0 here : never deliver a completely empty
398 * subbuffer. data_size is between 1 and subbuf_size.
400 static void client_buffer_end(struct lttng_ust_ring_buffer
*buf
, uint64_t tsc
,
401 unsigned int subbuf_idx
, unsigned long data_size
,
402 struct lttng_ust_shm_handle
*handle
)
404 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
405 struct packet_header
*header
=
406 (struct packet_header
*)
407 lib_ring_buffer_offset_address(&buf
->backend
,
408 subbuf_idx
* chan
->backend
.subbuf_size
,
410 unsigned long records_lost
= 0;
415 header
->ctx
.timestamp_end
= tsc
;
416 header
->ctx
.content_size
=
417 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
418 header
->ctx
.packet_size
=
419 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
421 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
422 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
423 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
424 header
->ctx
.events_discarded
= records_lost
;
427 static int client_buffer_create(
428 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
429 void *priv
__attribute__((unused
)),
430 int cpu
__attribute__((unused
)),
431 const char *name
__attribute__((unused
)),
432 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
437 static void client_buffer_finalize(
438 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
439 void *priv
__attribute__((unused
)),
440 int cpu
__attribute__((unused
)),
441 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
445 static void client_content_size_field(
446 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
447 size_t *offset
, size_t *length
)
449 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
450 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
453 static void client_packet_size_field(
454 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
455 size_t *offset
, size_t *length
)
457 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
458 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
461 static struct packet_header
*client_packet_header(struct lttng_ust_ring_buffer
*buf
,
462 struct lttng_ust_shm_handle
*handle
)
464 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
467 static int client_timestamp_begin(struct lttng_ust_ring_buffer
*buf
,
468 struct lttng_ust_ring_buffer_channel
*chan
,
469 uint64_t *timestamp_begin
)
471 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
472 struct packet_header
*header
;
474 header
= client_packet_header(buf
, handle
);
477 *timestamp_begin
= header
->ctx
.timestamp_begin
;
481 static int client_timestamp_end(struct lttng_ust_ring_buffer
*buf
,
482 struct lttng_ust_ring_buffer_channel
*chan
,
483 uint64_t *timestamp_end
)
485 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
486 struct packet_header
*header
;
488 header
= client_packet_header(buf
, handle
);
491 *timestamp_end
= header
->ctx
.timestamp_end
;
495 static int client_events_discarded(struct lttng_ust_ring_buffer
*buf
,
496 struct lttng_ust_ring_buffer_channel
*chan
,
497 uint64_t *events_discarded
)
499 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
500 struct packet_header
*header
;
502 header
= client_packet_header(buf
, handle
);
505 *events_discarded
= header
->ctx
.events_discarded
;
509 static int client_content_size(struct lttng_ust_ring_buffer
*buf
,
510 struct lttng_ust_ring_buffer_channel
*chan
,
511 uint64_t *content_size
)
513 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
514 struct packet_header
*header
;
516 header
= client_packet_header(buf
, handle
);
519 *content_size
= header
->ctx
.content_size
;
523 static int client_packet_size(struct lttng_ust_ring_buffer
*buf
,
524 struct lttng_ust_ring_buffer_channel
*chan
,
525 uint64_t *packet_size
)
527 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
528 struct packet_header
*header
;
530 header
= client_packet_header(buf
, handle
);
533 *packet_size
= header
->ctx
.packet_size
;
537 static int client_stream_id(struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
538 struct lttng_ust_ring_buffer_channel
*chan
,
541 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
543 *stream_id
= lttng_chan
->priv
->id
;
548 static int client_current_timestamp(
549 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
550 struct lttng_ust_ring_buffer_channel
*chan
,
553 *ts
= client_ring_buffer_clock_read(chan
);
558 static int client_sequence_number(struct lttng_ust_ring_buffer
*buf
,
559 struct lttng_ust_ring_buffer_channel
*chan
,
562 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
563 struct packet_header
*header
;
565 header
= client_packet_header(buf
, handle
);
568 *seq
= header
->ctx
.packet_seq_num
;
572 static int client_instance_id(struct lttng_ust_ring_buffer
*buf
,
573 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
576 *id
= buf
->backend
.cpu
;
582 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
584 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
585 .record_header_size
= client_record_header_size
,
586 .subbuffer_header_size
= client_packet_header_size
,
587 .buffer_begin
= client_buffer_begin
,
588 .buffer_end
= client_buffer_end
,
589 .buffer_create
= client_buffer_create
,
590 .buffer_finalize
= client_buffer_finalize
,
591 .content_size_field
= client_content_size_field
,
592 .packet_size_field
= client_packet_size_field
,
594 .timestamp_begin
= client_timestamp_begin
,
595 .timestamp_end
= client_timestamp_end
,
596 .events_discarded
= client_events_discarded
,
597 .content_size
= client_content_size
,
598 .packet_size
= client_packet_size
,
599 .stream_id
= client_stream_id
,
600 .current_timestamp
= client_current_timestamp
,
601 .sequence_number
= client_sequence_number
,
602 .instance_id
= client_instance_id
,
605 static const struct lttng_ust_ring_buffer_config client_config
= {
606 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
607 .cb
.record_header_size
= client_record_header_size
,
608 .cb
.subbuffer_header_size
= client_packet_header_size
,
609 .cb
.buffer_begin
= client_buffer_begin
,
610 .cb
.buffer_end
= client_buffer_end
,
611 .cb
.buffer_create
= client_buffer_create
,
612 .cb
.buffer_finalize
= client_buffer_finalize
,
613 .cb
.content_size_field
= client_content_size_field
,
614 .cb
.packet_size_field
= client_packet_size_field
,
616 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
617 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
618 .sync
= RING_BUFFER_SYNC_GLOBAL
,
619 .mode
= RING_BUFFER_MODE_TEMPLATE
,
620 .backend
= RING_BUFFER_PAGE
,
621 .output
= RING_BUFFER_MMAP
,
622 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
623 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
624 .wakeup
= LTTNG_CLIENT_WAKEUP
,
625 .client_type
= LTTNG_CLIENT_TYPE
,
627 .cb_ptr
= &client_cb
.parent
,
631 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
633 size_t subbuf_size
, size_t num_subbuf
,
634 unsigned int switch_timer_interval
,
635 unsigned int read_timer_interval
,
638 const int *stream_fds
, int nr_stream_fds
,
639 int64_t blocking_timeout
)
641 struct lttng_ust_abi_channel_config chan_priv_init
;
642 struct lttng_ust_shm_handle
*handle
;
643 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
645 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
648 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
649 lttng_chan_buf
->priv
->id
= chan_id
;
651 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
652 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
653 chan_priv_init
.id
= chan_id
;
655 handle
= channel_create(&client_config
, name
,
656 __alignof__(struct lttng_ust_abi_channel_config
),
657 sizeof(struct lttng_ust_abi_channel_config
),
659 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
660 switch_timer_interval
, read_timer_interval
,
661 stream_fds
, nr_stream_fds
, blocking_timeout
);
664 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
665 return lttng_chan_buf
;
668 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
673 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
675 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
676 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
680 int lttng_event_reserve(struct lttng_ust_ring_buffer_ctx
*ctx
)
682 struct lttng_ust_event_recorder
*event_recorder
= ctx
->client_priv
;
683 struct lttng_ust_channel_buffer
*lttng_chan
= event_recorder
->chan
;
684 struct lttng_client_ctx client_ctx
;
686 struct lttng_ust_ring_buffer_ctx_private
*private_ctx
;
689 event_id
= event_recorder
->priv
->id
;
690 client_ctx
.chan_ctx
= lttng_ust_rcu_dereference(lttng_chan
->priv
->ctx
);
691 client_ctx
.event_ctx
= lttng_ust_rcu_dereference(event_recorder
->priv
->ctx
);
692 /* Compute internal size of context structures. */
693 ctx_get_struct_size(ctx
, client_ctx
.chan_ctx
, &client_ctx
.packet_context_len
);
694 ctx_get_struct_size(ctx
, client_ctx
.event_ctx
, &client_ctx
.event_context_len
);
696 nesting
= lib_ring_buffer_nesting_inc(&client_config
);
700 private_ctx
= &URCU_TLS(private_ctx_stack
)[nesting
];
701 memset(private_ctx
, 0, sizeof(*private_ctx
));
702 private_ctx
->pub
= ctx
;
703 private_ctx
->chan
= lttng_chan
->priv
->rb_chan
;
705 ctx
->priv
= private_ctx
;
707 switch (lttng_chan
->priv
->header_type
) {
708 case 1: /* compact */
710 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
713 if (event_id
> 65534)
714 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
720 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, &client_ctx
);
721 if (caa_unlikely(ret
))
723 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
724 &private_ctx
->backend_pages
)) {
728 lttng_write_event_header(&client_config
, ctx
, &client_ctx
, event_id
);
731 lib_ring_buffer_nesting_dec(&client_config
);
736 void lttng_event_commit(struct lttng_ust_ring_buffer_ctx
*ctx
)
738 lib_ring_buffer_commit(&client_config
, ctx
);
739 lib_ring_buffer_nesting_dec(&client_config
);
743 void lttng_event_write(struct lttng_ust_ring_buffer_ctx
*ctx
,
744 const void *src
, size_t len
, size_t alignment
)
746 lttng_ust_ring_buffer_align_ctx(ctx
, alignment
);
747 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
751 void lttng_event_strcpy(struct lttng_ust_ring_buffer_ctx
*ctx
,
752 const char *src
, size_t len
)
754 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
758 void lttng_event_pstrcpy_pad(struct lttng_ust_ring_buffer_ctx
*ctx
,
759 const char *src
, size_t len
)
761 lib_ring_buffer_pstrcpy(&client_config
, ctx
, src
, len
, '\0');
765 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
767 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
769 return lib_ring_buffer_channel_is_finalized(rb_chan
);
773 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
775 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
777 return lib_ring_buffer_channel_is_disabled(rb_chan
);
781 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
783 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
784 struct lttng_ust_ring_buffer
*buf
;
787 for_each_channel_cpu(cpu
, rb_chan
) {
788 int shm_fd
, wait_fd
, wakeup_fd
;
789 uint64_t memory_map_size
;
791 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
792 cpu
, rb_chan
->handle
, &shm_fd
, &wait_fd
,
793 &wakeup_fd
, &memory_map_size
);
794 lib_ring_buffer_switch(&client_config
, buf
,
795 SWITCH_ACTIVE
, rb_chan
->handle
);
800 static struct lttng_transport lttng_relay_transport
= {
801 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
803 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
804 .priv
= LTTNG_UST_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
805 .pub
= <tng_relay_transport
.ops
,
806 .channel_create
= _channel_create
,
807 .channel_destroy
= lttng_channel_destroy
,
808 .packet_avail_size
= NULL
, /* Would be racy anyway */
809 .is_finalized
= lttng_is_finalized
,
810 .is_disabled
= lttng_is_disabled
,
811 .flush_buffer
= lttng_flush_buffer
,
813 .event_reserve
= lttng_event_reserve
,
814 .event_commit
= lttng_event_commit
,
815 .event_write
= lttng_event_write
,
816 .event_strcpy
= lttng_event_strcpy
,
817 .event_pstrcpy_pad
= lttng_event_pstrcpy_pad
,
819 .client_config
= &client_config
,
822 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
824 DBG("LTT : ltt ring buffer client \"%s\" init\n",
825 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
826 lttng_transport_register(<tng_relay_transport
);
829 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
831 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
832 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
833 lttng_transport_unregister(<tng_relay_transport
);