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"
29 #include "../libringbuffer/rseq.h"
31 #define LTTNG_COMPACT_EVENT_BITS 5
32 #define LTTNG_COMPACT_TSC_BITS 27
40 * Keep the natural field alignment for _each field_ within this structure if
41 * you ever add/remove a field from this header. Packed attribute is not used
42 * because gcc generates poor code on at least powerpc and mips. Don't ever
43 * let gcc add padding between the structure elements.
46 struct packet_header
{
47 /* Trace packet header */
50 * contains endianness information.
52 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
54 uint64_t stream_instance_id
;
57 /* Stream packet context */
58 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
59 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
60 uint64_t content_size
; /* Size of data in subbuffer */
61 uint64_t packet_size
; /* Subbuffer size (include padding) */
62 uint64_t packet_seq_num
; /* Packet sequence number */
63 unsigned long events_discarded
; /*
64 * Events lost in this subbuffer since
65 * the beginning of the trace.
68 uint32_t cpu_id
; /* CPU id associated with stream */
69 uint8_t header_end
; /* End of header */
74 static inline uint64_t lib_ring_buffer_clock_read(struct channel
*chan
)
76 return trace_clock_read64();
80 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
,
81 enum app_ctx_mode mode
)
84 size_t orig_offset
= offset
;
88 offset
+= lib_ring_buffer_align(offset
, ctx
->largest_align
);
89 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
90 if (mode
== APP_CTX_ENABLED
) {
91 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
93 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
95 * Before UST 2.8, we cannot use the
96 * application context, because we
97 * cannot trust that the handler used
98 * for get_size is the same used for
99 * ctx_record, which would result in
100 * corrupted traces when tracing
101 * concurrently with application context
102 * register/unregister.
104 offset
+= lttng_ust_dummy_get_size(&ctx
->fields
[i
], offset
);
106 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
110 return offset
- orig_offset
;
114 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
115 struct lttng_channel
*chan
,
116 struct lttng_ctx
*ctx
,
117 enum app_ctx_mode mode
)
121 if (caa_likely(!ctx
))
123 lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
124 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
125 if (mode
== APP_CTX_ENABLED
) {
126 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
128 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
130 * Before UST 2.8, we cannot use the
131 * application context, because we
132 * cannot trust that the handler used
133 * for get_size is the same used for
134 * ctx_record, which would result in
135 * corrupted traces when tracing
136 * concurrently with application context
137 * register/unregister.
139 lttng_ust_dummy_record(&ctx
->fields
[i
], bufctx
, chan
);
141 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
148 * record_header_size - Calculate the header size and padding necessary.
149 * @config: ring buffer instance configuration
151 * @offset: offset in the write buffer
152 * @pre_header_padding: padding to add before the header (output)
153 * @ctx: reservation context
155 * Returns the event header size (including padding).
157 * The payload must itself determine its own alignment from the biggest type it
161 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
162 struct channel
*chan
, size_t offset
,
163 size_t *pre_header_padding
,
164 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
166 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
167 struct lttng_event
*event
= ctx
->priv
;
168 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
169 size_t orig_offset
= offset
;
172 switch (lttng_chan
->header_type
) {
173 case 1: /* compact */
174 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
176 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
177 offset
+= sizeof(uint32_t); /* id and timestamp */
179 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
180 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
181 /* Align extended struct on largest member */
182 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
183 offset
+= sizeof(uint32_t); /* id */
184 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
185 offset
+= sizeof(uint64_t); /* timestamp */
189 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
191 offset
+= sizeof(uint16_t);
192 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
193 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
194 offset
+= sizeof(uint32_t); /* timestamp */
196 /* Align extended struct on largest member */
197 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
198 offset
+= sizeof(uint32_t); /* id */
199 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
200 offset
+= sizeof(uint64_t); /* timestamp */
208 /* 2.8+ probe ABI. */
209 offset
+= ctx_get_size(offset
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
210 offset
+= ctx_get_size(offset
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
212 /* Pre 2.8 probe ABI. */
213 offset
+= ctx_get_size(offset
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
214 offset
+= ctx_get_size(offset
, event
->ctx
, APP_CTX_DISABLED
);
216 *pre_header_padding
= padding
;
217 return offset
- orig_offset
;
220 #include "../libringbuffer/api.h"
221 #include "lttng-rb-clients.h"
224 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
225 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
229 * lttng_write_event_header
231 * Writes the event header to the offset (already aligned on 32-bits).
233 * @config: ring buffer instance configuration
234 * @ctx: reservation context
235 * @event_id: event ID
238 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
239 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
242 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
243 struct lttng_event
*event
= ctx
->priv
;
244 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
246 if (caa_unlikely(ctx
->rflags
))
249 switch (lttng_chan
->header_type
) {
250 case 1: /* compact */
252 uint32_t id_time
= 0;
254 bt_bitfield_write(&id_time
, uint32_t,
256 LTTNG_COMPACT_EVENT_BITS
,
258 bt_bitfield_write(&id_time
, uint32_t,
259 LTTNG_COMPACT_EVENT_BITS
,
260 LTTNG_COMPACT_TSC_BITS
,
262 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
267 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
268 uint16_t id
= event_id
;
270 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
271 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
272 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
280 /* 2.8+ probe ABI. */
281 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
282 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
284 /* Pre 2.8 probe ABI. */
285 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
286 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
288 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
293 lttng_write_event_header_slow(config
, ctx
, event_id
);
297 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
298 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
301 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
302 struct lttng_event
*event
= ctx
->priv
;
303 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
305 switch (lttng_chan
->header_type
) {
306 case 1: /* compact */
307 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
308 uint32_t id_time
= 0;
310 bt_bitfield_write(&id_time
, uint32_t,
312 LTTNG_COMPACT_EVENT_BITS
,
314 bt_bitfield_write(&id_time
, uint32_t,
315 LTTNG_COMPACT_EVENT_BITS
,
316 LTTNG_COMPACT_TSC_BITS
,
318 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
321 uint64_t timestamp
= ctx
->tsc
;
323 bt_bitfield_write(&id
, uint8_t,
325 LTTNG_COMPACT_EVENT_BITS
,
327 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
328 /* Align extended struct on largest member */
329 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
330 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
331 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
332 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
337 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
338 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
339 uint16_t id
= event_id
;
341 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
342 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
343 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
346 uint64_t timestamp
= ctx
->tsc
;
348 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
349 /* Align extended struct on largest member */
350 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
351 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
352 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
353 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
361 /* 2.8+ probe ABI. */
362 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
363 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
365 /* Pre 2.8 probe ABI. */
366 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
367 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
369 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
372 static const struct lttng_ust_lib_ring_buffer_config client_config
;
374 static uint64_t client_ring_buffer_clock_read(struct channel
*chan
)
376 return lib_ring_buffer_clock_read(chan
);
380 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
381 struct channel
*chan
, size_t offset
,
382 size_t *pre_header_padding
,
383 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
385 return record_header_size(config
, chan
, offset
,
386 pre_header_padding
, ctx
);
390 * client_packet_header_size - called on buffer-switch to a new sub-buffer
392 * Return header size without padding after the structure. Don't use packed
393 * structure because gcc generates inefficient code on some architectures
396 static size_t client_packet_header_size(void)
398 return offsetof(struct packet_header
, ctx
.header_end
);
401 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
402 unsigned int subbuf_idx
,
403 struct lttng_ust_shm_handle
*handle
)
405 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
406 struct packet_header
*header
=
407 (struct packet_header
*)
408 lib_ring_buffer_offset_address(&buf
->backend
,
409 subbuf_idx
* chan
->backend
.subbuf_size
,
411 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
412 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
417 header
->magic
= CTF_MAGIC_NUMBER
;
418 memcpy(header
->uuid
, lttng_chan
->uuid
, sizeof(lttng_chan
->uuid
));
419 header
->stream_id
= lttng_chan
->id
;
420 header
->stream_instance_id
= buf
->backend
.cpu
;
421 header
->ctx
.timestamp_begin
= tsc
;
422 header
->ctx
.timestamp_end
= 0;
423 header
->ctx
.content_size
= ~0ULL; /* for debugging */
424 header
->ctx
.packet_size
= ~0ULL;
425 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
426 header
->ctx
.events_discarded
= 0;
427 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
431 * offset is assumed to never be 0 here : never deliver a completely empty
432 * subbuffer. data_size is between 1 and subbuf_size.
434 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
435 unsigned int subbuf_idx
, unsigned long data_size
,
436 struct lttng_ust_shm_handle
*handle
)
438 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
439 struct packet_header
*header
=
440 (struct packet_header
*)
441 lib_ring_buffer_offset_address(&buf
->backend
,
442 subbuf_idx
* chan
->backend
.subbuf_size
,
444 unsigned long records_lost
= 0;
449 header
->ctx
.timestamp_end
= tsc
;
450 header
->ctx
.content_size
=
451 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
452 header
->ctx
.packet_size
=
453 (uint64_t) PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
455 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
456 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
457 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
458 header
->ctx
.events_discarded
= records_lost
;
461 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
462 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
467 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
471 static void client_content_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
.content_size
);
475 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
478 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
479 size_t *offset
, size_t *length
)
481 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
482 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
485 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
486 struct lttng_ust_shm_handle
*handle
)
488 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
491 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
492 struct lttng_ust_shm_handle
*handle
,
493 uint64_t *timestamp_begin
)
495 struct packet_header
*header
;
497 header
= client_packet_header(buf
, handle
);
500 *timestamp_begin
= header
->ctx
.timestamp_begin
;
504 static int client_timestamp_end(struct lttng_ust_lib_ring_buffer
*buf
,
505 struct lttng_ust_shm_handle
*handle
,
506 uint64_t *timestamp_end
)
508 struct packet_header
*header
;
510 header
= client_packet_header(buf
, handle
);
513 *timestamp_end
= header
->ctx
.timestamp_end
;
517 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
518 struct lttng_ust_shm_handle
*handle
,
519 uint64_t *events_discarded
)
521 struct packet_header
*header
;
523 header
= client_packet_header(buf
, handle
);
526 *events_discarded
= header
->ctx
.events_discarded
;
530 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
531 struct lttng_ust_shm_handle
*handle
,
532 uint64_t *content_size
)
534 struct packet_header
*header
;
536 header
= client_packet_header(buf
, handle
);
539 *content_size
= header
->ctx
.content_size
;
543 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
544 struct lttng_ust_shm_handle
*handle
,
545 uint64_t *packet_size
)
547 struct packet_header
*header
;
549 header
= client_packet_header(buf
, handle
);
552 *packet_size
= header
->ctx
.packet_size
;
556 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
557 struct lttng_ust_shm_handle
*handle
,
560 struct packet_header
*header
;
562 header
= client_packet_header(buf
, handle
);
565 *stream_id
= header
->stream_id
;
569 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
570 struct lttng_ust_shm_handle
*handle
,
573 struct channel
*chan
;
575 chan
= shmp(handle
, handle
->chan
);
576 *ts
= client_ring_buffer_clock_read(chan
);
581 static int client_sequence_number(struct lttng_ust_lib_ring_buffer
*buf
,
582 struct lttng_ust_shm_handle
*handle
,
585 struct packet_header
*header
;
587 header
= client_packet_header(buf
, handle
);
588 *seq
= header
->ctx
.packet_seq_num
;
592 static int client_instance_id(struct lttng_ust_lib_ring_buffer
*buf
,
593 struct lttng_ust_shm_handle
*handle
,
596 struct packet_header
*header
;
598 header
= client_packet_header(buf
, handle
);
599 *id
= header
->stream_instance_id
;
604 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
606 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
607 .record_header_size
= client_record_header_size
,
608 .subbuffer_header_size
= client_packet_header_size
,
609 .buffer_begin
= client_buffer_begin
,
610 .buffer_end
= client_buffer_end
,
611 .buffer_create
= client_buffer_create
,
612 .buffer_finalize
= client_buffer_finalize
,
613 .content_size_field
= client_content_size_field
,
614 .packet_size_field
= client_packet_size_field
,
616 .timestamp_begin
= client_timestamp_begin
,
617 .timestamp_end
= client_timestamp_end
,
618 .events_discarded
= client_events_discarded
,
619 .content_size
= client_content_size
,
620 .packet_size
= client_packet_size
,
621 .stream_id
= client_stream_id
,
622 .current_timestamp
= client_current_timestamp
,
623 .sequence_number
= client_sequence_number
,
624 .instance_id
= client_instance_id
,
627 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
628 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
629 .cb
.record_header_size
= client_record_header_size
,
630 .cb
.subbuffer_header_size
= client_packet_header_size
,
631 .cb
.buffer_begin
= client_buffer_begin
,
632 .cb
.buffer_end
= client_buffer_end
,
633 .cb
.buffer_create
= client_buffer_create
,
634 .cb
.buffer_finalize
= client_buffer_finalize
,
635 .cb
.content_size_field
= client_content_size_field
,
636 .cb
.packet_size_field
= client_packet_size_field
,
638 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
639 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
640 .sync
= RING_BUFFER_SYNC_PER_CPU
,
641 .mode
= RING_BUFFER_MODE_TEMPLATE
,
642 .backend
= RING_BUFFER_PAGE
,
643 .output
= RING_BUFFER_MMAP
,
644 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
645 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
646 .wakeup
= LTTNG_CLIENT_WAKEUP
,
647 .client_type
= LTTNG_CLIENT_TYPE
,
649 .cb_ptr
= &client_cb
.parent
,
652 const struct lttng_ust_client_lib_ring_buffer_client_cb
*LTTNG_CLIENT_CALLBACKS
= &client_cb
;
655 struct lttng_channel
*_channel_create(const char *name
,
657 size_t subbuf_size
, size_t num_subbuf
,
658 unsigned int switch_timer_interval
,
659 unsigned int read_timer_interval
,
662 const int *stream_fds
, int nr_stream_fds
)
664 struct lttng_channel chan_priv_init
;
665 struct lttng_ust_shm_handle
*handle
;
666 struct lttng_channel
*lttng_chan
;
669 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
670 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
671 chan_priv_init
.id
= chan_id
;
672 handle
= channel_create(&client_config
, name
,
673 &priv
, __alignof__(struct lttng_channel
),
674 sizeof(struct lttng_channel
),
676 buf_addr
, subbuf_size
, num_subbuf
,
677 switch_timer_interval
, read_timer_interval
,
678 stream_fds
, nr_stream_fds
);
682 lttng_chan
->handle
= handle
;
683 lttng_chan
->chan
= shmp(handle
, handle
->chan
);
688 void lttng_channel_destroy(struct lttng_channel
*chan
)
690 channel_destroy(chan
->chan
, chan
->handle
, 1);
694 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
697 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
698 struct lttng_rseq_state rseq_state
;
699 int ret
, cpu
, fallback
= 0;
701 if (lib_ring_buffer_begin(&client_config
))
704 rseq_state
= rseq_start();
705 if (caa_unlikely(rseq_cpu_at_start(rseq_state
) < 0)) {
706 if (caa_unlikely(rseq_cpu_at_start(rseq_state
) == -1)) {
707 if (!rseq_register_current_thread())
710 /* rseq is unavailable. */
711 cpu
= lib_ring_buffer_get_cpu(&client_config
);
712 if (caa_unlikely(cpu
< 0)) {
717 cpu
= rseq_cpu_at_start(rseq_state
);
722 switch (lttng_chan
->header_type
) {
723 case 1: /* compact */
725 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
728 if (event_id
> 65534)
729 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
735 if (caa_likely(ctx
->ctx_len
736 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx
)))
737 ctx
->rseq_state
= rseq_state
;
739 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
740 if (caa_unlikely(ret
)) {
741 if (ret
== -EAGAIN
) {
744 uatomic_inc(<tng_chan
->chan
->u
.reserve_fallback_ref
);
745 cpu
= lib_ring_buffer_get_cpu(&client_config
);
746 if (caa_unlikely(cpu
< 0)) {
754 if (caa_likely(ctx
->ctx_len
755 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx
))) {
756 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
757 &ctx
->backend_pages
)) {
762 lttng_write_event_header(&client_config
, ctx
, event_id
);
764 if (caa_unlikely(fallback
))
765 uatomic_dec(<tng_chan
->chan
->u
.reserve_fallback_ref
);
769 lib_ring_buffer_end(&client_config
);
771 uatomic_dec(<tng_chan
->chan
->u
.reserve_fallback_ref
);
776 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
778 lib_ring_buffer_commit(&client_config
, ctx
);
779 lib_ring_buffer_end(&client_config
);
783 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
786 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
790 void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const char *src
,
793 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
798 wait_queue_head_t
*lttng_get_reader_wait_queue(struct channel
*chan
)
800 return &chan
->read_wait
;
804 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
806 return &chan
->hp_wait
;
811 int lttng_is_finalized(struct channel
*chan
)
813 return lib_ring_buffer_channel_is_finalized(chan
);
817 int lttng_is_disabled(struct channel
*chan
)
819 return lib_ring_buffer_channel_is_disabled(chan
);
823 int lttng_flush_buffer(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
825 struct lttng_ust_lib_ring_buffer
*buf
;
828 for_each_channel_cpu(cpu
, chan
) {
829 int shm_fd
, wait_fd
, wakeup_fd
;
830 uint64_t memory_map_size
;
832 buf
= channel_get_ring_buffer(&client_config
, chan
,
833 cpu
, handle
, &shm_fd
, &wait_fd
,
834 &wakeup_fd
, &memory_map_size
);
835 lib_ring_buffer_switch(&client_config
, buf
,
836 SWITCH_ACTIVE
, handle
);
841 static struct lttng_transport lttng_relay_transport
= {
842 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
844 .channel_create
= _channel_create
,
845 .channel_destroy
= lttng_channel_destroy
,
847 .event_reserve
= lttng_event_reserve
,
848 .event_commit
= lttng_event_commit
,
849 .event_write
= lttng_event_write
,
850 .packet_avail_size
= NULL
, /* Would be racy anyway */
851 //.get_reader_wait_queue = lttng_get_reader_wait_queue,
852 //.get_hp_wait_queue = lttng_get_hp_wait_queue,
853 .is_finalized
= lttng_is_finalized
,
854 .is_disabled
= lttng_is_disabled
,
855 .flush_buffer
= lttng_flush_buffer
,
856 .event_strcpy
= lttng_event_strcpy
,
858 .client_config
= &client_config
,
861 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
863 DBG("LTT : ltt ring buffer client \"%s\" init\n",
864 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
865 lttng_transport_register(<tng_relay_transport
);
868 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
870 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
871 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
872 lttng_transport_unregister(<tng_relay_transport
);