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"
19 * Keep the natural field alignment for _each field_ within this structure if
20 * you ever add/remove a field from this header. Packed attribute is not used
21 * because gcc generates poor code on at least powerpc and mips. Don't ever
22 * let gcc add padding between the structure elements.
25 struct packet_header
{
26 /* Trace packet header */
29 * contains endianness information.
35 /* Stream packet context */
36 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
37 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
38 uint32_t events_discarded
; /*
39 * Events lost in this subbuffer since
40 * the beginning of the trace.
43 uint32_t content_size
; /* Size of data in subbuffer */
44 uint32_t packet_size
; /* Subbuffer size (include padding) */
45 uint32_t cpu_id
; /* CPU id associated with stream */
46 uint8_t header_end
; /* End of header */
51 static inline uint64_t lib_ring_buffer_clock_read(struct channel
*chan
)
53 return trace_clock_read64();
57 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
)
60 size_t orig_offset
= offset
;
64 for (i
= 0; i
< ctx
->nr_fields
; i
++)
65 offset
+= ctx
->fields
[i
].get_size(offset
);
66 return offset
- orig_offset
;
70 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
71 struct ltt_channel
*chan
,
72 struct lttng_ctx
*ctx
)
78 for (i
= 0; i
< ctx
->nr_fields
; i
++)
79 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
83 * record_header_size - Calculate the header size and padding necessary.
84 * @config: ring buffer instance configuration
86 * @offset: offset in the write buffer
87 * @pre_header_padding: padding to add before the header (output)
88 * @ctx: reservation context
90 * Returns the event header size (including padding).
92 * The payload must itself determine its own alignment from the biggest type it
96 unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
97 struct channel
*chan
, size_t offset
,
98 size_t *pre_header_padding
,
99 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
101 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
102 struct ltt_event
*event
= ctx
->priv
;
103 size_t orig_offset
= offset
;
106 switch (ltt_chan
->header_type
) {
107 case 1: /* compact */
108 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
110 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
111 offset
+= sizeof(uint32_t); /* id and timestamp */
113 /* Minimum space taken by 5-bit id */
114 offset
+= sizeof(uint8_t);
115 /* Align extended struct on largest member */
116 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
117 offset
+= sizeof(uint32_t); /* id */
118 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
119 offset
+= sizeof(uint64_t); /* timestamp */
123 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
125 offset
+= sizeof(uint16_t);
126 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
127 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
128 offset
+= sizeof(uint32_t); /* timestamp */
130 /* Align extended struct on largest member */
131 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
132 offset
+= sizeof(uint32_t); /* id */
133 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
134 offset
+= sizeof(uint64_t); /* timestamp */
141 offset
+= ctx_get_size(offset
, event
->ctx
);
142 offset
+= ctx_get_size(offset
, ltt_chan
->ctx
);
144 *pre_header_padding
= padding
;
145 return offset
- orig_offset
;
148 #include "../libringbuffer/api.h"
151 void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
152 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
156 * ltt_write_event_header
158 * Writes the event header to the offset (already aligned on 32-bits).
160 * @config: ring buffer instance configuration
161 * @ctx: reservation context
162 * @event_id: event ID
165 void ltt_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
166 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
169 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
170 struct ltt_event
*event
= ctx
->priv
;
172 if (caa_unlikely(ctx
->rflags
))
175 switch (ltt_chan
->header_type
) {
176 case 1: /* compact */
178 uint32_t id_time
= 0;
180 bt_bitfield_write(&id_time
, uint32_t, 0, 5, event_id
);
181 bt_bitfield_write(&id_time
, uint32_t, 5, 27, ctx
->tsc
);
182 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
187 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
188 uint16_t id
= event_id
;
190 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
191 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
192 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
199 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
200 ctx_record(ctx
, ltt_chan
, event
->ctx
);
201 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
206 ltt_write_event_header_slow(config
, ctx
, event_id
);
210 void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
211 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
214 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
215 struct ltt_event
*event
= ctx
->priv
;
217 switch (ltt_chan
->header_type
) {
218 case 1: /* compact */
219 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
220 uint32_t id_time
= 0;
222 bt_bitfield_write(&id_time
, uint32_t, 0, 5, event_id
);
223 bt_bitfield_write(&id_time
, uint32_t, 5, 27, ctx
->tsc
);
224 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
227 uint64_t timestamp
= ctx
->tsc
;
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
, lttng_alignof(uint64_t));
233 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
234 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
235 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
240 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTT_RFLAG_EXTENDED
))) {
241 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
242 uint16_t id
= event_id
;
244 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
245 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
246 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
249 uint64_t timestamp
= ctx
->tsc
;
251 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
252 /* Align extended struct on largest member */
253 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
254 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
255 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
256 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
263 ctx_record(ctx
, ltt_chan
, ltt_chan
->ctx
);
264 ctx_record(ctx
, ltt_chan
, event
->ctx
);
265 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
268 static const struct lttng_ust_lib_ring_buffer_config client_config
;
270 static uint64_t 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 lttng_ust_lib_ring_buffer_config
*config
,
277 struct channel
*chan
, size_t offset
,
278 size_t *pre_header_padding
,
279 struct lttng_ust_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 lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
298 unsigned int subbuf_idx
,
299 struct lttng_ust_shm_handle
*handle
)
301 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
302 struct packet_header
*header
=
303 (struct packet_header
*)
304 lib_ring_buffer_offset_address(&buf
->backend
,
305 subbuf_idx
* chan
->backend
.subbuf_size
,
307 struct ltt_channel
*ltt_chan
= channel_get_private(chan
);
309 header
->magic
= CTF_MAGIC_NUMBER
;
310 memcpy(header
->uuid
, ltt_chan
->uuid
, sizeof(ltt_chan
->uuid
));
311 header
->stream_id
= ltt_chan
->id
;
312 header
->ctx
.timestamp_begin
= tsc
;
313 header
->ctx
.timestamp_end
= 0;
314 header
->ctx
.events_discarded
= 0;
315 header
->ctx
.content_size
= 0xFFFFFFFF; /* for debugging */
316 header
->ctx
.packet_size
= 0xFFFFFFFF;
317 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
321 * offset is assumed to never be 0 here : never deliver a completely empty
322 * subbuffer. data_size is between 1 and subbuf_size.
324 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
325 unsigned int subbuf_idx
, unsigned long data_size
,
326 struct lttng_ust_shm_handle
*handle
)
328 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
329 struct packet_header
*header
=
330 (struct packet_header
*)
331 lib_ring_buffer_offset_address(&buf
->backend
,
332 subbuf_idx
* chan
->backend
.subbuf_size
,
334 unsigned long records_lost
= 0;
336 header
->ctx
.timestamp_end
= tsc
;
337 header
->ctx
.content_size
= data_size
* CHAR_BIT
; /* in bits */
338 header
->ctx
.packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
340 * We do not care about the records lost count, because the metadata
341 * channel waits and retry.
343 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
344 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
345 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
346 header
->ctx
.events_discarded
= records_lost
;
349 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
350 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
355 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
359 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
360 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
361 .cb
.record_header_size
= client_record_header_size
,
362 .cb
.subbuffer_header_size
= client_packet_header_size
,
363 .cb
.buffer_begin
= client_buffer_begin
,
364 .cb
.buffer_end
= client_buffer_end
,
365 .cb
.buffer_create
= client_buffer_create
,
366 .cb
.buffer_finalize
= client_buffer_finalize
,
369 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
370 .sync
= RING_BUFFER_SYNC_GLOBAL
,
371 .mode
= RING_BUFFER_MODE_TEMPLATE
,
372 .backend
= RING_BUFFER_PAGE
,
373 .output
= RING_BUFFER_MMAP
,
374 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
375 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
376 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
377 .client_type
= LTTNG_CLIENT_TYPE
,
380 const struct lttng_ust_lib_ring_buffer_client_cb
*LTTNG_CLIENT_CALLBACKS
= &client_config
.cb
;
383 struct ltt_channel
*_channel_create(const char *name
,
385 size_t subbuf_size
, size_t num_subbuf
,
386 unsigned int switch_timer_interval
,
387 unsigned int read_timer_interval
,
388 int *shm_fd
, int *wait_fd
,
389 uint64_t *memory_map_size
,
390 struct ltt_channel
*chan_priv_init
)
393 struct ltt_channel
*ltt_chan
= NULL
;
394 struct lttng_ust_shm_handle
*handle
;
396 handle
= channel_create(&client_config
, name
,
397 &priv
, __alignof__(*ltt_chan
), sizeof(*ltt_chan
),
399 buf_addr
, subbuf_size
, num_subbuf
,
400 switch_timer_interval
, read_timer_interval
,
401 shm_fd
, wait_fd
, memory_map_size
);
405 ltt_chan
->handle
= handle
;
406 ltt_chan
->chan
= shmp(ltt_chan
->handle
, ltt_chan
->handle
->chan
);
411 void ltt_channel_destroy(struct ltt_channel
*ltt_chan
)
413 channel_destroy(ltt_chan
->chan
, ltt_chan
->handle
, 0);
417 struct lttng_ust_lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
,
418 struct lttng_ust_shm_handle
*handle
,
419 int *shm_fd
, int *wait_fd
,
420 uint64_t *memory_map_size
)
422 struct lttng_ust_lib_ring_buffer
*buf
;
425 for_each_channel_cpu(cpu
, chan
) {
426 buf
= channel_get_ring_buffer(&client_config
, chan
,
427 cpu
, handle
, shm_fd
, wait_fd
,
429 if (!lib_ring_buffer_open_read(buf
, handle
, 0))
436 void ltt_buffer_read_close(struct lttng_ust_lib_ring_buffer
*buf
,
437 struct lttng_ust_shm_handle
*handle
)
439 lib_ring_buffer_release_read(buf
, handle
, 0);
443 int ltt_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
446 struct ltt_channel
*ltt_chan
= channel_get_private(ctx
->chan
);
449 cpu
= lib_ring_buffer_get_cpu(&client_config
);
454 switch (ltt_chan
->header_type
) {
455 case 1: /* compact */
457 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
460 if (event_id
> 65534)
461 ctx
->rflags
|= LTT_RFLAG_EXTENDED
;
467 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
470 ltt_write_event_header(&client_config
, ctx
, event_id
);
473 lib_ring_buffer_put_cpu(&client_config
);
478 void ltt_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
480 lib_ring_buffer_commit(&client_config
, ctx
);
481 lib_ring_buffer_put_cpu(&client_config
);
485 void ltt_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
488 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
493 wait_queue_head_t
*ltt_get_reader_wait_queue(struct channel
*chan
)
495 return &chan
->read_wait
;
499 wait_queue_head_t
*ltt_get_hp_wait_queue(struct channel
*chan
)
501 return &chan
->hp_wait
;
506 int ltt_is_finalized(struct channel
*chan
)
508 return lib_ring_buffer_channel_is_finalized(chan
);
512 int ltt_is_disabled(struct channel
*chan
)
514 return lib_ring_buffer_channel_is_disabled(chan
);
518 int ltt_flush_buffer(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
520 struct lttng_ust_lib_ring_buffer
*buf
;
523 for_each_channel_cpu(cpu
, chan
) {
525 uint64_t memory_map_size
;
527 buf
= channel_get_ring_buffer(&client_config
, chan
,
528 cpu
, handle
, &shm_fd
, &wait_fd
,
530 lib_ring_buffer_switch(&client_config
, buf
,
531 SWITCH_ACTIVE
, handle
);
536 static struct ltt_transport ltt_relay_transport
= {
537 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
539 .channel_create
= _channel_create
,
540 .channel_destroy
= ltt_channel_destroy
,
541 .buffer_read_open
= ltt_buffer_read_open
,
542 .buffer_read_close
= ltt_buffer_read_close
,
543 .event_reserve
= ltt_event_reserve
,
544 .event_commit
= ltt_event_commit
,
545 .event_write
= ltt_event_write
,
546 .packet_avail_size
= NULL
, /* Would be racy anyway */
547 //.get_reader_wait_queue = ltt_get_reader_wait_queue,
548 //.get_hp_wait_queue = ltt_get_hp_wait_queue,
549 .is_finalized
= ltt_is_finalized
,
550 .is_disabled
= ltt_is_disabled
,
551 .flush_buffer
= ltt_flush_buffer
,
555 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
557 DBG("LTT : ltt ring buffer client init\n");
558 ltt_transport_register(<t_relay_transport
);
561 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
563 DBG("LTT : ltt ring buffer client exit\n");
564 ltt_transport_unregister(<t_relay_transport
);