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_TIMESTAMP_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
;
66 * Indexed by lib_ring_buffer_nesting_count().
68 typedef struct lttng_ust_ring_buffer_ctx_private private_ctx_stack_t
[LIB_RING_BUFFER_MAX_NESTING
];
69 static DEFINE_URCU_TLS(private_ctx_stack_t
, private_ctx_stack
);
72 * Force a read (imply TLS allocation for dlopen) of TLS variables.
74 void RING_BUFFER_MODE_TEMPLATE_ALLOC_TLS(void)
76 __asm__
__volatile__ ("" : : "m" (URCU_TLS(private_ctx_stack
)));
79 static inline uint64_t lib_ring_buffer_clock_read(
80 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)))
82 return trace_clock_read64();
86 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ust_ctx
*ctx
,
89 size_t orig_offset
= offset
;
93 offset
+= lttng_ust_ring_buffer_align(offset
, ctx
->largest_align
);
95 return offset
- orig_offset
;
99 void ctx_get_struct_size(struct lttng_ust_ring_buffer_ctx
*bufctx
,
100 struct lttng_ust_ctx
*ctx
, size_t *ctx_len
)
105 if (caa_likely(!ctx
)) {
109 for (i
= 0; i
< ctx
->nr_fields
; i
++)
110 offset
+= ctx
->fields
[i
].get_size(ctx
->fields
[i
].priv
, bufctx
->probe_ctx
, offset
);
115 void ctx_record(struct lttng_ust_ring_buffer_ctx
*bufctx
,
116 struct lttng_ust_channel_buffer
*chan
,
117 struct lttng_ust_ctx
*ctx
)
121 if (caa_likely(!ctx
))
123 lttng_ust_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
124 for (i
= 0; i
< ctx
->nr_fields
; i
++)
125 ctx
->fields
[i
].record(ctx
->fields
[i
].priv
, bufctx
->probe_ctx
, bufctx
, chan
);
129 * record_header_size - Calculate the header size and padding necessary.
130 * @config: ring buffer instance configuration
132 * @offset: offset in the write buffer
133 * @pre_header_padding: padding to add before the header (output)
134 * @ctx: reservation context
136 * Returns the event header size (including padding).
138 * The payload must itself determine its own alignment from the biggest type it
142 size_t record_header_size(
143 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
144 struct lttng_ust_ring_buffer_channel
*chan
,
146 size_t *pre_header_padding
,
147 struct lttng_ust_ring_buffer_ctx
*ctx
,
148 struct lttng_client_ctx
*client_ctx
)
150 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
151 size_t orig_offset
= offset
;
154 switch (lttng_chan
->priv
->header_type
) {
155 case 1: /* compact */
156 padding
= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
158 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TIMESTAMP
| LTTNG_RFLAG_EXTENDED
))) {
159 offset
+= sizeof(uint32_t); /* id and timestamp */
161 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
162 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
163 /* Align extended struct on largest member */
164 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
165 offset
+= sizeof(uint32_t); /* id */
166 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
167 offset
+= sizeof(uint64_t); /* timestamp */
171 padding
= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint16_t));
173 offset
+= sizeof(uint16_t);
174 if (!(ctx
->priv
->rflags
& (RING_BUFFER_RFLAG_FULL_TIMESTAMP
| LTTNG_RFLAG_EXTENDED
))) {
175 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint32_t));
176 offset
+= sizeof(uint32_t); /* timestamp */
178 /* Align extended struct on largest member */
179 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
180 offset
+= sizeof(uint32_t); /* id */
181 offset
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
182 offset
+= sizeof(uint64_t); /* timestamp */
189 offset
+= ctx_get_aligned_size(offset
, client_ctx
->chan_ctx
,
190 client_ctx
->packet_context_len
);
191 *pre_header_padding
= padding
;
192 return offset
- orig_offset
;
195 #include "common/ringbuffer/api.h"
196 #include "common/ringbuffer-clients/clients.h"
199 void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config
*config
,
200 struct lttng_ust_ring_buffer_ctx
*ctx
,
201 struct lttng_client_ctx
*client_ctx
,
205 * lttng_write_event_header
207 * Writes the event header to the offset (already aligned on 32-bits).
209 * @config: ring buffer instance configuration
210 * @ctx: reservation context
211 * @event_id: event ID
214 void lttng_write_event_header(const struct lttng_ust_ring_buffer_config
*config
,
215 struct lttng_ust_ring_buffer_ctx
*ctx
,
216 struct lttng_client_ctx
*client_ctx
,
219 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
221 if (caa_unlikely(ctx
->priv
->rflags
))
224 switch (lttng_chan
->priv
->header_type
) {
225 case 1: /* compact */
227 uint32_t id_time
= 0;
229 bt_bitfield_write(&id_time
, uint32_t,
231 LTTNG_COMPACT_EVENT_BITS
,
233 bt_bitfield_write(&id_time
, uint32_t,
234 LTTNG_COMPACT_EVENT_BITS
,
235 LTTNG_COMPACT_TIMESTAMP_BITS
,
236 ctx
->priv
->timestamp
);
237 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
242 uint32_t timestamp
= (uint32_t) ctx
->priv
->timestamp
;
243 uint16_t id
= event_id
;
245 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
246 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
247 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
254 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
);
255 lttng_ust_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
260 lttng_write_event_header_slow(config
, ctx
, client_ctx
, event_id
);
264 void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config
*config
,
265 struct lttng_ust_ring_buffer_ctx
*ctx
,
266 struct lttng_client_ctx
*client_ctx
,
269 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
270 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->priv
->chan
);
272 switch (lttng_chan
->priv
->header_type
) {
273 case 1: /* compact */
274 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TIMESTAMP
| LTTNG_RFLAG_EXTENDED
))) {
275 uint32_t id_time
= 0;
277 bt_bitfield_write(&id_time
, uint32_t,
279 LTTNG_COMPACT_EVENT_BITS
,
281 bt_bitfield_write(&id_time
, uint32_t,
282 LTTNG_COMPACT_EVENT_BITS
,
283 LTTNG_COMPACT_TIMESTAMP_BITS
,
284 ctx_private
->timestamp
);
285 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
288 uint64_t timestamp
= ctx_private
->timestamp
;
290 bt_bitfield_write(&id
, uint8_t,
292 LTTNG_COMPACT_EVENT_BITS
,
294 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
295 /* Align extended struct on largest member */
296 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
297 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
298 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
299 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
304 if (!(ctx_private
->rflags
& (RING_BUFFER_RFLAG_FULL_TIMESTAMP
| LTTNG_RFLAG_EXTENDED
))) {
305 uint32_t timestamp
= (uint32_t) ctx_private
->timestamp
;
306 uint16_t id
= event_id
;
308 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
309 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint32_t));
310 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
313 uint64_t timestamp
= ctx_private
->timestamp
;
315 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
316 /* Align extended struct on largest member */
317 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
318 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
319 lttng_ust_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(uint64_t));
320 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
327 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
);
328 lttng_ust_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
331 static const struct lttng_ust_ring_buffer_config client_config
;
333 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_ring_buffer_channel
*chan
)
335 return lib_ring_buffer_clock_read(chan
);
339 size_t client_record_header_size(const struct lttng_ust_ring_buffer_config
*config
,
340 struct lttng_ust_ring_buffer_channel
*chan
,
342 size_t *pre_header_padding
,
343 struct lttng_ust_ring_buffer_ctx
*ctx
,
346 return record_header_size(config
, chan
, offset
,
347 pre_header_padding
, ctx
, client_ctx
);
351 * client_packet_header_size - called on buffer-switch to a new sub-buffer
353 * Return header size without padding after the structure. Don't use packed
354 * structure because gcc generates inefficient code on some architectures
357 static size_t client_packet_header_size(void)
359 return offsetof(struct packet_header
, ctx
.header_end
);
362 static void client_buffer_begin(struct lttng_ust_ring_buffer
*buf
, uint64_t timestamp
,
363 unsigned int subbuf_idx
,
364 struct lttng_ust_shm_handle
*handle
)
366 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
367 struct packet_header
*header
=
368 (struct packet_header
*)
369 lib_ring_buffer_offset_address(&buf
->backend
,
370 subbuf_idx
* chan
->backend
.subbuf_size
,
372 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
373 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
378 header
->magic
= CTF_MAGIC_NUMBER
;
379 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
380 header
->stream_id
= lttng_chan
->priv
->id
;
381 header
->stream_instance_id
= buf
->backend
.cpu
;
382 header
->ctx
.timestamp_begin
= timestamp
;
383 header
->ctx
.timestamp_end
= 0;
384 header
->ctx
.content_size
= ~0ULL; /* for debugging */
385 header
->ctx
.packet_size
= ~0ULL;
386 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
387 header
->ctx
.events_discarded
= 0;
388 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
392 * offset is assumed to never be 0 here : never deliver a completely empty
393 * subbuffer. data_size is between 1 and subbuf_size.
395 static void client_buffer_end(struct lttng_ust_ring_buffer
*buf
, uint64_t timestamp
,
396 unsigned int subbuf_idx
, unsigned long data_size
,
397 struct lttng_ust_shm_handle
*handle
,
398 const struct lttng_ust_ring_buffer_ctx
*ctx
)
400 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
401 struct packet_header
*header
=
402 (struct packet_header
*)
403 lib_ring_buffer_offset_address(&buf
->backend
,
404 subbuf_idx
* chan
->backend
.subbuf_size
,
406 unsigned long records_lost
= 0;
411 header
->ctx
.timestamp_end
= timestamp
;
412 header
->ctx
.content_size
=
413 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
414 header
->ctx
.packet_size
=
415 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
417 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, ctx
);
418 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, ctx
);
419 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, ctx
);
420 header
->ctx
.events_discarded
= records_lost
;
423 static int client_buffer_create(
424 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
425 void *priv
__attribute__((unused
)),
426 int cpu
__attribute__((unused
)),
427 const char *name
__attribute__((unused
)),
428 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
433 static void client_buffer_finalize(
434 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
435 void *priv
__attribute__((unused
)),
436 int cpu
__attribute__((unused
)),
437 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
441 static void client_content_size_field(
442 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
443 size_t *offset
, size_t *length
)
445 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
446 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
449 static void client_packet_size_field(
450 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
451 size_t *offset
, size_t *length
)
453 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
454 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
457 static struct packet_header
*client_packet_header(struct lttng_ust_ring_buffer
*buf
,
458 struct lttng_ust_shm_handle
*handle
)
460 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
463 static int client_timestamp_begin(struct lttng_ust_ring_buffer
*buf
,
464 struct lttng_ust_ring_buffer_channel
*chan
,
465 uint64_t *timestamp_begin
)
467 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
468 struct packet_header
*header
;
470 header
= client_packet_header(buf
, handle
);
473 *timestamp_begin
= header
->ctx
.timestamp_begin
;
477 static int client_timestamp_end(struct lttng_ust_ring_buffer
*buf
,
478 struct lttng_ust_ring_buffer_channel
*chan
,
479 uint64_t *timestamp_end
)
481 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
482 struct packet_header
*header
;
484 header
= client_packet_header(buf
, handle
);
487 *timestamp_end
= header
->ctx
.timestamp_end
;
491 static int client_events_discarded(struct lttng_ust_ring_buffer
*buf
,
492 struct lttng_ust_ring_buffer_channel
*chan
,
493 uint64_t *events_discarded
)
495 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
496 struct packet_header
*header
;
498 header
= client_packet_header(buf
, handle
);
501 *events_discarded
= header
->ctx
.events_discarded
;
505 static int client_content_size(struct lttng_ust_ring_buffer
*buf
,
506 struct lttng_ust_ring_buffer_channel
*chan
,
507 uint64_t *content_size
)
509 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
510 struct packet_header
*header
;
512 header
= client_packet_header(buf
, handle
);
515 *content_size
= header
->ctx
.content_size
;
519 static int client_packet_size(struct lttng_ust_ring_buffer
*buf
,
520 struct lttng_ust_ring_buffer_channel
*chan
,
521 uint64_t *packet_size
)
523 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
524 struct packet_header
*header
;
526 header
= client_packet_header(buf
, handle
);
529 *packet_size
= header
->ctx
.packet_size
;
533 static int client_stream_id(struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
534 struct lttng_ust_ring_buffer_channel
*chan
,
537 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
539 *stream_id
= lttng_chan
->priv
->id
;
544 static int client_current_timestamp(
545 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
546 struct lttng_ust_ring_buffer_channel
*chan
,
549 *ts
= client_ring_buffer_clock_read(chan
);
554 static int client_sequence_number(struct lttng_ust_ring_buffer
*buf
,
555 struct lttng_ust_ring_buffer_channel
*chan
,
558 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
559 struct packet_header
*header
;
561 header
= client_packet_header(buf
, handle
);
564 *seq
= header
->ctx
.packet_seq_num
;
568 static int client_instance_id(struct lttng_ust_ring_buffer
*buf
,
569 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
572 *id
= buf
->backend
.cpu
;
578 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
580 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
581 .record_header_size
= client_record_header_size
,
582 .subbuffer_header_size
= client_packet_header_size
,
583 .buffer_begin
= client_buffer_begin
,
584 .buffer_end
= client_buffer_end
,
585 .buffer_create
= client_buffer_create
,
586 .buffer_finalize
= client_buffer_finalize
,
587 .content_size_field
= client_content_size_field
,
588 .packet_size_field
= client_packet_size_field
,
590 .timestamp_begin
= client_timestamp_begin
,
591 .timestamp_end
= client_timestamp_end
,
592 .events_discarded
= client_events_discarded
,
593 .content_size
= client_content_size
,
594 .packet_size
= client_packet_size
,
595 .stream_id
= client_stream_id
,
596 .current_timestamp
= client_current_timestamp
,
597 .sequence_number
= client_sequence_number
,
598 .instance_id
= client_instance_id
,
601 static const struct lttng_ust_ring_buffer_config client_config
= {
602 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
603 .cb
.record_header_size
= client_record_header_size
,
604 .cb
.subbuffer_header_size
= client_packet_header_size
,
605 .cb
.buffer_begin
= client_buffer_begin
,
606 .cb
.buffer_end
= client_buffer_end
,
607 .cb
.buffer_create
= client_buffer_create
,
608 .cb
.buffer_finalize
= client_buffer_finalize
,
609 .cb
.content_size_field
= client_content_size_field
,
610 .cb
.packet_size_field
= client_packet_size_field
,
612 .timestamp_bits
= LTTNG_COMPACT_TIMESTAMP_BITS
,
613 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
614 .sync
= RING_BUFFER_SYNC_GLOBAL
,
615 .mode
= RING_BUFFER_MODE_TEMPLATE
,
616 .backend
= RING_BUFFER_PAGE
,
617 .output
= RING_BUFFER_MMAP
,
618 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
619 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
620 .wakeup
= LTTNG_CLIENT_WAKEUP
,
621 .client_type
= LTTNG_CLIENT_TYPE
,
623 .cb_ptr
= &client_cb
.parent
,
627 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
629 size_t subbuf_size
, size_t num_subbuf
,
630 unsigned int switch_timer_interval
,
631 unsigned int read_timer_interval
,
634 const int *stream_fds
, int nr_stream_fds
,
635 int64_t blocking_timeout
)
637 struct lttng_ust_abi_channel_config chan_priv_init
;
638 struct lttng_ust_shm_handle
*handle
;
639 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
641 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
644 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
645 lttng_chan_buf
->priv
->id
= chan_id
;
647 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
648 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
649 chan_priv_init
.id
= chan_id
;
651 handle
= channel_create(&client_config
, name
,
652 __alignof__(struct lttng_ust_abi_channel_config
),
653 sizeof(struct lttng_ust_abi_channel_config
),
655 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
656 switch_timer_interval
, read_timer_interval
,
657 stream_fds
, nr_stream_fds
, blocking_timeout
);
660 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
661 return lttng_chan_buf
;
664 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
669 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
671 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
672 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
676 int lttng_event_reserve(struct lttng_ust_ring_buffer_ctx
*ctx
)
678 struct lttng_ust_event_recorder
*event_recorder
= ctx
->client_priv
;
679 struct lttng_ust_channel_buffer
*lttng_chan
= event_recorder
->chan
;
680 struct lttng_client_ctx client_ctx
;
682 struct lttng_ust_ring_buffer_ctx_private
*private_ctx
;
685 event_id
= (uint32_t) event_recorder
->priv
->parent
.id
;
686 client_ctx
.chan_ctx
= lttng_ust_rcu_dereference(lttng_chan
->priv
->ctx
);
687 /* Compute internal size of context structures. */
688 ctx_get_struct_size(ctx
, client_ctx
.chan_ctx
, &client_ctx
.packet_context_len
);
690 nesting
= lib_ring_buffer_nesting_inc(&client_config
);
694 private_ctx
= &URCU_TLS(private_ctx_stack
)[nesting
];
695 memset(private_ctx
, 0, sizeof(*private_ctx
));
696 private_ctx
->pub
= ctx
;
697 private_ctx
->chan
= lttng_chan
->priv
->rb_chan
;
699 ctx
->priv
= private_ctx
;
701 switch (lttng_chan
->priv
->header_type
) {
702 case 1: /* compact */
704 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
707 if (event_id
> 65534)
708 private_ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
714 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, &client_ctx
);
715 if (caa_unlikely(ret
))
717 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
718 &private_ctx
->backend_pages
)) {
722 lttng_write_event_header(&client_config
, ctx
, &client_ctx
, event_id
);
725 lib_ring_buffer_nesting_dec(&client_config
);
730 void lttng_event_commit(struct lttng_ust_ring_buffer_ctx
*ctx
)
732 lib_ring_buffer_commit(&client_config
, ctx
);
733 lib_ring_buffer_nesting_dec(&client_config
);
737 void lttng_event_write(struct lttng_ust_ring_buffer_ctx
*ctx
,
738 const void *src
, size_t len
, size_t alignment
)
740 lttng_ust_ring_buffer_align_ctx(ctx
, alignment
);
741 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
745 void lttng_event_strcpy(struct lttng_ust_ring_buffer_ctx
*ctx
,
746 const char *src
, size_t len
)
748 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
752 void lttng_event_pstrcpy_pad(struct lttng_ust_ring_buffer_ctx
*ctx
,
753 const char *src
, size_t len
)
755 lib_ring_buffer_pstrcpy(&client_config
, ctx
, src
, len
, '\0');
759 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
761 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
763 return lib_ring_buffer_channel_is_finalized(rb_chan
);
767 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
769 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
771 return lib_ring_buffer_channel_is_disabled(rb_chan
);
775 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
777 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
778 struct lttng_ust_ring_buffer
*buf
;
781 for_each_channel_cpu(cpu
, rb_chan
) {
782 int shm_fd
, wait_fd
, wakeup_fd
;
783 uint64_t memory_map_size
;
784 void *memory_map_addr
;
786 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
787 cpu
, rb_chan
->handle
, &shm_fd
, &wait_fd
,
788 &wakeup_fd
, &memory_map_size
, &memory_map_addr
);
789 lib_ring_buffer_switch(&client_config
, buf
,
790 SWITCH_ACTIVE
, rb_chan
->handle
);
795 static struct lttng_transport lttng_relay_transport
= {
796 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
798 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
799 .priv
= LTTNG_UST_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
800 .pub
= <tng_relay_transport
.ops
,
801 .channel_create
= _channel_create
,
802 .channel_destroy
= lttng_channel_destroy
,
803 .packet_avail_size
= NULL
, /* Would be racy anyway */
804 .is_finalized
= lttng_is_finalized
,
805 .is_disabled
= lttng_is_disabled
,
806 .flush_buffer
= lttng_flush_buffer
,
808 .event_reserve
= lttng_event_reserve
,
809 .event_commit
= lttng_event_commit
,
810 .event_write
= lttng_event_write
,
811 .event_strcpy
= lttng_event_strcpy
,
812 .event_pstrcpy_pad
= lttng_event_pstrcpy_pad
,
814 .client_config
= &client_config
,
817 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
819 DBG("LTT : ltt ring buffer client \"%s\" init\n",
820 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
821 lttng_transport_register(<tng_relay_transport
);
824 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
826 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
827 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
828 lttng_transport_unregister(<tng_relay_transport
);