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