1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * lttng-ring-buffer-client.h
5 * LTTng lib ring buffer client template.
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
13 #include <lttng/events.h>
14 #include <lttng/events-internal.h>
15 #include <lttng/tracer.h>
17 static struct lttng_transport lttng_relay_transport
;
19 struct metadata_packet_header
{
20 uint32_t magic
; /* 0x75D11D57 */
21 uint8_t uuid
[16]; /* 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_kernel_ring_buffer_config client_config
;
40 u64
lib_ring_buffer_clock_read(struct lttng_kernel_ring_buffer_channel
*chan
)
46 size_t record_header_size(const struct lttng_kernel_ring_buffer_config
*config
,
47 struct lttng_kernel_ring_buffer_channel
*chan
, size_t offset
,
48 size_t *pre_header_padding
,
49 struct lttng_kernel_ring_buffer_ctx
*ctx
,
55 #include <ringbuffer/api.h>
57 static u64
client_ring_buffer_clock_read(struct lttng_kernel_ring_buffer_channel
*chan
)
63 size_t client_record_header_size(const struct lttng_kernel_ring_buffer_config
*config
,
64 struct lttng_kernel_ring_buffer_channel
*chan
, size_t offset
,
65 size_t *pre_header_padding
,
66 struct lttng_kernel_ring_buffer_ctx
*ctx
,
73 * client_packet_header_size - called on buffer-switch to a new sub-buffer
75 * Return header size without padding after the structure. Don't use packed
76 * structure because gcc generates inefficient code on some architectures
79 static size_t client_packet_header_size(void)
81 return offsetof(struct metadata_packet_header
, header_end
);
84 static void client_buffer_begin(struct lttng_kernel_ring_buffer
*buf
, u64 tsc
,
85 unsigned int subbuf_idx
)
87 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
88 struct metadata_packet_header
*header
=
89 (struct metadata_packet_header
*)
90 lib_ring_buffer_offset_address(&buf
->backend
,
91 subbuf_idx
* chan
->backend
.subbuf_size
);
92 struct lttng_metadata_cache
*metadata_cache
=
93 channel_get_private(chan
);
95 header
->magic
= TSDL_MAGIC_NUMBER
;
96 memcpy(header
->uuid
, metadata_cache
->uuid
.b
,
97 sizeof(metadata_cache
->uuid
));
98 header
->checksum
= 0; /* 0 if unused */
99 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
100 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
101 header
->compression_scheme
= 0; /* 0 if unused */
102 header
->encryption_scheme
= 0; /* 0 if unused */
103 header
->checksum_scheme
= 0; /* 0 if unused */
104 header
->major
= CTF_SPEC_MAJOR
;
105 header
->minor
= CTF_SPEC_MINOR
;
109 * offset is assumed to never be 0 here : never deliver a completely empty
110 * subbuffer. data_size is between 1 and subbuf_size.
112 static void client_buffer_end(struct lttng_kernel_ring_buffer
*buf
, u64 tsc
,
113 unsigned int subbuf_idx
, unsigned long data_size
)
115 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
116 struct metadata_packet_header
*header
=
117 (struct metadata_packet_header
*)
118 lib_ring_buffer_offset_address(&buf
->backend
,
119 subbuf_idx
* chan
->backend
.subbuf_size
);
120 unsigned long records_lost
= 0;
122 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
123 header
->packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
125 * We do not care about the records lost count, because the metadata
126 * channel waits and retry.
128 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
129 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
130 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
131 WARN_ON_ONCE(records_lost
!= 0);
134 static int client_buffer_create(struct lttng_kernel_ring_buffer
*buf
, void *priv
,
135 int cpu
, const char *name
)
140 static void client_buffer_finalize(struct lttng_kernel_ring_buffer
*buf
, void *priv
, int cpu
)
144 static int client_timestamp_begin(const struct lttng_kernel_ring_buffer_config
*config
,
145 struct lttng_kernel_ring_buffer
*buf
, uint64_t *timestamp_begin
)
150 static int client_timestamp_end(const struct lttng_kernel_ring_buffer_config
*config
,
151 struct lttng_kernel_ring_buffer
*bufb
,
152 uint64_t *timestamp_end
)
157 static int client_events_discarded(const struct lttng_kernel_ring_buffer_config
*config
,
158 struct lttng_kernel_ring_buffer
*bufb
,
159 uint64_t *events_discarded
)
164 static int client_current_timestamp(const struct lttng_kernel_ring_buffer_config
*config
,
165 struct lttng_kernel_ring_buffer
*bufb
,
171 static int client_content_size(const struct lttng_kernel_ring_buffer_config
*config
,
172 struct lttng_kernel_ring_buffer
*bufb
,
173 uint64_t *content_size
)
178 static int client_packet_size(const struct lttng_kernel_ring_buffer_config
*config
,
179 struct lttng_kernel_ring_buffer
*bufb
,
180 uint64_t *packet_size
)
185 static int client_stream_id(const struct lttng_kernel_ring_buffer_config
*config
,
186 struct lttng_kernel_ring_buffer
*bufb
,
192 static int client_sequence_number(const struct lttng_kernel_ring_buffer_config
*config
,
193 struct lttng_kernel_ring_buffer
*bufb
,
200 int client_instance_id(const struct lttng_kernel_ring_buffer_config
*config
,
201 struct lttng_kernel_ring_buffer
*bufb
,
207 static const struct lttng_kernel_ring_buffer_config client_config
= {
208 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
209 .cb
.record_header_size
= client_record_header_size
,
210 .cb
.subbuffer_header_size
= client_packet_header_size
,
211 .cb
.buffer_begin
= client_buffer_begin
,
212 .cb
.buffer_end
= client_buffer_end
,
213 .cb
.buffer_create
= client_buffer_create
,
214 .cb
.buffer_finalize
= client_buffer_finalize
,
217 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
218 .sync
= RING_BUFFER_SYNC_GLOBAL
,
219 .mode
= RING_BUFFER_MODE_TEMPLATE
,
220 .backend
= RING_BUFFER_PAGE
,
221 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
222 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
223 .ipi
= RING_BUFFER_IPI_BARRIER
,
224 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
228 void release_priv_ops(void *priv_ops
)
230 module_put(THIS_MODULE
);
234 void lttng_channel_destroy(struct lttng_kernel_ring_buffer_channel
*chan
)
236 channel_destroy(chan
);
240 struct lttng_kernel_ring_buffer_channel
*_channel_create(const char *name
,
241 void *priv
, void *buf_addr
,
242 size_t subbuf_size
, size_t num_subbuf
,
243 unsigned int switch_timer_interval
,
244 unsigned int read_timer_interval
)
246 struct lttng_kernel_channel_buffer
*lttng_chan
= priv
;
247 struct lttng_kernel_ring_buffer_channel
*chan
;
249 chan
= channel_create(&client_config
, name
,
250 lttng_chan
->parent
.session
->priv
->metadata_cache
, buf_addr
,
251 subbuf_size
, num_subbuf
, switch_timer_interval
,
252 read_timer_interval
);
255 * Ensure this module is not unloaded before we finish
256 * using lttng_relay_transport.ops.
258 if (!try_module_get(THIS_MODULE
)) {
259 printk(KERN_WARNING
"LTTng: Can't lock transport module.\n");
262 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
263 chan
->backend
.release_priv_ops
= release_priv_ops
;
268 lttng_channel_destroy(chan
);
273 struct lttng_kernel_ring_buffer
*lttng_buffer_read_open(struct lttng_kernel_ring_buffer_channel
*chan
)
275 struct lttng_kernel_ring_buffer
*buf
;
277 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
278 if (!lib_ring_buffer_open_read(buf
))
284 int lttng_buffer_has_read_closed_stream(struct lttng_kernel_ring_buffer_channel
*chan
)
286 struct lttng_kernel_ring_buffer
*buf
;
289 for_each_channel_cpu(cpu
, chan
) {
290 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
291 if (!atomic_long_read(&buf
->active_readers
))
298 void lttng_buffer_read_close(struct lttng_kernel_ring_buffer
*buf
)
300 lib_ring_buffer_release_read(buf
);
304 int lttng_event_reserve(struct lttng_kernel_ring_buffer_ctx
*ctx
)
306 struct lttng_kernel_ring_buffer_channel
*chan
= ctx
->client_priv
;
309 memset(&ctx
->priv
, 0, sizeof(ctx
->priv
));
310 ctx
->priv
.chan
= chan
;
312 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
315 lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
316 &ctx
->priv
.backend_pages
);
322 void lttng_event_commit(struct lttng_kernel_ring_buffer_ctx
*ctx
)
324 lib_ring_buffer_commit(&client_config
, ctx
);
328 void lttng_event_write(struct lttng_kernel_ring_buffer_ctx
*ctx
, const void *src
,
329 size_t len
, size_t alignment
)
331 lib_ring_buffer_align_ctx(ctx
, alignment
);
332 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
336 void lttng_event_write_from_user(struct lttng_kernel_ring_buffer_ctx
*ctx
,
337 const void __user
*src
, size_t len
, size_t alignment
)
339 lib_ring_buffer_align_ctx(ctx
, alignment
);
340 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
344 void lttng_event_memset(struct lttng_kernel_ring_buffer_ctx
*ctx
,
347 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
351 void lttng_event_strcpy(struct lttng_kernel_ring_buffer_ctx
*ctx
, const char *src
,
354 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
358 void lttng_event_pstrcpy_pad(struct lttng_kernel_ring_buffer_ctx
*ctx
, const char *src
,
361 lib_ring_buffer_pstrcpy(&client_config
, ctx
, src
, len
, '\0');
365 size_t lttng_packet_avail_size(struct lttng_kernel_ring_buffer_channel
*chan
)
367 unsigned long o_begin
;
368 struct lttng_kernel_ring_buffer
*buf
;
370 buf
= chan
->backend
.buf
; /* Only for global buffer ! */
371 o_begin
= v_read(&client_config
, &buf
->offset
);
372 if (subbuf_offset(o_begin
, chan
) != 0) {
373 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
375 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
376 - sizeof(struct metadata_packet_header
);
381 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct lttng_kernel_ring_buffer_channel
*chan
, int cpu
)
383 struct lttng_kernel_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
385 return &buf
->write_wait
;
389 wait_queue_head_t
*lttng_get_hp_wait_queue(struct lttng_kernel_ring_buffer_channel
*chan
)
391 return &chan
->hp_wait
;
395 int lttng_is_finalized(struct lttng_kernel_ring_buffer_channel
*chan
)
397 return lib_ring_buffer_channel_is_finalized(chan
);
401 int lttng_is_disabled(struct lttng_kernel_ring_buffer_channel
*chan
)
403 return lib_ring_buffer_channel_is_disabled(chan
);
406 static struct lttng_transport lttng_relay_transport
= {
407 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
408 .owner
= THIS_MODULE
,
410 .priv
= __LTTNG_COMPOUND_LITERAL(struct lttng_kernel_channel_buffer_ops_private
, {
411 .pub
= <tng_relay_transport
.ops
,
412 .channel_create
= _channel_create
,
413 .channel_destroy
= lttng_channel_destroy
,
414 .buffer_read_open
= lttng_buffer_read_open
,
415 .buffer_has_read_closed_stream
=
416 lttng_buffer_has_read_closed_stream
,
417 .buffer_read_close
= lttng_buffer_read_close
,
418 .packet_avail_size
= lttng_packet_avail_size
,
419 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
420 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
421 .is_finalized
= lttng_is_finalized
,
422 .is_disabled
= lttng_is_disabled
,
423 .timestamp_begin
= client_timestamp_begin
,
424 .timestamp_end
= client_timestamp_end
,
425 .events_discarded
= client_events_discarded
,
426 .content_size
= client_content_size
,
427 .packet_size
= client_packet_size
,
428 .stream_id
= client_stream_id
,
429 .current_timestamp
= client_current_timestamp
,
430 .sequence_number
= client_sequence_number
,
431 .instance_id
= client_instance_id
,
433 .event_reserve
= lttng_event_reserve
,
434 .event_commit
= lttng_event_commit
,
435 .event_write_from_user
= lttng_event_write_from_user
,
436 .event_memset
= lttng_event_memset
,
437 .event_write
= lttng_event_write
,
438 .event_strcpy
= lttng_event_strcpy
,
439 .event_pstrcpy_pad
= lttng_event_pstrcpy_pad
,
443 static int __init
lttng_ring_buffer_client_init(void)
446 * This vmalloc sync all also takes care of the lib ring buffer
447 * vmalloc'd module pages when it is built as a module into LTTng.
449 wrapper_vmalloc_sync_mappings();
450 lttng_transport_register(<tng_relay_transport
);
454 module_init(lttng_ring_buffer_client_init
);
456 static void __exit
lttng_ring_buffer_client_exit(void)
458 lttng_transport_unregister(<tng_relay_transport
);
461 module_exit(lttng_ring_buffer_client_exit
);
463 MODULE_LICENSE("GPL and additional rights");
464 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
465 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
467 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
468 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
469 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
470 LTTNG_MODULES_EXTRAVERSION
);