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