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"
19 struct metadata_packet_header
{
20 uint32_t magic
; /* 0x75D11D57 */
21 uint8_t uuid
[LTTNG_UST_UUID_LEN
]; /* Unique Universal Identifier */
22 uint32_t checksum
; /* 0 if unused */
23 uint32_t content_size
; /* in bits */
24 uint32_t packet_size
; /* in bits */
25 uint8_t compression_scheme
; /* 0 if unused */
26 uint8_t encryption_scheme
; /* 0 if unused */
27 uint8_t checksum_scheme
; /* 0 if unused */
28 uint8_t major
; /* CTF spec major version number */
29 uint8_t minor
; /* CTF spec minor version number */
30 uint8_t header_end
[0];
33 struct metadata_record_header
{
34 uint8_t header_end
[0]; /* End of header */
37 static const struct lttng_ust_lib_ring_buffer_config client_config
;
39 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
45 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
46 struct lttng_ust_lib_ring_buffer_channel
*chan
,
48 size_t *pre_header_padding
,
49 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
55 #include "../libringbuffer/api.h"
56 #include "lttng-rb-clients.h"
58 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
64 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
65 struct lttng_ust_lib_ring_buffer_channel
*chan
,
67 size_t *pre_header_padding
,
68 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
75 * client_packet_header_size - called on buffer-switch to a new sub-buffer
77 * Return header size without padding after the structure. Don't use packed
78 * structure because gcc generates inefficient code on some architectures
81 static size_t client_packet_header_size(void)
83 return offsetof(struct metadata_packet_header
, header_end
);
86 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
87 unsigned int subbuf_idx
,
88 struct lttng_ust_shm_handle
*handle
)
90 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
91 struct metadata_packet_header
*header
=
92 (struct metadata_packet_header
*)
93 lib_ring_buffer_offset_address(&buf
->backend
,
94 subbuf_idx
* chan
->backend
.subbuf_size
,
96 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
101 header
->magic
= TSDL_MAGIC_NUMBER
;
102 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
103 header
->checksum
= 0; /* 0 if unused */
104 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
105 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
106 header
->compression_scheme
= 0; /* 0 if unused */
107 header
->encryption_scheme
= 0; /* 0 if unused */
108 header
->checksum_scheme
= 0; /* 0 if unused */
109 header
->major
= CTF_SPEC_MAJOR
;
110 header
->minor
= CTF_SPEC_MINOR
;
114 * offset is assumed to never be 0 here : never deliver a completely empty
115 * subbuffer. data_size is between 1 and subbuf_size.
117 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
118 unsigned int subbuf_idx
, unsigned long data_size
,
119 struct lttng_ust_shm_handle
*handle
)
121 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
122 struct metadata_packet_header
*header
=
123 (struct metadata_packet_header
*)
124 lib_ring_buffer_offset_address(&buf
->backend
,
125 subbuf_idx
* chan
->backend
.subbuf_size
,
127 unsigned long records_lost
= 0;
132 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
133 header
->packet_size
= LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
135 * We do not care about the records lost count, because the metadata
136 * channel waits and retry.
138 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
139 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
140 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
141 WARN_ON_ONCE(records_lost
!= 0);
144 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
145 int cpu
, const char *name
,
146 struct lttng_ust_shm_handle
*handle
)
151 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
,
153 struct lttng_ust_shm_handle
*handle
)
158 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
160 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
161 .record_header_size
= client_record_header_size
,
162 .subbuffer_header_size
= client_packet_header_size
,
163 .buffer_begin
= client_buffer_begin
,
164 .buffer_end
= client_buffer_end
,
165 .buffer_create
= client_buffer_create
,
166 .buffer_finalize
= client_buffer_finalize
,
170 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
171 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
172 .cb
.record_header_size
= client_record_header_size
,
173 .cb
.subbuffer_header_size
= client_packet_header_size
,
174 .cb
.buffer_begin
= client_buffer_begin
,
175 .cb
.buffer_end
= client_buffer_end
,
176 .cb
.buffer_create
= client_buffer_create
,
177 .cb
.buffer_finalize
= client_buffer_finalize
,
180 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
181 .sync
= RING_BUFFER_SYNC_GLOBAL
,
182 .mode
= RING_BUFFER_MODE_TEMPLATE
,
183 .backend
= RING_BUFFER_PAGE
,
184 .output
= RING_BUFFER_MMAP
,
185 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
186 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
187 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
188 .client_type
= LTTNG_CLIENT_TYPE
,
190 .cb_ptr
= &client_cb
.parent
,
194 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
196 size_t subbuf_size
, size_t num_subbuf
,
197 unsigned int switch_timer_interval
,
198 unsigned int read_timer_interval
,
201 const int *stream_fds
, int nr_stream_fds
,
202 int64_t blocking_timeout
)
204 struct lttng_ust_abi_channel_config chan_priv_init
;
205 struct lttng_ust_shm_handle
*handle
;
206 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
208 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
211 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
212 lttng_chan_buf
->priv
->id
= chan_id
;
214 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
215 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
216 chan_priv_init
.id
= chan_id
;
218 handle
= channel_create(&client_config
, name
,
219 __alignof__(struct lttng_ust_channel_buffer
),
220 sizeof(struct lttng_ust_channel_buffer
),
222 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
223 switch_timer_interval
, read_timer_interval
,
224 stream_fds
, nr_stream_fds
, blocking_timeout
);
227 lttng_chan_buf
->priv
->rb_chan
= shmp(handle
, handle
->chan
);
228 return lttng_chan_buf
;
231 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
236 void lttng_channel_destroy(struct lttng_ust_channel_buffer
*lttng_chan_buf
)
238 channel_destroy(lttng_chan_buf
->priv
->rb_chan
, lttng_chan_buf
->priv
->rb_chan
->handle
, 1);
239 lttng_ust_free_channel_common(lttng_chan_buf
->parent
);
243 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
247 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
250 if (lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
251 &ctx
->backend_pages
))
257 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
259 lib_ring_buffer_commit(&client_config
, ctx
);
263 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
266 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
270 size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer
*chan
)
272 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
273 unsigned long o_begin
;
274 struct lttng_ust_lib_ring_buffer
*buf
;
276 buf
= shmp(rb_chan
->handle
, rb_chan
->backend
.buf
[0].shmp
); /* Only for global buffer ! */
277 o_begin
= v_read(&client_config
, &buf
->offset
);
278 if (subbuf_offset(o_begin
, rb_chan
) != 0) {
279 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
);
281 return rb_chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, rb_chan
)
282 - sizeof(struct metadata_packet_header
);
287 int lttng_is_finalized(struct lttng_ust_channel_buffer
*chan
)
289 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
291 return lib_ring_buffer_channel_is_finalized(rb_chan
);
295 int lttng_is_disabled(struct lttng_ust_channel_buffer
*chan
)
297 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
299 return lib_ring_buffer_channel_is_disabled(rb_chan
);
303 int lttng_flush_buffer(struct lttng_ust_channel_buffer
*chan
)
305 struct lttng_ust_lib_ring_buffer_channel
*rb_chan
= chan
->priv
->rb_chan
;
306 struct lttng_ust_lib_ring_buffer
*buf
;
307 int shm_fd
, wait_fd
, wakeup_fd
;
308 uint64_t memory_map_size
;
310 buf
= channel_get_ring_buffer(&client_config
, rb_chan
,
311 0, rb_chan
->handle
, &shm_fd
, &wait_fd
, &wakeup_fd
,
313 lib_ring_buffer_switch(&client_config
, buf
,
314 SWITCH_ACTIVE
, rb_chan
->handle
);
318 static struct lttng_transport lttng_relay_transport
= {
319 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
321 .struct_size
= sizeof(struct lttng_ust_channel_buffer_ops
),
323 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private
, {
324 .pub
= <tng_relay_transport
.ops
,
325 .channel_create
= _channel_create
,
326 .channel_destroy
= lttng_channel_destroy
,
327 .packet_avail_size
= lttng_packet_avail_size
,
328 .is_finalized
= lttng_is_finalized
,
329 .is_disabled
= lttng_is_disabled
,
330 .flush_buffer
= lttng_flush_buffer
,
332 .event_reserve
= lttng_event_reserve
,
333 .event_commit
= lttng_event_commit
,
334 .event_write
= lttng_event_write
,
336 .client_config
= &client_config
,
339 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
341 DBG("LTT : ltt ring buffer client \"%s\" init\n",
342 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
343 lttng_transport_register(<tng_relay_transport
);
346 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
348 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
349 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
350 lttng_transport_unregister(<tng_relay_transport
);