SoW-2020-0003: Trace Hit Counters
[deliverable/lttng-modules.git] / src / lttng-ring-buffer-client.h
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng-ring-buffer-client.h
4 *
5 * LTTng lib ring buffer client template.
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <lttng/bitfield.h>
13 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
14 #include <wrapper/trace-clock.h>
15 #include <lttng/events.h>
16 #include <lttng/tracer.h>
17 #include <ringbuffer/frontend_types.h>
18
19 #define LTTNG_COMPACT_EVENT_BITS 5
20 #define LTTNG_COMPACT_TSC_BITS 27
21
22 static struct lttng_transport lttng_relay_transport;
23
24 /*
25 * Keep the natural field alignment for _each field_ within this structure if
26 * you ever add/remove a field from this header. Packed attribute is not used
27 * because gcc generates poor code on at least powerpc and mips. Don't ever
28 * let gcc add padding between the structure elements.
29 *
30 * The guarantee we have with timestamps is that all the events in a
31 * packet are included (inclusive) within the begin/end timestamps of
32 * the packet. Another guarantee we have is that the "timestamp begin",
33 * as well as the event timestamps, are monotonically increasing (never
34 * decrease) when moving forward in a stream (physically). But this
35 * guarantee does not apply to "timestamp end", because it is sampled at
36 * commit time, which is not ordered with respect to space reservation.
37 */
38
39 struct packet_header {
40 /* Trace packet header */
41 uint32_t magic; /*
42 * Trace magic number.
43 * contains endianness information.
44 */
45 uint8_t uuid[16];
46 uint32_t stream_id;
47 uint64_t stream_instance_id;
48
49 struct {
50 /* Stream packet context */
51 uint64_t timestamp_begin; /* Cycle count at subbuffer start */
52 uint64_t timestamp_end; /* Cycle count at subbuffer end */
53 uint64_t content_size; /* Size of data in subbuffer */
54 uint64_t packet_size; /* Subbuffer size (include padding) */
55 uint64_t packet_seq_num; /* Packet sequence number */
56 unsigned long events_discarded; /*
57 * Events lost in this subbuffer since
58 * the beginning of the trace.
59 * (may overflow)
60 */
61 uint32_t cpu_id; /* CPU id associated with stream */
62 uint8_t header_end; /* End of header */
63 } ctx;
64 };
65
66 struct lttng_client_ctx {
67 size_t packet_context_len;
68 size_t event_context_len;
69 };
70
71 static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan)
72 {
73 return trace_clock_read64();
74 }
75
76 static inline
77 size_t ctx_get_aligned_size(size_t offset, struct lttng_ctx *ctx,
78 size_t ctx_len)
79 {
80 size_t orig_offset = offset;
81
82 if (likely(!ctx))
83 return 0;
84 offset += lib_ring_buffer_align(offset, ctx->largest_align);
85 offset += ctx_len;
86 return offset - orig_offset;
87 }
88
89 static inline
90 void ctx_get_struct_size(struct lttng_ctx *ctx, size_t *ctx_len,
91 struct lttng_channel *chan, struct lib_ring_buffer_ctx *bufctx)
92 {
93 int i;
94 size_t offset = 0;
95
96 if (likely(!ctx)) {
97 *ctx_len = 0;
98 return;
99 }
100 for (i = 0; i < ctx->nr_fields; i++) {
101 if (ctx->fields[i].get_size)
102 offset += ctx->fields[i].get_size(offset);
103 if (ctx->fields[i].get_size_arg)
104 offset += ctx->fields[i].get_size_arg(offset,
105 &ctx->fields[i], bufctx, chan);
106 }
107 *ctx_len = offset;
108 }
109
110 static inline
111 void ctx_record(struct lib_ring_buffer_ctx *bufctx,
112 struct lttng_channel *chan,
113 struct lttng_ctx *ctx)
114 {
115 int i;
116
117 if (likely(!ctx))
118 return;
119 lib_ring_buffer_align_ctx(bufctx, ctx->largest_align);
120 for (i = 0; i < ctx->nr_fields; i++)
121 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
122 }
123
124 /*
125 * record_header_size - Calculate the header size and padding necessary.
126 * @config: ring buffer instance configuration
127 * @chan: channel
128 * @offset: offset in the write buffer
129 * @pre_header_padding: padding to add before the header (output)
130 * @ctx: reservation context
131 *
132 * Returns the event header size (including padding).
133 *
134 * The payload must itself determine its own alignment from the biggest type it
135 * contains.
136 */
137 static __inline__
138 size_t record_header_size(const struct lib_ring_buffer_config *config,
139 struct channel *chan, size_t offset,
140 size_t *pre_header_padding,
141 struct lib_ring_buffer_ctx *ctx,
142 struct lttng_client_ctx *client_ctx)
143 {
144 struct lttng_event_container *container = channel_get_private(chan);
145 struct lttng_channel *lttng_chan = lttng_event_container_get_channel(container);
146 struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv;
147 struct lttng_event *event = lttng_probe_ctx->event;
148 size_t orig_offset = offset;
149 size_t padding;
150
151 switch (lttng_chan->header_type) {
152 case 1: /* compact */
153 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
154 offset += padding;
155 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
156 offset += sizeof(uint32_t); /* id and timestamp */
157 } else {
158 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
159 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
160 /* Align extended struct on largest member */
161 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
162 offset += sizeof(uint32_t); /* id */
163 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
164 offset += sizeof(uint64_t); /* timestamp */
165 }
166 break;
167 case 2: /* large */
168 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
169 offset += padding;
170 offset += sizeof(uint16_t);
171 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
172 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
173 offset += sizeof(uint32_t); /* timestamp */
174 } else {
175 /* Align extended struct on largest member */
176 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
177 offset += sizeof(uint32_t); /* id */
178 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
179 offset += sizeof(uint64_t); /* timestamp */
180 }
181 break;
182 default:
183 padding = 0;
184 WARN_ON_ONCE(1);
185 }
186 offset += ctx_get_aligned_size(offset, lttng_chan->ctx,
187 client_ctx->packet_context_len);
188 offset += ctx_get_aligned_size(offset, event->ctx,
189 client_ctx->event_context_len);
190
191 *pre_header_padding = padding;
192 return offset - orig_offset;
193 }
194
195 #include <ringbuffer/api.h>
196
197 static
198 void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config,
199 struct lib_ring_buffer_ctx *ctx,
200 uint32_t event_id);
201
202 /*
203 * lttng_write_event_header
204 *
205 * Writes the event header to the offset (already aligned on 32-bits).
206 *
207 * @config: ring buffer instance configuration
208 * @ctx: reservation context
209 * @event_id: event ID
210 */
211 static __inline__
212 void lttng_write_event_header(const struct lib_ring_buffer_config *config,
213 struct lib_ring_buffer_ctx *ctx,
214 uint32_t event_id)
215 {
216 struct lttng_event_container *container = channel_get_private(ctx->chan);
217 struct lttng_channel *lttng_chan = lttng_event_container_get_channel(container);
218 struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv;
219 struct lttng_event *event = lttng_probe_ctx->event;
220
221 if (unlikely(ctx->rflags))
222 goto slow_path;
223
224 switch (lttng_chan->header_type) {
225 case 1: /* compact */
226 {
227 uint32_t id_time = 0;
228
229 bt_bitfield_write(&id_time, uint32_t,
230 0,
231 LTTNG_COMPACT_EVENT_BITS,
232 event_id);
233 bt_bitfield_write(&id_time, uint32_t,
234 LTTNG_COMPACT_EVENT_BITS,
235 LTTNG_COMPACT_TSC_BITS,
236 ctx->tsc);
237 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
238 break;
239 }
240 case 2: /* large */
241 {
242 uint32_t timestamp = (uint32_t) ctx->tsc;
243 uint16_t id = event_id;
244
245 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
246 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
247 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
248 break;
249 }
250 default:
251 WARN_ON_ONCE(1);
252 }
253
254 ctx_record(ctx, lttng_chan, lttng_chan->ctx);
255 ctx_record(ctx, lttng_chan, event->ctx);
256 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
257
258 return;
259
260 slow_path:
261 lttng_write_event_header_slow(config, ctx, event_id);
262 }
263
264 static
265 void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config,
266 struct lib_ring_buffer_ctx *ctx,
267 uint32_t event_id)
268 {
269 struct lttng_event_container *container = channel_get_private(ctx->chan);
270 struct lttng_channel *lttng_chan = lttng_event_container_get_channel(container);
271 struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv;
272 struct lttng_event *event = lttng_probe_ctx->event;
273
274 switch (lttng_chan->header_type) {
275 case 1: /* compact */
276 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
277 uint32_t id_time = 0;
278
279 bt_bitfield_write(&id_time, uint32_t,
280 0,
281 LTTNG_COMPACT_EVENT_BITS,
282 event_id);
283 bt_bitfield_write(&id_time, uint32_t,
284 LTTNG_COMPACT_EVENT_BITS,
285 LTTNG_COMPACT_TSC_BITS, ctx->tsc);
286 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
287 } else {
288 uint8_t id = 0;
289 uint64_t timestamp = ctx->tsc;
290
291 bt_bitfield_write(&id, uint8_t,
292 0,
293 LTTNG_COMPACT_EVENT_BITS,
294 31);
295 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
296 /* Align extended struct on largest member */
297 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
298 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
299 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
300 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
301 }
302 break;
303 case 2: /* large */
304 {
305 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
306 uint32_t timestamp = (uint32_t) ctx->tsc;
307 uint16_t id = event_id;
308
309 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
310 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
311 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
312 } else {
313 uint16_t id = 65535;
314 uint64_t timestamp = ctx->tsc;
315
316 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
317 /* Align extended struct on largest member */
318 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
319 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
320 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
321 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
322 }
323 break;
324 }
325 default:
326 WARN_ON_ONCE(1);
327 }
328 ctx_record(ctx, lttng_chan, lttng_chan->ctx);
329 ctx_record(ctx, lttng_chan, event->ctx);
330 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
331 }
332
333 static const struct lib_ring_buffer_config client_config;
334
335 static u64 client_ring_buffer_clock_read(struct channel *chan)
336 {
337 return lib_ring_buffer_clock_read(chan);
338 }
339
340 static
341 size_t client_record_header_size(const struct lib_ring_buffer_config *config,
342 struct channel *chan, size_t offset,
343 size_t *pre_header_padding,
344 struct lib_ring_buffer_ctx *ctx,
345 void *client_ctx)
346 {
347 return record_header_size(config, chan, offset,
348 pre_header_padding, ctx, client_ctx);
349 }
350
351 /**
352 * client_packet_header_size - called on buffer-switch to a new sub-buffer
353 *
354 * Return header size without padding after the structure. Don't use packed
355 * structure because gcc generates inefficient code on some architectures
356 * (powerpc, mips..)
357 */
358 static size_t client_packet_header_size(void)
359 {
360 return offsetof(struct packet_header, ctx.header_end);
361 }
362
363 static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
364 unsigned int subbuf_idx)
365 {
366 struct channel *chan = buf->backend.chan;
367 struct packet_header *header =
368 (struct packet_header *)
369 lib_ring_buffer_offset_address(&buf->backend,
370 subbuf_idx * chan->backend.subbuf_size);
371 struct lttng_event_container *container = channel_get_private(chan);
372 struct lttng_channel *lttng_chan = lttng_event_container_get_channel(container);
373 struct lttng_session *session = container->session;
374
375 header->magic = CTF_MAGIC_NUMBER;
376 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
377 header->stream_id = lttng_chan->id;
378 header->stream_instance_id = buf->backend.cpu;
379 header->ctx.timestamp_begin = tsc;
380 header->ctx.timestamp_end = 0;
381 header->ctx.content_size = ~0ULL; /* for debugging */
382 header->ctx.packet_size = ~0ULL;
383 header->ctx.packet_seq_num = chan->backend.num_subbuf * \
384 buf->backend.buf_cnt[subbuf_idx].seq_cnt + \
385 subbuf_idx;
386 header->ctx.events_discarded = 0;
387 header->ctx.cpu_id = buf->backend.cpu;
388 }
389
390 /*
391 * offset is assumed to never be 0 here : never deliver a completely empty
392 * subbuffer. data_size is between 1 and subbuf_size.
393 */
394 static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
395 unsigned int subbuf_idx, unsigned long data_size)
396 {
397 struct channel *chan = buf->backend.chan;
398 struct packet_header *header =
399 (struct packet_header *)
400 lib_ring_buffer_offset_address(&buf->backend,
401 subbuf_idx * chan->backend.subbuf_size);
402 unsigned long records_lost = 0;
403
404 header->ctx.timestamp_end = tsc;
405 header->ctx.content_size =
406 (uint64_t) data_size * CHAR_BIT; /* in bits */
407 header->ctx.packet_size =
408 (uint64_t) PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
409 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
410 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
411 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
412 header->ctx.events_discarded = records_lost;
413 }
414
415 static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
416 int cpu, const char *name)
417 {
418 return 0;
419 }
420
421 static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
422 {
423 }
424
425 static struct packet_header *client_packet_header(
426 const struct lib_ring_buffer_config *config,
427 struct lib_ring_buffer *buf)
428 {
429 return lib_ring_buffer_read_offset_address(&buf->backend, 0);
430 }
431
432 static int client_timestamp_begin(const struct lib_ring_buffer_config *config,
433 struct lib_ring_buffer *buf,
434 uint64_t *timestamp_begin)
435 {
436 struct packet_header *header = client_packet_header(config, buf);
437 *timestamp_begin = header->ctx.timestamp_begin;
438
439 return 0;
440 }
441
442 static int client_timestamp_end(const struct lib_ring_buffer_config *config,
443 struct lib_ring_buffer *buf,
444 uint64_t *timestamp_end)
445 {
446 struct packet_header *header = client_packet_header(config, buf);
447 *timestamp_end = header->ctx.timestamp_end;
448
449 return 0;
450 }
451
452 static int client_events_discarded(const struct lib_ring_buffer_config *config,
453 struct lib_ring_buffer *buf,
454 uint64_t *events_discarded)
455 {
456 struct packet_header *header = client_packet_header(config, buf);
457 *events_discarded = header->ctx.events_discarded;
458
459 return 0;
460 }
461
462 static int client_content_size(const struct lib_ring_buffer_config *config,
463 struct lib_ring_buffer *buf,
464 uint64_t *content_size)
465 {
466 struct packet_header *header = client_packet_header(config, buf);
467 *content_size = header->ctx.content_size;
468
469 return 0;
470 }
471
472 static int client_packet_size(const struct lib_ring_buffer_config *config,
473 struct lib_ring_buffer *buf,
474 uint64_t *packet_size)
475 {
476 struct packet_header *header = client_packet_header(config, buf);
477 *packet_size = header->ctx.packet_size;
478
479 return 0;
480 }
481
482 static int client_stream_id(const struct lib_ring_buffer_config *config,
483 struct lib_ring_buffer *buf,
484 uint64_t *stream_id)
485 {
486 struct channel *chan = buf->backend.chan;
487 struct lttng_event_container *container = channel_get_private(chan);
488 struct lttng_channel *lttng_chan = lttng_event_container_get_channel(container);
489
490 *stream_id = lttng_chan->id;
491 return 0;
492 }
493
494 static int client_current_timestamp(const struct lib_ring_buffer_config *config,
495 struct lib_ring_buffer *bufb,
496 uint64_t *ts)
497 {
498 *ts = config->cb.ring_buffer_clock_read(bufb->backend.chan);
499
500 return 0;
501 }
502
503 static int client_sequence_number(const struct lib_ring_buffer_config *config,
504 struct lib_ring_buffer *buf,
505 uint64_t *seq)
506 {
507 struct packet_header *header = client_packet_header(config, buf);
508
509 *seq = header->ctx.packet_seq_num;
510
511 return 0;
512 }
513
514 static
515 int client_instance_id(const struct lib_ring_buffer_config *config,
516 struct lib_ring_buffer *buf,
517 uint64_t *id)
518 {
519 *id = buf->backend.cpu;
520
521 return 0;
522 }
523
524 static const struct lib_ring_buffer_config client_config = {
525 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
526 .cb.record_header_size = client_record_header_size,
527 .cb.subbuffer_header_size = client_packet_header_size,
528 .cb.buffer_begin = client_buffer_begin,
529 .cb.buffer_end = client_buffer_end,
530 .cb.buffer_create = client_buffer_create,
531 .cb.buffer_finalize = client_buffer_finalize,
532
533 .tsc_bits = LTTNG_COMPACT_TSC_BITS,
534 .alloc = RING_BUFFER_ALLOC_PER_CPU,
535 .sync = RING_BUFFER_SYNC_PER_CPU,
536 .mode = RING_BUFFER_MODE_TEMPLATE,
537 .backend = RING_BUFFER_PAGE,
538 .output = RING_BUFFER_OUTPUT_TEMPLATE,
539 .oops = RING_BUFFER_OOPS_CONSISTENCY,
540 .ipi = RING_BUFFER_IPI_BARRIER,
541 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
542 };
543
544 static
545 void release_priv_ops(void *priv_ops)
546 {
547 module_put(THIS_MODULE);
548 }
549
550 static
551 void lttng_channel_destroy(struct channel *chan)
552 {
553 channel_destroy(chan);
554 }
555
556 static
557 struct channel *_channel_create(const char *name,
558 void *priv, void *buf_addr,
559 size_t subbuf_size, size_t num_subbuf,
560 unsigned int switch_timer_interval,
561 unsigned int read_timer_interval)
562 {
563 struct lttng_event_container *container = priv;
564 struct channel *chan;
565
566 chan = channel_create(&client_config, name, container, buf_addr,
567 subbuf_size, num_subbuf, switch_timer_interval,
568 read_timer_interval);
569 if (chan) {
570 /*
571 * Ensure this module is not unloaded before we finish
572 * using lttng_relay_transport.ops.
573 */
574 if (!try_module_get(THIS_MODULE)) {
575 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
576 goto error;
577 }
578 chan->backend.priv_ops = &lttng_relay_transport.ops;
579 chan->backend.release_priv_ops = release_priv_ops;
580 }
581 return chan;
582
583 error:
584 lttng_channel_destroy(chan);
585 return NULL;
586 }
587
588 static
589 struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
590 {
591 struct lib_ring_buffer *buf;
592 int cpu;
593
594 for_each_channel_cpu(cpu, chan) {
595 buf = channel_get_ring_buffer(&client_config, chan, cpu);
596 if (!lib_ring_buffer_open_read(buf))
597 return buf;
598 }
599 return NULL;
600 }
601
602 static
603 int lttng_buffer_has_read_closed_stream(struct channel *chan)
604 {
605 struct lib_ring_buffer *buf;
606 int cpu;
607
608 for_each_channel_cpu(cpu, chan) {
609 buf = channel_get_ring_buffer(&client_config, chan, cpu);
610 if (!atomic_long_read(&buf->active_readers))
611 return 1;
612 }
613 return 0;
614 }
615
616 static
617 void lttng_buffer_read_close(struct lib_ring_buffer *buf)
618 {
619 lib_ring_buffer_release_read(buf);
620 }
621
622 static
623 int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx,
624 uint32_t event_id)
625 {
626 struct lttng_event_container *container = channel_get_private(ctx->chan);
627 struct lttng_channel *lttng_chan = lttng_event_container_get_channel(container);
628 struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv;
629 struct lttng_event *event = lttng_probe_ctx->event;
630 struct lttng_client_ctx client_ctx;
631 int ret, cpu;
632
633 cpu = lib_ring_buffer_get_cpu(&client_config);
634 if (unlikely(cpu < 0))
635 return -EPERM;
636 ctx->cpu = cpu;
637
638 /* Compute internal size of context structures. */
639 ctx_get_struct_size(lttng_chan->ctx, &client_ctx.packet_context_len, lttng_chan, ctx);
640 ctx_get_struct_size(event->ctx, &client_ctx.event_context_len, lttng_chan, ctx);
641
642 switch (lttng_chan->header_type) {
643 case 1: /* compact */
644 if (event_id > 30)
645 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
646 break;
647 case 2: /* large */
648 if (event_id > 65534)
649 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
650 break;
651 default:
652 WARN_ON_ONCE(1);
653 }
654
655 ret = lib_ring_buffer_reserve(&client_config, ctx, &client_ctx);
656 if (unlikely(ret))
657 goto put;
658 lib_ring_buffer_backend_get_pages(&client_config, ctx,
659 &ctx->backend_pages);
660 lttng_write_event_header(&client_config, ctx, event_id);
661 return 0;
662 put:
663 lib_ring_buffer_put_cpu(&client_config);
664 return ret;
665 }
666
667 static
668 void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
669 {
670 lib_ring_buffer_commit(&client_config, ctx);
671 lib_ring_buffer_put_cpu(&client_config);
672 }
673
674 static
675 void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
676 size_t len)
677 {
678 lib_ring_buffer_write(&client_config, ctx, src, len);
679 }
680
681 static
682 void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
683 const void __user *src, size_t len)
684 {
685 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
686 }
687
688 static
689 void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
690 int c, size_t len)
691 {
692 lib_ring_buffer_memset(&client_config, ctx, c, len);
693 }
694
695 static
696 void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src,
697 size_t len)
698 {
699 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
700 }
701
702 static
703 void lttng_event_strcpy_from_user(struct lib_ring_buffer_ctx *ctx,
704 const char __user *src, size_t len)
705 {
706 lib_ring_buffer_strcpy_from_user_inatomic(&client_config, ctx, src,
707 len, '#');
708 }
709
710 static
711 wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
712 {
713 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
714 chan, cpu);
715 return &buf->write_wait;
716 }
717
718 static
719 wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
720 {
721 return &chan->hp_wait;
722 }
723
724 static
725 int lttng_is_finalized(struct channel *chan)
726 {
727 return lib_ring_buffer_channel_is_finalized(chan);
728 }
729
730 static
731 int lttng_is_disabled(struct channel *chan)
732 {
733 return lib_ring_buffer_channel_is_disabled(chan);
734 }
735
736 static struct lttng_transport lttng_relay_transport = {
737 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
738 .owner = THIS_MODULE,
739 .ops = {
740 .channel_create = _channel_create,
741 .channel_destroy = lttng_channel_destroy,
742 .buffer_read_open = lttng_buffer_read_open,
743 .buffer_has_read_closed_stream =
744 lttng_buffer_has_read_closed_stream,
745 .buffer_read_close = lttng_buffer_read_close,
746 .event_reserve = lttng_event_reserve,
747 .event_commit = lttng_event_commit,
748 .event_write = lttng_event_write,
749 .event_write_from_user = lttng_event_write_from_user,
750 .event_memset = lttng_event_memset,
751 .event_strcpy = lttng_event_strcpy,
752 .event_strcpy_from_user = lttng_event_strcpy_from_user,
753 .packet_avail_size = NULL, /* Would be racy anyway */
754 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
755 .get_hp_wait_queue = lttng_get_hp_wait_queue,
756 .is_finalized = lttng_is_finalized,
757 .is_disabled = lttng_is_disabled,
758 .timestamp_begin = client_timestamp_begin,
759 .timestamp_end = client_timestamp_end,
760 .events_discarded = client_events_discarded,
761 .content_size = client_content_size,
762 .packet_size = client_packet_size,
763 .stream_id = client_stream_id,
764 .current_timestamp = client_current_timestamp,
765 .sequence_number = client_sequence_number,
766 .instance_id = client_instance_id,
767 },
768 };
769
770 static int __init lttng_ring_buffer_client_init(void)
771 {
772 /*
773 * This vmalloc sync all also takes care of the lib ring buffer
774 * vmalloc'd module pages when it is built as a module into LTTng.
775 */
776 wrapper_vmalloc_sync_mappings();
777 lttng_transport_register(&lttng_relay_transport);
778 return 0;
779 }
780
781 module_init(lttng_ring_buffer_client_init);
782
783 static void __exit lttng_ring_buffer_client_exit(void)
784 {
785 lttng_transport_unregister(&lttng_relay_transport);
786 }
787
788 module_exit(lttng_ring_buffer_client_exit);
789
790 MODULE_LICENSE("GPL and additional rights");
791 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
792 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
793 " client");
794 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
795 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
796 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
797 LTTNG_MODULES_EXTRAVERSION);
This page took 0.048218 seconds and 5 git commands to generate.