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
33 #define LTTNG_RSEQ_ATTEMPTS 8
41 * Keep the natural field alignment for _each field_ within this structure if
42 * you ever add/remove a field from this header. Packed attribute is not used
43 * because gcc generates poor code on at least powerpc and mips. Don't ever
44 * let gcc add padding between the structure elements.
47 struct packet_header
{
48 /* Trace packet header */
51 * contains endianness information.
53 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
55 uint64_t stream_instance_id
;
58 /* Stream packet context */
59 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
60 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
61 uint64_t content_size
; /* Size of data in subbuffer */
62 uint64_t packet_size
; /* Subbuffer size (include padding) */
63 uint64_t packet_seq_num
; /* Packet sequence number */
64 unsigned long events_discarded
; /*
65 * Events lost in this subbuffer since
66 * the beginning of the trace.
69 uint32_t cpu_id
; /* CPU id associated with stream */
70 uint8_t header_end
; /* End of header */
75 static inline uint64_t lib_ring_buffer_clock_read(struct channel
*chan
)
77 return trace_clock_read64();
81 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
,
82 enum app_ctx_mode mode
)
85 size_t orig_offset
= offset
;
89 offset
+= lib_ring_buffer_align(offset
, ctx
->largest_align
);
90 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
91 if (mode
== APP_CTX_ENABLED
) {
92 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
94 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
96 * Before UST 2.8, we cannot use the
97 * application context, because we
98 * cannot trust that the handler used
99 * for get_size is the same used for
100 * ctx_record, which would result in
101 * corrupted traces when tracing
102 * concurrently with application context
103 * register/unregister.
105 offset
+= lttng_ust_dummy_get_size(&ctx
->fields
[i
], offset
);
107 offset
+= ctx
->fields
[i
].get_size(&ctx
->fields
[i
], offset
);
111 return offset
- orig_offset
;
115 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
116 struct lttng_channel
*chan
,
117 struct lttng_ctx
*ctx
,
118 enum app_ctx_mode mode
)
122 if (caa_likely(!ctx
))
124 lib_ring_buffer_align_ctx(bufctx
, ctx
->largest_align
);
125 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
126 if (mode
== APP_CTX_ENABLED
) {
127 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
129 if (lttng_context_is_app(ctx
->fields
[i
].event_field
.name
)) {
131 * Before UST 2.8, we cannot use the
132 * application context, because we
133 * cannot trust that the handler used
134 * for get_size is the same used for
135 * ctx_record, which would result in
136 * corrupted traces when tracing
137 * concurrently with application context
138 * register/unregister.
140 lttng_ust_dummy_record(&ctx
->fields
[i
], bufctx
, chan
);
142 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
149 * record_header_size - Calculate the header size and padding necessary.
150 * @config: ring buffer instance configuration
152 * @offset: offset in the write buffer
153 * @pre_header_padding: padding to add before the header (output)
154 * @ctx: reservation context
156 * Returns the event header size (including padding).
158 * The payload must itself determine its own alignment from the biggest type it
162 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
163 struct channel
*chan
, size_t offset
,
164 size_t *pre_header_padding
,
165 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
167 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
168 struct lttng_event
*event
= ctx
->priv
;
169 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
170 size_t orig_offset
= offset
;
173 switch (lttng_chan
->header_type
) {
174 case 1: /* compact */
175 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
177 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
178 offset
+= sizeof(uint32_t); /* id and timestamp */
180 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
181 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
182 /* Align extended struct on largest member */
183 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
184 offset
+= sizeof(uint32_t); /* id */
185 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
186 offset
+= sizeof(uint64_t); /* timestamp */
190 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
192 offset
+= sizeof(uint16_t);
193 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
194 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
195 offset
+= sizeof(uint32_t); /* timestamp */
197 /* Align extended struct on largest member */
198 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
199 offset
+= sizeof(uint32_t); /* id */
200 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
201 offset
+= sizeof(uint64_t); /* timestamp */
209 /* 2.8+ probe ABI. */
210 offset
+= ctx_get_size(offset
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
211 offset
+= ctx_get_size(offset
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
213 /* Pre 2.8 probe ABI. */
214 offset
+= ctx_get_size(offset
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
215 offset
+= ctx_get_size(offset
, event
->ctx
, APP_CTX_DISABLED
);
217 *pre_header_padding
= padding
;
218 return offset
- orig_offset
;
221 #include "../libringbuffer/api.h"
222 #include "lttng-rb-clients.h"
225 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
226 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
230 * lttng_write_event_header
232 * Writes the event header to the offset (already aligned on 32-bits).
234 * @config: ring buffer instance configuration
235 * @ctx: reservation context
236 * @event_id: event ID
239 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
240 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
243 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
244 struct lttng_event
*event
= ctx
->priv
;
245 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
247 if (caa_unlikely(ctx
->rflags
))
250 switch (lttng_chan
->header_type
) {
251 case 1: /* compact */
253 uint32_t id_time
= 0;
255 bt_bitfield_write(&id_time
, uint32_t,
257 LTTNG_COMPACT_EVENT_BITS
,
259 bt_bitfield_write(&id_time
, uint32_t,
260 LTTNG_COMPACT_EVENT_BITS
,
261 LTTNG_COMPACT_TSC_BITS
,
263 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
268 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
269 uint16_t id
= event_id
;
271 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
272 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
273 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
281 /* 2.8+ probe ABI. */
282 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
283 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
285 /* Pre 2.8 probe ABI. */
286 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
287 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
289 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
294 lttng_write_event_header_slow(config
, ctx
, event_id
);
298 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
299 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
302 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
303 struct lttng_event
*event
= ctx
->priv
;
304 struct lttng_stack_ctx
*lttng_ctx
= ctx
->priv2
;
306 switch (lttng_chan
->header_type
) {
307 case 1: /* compact */
308 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
309 uint32_t id_time
= 0;
311 bt_bitfield_write(&id_time
, uint32_t,
313 LTTNG_COMPACT_EVENT_BITS
,
315 bt_bitfield_write(&id_time
, uint32_t,
316 LTTNG_COMPACT_EVENT_BITS
,
317 LTTNG_COMPACT_TSC_BITS
,
319 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
322 uint64_t timestamp
= ctx
->tsc
;
324 bt_bitfield_write(&id
, uint8_t,
326 LTTNG_COMPACT_EVENT_BITS
,
328 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
329 /* Align extended struct on largest member */
330 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
331 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
332 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
333 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
338 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
339 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
340 uint16_t id
= event_id
;
342 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
343 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
344 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
347 uint64_t timestamp
= ctx
->tsc
;
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
));
362 /* 2.8+ probe ABI. */
363 ctx_record(ctx
, lttng_chan
, lttng_ctx
->chan_ctx
, APP_CTX_ENABLED
);
364 ctx_record(ctx
, lttng_chan
, lttng_ctx
->event_ctx
, APP_CTX_ENABLED
);
366 /* Pre 2.8 probe ABI. */
367 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
, APP_CTX_DISABLED
);
368 ctx_record(ctx
, lttng_chan
, event
->ctx
, APP_CTX_DISABLED
);
370 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
373 static const struct lttng_ust_lib_ring_buffer_config client_config
;
375 static uint64_t client_ring_buffer_clock_read(struct channel
*chan
)
377 return lib_ring_buffer_clock_read(chan
);
381 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
382 struct channel
*chan
, size_t offset
,
383 size_t *pre_header_padding
,
384 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
386 return record_header_size(config
, chan
, offset
,
387 pre_header_padding
, ctx
);
391 * client_packet_header_size - called on buffer-switch to a new sub-buffer
393 * Return header size without padding after the structure. Don't use packed
394 * structure because gcc generates inefficient code on some architectures
397 static size_t client_packet_header_size(void)
399 return offsetof(struct packet_header
, ctx
.header_end
);
402 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
403 unsigned int subbuf_idx
,
404 struct lttng_ust_shm_handle
*handle
)
406 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
407 struct packet_header
*header
=
408 (struct packet_header
*)
409 lib_ring_buffer_offset_address(&buf
->backend
,
410 subbuf_idx
* chan
->backend
.subbuf_size
,
412 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
413 uint64_t cnt
= shmp_index(handle
, buf
->backend
.buf_cnt
, subbuf_idx
)->seq_cnt
;
418 header
->magic
= CTF_MAGIC_NUMBER
;
419 memcpy(header
->uuid
, lttng_chan
->uuid
, sizeof(lttng_chan
->uuid
));
420 header
->stream_id
= lttng_chan
->id
;
421 header
->stream_instance_id
= buf
->backend
.cpu
;
422 header
->ctx
.timestamp_begin
= tsc
;
423 header
->ctx
.timestamp_end
= 0;
424 header
->ctx
.content_size
= ~0ULL; /* for debugging */
425 header
->ctx
.packet_size
= ~0ULL;
426 header
->ctx
.packet_seq_num
= chan
->backend
.num_subbuf
* cnt
+ subbuf_idx
;
427 header
->ctx
.events_discarded
= 0;
428 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
432 * offset is assumed to never be 0 here : never deliver a completely empty
433 * subbuffer. data_size is between 1 and subbuf_size.
435 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
436 unsigned int subbuf_idx
, unsigned long data_size
,
437 struct lttng_ust_shm_handle
*handle
)
439 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
440 struct packet_header
*header
=
441 (struct packet_header
*)
442 lib_ring_buffer_offset_address(&buf
->backend
,
443 subbuf_idx
* chan
->backend
.subbuf_size
,
445 unsigned long records_lost
= 0;
450 header
->ctx
.timestamp_end
= tsc
;
451 header
->ctx
.content_size
=
452 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
453 header
->ctx
.packet_size
=
454 (uint64_t) PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
456 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
457 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
458 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
459 header
->ctx
.events_discarded
= records_lost
;
462 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
463 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
468 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
472 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
473 size_t *offset
, size_t *length
)
475 *offset
= offsetof(struct packet_header
, ctx
.content_size
);
476 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.content_size
);
479 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config
*config
,
480 size_t *offset
, size_t *length
)
482 *offset
= offsetof(struct packet_header
, ctx
.packet_size
);
483 *length
= sizeof(((struct packet_header
*) NULL
)->ctx
.packet_size
);
486 static struct packet_header
*client_packet_header(struct lttng_ust_lib_ring_buffer
*buf
,
487 struct lttng_ust_shm_handle
*handle
)
489 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0, handle
);
492 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer
*buf
,
493 struct lttng_ust_shm_handle
*handle
,
494 uint64_t *timestamp_begin
)
496 struct packet_header
*header
;
498 header
= client_packet_header(buf
, handle
);
501 *timestamp_begin
= header
->ctx
.timestamp_begin
;
505 static int client_timestamp_end(struct lttng_ust_lib_ring_buffer
*buf
,
506 struct lttng_ust_shm_handle
*handle
,
507 uint64_t *timestamp_end
)
509 struct packet_header
*header
;
511 header
= client_packet_header(buf
, handle
);
514 *timestamp_end
= header
->ctx
.timestamp_end
;
518 static int client_events_discarded(struct lttng_ust_lib_ring_buffer
*buf
,
519 struct lttng_ust_shm_handle
*handle
,
520 uint64_t *events_discarded
)
522 struct packet_header
*header
;
524 header
= client_packet_header(buf
, handle
);
527 *events_discarded
= header
->ctx
.events_discarded
;
531 static int client_content_size(struct lttng_ust_lib_ring_buffer
*buf
,
532 struct lttng_ust_shm_handle
*handle
,
533 uint64_t *content_size
)
535 struct packet_header
*header
;
537 header
= client_packet_header(buf
, handle
);
540 *content_size
= header
->ctx
.content_size
;
544 static int client_packet_size(struct lttng_ust_lib_ring_buffer
*buf
,
545 struct lttng_ust_shm_handle
*handle
,
546 uint64_t *packet_size
)
548 struct packet_header
*header
;
550 header
= client_packet_header(buf
, handle
);
553 *packet_size
= header
->ctx
.packet_size
;
557 static int client_stream_id(struct lttng_ust_lib_ring_buffer
*buf
,
558 struct lttng_ust_shm_handle
*handle
,
561 struct packet_header
*header
;
563 header
= client_packet_header(buf
, handle
);
566 *stream_id
= header
->stream_id
;
570 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer
*buf
,
571 struct lttng_ust_shm_handle
*handle
,
574 struct channel
*chan
;
576 chan
= shmp(handle
, handle
->chan
);
577 *ts
= client_ring_buffer_clock_read(chan
);
582 static int client_sequence_number(struct lttng_ust_lib_ring_buffer
*buf
,
583 struct lttng_ust_shm_handle
*handle
,
586 struct packet_header
*header
;
588 header
= client_packet_header(buf
, handle
);
589 *seq
= header
->ctx
.packet_seq_num
;
593 static int client_instance_id(struct lttng_ust_lib_ring_buffer
*buf
,
594 struct lttng_ust_shm_handle
*handle
,
597 struct packet_header
*header
;
599 header
= client_packet_header(buf
, handle
);
600 *id
= header
->stream_instance_id
;
605 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
607 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
608 .record_header_size
= client_record_header_size
,
609 .subbuffer_header_size
= client_packet_header_size
,
610 .buffer_begin
= client_buffer_begin
,
611 .buffer_end
= client_buffer_end
,
612 .buffer_create
= client_buffer_create
,
613 .buffer_finalize
= client_buffer_finalize
,
614 .content_size_field
= client_content_size_field
,
615 .packet_size_field
= client_packet_size_field
,
617 .timestamp_begin
= client_timestamp_begin
,
618 .timestamp_end
= client_timestamp_end
,
619 .events_discarded
= client_events_discarded
,
620 .content_size
= client_content_size
,
621 .packet_size
= client_packet_size
,
622 .stream_id
= client_stream_id
,
623 .current_timestamp
= client_current_timestamp
,
624 .sequence_number
= client_sequence_number
,
625 .instance_id
= client_instance_id
,
628 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
629 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
630 .cb
.record_header_size
= client_record_header_size
,
631 .cb
.subbuffer_header_size
= client_packet_header_size
,
632 .cb
.buffer_begin
= client_buffer_begin
,
633 .cb
.buffer_end
= client_buffer_end
,
634 .cb
.buffer_create
= client_buffer_create
,
635 .cb
.buffer_finalize
= client_buffer_finalize
,
636 .cb
.content_size_field
= client_content_size_field
,
637 .cb
.packet_size_field
= client_packet_size_field
,
639 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
640 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
641 .sync
= RING_BUFFER_SYNC_PER_CPU
,
642 .mode
= RING_BUFFER_MODE_TEMPLATE
,
643 .backend
= RING_BUFFER_PAGE
,
644 .output
= RING_BUFFER_MMAP
,
645 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
646 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
647 .wakeup
= LTTNG_CLIENT_WAKEUP
,
648 .client_type
= LTTNG_CLIENT_TYPE
,
650 .cb_ptr
= &client_cb
.parent
,
653 const struct lttng_ust_client_lib_ring_buffer_client_cb
*LTTNG_CLIENT_CALLBACKS
= &client_cb
;
656 struct lttng_channel
*_channel_create(const char *name
,
658 size_t subbuf_size
, size_t num_subbuf
,
659 unsigned int switch_timer_interval
,
660 unsigned int read_timer_interval
,
663 const int *stream_fds
, int nr_stream_fds
)
665 struct lttng_channel chan_priv_init
;
666 struct lttng_ust_shm_handle
*handle
;
667 struct lttng_channel
*lttng_chan
;
670 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
671 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
672 chan_priv_init
.id
= chan_id
;
673 handle
= channel_create(&client_config
, name
,
674 &priv
, __alignof__(struct lttng_channel
),
675 sizeof(struct lttng_channel
),
677 buf_addr
, subbuf_size
, num_subbuf
,
678 switch_timer_interval
, read_timer_interval
,
679 stream_fds
, nr_stream_fds
);
683 lttng_chan
->handle
= handle
;
684 lttng_chan
->chan
= shmp(handle
, handle
->chan
);
689 void lttng_channel_destroy(struct lttng_channel
*chan
)
691 channel_destroy(chan
->chan
, chan
->handle
, 1);
695 bool refcount_get_saturate(long *ref
)
699 old
= uatomic_read(ref
);
701 if (old
== LONG_MAX
) {
702 return false; /* Saturated. */
705 res
= uatomic_cmpxchg(ref
, old
, _new
);
707 if (_new
== LONG_MAX
) {
708 return false; /* Saturation. */
710 return true; /* Success. */
717 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
720 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
721 struct lttng_rseq_state rseq_state
;
722 int ret
, cpu
, attempt
= 0;
723 bool put_fallback_ref
= false;
725 if (lib_ring_buffer_begin(&client_config
))
728 rseq_state
= rseq_start();
729 if (caa_unlikely(rseq_cpu_at_start(rseq_state
) < 0)) {
730 if (caa_unlikely(rseq_cpu_at_start(rseq_state
) == -1)) {
731 if (!rseq_register_current_thread())
734 /* rseq is unavailable. */
735 cpu
= lib_ring_buffer_get_cpu(&client_config
);
736 if (caa_unlikely(cpu
< 0)) {
741 cpu
= rseq_cpu_at_start(rseq_state
);
746 switch (lttng_chan
->header_type
) {
747 case 1: /* compact */
749 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
752 if (event_id
> 65534)
753 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
759 if (caa_likely(ctx
->ctx_len
760 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx
)))
761 ctx
->rseq_state
= rseq_state
;
763 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
764 if (caa_unlikely(ret
)) {
765 if (ret
== -EAGAIN
) {
766 assert(!put_fallback_ref
);
767 if (++attempt
< LTTNG_RSEQ_ATTEMPTS
) {
771 put_fallback_ref
= refcount_get_saturate(
772 <tng_chan
->chan
->u
.reserve_fallback_ref
);
773 cpu
= lib_ring_buffer_get_cpu(&client_config
);
774 if (caa_unlikely(cpu
< 0)) {
782 if (caa_likely(ctx
->ctx_len
783 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx
))) {
784 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
785 &ctx
->backend_pages
)) {
790 lttng_write_event_header(&client_config
, ctx
, event_id
);
792 if (caa_unlikely(put_fallback_ref
))
793 uatomic_dec(<tng_chan
->chan
->u
.reserve_fallback_ref
);
797 lib_ring_buffer_end(&client_config
);
798 if (put_fallback_ref
)
799 uatomic_dec(<tng_chan
->chan
->u
.reserve_fallback_ref
);
804 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
806 lib_ring_buffer_commit(&client_config
, ctx
);
807 lib_ring_buffer_end(&client_config
);
811 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
814 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
818 void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const char *src
,
821 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
826 wait_queue_head_t
*lttng_get_reader_wait_queue(struct channel
*chan
)
828 return &chan
->read_wait
;
832 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
834 return &chan
->hp_wait
;
839 int lttng_is_finalized(struct channel
*chan
)
841 return lib_ring_buffer_channel_is_finalized(chan
);
845 int lttng_is_disabled(struct channel
*chan
)
847 return lib_ring_buffer_channel_is_disabled(chan
);
851 int lttng_flush_buffer(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
853 struct lttng_ust_lib_ring_buffer
*buf
;
856 for_each_channel_cpu(cpu
, chan
) {
857 int shm_fd
, wait_fd
, wakeup_fd
;
858 uint64_t memory_map_size
;
860 buf
= channel_get_ring_buffer(&client_config
, chan
,
861 cpu
, handle
, &shm_fd
, &wait_fd
,
862 &wakeup_fd
, &memory_map_size
);
863 lib_ring_buffer_switch(&client_config
, buf
,
864 SWITCH_ACTIVE
, handle
);
869 static struct lttng_transport lttng_relay_transport
= {
870 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
872 .channel_create
= _channel_create
,
873 .channel_destroy
= lttng_channel_destroy
,
875 .event_reserve
= lttng_event_reserve
,
876 .event_commit
= lttng_event_commit
,
877 .event_write
= lttng_event_write
,
878 .packet_avail_size
= NULL
, /* Would be racy anyway */
879 //.get_reader_wait_queue = lttng_get_reader_wait_queue,
880 //.get_hp_wait_queue = lttng_get_hp_wait_queue,
881 .is_finalized
= lttng_is_finalized
,
882 .is_disabled
= lttng_is_disabled
,
883 .flush_buffer
= lttng_flush_buffer
,
884 .event_strcpy
= lttng_event_strcpy
,
886 .client_config
= &client_config
,
889 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
891 DBG("LTT : ltt ring buffer client \"%s\" init\n",
892 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
893 lttng_transport_register(<tng_relay_transport
);
896 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
898 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
899 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
900 lttng_transport_unregister(<tng_relay_transport
);