Handle perf counter's inability to setup counters before cpu is brought online
[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:
64c796d8 140 WARN_ON_ONCE(1);
881833e3 141 }
f1676205
MD
142 offset += ctx_get_size(offset, event->ctx);
143 offset += ctx_get_size(offset, ltt_chan->ctx);
881833e3
MD
144
145 *pre_header_padding = padding;
146 return offset - orig_offset;
147}
148
149#include "wrapper/ringbuffer/api.h"
150
151extern
152void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config,
153 struct lib_ring_buffer_ctx *ctx,
64c796d8 154 uint32_t event_id);
881833e3
MD
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
4e1f08f4 163 * @event_id: event ID
881833e3
MD
164 */
165static __inline__
166void ltt_write_event_header(const struct lib_ring_buffer_config *config,
167 struct lib_ring_buffer_ctx *ctx,
64c796d8 168 uint32_t event_id)
881833e3 169{
9115fbdc 170 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
f1676205 171 struct ltt_event *event = ctx->priv;
881833e3
MD
172
173 if (unlikely(ctx->rflags))
174 goto slow_path;
175
9115fbdc
MD
176 switch (ltt_chan->header_type) {
177 case 1: /* compact */
178 {
179 uint32_t id_time = 0;
180
4e1f08f4 181 bt_bitfield_write(&id_time, uint32_t, 0, 5, event_id);
9115fbdc
MD
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 {
9115fbdc 188 uint32_t timestamp = (uint32_t) ctx->tsc;
7e855749 189 uint16_t id = event_id;
9115fbdc 190
7e855749 191 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
9115fbdc
MD
192 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint32_t));
193 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
194 break;
195 }
196 default:
64c796d8 197 WARN_ON_ONCE(1);
9115fbdc 198 }
f1676205
MD
199
200 ctx_record(ctx, ltt_chan, event->ctx);
201 ctx_record(ctx, ltt_chan, ltt_chan->ctx);
f1676205 202
9115fbdc 203 return;
881833e3
MD
204
205slow_path:
4e1f08f4 206 ltt_write_event_header_slow(config, ctx, event_id);
881833e3
MD
207}
208
881833e3 209void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config,
64c796d8
MD
210 struct lib_ring_buffer_ctx *ctx,
211 uint32_t event_id)
881833e3 212{
9115fbdc 213 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
f1676205 214 struct ltt_event *event = ctx->priv;
9115fbdc
MD
215
216 switch (ltt_chan->header_type) {
217 case 1: /* compact */
64c796d8 218 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
9115fbdc
MD
219 uint32_t id_time = 0;
220
4e1f08f4 221 bt_bitfield_write(&id_time, uint32_t, 0, 5, event_id);
9115fbdc
MD
222 bt_bitfield_write(&id_time, uint32_t, 5, 27, ctx->tsc);
223 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
224 } else {
225 uint8_t id = 0;
9115fbdc
MD
226 uint64_t timestamp = ctx->tsc;
227
228 bt_bitfield_write(&id, uint8_t, 0, 5, 31);
229 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
230 /* Align extended struct on largest member */
231 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
232 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
233 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
234 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
235 }
881833e3 236 break;
9115fbdc
MD
237 case 2: /* large */
238 {
64c796d8 239 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
9115fbdc 240 uint32_t timestamp = (uint32_t) ctx->tsc;
7e855749 241 uint16_t id = event_id;
9115fbdc 242
7e855749 243 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
9115fbdc
MD
244 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint32_t));
245 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
246 } else {
64c796d8 247 uint16_t id = 65535;
9115fbdc
MD
248 uint64_t timestamp = ctx->tsc;
249
64c796d8 250 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
9115fbdc
MD
251 /* Align extended struct on largest member */
252 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
64c796d8 253 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
9115fbdc
MD
254 lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t));
255 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
256 }
881833e3 257 break;
881833e3 258 }
9115fbdc 259 default:
64c796d8 260 WARN_ON_ONCE(1);
881833e3 261 }
f1676205
MD
262 ctx_record(ctx, ltt_chan, event->ctx);
263 ctx_record(ctx, ltt_chan, ltt_chan->ctx);
881833e3
MD
264}
265
7514523f
MD
266static const struct lib_ring_buffer_config client_config;
267
268static u64 client_ring_buffer_clock_read(struct channel *chan)
269{
270 return lib_ring_buffer_clock_read(chan);
271}
272
1e2015dc 273static
7514523f
MD
274size_t client_record_header_size(const struct lib_ring_buffer_config *config,
275 struct channel *chan, size_t offset,
7514523f 276 size_t *pre_header_padding,
7514523f
MD
277 struct lib_ring_buffer_ctx *ctx)
278{
64c796d8
MD
279 return record_header_size(config, chan, offset,
280 pre_header_padding, ctx);
7514523f
MD
281}
282
283/**
1c25284c 284 * client_packet_header_size - called on buffer-switch to a new sub-buffer
7514523f
MD
285 *
286 * Return header size without padding after the structure. Don't use packed
287 * structure because gcc generates inefficient code on some architectures
288 * (powerpc, mips..)
289 */
1c25284c 290static size_t client_packet_header_size(void)
7514523f 291{
9115fbdc 292 return offsetof(struct packet_header, ctx.header_end);
7514523f
MD
293}
294
295static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
296 unsigned int subbuf_idx)
297{
298 struct channel *chan = buf->backend.chan;
1c25284c
MD
299 struct packet_header *header =
300 (struct packet_header *)
7514523f
MD
301 lib_ring_buffer_offset_address(&buf->backend,
302 subbuf_idx * chan->backend.subbuf_size);
9115fbdc
MD
303 struct ltt_channel *ltt_chan = channel_get_private(chan);
304 struct ltt_session *session = ltt_chan->session;
7514523f 305
d793d5e1 306 header->magic = CTF_MAGIC_NUMBER;
1ec3f75a 307 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
9115fbdc
MD
308 header->stream_id = ltt_chan->id;
309 header->ctx.timestamp_begin = tsc;
310 header->ctx.timestamp_end = 0;
311 header->ctx.events_discarded = 0;
312 header->ctx.content_size = 0xFFFFFFFF; /* for debugging */
313 header->ctx.packet_size = 0xFFFFFFFF;
314 header->ctx.cpu_id = buf->backend.cpu;
7514523f
MD
315}
316
317/*
318 * offset is assumed to never be 0 here : never deliver a completely empty
319 * subbuffer. data_size is between 1 and subbuf_size.
320 */
321static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
322 unsigned int subbuf_idx, unsigned long data_size)
323{
324 struct channel *chan = buf->backend.chan;
1c25284c
MD
325 struct packet_header *header =
326 (struct packet_header *)
7514523f
MD
327 lib_ring_buffer_offset_address(&buf->backend,
328 subbuf_idx * chan->backend.subbuf_size);
329 unsigned long records_lost = 0;
330
9115fbdc 331 header->ctx.timestamp_end = tsc;
05d32c64
MD
332 header->ctx.content_size = data_size * CHAR_BIT; /* in bits */
333 header->ctx.packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
7514523f
MD
334 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
335 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
336 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
9115fbdc 337 header->ctx.events_discarded = records_lost;
7514523f
MD
338}
339
340static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
341 int cpu, const char *name)
342{
1c25284c 343 return 0;
7514523f
MD
344}
345
346static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
347{
7514523f
MD
348}
349
350static const struct lib_ring_buffer_config client_config = {
351 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
352 .cb.record_header_size = client_record_header_size,
1c25284c 353 .cb.subbuffer_header_size = client_packet_header_size,
7514523f
MD
354 .cb.buffer_begin = client_buffer_begin,
355 .cb.buffer_end = client_buffer_end,
356 .cb.buffer_create = client_buffer_create,
357 .cb.buffer_finalize = client_buffer_finalize,
358
359 .tsc_bits = 32,
360 .alloc = RING_BUFFER_ALLOC_PER_CPU,
361 .sync = RING_BUFFER_SYNC_PER_CPU,
3d084699 362 .mode = RING_BUFFER_MODE_TEMPLATE,
7514523f
MD
363 .backend = RING_BUFFER_PAGE,
364 .output = RING_BUFFER_SPLICE,
365 .oops = RING_BUFFER_OOPS_CONSISTENCY,
366 .ipi = RING_BUFFER_IPI_BARRIER,
367 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
368};
369
1e2015dc 370static
1c25284c 371struct channel *_channel_create(const char *name,
9115fbdc 372 struct ltt_channel *ltt_chan, void *buf_addr,
1c25284c
MD
373 size_t subbuf_size, size_t num_subbuf,
374 unsigned int switch_timer_interval,
375 unsigned int read_timer_interval)
7514523f 376{
9115fbdc 377 return channel_create(&client_config, name, ltt_chan, buf_addr,
7514523f
MD
378 subbuf_size, num_subbuf, switch_timer_interval,
379 read_timer_interval);
7514523f
MD
380}
381
1e2015dc 382static
7514523f
MD
383void ltt_channel_destroy(struct channel *chan)
384{
7514523f 385 channel_destroy(chan);
7514523f
MD
386}
387
ad1c05e1
MD
388static
389struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan)
390{
391 struct lib_ring_buffer *buf;
392 int cpu;
393
1c25284c
MD
394 for_each_channel_cpu(cpu, chan) {
395 buf = channel_get_ring_buffer(&client_config, chan, cpu);
ad1c05e1
MD
396 if (!lib_ring_buffer_open_read(buf))
397 return buf;
398 }
399 return NULL;
400}
401
402static
1c25284c 403void ltt_buffer_read_close(struct lib_ring_buffer *buf)
ad1c05e1
MD
404{
405 lib_ring_buffer_release_read(buf);
1c25284c
MD
406}
407
c099397a 408static
4e1f08f4 409int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx,
64c796d8 410 uint32_t event_id)
1c25284c 411{
64c796d8 412 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
1c25284c
MD
413 int ret, cpu;
414
415 cpu = lib_ring_buffer_get_cpu(&client_config);
416 if (cpu < 0)
417 return -EPERM;
418 ctx->cpu = cpu;
419
64c796d8
MD
420 switch (ltt_chan->header_type) {
421 case 1: /* compact */
422 if (event_id > 30)
423 ctx->rflags |= LTT_RFLAG_EXTENDED;
424 break;
425 case 2: /* large */
426 if (event_id > 65534)
427 ctx->rflags |= LTT_RFLAG_EXTENDED;
428 break;
429 default:
430 WARN_ON_ONCE(1);
431 }
432
1c25284c
MD
433 ret = lib_ring_buffer_reserve(&client_config, ctx);
434 if (ret)
435 goto put;
4e1f08f4
MD
436 ltt_write_event_header(&client_config, ctx, event_id);
437 return 0;
1c25284c
MD
438put:
439 lib_ring_buffer_put_cpu(&client_config);
440 return ret;
ad1c05e1
MD
441}
442
c099397a 443static
1c25284c
MD
444void ltt_event_commit(struct lib_ring_buffer_ctx *ctx)
445{
446 lib_ring_buffer_commit(&client_config, ctx);
447 lib_ring_buffer_put_cpu(&client_config);
448}
449
c099397a 450static
e763dbf5
MD
451void ltt_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
452 size_t len)
453{
454 lib_ring_buffer_write(&client_config, ctx, src, len);
455}
1c25284c 456
c099397a
MD
457static
458wait_queue_head_t *ltt_get_reader_wait_queue(struct ltt_channel *chan)
459{
460 return &chan->chan->read_wait;
461}
462
7514523f 463static struct ltt_transport ltt_relay_transport = {
3d084699 464 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
7514523f
MD
465 .owner = THIS_MODULE,
466 .ops = {
1c25284c
MD
467 .channel_create = _channel_create,
468 .channel_destroy = ltt_channel_destroy,
ad1c05e1
MD
469 .buffer_read_open = ltt_buffer_read_open,
470 .buffer_read_close = ltt_buffer_read_close,
1c25284c
MD
471 .event_reserve = ltt_event_reserve,
472 .event_commit = ltt_event_commit,
e763dbf5 473 .event_write = ltt_event_write,
1ec3f75a 474 .packet_avail_size = NULL, /* Would be racy anyway */
c099397a 475 .get_reader_wait_queue = ltt_get_reader_wait_queue,
7514523f
MD
476 },
477};
478
3d084699 479static int __init ltt_ring_buffer_client_init(void)
7514523f 480{
a509e133
MD
481 /*
482 * This vmalloc sync all also takes care of the lib ring buffer
483 * vmalloc'd module pages when it is built as a module into LTTng.
484 */
6d2a620c 485 wrapper_vmalloc_sync_all();
7514523f
MD
486 printk(KERN_INFO "LTT : ltt ring buffer client init\n");
487 ltt_transport_register(&ltt_relay_transport);
488 return 0;
489}
490
1c25284c
MD
491module_init(ltt_ring_buffer_client_init);
492
3d084699 493static void __exit ltt_ring_buffer_client_exit(void)
7514523f
MD
494{
495 printk(KERN_INFO "LTT : ltt ring buffer client exit\n");
496 ltt_transport_unregister(&ltt_relay_transport);
497}
498
1c25284c
MD
499module_exit(ltt_ring_buffer_client_exit);
500
7514523f
MD
501MODULE_LICENSE("GPL and additional rights");
502MODULE_AUTHOR("Mathieu Desnoyers");
3d084699
MD
503MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
504 " client");
This page took 0.04718 seconds and 4 git commands to generate.