ea0ab66ed1fa868dd3d6701014bb9e081b18c8a3
[lttng-modules.git] / src / lttng-ring-buffer-event-notifier-client.h
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
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>
15
16 static struct lttng_transport lttng_relay_transport;
17
18 struct event_notifier_packet_header {
19 uint8_t header_end[0];
20 };
21
22 struct event_notifier_record_header {
23 uint8_t header_end[0]; /* End of header */
24 };
25
26 static const struct lib_ring_buffer_config client_config;
27
28 static inline
29 u64 lib_ring_buffer_clock_read(struct channel *chan)
30 {
31 return 0;
32 }
33
34 static inline
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,
39 void *client_ctx)
40 {
41 return 0;
42 }
43
44 #include <ringbuffer/api.h>
45
46 static u64 client_ring_buffer_clock_read(struct channel *chan)
47 {
48 return 0;
49 }
50
51 static
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,
56 void *client_ctx)
57 {
58 return 0;
59 }
60
61 /**
62 * client_packet_header_size - called on buffer-switch to a new sub-buffer
63 *
64 * Return header size without padding after the structure. Don't use packed
65 * structure because gcc generates inefficient code on some architectures
66 * (powerpc, mips..)
67 */
68 static size_t client_packet_header_size(void)
69 {
70 return offsetof(struct event_notifier_packet_header, header_end);
71 }
72
73 static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
74 unsigned int subbuf_idx)
75 {
76 }
77
78 /*
79 * offset is assumed to never be 0 here : never deliver a completely empty
80 * subbuffer. data_size is between 1 and subbuf_size.
81 */
82 static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
83 unsigned int subbuf_idx, unsigned long data_size)
84 {
85 }
86
87 static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
88 int cpu, const char *name)
89 {
90 return 0;
91 }
92
93 static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
94 {
95 }
96
97 static int client_timestamp_begin(const struct lib_ring_buffer_config *config,
98 struct lib_ring_buffer *buf, uint64_t *timestamp_begin)
99 {
100 return -ENOSYS;
101 }
102
103 static int client_timestamp_end(const struct lib_ring_buffer_config *config,
104 struct lib_ring_buffer *bufb,
105 uint64_t *timestamp_end)
106 {
107 return -ENOSYS;
108 }
109
110 static int client_events_discarded(const struct lib_ring_buffer_config *config,
111 struct lib_ring_buffer *bufb,
112 uint64_t *events_discarded)
113 {
114 return -ENOSYS;
115 }
116
117 static int client_current_timestamp(const struct lib_ring_buffer_config *config,
118 struct lib_ring_buffer *bufb,
119 uint64_t *ts)
120 {
121 return -ENOSYS;
122 }
123
124 static int client_content_size(const struct lib_ring_buffer_config *config,
125 struct lib_ring_buffer *bufb,
126 uint64_t *content_size)
127 {
128 return -ENOSYS;
129 }
130
131 static int client_packet_size(const struct lib_ring_buffer_config *config,
132 struct lib_ring_buffer *bufb,
133 uint64_t *packet_size)
134 {
135 return -ENOSYS;
136 }
137
138 static int client_stream_id(const struct lib_ring_buffer_config *config,
139 struct lib_ring_buffer *bufb,
140 uint64_t *stream_id)
141 {
142 return -ENOSYS;
143 }
144
145 static int client_sequence_number(const struct lib_ring_buffer_config *config,
146 struct lib_ring_buffer *bufb,
147 uint64_t *seq)
148 {
149 return -ENOSYS;
150 }
151
152 static
153 int client_instance_id(const struct lib_ring_buffer_config *config,
154 struct lib_ring_buffer *bufb,
155 uint64_t *id)
156 {
157 return -ENOSYS;
158 }
159
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)
164 {
165 struct event_notifier_record_header header;
166 int ret;
167
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);
172 /*
173 * Currently, only 64-bit event notifier ID.
174 */
175 *payload_len = sizeof(uint64_t);
176 *timestamp = 0;
177 }
178
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,
188
189 .tsc_bits = 0,
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,
198 };
199
200 static
201 void release_priv_ops(void *priv_ops)
202 {
203 module_put(THIS_MODULE);
204 }
205
206 static
207 void lttng_channel_destroy(struct channel *chan)
208 {
209 channel_destroy(chan);
210 }
211
212 static
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)
218 {
219 struct lttng_event_notifier_group *event_notifier_group = priv;
220 struct channel *chan;
221
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);
226 if (chan) {
227 /*
228 * Ensure this module is not unloaded before we finish
229 * using lttng_relay_transport.ops.
230 */
231 if (!try_module_get(THIS_MODULE)) {
232 printk(KERN_WARNING "LTTng: Can't lock event_notifier transport module.\n");
233 goto error;
234 }
235 chan->backend.priv_ops = &lttng_relay_transport.ops;
236 chan->backend.release_priv_ops = release_priv_ops;
237 }
238 return chan;
239
240 error:
241 lttng_channel_destroy(chan);
242 return NULL;
243 }
244
245 static
246 struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
247 {
248 struct lib_ring_buffer *buf;
249
250 buf = channel_get_ring_buffer(&client_config, chan, 0);
251 if (!lib_ring_buffer_open_read(buf))
252 return buf;
253 return NULL;
254 }
255
256 static
257 int lttng_buffer_has_read_closed_stream(struct channel *chan)
258 {
259 struct lib_ring_buffer *buf;
260 int cpu;
261
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))
265 return 1;
266 }
267 return 0;
268 }
269
270 static
271 void lttng_buffer_read_close(struct lib_ring_buffer *buf)
272 {
273 lib_ring_buffer_release_read(buf);
274 }
275
276 static
277 int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
278 {
279 int ret;
280
281 ret = lib_ring_buffer_reserve(&client_config, ctx, NULL);
282 if (ret)
283 return ret;
284 lib_ring_buffer_backend_get_pages(&client_config, ctx,
285 &ctx->backend_pages);
286 return 0;
287 }
288
289 static
290 void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
291 {
292 lib_ring_buffer_commit(&client_config, ctx);
293 }
294
295 static
296 void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
297 size_t len)
298 {
299 lib_ring_buffer_write(&client_config, ctx, src, len);
300 }
301
302 static
303 void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
304 const void __user *src, size_t len)
305 {
306 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
307 }
308
309 static
310 void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
311 int c, size_t len)
312 {
313 lib_ring_buffer_memset(&client_config, ctx, c, len);
314 }
315
316 static
317 void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src,
318 size_t len)
319 {
320 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
321 }
322
323 static
324 size_t lttng_packet_avail_size(struct channel *chan)
325 {
326 unsigned long o_begin;
327 struct lib_ring_buffer *buf;
328
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);
333 } else {
334 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
335 - sizeof(struct event_notifier_packet_header);
336 }
337 }
338
339 static
340 wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
341 {
342 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
343 chan, cpu);
344 return &buf->write_wait;
345 }
346
347 static
348 wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
349 {
350 return &chan->hp_wait;
351 }
352
353 static
354 int lttng_is_finalized(struct channel *chan)
355 {
356 return lib_ring_buffer_channel_is_finalized(chan);
357 }
358
359 static
360 int lttng_is_disabled(struct channel *chan)
361 {
362 return lib_ring_buffer_channel_is_disabled(chan);
363 }
364
365 static struct lttng_transport lttng_relay_transport = {
366 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
367 .owner = THIS_MODULE,
368 .ops = {
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,
395 },
396 };
397
398 static int __init lttng_ring_buffer_event_notifier_client_init(void)
399 {
400 /*
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.
403 */
404 wrapper_vmalloc_sync_mappings();
405 lttng_transport_register(&lttng_relay_transport);
406 return 0;
407 }
408
409 module_init(lttng_ring_buffer_event_notifier_client_init);
410
411 static void __exit lttng_ring_buffer_event_notifier_client_exit(void)
412 {
413 lttng_transport_unregister(&lttng_relay_transport);
414 }
415
416 module_exit(lttng_ring_buffer_event_notifier_client_exit);
417
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
421 " client");
422 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
423 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
424 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
425 LTTNG_MODULES_EXTRAVERSION);
This page took 0.037688 seconds and 3 git commands to generate.