Introduce limits wrapper
[lttng-modules.git] / src / lttng-ring-buffer-event-notifier-client.h
CommitLineData
250c663f
MD
1/* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
2 *
3 * lttng-ring-buffer-event-notifier-client.h
4 *
5 * LTTng lib ring buffer event notifier client template.
6 *
7 * Copyright (C) 2010-2020 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
99d223ad 10#include <linux/limits.h>
250c663f
MD
11#include <linux/module.h>
12#include <linux/types.h>
13#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
99d223ad 14#include <lttng/abi.h>
250c663f
MD
15#include <lttng/events.h>
16#include <lttng/tracer.h>
236233f7 17#include <wrapper/limits.h>
250c663f
MD
18
19static struct lttng_transport lttng_relay_transport;
20
21struct event_notifier_packet_header {
22 uint8_t header_end[0];
23};
24
25struct event_notifier_record_header {
99d223ad 26 uint32_t payload_len; /* in bytes */
250c663f
MD
27 uint8_t header_end[0]; /* End of header */
28};
29
30static const struct lib_ring_buffer_config client_config;
31
32static inline
33u64 lib_ring_buffer_clock_read(struct channel *chan)
34{
35 return 0;
36}
37
38static inline
39size_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,
43 void *client_ctx)
44{
99d223ad
FD
45 size_t orig_offset = offset;
46 size_t padding;
47
48 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
49 offset += padding;
50
51 offset += sizeof(uint32_t);
52
53 *pre_header_padding = padding;
54
55 return offset - orig_offset;
250c663f
MD
56}
57
58#include <ringbuffer/api.h>
59
60static u64 client_ring_buffer_clock_read(struct channel *chan)
61{
62 return 0;
63}
64
65static
66size_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,
70 void *client_ctx)
71{
99d223ad
FD
72 return record_header_size(config, chan, offset,
73 pre_header_padding, ctx, client_ctx);
250c663f
MD
74}
75
76/**
77 * client_packet_header_size - called on buffer-switch to a new sub-buffer
78 *
79 * Return header size without padding after the structure. Don't use packed
80 * structure because gcc generates inefficient code on some architectures
81 * (powerpc, mips..)
82 */
83static size_t client_packet_header_size(void)
84{
85 return offsetof(struct event_notifier_packet_header, header_end);
86}
87
88static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
89 unsigned int subbuf_idx)
90{
91}
92
93/*
94 * offset is assumed to never be 0 here : never deliver a completely empty
95 * subbuffer. data_size is between 1 and subbuf_size.
96 */
97static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
98 unsigned int subbuf_idx, unsigned long data_size)
99{
100}
101
102static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
103 int cpu, const char *name)
104{
105 return 0;
106}
107
108static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
109{
110}
111
112static int client_timestamp_begin(const struct lib_ring_buffer_config *config,
113 struct lib_ring_buffer *buf, uint64_t *timestamp_begin)
114{
115 return -ENOSYS;
116}
117
118static int client_timestamp_end(const struct lib_ring_buffer_config *config,
119 struct lib_ring_buffer *bufb,
120 uint64_t *timestamp_end)
121{
122 return -ENOSYS;
123}
124
125static int client_events_discarded(const struct lib_ring_buffer_config *config,
126 struct lib_ring_buffer *bufb,
127 uint64_t *events_discarded)
128{
129 return -ENOSYS;
130}
131
132static int client_current_timestamp(const struct lib_ring_buffer_config *config,
133 struct lib_ring_buffer *bufb,
134 uint64_t *ts)
135{
136 return -ENOSYS;
137}
138
139static int client_content_size(const struct lib_ring_buffer_config *config,
140 struct lib_ring_buffer *bufb,
141 uint64_t *content_size)
142{
143 return -ENOSYS;
144}
145
146static int client_packet_size(const struct lib_ring_buffer_config *config,
147 struct lib_ring_buffer *bufb,
148 uint64_t *packet_size)
149{
150 return -ENOSYS;
151}
152
153static int client_stream_id(const struct lib_ring_buffer_config *config,
154 struct lib_ring_buffer *bufb,
155 uint64_t *stream_id)
156{
157 return -ENOSYS;
158}
159
160static int client_sequence_number(const struct lib_ring_buffer_config *config,
161 struct lib_ring_buffer *bufb,
162 uint64_t *seq)
163{
164 return -ENOSYS;
165}
166
167static
168int client_instance_id(const struct lib_ring_buffer_config *config,
169 struct lib_ring_buffer *bufb,
170 uint64_t *id)
171{
172 return -ENOSYS;
173}
174
175static 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)
179{
180 struct event_notifier_record_header header;
181 int ret;
182
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);
99d223ad 187 *payload_len = header.payload_len;
250c663f
MD
188 *timestamp = 0;
189}
190
191static 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,
200
201 .tsc_bits = 0,
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,
210};
211
212static
213void release_priv_ops(void *priv_ops)
214{
215 module_put(THIS_MODULE);
216}
217
218static
219void lttng_channel_destroy(struct channel *chan)
220{
221 channel_destroy(chan);
222}
223
224static
225struct 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)
230{
231 struct lttng_event_notifier_group *event_notifier_group = priv;
232 struct channel *chan;
233
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);
238 if (chan) {
239 /*
240 * Ensure this module is not unloaded before we finish
241 * using lttng_relay_transport.ops.
242 */
243 if (!try_module_get(THIS_MODULE)) {
244 printk(KERN_WARNING "LTTng: Can't lock event_notifier transport module.\n");
245 goto error;
246 }
247 chan->backend.priv_ops = &lttng_relay_transport.ops;
248 chan->backend.release_priv_ops = release_priv_ops;
249 }
250 return chan;
251
252error:
253 lttng_channel_destroy(chan);
254 return NULL;
255}
256
257static
258struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
259{
260 struct lib_ring_buffer *buf;
261
262 buf = channel_get_ring_buffer(&client_config, chan, 0);
263 if (!lib_ring_buffer_open_read(buf))
264 return buf;
265 return NULL;
266}
267
268static
269int lttng_buffer_has_read_closed_stream(struct channel *chan)
270{
271 struct lib_ring_buffer *buf;
272 int cpu;
273
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))
277 return 1;
278 }
279 return 0;
280}
281
282static
283void lttng_buffer_read_close(struct lib_ring_buffer *buf)
284{
285 lib_ring_buffer_release_read(buf);
286}
287
99d223ad
FD
288static
289void lttng_write_event_notifier_header(const struct lib_ring_buffer_config *config,
290 struct lib_ring_buffer_ctx *ctx)
291{
292 uint32_t data_size;
293
294 WARN_ON_ONCE(ctx->data_size > U32_MAX);
295
296 data_size = (uint32_t) ctx->data_size;
297
298 lib_ring_buffer_write(config, ctx, &data_size, sizeof(data_size));
299
300 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
301}
302
250c663f
MD
303static
304int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
305{
306 int ret;
307
308 ret = lib_ring_buffer_reserve(&client_config, ctx, NULL);
309 if (ret)
310 return ret;
311 lib_ring_buffer_backend_get_pages(&client_config, ctx,
312 &ctx->backend_pages);
99d223ad
FD
313
314 lttng_write_event_notifier_header(&client_config, ctx);
250c663f
MD
315 return 0;
316}
317
318static
319void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
320{
321 lib_ring_buffer_commit(&client_config, ctx);
322}
323
324static
325void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
326 size_t len)
327{
328 lib_ring_buffer_write(&client_config, ctx, src, len);
329}
330
331static
332void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
333 const void __user *src, size_t len)
334{
335 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
336}
337
338static
339void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
340 int c, size_t len)
341{
342 lib_ring_buffer_memset(&client_config, ctx, c, len);
343}
344
345static
346void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src,
347 size_t len)
348{
349 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
350}
351
352static
353size_t lttng_packet_avail_size(struct channel *chan)
354{
355 unsigned long o_begin;
356 struct lib_ring_buffer *buf;
357
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);
362 } else {
363 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
364 - sizeof(struct event_notifier_packet_header);
365 }
366}
367
368static
369wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
370{
371 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
372 chan, cpu);
373 return &buf->write_wait;
374}
375
376static
377wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
378{
379 return &chan->hp_wait;
380}
381
382static
383int lttng_is_finalized(struct channel *chan)
384{
385 return lib_ring_buffer_channel_is_finalized(chan);
386}
387
388static
389int lttng_is_disabled(struct channel *chan)
390{
391 return lib_ring_buffer_channel_is_disabled(chan);
392}
393
394static struct lttng_transport lttng_relay_transport = {
395 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
396 .owner = THIS_MODULE,
397 .ops = {
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,
424 },
425};
426
427static int __init lttng_ring_buffer_event_notifier_client_init(void)
428{
429 /*
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.
432 */
433 wrapper_vmalloc_sync_mappings();
434 lttng_transport_register(&lttng_relay_transport);
435 return 0;
436}
437
438module_init(lttng_ring_buffer_event_notifier_client_init);
439
440static void __exit lttng_ring_buffer_event_notifier_client_exit(void)
441{
442 lttng_transport_unregister(&lttng_relay_transport);
443}
444
445module_exit(lttng_ring_buffer_event_notifier_client_exit);
446
447MODULE_LICENSE("GPL and additional rights");
448MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
449MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
450 " client");
451MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
452 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
453 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
454 LTTNG_MODULES_EXTRAVERSION);
This page took 0.041146 seconds and 4 git commands to generate.