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.
11 #include <lttng/ust-events.h>
12 #include "ust-bitfield.h"
13 #include "ust-compat.h"
15 #include "lttng-tracer.h"
16 #include "../libringbuffer/frontend_types.h"
18 #define LTTNG_COMPACT_EVENT_BITS 5
19 #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
;
65 static inline uint64_t lib_ring_buffer_clock_read(struct channel
*chan
)
67 return trace_clock_read64();
71 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ctx
*ctx
,
74 size_t orig_offset
= offset
;
78 offset
+= lib_ring_buffer_align(offset
, ctx
->largest_align
);
80 return offset
- orig_offset
;
84 void ctx_get_struct_size(struct lttng_ctx
*ctx
, size_t *ctx_len
,
85 enum app_ctx_mode mode
)
90 if (caa_likely(!ctx
)) {
94 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
95 if (mode
== APP_CTX_ENABLED
) {
96 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
98 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
100 * Before UST 2.8, we cannot use the
101 * application context, because we
102 * cannot trust that the handler used
103 * for get_size is the same used for
104 * ctx_record, which would result in
105 * corrupted traces when tracing
106 * concurrently with application context
107 * register/unregister.
109 offset
+= lttng_ust_dummy_get_size(&ctx
->fields
[i
], offset
);
111 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
119 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
120 struct lttng_channel
*chan
,
121 struct lttng_ctx
*ctx
,
122 enum app_ctx_mode mode
)
126 if (caa_likely(!ctx
))
128 lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
129 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
130 if (mode
== APP_CTX_ENABLED
) {
131 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
133 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
135 * Before UST 2.8, we cannot use the
136 * application context, because we
137 * cannot trust that the handler used
138 * for get_size is the same used for
139 * ctx_record, which would result in
140 * corrupted traces when tracing
141 * concurrently with application context
142 * register/unregister.
144 lttng_ust_dummy_record(&ctx
->fields
[i
], bufctx
, chan
);
146 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
153 * record_header_size - Calculate the header size and padding necessary.
154 * @config: ring buffer instance configuration
156 * @offset: offset in the write buffer
157 * @pre_header_padding: padding to add before the header (output)
158 * @ctx: reservation context
160 * Returns the event header size (including padding).
162 * The payload must itself determine its own alignment from the biggest type it
166 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
167 struct channel
*chan
, size_t offset
,
168 size_t *pre_header_padding
,
169 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
170 struct lttng_client_ctx
*client_ctx
)
172 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
173 struct lttng_event
*event
= ctx
->priv
;
174 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
175 size_t orig_offset
= offset
;
178 switch (lttng_chan
->header_type
) {
179 case 1: /* compact */
180 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
182 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
183 offset
+= sizeof(uint32_t); /* id and timestamp */
185 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
186 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
187 /* Align extended struct on largest member */
188 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
189 offset
+= sizeof(uint32_t); /* id */
190 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
191 offset
+= sizeof(uint64_t); /* timestamp */
195 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
197 offset
+= sizeof(uint16_t);
198 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
199 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
200 offset
+= sizeof(uint32_t); /* timestamp */
202 /* Align extended struct on largest member */
203 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
204 offset
+= sizeof(uint32_t); /* id */
205 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
206 offset
+= sizeof(uint64_t); /* timestamp */
214 /* 2.8+ probe ABI. */
215 offset
+= ctx_get_aligned_size(offset
, lttng_ctx
->chan_ctx
,
216 client_ctx
->packet_context_len
);
217 offset
+= ctx_get_aligned_size(offset
, lttng_ctx
->event_ctx
,
218 client_ctx
->event_context_len
);
220 /* Pre 2.8 probe ABI. */
221 offset
+= ctx_get_aligned_size(offset
, lttng_chan
->ctx
,
222 client_ctx
->packet_context_len
);
223 offset
+= ctx_get_aligned_size(offset
, event
->ctx
,
224 client_ctx
->event_context_len
);
226 *pre_header_padding
= padding
;
227 return offset
- orig_offset
;
230 #include "../libringbuffer/api.h"
231 #include "lttng-rb-clients.h"
234 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
235 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
239 * lttng_write_event_header
241 * Writes the event header to the offset (already aligned on 32-bits).
243 * @config: ring buffer instance configuration
244 * @ctx: reservation context
245 * @event_id: event ID
248 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
249 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
252 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
253 struct lttng_event
*event
= ctx
->priv
;
254 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
256 if (caa_unlikely(ctx
->rflags
))
259 switch (lttng_chan
->header_type
) {
260 case 1: /* compact */
262 uint32_t id_time
= 0;
264 bt_bitfield_write(&id_time
, uint32_t,
266 LTTNG_COMPACT_EVENT_BITS
,
268 bt_bitfield_write(&id_time
, uint32_t,
269 LTTNG_COMPACT_EVENT_BITS
,
270 LTTNG_COMPACT_TSC_BITS
,
272 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
277 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
278 uint16_t id
= event_id
;
280 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
281 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
282 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
290 /* 2.8+ probe ABI. */
291 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
292 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
294 /* Pre 2.8 probe ABI. */
295 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
296 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
298 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
303 lttng_write_event_header_slow(config
, ctx
, event_id
);
307 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
308 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
311 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
312 struct lttng_event
*event
= ctx
->priv
;
313 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
315 switch (lttng_chan
->header_type
) {
316 case 1: /* compact */
317 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
318 uint32_t id_time
= 0;
320 bt_bitfield_write(&id_time
, uint32_t,
322 LTTNG_COMPACT_EVENT_BITS
,
324 bt_bitfield_write(&id_time
, uint32_t,
325 LTTNG_COMPACT_EVENT_BITS
,
326 LTTNG_COMPACT_TSC_BITS
,
328 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
331 uint64_t timestamp
= ctx
->tsc
;
333 bt_bitfield_write(&id
, uint8_t,
335 LTTNG_COMPACT_EVENT_BITS
,
337 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
338 /* Align extended struct on largest member */
339 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
340 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
341 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
342 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
347 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
348 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
349 uint16_t id
= event_id
;
351 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
352 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
353 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
356 uint64_t timestamp
= ctx
->tsc
;
358 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
359 /* Align extended struct on largest member */
360 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
361 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
362 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
363 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
371 /* 2.8+ probe ABI. */
372 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
373 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
375 /* Pre 2.8 probe ABI. */
376 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
377 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
379 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
382 static const struct lttng_ust_lib_ring_buffer_config client_config
;
384 static uint64_t client_ring_buffer_clock_read(struct channel
*chan
)
386 return lib_ring_buffer_clock_read(chan
);
390 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
391 struct channel
*chan
, size_t offset
,
392 size_t *pre_header_padding
,
393 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
396 return record_header_size(config
, chan
, offset
,
397 pre_header_padding
, ctx
, client_ctx
);
401 * client_packet_header_size - called on buffer-switch to a new sub-buffer
403 * Return header size without padding after the structure. Don't use packed
404 * structure because gcc generates inefficient code on some architectures
407 static size_t client_packet_header_size(void)
409 return offsetof(struct packet_header
, ctx
.header_end
);
412 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
413 unsigned int subbuf_idx
,
414 struct lttng_ust_shm_handle
*handle
)
416 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
417 struct packet_header
*header
=
418 (struct packet_header
*)
419 lib_ring_buffer_offset_address(&buf
->backend
,
420 subbuf_idx
* chan
->backend
.subbuf_size
,
422 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
423 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
428 header
->magic
= CTF_MAGIC_NUMBER
;
429 memcpy(header
->uuid
, lttng_chan
->uuid
, sizeof(lttng_chan
->uuid
));
430 header
->stream_id
= lttng_chan
->id
;
431 header
->stream_instance_id
= buf
->backend
.cpu
;
432 header
->ctx
.timestamp_begin
= tsc
;
433 header
->ctx
.timestamp_end
= 0;
434 header
->ctx
.content_size
= ~0ULL; /* for debugging */
435 header
->ctx
.packet_size
= ~0ULL;
436 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
437 header
->ctx
.events_discarded
= 0;
438 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
442 * offset is assumed to never be 0 here : never deliver a completely empty
443 * subbuffer. data_size is between 1 and subbuf_size.
445 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
446 unsigned int subbuf_idx
, unsigned long data_size
,
447 struct lttng_ust_shm_handle
*handle
)
449 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
450 struct packet_header
*header
=
451 (struct packet_header
*)
452 lib_ring_buffer_offset_address(&buf
->backend
,
453 subbuf_idx
* chan
->backend
.subbuf_size
,
455 unsigned long records_lost
= 0;
460 header
->ctx
.timestamp_end
= tsc
;
461 header
->ctx
.content_size
=
462 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
463 header
->ctx
.packet_size
=
464 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
466 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
467 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
468 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
469 header
->ctx
.events_discarded
= records_lost
;
472 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
473 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
478 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
482 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
483 size_t *offset
, size_t *length
)
485 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
486 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
489 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
490 size_t *offset
, size_t *length
)
492 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
493 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
496 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
497 struct lttng_ust_shm_handle
*handle
)
499 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
502 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
503 struct lttng_ust_shm_handle
*handle
,
504 uint64_t *timestamp_begin
)
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_shm_handle
*handle
,
517 uint64_t *timestamp_end
)
519 struct packet_header
*header
;
521 header
= client_packet_header(buf
, handle
);
524 *timestamp_end
= header
->ctx
.timestamp_end
;
528 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
529 struct lttng_ust_shm_handle
*handle
,
530 uint64_t *events_discarded
)
532 struct packet_header
*header
;
534 header
= client_packet_header(buf
, handle
);
537 *events_discarded
= header
->ctx
.events_discarded
;
541 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
542 struct lttng_ust_shm_handle
*handle
,
543 uint64_t *content_size
)
545 struct packet_header
*header
;
547 header
= client_packet_header(buf
, handle
);
550 *content_size
= header
->ctx
.content_size
;
554 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
555 struct lttng_ust_shm_handle
*handle
,
556 uint64_t *packet_size
)
558 struct packet_header
*header
;
560 header
= client_packet_header(buf
, handle
);
563 *packet_size
= header
->ctx
.packet_size
;
567 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
568 struct lttng_ust_shm_handle
*handle
,
571 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
572 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
574 *stream_id
= lttng_chan
->id
;
579 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
580 struct lttng_ust_shm_handle
*handle
,
583 struct channel
*chan
;
585 chan
= shmp(handle
, handle
->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_shm_handle
*handle
,
595 struct packet_header
*header
;
597 header
= client_packet_header(buf
, handle
);
600 *seq
= header
->ctx
.packet_seq_num
;
604 static int client_instance_id(struct lttng_ust_lib_ring_buffer
*buf
,
605 struct lttng_ust_shm_handle
*handle
,
608 *id
= buf
->backend
.cpu
;
614 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
616 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
617 .record_header_size
= client_record_header_size
,
618 .subbuffer_header_size
= client_packet_header_size
,
619 .buffer_begin
= client_buffer_begin
,
620 .buffer_end
= client_buffer_end
,
621 .buffer_create
= client_buffer_create
,
622 .buffer_finalize
= client_buffer_finalize
,
623 .content_size_field
= client_content_size_field
,
624 .packet_size_field
= client_packet_size_field
,
626 .timestamp_begin
= client_timestamp_begin
,
627 .timestamp_end
= client_timestamp_end
,
628 .events_discarded
= client_events_discarded
,
629 .content_size
= client_content_size
,
630 .packet_size
= client_packet_size
,
631 .stream_id
= client_stream_id
,
632 .current_timestamp
= client_current_timestamp
,
633 .sequence_number
= client_sequence_number
,
634 .instance_id
= client_instance_id
,
637 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
638 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
639 .cb
.record_header_size
= client_record_header_size
,
640 .cb
.subbuffer_header_size
= client_packet_header_size
,
641 .cb
.buffer_begin
= client_buffer_begin
,
642 .cb
.buffer_end
= client_buffer_end
,
643 .cb
.buffer_create
= client_buffer_create
,
644 .cb
.buffer_finalize
= client_buffer_finalize
,
645 .cb
.content_size_field
= client_content_size_field
,
646 .cb
.packet_size_field
= client_packet_size_field
,
648 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
649 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
650 .sync
= RING_BUFFER_SYNC_GLOBAL
,
651 .mode
= RING_BUFFER_MODE_TEMPLATE
,
652 .backend
= RING_BUFFER_PAGE
,
653 .output
= RING_BUFFER_MMAP
,
654 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
655 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
656 .wakeup
= LTTNG_CLIENT_WAKEUP
,
657 .client_type
= LTTNG_CLIENT_TYPE
,
659 .cb_ptr
= &client_cb
.parent
,
663 struct lttng_channel
*_channel_create(const char *name
,
665 size_t subbuf_size
, size_t num_subbuf
,
666 unsigned int switch_timer_interval
,
667 unsigned int read_timer_interval
,
670 const int *stream_fds
, int nr_stream_fds
,
671 int64_t blocking_timeout
)
673 struct lttng_channel chan_priv_init
;
674 struct lttng_ust_shm_handle
*handle
;
675 struct lttng_channel
*lttng_chan
;
678 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
679 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
680 chan_priv_init
.id
= chan_id
;
681 handle
= channel_create(&client_config
, name
,
682 &priv
, __alignof__(struct lttng_channel
),
683 sizeof(struct lttng_channel
),
685 buf_addr
, subbuf_size
, num_subbuf
,
686 switch_timer_interval
, read_timer_interval
,
687 stream_fds
, nr_stream_fds
, blocking_timeout
);
691 lttng_chan
->handle
= handle
;
692 lttng_chan
->chan
= shmp(handle
, handle
->chan
);
697 void lttng_channel_destroy(struct lttng_channel
*chan
)
699 channel_destroy(chan
->chan
, chan
->handle
, 1);
703 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
706 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
707 struct lttng_event
*event
= ctx
->priv
;
708 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
709 struct lttng_client_ctx client_ctx
;
712 /* Compute internal size of context structures. */
715 /* 2.8+ probe ABI. */
716 ctx_get_struct_size(lttng_ctx
->chan_ctx
, &client_ctx
.packet_context_len
,
718 ctx_get_struct_size(lttng_ctx
->event_ctx
, &client_ctx
.event_context_len
,
721 /* Pre 2.8 probe ABI. */
722 ctx_get_struct_size(lttng_chan
->ctx
, &client_ctx
.packet_context_len
,
724 ctx_get_struct_size(event
->ctx
, &client_ctx
.event_context_len
,
728 cpu
= lib_ring_buffer_get_cpu(&client_config
);
733 switch (lttng_chan
->header_type
) {
734 case 1: /* compact */
736 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
739 if (event_id
> 65534)
740 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
746 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, &client_ctx
);
747 if (caa_unlikely(ret
))
749 if (caa_likely(ctx
->ctx_len
750 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx
))) {
751 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
752 &ctx
->backend_pages
)) {
757 lttng_write_event_header(&client_config
, ctx
, event_id
);
760 lib_ring_buffer_put_cpu(&client_config
);
765 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
767 lib_ring_buffer_commit(&client_config
, ctx
);
768 lib_ring_buffer_put_cpu(&client_config
);
772 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
775 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
779 void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const char *src
,
782 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
787 wait_queue_head_t
*lttng_get_reader_wait_queue(struct channel
*chan
)
789 return &chan
->read_wait
;
793 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
795 return &chan
->hp_wait
;
800 int lttng_is_finalized(struct channel
*chan
)
802 return lib_ring_buffer_channel_is_finalized(chan
);
806 int lttng_is_disabled(struct channel
*chan
)
808 return lib_ring_buffer_channel_is_disabled(chan
);
812 int lttng_flush_buffer(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
814 struct lttng_ust_lib_ring_buffer
*buf
;
817 for_each_channel_cpu(cpu
, chan
) {
818 int shm_fd
, wait_fd
, wakeup_fd
;
819 uint64_t memory_map_size
;
821 buf
= channel_get_ring_buffer(&client_config
, chan
,
822 cpu
, handle
, &shm_fd
, &wait_fd
,
823 &wakeup_fd
, &memory_map_size
);
824 lib_ring_buffer_switch(&client_config
, buf
,
825 SWITCH_ACTIVE
, handle
);
830 static struct lttng_transport lttng_relay_transport
= {
831 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
833 .channel_create
= _channel_create
,
834 .channel_destroy
= lttng_channel_destroy
,
835 .event_reserve
= lttng_event_reserve
,
836 .event_commit
= lttng_event_commit
,
837 .event_write
= lttng_event_write
,
838 .packet_avail_size
= NULL
, /* Would be racy anyway */
839 .is_finalized
= lttng_is_finalized
,
840 .is_disabled
= lttng_is_disabled
,
841 .flush_buffer
= lttng_flush_buffer
,
842 .event_strcpy
= lttng_event_strcpy
,
844 .client_config
= &client_config
,
847 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
849 DBG("LTT : ltt ring buffer client \"%s\" init\n",
850 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
851 lttng_transport_register(<tng_relay_transport
);
854 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
856 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
857 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
858 lttng_transport_unregister(<tng_relay_transport
);