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