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
;
67 struct lttng_ust_ctx
*event_ctx
;
70 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
72 return trace_clock_read64();
76 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ust_ctx
*ctx
,
79 size_t orig_offset
= offset
;
83 offset
+= lib_ring_buffer_align(offset
, ctx
->largest_align
);
85 return offset
- orig_offset
;
89 void ctx_get_struct_size(struct lttng_ust_ctx
*ctx
, size_t *ctx_len
,
90 enum app_ctx_mode mode
)
95 if (caa_likely(!ctx
)) {
99 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
100 if (mode
== APP_CTX_ENABLED
) {
101 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
103 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
105 * Before UST 2.8, we cannot use the
106 * application context, because we
107 * cannot trust that the handler used
108 * for get_size is the same used for
109 * ctx_record, which would result in
110 * corrupted traces when tracing
111 * concurrently with application context
112 * register/unregister.
114 offset
+= lttng_ust_dummy_get_size(ctx
->fields
[i
], offset
);
116 offset
+= ctx
->fields
[i
]->get_size(ctx
->fields
[i
], offset
);
124 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
125 struct lttng_ust_channel_buffer
*chan
,
126 struct lttng_ust_ctx
*ctx
,
127 enum app_ctx_mode mode
)
131 if (caa_likely(!ctx
))
133 lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
134 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
135 if (mode
== APP_CTX_ENABLED
) {
136 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
138 if (lttng_context_is_app(ctx
->fields
[i
]->event_field
->name
)) {
140 * Before UST 2.8, we cannot use the
141 * application context, because we
142 * cannot trust that the handler used
143 * for get_size is the same used for
144 * ctx_record, which would result in
145 * corrupted traces when tracing
146 * concurrently with application context
147 * register/unregister.
149 lttng_ust_dummy_record(ctx
->fields
[i
], bufctx
, chan
);
151 ctx
->fields
[i
]->record(ctx
->fields
[i
], bufctx
, chan
);
158 * record_header_size - Calculate the header size and padding necessary.
159 * @config: ring buffer instance configuration
161 * @offset: offset in the write buffer
162 * @pre_header_padding: padding to add before the header (output)
163 * @ctx: reservation context
165 * Returns the event header size (including padding).
167 * The payload must itself determine its own alignment from the biggest type it
171 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
172 struct lttng_ust_lib_ring_buffer_channel
*chan
,
174 size_t *pre_header_padding
,
175 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
176 struct lttng_client_ctx
*client_ctx
)
178 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
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
, client_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
);
251 if (caa_unlikely(ctx
->rflags
))
254 switch (lttng_chan
->priv
->header_type
) {
255 case 1: /* compact */
257 uint32_t id_time
= 0;
259 bt_bitfield_write(&id_time
, uint32_t,
261 LTTNG_COMPACT_EVENT_BITS
,
263 bt_bitfield_write(&id_time
, uint32_t,
264 LTTNG_COMPACT_EVENT_BITS
,
265 LTTNG_COMPACT_TSC_BITS
,
267 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
272 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
273 uint16_t id
= event_id
;
275 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
276 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
277 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
284 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
, APP_CTX_ENABLED
);
285 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
, APP_CTX_ENABLED
);
286 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
291 lttng_write_event_header_slow(config
, ctx
, client_ctx
, event_id
);
295 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
296 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
297 struct lttng_client_ctx
*client_ctx
,
300 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->chan
);
302 switch (lttng_chan
->priv
->header_type
) {
303 case 1: /* compact */
304 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
305 uint32_t id_time
= 0;
307 bt_bitfield_write(&id_time
, uint32_t,
309 LTTNG_COMPACT_EVENT_BITS
,
311 bt_bitfield_write(&id_time
, uint32_t,
312 LTTNG_COMPACT_EVENT_BITS
,
313 LTTNG_COMPACT_TSC_BITS
,
315 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
318 uint64_t timestamp
= ctx
->tsc
;
320 bt_bitfield_write(&id
, uint8_t,
322 LTTNG_COMPACT_EVENT_BITS
,
324 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
325 /* Align extended struct on largest member */
326 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
327 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
328 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
329 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
334 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
335 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
336 uint16_t id
= event_id
;
338 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
339 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
340 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
343 uint64_t timestamp
= ctx
->tsc
;
345 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
346 /* Align extended struct on largest member */
347 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
348 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
349 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
350 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
357 ctx_record(ctx
, lttng_chan
, client_ctx
->chan_ctx
, APP_CTX_ENABLED
);
358 ctx_record(ctx
, lttng_chan
, client_ctx
->event_ctx
, APP_CTX_ENABLED
);
359 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
362 static const struct lttng_ust_lib_ring_buffer_config client_config
;
364 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
366 return lib_ring_buffer_clock_read(chan
);
370 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
371 struct lttng_ust_lib_ring_buffer_channel
*chan
,
373 size_t *pre_header_padding
,
374 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
377 return record_header_size(config
, chan
, offset
,
378 pre_header_padding
, ctx
, client_ctx
);
382 * client_packet_header_size - called on buffer-switch to a new sub-buffer
384 * Return header size without padding after the structure. Don't use packed
385 * structure because gcc generates inefficient code on some architectures
388 static size_t client_packet_header_size(void)
390 return offsetof(struct packet_header
, ctx
.header_end
);
393 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
394 unsigned int subbuf_idx
,
395 struct lttng_ust_shm_handle
*handle
)
397 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
398 struct packet_header
*header
=
399 (struct packet_header
*)
400 lib_ring_buffer_offset_address(&buf
->backend
,
401 subbuf_idx
* chan
->backend
.subbuf_size
,
403 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
404 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
409 header
->magic
= CTF_MAGIC_NUMBER
;
410 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
411 header
->stream_id
= lttng_chan
->priv
->id
;
412 header
->stream_instance_id
= buf
->backend
.cpu
;
413 header
->ctx
.timestamp_begin
= tsc
;
414 header
->ctx
.timestamp_end
= 0;
415 header
->ctx
.content_size
= ~0ULL; /* for debugging */
416 header
->ctx
.packet_size
= ~0ULL;
417 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
418 header
->ctx
.events_discarded
= 0;
419 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
423 * offset is assumed to never be 0 here : never deliver a completely empty
424 * subbuffer. data_size is between 1 and subbuf_size.
426 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
427 unsigned int subbuf_idx
, unsigned long data_size
,
428 struct lttng_ust_shm_handle
*handle
)
430 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
431 struct packet_header
*header
=
432 (struct packet_header
*)
433 lib_ring_buffer_offset_address(&buf
->backend
,
434 subbuf_idx
* chan
->backend
.subbuf_size
,
436 unsigned long records_lost
= 0;
441 header
->ctx
.timestamp_end
= tsc
;
442 header
->ctx
.content_size
=
443 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
444 header
->ctx
.packet_size
=
445 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
447 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
448 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
449 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
450 header
->ctx
.events_discarded
= records_lost
;
453 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
454 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
459 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
463 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
464 size_t *offset
, size_t *length
)
466 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
467 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
470 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
471 size_t *offset
, size_t *length
)
473 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
474 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
477 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
478 struct lttng_ust_shm_handle
*handle
)
480 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
483 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
484 struct lttng_ust_shm_handle
*handle
,
485 uint64_t *timestamp_begin
)
487 struct packet_header
*header
;
489 header
= client_packet_header(buf
, handle
);
492 *timestamp_begin
= header
->ctx
.timestamp_begin
;
496 static int client_timestamp_end(struct lttng_ust_lib_ring_buffer
*buf
,
497 struct lttng_ust_shm_handle
*handle
,
498 uint64_t *timestamp_end
)
500 struct packet_header
*header
;
502 header
= client_packet_header(buf
, handle
);
505 *timestamp_end
= header
->ctx
.timestamp_end
;
509 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
510 struct lttng_ust_shm_handle
*handle
,
511 uint64_t *events_discarded
)
513 struct packet_header
*header
;
515 header
= client_packet_header(buf
, handle
);
518 *events_discarded
= header
->ctx
.events_discarded
;
522 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
523 struct lttng_ust_shm_handle
*handle
,
524 uint64_t *content_size
)
526 struct packet_header
*header
;
528 header
= client_packet_header(buf
, handle
);
531 *content_size
= header
->ctx
.content_size
;
535 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
536 struct lttng_ust_shm_handle
*handle
,
537 uint64_t *packet_size
)
539 struct packet_header
*header
;
541 header
= client_packet_header(buf
, handle
);
544 *packet_size
= header
->ctx
.packet_size
;
548 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
549 struct lttng_ust_shm_handle
*handle
,
552 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
,
554 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
556 *stream_id
= lttng_chan
->priv
->id
;
561 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
562 struct lttng_ust_shm_handle
*handle
,
565 struct lttng_ust_lib_ring_buffer_channel
*chan
;
567 chan
= shmp(handle
, handle
->chan
);
568 *ts
= client_ring_buffer_clock_read(chan
);
573 static int client_sequence_number(struct lttng_ust_lib_ring_buffer
*buf
,
574 struct lttng_ust_shm_handle
*handle
,
577 struct packet_header
*header
;
579 header
= client_packet_header(buf
, handle
);
582 *seq
= header
->ctx
.packet_seq_num
;
586 static int client_instance_id(struct lttng_ust_lib_ring_buffer
*buf
,
587 struct lttng_ust_shm_handle
*handle
,
590 *id
= buf
->backend
.cpu
;
596 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
598 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
599 .record_header_size
= client_record_header_size
,
600 .subbuffer_header_size
= client_packet_header_size
,
601 .buffer_begin
= client_buffer_begin
,
602 .buffer_end
= client_buffer_end
,
603 .buffer_create
= client_buffer_create
,
604 .buffer_finalize
= client_buffer_finalize
,
605 .content_size_field
= client_content_size_field
,
606 .packet_size_field
= client_packet_size_field
,
608 .timestamp_begin
= client_timestamp_begin
,
609 .timestamp_end
= client_timestamp_end
,
610 .events_discarded
= client_events_discarded
,
611 .content_size
= client_content_size
,
612 .packet_size
= client_packet_size
,
613 .stream_id
= client_stream_id
,
614 .current_timestamp
= client_current_timestamp
,
615 .sequence_number
= client_sequence_number
,
616 .instance_id
= client_instance_id
,
619 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
620 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
621 .cb
.record_header_size
= client_record_header_size
,
622 .cb
.subbuffer_header_size
= client_packet_header_size
,
623 .cb
.buffer_begin
= client_buffer_begin
,
624 .cb
.buffer_end
= client_buffer_end
,
625 .cb
.buffer_create
= client_buffer_create
,
626 .cb
.buffer_finalize
= client_buffer_finalize
,
627 .cb
.content_size_field
= client_content_size_field
,
628 .cb
.packet_size_field
= client_packet_size_field
,
630 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
631 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
632 .sync
= RING_BUFFER_SYNC_GLOBAL
,
633 .mode
= RING_BUFFER_MODE_TEMPLATE
,
634 .backend
= RING_BUFFER_PAGE
,
635 .output
= RING_BUFFER_MMAP
,
636 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
637 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
638 .wakeup
= LTTNG_CLIENT_WAKEUP
,
639 .client_type
= LTTNG_CLIENT_TYPE
,
641 .cb_ptr
= &client_cb
.parent
,
645 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
647 size_t subbuf_size
, size_t num_subbuf
,
648 unsigned int switch_timer_interval
,
649 unsigned int read_timer_interval
,
652 const int *stream_fds
, int nr_stream_fds
,
653 int64_t blocking_timeout
)
655 struct lttng_ust_abi_channel_config chan_priv_init
;
656 struct lttng_ust_shm_handle
*handle
;
657 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
659 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
662 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
663 lttng_chan_buf
->priv
->id
= chan_id
;
665 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
666 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
667 chan_priv_init
.id
= chan_id
;
669 handle
= channel_create(&client_config
, name
,
670 __alignof__(struct lttng_ust_abi_channel_config
),
671 sizeof(struct lttng_ust_abi_channel_config
),
673 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
674 switch_timer_interval
, read_timer_interval
,
675 stream_fds
, nr_stream_fds
, blocking_timeout
);
678 lttng_chan_buf
->handle
= handle
;
679 lttng_chan_buf
->chan
= shmp(handle
, handle
->chan
);
680 return lttng_chan_buf
;
683 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
688 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
690 channel_destroy(lttng_chan_buf
->chan
, lttng_chan_buf
->handle
, 1);
691 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
695 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
698 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(ctx
->chan
);
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_client_ctx client_ctx
;
704 client_ctx
.chan_ctx
= lttng_ust_rcu_dereference(lttng_chan
->priv
->ctx
);
705 client_ctx
.event_ctx
= lttng_ust_rcu_dereference(event_recorder
->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(client_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
, '#');
767 void lttng_event_strcpy_pad(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
768 const char *src
, size_t len
)
770 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '\0');
775 wait_queue_head_t
*lttng_get_reader_wait_queue(struct lttng_ust_lib_ring_buffer_channel
*chan
)
777 return &chan
->read_wait
;
781 wait_queue_head_t
*lttng_get_hp_wait_queue(struct lttng_ust_lib_ring_buffer_channel
*chan
)
783 return &chan
->hp_wait
;
788 int lttng_is_finalized(struct lttng_ust_lib_ring_buffer_channel
*chan
)
790 return lib_ring_buffer_channel_is_finalized(chan
);
794 int lttng_is_disabled(struct lttng_ust_lib_ring_buffer_channel
*chan
)
796 return lib_ring_buffer_channel_is_disabled(chan
);
800 int lttng_flush_buffer(struct lttng_ust_lib_ring_buffer_channel
*chan
,
801 struct lttng_ust_shm_handle
*handle
)
803 struct lttng_ust_lib_ring_buffer
*buf
;
806 for_each_channel_cpu(cpu
, chan
) {
807 int shm_fd
, wait_fd
, wakeup_fd
;
808 uint64_t memory_map_size
;
810 buf
= channel_get_ring_buffer(&client_config
, chan
,
811 cpu
, handle
, &shm_fd
, &wait_fd
,
812 &wakeup_fd
, &memory_map_size
);
813 lib_ring_buffer_switch(&client_config
, buf
,
814 SWITCH_ACTIVE
, handle
);
819 static struct lttng_transport lttng_relay_transport
= {
820 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
822 .struct_size
= sizeof(struct lttng_ust_channel_ops
),
823 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_ops_private
, {
824 .pub
= <tng_relay_transport
.ops
,
825 .channel_create
= _channel_create
,
826 .channel_destroy
= lttng_channel_destroy
,
827 .packet_avail_size
= NULL
, /* Would be racy anyway */
828 .is_finalized
= lttng_is_finalized
,
829 .is_disabled
= lttng_is_disabled
,
830 .flush_buffer
= lttng_flush_buffer
,
832 .event_reserve
= lttng_event_reserve
,
833 .event_commit
= lttng_event_commit
,
834 .event_write
= lttng_event_write
,
835 .event_strcpy
= lttng_event_strcpy
,
836 .event_strcpy_pad
= lttng_event_strcpy_pad
,
838 .client_config
= &client_config
,
841 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
843 DBG("LTT : ltt ring buffer client \"%s\" init\n",
844 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
845 lttng_transport_register(<tng_relay_transport
);
848 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
850 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
851 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
852 lttng_transport_unregister(<tng_relay_transport
);