2 * lttng-ring-buffer-client.h
4 * LTTng lib ring buffer client template.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lttng/ust-events.h>
25 #include "lttng/bitfield.h"
27 #include "lttng-tracer.h"
28 #include "../libringbuffer/frontend_types.h"
30 #define LTTNG_COMPACT_EVENT_BITS 5
31 #define LTTNG_COMPACT_TSC_BITS 27
39 * Keep the natural field alignment for _each field_ within this structure if
40 * you ever add/remove a field from this header. Packed attribute is not used
41 * because gcc generates poor code on at least powerpc and mips. Don't ever
42 * let gcc add padding between the structure elements.
45 struct packet_header
{
46 /* Trace packet header */
49 * contains endianness information.
51 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
53 uint64_t stream_instance_id
;
56 /* Stream packet context */
57 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
58 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
59 uint64_t content_size
; /* Size of data in subbuffer */
60 uint64_t packet_size
; /* Subbuffer size (include padding) */
61 uint64_t packet_seq_num
; /* Packet sequence number */
62 unsigned long events_discarded
; /*
63 * Events lost in this subbuffer since
64 * the beginning of the trace.
67 uint32_t cpu_id
; /* CPU id associated with stream */
68 uint8_t header_end
; /* End of header */
72 struct lttng_client_ctx
{
73 size_t packet_context_len
;
74 size_t event_context_len
;
77 static inline uint64_t lib_ring_buffer_clock_read(struct channel
*chan
)
79 return trace_clock_read64();
83 size_t ctx_get_aligned_size(size_t offset
, struct lttng_ctx
*ctx
,
86 size_t orig_offset
= offset
;
90 offset
+= lib_ring_buffer_align(offset
, ctx
->largest_align
);
92 return offset
- orig_offset
;
96 void ctx_get_struct_size(struct lttng_ctx
*ctx
, size_t *ctx_len
,
97 enum app_ctx_mode mode
)
102 if (caa_likely(!ctx
)) {
106 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
107 if (mode
== APP_CTX_ENABLED
) {
108 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
110 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
112 * Before UST 2.8, we cannot use the
113 * application context, because we
114 * cannot trust that the handler used
115 * for get_size is the same used for
116 * ctx_record, which would result in
117 * corrupted traces when tracing
118 * concurrently with application context
119 * register/unregister.
121 offset
+= lttng_ust_dummy_get_size(&ctx
->fields
[i
], offset
);
123 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
131 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
132 struct lttng_channel
*chan
,
133 struct lttng_ctx
*ctx
,
134 enum app_ctx_mode mode
)
138 if (caa_likely(!ctx
))
140 lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
141 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
142 if (mode
== APP_CTX_ENABLED
) {
143 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
145 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
147 * Before UST 2.8, we cannot use the
148 * application context, because we
149 * cannot trust that the handler used
150 * for get_size is the same used for
151 * ctx_record, which would result in
152 * corrupted traces when tracing
153 * concurrently with application context
154 * register/unregister.
156 lttng_ust_dummy_record(&ctx
->fields
[i
], bufctx
, chan
);
158 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
165 * record_header_size - Calculate the header size and padding necessary.
166 * @config: ring buffer instance configuration
168 * @offset: offset in the write buffer
169 * @pre_header_padding: padding to add before the header (output)
170 * @ctx: reservation context
172 * Returns the event header size (including padding).
174 * The payload must itself determine its own alignment from the biggest type it
178 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
179 struct channel
*chan
, size_t offset
,
180 size_t *pre_header_padding
,
181 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
182 struct lttng_client_ctx
*client_ctx
)
184 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
185 struct lttng_event
*event
= ctx
->priv
;
186 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
187 size_t orig_offset
= offset
;
190 switch (lttng_chan
->header_type
) {
191 case 1: /* compact */
192 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
194 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
195 offset
+= sizeof(uint32_t); /* id and timestamp */
197 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
198 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
199 /* Align extended struct on largest member */
200 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
201 offset
+= sizeof(uint32_t); /* id */
202 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
203 offset
+= sizeof(uint64_t); /* timestamp */
207 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
209 offset
+= sizeof(uint16_t);
210 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
211 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
212 offset
+= sizeof(uint32_t); /* timestamp */
214 /* Align extended struct on largest member */
215 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
216 offset
+= sizeof(uint32_t); /* id */
217 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
218 offset
+= sizeof(uint64_t); /* timestamp */
226 /* 2.8+ probe ABI. */
227 offset
+= ctx_get_aligned_size(offset
, lttng_ctx
->chan_ctx
,
228 client_ctx
->packet_context_len
);
229 offset
+= ctx_get_aligned_size(offset
, lttng_ctx
->event_ctx
,
230 client_ctx
->event_context_len
);
232 /* Pre 2.8 probe ABI. */
233 offset
+= ctx_get_aligned_size(offset
, lttng_chan
->ctx
,
234 client_ctx
->packet_context_len
);
235 offset
+= ctx_get_aligned_size(offset
, event
->ctx
,
236 client_ctx
->event_context_len
);
238 *pre_header_padding
= padding
;
239 return offset
- orig_offset
;
242 #include "../libringbuffer/api.h"
243 #include "lttng-rb-clients.h"
246 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
247 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
251 * lttng_write_event_header
253 * Writes the event header to the offset (already aligned on 32-bits).
255 * @config: ring buffer instance configuration
256 * @ctx: reservation context
257 * @event_id: event ID
260 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
261 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
264 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
265 struct lttng_event
*event
= ctx
->priv
;
266 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
268 if (caa_unlikely(ctx
->rflags
))
271 switch (lttng_chan
->header_type
) {
272 case 1: /* compact */
274 uint32_t id_time
= 0;
276 bt_bitfield_write(&id_time
, uint32_t,
278 LTTNG_COMPACT_EVENT_BITS
,
280 bt_bitfield_write(&id_time
, uint32_t,
281 LTTNG_COMPACT_EVENT_BITS
,
282 LTTNG_COMPACT_TSC_BITS
,
284 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
289 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
290 uint16_t id
= event_id
;
292 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
293 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
294 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
302 /* 2.8+ probe ABI. */
303 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
304 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
306 /* Pre 2.8 probe ABI. */
307 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
308 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
310 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
315 lttng_write_event_header_slow(config
, ctx
, event_id
);
319 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
320 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
323 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
324 struct lttng_event
*event
= ctx
->priv
;
325 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
327 switch (lttng_chan
->header_type
) {
328 case 1: /* compact */
329 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
330 uint32_t id_time
= 0;
332 bt_bitfield_write(&id_time
, uint32_t,
334 LTTNG_COMPACT_EVENT_BITS
,
336 bt_bitfield_write(&id_time
, uint32_t,
337 LTTNG_COMPACT_EVENT_BITS
,
338 LTTNG_COMPACT_TSC_BITS
,
340 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
343 uint64_t timestamp
= ctx
->tsc
;
345 bt_bitfield_write(&id
, uint8_t,
347 LTTNG_COMPACT_EVENT_BITS
,
349 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
350 /* Align extended struct on largest member */
351 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
352 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
353 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
354 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
359 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
360 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
361 uint16_t id
= event_id
;
363 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
364 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
365 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
368 uint64_t timestamp
= ctx
->tsc
;
370 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
371 /* Align extended struct on largest member */
372 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
373 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
374 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
375 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
383 /* 2.8+ probe ABI. */
384 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
385 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
387 /* Pre 2.8 probe ABI. */
388 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
389 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
391 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
394 static const struct lttng_ust_lib_ring_buffer_config client_config
;
396 static uint64_t client_ring_buffer_clock_read(struct channel
*chan
)
398 return lib_ring_buffer_clock_read(chan
);
402 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
403 struct channel
*chan
, size_t offset
,
404 size_t *pre_header_padding
,
405 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
408 return record_header_size(config
, chan
, offset
,
409 pre_header_padding
, ctx
, client_ctx
);
413 * client_packet_header_size - called on buffer-switch to a new sub-buffer
415 * Return header size without padding after the structure. Don't use packed
416 * structure because gcc generates inefficient code on some architectures
419 static size_t client_packet_header_size(void)
421 return offsetof(struct packet_header
, ctx
.header_end
);
424 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
425 unsigned int subbuf_idx
,
426 struct lttng_ust_shm_handle
*handle
)
428 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
429 struct packet_header
*header
=
430 (struct packet_header
*)
431 lib_ring_buffer_offset_address(&buf
->backend
,
432 subbuf_idx
* chan
->backend
.subbuf_size
,
434 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
435 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
440 header
->magic
= CTF_MAGIC_NUMBER
;
441 memcpy(header
->uuid
, lttng_chan
->uuid
, sizeof(lttng_chan
->uuid
));
442 header
->stream_id
= lttng_chan
->id
;
443 header
->stream_instance_id
= buf
->backend
.cpu
;
444 header
->ctx
.timestamp_begin
= tsc
;
445 header
->ctx
.timestamp_end
= 0;
446 header
->ctx
.content_size
= ~0ULL; /* for debugging */
447 header
->ctx
.packet_size
= ~0ULL;
448 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
449 header
->ctx
.events_discarded
= 0;
450 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
454 * offset is assumed to never be 0 here : never deliver a completely empty
455 * subbuffer. data_size is between 1 and subbuf_size.
457 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
458 unsigned int subbuf_idx
, unsigned long data_size
,
459 struct lttng_ust_shm_handle
*handle
)
461 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
462 struct packet_header
*header
=
463 (struct packet_header
*)
464 lib_ring_buffer_offset_address(&buf
->backend
,
465 subbuf_idx
* chan
->backend
.subbuf_size
,
467 unsigned long records_lost
= 0;
472 header
->ctx
.timestamp_end
= tsc
;
473 header
->ctx
.content_size
=
474 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
475 header
->ctx
.packet_size
=
476 (uint64_t) PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
478 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
479 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
480 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
481 header
->ctx
.events_discarded
= records_lost
;
484 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
485 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
490 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
494 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
495 size_t *offset
, size_t *length
)
497 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
498 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
501 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
502 size_t *offset
, size_t *length
)
504 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
505 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
508 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
509 struct lttng_ust_shm_handle
*handle
)
511 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
514 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
515 struct lttng_ust_shm_handle
*handle
,
516 uint64_t *timestamp_begin
)
518 struct packet_header
*header
;
520 header
= client_packet_header(buf
, handle
);
523 *timestamp_begin
= header
->ctx
.timestamp_begin
;
527 static int client_timestamp_end(struct lttng_ust_lib_ring_buffer
*buf
,
528 struct lttng_ust_shm_handle
*handle
,
529 uint64_t *timestamp_end
)
531 struct packet_header
*header
;
533 header
= client_packet_header(buf
, handle
);
536 *timestamp_end
= header
->ctx
.timestamp_end
;
540 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
541 struct lttng_ust_shm_handle
*handle
,
542 uint64_t *events_discarded
)
544 struct packet_header
*header
;
546 header
= client_packet_header(buf
, handle
);
549 *events_discarded
= header
->ctx
.events_discarded
;
553 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
554 struct lttng_ust_shm_handle
*handle
,
555 uint64_t *content_size
)
557 struct packet_header
*header
;
559 header
= client_packet_header(buf
, handle
);
562 *content_size
= header
->ctx
.content_size
;
566 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
567 struct lttng_ust_shm_handle
*handle
,
568 uint64_t *packet_size
)
570 struct packet_header
*header
;
572 header
= client_packet_header(buf
, handle
);
575 *packet_size
= header
->ctx
.packet_size
;
579 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
580 struct lttng_ust_shm_handle
*handle
,
583 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
584 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
586 *stream_id
= lttng_chan
->id
;
591 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
592 struct lttng_ust_shm_handle
*handle
,
595 struct channel
*chan
;
597 chan
= shmp(handle
, handle
->chan
);
598 *ts
= client_ring_buffer_clock_read(chan
);
603 static int client_sequence_number(struct lttng_ust_lib_ring_buffer
*buf
,
604 struct lttng_ust_shm_handle
*handle
,
607 struct packet_header
*header
;
609 header
= client_packet_header(buf
, handle
);
610 *seq
= header
->ctx
.packet_seq_num
;
614 static int client_instance_id(struct lttng_ust_lib_ring_buffer
*buf
,
615 struct lttng_ust_shm_handle
*handle
,
618 *id
= buf
->backend
.cpu
;
624 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
626 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
627 .record_header_size
= client_record_header_size
,
628 .subbuffer_header_size
= client_packet_header_size
,
629 .buffer_begin
= client_buffer_begin
,
630 .buffer_end
= client_buffer_end
,
631 .buffer_create
= client_buffer_create
,
632 .buffer_finalize
= client_buffer_finalize
,
633 .content_size_field
= client_content_size_field
,
634 .packet_size_field
= client_packet_size_field
,
636 .timestamp_begin
= client_timestamp_begin
,
637 .timestamp_end
= client_timestamp_end
,
638 .events_discarded
= client_events_discarded
,
639 .content_size
= client_content_size
,
640 .packet_size
= client_packet_size
,
641 .stream_id
= client_stream_id
,
642 .current_timestamp
= client_current_timestamp
,
643 .sequence_number
= client_sequence_number
,
644 .instance_id
= client_instance_id
,
647 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
648 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
649 .cb
.record_header_size
= client_record_header_size
,
650 .cb
.subbuffer_header_size
= client_packet_header_size
,
651 .cb
.buffer_begin
= client_buffer_begin
,
652 .cb
.buffer_end
= client_buffer_end
,
653 .cb
.buffer_create
= client_buffer_create
,
654 .cb
.buffer_finalize
= client_buffer_finalize
,
655 .cb
.content_size_field
= client_content_size_field
,
656 .cb
.packet_size_field
= client_packet_size_field
,
658 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
659 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
660 .sync
= RING_BUFFER_SYNC_GLOBAL
,
661 .mode
= RING_BUFFER_MODE_TEMPLATE
,
662 .backend
= RING_BUFFER_PAGE
,
663 .output
= RING_BUFFER_MMAP
,
664 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
665 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
666 .wakeup
= LTTNG_CLIENT_WAKEUP
,
667 .client_type
= LTTNG_CLIENT_TYPE
,
669 .cb_ptr
= &client_cb
.parent
,
672 const struct lttng_ust_client_lib_ring_buffer_client_cb
*LTTNG_CLIENT_CALLBACKS
= &client_cb
;
675 struct lttng_channel
*_channel_create(const char *name
,
677 size_t subbuf_size
, size_t num_subbuf
,
678 unsigned int switch_timer_interval
,
679 unsigned int read_timer_interval
,
682 const int *stream_fds
, int nr_stream_fds
,
683 int64_t blocking_timeout
)
685 struct lttng_channel chan_priv_init
;
686 struct lttng_ust_shm_handle
*handle
;
687 struct lttng_channel
*lttng_chan
;
690 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
691 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
692 chan_priv_init
.id
= chan_id
;
693 handle
= channel_create(&client_config
, name
,
694 &priv
, __alignof__(struct lttng_channel
),
695 sizeof(struct lttng_channel
),
697 buf_addr
, subbuf_size
, num_subbuf
,
698 switch_timer_interval
, read_timer_interval
,
699 stream_fds
, nr_stream_fds
, blocking_timeout
);
703 lttng_chan
->handle
= handle
;
704 lttng_chan
->chan
= shmp(handle
, handle
->chan
);
709 void lttng_channel_destroy(struct lttng_channel
*chan
)
711 channel_destroy(chan
->chan
, chan
->handle
, 1);
715 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
718 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
719 struct lttng_event
*event
= ctx
->priv
;
720 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
721 struct lttng_client_ctx client_ctx
;
724 /* Compute internal size of context structures. */
727 /* 2.8+ probe ABI. */
728 ctx_get_struct_size(lttng_ctx
->chan_ctx
, &client_ctx
.packet_context_len
,
730 ctx_get_struct_size(lttng_ctx
->event_ctx
, &client_ctx
.event_context_len
,
733 /* Pre 2.8 probe ABI. */
734 ctx_get_struct_size(lttng_chan
->ctx
, &client_ctx
.packet_context_len
,
736 ctx_get_struct_size(event
->ctx
, &client_ctx
.event_context_len
,
740 cpu
= lib_ring_buffer_get_cpu(&client_config
);
745 switch (lttng_chan
->header_type
) {
746 case 1: /* compact */
748 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
751 if (event_id
> 65534)
752 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
758 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, &client_ctx
);
759 if (caa_unlikely(ret
))
761 if (caa_likely(ctx
->ctx_len
762 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx
))) {
763 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
764 &ctx
->backend_pages
)) {
769 lttng_write_event_header(&client_config
, ctx
, event_id
);
772 lib_ring_buffer_put_cpu(&client_config
);
777 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
779 lib_ring_buffer_commit(&client_config
, ctx
);
780 lib_ring_buffer_put_cpu(&client_config
);
784 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
787 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
791 void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const char *src
,
794 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
799 wait_queue_head_t
*lttng_get_reader_wait_queue(struct channel
*chan
)
801 return &chan
->read_wait
;
805 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
807 return &chan
->hp_wait
;
812 int lttng_is_finalized(struct channel
*chan
)
814 return lib_ring_buffer_channel_is_finalized(chan
);
818 int lttng_is_disabled(struct channel
*chan
)
820 return lib_ring_buffer_channel_is_disabled(chan
);
824 int lttng_flush_buffer(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
826 struct lttng_ust_lib_ring_buffer
*buf
;
829 for_each_channel_cpu(cpu
, chan
) {
830 int shm_fd
, wait_fd
, wakeup_fd
;
831 uint64_t memory_map_size
;
833 buf
= channel_get_ring_buffer(&client_config
, chan
,
834 cpu
, handle
, &shm_fd
, &wait_fd
,
835 &wakeup_fd
, &memory_map_size
);
836 lib_ring_buffer_switch(&client_config
, buf
,
837 SWITCH_ACTIVE
, handle
);
842 static struct lttng_transport lttng_relay_transport
= {
843 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
845 .channel_create
= _channel_create
,
846 .channel_destroy
= lttng_channel_destroy
,
848 .event_reserve
= lttng_event_reserve
,
849 .event_commit
= lttng_event_commit
,
850 .event_write
= lttng_event_write
,
851 .packet_avail_size
= NULL
, /* Would be racy anyway */
852 //.get_reader_wait_queue = lttng_get_reader_wait_queue,
853 //.get_hp_wait_queue = lttng_get_hp_wait_queue,
854 .is_finalized
= lttng_is_finalized
,
855 .is_disabled
= lttng_is_disabled
,
856 .flush_buffer
= lttng_flush_buffer
,
857 .event_strcpy
= lttng_event_strcpy
,
859 .client_config
= &client_config
,
862 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
864 DBG("LTT : ltt ring buffer client \"%s\" init\n",
865 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
866 lttng_transport_register(<tng_relay_transport
);
869 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
871 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
872 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
873 lttng_transport_unregister(<tng_relay_transport
);