2 * lttng-ring-buffer-client.h
4 * LTTng lib ring buffer client template.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lttng/ust-events.h>
25 #include "lttng/bitfield.h"
27 #include "lttng-ust-uuid.h"
28 #include "lttng-tracer.h"
29 #include "../libringbuffer/frontend_types.h"
31 #define LTTNG_COMPACT_EVENT_BITS 5
32 #define LTTNG_COMPACT_TSC_BITS 27
35 * Keep the natural field alignment for _each field_ within this structure if
36 * you ever add/remove a field from this header. Packed attribute is not used
37 * because gcc generates poor code on at least powerpc and mips. Don't ever
38 * let gcc add padding between the structure elements.
41 struct packet_header
{
42 /* Trace packet header */
45 * contains endianness information.
47 uint8_t uuid
[LTTNG_UST_UUID_LEN
];
51 /* Stream packet context */
52 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
53 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
54 uint64_t content_size
; /* Size of data in subbuffer */
55 uint64_t packet_size
; /* Subbuffer size (include padding) */
56 unsigned long events_discarded
; /*
57 * Events lost in this subbuffer since
58 * the beginning of the trace.
61 uint32_t cpu_id
; /* CPU id associated with stream */
62 uint8_t header_end
; /* End of header */
67 static inline uint64_t lib_ring_buffer_clock_read(struct channel
*chan
)
69 return trace_clock_read64();
73 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
)
76 size_t orig_offset
= offset
;
80 for (i
= 0; i
< ctx
->nr_fields
; i
++)
81 offset
+= ctx
->fields
[i
].get_size(offset
);
82 return offset
- orig_offset
;
86 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx
*bufctx
,
87 struct lttng_channel
*chan
,
88 struct lttng_ctx
*ctx
)
94 for (i
= 0; i
< ctx
->nr_fields
; i
++)
95 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
99 * record_header_size - Calculate the header size and padding necessary.
100 * @config: ring buffer instance configuration
102 * @offset: offset in the write buffer
103 * @pre_header_padding: padding to add before the header (output)
104 * @ctx: reservation context
106 * Returns the event header size (including padding).
108 * The payload must itself determine its own alignment from the biggest type it
112 unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
113 struct channel
*chan
, size_t offset
,
114 size_t *pre_header_padding
,
115 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
117 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
118 struct lttng_event
*event
= ctx
->priv
;
119 size_t orig_offset
= offset
;
122 switch (lttng_chan
->header_type
) {
123 case 1: /* compact */
124 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
126 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
127 offset
+= sizeof(uint32_t); /* id and timestamp */
129 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
130 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
131 /* Align extended struct on largest member */
132 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
133 offset
+= sizeof(uint32_t); /* id */
134 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
135 offset
+= sizeof(uint64_t); /* timestamp */
139 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
141 offset
+= sizeof(uint16_t);
142 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
143 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
144 offset
+= sizeof(uint32_t); /* timestamp */
146 /* Align extended struct on largest member */
147 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
148 offset
+= sizeof(uint32_t); /* id */
149 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
150 offset
+= sizeof(uint64_t); /* timestamp */
157 offset
+= ctx_get_size(offset
, event
->ctx
);
158 offset
+= ctx_get_size(offset
, lttng_chan
->ctx
);
160 *pre_header_padding
= padding
;
161 return offset
- orig_offset
;
164 #include "../libringbuffer/api.h"
167 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
168 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
172 * lttng_write_event_header
174 * Writes the event header to the offset (already aligned on 32-bits).
176 * @config: ring buffer instance configuration
177 * @ctx: reservation context
178 * @event_id: event ID
181 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config
*config
,
182 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
185 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
186 struct lttng_event
*event
= ctx
->priv
;
188 if (caa_unlikely(ctx
->rflags
))
191 switch (lttng_chan
->header_type
) {
192 case 1: /* compact */
194 uint32_t id_time
= 0;
196 bt_bitfield_write(&id_time
, uint32_t,
198 LTTNG_COMPACT_EVENT_BITS
,
200 bt_bitfield_write(&id_time
, uint32_t,
201 LTTNG_COMPACT_EVENT_BITS
,
202 LTTNG_COMPACT_TSC_BITS
,
204 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
209 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
210 uint16_t id
= event_id
;
212 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
213 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
214 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
221 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
);
222 ctx_record(ctx
, lttng_chan
, event
->ctx
);
223 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
228 lttng_write_event_header_slow(config
, ctx
, event_id
);
232 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
233 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
236 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
237 struct lttng_event
*event
= ctx
->priv
;
239 switch (lttng_chan
->header_type
) {
240 case 1: /* compact */
241 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
242 uint32_t id_time
= 0;
244 bt_bitfield_write(&id_time
, uint32_t,
246 LTTNG_COMPACT_EVENT_BITS
,
248 bt_bitfield_write(&id_time
, uint32_t,
249 LTTNG_COMPACT_EVENT_BITS
,
250 LTTNG_COMPACT_TSC_BITS
,
252 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
255 uint64_t timestamp
= ctx
->tsc
;
257 bt_bitfield_write(&id
, uint8_t,
259 LTTNG_COMPACT_EVENT_BITS
,
261 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
262 /* Align extended struct on largest member */
263 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
264 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
265 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
266 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
271 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
272 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
273 uint16_t id
= event_id
;
275 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
276 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
277 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
280 uint64_t timestamp
= ctx
->tsc
;
282 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
283 /* Align extended struct on largest member */
284 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
285 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
286 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
287 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
294 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
);
295 ctx_record(ctx
, lttng_chan
, event
->ctx
);
296 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
299 static const struct lttng_ust_lib_ring_buffer_config client_config
;
301 static uint64_t client_ring_buffer_clock_read(struct channel
*chan
)
303 return lib_ring_buffer_clock_read(chan
);
307 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
308 struct channel
*chan
, size_t offset
,
309 size_t *pre_header_padding
,
310 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
312 return record_header_size(config
, chan
, offset
,
313 pre_header_padding
, ctx
);
317 * client_packet_header_size - called on buffer-switch to a new sub-buffer
319 * Return header size without padding after the structure. Don't use packed
320 * structure because gcc generates inefficient code on some architectures
323 static size_t client_packet_header_size(void)
325 return offsetof(struct packet_header
, ctx
.header_end
);
328 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
329 unsigned int subbuf_idx
,
330 struct lttng_ust_shm_handle
*handle
)
332 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
333 struct packet_header
*header
=
334 (struct packet_header
*)
335 lib_ring_buffer_offset_address(&buf
->backend
,
336 subbuf_idx
* chan
->backend
.subbuf_size
,
338 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
340 header
->magic
= CTF_MAGIC_NUMBER
;
341 memcpy(header
->uuid
, lttng_chan
->uuid
, sizeof(lttng_chan
->uuid
));
342 header
->stream_id
= lttng_chan
->id
;
343 header
->ctx
.timestamp_begin
= tsc
;
344 header
->ctx
.timestamp_end
= 0;
345 header
->ctx
.content_size
= ~0ULL; /* for debugging */
346 header
->ctx
.packet_size
= ~0ULL;
347 header
->ctx
.events_discarded
= 0;
348 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
352 * offset is assumed to never be 0 here : never deliver a completely empty
353 * subbuffer. data_size is between 1 and subbuf_size.
355 static void client_buffer_end(struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
356 unsigned int subbuf_idx
, unsigned long data_size
,
357 struct lttng_ust_shm_handle
*handle
)
359 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
360 struct packet_header
*header
=
361 (struct packet_header
*)
362 lib_ring_buffer_offset_address(&buf
->backend
,
363 subbuf_idx
* chan
->backend
.subbuf_size
,
365 unsigned long records_lost
= 0;
367 header
->ctx
.timestamp_end
= tsc
;
368 header
->ctx
.content_size
=
369 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
370 header
->ctx
.packet_size
=
371 (uint64_t) PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
373 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
374 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
375 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
376 header
->ctx
.events_discarded
= records_lost
;
379 static int client_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
380 int cpu
, const char *name
, struct lttng_ust_shm_handle
*handle
)
385 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer
*buf
, void *priv
, int cpu
, struct lttng_ust_shm_handle
*handle
)
389 static const struct lttng_ust_lib_ring_buffer_config client_config
= {
390 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
391 .cb
.record_header_size
= client_record_header_size
,
392 .cb
.subbuffer_header_size
= client_packet_header_size
,
393 .cb
.buffer_begin
= client_buffer_begin
,
394 .cb
.buffer_end
= client_buffer_end
,
395 .cb
.buffer_create
= client_buffer_create
,
396 .cb
.buffer_finalize
= client_buffer_finalize
,
398 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
399 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
400 .sync
= RING_BUFFER_SYNC_GLOBAL
,
401 .mode
= RING_BUFFER_MODE_TEMPLATE
,
402 .backend
= RING_BUFFER_PAGE
,
403 .output
= RING_BUFFER_MMAP
,
404 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
405 .ipi
= RING_BUFFER_NO_IPI_BARRIER
,
406 .wakeup
= RING_BUFFER_WAKEUP_BY_WRITER
,
407 .client_type
= LTTNG_CLIENT_TYPE
,
410 const struct lttng_ust_lib_ring_buffer_client_cb
*LTTNG_CLIENT_CALLBACKS
= &client_config
.cb
;
413 struct lttng_channel
*_channel_create(const char *name
,
415 size_t subbuf_size
, size_t num_subbuf
,
416 unsigned int switch_timer_interval
,
417 unsigned int read_timer_interval
,
418 int **shm_fd
, int **wait_fd
,
419 uint64_t **memory_map_size
,
420 struct lttng_channel
*chan_priv_init
)
423 struct lttng_channel
*lttng_chan
= NULL
;
424 struct lttng_ust_shm_handle
*handle
;
426 handle
= channel_create(&client_config
, name
,
427 &priv
, __alignof__(*lttng_chan
), sizeof(*lttng_chan
),
429 buf_addr
, subbuf_size
, num_subbuf
,
430 switch_timer_interval
, read_timer_interval
,
431 shm_fd
, wait_fd
, memory_map_size
);
435 lttng_chan
->handle
= handle
;
436 lttng_chan
->chan
= shmp(lttng_chan
->handle
, lttng_chan
->handle
->chan
);
441 void lttng_channel_destroy(struct lttng_channel
*lttng_chan
)
443 channel_destroy(lttng_chan
->chan
, lttng_chan
->handle
, 0);
447 struct lttng_ust_lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
,
448 struct lttng_ust_shm_handle
*handle
,
449 int **shm_fd
, int **wait_fd
,
450 uint64_t **memory_map_size
)
452 struct lttng_ust_lib_ring_buffer
*buf
;
455 for_each_channel_cpu(cpu
, chan
) {
456 buf
= channel_get_ring_buffer(&client_config
, chan
,
457 cpu
, handle
, shm_fd
, wait_fd
,
459 if (!lib_ring_buffer_open_read(buf
, handle
, 0))
466 void lttng_buffer_read_close(struct lttng_ust_lib_ring_buffer
*buf
,
467 struct lttng_ust_shm_handle
*handle
)
469 lib_ring_buffer_release_read(buf
, handle
, 0);
473 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
476 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
479 cpu
= lib_ring_buffer_get_cpu(&client_config
);
484 switch (lttng_chan
->header_type
) {
485 case 1: /* compact */
487 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
490 if (event_id
> 65534)
491 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
497 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
500 lttng_write_event_header(&client_config
, ctx
, event_id
);
503 lib_ring_buffer_put_cpu(&client_config
);
508 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
510 lib_ring_buffer_commit(&client_config
, ctx
);
511 lib_ring_buffer_put_cpu(&client_config
);
515 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx
*ctx
, const void *src
,
518 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
523 wait_queue_head_t
*lttng_get_reader_wait_queue(struct channel
*chan
)
525 return &chan
->read_wait
;
529 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
531 return &chan
->hp_wait
;
536 int lttng_is_finalized(struct channel
*chan
)
538 return lib_ring_buffer_channel_is_finalized(chan
);
542 int lttng_is_disabled(struct channel
*chan
)
544 return lib_ring_buffer_channel_is_disabled(chan
);
548 int lttng_flush_buffer(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
550 struct lttng_ust_lib_ring_buffer
*buf
;
553 for_each_channel_cpu(cpu
, chan
) {
554 int *shm_fd
, *wait_fd
;
555 uint64_t *memory_map_size
;
557 buf
= channel_get_ring_buffer(&client_config
, chan
,
558 cpu
, handle
, &shm_fd
, &wait_fd
,
560 lib_ring_buffer_switch(&client_config
, buf
,
561 SWITCH_ACTIVE
, handle
);
566 static struct lttng_transport lttng_relay_transport
= {
567 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
"-mmap",
569 .channel_create
= _channel_create
,
570 .channel_destroy
= lttng_channel_destroy
,
571 .buffer_read_open
= lttng_buffer_read_open
,
572 .buffer_read_close
= lttng_buffer_read_close
,
573 .event_reserve
= lttng_event_reserve
,
574 .event_commit
= lttng_event_commit
,
575 .event_write
= lttng_event_write
,
576 .packet_avail_size
= NULL
, /* Would be racy anyway */
577 //.get_reader_wait_queue = lttng_get_reader_wait_queue,
578 //.get_hp_wait_queue = lttng_get_hp_wait_queue,
579 .is_finalized
= lttng_is_finalized
,
580 .is_disabled
= lttng_is_disabled
,
581 .flush_buffer
= lttng_flush_buffer
,
585 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
587 DBG("LTT : ltt ring buffer client init\n");
588 lttng_transport_register(<tng_relay_transport
);
591 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
593 DBG("LTT : ltt ring buffer client exit\n");
594 lttng_transport_unregister(<tng_relay_transport
);