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 "lib/lttng-ust/events.h"
14 #include "common/bitfield.h"
15 #include "common/align.h"
16 #include "lttng-tracer.h"
17 #include "common/ringbuffer/frontend_types.h"
18 #include <urcu/tls-compat.h>
20 struct metadata_packet_header
{
21 uint32_t magic
; /* 0x75D11D57 */
22 uint8_t uuid
[LTTNG_UST_UUID_LEN
]; /* Unique Universal Identifier */
23 uint32_t checksum
; /* 0 if unused */
24 uint32_t content_size
; /* in bits */
25 uint32_t packet_size
; /* in bits */
26 uint8_t compression_scheme
; /* 0 if unused */
27 uint8_t encryption_scheme
; /* 0 if unused */
28 uint8_t checksum_scheme
; /* 0 if unused */
29 uint8_t major
; /* CTF spec major version number */
30 uint8_t minor
; /* CTF spec minor version number */
31 uint8_t header_end
[0];
34 struct metadata_record_header
{
35 uint8_t header_end
[0]; /* End of header */
38 static const struct lttng_ust_ring_buffer_config client_config
;
40 /* No nested use supported for metadata ring buffer. */
41 static DEFINE_URCU_TLS(struct lttng_ust_ring_buffer_ctx_private
, private_ctx
);
43 static inline uint64_t lib_ring_buffer_clock_read(
44 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)))
50 size_t record_header_size(
51 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
52 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
53 size_t offset
__attribute__((unused
)),
54 size_t *pre_header_padding
__attribute__((unused
)),
55 struct lttng_ust_ring_buffer_ctx
*ctx
__attribute__((unused
)),
56 void *client_ctx
__attribute__((unused
)))
61 #include "common/ringbuffer/api.h"
62 #include "lttng-rb-clients.h"
64 static uint64_t client_ring_buffer_clock_read(
65 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)))
71 size_t client_record_header_size(
72 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
73 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
74 size_t offset
__attribute__((unused
)),
75 size_t *pre_header_padding
__attribute__((unused
)),
76 struct lttng_ust_ring_buffer_ctx
*ctx
__attribute__((unused
)),
77 void *client_ctx
__attribute__((unused
)))
83 * client_packet_header_size - called on buffer-switch to a new sub-buffer
85 * Return header size without padding after the structure. Don't use packed
86 * structure because gcc generates inefficient code on some architectures
89 static size_t client_packet_header_size(void)
91 return offsetof(struct metadata_packet_header
, header_end
);
94 static void client_buffer_begin(struct lttng_ust_ring_buffer
*buf
,
95 uint64_t tsc
__attribute__((unused
)),
96 unsigned int subbuf_idx
,
97 struct lttng_ust_shm_handle
*handle
)
99 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
100 struct metadata_packet_header
*header
=
101 (struct metadata_packet_header
*)
102 lib_ring_buffer_offset_address(&buf
->backend
,
103 subbuf_idx
* chan
->backend
.subbuf_size
,
105 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
110 header
->magic
= TSDL_MAGIC_NUMBER
;
111 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
112 header
->checksum
= 0; /* 0 if unused */
113 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
114 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
115 header
->compression_scheme
= 0; /* 0 if unused */
116 header
->encryption_scheme
= 0; /* 0 if unused */
117 header
->checksum_scheme
= 0; /* 0 if unused */
118 header
->major
= CTF_SPEC_MAJOR
;
119 header
->minor
= CTF_SPEC_MINOR
;
123 * offset is assumed to never be 0 here : never deliver a completely empty
124 * subbuffer. data_size is between 1 and subbuf_size.
126 static void client_buffer_end(struct lttng_ust_ring_buffer
*buf
,
127 uint64_t tsc
__attribute__((unused
)),
128 unsigned int subbuf_idx
, unsigned long data_size
,
129 struct lttng_ust_shm_handle
*handle
)
131 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
132 struct metadata_packet_header
*header
=
133 (struct metadata_packet_header
*)
134 lib_ring_buffer_offset_address(&buf
->backend
,
135 subbuf_idx
* chan
->backend
.subbuf_size
,
137 unsigned long records_lost
= 0;
142 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
143 header
->packet_size
= LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
145 * We do not care about the records lost count, because the metadata
146 * channel waits and retry.
148 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
149 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
150 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
151 WARN_ON_ONCE(records_lost
!= 0);
154 static int client_buffer_create(
155 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
156 void *priv
__attribute__((unused
)),
157 int cpu
__attribute__((unused
)),
158 const char *name
__attribute__((unused
)),
159 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
164 static void client_buffer_finalize(
165 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
166 void *priv
__attribute__((unused
)),
167 int cpu
__attribute__((unused
)),
168 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
173 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
175 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
176 .record_header_size
= client_record_header_size
,
177 .subbuffer_header_size
= client_packet_header_size
,
178 .buffer_begin
= client_buffer_begin
,
179 .buffer_end
= client_buffer_end
,
180 .buffer_create
= client_buffer_create
,
181 .buffer_finalize
= client_buffer_finalize
,
185 static const struct lttng_ust_ring_buffer_config client_config
= {
186 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
187 .cb
.record_header_size
= client_record_header_size
,
188 .cb
.subbuffer_header_size
= client_packet_header_size
,
189 .cb
.buffer_begin
= client_buffer_begin
,
190 .cb
.buffer_end
= client_buffer_end
,
191 .cb
.buffer_create
= client_buffer_create
,
192 .cb
.buffer_finalize
= client_buffer_finalize
,
195 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
196 .sync
= RING_BUFFER_SYNC_GLOBAL
,
197 .mode
= RING_BUFFER_MODE_TEMPLATE
,
198 .backend
= RING_BUFFER_PAGE
,
199 .output
= RING_BUFFER_MMAP
,
200 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
201 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
202 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
203 .client_type
= LTTNG_CLIENT_TYPE
,
205 .cb_ptr
= &client_cb
.parent
,
209 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
211 size_t subbuf_size
, size_t num_subbuf
,
212 unsigned int switch_timer_interval
,
213 unsigned int read_timer_interval
,
216 const int *stream_fds
, int nr_stream_fds
,
217 int64_t blocking_timeout
)
219 struct lttng_ust_abi_channel_config chan_priv_init
;
220 struct lttng_ust_shm_handle
*handle
;
221 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
223 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
226 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
227 lttng_chan_buf
->priv
->id
= chan_id
;
229 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
230 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
231 chan_priv_init
.id
= chan_id
;
233 handle
= channel_create(&client_config
, name
,
234 __alignof__(struct lttng_ust_channel_buffer
),
235 sizeof(struct lttng_ust_channel_buffer
),
237 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
238 switch_timer_interval
, read_timer_interval
,
239 stream_fds
, nr_stream_fds
, blocking_timeout
);
242 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
243 return lttng_chan_buf
;
246 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
251 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
253 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
254 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
258 int lttng_event_reserve(struct lttng_ust_ring_buffer_ctx
*ctx
)
262 memset(&URCU_TLS(private_ctx
), 0, sizeof(struct lttng_ust_ring_buffer_ctx_private
));
263 URCU_TLS(private_ctx
).pub
= ctx
;
264 URCU_TLS(private_ctx
).chan
= ctx
->client_priv
;
265 ctx
->priv
= &URCU_TLS(private_ctx
);
266 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
269 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
270 &ctx
->priv
->backend_pages
))
276 void lttng_event_commit(struct lttng_ust_ring_buffer_ctx
*ctx
)
278 lib_ring_buffer_commit(&client_config
, ctx
);
282 void lttng_event_write(struct lttng_ust_ring_buffer_ctx
*ctx
,
283 const void *src
, size_t len
, size_t alignment
)
285 lttng_ust_ring_buffer_align_ctx(ctx
, alignment
);
286 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
290 size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer
*chan
)
292 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
293 unsigned long o_begin
;
294 struct lttng_ust_ring_buffer
*buf
;
296 buf
= shmp(rb_chan
->handle
, rb_chan
->backend
.buf
[0].shmp
); /* Only for global buffer ! */
297 o_begin
= v_read(&client_config
, &buf
->offset
);
298 if (subbuf_offset(o_begin
, rb_chan
) != 0) {
299 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
);
301 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
)
302 - sizeof(struct metadata_packet_header
);
307 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
309 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
311 return lib_ring_buffer_channel_is_finalized(rb_chan
);
315 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
317 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
319 return lib_ring_buffer_channel_is_disabled(rb_chan
);
323 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
325 struct lttng_ust_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
326 struct lttng_ust_ring_buffer
*buf
;
327 int shm_fd
, wait_fd
, wakeup_fd
;
328 uint64_t memory_map_size
;
330 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
331 0, rb_chan
->handle
, &shm_fd
, &wait_fd
, &wakeup_fd
,
333 lib_ring_buffer_switch(&client_config
, buf
,
334 SWITCH_ACTIVE
, rb_chan
->handle
);
338 static struct lttng_transport lttng_relay_transport
= {
339 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
341 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
343 .priv
= LTTNG_UST_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
344 .pub
= <tng_relay_transport
.ops
,
345 .channel_create
= _channel_create
,
346 .channel_destroy
= lttng_channel_destroy
,
347 .packet_avail_size
= lttng_packet_avail_size
,
348 .is_finalized
= lttng_is_finalized
,
349 .is_disabled
= lttng_is_disabled
,
350 .flush_buffer
= lttng_flush_buffer
,
352 .event_reserve
= lttng_event_reserve
,
353 .event_commit
= lttng_event_commit
,
354 .event_write
= lttng_event_write
,
356 .client_config
= &client_config
,
359 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
361 DBG("LTT : ltt ring buffer client \"%s\" init\n",
362 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
363 lttng_transport_register(<tng_relay_transport
);
366 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
368 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
369 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
370 lttng_transport_unregister(<tng_relay_transport
);