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
,
131 const struct lttng_ust_ring_buffer_ctx
*ctx
)
133 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
134 struct metadata_packet_header
*header
=
135 (struct metadata_packet_header
*)
136 lib_ring_buffer_offset_address(&buf
->backend
,
137 subbuf_idx
* chan
->backend
.subbuf_size
,
139 unsigned long records_lost
= 0;
144 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
145 header
->packet_size
= LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
147 * We do not care about the records lost count, because the metadata
148 * channel waits and retry.
150 (void) lib_ring_buffer_get_records_lost_full(&client_config
, ctx
);
151 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, ctx
);
152 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, ctx
);
153 WARN_ON_ONCE(records_lost
!= 0);
156 static int client_buffer_create(
157 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
158 void *priv
__attribute__((unused
)),
159 int cpu
__attribute__((unused
)),
160 const char *name
__attribute__((unused
)),
161 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
166 static void client_buffer_finalize(
167 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
168 void *priv
__attribute__((unused
)),
169 int cpu
__attribute__((unused
)),
170 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
175 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
177 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
178 .record_header_size
= client_record_header_size
,
179 .subbuffer_header_size
= client_packet_header_size
,
180 .buffer_begin
= client_buffer_begin
,
181 .buffer_end
= client_buffer_end
,
182 .buffer_create
= client_buffer_create
,
183 .buffer_finalize
= client_buffer_finalize
,
187 static const struct lttng_ust_ring_buffer_config client_config
= {
188 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
189 .cb
.record_header_size
= client_record_header_size
,
190 .cb
.subbuffer_header_size
= client_packet_header_size
,
191 .cb
.buffer_begin
= client_buffer_begin
,
192 .cb
.buffer_end
= client_buffer_end
,
193 .cb
.buffer_create
= client_buffer_create
,
194 .cb
.buffer_finalize
= client_buffer_finalize
,
197 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
198 .sync
= RING_BUFFER_SYNC_GLOBAL
,
199 .mode
= RING_BUFFER_MODE_TEMPLATE
,
200 .backend
= RING_BUFFER_PAGE
,
201 .output
= RING_BUFFER_MMAP
,
202 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
203 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
204 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
205 .client_type
= LTTNG_CLIENT_TYPE
,
207 .cb_ptr
= &client_cb
.parent
,
211 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
213 size_t subbuf_size
, size_t num_subbuf
,
214 unsigned int switch_timer_interval
,
215 unsigned int read_timer_interval
,
218 const int *stream_fds
, int nr_stream_fds
,
219 int64_t blocking_timeout
)
221 struct lttng_ust_abi_channel_config chan_priv_init
;
222 struct lttng_ust_shm_handle
*handle
;
223 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
225 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
228 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
229 lttng_chan_buf
->priv
->id
= chan_id
;
231 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
232 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
233 chan_priv_init
.id
= chan_id
;
235 handle
= channel_create(&client_config
, name
,
236 __alignof__(struct lttng_ust_channel_buffer
),
237 sizeof(struct lttng_ust_channel_buffer
),
239 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
240 switch_timer_interval
, read_timer_interval
,
241 stream_fds
, nr_stream_fds
, blocking_timeout
);
244 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
245 return lttng_chan_buf
;
248 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
253 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
255 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
256 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
260 int lttng_event_reserve(struct lttng_ust_ring_buffer_ctx
*ctx
)
264 memset(&URCU_TLS(private_ctx
), 0, sizeof(struct lttng_ust_ring_buffer_ctx_private
));
265 URCU_TLS(private_ctx
).pub
= ctx
;
266 URCU_TLS(private_ctx
).chan
= ctx
->client_priv
;
267 ctx
->priv
= &URCU_TLS(private_ctx
);
268 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
271 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
272 &ctx
->priv
->backend_pages
))
278 void lttng_event_commit(struct lttng_ust_ring_buffer_ctx
*ctx
)
280 lib_ring_buffer_commit(&client_config
, ctx
);
284 void lttng_event_write(struct lttng_ust_ring_buffer_ctx
*ctx
,
285 const void *src
, size_t len
, size_t alignment
)
287 lttng_ust_ring_buffer_align_ctx(ctx
, alignment
);
288 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
292 size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer
*chan
)
294 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
295 unsigned long o_begin
;
296 struct lttng_ust_ring_buffer
*buf
;
298 buf
= shmp(rb_chan
->handle
, rb_chan
->backend
.buf
[0].shmp
); /* Only for global buffer ! */
299 o_begin
= v_read(&client_config
, &buf
->offset
);
300 if (subbuf_offset(o_begin
, rb_chan
) != 0) {
301 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
);
303 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
)
304 - sizeof(struct metadata_packet_header
);
309 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
311 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
313 return lib_ring_buffer_channel_is_finalized(rb_chan
);
317 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
319 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
321 return lib_ring_buffer_channel_is_disabled(rb_chan
);
325 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
327 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
328 struct lttng_ust_ring_buffer
*buf
;
329 int shm_fd
, wait_fd
, wakeup_fd
;
330 uint64_t memory_map_size
;
331 void *memory_map_addr
;
333 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
334 0, rb_chan
->handle
, &shm_fd
, &wait_fd
, &wakeup_fd
,
335 &memory_map_size
, &memory_map_addr
);
336 lib_ring_buffer_switch(&client_config
, buf
,
337 SWITCH_ACTIVE
, rb_chan
->handle
);
341 static struct lttng_transport lttng_relay_transport
= {
342 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
344 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
346 .priv
= LTTNG_UST_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
347 .pub
= <tng_relay_transport
.ops
,
348 .channel_create
= _channel_create
,
349 .channel_destroy
= lttng_channel_destroy
,
350 .packet_avail_size
= lttng_packet_avail_size
,
351 .is_finalized
= lttng_is_finalized
,
352 .is_disabled
= lttng_is_disabled
,
353 .flush_buffer
= lttng_flush_buffer
,
355 .event_reserve
= lttng_event_reserve
,
356 .event_commit
= lttng_event_commit
,
357 .event_write
= lttng_event_write
,
359 .client_config
= &client_config
,
362 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
364 DBG("LTT : ltt ring buffer client \"%s\" init\n",
365 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
366 lttng_transport_register(<tng_relay_transport
);
369 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
371 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
372 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
373 lttng_transport_unregister(<tng_relay_transport
);