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.
12 #include <ust-events-internal.h>
13 #include "ust-bitfield.h"
14 #include "ust-compat.h"
15 #include "lttng-tracer.h"
16 #include "../libringbuffer/frontend_types.h"
18 struct metadata_packet_header
{
19 uint32_t magic
; /* 0x75D11D57 */
20 uint8_t uuid
[LTTNG_UST_UUID_LEN
]; /* Unique Universal Identifier */
21 uint32_t checksum
; /* 0 if unused */
22 uint32_t content_size
; /* in bits */
23 uint32_t packet_size
; /* in bits */
24 uint8_t compression_scheme
; /* 0 if unused */
25 uint8_t encryption_scheme
; /* 0 if unused */
26 uint8_t checksum_scheme
; /* 0 if unused */
27 uint8_t major
; /* CTF spec major version number */
28 uint8_t minor
; /* CTF spec minor version number */
29 uint8_t header_end
[0];
32 struct metadata_record_header
{
33 uint8_t header_end
[0]; /* End of header */
36 static const struct lttng_ust_lib_ring_buffer_config client_config
;
38 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
44 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
45 struct lttng_ust_lib_ring_buffer_channel
*chan
,
47 size_t *pre_header_padding
,
48 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
54 #include "../libringbuffer/api.h"
55 #include "lttng-rb-clients.h"
57 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
63 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
64 struct lttng_ust_lib_ring_buffer_channel
*chan
,
66 size_t *pre_header_padding
,
67 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
74 * client_packet_header_size - called on buffer-switch to a new sub-buffer
76 * Return header size without padding after the structure. Don't use packed
77 * structure because gcc generates inefficient code on some architectures
80 static size_t client_packet_header_size(void)
82 return offsetof(struct metadata_packet_header
, header_end
);
85 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
86 unsigned int subbuf_idx
,
87 struct lttng_ust_shm_handle
*handle
)
89 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
90 struct metadata_packet_header
*header
=
91 (struct metadata_packet_header
*)
92 lib_ring_buffer_offset_address(&buf
->backend
,
93 subbuf_idx
* chan
->backend
.subbuf_size
,
95 struct lttng_ust_channel_buffer
*lttng_chan
= channel_get_private(chan
);
100 header
->magic
= TSDL_MAGIC_NUMBER
;
101 memcpy(header
->uuid
, lttng_chan
->priv
->uuid
, sizeof(lttng_chan
->priv
->uuid
));
102 header
->checksum
= 0; /* 0 if unused */
103 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
104 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
105 header
->compression_scheme
= 0; /* 0 if unused */
106 header
->encryption_scheme
= 0; /* 0 if unused */
107 header
->checksum_scheme
= 0; /* 0 if unused */
108 header
->major
= CTF_SPEC_MAJOR
;
109 header
->minor
= CTF_SPEC_MINOR
;
113 * offset is assumed to never be 0 here : never deliver a completely empty
114 * subbuffer. data_size is between 1 and subbuf_size.
116 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
117 unsigned int subbuf_idx
, unsigned long data_size
,
118 struct lttng_ust_shm_handle
*handle
)
120 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
121 struct metadata_packet_header
*header
=
122 (struct metadata_packet_header
*)
123 lib_ring_buffer_offset_address(&buf
->backend
,
124 subbuf_idx
* chan
->backend
.subbuf_size
,
126 unsigned long records_lost
= 0;
131 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
132 header
->packet_size
= LTTNG_UST_PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
134 * We do not care about the records lost count, because the metadata
135 * channel waits and retry.
137 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
138 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
139 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
140 WARN_ON_ONCE(records_lost
!= 0);
143 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
144 int cpu
, const char *name
,
145 struct lttng_ust_shm_handle
*handle
)
150 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
,
152 struct lttng_ust_shm_handle
*handle
)
157 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb
= {
159 .ring_buffer_clock_read
= client_ring_buffer_clock_read
,
160 .record_header_size
= client_record_header_size
,
161 .subbuffer_header_size
= client_packet_header_size
,
162 .buffer_begin
= client_buffer_begin
,
163 .buffer_end
= client_buffer_end
,
164 .buffer_create
= client_buffer_create
,
165 .buffer_finalize
= client_buffer_finalize
,
169 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
170 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
171 .cb
.record_header_size
= client_record_header_size
,
172 .cb
.subbuffer_header_size
= client_packet_header_size
,
173 .cb
.buffer_begin
= client_buffer_begin
,
174 .cb
.buffer_end
= client_buffer_end
,
175 .cb
.buffer_create
= client_buffer_create
,
176 .cb
.buffer_finalize
= client_buffer_finalize
,
179 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
180 .sync
= RING_BUFFER_SYNC_GLOBAL
,
181 .mode
= RING_BUFFER_MODE_TEMPLATE
,
182 .backend
= RING_BUFFER_PAGE
,
183 .output
= RING_BUFFER_MMAP
,
184 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
185 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
186 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
187 .client_type
= LTTNG_CLIENT_TYPE
,
189 .cb_ptr
= &client_cb
.parent
,
193 struct lttng_ust_channel_buffer
*_channel_create(const char *name
,
195 size_t subbuf_size
, size_t num_subbuf
,
196 unsigned int switch_timer_interval
,
197 unsigned int read_timer_interval
,
200 const int *stream_fds
, int nr_stream_fds
,
201 int64_t blocking_timeout
)
203 struct lttng_ust_abi_channel_config chan_priv_init
;
204 struct lttng_ust_shm_handle
*handle
;
205 struct lttng_ust_channel_buffer
*lttng_chan_buf
;
207 lttng_chan_buf
= lttng_ust_alloc_channel_buffer();
210 memcpy(lttng_chan_buf
->priv
->uuid
, uuid
, LTTNG_UST_UUID_LEN
);
211 lttng_chan_buf
->priv
->id
= chan_id
;
213 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
214 memcpy(chan_priv_init
.uuid
, uuid
, LTTNG_UST_UUID_LEN
);
215 chan_priv_init
.id
= chan_id
;
217 handle
= channel_create(&client_config
, name
,
218 __alignof__(struct lttng_ust_channel_buffer
),
219 sizeof(struct lttng_ust_channel_buffer
),
221 lttng_chan_buf
, buf_addr
, subbuf_size
, num_subbuf
,
222 switch_timer_interval
, read_timer_interval
,
223 stream_fds
, nr_stream_fds
, blocking_timeout
);
226 lttng_chan_buf
->handle
= handle
;
227 lttng_chan_buf
->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
->chan
, lttng_chan_buf
->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_lib_ring_buffer_channel
*chan
,
271 struct lttng_ust_shm_handle
*handle
)
273 unsigned long o_begin
;
274 struct lttng_ust_lib_ring_buffer
*buf
;
276 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
); /* Only for global buffer ! */
277 o_begin
= v_read(&client_config
, &buf
->offset
);
278 if (subbuf_offset(o_begin
, chan
) != 0) {
279 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
281 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
282 - sizeof(struct metadata_packet_header
);
288 wait_queue_head_t
*lttng_get_reader_wait_queue(struct lttng_ust_lib_ring_buffer_channel
*chan
)
290 return &chan
->read_wait
;
294 wait_queue_head_t
*lttng_get_hp_wait_queue(struct lttng_ust_lib_ring_buffer_channel
*chan
)
296 return &chan
->hp_wait
;
301 int lttng_is_finalized(struct lttng_ust_lib_ring_buffer_channel
*chan
)
303 return lib_ring_buffer_channel_is_finalized(chan
);
307 int lttng_is_disabled(struct lttng_ust_lib_ring_buffer_channel
*chan
)
309 return lib_ring_buffer_channel_is_disabled(chan
);
313 int lttng_flush_buffer(struct lttng_ust_lib_ring_buffer_channel
*chan
,
314 struct lttng_ust_shm_handle
*handle
)
316 struct lttng_ust_lib_ring_buffer
*buf
;
317 int shm_fd
, wait_fd
, wakeup_fd
;
318 uint64_t memory_map_size
;
320 buf
= channel_get_ring_buffer(&client_config
, chan
,
321 0, handle
, &shm_fd
, &wait_fd
, &wakeup_fd
,
323 lib_ring_buffer_switch(&client_config
, buf
,
324 SWITCH_ACTIVE
, handle
);
328 static struct lttng_transport lttng_relay_transport
= {
329 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
331 .struct_size
= sizeof(struct lttng_ust_channel_ops
),
333 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_ops_private
, {
334 .pub
= <tng_relay_transport
.ops
,
335 .channel_create
= _channel_create
,
336 .channel_destroy
= lttng_channel_destroy
,
337 .packet_avail_size
= lttng_packet_avail_size
,
338 .is_finalized
= lttng_is_finalized
,
339 .is_disabled
= lttng_is_disabled
,
340 .flush_buffer
= lttng_flush_buffer
,
342 .event_reserve
= lttng_event_reserve
,
343 .event_commit
= lttng_event_commit
,
344 .event_write
= lttng_event_write
,
346 .client_config
= &client_config
,
349 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
351 DBG("LTT : ltt ring buffer client \"%s\" init\n",
352 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
353 lttng_transport_register(<tng_relay_transport
);
356 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
358 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
359 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap");
360 lttng_transport_unregister(<tng_relay_transport
);