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>
17 #include <wrapper/limits.h>
19 static struct lttng_transport lttng_relay_transport
;
21 struct event_notifier_packet_header
{
22 uint8_t header_end
[0];
25 struct event_notifier_record_header
{
26 uint32_t payload_len
; /* in bytes */
27 uint8_t header_end
[0]; /* End of header */
30 static const struct lib_ring_buffer_config client_config
;
33 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
39 size_t record_header_size(const struct lib_ring_buffer_config
*config
,
40 struct channel
*chan
, size_t offset
,
41 size_t *pre_header_padding
,
42 struct lib_ring_buffer_ctx
*ctx
,
45 size_t orig_offset
= offset
;
48 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
51 offset
+= sizeof(uint32_t);
53 *pre_header_padding
= padding
;
55 return offset
- orig_offset
;
58 #include <ringbuffer/api.h>
60 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
66 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
67 struct channel
*chan
, size_t offset
,
68 size_t *pre_header_padding
,
69 struct lib_ring_buffer_ctx
*ctx
,
72 return record_header_size(config
, chan
, offset
,
73 pre_header_padding
, ctx
, client_ctx
);
77 * client_packet_header_size - called on buffer-switch to a new sub-buffer
79 * Return header size without padding after the structure. Don't use packed
80 * structure because gcc generates inefficient code on some architectures
83 static size_t client_packet_header_size(void)
85 return offsetof(struct event_notifier_packet_header
, header_end
);
88 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
89 unsigned int subbuf_idx
)
94 * offset is assumed to never be 0 here : never deliver a completely empty
95 * subbuffer. data_size is between 1 and subbuf_size.
97 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
98 unsigned int subbuf_idx
, unsigned long data_size
)
102 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
103 int cpu
, const char *name
)
108 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
112 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
113 struct lib_ring_buffer
*buf
, uint64_t *timestamp_begin
)
118 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
119 struct lib_ring_buffer
*bufb
,
120 uint64_t *timestamp_end
)
125 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
126 struct lib_ring_buffer
*bufb
,
127 uint64_t *events_discarded
)
132 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
133 struct lib_ring_buffer
*bufb
,
139 static int client_content_size(const struct lib_ring_buffer_config
*config
,
140 struct lib_ring_buffer
*bufb
,
141 uint64_t *content_size
)
146 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
147 struct lib_ring_buffer
*bufb
,
148 uint64_t *packet_size
)
153 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
154 struct lib_ring_buffer
*bufb
,
160 static int client_sequence_number(const struct lib_ring_buffer_config
*config
,
161 struct lib_ring_buffer
*bufb
,
168 int client_instance_id(const struct lib_ring_buffer_config
*config
,
169 struct lib_ring_buffer
*bufb
,
175 static void client_record_get(const struct lib_ring_buffer_config
*config
,
176 struct channel
*chan
, struct lib_ring_buffer
*buf
,
177 size_t offset
, size_t *header_len
,
178 size_t *payload_len
, u64
*timestamp
)
180 struct event_notifier_record_header header
;
183 ret
= lib_ring_buffer_read(&buf
->backend
, offset
, &header
,
184 offsetof(struct event_notifier_record_header
, header_end
));
185 CHAN_WARN_ON(chan
, ret
!= offsetof(struct event_notifier_record_header
, header_end
));
186 *header_len
= offsetof(struct event_notifier_record_header
, header_end
);
187 *payload_len
= header
.payload_len
;
191 static const struct lib_ring_buffer_config client_config
= {
192 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
193 .cb
.record_header_size
= client_record_header_size
,
194 .cb
.subbuffer_header_size
= client_packet_header_size
,
195 .cb
.buffer_begin
= client_buffer_begin
,
196 .cb
.buffer_end
= client_buffer_end
,
197 .cb
.buffer_create
= client_buffer_create
,
198 .cb
.buffer_finalize
= client_buffer_finalize
,
199 .cb
.record_get
= client_record_get
,
202 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
203 .sync
= RING_BUFFER_SYNC_GLOBAL
,
204 .mode
= RING_BUFFER_MODE_TEMPLATE
,
205 .backend
= RING_BUFFER_PAGE
,
206 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
207 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
208 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
209 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
213 void release_priv_ops(void *priv_ops
)
215 module_put(THIS_MODULE
);
219 void lttng_channel_destroy(struct channel
*chan
)
221 channel_destroy(chan
);
225 struct channel
*_channel_create(const char *name
,
226 void *priv
, void *buf_addr
,
227 size_t subbuf_size
, size_t num_subbuf
,
228 unsigned int switch_timer_interval
,
229 unsigned int read_timer_interval
)
231 struct lttng_event_notifier_group
*event_notifier_group
= priv
;
232 struct channel
*chan
;
234 chan
= channel_create(&client_config
, name
,
235 event_notifier_group
, buf_addr
,
236 subbuf_size
, num_subbuf
, switch_timer_interval
,
237 read_timer_interval
);
240 * Ensure this module is not unloaded before we finish
241 * using lttng_relay_transport.ops.
243 if (!try_module_get(THIS_MODULE
)) {
244 printk(KERN_WARNING
"LTTng: Can't lock event_notifier transport module.\n");
247 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
248 chan
->backend
.release_priv_ops
= release_priv_ops
;
253 lttng_channel_destroy(chan
);
258 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
260 struct lib_ring_buffer
*buf
;
262 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
263 if (!lib_ring_buffer_open_read(buf
))
269 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
271 struct lib_ring_buffer
*buf
;
274 for_each_channel_cpu(cpu
, chan
) {
275 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
276 if (!atomic_long_read(&buf
->active_readers
))
283 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
285 lib_ring_buffer_release_read(buf
);
289 void lttng_write_event_notifier_header(const struct lib_ring_buffer_config
*config
,
290 struct lib_ring_buffer_ctx
*ctx
)
294 WARN_ON_ONCE(ctx
->data_size
> U32_MAX
);
296 data_size
= (uint32_t) ctx
->data_size
;
298 lib_ring_buffer_write(config
, ctx
, &data_size
, sizeof(data_size
));
300 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
304 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
308 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
311 lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
312 &ctx
->backend_pages
);
314 lttng_write_event_notifier_header(&client_config
, ctx
);
319 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
321 lib_ring_buffer_commit(&client_config
, ctx
);
325 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
328 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
332 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
333 const void __user
*src
, size_t len
)
335 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
339 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
342 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
346 void lttng_event_strcpy(struct lib_ring_buffer_ctx
*ctx
, const char *src
,
349 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
353 size_t lttng_packet_avail_size(struct channel
*chan
)
355 unsigned long o_begin
;
356 struct lib_ring_buffer
*buf
;
358 buf
= chan
->backend
.buf
; /* Only for global buffer ! */
359 o_begin
= v_read(&client_config
, &buf
->offset
);
360 if (subbuf_offset(o_begin
, chan
) != 0) {
361 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
363 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
364 - sizeof(struct event_notifier_packet_header
);
369 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
371 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
373 return &buf
->write_wait
;
377 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
379 return &chan
->hp_wait
;
383 int lttng_is_finalized(struct channel
*chan
)
385 return lib_ring_buffer_channel_is_finalized(chan
);
389 int lttng_is_disabled(struct channel
*chan
)
391 return lib_ring_buffer_channel_is_disabled(chan
);
394 static struct lttng_transport lttng_relay_transport
= {
395 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
396 .owner
= THIS_MODULE
,
398 .channel_create
= _channel_create
,
399 .channel_destroy
= lttng_channel_destroy
,
400 .buffer_read_open
= lttng_buffer_read_open
,
401 .buffer_has_read_closed_stream
=
402 lttng_buffer_has_read_closed_stream
,
403 .buffer_read_close
= lttng_buffer_read_close
,
404 .event_reserve
= lttng_event_reserve
,
405 .event_commit
= lttng_event_commit
,
406 .event_write_from_user
= lttng_event_write_from_user
,
407 .event_memset
= lttng_event_memset
,
408 .event_write
= lttng_event_write
,
409 .event_strcpy
= lttng_event_strcpy
,
410 .packet_avail_size
= lttng_packet_avail_size
,
411 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
412 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
413 .is_finalized
= lttng_is_finalized
,
414 .is_disabled
= lttng_is_disabled
,
415 .timestamp_begin
= client_timestamp_begin
,
416 .timestamp_end
= client_timestamp_end
,
417 .events_discarded
= client_events_discarded
,
418 .content_size
= client_content_size
,
419 .packet_size
= client_packet_size
,
420 .stream_id
= client_stream_id
,
421 .current_timestamp
= client_current_timestamp
,
422 .sequence_number
= client_sequence_number
,
423 .instance_id
= client_instance_id
,
427 static int __init
lttng_ring_buffer_event_notifier_client_init(void)
430 * This vmalloc sync all also takes care of the lib ring buffer
431 * vmalloc'd module pages when it is built as a module into LTTng.
433 wrapper_vmalloc_sync_mappings();
434 lttng_transport_register(<tng_relay_transport
);
438 module_init(lttng_ring_buffer_event_notifier_client_init
);
440 static void __exit
lttng_ring_buffer_event_notifier_client_exit(void)
442 lttng_transport_unregister(<tng_relay_transport
);
445 module_exit(lttng_ring_buffer_event_notifier_client_exit
);
447 MODULE_LICENSE("GPL and additional rights");
448 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
449 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
451 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
452 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
453 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
454 LTTNG_MODULES_EXTRAVERSION
);