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