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