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.
12 #include <lttng/ust-events.h>
13 #include "lttng/bitfield.h"
15 #include "ltt-tracer.h"
16 #include "../libringbuffer/frontend_types.h"
18 #define LTTNG_COMPACT_EVENT_BITS 5
19 #define LTTNG_COMPACT_TSC_BITS 27
22 * Keep the natural field alignment for _each field_ within this structure if
23 * you ever add/remove a field from this header. Packed attribute is not used
24 * because gcc generates poor code on at least powerpc and mips. Don't ever
25 * let gcc add padding between the structure elements.
28 struct packet_header
{
29 /* Trace packet header */
32 * contains endianness information.
38 /* Stream packet context */
39 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
40 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
41 uint32_t events_discarded
; /*
42 * Events lost in this subbuffer since
43 * the beginning of the trace.
46 uint32_t content_size
; /* Size of data in subbuffer */
47 uint32_t packet_size
; /* Subbuffer size (include padding) */
48 uint32_t cpu_id
; /* CPU id associated with stream */
49 uint8_t header_end
; /* End of header */
54 static inline uint64_t lib_ring_buffer_clock_read(struct channel
*chan
)
56 return trace_clock_read64();
60 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
)
63 size_t orig_offset
= offset
;
67 for (i
= 0; i
< ctx
->nr_fields
; i
++)
68 offset
+= ctx
->fields
[i
].get_size(offset
);
69 return offset
- orig_offset
;
73 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
74 struct ltt_channel
*chan
,
75 struct lttng_ctx
*ctx
)
81 for (i
= 0; i
< ctx
->nr_fields
; i
++)
82 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
86 * record_header_size - Calculate the header size and padding necessary.
87 * @config: ring buffer instance configuration
89 * @offset: offset in the write buffer
90 * @pre_header_padding: padding to add before the header (output)
91 * @ctx: reservation context
93 * Returns the event header size (including padding).
95 * The payload must itself determine its own alignment from the biggest type it
99 unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
100 struct channel
*chan
, size_t offset
,
101 size_t *pre_header_padding
,
102 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
104 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
105 struct ltt_event
*event
= ctx
->priv
;
106 size_t orig_offset
= offset
;
109 switch (ltt_chan
->header_type
) {
110 case 1: /* compact */
111 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
113 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
114 offset
+= sizeof(uint32_t); /* id and timestamp */
116 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
117 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
118 /* Align extended struct on largest member */
119 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
120 offset
+= sizeof(uint32_t); /* id */
121 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
122 offset
+= sizeof(uint64_t); /* timestamp */
126 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
128 offset
+= sizeof(uint16_t);
129 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
130 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
131 offset
+= sizeof(uint32_t); /* timestamp */
133 /* Align extended struct on largest member */
134 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
135 offset
+= sizeof(uint32_t); /* id */
136 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
137 offset
+= sizeof(uint64_t); /* timestamp */
144 offset
+= ctx_get_size(offset
, event
->ctx
);
145 offset
+= ctx_get_size(offset
, ltt_chan
->ctx
);
147 *pre_header_padding
= padding
;
148 return offset
- orig_offset
;
151 #include "../libringbuffer/api.h"
154 void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
155 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
159 * ltt_write_event_header
161 * Writes the event header to the offset (already aligned on 32-bits).
163 * @config: ring buffer instance configuration
164 * @ctx: reservation context
165 * @event_id: event ID
168 void ltt_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
169 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
172 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
173 struct ltt_event
*event
= ctx
->priv
;
175 if (caa_unlikely(ctx
->rflags
))
178 switch (ltt_chan
->header_type
) {
179 case 1: /* compact */
181 uint32_t id_time
= 0;
183 bt_bitfield_write(&id_time
, uint32_t,
185 LTTNG_COMPACT_EVENT_BITS
,
187 bt_bitfield_write(&id_time
, uint32_t,
188 LTTNG_COMPACT_EVENT_BITS
,
189 LTTNG_COMPACT_TSC_BITS
,
191 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
196 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
197 uint16_t id
= event_id
;
199 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
200 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
201 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
208 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
209 ctx_record(ctx
, ltt_chan
, event
->ctx
);
210 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
215 ltt_write_event_header_slow(config
, ctx
, event_id
);
219 void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
220 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
223 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
224 struct ltt_event
*event
= ctx
->priv
;
226 switch (ltt_chan
->header_type
) {
227 case 1: /* compact */
228 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
229 uint32_t id_time
= 0;
231 bt_bitfield_write(&id_time
, uint32_t,
233 LTTNG_COMPACT_EVENT_BITS
,
235 bt_bitfield_write(&id_time
, uint32_t,
236 LTTNG_COMPACT_EVENT_BITS
,
237 LTTNG_COMPACT_TSC_BITS
,
239 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
242 uint64_t timestamp
= ctx
->tsc
;
244 bt_bitfield_write(&id
, uint8_t,
246 LTTNG_COMPACT_EVENT_BITS
,
248 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
249 /* Align extended struct on largest member */
250 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
251 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
252 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
253 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
258 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
259 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
260 uint16_t id
= event_id
;
262 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
263 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
264 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
267 uint64_t timestamp
= ctx
->tsc
;
269 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
270 /* Align extended struct on largest member */
271 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
272 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
273 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
274 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
281 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
282 ctx_record(ctx
, ltt_chan
, event
->ctx
);
283 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
286 static const struct lttng_ust_lib_ring_buffer_config client_config
;
288 static uint64_t client_ring_buffer_clock_read(struct channel
*chan
)
290 return lib_ring_buffer_clock_read(chan
);
294 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
295 struct channel
*chan
, size_t offset
,
296 size_t *pre_header_padding
,
297 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
299 return record_header_size(config
, chan
, offset
,
300 pre_header_padding
, ctx
);
304 * client_packet_header_size - called on buffer-switch to a new sub-buffer
306 * Return header size without padding after the structure. Don't use packed
307 * structure because gcc generates inefficient code on some architectures
310 static size_t client_packet_header_size(void)
312 return offsetof(struct packet_header
, ctx
.header_end
);
315 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
316 unsigned int subbuf_idx
,
317 struct lttng_ust_shm_handle
*handle
)
319 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
320 struct packet_header
*header
=
321 (struct packet_header
*)
322 lib_ring_buffer_offset_address(&buf
->backend
,
323 subbuf_idx
* chan
->backend
.subbuf_size
,
325 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
327 header
->magic
= CTF_MAGIC_NUMBER
;
328 memcpy(header
->uuid
, ltt_chan
->uuid
, sizeof(ltt_chan
->uuid
));
329 header
->stream_id
= ltt_chan
->id
;
330 header
->ctx
.timestamp_begin
= tsc
;
331 header
->ctx
.timestamp_end
= 0;
332 header
->ctx
.events_discarded
= 0;
333 header
->ctx
.content_size
= 0xFFFFFFFF; /* for debugging */
334 header
->ctx
.packet_size
= 0xFFFFFFFF;
335 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
339 * offset is assumed to never be 0 here : never deliver a completely empty
340 * subbuffer. data_size is between 1 and subbuf_size.
342 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
343 unsigned int subbuf_idx
, unsigned long data_size
,
344 struct lttng_ust_shm_handle
*handle
)
346 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
347 struct packet_header
*header
=
348 (struct packet_header
*)
349 lib_ring_buffer_offset_address(&buf
->backend
,
350 subbuf_idx
* chan
->backend
.subbuf_size
,
352 unsigned long records_lost
= 0;
354 header
->ctx
.timestamp_end
= tsc
;
355 header
->ctx
.content_size
= data_size
* CHAR_BIT
; /* in bits */
356 header
->ctx
.packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
358 * We do not care about the records lost count, because the metadata
359 * channel waits and retry.
361 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
362 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
363 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
364 header
->ctx
.events_discarded
= records_lost
;
367 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
368 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
373 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
377 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
378 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
379 .cb
.record_header_size
= client_record_header_size
,
380 .cb
.subbuffer_header_size
= client_packet_header_size
,
381 .cb
.buffer_begin
= client_buffer_begin
,
382 .cb
.buffer_end
= client_buffer_end
,
383 .cb
.buffer_create
= client_buffer_create
,
384 .cb
.buffer_finalize
= client_buffer_finalize
,
386 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
387 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
388 .sync
= RING_BUFFER_SYNC_GLOBAL
,
389 .mode
= RING_BUFFER_MODE_TEMPLATE
,
390 .backend
= RING_BUFFER_PAGE
,
391 .output
= RING_BUFFER_MMAP
,
392 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
393 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
394 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
395 .client_type
= LTTNG_CLIENT_TYPE
,
398 const struct lttng_ust_lib_ring_buffer_client_cb
*LTTNG_CLIENT_CALLBACKS
= &client_config
.cb
;
401 struct ltt_channel
*_channel_create(const char *name
,
403 size_t subbuf_size
, size_t num_subbuf
,
404 unsigned int switch_timer_interval
,
405 unsigned int read_timer_interval
,
406 int **shm_fd
, int **wait_fd
,
407 uint64_t **memory_map_size
,
408 struct ltt_channel
*chan_priv_init
)
411 struct ltt_channel
*ltt_chan
= NULL
;
412 struct lttng_ust_shm_handle
*handle
;
414 handle
= channel_create(&client_config
, name
,
415 &priv
, __alignof__(*ltt_chan
), sizeof(*ltt_chan
),
417 buf_addr
, subbuf_size
, num_subbuf
,
418 switch_timer_interval
, read_timer_interval
,
419 shm_fd
, wait_fd
, memory_map_size
);
423 ltt_chan
->handle
= handle
;
424 ltt_chan
->chan
= shmp(ltt_chan
->handle
, ltt_chan
->handle
->chan
);
429 void ltt_channel_destroy(struct ltt_channel
*ltt_chan
)
431 channel_destroy(ltt_chan
->chan
, ltt_chan
->handle
, 0);
435 struct lttng_ust_lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
,
436 struct lttng_ust_shm_handle
*handle
,
437 int **shm_fd
, int **wait_fd
,
438 uint64_t **memory_map_size
)
440 struct lttng_ust_lib_ring_buffer
*buf
;
443 for_each_channel_cpu(cpu
, chan
) {
444 buf
= channel_get_ring_buffer(&client_config
, chan
,
445 cpu
, handle
, shm_fd
, wait_fd
,
447 if (!lib_ring_buffer_open_read(buf
, handle
, 0))
454 void ltt_buffer_read_close(struct lttng_ust_lib_ring_buffer
*buf
,
455 struct lttng_ust_shm_handle
*handle
)
457 lib_ring_buffer_release_read(buf
, handle
, 0);
461 int ltt_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
464 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
467 cpu
= lib_ring_buffer_get_cpu(&client_config
);
472 switch (ltt_chan
->header_type
) {
473 case 1: /* compact */
475 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
478 if (event_id
> 65534)
479 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
485 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
488 ltt_write_event_header(&client_config
, ctx
, event_id
);
491 lib_ring_buffer_put_cpu(&client_config
);
496 void ltt_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
498 lib_ring_buffer_commit(&client_config
, ctx
);
499 lib_ring_buffer_put_cpu(&client_config
);
503 void ltt_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
506 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
511 wait_queue_head_t
*ltt_get_reader_wait_queue(struct channel
*chan
)
513 return &chan
->read_wait
;
517 wait_queue_head_t
*ltt_get_hp_wait_queue(struct channel
*chan
)
519 return &chan
->hp_wait
;
524 int ltt_is_finalized(struct channel
*chan
)
526 return lib_ring_buffer_channel_is_finalized(chan
);
530 int ltt_is_disabled(struct channel
*chan
)
532 return lib_ring_buffer_channel_is_disabled(chan
);
536 int ltt_flush_buffer(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
538 struct lttng_ust_lib_ring_buffer
*buf
;
541 for_each_channel_cpu(cpu
, chan
) {
542 int *shm_fd
, *wait_fd
;
543 uint64_t *memory_map_size
;
545 buf
= channel_get_ring_buffer(&client_config
, chan
,
546 cpu
, handle
, &shm_fd
, &wait_fd
,
548 lib_ring_buffer_switch(&client_config
, buf
,
549 SWITCH_ACTIVE
, handle
);
554 static struct ltt_transport ltt_relay_transport
= {
555 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
557 .channel_create
= _channel_create
,
558 .channel_destroy
= ltt_channel_destroy
,
559 .buffer_read_open
= ltt_buffer_read_open
,
560 .buffer_read_close
= ltt_buffer_read_close
,
561 .event_reserve
= ltt_event_reserve
,
562 .event_commit
= ltt_event_commit
,
563 .event_write
= ltt_event_write
,
564 .packet_avail_size
= NULL
, /* Would be racy anyway */
565 //.get_reader_wait_queue = ltt_get_reader_wait_queue,
566 //.get_hp_wait_queue = ltt_get_hp_wait_queue,
567 .is_finalized
= ltt_is_finalized
,
568 .is_disabled
= ltt_is_disabled
,
569 .flush_buffer
= ltt_flush_buffer
,
573 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
575 DBG("LTT : ltt ring buffer client init\n");
576 ltt_transport_register(<t_relay_transport
);
579 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
581 DBG("LTT : ltt ring buffer client exit\n");
582 ltt_transport_unregister(<t_relay_transport
);