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.
12 #include <ust-events-internal.h>
13 #include <lttng/urcu/pointer.h>
14 #include "ust-bitfield.h"
15 #include "ust-compat.h"
17 #include "context-internal.h"
18 #include "lttng-tracer.h"
19 #include "../libringbuffer/frontend_types.h"
21 #define LTTNG_COMPACT_EVENT_BITS 5
22 #define LTTNG_COMPACT_TSC_BITS 27
30 * Keep the natural field alignment for _each field_ within this structure if
31 * you ever add/remove a field from this header. Packed attribute is not used
32 * because gcc generates poor code on at least powerpc and mips. Don't ever
33 * let gcc add padding between the structure elements.
36 struct packet_header
{
37 /* Trace packet header */
40 * contains endianness information.
42 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
44 uint64_t stream_instance_id
;
47 /* Stream packet context */
48 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
49 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
50 uint64_t content_size
; /* Size of data in subbuffer */
51 uint64_t packet_size
; /* Subbuffer size (include padding) */
52 uint64_t packet_seq_num
; /* Packet sequence number */
53 unsigned long events_discarded
; /*
54 * Events lost in this subbuffer since
55 * the beginning of the trace.
58 uint32_t cpu_id
; /* CPU id associated with stream */
59 uint8_t header_end
; /* End of header */
63 struct lttng_client_ctx
{
64 size_t packet_context_len
;
65 size_t event_context_len
;
66 struct lttng_ust_ctx
*chan_ctx
;
69 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
71 return trace_clock_read64();
75 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ust_ctx
*ctx
,
78 size_t orig_offset
= offset
;
82 offset
+= lib_ring_buffer_align(offset
, ctx
->largest_align
);
84 return offset
- orig_offset
;
88 void ctx_get_struct_size(struct lttng_ust_ctx
*ctx
, size_t *ctx_len
,
89 enum app_ctx_mode mode
)
94 if (caa_likely(!ctx
)) {
98 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
99 if (mode
== APP_CTX_ENABLED
) {
100 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
102 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
104 * Before UST 2.8, we cannot use the
105 * application context, because we
106 * cannot trust that the handler used
107 * for get_size is the same used for
108 * ctx_record, which would result in
109 * corrupted traces when tracing
110 * concurrently with application context
111 * register/unregister.
113 offset
+= lttng_ust_dummy_get_size(ctx
->fields
[i
], offset
);
115 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
123 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
124 struct lttng_ust_channel_buffer
*chan
,
125 struct lttng_ust_ctx
*ctx
,
126 enum app_ctx_mode mode
)
130 if (caa_likely(!ctx
))
132 lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
133 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
134 if (mode
== APP_CTX_ENABLED
) {
135 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
137 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
139 * Before UST 2.8, we cannot use the
140 * application context, because we
141 * cannot trust that the handler used
142 * for get_size is the same used for
143 * ctx_record, which would result in
144 * corrupted traces when tracing
145 * concurrently with application context
146 * register/unregister.
148 lttng_ust_dummy_record(ctx
->fields
[i
], bufctx
, chan
);
150 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
157 * record_header_size - Calculate the header size and padding necessary.
158 * @config: ring buffer instance configuration
160 * @offset: offset in the write buffer
161 * @pre_header_padding: padding to add before the header (output)
162 * @ctx: reservation context
164 * Returns the event header size (including padding).
166 * The payload must itself determine its own alignment from the biggest type it
170 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
171 struct lttng_ust_lib_ring_buffer_channel
*chan
,
173 size_t *pre_header_padding
,
174 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
175 struct lttng_client_ctx
*client_ctx
)
177 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
178 struct lttng_ust_stack_ctx
*lttng_ctx
= ctx
->priv
;
179 size_t orig_offset
= offset
;
182 switch (lttng_chan
->priv
->header_type
) {
183 case 1: /* compact */
184 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
186 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
187 offset
+= sizeof(uint32_t); /* id and timestamp */
189 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
190 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
191 /* Align extended struct on largest member */
192 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
193 offset
+= sizeof(uint32_t); /* id */
194 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
195 offset
+= sizeof(uint64_t); /* timestamp */
199 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
201 offset
+= sizeof(uint16_t);
202 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
203 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
204 offset
+= sizeof(uint32_t); /* timestamp */
206 /* Align extended struct on largest member */
207 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
208 offset
+= sizeof(uint32_t); /* id */
209 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
210 offset
+= sizeof(uint64_t); /* timestamp */
217 offset
+= ctx_get_aligned_size(offset
, client_ctx
->chan_ctx
,
218 client_ctx
->packet_context_len
);
219 offset
+= ctx_get_aligned_size(offset
, lttng_ctx
->event_ctx
,
220 client_ctx
->event_context_len
);
221 *pre_header_padding
= padding
;
222 return offset
- orig_offset
;
225 #include "../libringbuffer/api.h"
226 #include "lttng-rb-clients.h"
229 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
230 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
231 struct lttng_client_ctx
*client_ctx
,
235 * lttng_write_event_header
237 * Writes the event header to the offset (already aligned on 32-bits).
239 * @config: ring buffer instance configuration
240 * @ctx: reservation context
241 * @event_id: event ID
244 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
245 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
246 struct lttng_client_ctx
*client_ctx
,
249 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->chan
);
250 struct lttng_ust_stack_ctx
*lttng_ctx
= ctx
->priv
;
252 if (caa_unlikely(ctx
->rflags
))
255 switch (lttng_chan
->priv
->header_type
) {
256 case 1: /* compact */
258 uint32_t id_time
= 0;
260 bt_bitfield_write(&id_time
, uint32_t,
262 LTTNG_COMPACT_EVENT_BITS
,
264 bt_bitfield_write(&id_time
, uint32_t,
265 LTTNG_COMPACT_EVENT_BITS
,
266 LTTNG_COMPACT_TSC_BITS
,
268 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
273 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
274 uint16_t id
= event_id
;
276 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
277 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
278 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
285 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
, APP_CTX_ENABLED
);
286 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
287 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
292 lttng_write_event_header_slow(config
, ctx
, client_ctx
, event_id
);
296 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
297 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
298 struct lttng_client_ctx
*client_ctx
,
301 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->chan
);
302 struct lttng_ust_stack_ctx
*lttng_ctx
= ctx
->priv
;
304 switch (lttng_chan
->priv
->header_type
) {
305 case 1: /* compact */
306 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
307 uint32_t id_time
= 0;
309 bt_bitfield_write(&id_time
, uint32_t,
311 LTTNG_COMPACT_EVENT_BITS
,
313 bt_bitfield_write(&id_time
, uint32_t,
314 LTTNG_COMPACT_EVENT_BITS
,
315 LTTNG_COMPACT_TSC_BITS
,
317 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
320 uint64_t timestamp
= ctx
->tsc
;
322 bt_bitfield_write(&id
, uint8_t,
324 LTTNG_COMPACT_EVENT_BITS
,
326 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
327 /* Align extended struct on largest member */
328 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
329 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
330 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
331 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
336 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
337 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
338 uint16_t id
= event_id
;
340 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
341 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
342 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
345 uint64_t timestamp
= ctx
->tsc
;
347 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
348 /* Align extended struct on largest member */
349 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
350 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
351 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
352 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
359 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
, APP_CTX_ENABLED
);
360 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
361 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
364 static const struct lttng_ust_lib_ring_buffer_config client_config
;
366 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
368 return lib_ring_buffer_clock_read(chan
);
372 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
373 struct lttng_ust_lib_ring_buffer_channel
*chan
,
375 size_t *pre_header_padding
,
376 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
379 return record_header_size(config
, chan
, offset
,
380 pre_header_padding
, ctx
, client_ctx
);
384 * client_packet_header_size - called on buffer-switch to a new sub-buffer
386 * Return header size without padding after the structure. Don't use packed
387 * structure because gcc generates inefficient code on some architectures
390 static size_t client_packet_header_size(void)
392 return offsetof(struct packet_header
, ctx
.header_end
);
395 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
396 unsigned int subbuf_idx
,
397 struct lttng_ust_shm_handle
*handle
)
399 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
400 struct packet_header
*header
=
401 (struct packet_header
*)
402 lib_ring_buffer_offset_address(&buf
->backend
,
403 subbuf_idx
* chan
->backend
.subbuf_size
,
405 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
406 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
411 header
->magic
= CTF_MAGIC_NUMBER
;
412 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
413 header
->stream_id
= lttng_chan
->priv
->id
;
414 header
->stream_instance_id
= buf
->backend
.cpu
;
415 header
->ctx
.timestamp_begin
= tsc
;
416 header
->ctx
.timestamp_end
= 0;
417 header
->ctx
.content_size
= ~0ULL; /* for debugging */
418 header
->ctx
.packet_size
= ~0ULL;
419 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
420 header
->ctx
.events_discarded
= 0;
421 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
425 * offset is assumed to never be 0 here : never deliver a completely empty
426 * subbuffer. data_size is between 1 and subbuf_size.
428 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
429 unsigned int subbuf_idx
, unsigned long data_size
,
430 struct lttng_ust_shm_handle
*handle
)
432 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
433 struct packet_header
*header
=
434 (struct packet_header
*)
435 lib_ring_buffer_offset_address(&buf
->backend
,
436 subbuf_idx
* chan
->backend
.subbuf_size
,
438 unsigned long records_lost
= 0;
443 header
->ctx
.timestamp_end
= tsc
;
444 header
->ctx
.content_size
=
445 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
446 header
->ctx
.packet_size
=
447 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
449 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
450 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
451 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
452 header
->ctx
.events_discarded
= records_lost
;
455 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
456 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
461 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
465 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
466 size_t *offset
, size_t *length
)
468 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
469 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
472 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
473 size_t *offset
, size_t *length
)
475 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
476 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
479 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
480 struct lttng_ust_shm_handle
*handle
)
482 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
485 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
486 struct lttng_ust_shm_handle
*handle
,
487 uint64_t *timestamp_begin
)
489 struct packet_header
*header
;
491 header
= client_packet_header(buf
, handle
);
494 *timestamp_begin
= header
->ctx
.timestamp_begin
;
498 static int client_timestamp_end(struct lttng_ust_lib_ring_buffer
*buf
,
499 struct lttng_ust_shm_handle
*handle
,
500 uint64_t *timestamp_end
)
502 struct packet_header
*header
;
504 header
= client_packet_header(buf
, handle
);
507 *timestamp_end
= header
->ctx
.timestamp_end
;
511 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
512 struct lttng_ust_shm_handle
*handle
,
513 uint64_t *events_discarded
)
515 struct packet_header
*header
;
517 header
= client_packet_header(buf
, handle
);
520 *events_discarded
= header
->ctx
.events_discarded
;
524 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
525 struct lttng_ust_shm_handle
*handle
,
526 uint64_t *content_size
)
528 struct packet_header
*header
;
530 header
= client_packet_header(buf
, handle
);
533 *content_size
= header
->ctx
.content_size
;
537 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
538 struct lttng_ust_shm_handle
*handle
,
539 uint64_t *packet_size
)
541 struct packet_header
*header
;
543 header
= client_packet_header(buf
, handle
);
546 *packet_size
= header
->ctx
.packet_size
;
550 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
551 struct lttng_ust_shm_handle
*handle
,
554 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
,
556 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
558 *stream_id
= lttng_chan
->priv
->id
;
563 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
564 struct lttng_ust_shm_handle
*handle
,
567 struct lttng_ust_lib_ring_buffer_channel
*chan
;
569 chan
= shmp(handle
, handle
->chan
);
570 *ts
= client_ring_buffer_clock_read(chan
);
575 static int client_sequence_number(struct lttng_ust_lib_ring_buffer
*buf
,
576 struct lttng_ust_shm_handle
*handle
,
579 struct packet_header
*header
;
581 header
= client_packet_header(buf
, handle
);
584 *seq
= header
->ctx
.packet_seq_num
;
588 static int client_instance_id(struct lttng_ust_lib_ring_buffer
*buf
,
589 struct lttng_ust_shm_handle
*handle
,
592 *id
= buf
->backend
.cpu
;
598 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
600 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
601 .record_header_size
= client_record_header_size
,
602 .subbuffer_header_size
= client_packet_header_size
,
603 .buffer_begin
= client_buffer_begin
,
604 .buffer_end
= client_buffer_end
,
605 .buffer_create
= client_buffer_create
,
606 .buffer_finalize
= client_buffer_finalize
,
607 .content_size_field
= client_content_size_field
,
608 .packet_size_field
= client_packet_size_field
,
610 .timestamp_begin
= client_timestamp_begin
,
611 .timestamp_end
= client_timestamp_end
,
612 .events_discarded
= client_events_discarded
,
613 .content_size
= client_content_size
,
614 .packet_size
= client_packet_size
,
615 .stream_id
= client_stream_id
,
616 .current_timestamp
= client_current_timestamp
,
617 .sequence_number
= client_sequence_number
,
618 .instance_id
= client_instance_id
,
621 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
622 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
623 .cb
.record_header_size
= client_record_header_size
,
624 .cb
.subbuffer_header_size
= client_packet_header_size
,
625 .cb
.buffer_begin
= client_buffer_begin
,
626 .cb
.buffer_end
= client_buffer_end
,
627 .cb
.buffer_create
= client_buffer_create
,
628 .cb
.buffer_finalize
= client_buffer_finalize
,
629 .cb
.content_size_field
= client_content_size_field
,
630 .cb
.packet_size_field
= client_packet_size_field
,
632 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
633 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
634 .sync
= RING_BUFFER_SYNC_GLOBAL
,
635 .mode
= RING_BUFFER_MODE_TEMPLATE
,
636 .backend
= RING_BUFFER_PAGE
,
637 .output
= RING_BUFFER_MMAP
,
638 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
639 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
640 .wakeup
= LTTNG_CLIENT_WAKEUP
,
641 .client_type
= LTTNG_CLIENT_TYPE
,
643 .cb_ptr
= &client_cb
.parent
,
647 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
649 size_t subbuf_size
, size_t num_subbuf
,
650 unsigned int switch_timer_interval
,
651 unsigned int read_timer_interval
,
654 const int *stream_fds
, int nr_stream_fds
,
655 int64_t blocking_timeout
)
657 struct lttng_ust_abi_channel_config chan_priv_init
;
658 struct lttng_ust_shm_handle
*handle
;
659 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
661 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
664 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
665 lttng_chan_buf
->priv
->id
= chan_id
;
667 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
668 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
669 chan_priv_init
.id
= chan_id
;
671 handle
= channel_create(&client_config
, name
,
672 __alignof__(struct lttng_ust_abi_channel_config
),
673 sizeof(struct lttng_ust_abi_channel_config
),
675 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
676 switch_timer_interval
, read_timer_interval
,
677 stream_fds
, nr_stream_fds
, blocking_timeout
);
680 lttng_chan_buf
->handle
= handle
;
681 lttng_chan_buf
->chan
= shmp(handle
, handle
->chan
);
682 return lttng_chan_buf
;
685 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
690 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
692 channel_destroy(lttng_chan_buf
->chan
, lttng_chan_buf
->handle
, 1);
693 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
697 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
700 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->chan
);
701 struct lttng_ust_stack_ctx
*lttng_ctx
= ctx
->priv
;
702 struct lttng_client_ctx client_ctx
;
705 client_ctx
.chan_ctx
= lttng_ust_rcu_dereference(lttng_chan
->priv
->ctx
);
706 /* Compute internal size of context structures. */
707 ctx_get_struct_size(client_ctx
.chan_ctx
, &client_ctx
.packet_context_len
,
709 ctx_get_struct_size(lttng_ctx
->event_ctx
, &client_ctx
.event_context_len
,
712 cpu
= lib_ring_buffer_get_cpu(&client_config
);
717 switch (lttng_chan
->priv
->header_type
) {
718 case 1: /* compact */
720 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
723 if (event_id
> 65534)
724 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
730 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, &client_ctx
);
731 if (caa_unlikely(ret
))
733 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
734 &ctx
->backend_pages
)) {
738 lttng_write_event_header(&client_config
, ctx
, &client_ctx
, event_id
);
741 lib_ring_buffer_put_cpu(&client_config
);
746 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
748 lib_ring_buffer_commit(&client_config
, ctx
);
749 lib_ring_buffer_put_cpu(&client_config
);
753 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
756 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
760 void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const char *src
,
763 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
768 wait_queue_head_t
*lttng_get_reader_wait_queue(struct lttng_ust_lib_ring_buffer_channel
*chan
)
770 return &chan
->read_wait
;
774 wait_queue_head_t
*lttng_get_hp_wait_queue(struct lttng_ust_lib_ring_buffer_channel
*chan
)
776 return &chan
->hp_wait
;
781 int lttng_is_finalized(struct lttng_ust_lib_ring_buffer_channel
*chan
)
783 return lib_ring_buffer_channel_is_finalized(chan
);
787 int lttng_is_disabled(struct lttng_ust_lib_ring_buffer_channel
*chan
)
789 return lib_ring_buffer_channel_is_disabled(chan
);
793 int lttng_flush_buffer(struct lttng_ust_lib_ring_buffer_channel
*chan
,
794 struct lttng_ust_shm_handle
*handle
)
796 struct lttng_ust_lib_ring_buffer
*buf
;
799 for_each_channel_cpu(cpu
, chan
) {
800 int shm_fd
, wait_fd
, wakeup_fd
;
801 uint64_t memory_map_size
;
803 buf
= channel_get_ring_buffer(&client_config
, chan
,
804 cpu
, handle
, &shm_fd
, &wait_fd
,
805 &wakeup_fd
, &memory_map_size
);
806 lib_ring_buffer_switch(&client_config
, buf
,
807 SWITCH_ACTIVE
, handle
);
812 static struct lttng_transport lttng_relay_transport
= {
813 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
815 .struct_size
= sizeof(struct lttng_ust_channel_ops
),
816 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_ops_private
, {
817 .pub
= <tng_relay_transport
.ops
,
818 .channel_create
= _channel_create
,
819 .channel_destroy
= lttng_channel_destroy
,
820 .packet_avail_size
= NULL
, /* Would be racy anyway */
821 .is_finalized
= lttng_is_finalized
,
822 .is_disabled
= lttng_is_disabled
,
823 .flush_buffer
= lttng_flush_buffer
,
825 .event_reserve
= lttng_event_reserve
,
826 .event_commit
= lttng_event_commit
,
827 .event_write
= lttng_event_write
,
828 .event_strcpy
= lttng_event_strcpy
,
830 .client_config
= &client_config
,
833 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
835 DBG("LTT : ltt ring buffer client \"%s\" init\n",
836 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
837 lttng_transport_register(<tng_relay_transport
);
840 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
842 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
843 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
844 lttng_transport_unregister(<tng_relay_transport
);