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
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include "lib/bitfield.h"
26 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
27 #include "wrapper/trace-clock.h"
28 #include "lttng-events.h"
29 #include "lttng-tracer.h"
30 #include "wrapper/ringbuffer/frontend_types.h"
32 #define LTTNG_COMPACT_EVENT_BITS 5
33 #define LTTNG_COMPACT_TSC_BITS 27
35 static struct lttng_transport lttng_relay_transport
;
38 * Keep the natural field alignment for _each field_ within this structure if
39 * you ever add/remove a field from this header. Packed attribute is not used
40 * because gcc generates poor code on at least powerpc and mips. Don't ever
41 * let gcc add padding between the structure elements.
43 * The guarantee we have with timestamps is that all the events in a
44 * packet are included (inclusive) within the begin/end timestamps of
45 * the packet. Another guarantee we have is that the "timestamp begin",
46 * as well as the event timestamps, are monotonically increasing (never
47 * decrease) when moving forward in a stream (physically). But this
48 * guarantee does not apply to "timestamp end", because it is sampled at
49 * commit time, which is not ordered with respect to space reservation.
52 struct packet_header
{
53 /* Trace packet header */
56 * contains endianness information.
62 /* Stream packet context */
63 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
64 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
65 uint64_t content_size
; /* Size of data in subbuffer */
66 uint64_t packet_size
; /* Subbuffer size (include padding) */
67 unsigned long events_discarded
; /*
68 * Events lost in this subbuffer since
69 * the beginning of the trace.
72 uint32_t cpu_id
; /* CPU id associated with stream */
73 uint8_t header_end
; /* End of header */
78 static inline notrace u64
lib_ring_buffer_clock_read(struct channel
*chan
)
80 return trace_clock_read64();
84 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
)
87 size_t orig_offset
= offset
;
91 for (i
= 0; i
< ctx
->nr_fields
; i
++)
92 offset
+= ctx
->fields
[i
].get_size(offset
);
93 return offset
- orig_offset
;
97 void ctx_record(struct lib_ring_buffer_ctx
*bufctx
,
98 struct lttng_channel
*chan
,
99 struct lttng_ctx
*ctx
)
105 for (i
= 0; i
< ctx
->nr_fields
; i
++)
106 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
110 * record_header_size - Calculate the header size and padding necessary.
111 * @config: ring buffer instance configuration
113 * @offset: offset in the write buffer
114 * @pre_header_padding: padding to add before the header (output)
115 * @ctx: reservation context
117 * Returns the event header size (including padding).
119 * The payload must itself determine its own alignment from the biggest type it
123 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
124 struct channel
*chan
, size_t offset
,
125 size_t *pre_header_padding
,
126 struct lib_ring_buffer_ctx
*ctx
)
128 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
129 struct lttng_event
*event
= ctx
->priv
;
130 size_t orig_offset
= offset
;
133 switch (lttng_chan
->header_type
) {
134 case 1: /* compact */
135 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
137 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
138 offset
+= sizeof(uint32_t); /* id and timestamp */
140 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
141 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
142 /* Align extended struct on largest member */
143 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
144 offset
+= sizeof(uint32_t); /* id */
145 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
146 offset
+= sizeof(uint64_t); /* timestamp */
150 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
152 offset
+= sizeof(uint16_t);
153 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
154 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
155 offset
+= sizeof(uint32_t); /* timestamp */
157 /* Align extended struct on largest member */
158 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
159 offset
+= sizeof(uint32_t); /* id */
160 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
161 offset
+= sizeof(uint64_t); /* timestamp */
168 offset
+= ctx_get_size(offset
, event
->ctx
);
169 offset
+= ctx_get_size(offset
, lttng_chan
->ctx
);
171 *pre_header_padding
= padding
;
172 return offset
- orig_offset
;
175 #include "wrapper/ringbuffer/api.h"
178 void lttng_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
179 struct lib_ring_buffer_ctx
*ctx
,
183 * lttng_write_event_header
185 * Writes the event header to the offset (already aligned on 32-bits).
187 * @config: ring buffer instance configuration
188 * @ctx: reservation context
189 * @event_id: event ID
192 void lttng_write_event_header(const struct lib_ring_buffer_config
*config
,
193 struct lib_ring_buffer_ctx
*ctx
,
196 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
197 struct lttng_event
*event
= ctx
->priv
;
199 if (unlikely(ctx
->rflags
))
202 switch (lttng_chan
->header_type
) {
203 case 1: /* compact */
205 uint32_t id_time
= 0;
207 bt_bitfield_write(&id_time
, uint32_t,
209 LTTNG_COMPACT_EVENT_BITS
,
211 bt_bitfield_write(&id_time
, uint32_t,
212 LTTNG_COMPACT_EVENT_BITS
,
213 LTTNG_COMPACT_TSC_BITS
,
215 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
220 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
221 uint16_t id
= event_id
;
223 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
224 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
225 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
232 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
);
233 ctx_record(ctx
, lttng_chan
, event
->ctx
);
234 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
239 lttng_write_event_header_slow(config
, ctx
, event_id
);
243 void lttng_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
244 struct lib_ring_buffer_ctx
*ctx
,
247 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
248 struct lttng_event
*event
= ctx
->priv
;
250 switch (lttng_chan
->header_type
) {
251 case 1: /* compact */
252 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
253 uint32_t id_time
= 0;
255 bt_bitfield_write(&id_time
, uint32_t,
257 LTTNG_COMPACT_EVENT_BITS
,
259 bt_bitfield_write(&id_time
, uint32_t,
260 LTTNG_COMPACT_EVENT_BITS
,
261 LTTNG_COMPACT_TSC_BITS
, ctx
->tsc
);
262 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
265 uint64_t timestamp
= ctx
->tsc
;
267 bt_bitfield_write(&id
, uint8_t,
269 LTTNG_COMPACT_EVENT_BITS
,
271 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
272 /* Align extended struct on largest member */
273 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
274 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
275 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
276 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
281 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
282 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
283 uint16_t id
= event_id
;
285 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
286 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
287 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
290 uint64_t timestamp
= ctx
->tsc
;
292 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
293 /* Align extended struct on largest member */
294 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
295 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
296 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
297 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
304 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
);
305 ctx_record(ctx
, lttng_chan
, event
->ctx
);
306 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
309 static const struct lib_ring_buffer_config client_config
;
311 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
313 return lib_ring_buffer_clock_read(chan
);
317 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
318 struct channel
*chan
, size_t offset
,
319 size_t *pre_header_padding
,
320 struct lib_ring_buffer_ctx
*ctx
)
322 return record_header_size(config
, chan
, offset
,
323 pre_header_padding
, ctx
);
327 * client_packet_header_size - called on buffer-switch to a new sub-buffer
329 * Return header size without padding after the structure. Don't use packed
330 * structure because gcc generates inefficient code on some architectures
333 static size_t client_packet_header_size(void)
335 return offsetof(struct packet_header
, ctx
.header_end
);
338 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
339 unsigned int subbuf_idx
)
341 struct channel
*chan
= buf
->backend
.chan
;
342 struct packet_header
*header
=
343 (struct packet_header
*)
344 lib_ring_buffer_offset_address(&buf
->backend
,
345 subbuf_idx
* chan
->backend
.subbuf_size
);
346 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
347 struct lttng_session
*session
= lttng_chan
->session
;
349 header
->magic
= CTF_MAGIC_NUMBER
;
350 memcpy(header
->uuid
, session
->uuid
.b
, sizeof(session
->uuid
));
351 header
->stream_id
= lttng_chan
->id
;
352 header
->ctx
.timestamp_begin
= tsc
;
353 header
->ctx
.timestamp_end
= 0;
354 header
->ctx
.content_size
= ~0ULL; /* for debugging */
355 header
->ctx
.packet_size
= ~0ULL;
356 header
->ctx
.events_discarded
= 0;
357 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
361 * offset is assumed to never be 0 here : never deliver a completely empty
362 * subbuffer. data_size is between 1 and subbuf_size.
364 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
365 unsigned int subbuf_idx
, unsigned long data_size
)
367 struct channel
*chan
= buf
->backend
.chan
;
368 struct packet_header
*header
=
369 (struct packet_header
*)
370 lib_ring_buffer_offset_address(&buf
->backend
,
371 subbuf_idx
* chan
->backend
.subbuf_size
);
372 unsigned long records_lost
= 0;
374 header
->ctx
.timestamp_end
= tsc
;
375 header
->ctx
.content_size
=
376 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
377 header
->ctx
.packet_size
=
378 (uint64_t) PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
379 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
380 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
381 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
382 header
->ctx
.events_discarded
= records_lost
;
385 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
386 int cpu
, const char *name
)
391 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
395 static struct packet_header
*client_packet_header(
396 const struct lib_ring_buffer_config
*config
,
397 struct lib_ring_buffer
*buf
)
399 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0);
402 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
403 struct lib_ring_buffer
*buf
,
404 uint64_t *timestamp_begin
)
406 struct packet_header
*header
= client_packet_header(config
, buf
);
407 *timestamp_begin
= header
->ctx
.timestamp_begin
;
412 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
413 struct lib_ring_buffer
*buf
,
414 uint64_t *timestamp_end
)
416 struct packet_header
*header
= client_packet_header(config
, buf
);
417 *timestamp_end
= header
->ctx
.timestamp_end
;
422 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
423 struct lib_ring_buffer
*buf
,
424 uint64_t *events_discarded
)
426 struct packet_header
*header
= client_packet_header(config
, buf
);
427 *events_discarded
= header
->ctx
.events_discarded
;
432 static int client_content_size(const struct lib_ring_buffer_config
*config
,
433 struct lib_ring_buffer
*buf
,
434 uint64_t *content_size
)
436 struct packet_header
*header
= client_packet_header(config
, buf
);
437 *content_size
= header
->ctx
.content_size
;
442 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
443 struct lib_ring_buffer
*buf
,
444 uint64_t *packet_size
)
446 struct packet_header
*header
= client_packet_header(config
, buf
);
447 *packet_size
= header
->ctx
.packet_size
;
452 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
453 struct lib_ring_buffer
*buf
,
456 struct packet_header
*header
= client_packet_header(config
, buf
);
457 *stream_id
= header
->stream_id
;
462 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
463 struct lib_ring_buffer
*bufb
,
466 *ts
= config
->cb
.ring_buffer_clock_read(bufb
->backend
.chan
);
471 static const struct lib_ring_buffer_config client_config
= {
472 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
473 .cb
.record_header_size
= client_record_header_size
,
474 .cb
.subbuffer_header_size
= client_packet_header_size
,
475 .cb
.buffer_begin
= client_buffer_begin
,
476 .cb
.buffer_end
= client_buffer_end
,
477 .cb
.buffer_create
= client_buffer_create
,
478 .cb
.buffer_finalize
= client_buffer_finalize
,
480 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
481 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
482 .sync
= RING_BUFFER_SYNC_PER_CPU
,
483 .mode
= RING_BUFFER_MODE_TEMPLATE
,
484 .backend
= RING_BUFFER_PAGE
,
485 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
486 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
487 .ipi
= RING_BUFFER_IPI_BARRIER
,
488 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
492 void release_priv_ops(void *priv_ops
)
494 module_put(THIS_MODULE
);
498 void lttng_channel_destroy(struct channel
*chan
)
500 channel_destroy(chan
);
504 struct channel
*_channel_create(const char *name
,
505 struct lttng_channel
*lttng_chan
, void *buf_addr
,
506 size_t subbuf_size
, size_t num_subbuf
,
507 unsigned int switch_timer_interval
,
508 unsigned int read_timer_interval
)
510 struct channel
*chan
;
512 chan
= channel_create(&client_config
, name
, lttng_chan
, buf_addr
,
513 subbuf_size
, num_subbuf
, switch_timer_interval
,
514 read_timer_interval
);
517 * Ensure this module is not unloaded before we finish
518 * using lttng_relay_transport.ops.
520 if (!try_module_get(THIS_MODULE
)) {
521 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
524 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
525 chan
->backend
.release_priv_ops
= release_priv_ops
;
530 lttng_channel_destroy(chan
);
535 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
537 struct lib_ring_buffer
*buf
;
540 for_each_channel_cpu(cpu
, chan
) {
541 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
542 if (!lib_ring_buffer_open_read(buf
))
549 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
551 struct lib_ring_buffer
*buf
;
554 for_each_channel_cpu(cpu
, chan
) {
555 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
556 if (!atomic_long_read(&buf
->active_readers
))
563 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
565 lib_ring_buffer_release_read(buf
);
569 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
,
572 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
575 cpu
= lib_ring_buffer_get_cpu(&client_config
);
580 switch (lttng_chan
->header_type
) {
581 case 1: /* compact */
583 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
586 if (event_id
> 65534)
587 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
593 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
596 lttng_write_event_header(&client_config
, ctx
, event_id
);
599 lib_ring_buffer_put_cpu(&client_config
);
604 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
606 lib_ring_buffer_commit(&client_config
, ctx
);
607 lib_ring_buffer_put_cpu(&client_config
);
611 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
614 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
618 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
619 const void __user
*src
, size_t len
)
621 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
625 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
628 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
632 void lttng_event_strcpy(struct lib_ring_buffer_ctx
*ctx
, const char *src
,
635 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
639 void lttng_event_strcpy_from_user(struct lib_ring_buffer_ctx
*ctx
,
640 const char __user
*src
, size_t len
)
642 lib_ring_buffer_strcpy_from_user_inatomic(&client_config
, ctx
, src
,
647 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
649 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
651 return &buf
->write_wait
;
655 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
657 return &chan
->hp_wait
;
661 int lttng_is_finalized(struct channel
*chan
)
663 return lib_ring_buffer_channel_is_finalized(chan
);
667 int lttng_is_disabled(struct channel
*chan
)
669 return lib_ring_buffer_channel_is_disabled(chan
);
672 static struct lttng_transport lttng_relay_transport
= {
673 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
674 .owner
= THIS_MODULE
,
676 .channel_create
= _channel_create
,
677 .channel_destroy
= lttng_channel_destroy
,
678 .buffer_read_open
= lttng_buffer_read_open
,
679 .buffer_has_read_closed_stream
=
680 lttng_buffer_has_read_closed_stream
,
681 .buffer_read_close
= lttng_buffer_read_close
,
682 .event_reserve
= lttng_event_reserve
,
683 .event_commit
= lttng_event_commit
,
684 .event_write
= lttng_event_write
,
685 .event_write_from_user
= lttng_event_write_from_user
,
686 .event_memset
= lttng_event_memset
,
687 .event_strcpy
= lttng_event_strcpy
,
688 .event_strcpy_from_user
= lttng_event_strcpy_from_user
,
689 .packet_avail_size
= NULL
, /* Would be racy anyway */
690 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
691 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
692 .is_finalized
= lttng_is_finalized
,
693 .is_disabled
= lttng_is_disabled
,
694 .timestamp_begin
= client_timestamp_begin
,
695 .timestamp_end
= client_timestamp_end
,
696 .events_discarded
= client_events_discarded
,
697 .content_size
= client_content_size
,
698 .packet_size
= client_packet_size
,
699 .stream_id
= client_stream_id
,
700 .current_timestamp
= client_current_timestamp
,
704 static int __init
lttng_ring_buffer_client_init(void)
707 * This vmalloc sync all also takes care of the lib ring buffer
708 * vmalloc'd module pages when it is built as a module into LTTng.
710 wrapper_vmalloc_sync_all();
711 lttng_transport_register(<tng_relay_transport
);
715 module_init(lttng_ring_buffer_client_init
);
717 static void __exit
lttng_ring_buffer_client_exit(void)
719 lttng_transport_unregister(<tng_relay_transport
);
722 module_exit(lttng_ring_buffer_client_exit
);
724 MODULE_LICENSE("GPL and additional rights");
725 MODULE_AUTHOR("Mathieu Desnoyers");
726 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING