Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
a90917c3 | 3 | * lttng-ring-buffer-client.h |
881833e3 | 4 | * |
881833e3 MD |
5 | * LTTng lib ring buffer client template. |
6 | * | |
886d51a3 | 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
881833e3 MD |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/types.h> | |
263b6c88 | 12 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */ |
2df37e95 | 13 | #include <lttng/events.h> |
e8c0bbd3 | 14 | #include <lttng/events-internal.h> |
2df37e95 | 15 | #include <lttng/tracer.h> |
881833e3 | 16 | |
dd5a0db3 MD |
17 | static struct lttng_transport lttng_relay_transport; |
18 | ||
881833e3 MD |
19 | struct metadata_packet_header { |
20 | uint32_t magic; /* 0x75D11D57 */ | |
1ec3f75a | 21 | uint8_t uuid[16]; /* Unique Universal Identifier */ |
881833e3 MD |
22 | uint32_t checksum; /* 0 if unused */ |
23 | uint32_t content_size; /* in bits */ | |
24 | uint32_t packet_size; /* in bits */ | |
25 | uint8_t compression_scheme; /* 0 if unused */ | |
26 | uint8_t encryption_scheme; /* 0 if unused */ | |
27 | uint8_t checksum_scheme; /* 0 if unused */ | |
4173df96 MD |
28 | uint8_t major; /* CTF spec major version number */ |
29 | uint8_t minor; /* CTF spec minor version number */ | |
881833e3 MD |
30 | uint8_t header_end[0]; |
31 | }; | |
32 | ||
33 | struct metadata_record_header { | |
34 | uint8_t header_end[0]; /* End of header */ | |
35 | }; | |
36 | ||
37 | static const struct lib_ring_buffer_config client_config; | |
38 | ||
39 | static inline | |
40 | u64 lib_ring_buffer_clock_read(struct channel *chan) | |
41 | { | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static inline | |
c1cc82b8 | 46 | size_t record_header_size(const struct lib_ring_buffer_config *config, |
881833e3 | 47 | struct channel *chan, size_t offset, |
64c796d8 | 48 | size_t *pre_header_padding, |
8a57ec02 | 49 | struct lttng_kernel_ring_buffer_ctx *ctx, |
cc62f29e | 50 | void *client_ctx) |
881833e3 MD |
51 | { |
52 | return 0; | |
53 | } | |
54 | ||
24591303 | 55 | #include <ringbuffer/api.h> |
881833e3 MD |
56 | |
57 | static u64 client_ring_buffer_clock_read(struct channel *chan) | |
58 | { | |
59 | return 0; | |
60 | } | |
61 | ||
62 | static | |
63 | size_t client_record_header_size(const struct lib_ring_buffer_config *config, | |
64 | struct channel *chan, size_t offset, | |
881833e3 | 65 | size_t *pre_header_padding, |
8a57ec02 | 66 | struct lttng_kernel_ring_buffer_ctx *ctx, |
cc62f29e | 67 | void *client_ctx) |
881833e3 MD |
68 | { |
69 | return 0; | |
70 | } | |
71 | ||
72 | /** | |
73 | * client_packet_header_size - called on buffer-switch to a new sub-buffer | |
74 | * | |
75 | * Return header size without padding after the structure. Don't use packed | |
76 | * structure because gcc generates inefficient code on some architectures | |
77 | * (powerpc, mips..) | |
78 | */ | |
79 | static size_t client_packet_header_size(void) | |
80 | { | |
81 | return offsetof(struct metadata_packet_header, header_end); | |
82 | } | |
83 | ||
84 | static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc, | |
85 | unsigned int subbuf_idx) | |
86 | { | |
87 | struct channel *chan = buf->backend.chan; | |
88 | struct metadata_packet_header *header = | |
89 | (struct metadata_packet_header *) | |
90 | lib_ring_buffer_offset_address(&buf->backend, | |
91 | subbuf_idx * chan->backend.subbuf_size); | |
a36580d5 MD |
92 | struct lttng_metadata_cache *metadata_cache = |
93 | channel_get_private(chan); | |
881833e3 MD |
94 | |
95 | header->magic = TSDL_MAGIC_NUMBER; | |
a36580d5 MD |
96 | memcpy(header->uuid, metadata_cache->uuid.b, |
97 | sizeof(metadata_cache->uuid)); | |
881833e3 MD |
98 | header->checksum = 0; /* 0 if unused */ |
99 | header->content_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
100 | header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
101 | header->compression_scheme = 0; /* 0 if unused */ | |
102 | header->encryption_scheme = 0; /* 0 if unused */ | |
103 | header->checksum_scheme = 0; /* 0 if unused */ | |
4173df96 MD |
104 | header->major = CTF_SPEC_MAJOR; |
105 | header->minor = CTF_SPEC_MINOR; | |
881833e3 MD |
106 | } |
107 | ||
108 | /* | |
109 | * offset is assumed to never be 0 here : never deliver a completely empty | |
110 | * subbuffer. data_size is between 1 and subbuf_size. | |
111 | */ | |
112 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, | |
113 | unsigned int subbuf_idx, unsigned long data_size) | |
114 | { | |
115 | struct channel *chan = buf->backend.chan; | |
d793d5e1 | 116 | struct metadata_packet_header *header = |
1ec3f75a | 117 | (struct metadata_packet_header *) |
881833e3 MD |
118 | lib_ring_buffer_offset_address(&buf->backend, |
119 | subbuf_idx * chan->backend.subbuf_size); | |
120 | unsigned long records_lost = 0; | |
121 | ||
122 | header->content_size = data_size * CHAR_BIT; /* in bits */ | |
123 | header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
71c1d843 MD |
124 | /* |
125 | * We do not care about the records lost count, because the metadata | |
126 | * channel waits and retry. | |
127 | */ | |
128 | (void) lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
881833e3 MD |
129 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
130 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
131 | WARN_ON_ONCE(records_lost != 0); | |
132 | } | |
133 | ||
134 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, | |
135 | int cpu, const char *name) | |
136 | { | |
137 | return 0; | |
138 | } | |
139 | ||
140 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) | |
141 | { | |
142 | } | |
143 | ||
3b731ab1 JD |
144 | static int client_timestamp_begin(const struct lib_ring_buffer_config *config, |
145 | struct lib_ring_buffer *buf, uint64_t *timestamp_begin) | |
146 | { | |
147 | return -ENOSYS; | |
148 | } | |
149 | ||
150 | static int client_timestamp_end(const struct lib_ring_buffer_config *config, | |
151 | struct lib_ring_buffer *bufb, | |
152 | uint64_t *timestamp_end) | |
153 | { | |
154 | return -ENOSYS; | |
155 | } | |
156 | ||
157 | static int client_events_discarded(const struct lib_ring_buffer_config *config, | |
158 | struct lib_ring_buffer *bufb, | |
159 | uint64_t *events_discarded) | |
160 | { | |
161 | return -ENOSYS; | |
162 | } | |
163 | ||
2348ca17 JD |
164 | static int client_current_timestamp(const struct lib_ring_buffer_config *config, |
165 | struct lib_ring_buffer *bufb, | |
166 | uint64_t *ts) | |
167 | { | |
168 | return -ENOSYS; | |
169 | } | |
170 | ||
3b731ab1 JD |
171 | static int client_content_size(const struct lib_ring_buffer_config *config, |
172 | struct lib_ring_buffer *bufb, | |
173 | uint64_t *content_size) | |
174 | { | |
175 | return -ENOSYS; | |
176 | } | |
177 | ||
178 | static int client_packet_size(const struct lib_ring_buffer_config *config, | |
179 | struct lib_ring_buffer *bufb, | |
180 | uint64_t *packet_size) | |
181 | { | |
182 | return -ENOSYS; | |
183 | } | |
184 | ||
185 | static int client_stream_id(const struct lib_ring_buffer_config *config, | |
186 | struct lib_ring_buffer *bufb, | |
187 | uint64_t *stream_id) | |
188 | { | |
189 | return -ENOSYS; | |
190 | } | |
191 | ||
5b3cf4f9 JD |
192 | static int client_sequence_number(const struct lib_ring_buffer_config *config, |
193 | struct lib_ring_buffer *bufb, | |
194 | uint64_t *seq) | |
195 | { | |
196 | return -ENOSYS; | |
197 | } | |
198 | ||
5594698f JD |
199 | static |
200 | int client_instance_id(const struct lib_ring_buffer_config *config, | |
201 | struct lib_ring_buffer *bufb, | |
202 | uint64_t *id) | |
203 | { | |
204 | return -ENOSYS; | |
205 | } | |
206 | ||
881833e3 MD |
207 | static const struct lib_ring_buffer_config client_config = { |
208 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, | |
209 | .cb.record_header_size = client_record_header_size, | |
210 | .cb.subbuffer_header_size = client_packet_header_size, | |
211 | .cb.buffer_begin = client_buffer_begin, | |
212 | .cb.buffer_end = client_buffer_end, | |
213 | .cb.buffer_create = client_buffer_create, | |
214 | .cb.buffer_finalize = client_buffer_finalize, | |
215 | ||
216 | .tsc_bits = 0, | |
217 | .alloc = RING_BUFFER_ALLOC_GLOBAL, | |
218 | .sync = RING_BUFFER_SYNC_GLOBAL, | |
219 | .mode = RING_BUFFER_MODE_TEMPLATE, | |
220 | .backend = RING_BUFFER_PAGE, | |
2db1399a | 221 | .output = RING_BUFFER_OUTPUT_TEMPLATE, |
881833e3 MD |
222 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
223 | .ipi = RING_BUFFER_IPI_BARRIER, | |
224 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, | |
225 | }; | |
226 | ||
dd5a0db3 MD |
227 | static |
228 | void release_priv_ops(void *priv_ops) | |
229 | { | |
230 | module_put(THIS_MODULE); | |
231 | } | |
232 | ||
233 | static | |
234 | void lttng_channel_destroy(struct channel *chan) | |
235 | { | |
236 | channel_destroy(chan); | |
237 | } | |
238 | ||
881833e3 MD |
239 | static |
240 | struct channel *_channel_create(const char *name, | |
5cf4b87c | 241 | void *priv, void *buf_addr, |
881833e3 MD |
242 | size_t subbuf_size, size_t num_subbuf, |
243 | unsigned int switch_timer_interval, | |
244 | unsigned int read_timer_interval) | |
245 | { | |
5cf4b87c | 246 | struct lttng_channel *lttng_chan = priv; |
dd5a0db3 MD |
247 | struct channel *chan; |
248 | ||
a36580d5 MD |
249 | chan = channel_create(&client_config, name, |
250 | lttng_chan->session->metadata_cache, buf_addr, | |
881833e3 MD |
251 | subbuf_size, num_subbuf, switch_timer_interval, |
252 | read_timer_interval); | |
dd5a0db3 MD |
253 | if (chan) { |
254 | /* | |
255 | * Ensure this module is not unloaded before we finish | |
256 | * using lttng_relay_transport.ops. | |
257 | */ | |
258 | if (!try_module_get(THIS_MODULE)) { | |
5a15f70c | 259 | printk(KERN_WARNING "LTTng: Can't lock transport module.\n"); |
dd5a0db3 MD |
260 | goto error; |
261 | } | |
262 | chan->backend.priv_ops = <tng_relay_transport.ops; | |
263 | chan->backend.release_priv_ops = release_priv_ops; | |
264 | } | |
265 | return chan; | |
881833e3 | 266 | |
dd5a0db3 MD |
267 | error: |
268 | lttng_channel_destroy(chan); | |
269 | return NULL; | |
881833e3 MD |
270 | } |
271 | ||
272 | static | |
a90917c3 | 273 | struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan) |
881833e3 MD |
274 | { |
275 | struct lib_ring_buffer *buf; | |
881833e3 | 276 | |
cd4bd11f MD |
277 | buf = channel_get_ring_buffer(&client_config, chan, 0); |
278 | if (!lib_ring_buffer_open_read(buf)) | |
279 | return buf; | |
881833e3 MD |
280 | return NULL; |
281 | } | |
282 | ||
f71ecafa | 283 | static |
a90917c3 | 284 | int lttng_buffer_has_read_closed_stream(struct channel *chan) |
f71ecafa MD |
285 | { |
286 | struct lib_ring_buffer *buf; | |
287 | int cpu; | |
288 | ||
289 | for_each_channel_cpu(cpu, chan) { | |
290 | buf = channel_get_ring_buffer(&client_config, chan, cpu); | |
291 | if (!atomic_long_read(&buf->active_readers)) | |
292 | return 1; | |
293 | } | |
294 | return 0; | |
295 | } | |
296 | ||
881833e3 | 297 | static |
a90917c3 | 298 | void lttng_buffer_read_close(struct lib_ring_buffer *buf) |
881833e3 MD |
299 | { |
300 | lib_ring_buffer_release_read(buf); | |
881833e3 MD |
301 | } |
302 | ||
c099397a | 303 | static |
8a57ec02 | 304 | int lttng_event_reserve(struct lttng_kernel_ring_buffer_ctx *ctx) |
881833e3 | 305 | { |
b1199bd3 | 306 | struct channel *chan = ctx->client_priv; |
85a07c33 MD |
307 | int ret; |
308 | ||
b1199bd3 MD |
309 | memset(&ctx->priv, 0, sizeof(ctx->priv)); |
310 | ctx->priv.chan = chan; | |
311 | ||
cc62f29e | 312 | ret = lib_ring_buffer_reserve(&client_config, ctx, NULL); |
85a07c33 MD |
313 | if (ret) |
314 | return ret; | |
315 | lib_ring_buffer_backend_get_pages(&client_config, ctx, | |
b1199bd3 | 316 | &ctx->priv.backend_pages); |
85a07c33 MD |
317 | return 0; |
318 | ||
881833e3 MD |
319 | } |
320 | ||
c099397a | 321 | static |
8a57ec02 | 322 | void lttng_event_commit(struct lttng_kernel_ring_buffer_ctx *ctx) |
881833e3 MD |
323 | { |
324 | lib_ring_buffer_commit(&client_config, ctx); | |
325 | } | |
326 | ||
c099397a | 327 | static |
8a57ec02 | 328 | void lttng_event_write(struct lttng_kernel_ring_buffer_ctx *ctx, const void *src, |
881833e3 MD |
329 | size_t len) |
330 | { | |
331 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
332 | } | |
333 | ||
4ea00e4f | 334 | static |
8a57ec02 | 335 | void lttng_event_write_from_user(struct lttng_kernel_ring_buffer_ctx *ctx, |
4ea00e4f JD |
336 | const void __user *src, size_t len) |
337 | { | |
7b8ea3a5 | 338 | lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len); |
4ea00e4f JD |
339 | } |
340 | ||
58aa5d24 | 341 | static |
8a57ec02 | 342 | void lttng_event_memset(struct lttng_kernel_ring_buffer_ctx *ctx, |
58aa5d24 MD |
343 | int c, size_t len) |
344 | { | |
345 | lib_ring_buffer_memset(&client_config, ctx, c, len); | |
346 | } | |
347 | ||
16f78f3a | 348 | static |
8a57ec02 | 349 | void lttng_event_strcpy(struct lttng_kernel_ring_buffer_ctx *ctx, const char *src, |
16f78f3a MD |
350 | size_t len) |
351 | { | |
352 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); | |
353 | } | |
354 | ||
1ec3f75a | 355 | static |
a90917c3 | 356 | size_t lttng_packet_avail_size(struct channel *chan) |
1ec3f75a MD |
357 | { |
358 | unsigned long o_begin; | |
359 | struct lib_ring_buffer *buf; | |
360 | ||
361 | buf = chan->backend.buf; /* Only for global buffer ! */ | |
362 | o_begin = v_read(&client_config, &buf->offset); | |
363 | if (subbuf_offset(o_begin, chan) != 0) { | |
364 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan); | |
365 | } else { | |
366 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan) | |
367 | - sizeof(struct metadata_packet_header); | |
368 | } | |
369 | } | |
370 | ||
c099397a | 371 | static |
a90917c3 | 372 | wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu) |
c099397a | 373 | { |
71c1d843 MD |
374 | struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config, |
375 | chan, cpu); | |
376 | return &buf->write_wait; | |
24cedcfe MD |
377 | } |
378 | ||
379 | static | |
a90917c3 | 380 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
24cedcfe MD |
381 | { |
382 | return &chan->hp_wait; | |
383 | } | |
384 | ||
385 | static | |
a90917c3 | 386 | int lttng_is_finalized(struct channel *chan) |
24cedcfe MD |
387 | { |
388 | return lib_ring_buffer_channel_is_finalized(chan); | |
c099397a MD |
389 | } |
390 | ||
254ec7bc | 391 | static |
a90917c3 | 392 | int lttng_is_disabled(struct channel *chan) |
254ec7bc MD |
393 | { |
394 | return lib_ring_buffer_channel_is_disabled(chan); | |
395 | } | |
396 | ||
a90917c3 | 397 | static struct lttng_transport lttng_relay_transport = { |
881833e3 MD |
398 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
399 | .owner = THIS_MODULE, | |
400 | .ops = { | |
4a399b76 MD |
401 | .priv = __LTTNG_COMPOUND_LITERAL(struct lttng_kernel_channel_buffer_ops_private, { |
402 | .pub = <tng_relay_transport.ops, | |
403 | .channel_create = _channel_create, | |
404 | .channel_destroy = lttng_channel_destroy, | |
405 | .buffer_read_open = lttng_buffer_read_open, | |
406 | .buffer_has_read_closed_stream = | |
407 | lttng_buffer_has_read_closed_stream, | |
408 | .buffer_read_close = lttng_buffer_read_close, | |
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 | }), | |
a90917c3 MD |
424 | .event_reserve = lttng_event_reserve, |
425 | .event_commit = lttng_event_commit, | |
426 | .event_write_from_user = lttng_event_write_from_user, | |
427 | .event_memset = lttng_event_memset, | |
428 | .event_write = lttng_event_write, | |
16f78f3a | 429 | .event_strcpy = lttng_event_strcpy, |
881833e3 MD |
430 | }, |
431 | }; | |
432 | ||
a90917c3 | 433 | static int __init lttng_ring_buffer_client_init(void) |
881833e3 MD |
434 | { |
435 | /* | |
436 | * This vmalloc sync all also takes care of the lib ring buffer | |
437 | * vmalloc'd module pages when it is built as a module into LTTng. | |
438 | */ | |
263b6c88 | 439 | wrapper_vmalloc_sync_mappings(); |
a90917c3 | 440 | lttng_transport_register(<tng_relay_transport); |
881833e3 MD |
441 | return 0; |
442 | } | |
443 | ||
a90917c3 | 444 | module_init(lttng_ring_buffer_client_init); |
881833e3 | 445 | |
a90917c3 | 446 | static void __exit lttng_ring_buffer_client_exit(void) |
881833e3 | 447 | { |
a90917c3 | 448 | lttng_transport_unregister(<tng_relay_transport); |
881833e3 MD |
449 | } |
450 | ||
a90917c3 | 451 | module_exit(lttng_ring_buffer_client_exit); |
881833e3 MD |
452 | |
453 | MODULE_LICENSE("GPL and additional rights"); | |
1c124020 | 454 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
881833e3 MD |
455 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING |
456 | " client"); | |
1c124020 MJ |
457 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
458 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." | |
459 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) | |
460 | LTTNG_MODULES_EXTRAVERSION); |