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