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