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 <ust-events-internal.h>
14 #include "ust-bitfield.h"
15 #include "ust-compat.h"
16 #include "lttng-tracer.h"
17 #include "../libringbuffer/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_lib_ring_buffer_config client_config
;
40 /* No nested use supported for metadata ring buffer. */
41 static DEFINE_URCU_TLS(struct lttng_ust_lib_ring_buffer_ctx_private
, private_ctx
);
43 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
49 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
50 struct lttng_ust_lib_ring_buffer_channel
*chan
,
52 size_t *pre_header_padding
,
53 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
59 #include "../libringbuffer/api.h"
60 #include "lttng-rb-clients.h"
62 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
68 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
69 struct lttng_ust_lib_ring_buffer_channel
*chan
,
71 size_t *pre_header_padding
,
72 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
79 * client_packet_header_size - called on buffer-switch to a new sub-buffer
81 * Return header size without padding after the structure. Don't use packed
82 * structure because gcc generates inefficient code on some architectures
85 static size_t client_packet_header_size(void)
87 return offsetof(struct metadata_packet_header
, header_end
);
90 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
91 unsigned int subbuf_idx
,
92 struct lttng_ust_shm_handle
*handle
)
94 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
95 struct metadata_packet_header
*header
=
96 (struct metadata_packet_header
*)
97 lib_ring_buffer_offset_address(&buf
->backend
,
98 subbuf_idx
* chan
->backend
.subbuf_size
,
100 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
105 header
->magic
= TSDL_MAGIC_NUMBER
;
106 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
107 header
->checksum
= 0; /* 0 if unused */
108 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
109 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
110 header
->compression_scheme
= 0; /* 0 if unused */
111 header
->encryption_scheme
= 0; /* 0 if unused */
112 header
->checksum_scheme
= 0; /* 0 if unused */
113 header
->major
= CTF_SPEC_MAJOR
;
114 header
->minor
= CTF_SPEC_MINOR
;
118 * offset is assumed to never be 0 here : never deliver a completely empty
119 * subbuffer. data_size is between 1 and subbuf_size.
121 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
122 unsigned int subbuf_idx
, unsigned long data_size
,
123 struct lttng_ust_shm_handle
*handle
)
125 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
126 struct metadata_packet_header
*header
=
127 (struct metadata_packet_header
*)
128 lib_ring_buffer_offset_address(&buf
->backend
,
129 subbuf_idx
* chan
->backend
.subbuf_size
,
131 unsigned long records_lost
= 0;
136 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
137 header
->packet_size
= LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
139 * We do not care about the records lost count, because the metadata
140 * channel waits and retry.
142 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
143 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
144 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
145 WARN_ON_ONCE(records_lost
!= 0);
148 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
149 int cpu
, const char *name
,
150 struct lttng_ust_shm_handle
*handle
)
155 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
,
157 struct lttng_ust_shm_handle
*handle
)
162 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
164 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
165 .record_header_size
= client_record_header_size
,
166 .subbuffer_header_size
= client_packet_header_size
,
167 .buffer_begin
= client_buffer_begin
,
168 .buffer_end
= client_buffer_end
,
169 .buffer_create
= client_buffer_create
,
170 .buffer_finalize
= client_buffer_finalize
,
174 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
175 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
176 .cb
.record_header_size
= client_record_header_size
,
177 .cb
.subbuffer_header_size
= client_packet_header_size
,
178 .cb
.buffer_begin
= client_buffer_begin
,
179 .cb
.buffer_end
= client_buffer_end
,
180 .cb
.buffer_create
= client_buffer_create
,
181 .cb
.buffer_finalize
= client_buffer_finalize
,
184 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
185 .sync
= RING_BUFFER_SYNC_GLOBAL
,
186 .mode
= RING_BUFFER_MODE_TEMPLATE
,
187 .backend
= RING_BUFFER_PAGE
,
188 .output
= RING_BUFFER_MMAP
,
189 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
190 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
191 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
192 .client_type
= LTTNG_CLIENT_TYPE
,
194 .cb_ptr
= &client_cb
.parent
,
198 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
200 size_t subbuf_size
, size_t num_subbuf
,
201 unsigned int switch_timer_interval
,
202 unsigned int read_timer_interval
,
205 const int *stream_fds
, int nr_stream_fds
,
206 int64_t blocking_timeout
)
208 struct lttng_ust_abi_channel_config chan_priv_init
;
209 struct lttng_ust_shm_handle
*handle
;
210 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
212 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
215 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
216 lttng_chan_buf
->priv
->id
= chan_id
;
218 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
219 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
220 chan_priv_init
.id
= chan_id
;
222 handle
= channel_create(&client_config
, name
,
223 __alignof__(struct lttng_ust_channel_buffer
),
224 sizeof(struct lttng_ust_channel_buffer
),
226 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
227 switch_timer_interval
, read_timer_interval
,
228 stream_fds
, nr_stream_fds
, blocking_timeout
);
231 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
232 return lttng_chan_buf
;
235 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
240 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
242 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
243 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
247 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
251 memset(&URCU_TLS(private_ctx
), 0, sizeof(struct lttng_ust_lib_ring_buffer_ctx_private
));
252 URCU_TLS(private_ctx
).pub
= ctx
;
253 URCU_TLS(private_ctx
).chan
= ctx
->client_priv
;
254 ctx
->priv
= &URCU_TLS(private_ctx
);
255 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
258 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
259 &ctx
->priv
->backend_pages
))
265 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
267 lib_ring_buffer_commit(&client_config
, ctx
);
271 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
272 const void *src
, size_t len
, size_t alignment
)
274 lttng_ust_lib_ring_buffer_align_ctx(ctx
, alignment
);
275 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
279 size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer
*chan
)
281 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
282 unsigned long o_begin
;
283 struct lttng_ust_lib_ring_buffer
*buf
;
285 buf
= shmp(rb_chan
->handle
, rb_chan
->backend
.buf
[0].shmp
); /* Only for global buffer ! */
286 o_begin
= v_read(&client_config
, &buf
->offset
);
287 if (subbuf_offset(o_begin
, rb_chan
) != 0) {
288 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
);
290 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
)
291 - sizeof(struct metadata_packet_header
);
296 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
298 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
300 return lib_ring_buffer_channel_is_finalized(rb_chan
);
304 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
306 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
308 return lib_ring_buffer_channel_is_disabled(rb_chan
);
312 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
314 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
315 struct lttng_ust_lib_ring_buffer
*buf
;
316 int shm_fd
, wait_fd
, wakeup_fd
;
317 uint64_t memory_map_size
;
319 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
320 0, rb_chan
->handle
, &shm_fd
, &wait_fd
, &wakeup_fd
,
322 lib_ring_buffer_switch(&client_config
, buf
,
323 SWITCH_ACTIVE
, rb_chan
->handle
);
327 static struct lttng_transport lttng_relay_transport
= {
328 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
330 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
332 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
333 .pub
= <tng_relay_transport
.ops
,
334 .channel_create
= _channel_create
,
335 .channel_destroy
= lttng_channel_destroy
,
336 .packet_avail_size
= lttng_packet_avail_size
,
337 .is_finalized
= lttng_is_finalized
,
338 .is_disabled
= lttng_is_disabled
,
339 .flush_buffer
= lttng_flush_buffer
,
341 .event_reserve
= lttng_event_reserve
,
342 .event_commit
= lttng_event_commit
,
343 .event_write
= lttng_event_write
,
345 .client_config
= &client_config
,
348 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
350 DBG("LTT : ltt ring buffer client \"%s\" init\n",
351 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
352 lttng_transport_register(<tng_relay_transport
);
355 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
357 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
358 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
359 lttng_transport_unregister(<tng_relay_transport
);