lib ring buffer: add frontend init as module_init
[lttng-modules.git] / ltt-ring-buffer-client.h
... / ...
CommitLineData
1/*
2 * ltt-ring-buffer-client.h
3 *
4 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng lib ring buffer client template.
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
11#include <linux/module.h>
12#include <linux/types.h>
13#include "lib/bitfield.h"
14#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
15#include "wrapper/trace-clock.h"
16#include "ltt-events.h"
17#include "ltt-tracer.h"
18#include "wrapper/ringbuffer/frontend_types.h"
19
20/*
21 * Keep the natural field alignment for _each field_ within this structure if
22 * you ever add/remove a field from this header. Packed attribute is not used
23 * because gcc generates poor code on at least powerpc and mips. Don't ever
24 * let gcc add padding between the structure elements.
25 *
26 * The guarantee we have with timestamps is that all the events in a
27 * packet are included (inclusive) within the begin/end timestamps of
28 * the packet. Another guarantee we have is that the "timestamp begin",
29 * as well as the event timestamps, are monotonically increasing (never
30 * decrease) when moving forward in a stream (physically). But this
31 * guarantee does not apply to "timestamp end", because it is sampled at
32 * commit time, which is not ordered with respect to space reservation.
33 */
34
35struct packet_header {
36 /* Trace packet header */
37 uint32_t magic; /*
38 * Trace magic number.
39 * contains endianness information.
40 */
41 uint8_t uuid[16];
42 uint32_t stream_id;
43
44 struct {
45 /* Stream packet context */
46 uint64_t timestamp_begin; /* Cycle count at subbuffer start */
47 uint64_t timestamp_end; /* Cycle count at subbuffer end */
48 uint32_t events_discarded; /*
49 * Events lost in this subbuffer since
50 * the beginning of the trace.
51 * (may overflow)
52 */
53 uint32_t content_size; /* Size of data in subbuffer */
54 uint32_t packet_size; /* Subbuffer size (include padding) */
55 uint32_t cpu_id; /* CPU id associated with stream */
56 uint8_t header_end; /* End of header */
57 } ctx;
58};
59
60
61static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan)
62{
63 return trace_clock_read64();
64}
65
66static inline
67size_t ctx_get_size(size_t offset, struct lttng_ctx *ctx)
68{
69 int i;
70 size_t orig_offset = offset;
71
72 if (likely(!ctx))
73 return 0;
74 for (i = 0; i < ctx->nr_fields; i++)
75 offset += ctx->fields[i].get_size(offset);
76 return offset - orig_offset;
77}
78
79static inline
80void ctx_record(struct lib_ring_buffer_ctx *bufctx,
81 struct ltt_channel *chan,
82 struct lttng_ctx *ctx)
83{
84 int i;
85
86 if (likely(!ctx))
87 return;
88 for (i = 0; i < ctx->nr_fields; i++)
89 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
90}
91
92/*
93 * record_header_size - Calculate the header size and padding necessary.
94 * @config: ring buffer instance configuration
95 * @chan: channel
96 * @offset: offset in the write buffer
97 * @pre_header_padding: padding to add before the header (output)
98 * @ctx: reservation context
99 *
100 * Returns the event header size (including padding).
101 *
102 * The payload must itself determine its own alignment from the biggest type it
103 * contains.
104 */
105static __inline__
106unsigned char record_header_size(const struct lib_ring_buffer_config *config,
107 struct channel *chan, size_t offset,
108 size_t *pre_header_padding,
109 struct lib_ring_buffer_ctx *ctx)
110{
111 struct ltt_channel *ltt_chan = channel_get_private(chan);
112 struct ltt_event *event = ctx->priv;
113 size_t orig_offset = offset;
114 size_t padding;
115
116 switch (ltt_chan->header_type) {
117 case 1: /* compact */
118 padding = lib_ring_buffer_align(offset, ltt_alignof(uint32_t));
119 offset += padding;
120 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
121 offset += sizeof(uint32_t); /* id and timestamp */
122 } else {
123 /* Minimum space taken by 5-bit id */
124 offset += sizeof(uint8_t);
125 /* Align extended struct on largest member */
126 offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t));
127 offset += sizeof(uint32_t); /* id */
128 offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t));
129 offset += sizeof(uint64_t); /* timestamp */
130 }
131 break;
132 case 2: /* large */
133 padding = lib_ring_buffer_align(offset, ltt_alignof(uint16_t));
134 offset += padding;
135 offset += sizeof(uint16_t);
136 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
137 offset += lib_ring_buffer_align(offset, ltt_alignof(uint32_t));
138 offset += sizeof(uint32_t); /* timestamp */
139 } else {
140 /* Align extended struct on largest member */
141 offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t));
142 offset += sizeof(uint32_t); /* id */
143 offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t));
144 offset += sizeof(uint64_t); /* timestamp */
145 }
146 break;
147 default:
148 padding = 0;
149 WARN_ON_ONCE(1);
150 }
151 offset += ctx_get_size(offset, event->ctx);
152 offset += ctx_get_size(offset, ltt_chan->ctx);
153
154 *pre_header_padding = padding;
155 return offset - orig_offset;
156}
157
158#include "wrapper/ringbuffer/api.h"
159
160static
161void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config,
162 struct lib_ring_buffer_ctx *ctx,
163 uint32_t event_id);
164
165/*
166 * ltt_write_event_header
167 *
168 * Writes the event header to the offset (already aligned on 32-bits).
169 *
170 * @config: ring buffer instance configuration
171 * @ctx: reservation context
172 * @event_id: event ID
173 */
174static __inline__
175void ltt_write_event_header(const struct lib_ring_buffer_config *config,
176 struct lib_ring_buffer_ctx *ctx,
177 uint32_t event_id)
178{
179 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
180 struct ltt_event *event = ctx->priv;
181
182 if (unlikely(ctx->rflags))
183 goto slow_path;
184
185 switch (ltt_chan->header_type) {
186 case 1: /* compact */
187 {
188 uint32_t id_time = 0;
189
190 bt_bitfield_write(&id_time, uint32_t, 0, 5, event_id);
191 bt_bitfield_write(&id_time, uint32_t, 5, 27, ctx->tsc);
192 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
193 break;
194 }
195 case 2: /* large */
196 {
197 uint32_t timestamp = (uint32_t) ctx->tsc;
198 uint16_t id = event_id;
199
200 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
201 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint32_t));
202 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
203 break;
204 }
205 default:
206 WARN_ON_ONCE(1);
207 }
208
209 ctx_record(ctx, ltt_chan, ltt_chan->ctx);
210 ctx_record(ctx, ltt_chan, event->ctx);
211
212 return;
213
214slow_path:
215 ltt_write_event_header_slow(config, ctx, event_id);
216}
217
218static
219void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config,
220 struct lib_ring_buffer_ctx *ctx,
221 uint32_t event_id)
222{
223 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
224 struct ltt_event *event = ctx->priv;
225
226 switch (ltt_chan->header_type) {
227 case 1: /* compact */
228 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
229 uint32_t id_time = 0;
230
231 bt_bitfield_write(&id_time, uint32_t, 0, 5, event_id);
232 bt_bitfield_write(&id_time, uint32_t, 5, 27, ctx->tsc);
233 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
234 } else {
235 uint8_t id = 0;
236 uint64_t timestamp = ctx->tsc;
237
238 bt_bitfield_write(&id, uint8_t, 0, 5, 31);
239 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
240 /* Align extended struct on largest member */
241 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
242 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
243 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
244 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
245 }
246 break;
247 case 2: /* large */
248 {
249 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
250 uint32_t timestamp = (uint32_t) ctx->tsc;
251 uint16_t id = event_id;
252
253 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
254 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint32_t));
255 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
256 } else {
257 uint16_t id = 65535;
258 uint64_t timestamp = ctx->tsc;
259
260 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
261 /* Align extended struct on largest member */
262 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
263 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
264 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
265 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
266 }
267 break;
268 }
269 default:
270 WARN_ON_ONCE(1);
271 }
272 ctx_record(ctx, ltt_chan, ltt_chan->ctx);
273 ctx_record(ctx, ltt_chan, event->ctx);
274}
275
276static const struct lib_ring_buffer_config client_config;
277
278static u64 client_ring_buffer_clock_read(struct channel *chan)
279{
280 return lib_ring_buffer_clock_read(chan);
281}
282
283static
284size_t client_record_header_size(const struct lib_ring_buffer_config *config,
285 struct channel *chan, size_t offset,
286 size_t *pre_header_padding,
287 struct lib_ring_buffer_ctx *ctx)
288{
289 return record_header_size(config, chan, offset,
290 pre_header_padding, ctx);
291}
292
293/**
294 * client_packet_header_size - called on buffer-switch to a new sub-buffer
295 *
296 * Return header size without padding after the structure. Don't use packed
297 * structure because gcc generates inefficient code on some architectures
298 * (powerpc, mips..)
299 */
300static size_t client_packet_header_size(void)
301{
302 return offsetof(struct packet_header, ctx.header_end);
303}
304
305static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
306 unsigned int subbuf_idx)
307{
308 struct channel *chan = buf->backend.chan;
309 struct packet_header *header =
310 (struct packet_header *)
311 lib_ring_buffer_offset_address(&buf->backend,
312 subbuf_idx * chan->backend.subbuf_size);
313 struct ltt_channel *ltt_chan = channel_get_private(chan);
314 struct ltt_session *session = ltt_chan->session;
315
316 header->magic = CTF_MAGIC_NUMBER;
317 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
318 header->stream_id = ltt_chan->id;
319 header->ctx.timestamp_begin = tsc;
320 header->ctx.timestamp_end = 0;
321 header->ctx.events_discarded = 0;
322 header->ctx.content_size = 0xFFFFFFFF; /* for debugging */
323 header->ctx.packet_size = 0xFFFFFFFF;
324 header->ctx.cpu_id = buf->backend.cpu;
325}
326
327/*
328 * offset is assumed to never be 0 here : never deliver a completely empty
329 * subbuffer. data_size is between 1 and subbuf_size.
330 */
331static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
332 unsigned int subbuf_idx, unsigned long data_size)
333{
334 struct channel *chan = buf->backend.chan;
335 struct packet_header *header =
336 (struct packet_header *)
337 lib_ring_buffer_offset_address(&buf->backend,
338 subbuf_idx * chan->backend.subbuf_size);
339 unsigned long records_lost = 0;
340
341 header->ctx.timestamp_end = tsc;
342 header->ctx.content_size = data_size * CHAR_BIT; /* in bits */
343 header->ctx.packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
344 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
345 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
346 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
347 header->ctx.events_discarded = records_lost;
348}
349
350static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
351 int cpu, const char *name)
352{
353 return 0;
354}
355
356static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
357{
358}
359
360static const struct lib_ring_buffer_config client_config = {
361 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
362 .cb.record_header_size = client_record_header_size,
363 .cb.subbuffer_header_size = client_packet_header_size,
364 .cb.buffer_begin = client_buffer_begin,
365 .cb.buffer_end = client_buffer_end,
366 .cb.buffer_create = client_buffer_create,
367 .cb.buffer_finalize = client_buffer_finalize,
368
369 .tsc_bits = 32,
370 .alloc = RING_BUFFER_ALLOC_PER_CPU,
371 .sync = RING_BUFFER_SYNC_PER_CPU,
372 .mode = RING_BUFFER_MODE_TEMPLATE,
373 .backend = RING_BUFFER_PAGE,
374 .output = RING_BUFFER_OUTPUT_TEMPLATE,
375 .oops = RING_BUFFER_OOPS_CONSISTENCY,
376 .ipi = RING_BUFFER_IPI_BARRIER,
377 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
378};
379
380static
381struct channel *_channel_create(const char *name,
382 struct ltt_channel *ltt_chan, void *buf_addr,
383 size_t subbuf_size, size_t num_subbuf,
384 unsigned int switch_timer_interval,
385 unsigned int read_timer_interval)
386{
387 return channel_create(&client_config, name, ltt_chan, buf_addr,
388 subbuf_size, num_subbuf, switch_timer_interval,
389 read_timer_interval);
390}
391
392static
393void ltt_channel_destroy(struct channel *chan)
394{
395 channel_destroy(chan);
396}
397
398static
399struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan)
400{
401 struct lib_ring_buffer *buf;
402 int cpu;
403
404 for_each_channel_cpu(cpu, chan) {
405 buf = channel_get_ring_buffer(&client_config, chan, cpu);
406 if (!lib_ring_buffer_open_read(buf))
407 return buf;
408 }
409 return NULL;
410}
411
412static
413int ltt_buffer_has_read_closed_stream(struct channel *chan)
414{
415 struct lib_ring_buffer *buf;
416 int cpu;
417
418 for_each_channel_cpu(cpu, chan) {
419 buf = channel_get_ring_buffer(&client_config, chan, cpu);
420 if (!atomic_long_read(&buf->active_readers))
421 return 1;
422 }
423 return 0;
424}
425
426static
427void ltt_buffer_read_close(struct lib_ring_buffer *buf)
428{
429 lib_ring_buffer_release_read(buf);
430}
431
432static
433int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx,
434 uint32_t event_id)
435{
436 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
437 int ret, cpu;
438
439 cpu = lib_ring_buffer_get_cpu(&client_config);
440 if (cpu < 0)
441 return -EPERM;
442 ctx->cpu = cpu;
443
444 switch (ltt_chan->header_type) {
445 case 1: /* compact */
446 if (event_id > 30)
447 ctx->rflags |= LTT_RFLAG_EXTENDED;
448 break;
449 case 2: /* large */
450 if (event_id > 65534)
451 ctx->rflags |= LTT_RFLAG_EXTENDED;
452 break;
453 default:
454 WARN_ON_ONCE(1);
455 }
456
457 ret = lib_ring_buffer_reserve(&client_config, ctx);
458 if (ret)
459 goto put;
460 ltt_write_event_header(&client_config, ctx, event_id);
461 return 0;
462put:
463 lib_ring_buffer_put_cpu(&client_config);
464 return ret;
465}
466
467static
468void ltt_event_commit(struct lib_ring_buffer_ctx *ctx)
469{
470 lib_ring_buffer_commit(&client_config, ctx);
471 lib_ring_buffer_put_cpu(&client_config);
472}
473
474static
475void ltt_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
476 size_t len)
477{
478 lib_ring_buffer_write(&client_config, ctx, src, len);
479}
480
481static
482wait_queue_head_t *ltt_get_writer_buf_wait_queue(struct channel *chan, int cpu)
483{
484 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
485 chan, cpu);
486 return &buf->write_wait;
487}
488
489static
490wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan)
491{
492 return &chan->hp_wait;
493}
494
495static
496int ltt_is_finalized(struct channel *chan)
497{
498 return lib_ring_buffer_channel_is_finalized(chan);
499}
500
501static
502int ltt_is_disabled(struct channel *chan)
503{
504 return lib_ring_buffer_channel_is_disabled(chan);
505}
506
507static struct ltt_transport ltt_relay_transport = {
508 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
509 .owner = THIS_MODULE,
510 .ops = {
511 .channel_create = _channel_create,
512 .channel_destroy = ltt_channel_destroy,
513 .buffer_read_open = ltt_buffer_read_open,
514 .buffer_has_read_closed_stream =
515 ltt_buffer_has_read_closed_stream,
516 .buffer_read_close = ltt_buffer_read_close,
517 .event_reserve = ltt_event_reserve,
518 .event_commit = ltt_event_commit,
519 .event_write = ltt_event_write,
520 .packet_avail_size = NULL, /* Would be racy anyway */
521 .get_writer_buf_wait_queue = ltt_get_writer_buf_wait_queue,
522 .get_hp_wait_queue = ltt_get_hp_wait_queue,
523 .is_finalized = ltt_is_finalized,
524 .is_disabled = ltt_is_disabled,
525 },
526};
527
528static int __init ltt_ring_buffer_client_init(void)
529{
530 /*
531 * This vmalloc sync all also takes care of the lib ring buffer
532 * vmalloc'd module pages when it is built as a module into LTTng.
533 */
534 wrapper_vmalloc_sync_all();
535 ltt_transport_register(&ltt_relay_transport);
536 return 0;
537}
538
539module_init(ltt_ring_buffer_client_init);
540
541static void __exit ltt_ring_buffer_client_exit(void)
542{
543 ltt_transport_unregister(&ltt_relay_transport);
544}
545
546module_exit(ltt_ring_buffer_client_exit);
547
548MODULE_LICENSE("GPL and additional rights");
549MODULE_AUTHOR("Mathieu Desnoyers");
550MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
551 " client");
This page took 0.023807 seconds and 4 git commands to generate.