2 * ltt-ring-buffer-client.h
4 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng lib ring buffer client template.
8 * Dual LGPL v2.1/GPL v2 license.
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"
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.
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.
35 struct packet_header
{
36 /* Trace packet header */
39 * contains endianness information.
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.
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 */
61 static inline notrace u64
lib_ring_buffer_clock_read(struct channel
*chan
)
63 return trace_clock_read64();
67 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
)
70 size_t orig_offset
= offset
;
74 for (i
= 0; i
< ctx
->nr_fields
; i
++)
75 offset
+= ctx
->fields
[i
].get_size(offset
);
76 return offset
- orig_offset
;
80 void ctx_record(struct lib_ring_buffer_ctx
*bufctx
,
81 struct ltt_channel
*chan
,
82 struct lttng_ctx
*ctx
)
88 for (i
= 0; i
< ctx
->nr_fields
; i
++)
89 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
93 * record_header_size - Calculate the header size and padding necessary.
94 * @config: ring buffer instance configuration
96 * @offset: offset in the write buffer
97 * @pre_header_padding: padding to add before the header (output)
98 * @ctx: reservation context
100 * Returns the event header size (including padding).
102 * The payload must itself determine its own alignment from the biggest type it
106 unsigned 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
)
111 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
112 struct ltt_event
*event
= ctx
->priv
;
113 size_t orig_offset
= offset
;
116 switch (ltt_chan
->header_type
) {
117 case 1: /* compact */
118 padding
= lib_ring_buffer_align(offset
, ltt_alignof(uint32_t));
120 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
121 offset
+= sizeof(uint32_t); /* id and timestamp */
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 */
133 padding
= lib_ring_buffer_align(offset
, ltt_alignof(uint16_t));
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 */
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 */
151 offset
+= ctx_get_size(offset
, event
->ctx
);
152 offset
+= ctx_get_size(offset
, ltt_chan
->ctx
);
154 *pre_header_padding
= padding
;
155 return offset
- orig_offset
;
158 #include "wrapper/ringbuffer/api.h"
161 void ltt_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
162 struct lib_ring_buffer_ctx
*ctx
,
166 * ltt_write_event_header
168 * Writes the event header to the offset (already aligned on 32-bits).
170 * @config: ring buffer instance configuration
171 * @ctx: reservation context
172 * @event_id: event ID
175 void ltt_write_event_header(const struct lib_ring_buffer_config
*config
,
176 struct lib_ring_buffer_ctx
*ctx
,
179 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
180 struct ltt_event
*event
= ctx
->priv
;
182 if (unlikely(ctx
->rflags
))
185 switch (ltt_chan
->header_type
) {
186 case 1: /* compact */
188 uint32_t id_time
= 0;
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
));
197 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
198 uint16_t id
= event_id
;
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
, ×tamp
, sizeof(timestamp
));
209 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
210 ctx_record(ctx
, ltt_chan
, event
->ctx
);
211 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
216 ltt_write_event_header_slow(config
, ctx
, event_id
);
220 void ltt_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
221 struct lib_ring_buffer_ctx
*ctx
,
224 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
225 struct ltt_event
*event
= ctx
->priv
;
227 switch (ltt_chan
->header_type
) {
228 case 1: /* compact */
229 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
230 uint32_t id_time
= 0;
232 bt_bitfield_write(&id_time
, uint32_t, 0, 5, event_id
);
233 bt_bitfield_write(&id_time
, uint32_t, 5, 27, ctx
->tsc
);
234 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
237 uint64_t timestamp
= ctx
->tsc
;
239 bt_bitfield_write(&id
, uint8_t, 0, 5, 31);
240 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
241 /* Align extended struct on largest member */
242 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
243 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
244 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
245 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
250 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
251 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
252 uint16_t id
= event_id
;
254 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
255 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint32_t));
256 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
259 uint64_t timestamp
= ctx
->tsc
;
261 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
262 /* Align extended struct on largest member */
263 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
264 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
265 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
266 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
273 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
274 ctx_record(ctx
, ltt_chan
, event
->ctx
);
275 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
278 static const struct lib_ring_buffer_config client_config
;
280 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
282 return lib_ring_buffer_clock_read(chan
);
286 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
287 struct channel
*chan
, size_t offset
,
288 size_t *pre_header_padding
,
289 struct lib_ring_buffer_ctx
*ctx
)
291 return record_header_size(config
, chan
, offset
,
292 pre_header_padding
, ctx
);
296 * client_packet_header_size - called on buffer-switch to a new sub-buffer
298 * Return header size without padding after the structure. Don't use packed
299 * structure because gcc generates inefficient code on some architectures
302 static size_t client_packet_header_size(void)
304 return offsetof(struct packet_header
, ctx
.header_end
);
307 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
308 unsigned int subbuf_idx
)
310 struct channel
*chan
= buf
->backend
.chan
;
311 struct packet_header
*header
=
312 (struct packet_header
*)
313 lib_ring_buffer_offset_address(&buf
->backend
,
314 subbuf_idx
* chan
->backend
.subbuf_size
);
315 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
316 struct ltt_session
*session
= ltt_chan
->session
;
318 header
->magic
= CTF_MAGIC_NUMBER
;
319 memcpy(header
->uuid
, session
->uuid
.b
, sizeof(session
->uuid
));
320 header
->stream_id
= ltt_chan
->id
;
321 header
->ctx
.timestamp_begin
= tsc
;
322 header
->ctx
.timestamp_end
= 0;
323 header
->ctx
.events_discarded
= 0;
324 header
->ctx
.content_size
= 0xFFFFFFFF; /* for debugging */
325 header
->ctx
.packet_size
= 0xFFFFFFFF;
326 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
330 * offset is assumed to never be 0 here : never deliver a completely empty
331 * subbuffer. data_size is between 1 and subbuf_size.
333 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
334 unsigned int subbuf_idx
, unsigned long data_size
)
336 struct channel
*chan
= buf
->backend
.chan
;
337 struct packet_header
*header
=
338 (struct packet_header
*)
339 lib_ring_buffer_offset_address(&buf
->backend
,
340 subbuf_idx
* chan
->backend
.subbuf_size
);
341 unsigned long records_lost
= 0;
343 header
->ctx
.timestamp_end
= tsc
;
344 header
->ctx
.content_size
= data_size
* CHAR_BIT
; /* in bits */
345 header
->ctx
.packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
346 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
347 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
348 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
349 header
->ctx
.events_discarded
= records_lost
;
352 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
353 int cpu
, const char *name
)
358 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
362 static const struct lib_ring_buffer_config client_config
= {
363 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
364 .cb
.record_header_size
= client_record_header_size
,
365 .cb
.subbuffer_header_size
= client_packet_header_size
,
366 .cb
.buffer_begin
= client_buffer_begin
,
367 .cb
.buffer_end
= client_buffer_end
,
368 .cb
.buffer_create
= client_buffer_create
,
369 .cb
.buffer_finalize
= client_buffer_finalize
,
372 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
373 .sync
= RING_BUFFER_SYNC_PER_CPU
,
374 .mode
= RING_BUFFER_MODE_TEMPLATE
,
375 .backend
= RING_BUFFER_PAGE
,
376 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
377 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
378 .ipi
= RING_BUFFER_IPI_BARRIER
,
379 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
383 struct channel
*_channel_create(const char *name
,
384 struct ltt_channel
*ltt_chan
, void *buf_addr
,
385 size_t subbuf_size
, size_t num_subbuf
,
386 unsigned int switch_timer_interval
,
387 unsigned int read_timer_interval
)
389 return channel_create(&client_config
, name
, ltt_chan
, buf_addr
,
390 subbuf_size
, num_subbuf
, switch_timer_interval
,
391 read_timer_interval
);
395 void ltt_channel_destroy(struct channel
*chan
)
397 channel_destroy(chan
);
401 struct lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
)
403 struct lib_ring_buffer
*buf
;
406 for_each_channel_cpu(cpu
, chan
) {
407 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
408 if (!lib_ring_buffer_open_read(buf
))
415 int ltt_buffer_has_read_closed_stream(struct channel
*chan
)
417 struct lib_ring_buffer
*buf
;
420 for_each_channel_cpu(cpu
, chan
) {
421 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
422 if (!atomic_long_read(&buf
->active_readers
))
429 void ltt_buffer_read_close(struct lib_ring_buffer
*buf
)
431 lib_ring_buffer_release_read(buf
);
435 int ltt_event_reserve(struct lib_ring_buffer_ctx
*ctx
,
438 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
441 cpu
= lib_ring_buffer_get_cpu(&client_config
);
446 switch (ltt_chan
->header_type
) {
447 case 1: /* compact */
449 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
452 if (event_id
> 65534)
453 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
459 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
462 ltt_write_event_header(&client_config
, ctx
, event_id
);
465 lib_ring_buffer_put_cpu(&client_config
);
470 void ltt_event_commit(struct lib_ring_buffer_ctx
*ctx
)
472 lib_ring_buffer_commit(&client_config
, ctx
);
473 lib_ring_buffer_put_cpu(&client_config
);
477 void ltt_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
480 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
484 wait_queue_head_t
*ltt_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
486 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
488 return &buf
->write_wait
;
492 wait_queue_head_t
*ltt_get_hp_wait_queue(struct channel
*chan
)
494 return &chan
->hp_wait
;
498 int ltt_is_finalized(struct channel
*chan
)
500 return lib_ring_buffer_channel_is_finalized(chan
);
504 int ltt_is_disabled(struct channel
*chan
)
506 return lib_ring_buffer_channel_is_disabled(chan
);
509 static struct ltt_transport ltt_relay_transport
= {
510 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
511 .owner
= THIS_MODULE
,
513 .channel_create
= _channel_create
,
514 .channel_destroy
= ltt_channel_destroy
,
515 .buffer_read_open
= ltt_buffer_read_open
,
516 .buffer_has_read_closed_stream
=
517 ltt_buffer_has_read_closed_stream
,
518 .buffer_read_close
= ltt_buffer_read_close
,
519 .event_reserve
= ltt_event_reserve
,
520 .event_commit
= ltt_event_commit
,
521 .event_write
= ltt_event_write
,
522 .packet_avail_size
= NULL
, /* Would be racy anyway */
523 .get_writer_buf_wait_queue
= ltt_get_writer_buf_wait_queue
,
524 .get_hp_wait_queue
= ltt_get_hp_wait_queue
,
525 .is_finalized
= ltt_is_finalized
,
526 .is_disabled
= ltt_is_disabled
,
530 static int __init
ltt_ring_buffer_client_init(void)
533 * This vmalloc sync all also takes care of the lib ring buffer
534 * vmalloc'd module pages when it is built as a module into LTTng.
536 wrapper_vmalloc_sync_all();
537 ltt_transport_register(<t_relay_transport
);
541 module_init(ltt_ring_buffer_client_init
);
543 static void __exit
ltt_ring_buffer_client_exit(void)
545 ltt_transport_unregister(<t_relay_transport
);
548 module_exit(ltt_ring_buffer_client_exit
);
550 MODULE_LICENSE("GPL and additional rights");
551 MODULE_AUTHOR("Mathieu Desnoyers");
552 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING