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