1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
3 * lttng-ring-buffer-event-notifier-client.h
5 * LTTng lib ring buffer event notifier 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_mappings() */
13 #include <lttng/events.h>
14 #include <lttng/tracer.h>
16 static struct lttng_transport lttng_relay_transport
;
18 struct event_notifier_packet_header
{
19 uint8_t header_end
[0];
22 struct event_notifier_record_header
{
23 uint8_t header_end
[0]; /* End of header */
26 static const struct lib_ring_buffer_config client_config
;
29 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
35 size_t record_header_size(const struct lib_ring_buffer_config
*config
,
36 struct channel
*chan
, size_t offset
,
37 size_t *pre_header_padding
,
38 struct lib_ring_buffer_ctx
*ctx
,
44 #include <ringbuffer/api.h>
46 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
52 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
53 struct channel
*chan
, size_t offset
,
54 size_t *pre_header_padding
,
55 struct lib_ring_buffer_ctx
*ctx
,
62 * client_packet_header_size - called on buffer-switch to a new sub-buffer
64 * Return header size without padding after the structure. Don't use packed
65 * structure because gcc generates inefficient code on some architectures
68 static size_t client_packet_header_size(void)
70 return offsetof(struct event_notifier_packet_header
, header_end
);
73 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
74 unsigned int subbuf_idx
)
79 * offset is assumed to never be 0 here : never deliver a completely empty
80 * subbuffer. data_size is between 1 and subbuf_size.
82 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
83 unsigned int subbuf_idx
, unsigned long data_size
)
87 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
88 int cpu
, const char *name
)
93 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
97 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
98 struct lib_ring_buffer
*buf
, uint64_t *timestamp_begin
)
103 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
104 struct lib_ring_buffer
*bufb
,
105 uint64_t *timestamp_end
)
110 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
111 struct lib_ring_buffer
*bufb
,
112 uint64_t *events_discarded
)
117 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
118 struct lib_ring_buffer
*bufb
,
124 static int client_content_size(const struct lib_ring_buffer_config
*config
,
125 struct lib_ring_buffer
*bufb
,
126 uint64_t *content_size
)
131 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
132 struct lib_ring_buffer
*bufb
,
133 uint64_t *packet_size
)
138 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
139 struct lib_ring_buffer
*bufb
,
145 static int client_sequence_number(const struct lib_ring_buffer_config
*config
,
146 struct lib_ring_buffer
*bufb
,
153 int client_instance_id(const struct lib_ring_buffer_config
*config
,
154 struct lib_ring_buffer
*bufb
,
160 static void client_record_get(const struct lib_ring_buffer_config
*config
,
161 struct channel
*chan
, struct lib_ring_buffer
*buf
,
162 size_t offset
, size_t *header_len
,
163 size_t *payload_len
, u64
*timestamp
)
165 struct event_notifier_record_header header
;
168 ret
= lib_ring_buffer_read(&buf
->backend
, offset
, &header
,
169 offsetof(struct event_notifier_record_header
, header_end
));
170 CHAN_WARN_ON(chan
, ret
!= offsetof(struct event_notifier_record_header
, header_end
));
171 *header_len
= offsetof(struct event_notifier_record_header
, header_end
);
173 * Currently, only 64-bit event notifier ID.
175 *payload_len
= sizeof(uint64_t);
179 static const struct lib_ring_buffer_config client_config
= {
180 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
181 .cb
.record_header_size
= client_record_header_size
,
182 .cb
.subbuffer_header_size
= client_packet_header_size
,
183 .cb
.buffer_begin
= client_buffer_begin
,
184 .cb
.buffer_end
= client_buffer_end
,
185 .cb
.buffer_create
= client_buffer_create
,
186 .cb
.buffer_finalize
= client_buffer_finalize
,
187 .cb
.record_get
= client_record_get
,
190 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
191 .sync
= RING_BUFFER_SYNC_GLOBAL
,
192 .mode
= RING_BUFFER_MODE_TEMPLATE
,
193 .backend
= RING_BUFFER_PAGE
,
194 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
195 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
196 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
197 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
201 void release_priv_ops(void *priv_ops
)
203 module_put(THIS_MODULE
);
207 void lttng_channel_destroy(struct channel
*chan
)
209 channel_destroy(chan
);
213 struct channel
*_channel_create(const char *name
,
214 void *priv
, void *buf_addr
,
215 size_t subbuf_size
, size_t num_subbuf
,
216 unsigned int switch_timer_interval
,
217 unsigned int read_timer_interval
)
219 struct lttng_event_notifier_group
*event_notifier_group
= priv
;
220 struct channel
*chan
;
222 chan
= channel_create(&client_config
, name
,
223 event_notifier_group
, buf_addr
,
224 subbuf_size
, num_subbuf
, switch_timer_interval
,
225 read_timer_interval
);
228 * Ensure this module is not unloaded before we finish
229 * using lttng_relay_transport.ops.
231 if (!try_module_get(THIS_MODULE
)) {
232 printk(KERN_WARNING
"LTTng: Can't lock event_notifier transport module.\n");
235 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
236 chan
->backend
.release_priv_ops
= release_priv_ops
;
241 lttng_channel_destroy(chan
);
246 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
248 struct lib_ring_buffer
*buf
;
250 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
251 if (!lib_ring_buffer_open_read(buf
))
257 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
259 struct lib_ring_buffer
*buf
;
262 for_each_channel_cpu(cpu
, chan
) {
263 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
264 if (!atomic_long_read(&buf
->active_readers
))
271 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
273 lib_ring_buffer_release_read(buf
);
277 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
281 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
284 lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
285 &ctx
->backend_pages
);
290 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
292 lib_ring_buffer_commit(&client_config
, ctx
);
296 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
299 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
303 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
304 const void __user
*src
, size_t len
)
306 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
310 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
313 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
317 void lttng_event_strcpy(struct lib_ring_buffer_ctx
*ctx
, const char *src
,
320 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
324 size_t lttng_packet_avail_size(struct channel
*chan
)
326 unsigned long o_begin
;
327 struct lib_ring_buffer
*buf
;
329 buf
= chan
->backend
.buf
; /* Only for global buffer ! */
330 o_begin
= v_read(&client_config
, &buf
->offset
);
331 if (subbuf_offset(o_begin
, chan
) != 0) {
332 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
334 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
335 - sizeof(struct event_notifier_packet_header
);
340 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
342 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
344 return &buf
->write_wait
;
348 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
350 return &chan
->hp_wait
;
354 int lttng_is_finalized(struct channel
*chan
)
356 return lib_ring_buffer_channel_is_finalized(chan
);
360 int lttng_is_disabled(struct channel
*chan
)
362 return lib_ring_buffer_channel_is_disabled(chan
);
365 static struct lttng_transport lttng_relay_transport
= {
366 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
367 .owner
= THIS_MODULE
,
369 .channel_create
= _channel_create
,
370 .channel_destroy
= lttng_channel_destroy
,
371 .buffer_read_open
= lttng_buffer_read_open
,
372 .buffer_has_read_closed_stream
=
373 lttng_buffer_has_read_closed_stream
,
374 .buffer_read_close
= lttng_buffer_read_close
,
375 .event_reserve
= lttng_event_reserve
,
376 .event_commit
= lttng_event_commit
,
377 .event_write_from_user
= lttng_event_write_from_user
,
378 .event_memset
= lttng_event_memset
,
379 .event_write
= lttng_event_write
,
380 .event_strcpy
= lttng_event_strcpy
,
381 .packet_avail_size
= lttng_packet_avail_size
,
382 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
383 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
384 .is_finalized
= lttng_is_finalized
,
385 .is_disabled
= lttng_is_disabled
,
386 .timestamp_begin
= client_timestamp_begin
,
387 .timestamp_end
= client_timestamp_end
,
388 .events_discarded
= client_events_discarded
,
389 .content_size
= client_content_size
,
390 .packet_size
= client_packet_size
,
391 .stream_id
= client_stream_id
,
392 .current_timestamp
= client_current_timestamp
,
393 .sequence_number
= client_sequence_number
,
394 .instance_id
= client_instance_id
,
398 static int __init
lttng_ring_buffer_event_notifier_client_init(void)
401 * This vmalloc sync all also takes care of the lib ring buffer
402 * vmalloc'd module pages when it is built as a module into LTTng.
404 wrapper_vmalloc_sync_mappings();
405 lttng_transport_register(<tng_relay_transport
);
409 module_init(lttng_ring_buffer_event_notifier_client_init
);
411 static void __exit
lttng_ring_buffer_event_notifier_client_exit(void)
413 lttng_transport_unregister(<tng_relay_transport
);
416 module_exit(lttng_ring_buffer_event_notifier_client_exit
);
418 MODULE_LICENSE("GPL and additional rights");
419 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
420 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
422 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
423 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
424 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
425 LTTNG_MODULES_EXTRAVERSION
);