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