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