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 <ust-events-internal.h>
14 #include <lttng/urcu/pointer.h>
15 #include "ust-bitfield.h"
16 #include "ust-compat.h"
18 #include "context-internal.h"
19 #include "lttng-tracer.h"
20 #include "../libringbuffer/frontend_types.h"
22 #define LTTNG_COMPACT_EVENT_BITS 5
23 #define LTTNG_COMPACT_TSC_BITS 27
31 * Keep the natural field alignment for _each field_ within this structure if
32 * you ever add/remove a field from this header. Packed attribute is not used
33 * because gcc generates poor code on at least powerpc and mips. Don't ever
34 * let gcc add padding between the structure elements.
37 struct packet_header
{
38 /* Trace packet header */
41 * contains endianness information.
43 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
45 uint64_t stream_instance_id
;
48 /* Stream packet context */
49 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
50 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
51 uint64_t content_size
; /* Size of data in subbuffer */
52 uint64_t packet_size
; /* Subbuffer size (include padding) */
53 uint64_t packet_seq_num
; /* Packet sequence number */
54 unsigned long events_discarded
; /*
55 * Events lost in this subbuffer since
56 * the beginning of the trace.
59 uint32_t cpu_id
; /* CPU id associated with stream */
60 uint8_t header_end
; /* End of header */
64 struct lttng_client_ctx
{
65 size_t packet_context_len
;
66 size_t event_context_len
;
67 struct lttng_ust_ctx
*chan_ctx
;
68 struct lttng_ust_ctx
*event_ctx
;
71 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
73 return trace_clock_read64();
77 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ust_ctx
*ctx
,
80 size_t orig_offset
= offset
;
84 offset
+= lttng_ust_lib_ring_buffer_align(offset
, ctx
->largest_align
);
86 return offset
- orig_offset
;
90 void ctx_get_struct_size(struct lttng_ust_ctx
*ctx
, size_t *ctx_len
,
91 enum app_ctx_mode mode
)
96 if (caa_likely(!ctx
)) {
100 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
101 if (mode
== APP_CTX_ENABLED
) {
102 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
104 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
106 * Before UST 2.8, we cannot use the
107 * application context, because we
108 * cannot trust that the handler used
109 * for get_size is the same used for
110 * ctx_record, which would result in
111 * corrupted traces when tracing
112 * concurrently with application context
113 * register/unregister.
115 offset
+= lttng_ust_dummy_get_size(ctx
->fields
[i
], offset
);
117 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
125 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
126 struct lttng_ust_channel_buffer
*chan
,
127 struct lttng_ust_ctx
*ctx
,
128 enum app_ctx_mode mode
)
132 if (caa_likely(!ctx
))
134 lttng_ust_lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
135 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
136 if (mode
== APP_CTX_ENABLED
) {
137 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
139 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
141 * Before UST 2.8, we cannot use the
142 * application context, because we
143 * cannot trust that the handler used
144 * for get_size is the same used for
145 * ctx_record, which would result in
146 * corrupted traces when tracing
147 * concurrently with application context
148 * register/unregister.
150 lttng_ust_dummy_record(ctx
->fields
[i
], bufctx
, chan
);
152 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
159 * record_header_size - Calculate the header size and padding necessary.
160 * @config: ring buffer instance configuration
162 * @offset: offset in the write buffer
163 * @pre_header_padding: padding to add before the header (output)
164 * @ctx: reservation context
166 * Returns the event header size (including padding).
168 * The payload must itself determine its own alignment from the biggest type it
172 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
173 struct lttng_ust_lib_ring_buffer_channel
*chan
,
175 size_t *pre_header_padding
,
176 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
177 struct lttng_client_ctx
*client_ctx
)
179 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
180 size_t orig_offset
= offset
;
183 switch (lttng_chan
->priv
->header_type
) {
184 case 1: /* compact */
185 padding
= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
187 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
188 offset
+= sizeof(uint32_t); /* id and timestamp */
190 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
191 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
192 /* Align extended struct on largest member */
193 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
194 offset
+= sizeof(uint32_t); /* id */
195 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
196 offset
+= sizeof(uint64_t); /* timestamp */
200 padding
= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
202 offset
+= sizeof(uint16_t);
203 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
204 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
205 offset
+= sizeof(uint32_t); /* timestamp */
207 /* Align extended struct on largest member */
208 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
209 offset
+= sizeof(uint32_t); /* id */
210 offset
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
211 offset
+= sizeof(uint64_t); /* timestamp */
218 offset
+= ctx_get_aligned_size(offset
, client_ctx
->chan_ctx
,
219 client_ctx
->packet_context_len
);
220 offset
+= ctx_get_aligned_size(offset
, client_ctx
->event_ctx
,
221 client_ctx
->event_context_len
);
222 *pre_header_padding
= padding
;
223 return offset
- orig_offset
;
226 #include "../libringbuffer/api.h"
227 #include "lttng-rb-clients.h"
230 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
231 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
232 struct lttng_client_ctx
*client_ctx
,
236 * lttng_write_event_header
238 * Writes the event header to the offset (already aligned on 32-bits).
240 * @config: ring buffer instance configuration
241 * @ctx: reservation context
242 * @event_id: event ID
245 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
246 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
247 struct lttng_client_ctx
*client_ctx
,
250 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->chan
);
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 lttng_ust_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
, client_ctx
->event_ctx
, APP_CTX_ENABLED
);
287 lttng_ust_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
);
303 switch (lttng_chan
->priv
->header_type
) {
304 case 1: /* compact */
305 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
306 uint32_t id_time
= 0;
308 bt_bitfield_write(&id_time
, uint32_t,
310 LTTNG_COMPACT_EVENT_BITS
,
312 bt_bitfield_write(&id_time
, uint32_t,
313 LTTNG_COMPACT_EVENT_BITS
,
314 LTTNG_COMPACT_TSC_BITS
,
316 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
319 uint64_t timestamp
= ctx
->tsc
;
321 bt_bitfield_write(&id
, uint8_t,
323 LTTNG_COMPACT_EVENT_BITS
,
325 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
326 /* Align extended struct on largest member */
327 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
328 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
329 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
330 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
335 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
336 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
337 uint16_t id
= event_id
;
339 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
340 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
341 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
344 uint64_t timestamp
= ctx
->tsc
;
346 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
347 /* Align extended struct on largest member */
348 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
349 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
350 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
351 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
358 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
, APP_CTX_ENABLED
);
359 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
, APP_CTX_ENABLED
);
360 lttng_ust_lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
363 static const struct lttng_ust_lib_ring_buffer_config client_config
;
365 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
367 return lib_ring_buffer_clock_read(chan
);
371 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
372 struct lttng_ust_lib_ring_buffer_channel
*chan
,
374 size_t *pre_header_padding
,
375 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
378 return record_header_size(config
, chan
, offset
,
379 pre_header_padding
, ctx
, client_ctx
);
383 * client_packet_header_size - called on buffer-switch to a new sub-buffer
385 * Return header size without padding after the structure. Don't use packed
386 * structure because gcc generates inefficient code on some architectures
389 static size_t client_packet_header_size(void)
391 return offsetof(struct packet_header
, ctx
.header_end
);
394 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
395 unsigned int subbuf_idx
,
396 struct lttng_ust_shm_handle
*handle
)
398 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
399 struct packet_header
*header
=
400 (struct packet_header
*)
401 lib_ring_buffer_offset_address(&buf
->backend
,
402 subbuf_idx
* chan
->backend
.subbuf_size
,
404 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
405 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
410 header
->magic
= CTF_MAGIC_NUMBER
;
411 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
412 header
->stream_id
= lttng_chan
->priv
->id
;
413 header
->stream_instance_id
= buf
->backend
.cpu
;
414 header
->ctx
.timestamp_begin
= tsc
;
415 header
->ctx
.timestamp_end
= 0;
416 header
->ctx
.content_size
= ~0ULL; /* for debugging */
417 header
->ctx
.packet_size
= ~0ULL;
418 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
419 header
->ctx
.events_discarded
= 0;
420 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
424 * offset is assumed to never be 0 here : never deliver a completely empty
425 * subbuffer. data_size is between 1 and subbuf_size.
427 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
428 unsigned int subbuf_idx
, unsigned long data_size
,
429 struct lttng_ust_shm_handle
*handle
)
431 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
432 struct packet_header
*header
=
433 (struct packet_header
*)
434 lib_ring_buffer_offset_address(&buf
->backend
,
435 subbuf_idx
* chan
->backend
.subbuf_size
,
437 unsigned long records_lost
= 0;
442 header
->ctx
.timestamp_end
= tsc
;
443 header
->ctx
.content_size
=
444 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
445 header
->ctx
.packet_size
=
446 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
448 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
449 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
450 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
451 header
->ctx
.events_discarded
= records_lost
;
454 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
455 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
460 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
464 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
465 size_t *offset
, size_t *length
)
467 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
468 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
471 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
472 size_t *offset
, size_t *length
)
474 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
475 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
478 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
479 struct lttng_ust_shm_handle
*handle
)
481 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
484 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
485 struct lttng_ust_lib_ring_buffer_channel
*chan
,
486 uint64_t *timestamp_begin
)
488 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
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_lib_ring_buffer_channel
*chan
,
500 uint64_t *timestamp_end
)
502 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
503 struct packet_header
*header
;
505 header
= client_packet_header(buf
, handle
);
508 *timestamp_end
= header
->ctx
.timestamp_end
;
512 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
513 struct lttng_ust_lib_ring_buffer_channel
*chan
,
514 uint64_t *events_discarded
)
516 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
517 struct packet_header
*header
;
519 header
= client_packet_header(buf
, handle
);
522 *events_discarded
= header
->ctx
.events_discarded
;
526 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
527 struct lttng_ust_lib_ring_buffer_channel
*chan
,
528 uint64_t *content_size
)
530 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
531 struct packet_header
*header
;
533 header
= client_packet_header(buf
, handle
);
536 *content_size
= header
->ctx
.content_size
;
540 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
541 struct lttng_ust_lib_ring_buffer_channel
*chan
,
542 uint64_t *packet_size
)
544 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
545 struct packet_header
*header
;
547 header
= client_packet_header(buf
, handle
);
550 *packet_size
= header
->ctx
.packet_size
;
554 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
555 struct lttng_ust_lib_ring_buffer_channel
*chan
,
558 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
560 *stream_id
= lttng_chan
->priv
->id
;
565 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
566 struct lttng_ust_lib_ring_buffer_channel
*chan
,
569 *ts
= client_ring_buffer_clock_read(chan
);
574 static int client_sequence_number(struct lttng_ust_lib_ring_buffer
*buf
,
575 struct lttng_ust_lib_ring_buffer_channel
*chan
,
578 struct lttng_ust_shm_handle
*handle
= chan
->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_lib_ring_buffer_channel
*chan
,
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
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
681 return lttng_chan_buf
;
684 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
689 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
691 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
692 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
696 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
699 struct lttng_ust_stack_ctx
*lttng_ctx
= ctx
->priv
;
700 struct lttng_ust_event_recorder
*event_recorder
= lttng_ctx
->event_recorder
;
701 struct lttng_ust_channel_buffer
*lttng_chan
= event_recorder
->chan
;
702 struct lttng_client_ctx client_ctx
;
705 ctx
->chan
= lttng_chan
->priv
->rb_chan
;
706 client_ctx
.chan_ctx
= lttng_ust_rcu_dereference(lttng_chan
->priv
->ctx
);
707 client_ctx
.event_ctx
= lttng_ust_rcu_dereference(event_recorder
->priv
->ctx
);
708 /* Compute internal size of context structures. */
709 ctx_get_struct_size(client_ctx
.chan_ctx
, &client_ctx
.packet_context_len
,
711 ctx_get_struct_size(client_ctx
.event_ctx
, &client_ctx
.event_context_len
,
714 if (lib_ring_buffer_nesting_inc(&client_config
) < 0)
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_nesting_dec(&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_nesting_dec(&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
, '#');
767 void lttng_event_pstrcpy_pad(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
768 const char *src
, size_t len
)
770 lib_ring_buffer_pstrcpy(&client_config
, ctx
, src
, len
, '\0');
774 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
776 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
778 return lib_ring_buffer_channel_is_finalized(rb_chan
);
782 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
784 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
786 return lib_ring_buffer_channel_is_disabled(rb_chan
);
790 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
792 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
793 struct lttng_ust_lib_ring_buffer
*buf
;
796 for_each_channel_cpu(cpu
, rb_chan
) {
797 int shm_fd
, wait_fd
, wakeup_fd
;
798 uint64_t memory_map_size
;
800 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
801 cpu
, rb_chan
->handle
, &shm_fd
, &wait_fd
,
802 &wakeup_fd
, &memory_map_size
);
803 lib_ring_buffer_switch(&client_config
, buf
,
804 SWITCH_ACTIVE
, rb_chan
->handle
);
809 static struct lttng_transport lttng_relay_transport
= {
810 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
812 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
813 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
814 .pub
= <tng_relay_transport
.ops
,
815 .channel_create
= _channel_create
,
816 .channel_destroy
= lttng_channel_destroy
,
817 .packet_avail_size
= NULL
, /* Would be racy anyway */
818 .is_finalized
= lttng_is_finalized
,
819 .is_disabled
= lttng_is_disabled
,
820 .flush_buffer
= lttng_flush_buffer
,
822 .event_reserve
= lttng_event_reserve
,
823 .event_commit
= lttng_event_commit
,
824 .event_write
= lttng_event_write
,
825 .event_strcpy
= lttng_event_strcpy
,
826 .event_pstrcpy_pad
= lttng_event_pstrcpy_pad
,
828 .client_config
= &client_config
,
831 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
833 DBG("LTT : ltt ring buffer client \"%s\" init\n",
834 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
835 lttng_transport_register(<tng_relay_transport
);
838 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
840 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
841 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
842 lttng_transport_unregister(<tng_relay_transport
);