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