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