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/limits.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
14 #include <lttng/abi.h>
15 #include <lttng/events.h>
16 #include <lttng/tracer.h>
18 static struct lttng_transport lttng_relay_transport
;
20 struct event_notifier_packet_header
{
21 uint8_t header_end
[0];
24 struct event_notifier_record_header
{
25 uint32_t payload_len
; /* in bytes */
26 uint8_t header_end
[0]; /* End of header */
29 static const struct lib_ring_buffer_config client_config
;
32 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
38 size_t record_header_size(const struct lib_ring_buffer_config
*config
,
39 struct channel
*chan
, size_t offset
,
40 size_t *pre_header_padding
,
41 struct lib_ring_buffer_ctx
*ctx
,
44 size_t orig_offset
= offset
;
47 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
50 offset
+= sizeof(uint32_t);
52 *pre_header_padding
= padding
;
54 return offset
- orig_offset
;
57 #include <ringbuffer/api.h>
59 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
65 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
66 struct channel
*chan
, size_t offset
,
67 size_t *pre_header_padding
,
68 struct lib_ring_buffer_ctx
*ctx
,
71 return record_header_size(config
, chan
, offset
,
72 pre_header_padding
, ctx
, client_ctx
);
76 * client_packet_header_size - called on buffer-switch to a new sub-buffer
78 * Return header size without padding after the structure. Don't use packed
79 * structure because gcc generates inefficient code on some architectures
82 static size_t client_packet_header_size(void)
84 return offsetof(struct event_notifier_packet_header
, header_end
);
87 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
88 unsigned int subbuf_idx
)
93 * offset is assumed to never be 0 here : never deliver a completely empty
94 * subbuffer. data_size is between 1 and subbuf_size.
96 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
97 unsigned int subbuf_idx
, unsigned long data_size
)
101 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
102 int cpu
, const char *name
)
107 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
111 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
112 struct lib_ring_buffer
*buf
, uint64_t *timestamp_begin
)
117 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
118 struct lib_ring_buffer
*bufb
,
119 uint64_t *timestamp_end
)
124 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
125 struct lib_ring_buffer
*bufb
,
126 uint64_t *events_discarded
)
131 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
132 struct lib_ring_buffer
*bufb
,
138 static int client_content_size(const struct lib_ring_buffer_config
*config
,
139 struct lib_ring_buffer
*bufb
,
140 uint64_t *content_size
)
145 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
146 struct lib_ring_buffer
*bufb
,
147 uint64_t *packet_size
)
152 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
153 struct lib_ring_buffer
*bufb
,
159 static int client_sequence_number(const struct lib_ring_buffer_config
*config
,
160 struct lib_ring_buffer
*bufb
,
167 int client_instance_id(const struct lib_ring_buffer_config
*config
,
168 struct lib_ring_buffer
*bufb
,
174 static void client_record_get(const struct lib_ring_buffer_config
*config
,
175 struct channel
*chan
, struct lib_ring_buffer
*buf
,
176 size_t offset
, size_t *header_len
,
177 size_t *payload_len
, u64
*timestamp
)
179 struct event_notifier_record_header header
;
182 ret
= lib_ring_buffer_read(&buf
->backend
, offset
, &header
,
183 offsetof(struct event_notifier_record_header
, header_end
));
184 CHAN_WARN_ON(chan
, ret
!= offsetof(struct event_notifier_record_header
, header_end
));
185 *header_len
= offsetof(struct event_notifier_record_header
, header_end
);
186 *payload_len
= header
.payload_len
;
190 static const struct lib_ring_buffer_config client_config
= {
191 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
192 .cb
.record_header_size
= client_record_header_size
,
193 .cb
.subbuffer_header_size
= client_packet_header_size
,
194 .cb
.buffer_begin
= client_buffer_begin
,
195 .cb
.buffer_end
= client_buffer_end
,
196 .cb
.buffer_create
= client_buffer_create
,
197 .cb
.buffer_finalize
= client_buffer_finalize
,
198 .cb
.record_get
= client_record_get
,
201 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
202 .sync
= RING_BUFFER_SYNC_GLOBAL
,
203 .mode
= RING_BUFFER_MODE_TEMPLATE
,
204 .backend
= RING_BUFFER_PAGE
,
205 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
206 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
207 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
208 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
212 void release_priv_ops(void *priv_ops
)
214 module_put(THIS_MODULE
);
218 void lttng_channel_destroy(struct channel
*chan
)
220 channel_destroy(chan
);
224 struct channel
*_channel_create(const char *name
,
225 void *priv
, void *buf_addr
,
226 size_t subbuf_size
, size_t num_subbuf
,
227 unsigned int switch_timer_interval
,
228 unsigned int read_timer_interval
)
230 struct lttng_event_notifier_group
*event_notifier_group
= priv
;
231 struct channel
*chan
;
233 chan
= channel_create(&client_config
, name
,
234 event_notifier_group
, buf_addr
,
235 subbuf_size
, num_subbuf
, switch_timer_interval
,
236 read_timer_interval
);
239 * Ensure this module is not unloaded before we finish
240 * using lttng_relay_transport.ops.
242 if (!try_module_get(THIS_MODULE
)) {
243 printk(KERN_WARNING
"LTTng: Can't lock event_notifier transport module.\n");
246 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
247 chan
->backend
.release_priv_ops
= release_priv_ops
;
252 lttng_channel_destroy(chan
);
257 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
259 struct lib_ring_buffer
*buf
;
261 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
262 if (!lib_ring_buffer_open_read(buf
))
268 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
270 struct lib_ring_buffer
*buf
;
273 for_each_channel_cpu(cpu
, chan
) {
274 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
275 if (!atomic_long_read(&buf
->active_readers
))
282 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
284 lib_ring_buffer_release_read(buf
);
288 void lttng_write_event_notifier_header(const struct lib_ring_buffer_config
*config
,
289 struct lib_ring_buffer_ctx
*ctx
)
293 WARN_ON_ONCE(ctx
->data_size
> U32_MAX
);
295 data_size
= (uint32_t) ctx
->data_size
;
297 lib_ring_buffer_write(config
, ctx
, &data_size
, sizeof(data_size
));
299 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
303 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
307 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
310 lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
311 &ctx
->backend_pages
);
313 lttng_write_event_notifier_header(&client_config
, ctx
);
318 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
320 lib_ring_buffer_commit(&client_config
, ctx
);
324 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
327 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
331 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
332 const void __user
*src
, size_t len
)
334 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
338 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
341 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
345 void lttng_event_strcpy(struct lib_ring_buffer_ctx
*ctx
, const char *src
,
348 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
352 size_t lttng_packet_avail_size(struct channel
*chan
)
354 unsigned long o_begin
;
355 struct lib_ring_buffer
*buf
;
357 buf
= chan
->backend
.buf
; /* Only for global buffer ! */
358 o_begin
= v_read(&client_config
, &buf
->offset
);
359 if (subbuf_offset(o_begin
, chan
) != 0) {
360 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
362 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
363 - sizeof(struct event_notifier_packet_header
);
368 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
370 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
372 return &buf
->write_wait
;
376 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
378 return &chan
->hp_wait
;
382 int lttng_is_finalized(struct channel
*chan
)
384 return lib_ring_buffer_channel_is_finalized(chan
);
388 int lttng_is_disabled(struct channel
*chan
)
390 return lib_ring_buffer_channel_is_disabled(chan
);
393 static struct lttng_transport lttng_relay_transport
= {
394 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
395 .owner
= THIS_MODULE
,
397 .channel_create
= _channel_create
,
398 .channel_destroy
= lttng_channel_destroy
,
399 .buffer_read_open
= lttng_buffer_read_open
,
400 .buffer_has_read_closed_stream
=
401 lttng_buffer_has_read_closed_stream
,
402 .buffer_read_close
= lttng_buffer_read_close
,
403 .event_reserve
= lttng_event_reserve
,
404 .event_commit
= lttng_event_commit
,
405 .event_write_from_user
= lttng_event_write_from_user
,
406 .event_memset
= lttng_event_memset
,
407 .event_write
= lttng_event_write
,
408 .event_strcpy
= lttng_event_strcpy
,
409 .packet_avail_size
= lttng_packet_avail_size
,
410 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
411 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
412 .is_finalized
= lttng_is_finalized
,
413 .is_disabled
= lttng_is_disabled
,
414 .timestamp_begin
= client_timestamp_begin
,
415 .timestamp_end
= client_timestamp_end
,
416 .events_discarded
= client_events_discarded
,
417 .content_size
= client_content_size
,
418 .packet_size
= client_packet_size
,
419 .stream_id
= client_stream_id
,
420 .current_timestamp
= client_current_timestamp
,
421 .sequence_number
= client_sequence_number
,
422 .instance_id
= client_instance_id
,
426 static int __init
lttng_ring_buffer_event_notifier_client_init(void)
429 * This vmalloc sync all also takes care of the lib ring buffer
430 * vmalloc'd module pages when it is built as a module into LTTng.
432 wrapper_vmalloc_sync_mappings();
433 lttng_transport_register(<tng_relay_transport
);
437 module_init(lttng_ring_buffer_event_notifier_client_init
);
439 static void __exit
lttng_ring_buffer_event_notifier_client_exit(void)
441 lttng_transport_unregister(<tng_relay_transport
);
444 module_exit(lttng_ring_buffer_event_notifier_client_exit
);
446 MODULE_LICENSE("GPL and additional rights");
447 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
448 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
450 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
451 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
452 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
453 LTTNG_MODULES_EXTRAVERSION
);