2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng lib ring buffer client template.
13 #include <urcu/tls-compat.h>
15 #include "common/bitfield.h"
16 #include "common/align.h"
17 #include "common/events.h"
18 #include "common/tracer.h"
19 #include "common/ringbuffer/frontend_types.h"
21 struct metadata_packet_header
{
22 uint32_t magic
; /* 0x75D11D57 */
23 uint8_t uuid
[LTTNG_UST_UUID_LEN
]; /* Unique Universal Identifier */
24 uint32_t checksum
; /* 0 if unused */
25 uint32_t content_size
; /* in bits */
26 uint32_t packet_size
; /* in bits */
27 uint8_t compression_scheme
; /* 0 if unused */
28 uint8_t encryption_scheme
; /* 0 if unused */
29 uint8_t checksum_scheme
; /* 0 if unused */
30 uint8_t major
; /* CTF spec major version number */
31 uint8_t minor
; /* CTF spec minor version number */
32 uint8_t header_end
[0];
35 struct metadata_record_header
{
36 uint8_t header_end
[0]; /* End of header */
39 static const struct lttng_ust_ring_buffer_config client_config
;
41 /* No nested use supported for metadata ring buffer. */
42 static DEFINE_URCU_TLS(struct lttng_ust_ring_buffer_ctx_private
, private_ctx
);
44 static inline uint64_t lib_ring_buffer_clock_read(
45 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)))
51 size_t record_header_size(
52 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
53 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
54 size_t offset
__attribute__((unused
)),
55 size_t *pre_header_padding
__attribute__((unused
)),
56 struct lttng_ust_ring_buffer_ctx
*ctx
__attribute__((unused
)),
57 void *client_ctx
__attribute__((unused
)))
62 #include "common/ringbuffer/api.h"
63 #include "common/ringbuffer-clients/clients.h"
65 static uint64_t client_ring_buffer_clock_read(
66 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)))
72 size_t client_record_header_size(
73 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
74 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
75 size_t offset
__attribute__((unused
)),
76 size_t *pre_header_padding
__attribute__((unused
)),
77 struct lttng_ust_ring_buffer_ctx
*ctx
__attribute__((unused
)),
78 void *client_ctx
__attribute__((unused
)))
84 * client_packet_header_size - called on buffer-switch to a new sub-buffer
86 * Return header size without padding after the structure. Don't use packed
87 * structure because gcc generates inefficient code on some architectures
90 static size_t client_packet_header_size(void)
92 return offsetof(struct metadata_packet_header
, header_end
);
95 static void client_buffer_begin(struct lttng_ust_ring_buffer
*buf
,
96 uint64_t tsc
__attribute__((unused
)),
97 unsigned int subbuf_idx
,
98 struct lttng_ust_shm_handle
*handle
)
100 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
101 struct metadata_packet_header
*header
=
102 (struct metadata_packet_header
*)
103 lib_ring_buffer_offset_address(&buf
->backend
,
104 subbuf_idx
* chan
->backend
.subbuf_size
,
106 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
111 header
->magic
= TSDL_MAGIC_NUMBER
;
112 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
113 header
->checksum
= 0; /* 0 if unused */
114 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
115 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
116 header
->compression_scheme
= 0; /* 0 if unused */
117 header
->encryption_scheme
= 0; /* 0 if unused */
118 header
->checksum_scheme
= 0; /* 0 if unused */
119 header
->major
= CTF_SPEC_MAJOR
;
120 header
->minor
= CTF_SPEC_MINOR
;
124 * offset is assumed to never be 0 here : never deliver a completely empty
125 * subbuffer. data_size is between 1 and subbuf_size.
127 static void client_buffer_end(struct lttng_ust_ring_buffer
*buf
,
128 uint64_t tsc
__attribute__((unused
)),
129 unsigned int subbuf_idx
, unsigned long data_size
,
130 struct lttng_ust_shm_handle
*handle
)
132 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
133 struct metadata_packet_header
*header
=
134 (struct metadata_packet_header
*)
135 lib_ring_buffer_offset_address(&buf
->backend
,
136 subbuf_idx
* chan
->backend
.subbuf_size
,
138 unsigned long records_lost
= 0;
143 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
144 header
->packet_size
= LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
146 * We do not care about the records lost count, because the metadata
147 * channel waits and retry.
149 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
150 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
151 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
152 WARN_ON_ONCE(records_lost
!= 0);
155 static int client_buffer_create(
156 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
157 void *priv
__attribute__((unused
)),
158 int cpu
__attribute__((unused
)),
159 const char *name
__attribute__((unused
)),
160 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
165 static void client_buffer_finalize(
166 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
167 void *priv
__attribute__((unused
)),
168 int cpu
__attribute__((unused
)),
169 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
174 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
176 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
177 .record_header_size
= client_record_header_size
,
178 .subbuffer_header_size
= client_packet_header_size
,
179 .buffer_begin
= client_buffer_begin
,
180 .buffer_end
= client_buffer_end
,
181 .buffer_create
= client_buffer_create
,
182 .buffer_finalize
= client_buffer_finalize
,
186 static const struct lttng_ust_ring_buffer_config client_config
= {
187 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
188 .cb
.record_header_size
= client_record_header_size
,
189 .cb
.subbuffer_header_size
= client_packet_header_size
,
190 .cb
.buffer_begin
= client_buffer_begin
,
191 .cb
.buffer_end
= client_buffer_end
,
192 .cb
.buffer_create
= client_buffer_create
,
193 .cb
.buffer_finalize
= client_buffer_finalize
,
196 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
197 .sync
= RING_BUFFER_SYNC_GLOBAL
,
198 .mode
= RING_BUFFER_MODE_TEMPLATE
,
199 .backend
= RING_BUFFER_PAGE
,
200 .output
= RING_BUFFER_MMAP
,
201 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
202 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
203 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
204 .client_type
= LTTNG_CLIENT_TYPE
,
206 .cb_ptr
= &client_cb
.parent
,
210 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
212 size_t subbuf_size
, size_t num_subbuf
,
213 unsigned int switch_timer_interval
,
214 unsigned int read_timer_interval
,
217 const int *stream_fds
, int nr_stream_fds
,
218 int64_t blocking_timeout
)
220 struct lttng_ust_abi_channel_config chan_priv_init
;
221 struct lttng_ust_shm_handle
*handle
;
222 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
224 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
227 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
228 lttng_chan_buf
->priv
->id
= chan_id
;
230 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
231 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
232 chan_priv_init
.id
= chan_id
;
234 handle
= channel_create(&client_config
, name
,
235 __alignof__(struct lttng_ust_channel_buffer
),
236 sizeof(struct lttng_ust_channel_buffer
),
238 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
239 switch_timer_interval
, read_timer_interval
,
240 stream_fds
, nr_stream_fds
, blocking_timeout
);
243 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
244 return lttng_chan_buf
;
247 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
252 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
254 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
255 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
259 int lttng_event_reserve(struct lttng_ust_ring_buffer_ctx
*ctx
)
263 memset(&URCU_TLS(private_ctx
), 0, sizeof(struct lttng_ust_ring_buffer_ctx_private
));
264 URCU_TLS(private_ctx
).pub
= ctx
;
265 URCU_TLS(private_ctx
).chan
= ctx
->client_priv
;
266 ctx
->priv
= &URCU_TLS(private_ctx
);
267 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
270 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
271 &ctx
->priv
->backend_pages
))
277 void lttng_event_commit(struct lttng_ust_ring_buffer_ctx
*ctx
)
279 lib_ring_buffer_commit(&client_config
, ctx
);
283 void lttng_event_write(struct lttng_ust_ring_buffer_ctx
*ctx
,
284 const void *src
, size_t len
, size_t alignment
)
286 lttng_ust_ring_buffer_align_ctx(ctx
, alignment
);
287 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
291 size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer
*chan
)
293 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
294 unsigned long o_begin
;
295 struct lttng_ust_ring_buffer
*buf
;
297 buf
= shmp(rb_chan
->handle
, rb_chan
->backend
.buf
[0].shmp
); /* Only for global buffer ! */
298 o_begin
= v_read(&client_config
, &buf
->offset
);
299 if (subbuf_offset(o_begin
, rb_chan
) != 0) {
300 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
);
302 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
)
303 - sizeof(struct metadata_packet_header
);
308 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
310 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
312 return lib_ring_buffer_channel_is_finalized(rb_chan
);
316 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
318 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
320 return lib_ring_buffer_channel_is_disabled(rb_chan
);
324 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
326 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
327 struct lttng_ust_ring_buffer
*buf
;
328 int shm_fd
, wait_fd
, wakeup_fd
;
329 uint64_t memory_map_size
;
331 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
332 0, rb_chan
->handle
, &shm_fd
, &wait_fd
, &wakeup_fd
,
334 lib_ring_buffer_switch(&client_config
, buf
,
335 SWITCH_ACTIVE
, rb_chan
->handle
);
339 static struct lttng_transport lttng_relay_transport
= {
340 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
342 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
344 .priv
= LTTNG_UST_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
345 .pub
= <tng_relay_transport
.ops
,
346 .channel_create
= _channel_create
,
347 .channel_destroy
= lttng_channel_destroy
,
348 .packet_avail_size
= lttng_packet_avail_size
,
349 .is_finalized
= lttng_is_finalized
,
350 .is_disabled
= lttng_is_disabled
,
351 .flush_buffer
= lttng_flush_buffer
,
353 .event_reserve
= lttng_event_reserve
,
354 .event_commit
= lttng_event_commit
,
355 .event_write
= lttng_event_write
,
357 .client_config
= &client_config
,
360 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
362 DBG("LTT : ltt ring buffer client \"%s\" init\n",
363 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
364 lttng_transport_register(<tng_relay_transport
);
367 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
369 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
370 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
371 lttng_transport_unregister(<tng_relay_transport
);