1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
3 * lttng-ring-buffer-trigger-client.h
5 * LTTng lib ring buffer trigger client template.
7 * Copyright (C) 2010-2020 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_all() */
13 #include <lttng-events.h>
14 #include <lttng-tracer.h>
16 static struct lttng_transport lttng_relay_transport
;
18 struct trigger_packet_header
{
19 uint32_t magic
; /* 0x75D11D57 */
20 uint32_t checksum
; /* 0 if unused */
21 uint32_t content_size
; /* in bits */
22 uint32_t packet_size
; /* in bits */
23 uint8_t compression_scheme
; /* 0 if unused */
24 uint8_t encryption_scheme
; /* 0 if unused */
25 uint8_t checksum_scheme
; /* 0 if unused */
26 uint8_t major
; /* CTF spec major version number */
27 uint8_t minor
; /* CTF spec minor version number */
28 uint8_t header_end
[0];
31 struct trigger_record_header
{
32 uint8_t header_end
[0]; /* End of header */
35 static const struct lib_ring_buffer_config client_config
;
38 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
44 size_t record_header_size(const struct lib_ring_buffer_config
*config
,
45 struct channel
*chan
, size_t offset
,
46 size_t *pre_header_padding
,
47 struct lib_ring_buffer_ctx
*ctx
,
53 #include <wrapper/ringbuffer/api.h>
55 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
61 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
62 struct channel
*chan
, size_t offset
,
63 size_t *pre_header_padding
,
64 struct lib_ring_buffer_ctx
*ctx
,
71 * client_packet_header_size - called on buffer-switch to a new sub-buffer
73 * Return header size without padding after the structure. Don't use packed
74 * structure because gcc generates inefficient code on some architectures
77 static size_t client_packet_header_size(void)
79 return offsetof(struct trigger_packet_header
, header_end
);
82 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
83 unsigned int subbuf_idx
)
85 struct channel
*chan
= buf
->backend
.chan
;
86 struct trigger_packet_header
*header
=
87 (struct trigger_packet_header
*)
88 lib_ring_buffer_offset_address(&buf
->backend
,
89 subbuf_idx
* chan
->backend
.subbuf_size
);
91 header
->magic
= TSDL_MAGIC_NUMBER
;
92 header
->checksum
= 0; /* 0 if unused */
93 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
94 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
95 header
->compression_scheme
= 0; /* 0 if unused */
96 header
->encryption_scheme
= 0; /* 0 if unused */
97 header
->checksum_scheme
= 0; /* 0 if unused */
98 header
->major
= CTF_SPEC_MAJOR
;
99 header
->minor
= CTF_SPEC_MINOR
;
103 * offset is assumed to never be 0 here : never deliver a completely empty
104 * subbuffer. data_size is between 1 and subbuf_size.
106 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
107 unsigned int subbuf_idx
, unsigned long data_size
)
109 struct channel
*chan
= buf
->backend
.chan
;
110 struct trigger_packet_header
*header
=
111 (struct trigger_packet_header
*)
112 lib_ring_buffer_offset_address(&buf
->backend
,
113 subbuf_idx
* chan
->backend
.subbuf_size
);
114 unsigned long records_lost
= 0;
116 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
117 header
->packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
119 * We do not care about the records lost count, because the trigger
120 * channel waits and retry.
122 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
123 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
124 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
125 WARN_ON_ONCE(records_lost
!= 0);
128 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
129 int cpu
, const char *name
)
134 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
138 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
139 struct lib_ring_buffer
*buf
, uint64_t *timestamp_begin
)
144 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
145 struct lib_ring_buffer
*bufb
,
146 uint64_t *timestamp_end
)
151 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
152 struct lib_ring_buffer
*bufb
,
153 uint64_t *events_discarded
)
158 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
159 struct lib_ring_buffer
*bufb
,
165 static int client_content_size(const struct lib_ring_buffer_config
*config
,
166 struct lib_ring_buffer
*bufb
,
167 uint64_t *content_size
)
172 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
173 struct lib_ring_buffer
*bufb
,
174 uint64_t *packet_size
)
179 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
180 struct lib_ring_buffer
*bufb
,
186 static int client_sequence_number(const struct lib_ring_buffer_config
*config
,
187 struct lib_ring_buffer
*bufb
,
194 int client_instance_id(const struct lib_ring_buffer_config
*config
,
195 struct lib_ring_buffer
*bufb
,
201 static void client_record_get(const struct lib_ring_buffer_config
*config
,
202 struct channel
*chan
, struct lib_ring_buffer
*buf
,
203 size_t offset
, size_t *header_len
,
204 size_t *payload_len
, u64
*timestamp
)
206 struct trigger_record_header header
;
209 ret
= lib_ring_buffer_read(&buf
->backend
, offset
, &header
,
210 offsetof(struct trigger_record_header
, header_end
));
211 CHAN_WARN_ON(chan
, ret
!= offsetof(struct trigger_record_header
, header_end
));
212 *header_len
= offsetof(struct trigger_record_header
, header_end
);
214 * Currently, only 64-bit trigger ID.
216 *payload_len
= sizeof(uint64_t);
220 static const struct lib_ring_buffer_config client_config
= {
221 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
222 .cb
.record_header_size
= client_record_header_size
,
223 .cb
.subbuffer_header_size
= client_packet_header_size
,
224 .cb
.buffer_begin
= client_buffer_begin
,
225 .cb
.buffer_end
= client_buffer_end
,
226 .cb
.buffer_create
= client_buffer_create
,
227 .cb
.buffer_finalize
= client_buffer_finalize
,
228 .cb
.record_get
= client_record_get
,
231 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
232 .sync
= RING_BUFFER_SYNC_GLOBAL
,
233 .mode
= RING_BUFFER_MODE_TEMPLATE
,
234 .backend
= RING_BUFFER_PAGE
,
235 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
236 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
237 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
238 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
242 void release_priv_ops(void *priv_ops
)
244 module_put(THIS_MODULE
);
248 void lttng_channel_destroy(struct channel
*chan
)
250 channel_destroy(chan
);
254 struct channel
*_channel_create(const char *name
,
255 void *priv
, void *buf_addr
,
256 size_t subbuf_size
, size_t num_subbuf
,
257 unsigned int switch_timer_interval
,
258 unsigned int read_timer_interval
)
260 struct lttng_trigger_group
*trigger_group
= priv
;
261 struct channel
*chan
;
263 chan
= channel_create(&client_config
, name
,
264 trigger_group
, buf_addr
,
265 subbuf_size
, num_subbuf
, switch_timer_interval
,
266 read_timer_interval
);
269 * Ensure this module is not unloaded before we finish
270 * using lttng_relay_transport.ops.
272 if (!try_module_get(THIS_MODULE
)) {
273 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
276 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
277 chan
->backend
.release_priv_ops
= release_priv_ops
;
282 lttng_channel_destroy(chan
);
287 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
289 struct lib_ring_buffer
*buf
;
291 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
292 if (!lib_ring_buffer_open_read(buf
))
298 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
300 struct lib_ring_buffer
*buf
;
303 for_each_channel_cpu(cpu
, chan
) {
304 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
305 if (!atomic_long_read(&buf
->active_readers
))
312 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
314 lib_ring_buffer_release_read(buf
);
318 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
322 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
325 lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
326 &ctx
->backend_pages
);
331 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
333 lib_ring_buffer_commit(&client_config
, ctx
);
337 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
340 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
344 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
345 const void __user
*src
, size_t len
)
347 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
351 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
354 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
358 void lttng_event_strcpy(struct lib_ring_buffer_ctx
*ctx
, const char *src
,
361 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
365 size_t lttng_packet_avail_size(struct channel
*chan
)
367 unsigned long o_begin
;
368 struct lib_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 trigger_packet_header
);
381 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
383 struct lib_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 channel
*chan
)
391 return &chan
->hp_wait
;
395 int lttng_is_finalized(struct channel
*chan
)
397 return lib_ring_buffer_channel_is_finalized(chan
);
401 int lttng_is_disabled(struct 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 .channel_create
= _channel_create
,
411 .channel_destroy
= lttng_channel_destroy
,
412 .buffer_read_open
= lttng_buffer_read_open
,
413 .buffer_has_read_closed_stream
=
414 lttng_buffer_has_read_closed_stream
,
415 .buffer_read_close
= lttng_buffer_read_close
,
416 .event_reserve
= lttng_event_reserve
,
417 .event_commit
= lttng_event_commit
,
418 .event_write_from_user
= lttng_event_write_from_user
,
419 .event_memset
= lttng_event_memset
,
420 .event_write
= lttng_event_write
,
421 .event_strcpy
= lttng_event_strcpy
,
422 .packet_avail_size
= lttng_packet_avail_size
,
423 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
424 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
425 .is_finalized
= lttng_is_finalized
,
426 .is_disabled
= lttng_is_disabled
,
427 .timestamp_begin
= client_timestamp_begin
,
428 .timestamp_end
= client_timestamp_end
,
429 .events_discarded
= client_events_discarded
,
430 .content_size
= client_content_size
,
431 .packet_size
= client_packet_size
,
432 .stream_id
= client_stream_id
,
433 .current_timestamp
= client_current_timestamp
,
434 .sequence_number
= client_sequence_number
,
435 .instance_id
= client_instance_id
,
439 static int __init
lttng_ring_buffer_trigger_client_init(void)
442 * This vmalloc sync all also takes care of the lib ring buffer
443 * vmalloc'd module pages when it is built as a module into LTTng.
445 wrapper_vmalloc_sync_all();
446 lttng_transport_register(<tng_relay_transport
);
450 module_init(lttng_ring_buffer_trigger_client_init
);
452 static void __exit
lttng_ring_buffer_trigger_client_exit(void)
454 lttng_transport_unregister(<tng_relay_transport
);
457 module_exit(lttng_ring_buffer_trigger_client_exit
);
459 MODULE_LICENSE("GPL and additional rights");
460 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
461 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
463 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
464 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
465 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
466 LTTNG_MODULES_EXTRAVERSION
);