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.
27 struct packet_header
{
28 /* Trace packet header */
31 * contains endianness information.
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.
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 */
53 static inline notrace u64
lib_ring_buffer_clock_read(struct channel
*chan
)
55 return trace_clock_read64();
59 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
)
62 size_t orig_offset
= offset
;
66 for (i
= 0; i
< ctx
->nr_fields
; i
++)
67 offset
+= ctx
->fields
[i
].get_size(offset
);
68 return offset
- orig_offset
;
72 void ctx_record(struct lib_ring_buffer_ctx
*bufctx
,
73 struct ltt_channel
*chan
,
74 struct lttng_ctx
*ctx
)
80 for (i
= 0; i
< ctx
->nr_fields
; i
++)
81 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
85 * record_header_size - Calculate the header size and padding necessary.
86 * @config: ring buffer instance configuration
88 * @offset: offset in the write buffer
89 * @pre_header_padding: padding to add before the header (output)
90 * @ctx: reservation context
92 * Returns the event header size (including padding).
94 * The payload must itself determine its own alignment from the biggest type it
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
)
103 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
104 struct ltt_event
*event
= ctx
->priv
;
105 size_t orig_offset
= offset
;
108 switch (ltt_chan
->header_type
) {
109 case 1: /* compact */
110 padding
= lib_ring_buffer_align(offset
, ltt_alignof(uint32_t));
112 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
113 offset
+= sizeof(uint32_t); /* id and timestamp */
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 */
125 padding
= lib_ring_buffer_align(offset
, ltt_alignof(uint16_t));
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 */
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 */
143 offset
+= ctx_get_size(offset
, event
->ctx
);
144 offset
+= ctx_get_size(offset
, ltt_chan
->ctx
);
146 *pre_header_padding
= padding
;
147 return offset
- orig_offset
;
150 #include "wrapper/ringbuffer/api.h"
153 void ltt_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
154 struct lib_ring_buffer_ctx
*ctx
,
158 * ltt_write_event_header
160 * Writes the event header to the offset (already aligned on 32-bits).
162 * @config: ring buffer instance configuration
163 * @ctx: reservation context
164 * @event_id: event ID
167 void ltt_write_event_header(const struct lib_ring_buffer_config
*config
,
168 struct lib_ring_buffer_ctx
*ctx
,
171 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
172 struct ltt_event
*event
= ctx
->priv
;
174 if (unlikely(ctx
->rflags
))
177 switch (ltt_chan
->header_type
) {
178 case 1: /* compact */
180 uint32_t id_time
= 0;
182 bt_bitfield_write(&id_time
, uint32_t, 0, 5, event_id
);
183 bt_bitfield_write(&id_time
, uint32_t, 5, 27, ctx
->tsc
);
184 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
189 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
190 uint16_t id
= event_id
;
192 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
193 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint32_t));
194 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
201 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
202 ctx_record(ctx
, ltt_chan
, event
->ctx
);
207 ltt_write_event_header_slow(config
, ctx
, event_id
);
211 void ltt_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
212 struct lib_ring_buffer_ctx
*ctx
,
215 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
216 struct ltt_event
*event
= ctx
->priv
;
218 switch (ltt_chan
->header_type
) {
219 case 1: /* compact */
220 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
221 uint32_t id_time
= 0;
223 bt_bitfield_write(&id_time
, uint32_t, 0, 5, event_id
);
224 bt_bitfield_write(&id_time
, uint32_t, 5, 27, ctx
->tsc
);
225 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
228 uint64_t timestamp
= ctx
->tsc
;
230 bt_bitfield_write(&id
, uint8_t, 0, 5, 31);
231 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
232 /* Align extended struct on largest member */
233 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
234 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
235 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
236 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
241 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
242 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
243 uint16_t id
= event_id
;
245 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
246 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint32_t));
247 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
250 uint64_t timestamp
= ctx
->tsc
;
252 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
253 /* Align extended struct on largest member */
254 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
255 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
256 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(uint64_t));
257 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
264 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
265 ctx_record(ctx
, ltt_chan
, event
->ctx
);
268 static const struct lib_ring_buffer_config client_config
;
270 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
272 return lib_ring_buffer_clock_read(chan
);
276 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
277 struct channel
*chan
, size_t offset
,
278 size_t *pre_header_padding
,
279 struct lib_ring_buffer_ctx
*ctx
)
281 return record_header_size(config
, chan
, offset
,
282 pre_header_padding
, ctx
);
286 * client_packet_header_size - called on buffer-switch to a new sub-buffer
288 * Return header size without padding after the structure. Don't use packed
289 * structure because gcc generates inefficient code on some architectures
292 static size_t client_packet_header_size(void)
294 return offsetof(struct packet_header
, ctx
.header_end
);
297 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
298 unsigned int subbuf_idx
)
300 struct channel
*chan
= buf
->backend
.chan
;
301 struct packet_header
*header
=
302 (struct packet_header
*)
303 lib_ring_buffer_offset_address(&buf
->backend
,
304 subbuf_idx
* chan
->backend
.subbuf_size
);
305 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
306 struct ltt_session
*session
= ltt_chan
->session
;
308 header
->magic
= CTF_MAGIC_NUMBER
;
309 memcpy(header
->uuid
, session
->uuid
.b
, sizeof(session
->uuid
));
310 header
->stream_id
= ltt_chan
->id
;
311 header
->ctx
.timestamp_begin
= tsc
;
312 header
->ctx
.timestamp_end
= 0;
313 header
->ctx
.events_discarded
= 0;
314 header
->ctx
.content_size
= 0xFFFFFFFF; /* for debugging */
315 header
->ctx
.packet_size
= 0xFFFFFFFF;
316 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
320 * offset is assumed to never be 0 here : never deliver a completely empty
321 * subbuffer. data_size is between 1 and subbuf_size.
323 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
324 unsigned int subbuf_idx
, unsigned long data_size
)
326 struct channel
*chan
= buf
->backend
.chan
;
327 struct packet_header
*header
=
328 (struct packet_header
*)
329 lib_ring_buffer_offset_address(&buf
->backend
,
330 subbuf_idx
* chan
->backend
.subbuf_size
);
331 unsigned long records_lost
= 0;
333 header
->ctx
.timestamp_end
= tsc
;
334 header
->ctx
.content_size
= data_size
* CHAR_BIT
; /* in bits */
335 header
->ctx
.packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
336 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
337 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
338 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
339 header
->ctx
.events_discarded
= records_lost
;
342 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
343 int cpu
, const char *name
)
348 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
352 static const struct lib_ring_buffer_config client_config
= {
353 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
354 .cb
.record_header_size
= client_record_header_size
,
355 .cb
.subbuffer_header_size
= client_packet_header_size
,
356 .cb
.buffer_begin
= client_buffer_begin
,
357 .cb
.buffer_end
= client_buffer_end
,
358 .cb
.buffer_create
= client_buffer_create
,
359 .cb
.buffer_finalize
= client_buffer_finalize
,
362 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
363 .sync
= RING_BUFFER_SYNC_PER_CPU
,
364 .mode
= RING_BUFFER_MODE_TEMPLATE
,
365 .backend
= RING_BUFFER_PAGE
,
366 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
367 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
368 .ipi
= RING_BUFFER_IPI_BARRIER
,
369 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
373 struct channel
*_channel_create(const char *name
,
374 struct ltt_channel
*ltt_chan
, void *buf_addr
,
375 size_t subbuf_size
, size_t num_subbuf
,
376 unsigned int switch_timer_interval
,
377 unsigned int read_timer_interval
)
379 return channel_create(&client_config
, name
, ltt_chan
, buf_addr
,
380 subbuf_size
, num_subbuf
, switch_timer_interval
,
381 read_timer_interval
);
385 void ltt_channel_destroy(struct channel
*chan
)
387 channel_destroy(chan
);
391 struct lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
)
393 struct lib_ring_buffer
*buf
;
396 for_each_channel_cpu(cpu
, chan
) {
397 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
398 if (!lib_ring_buffer_open_read(buf
))
405 int ltt_buffer_has_read_closed_stream(struct channel
*chan
)
407 struct lib_ring_buffer
*buf
;
410 for_each_channel_cpu(cpu
, chan
) {
411 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
412 if (!atomic_long_read(&buf
->active_readers
))
419 void ltt_buffer_read_close(struct lib_ring_buffer
*buf
)
421 lib_ring_buffer_release_read(buf
);
425 int ltt_event_reserve(struct lib_ring_buffer_ctx
*ctx
,
428 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
431 cpu
= lib_ring_buffer_get_cpu(&client_config
);
436 switch (ltt_chan
->header_type
) {
437 case 1: /* compact */
439 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
442 if (event_id
> 65534)
443 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
449 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
452 ltt_write_event_header(&client_config
, ctx
, event_id
);
455 lib_ring_buffer_put_cpu(&client_config
);
460 void ltt_event_commit(struct lib_ring_buffer_ctx
*ctx
)
462 lib_ring_buffer_commit(&client_config
, ctx
);
463 lib_ring_buffer_put_cpu(&client_config
);
467 void ltt_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
470 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
474 wait_queue_head_t
*ltt_get_reader_wait_queue(struct channel
*chan
)
476 return &chan
->read_wait
;
480 wait_queue_head_t
*ltt_get_hp_wait_queue(struct channel
*chan
)
482 return &chan
->hp_wait
;
486 int ltt_is_finalized(struct channel
*chan
)
488 return lib_ring_buffer_channel_is_finalized(chan
);
492 int ltt_is_disabled(struct channel
*chan
)
494 return lib_ring_buffer_channel_is_disabled(chan
);
497 static struct ltt_transport ltt_relay_transport
= {
498 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
499 .owner
= THIS_MODULE
,
501 .channel_create
= _channel_create
,
502 .channel_destroy
= ltt_channel_destroy
,
503 .buffer_read_open
= ltt_buffer_read_open
,
504 .buffer_has_read_closed_stream
=
505 ltt_buffer_has_read_closed_stream
,
506 .buffer_read_close
= ltt_buffer_read_close
,
507 .event_reserve
= ltt_event_reserve
,
508 .event_commit
= ltt_event_commit
,
509 .event_write
= ltt_event_write
,
510 .packet_avail_size
= NULL
, /* Would be racy anyway */
511 .get_reader_wait_queue
= ltt_get_reader_wait_queue
,
512 .get_hp_wait_queue
= ltt_get_hp_wait_queue
,
513 .is_finalized
= ltt_is_finalized
,
514 .is_disabled
= ltt_is_disabled
,
518 static int __init
ltt_ring_buffer_client_init(void)
521 * This vmalloc sync all also takes care of the lib ring buffer
522 * vmalloc'd module pages when it is built as a module into LTTng.
524 wrapper_vmalloc_sync_all();
525 ltt_transport_register(<t_relay_transport
);
529 module_init(ltt_ring_buffer_client_init
);
531 static void __exit
ltt_ring_buffer_client_exit(void)
533 ltt_transport_unregister(<t_relay_transport
);
536 module_exit(ltt_ring_buffer_client_exit
);
538 MODULE_LICENSE("GPL and additional rights");
539 MODULE_AUTHOR("Mathieu Desnoyers");
540 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING