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
36 * Keep the natural field alignment for _each field_ within this structure if
37 * you ever add/remove a field from this header. Packed attribute is not used
38 * because gcc generates poor code on at least powerpc and mips. Don't ever
39 * let gcc add padding between the structure elements.
41 * The guarantee we have with timestamps is that all the events in a
42 * packet are included (inclusive) within the begin/end timestamps of
43 * the packet. Another guarantee we have is that the "timestamp begin",
44 * as well as the event timestamps, are monotonically increasing (never
45 * decrease) when moving forward in a stream (physically). But this
46 * guarantee does not apply to "timestamp end", because it is sampled at
47 * commit time, which is not ordered with respect to space reservation.
50 struct packet_header
{
51 /* Trace packet header */
54 * contains endianness information.
60 /* Stream packet context */
61 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
62 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
63 uint64_t content_size
; /* Size of data in subbuffer */
64 uint64_t packet_size
; /* Subbuffer size (include padding) */
65 unsigned long events_discarded
; /*
66 * Events lost in this subbuffer since
67 * the beginning of the trace.
70 uint32_t cpu_id
; /* CPU id associated with stream */
71 uint8_t header_end
; /* End of header */
76 static inline notrace u64
lib_ring_buffer_clock_read(struct channel
*chan
)
78 return trace_clock_read64();
82 size_t ctx_get_size(size_t offset
, struct lttng_ctx
*ctx
)
85 size_t orig_offset
= offset
;
89 for (i
= 0; i
< ctx
->nr_fields
; i
++)
90 offset
+= ctx
->fields
[i
].get_size(offset
);
91 return offset
- orig_offset
;
95 void ctx_record(struct lib_ring_buffer_ctx
*bufctx
,
96 struct lttng_channel
*chan
,
97 struct lttng_ctx
*ctx
)
103 for (i
= 0; i
< ctx
->nr_fields
; i
++)
104 ctx
->fields
[i
].record(&ctx
->fields
[i
], bufctx
, chan
);
108 * record_header_size - Calculate the header size and padding necessary.
109 * @config: ring buffer instance configuration
111 * @offset: offset in the write buffer
112 * @pre_header_padding: padding to add before the header (output)
113 * @ctx: reservation context
115 * Returns the event header size (including padding).
117 * The payload must itself determine its own alignment from the biggest type it
121 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
122 struct channel
*chan
, size_t offset
,
123 size_t *pre_header_padding
,
124 struct lib_ring_buffer_ctx
*ctx
)
126 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
127 struct lttng_event
*event
= ctx
->priv
;
128 size_t orig_offset
= offset
;
131 switch (lttng_chan
->header_type
) {
132 case 1: /* compact */
133 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
135 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
136 offset
+= sizeof(uint32_t); /* id and timestamp */
138 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
139 offset
+= (LTTNG_COMPACT_EVENT_BITS
+ CHAR_BIT
- 1) / CHAR_BIT
;
140 /* Align extended struct on largest member */
141 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
142 offset
+= sizeof(uint32_t); /* id */
143 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
144 offset
+= sizeof(uint64_t); /* timestamp */
148 padding
= lib_ring_buffer_align(offset
, lttng_alignof(uint16_t));
150 offset
+= sizeof(uint16_t);
151 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
152 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint32_t));
153 offset
+= sizeof(uint32_t); /* timestamp */
155 /* Align extended struct on largest member */
156 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
157 offset
+= sizeof(uint32_t); /* id */
158 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
159 offset
+= sizeof(uint64_t); /* timestamp */
166 offset
+= ctx_get_size(offset
, event
->ctx
);
167 offset
+= ctx_get_size(offset
, lttng_chan
->ctx
);
169 *pre_header_padding
= padding
;
170 return offset
- orig_offset
;
173 #include "wrapper/ringbuffer/api.h"
176 void lttng_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
177 struct lib_ring_buffer_ctx
*ctx
,
181 * lttng_write_event_header
183 * Writes the event header to the offset (already aligned on 32-bits).
185 * @config: ring buffer instance configuration
186 * @ctx: reservation context
187 * @event_id: event ID
190 void lttng_write_event_header(const struct lib_ring_buffer_config
*config
,
191 struct lib_ring_buffer_ctx
*ctx
,
194 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
195 struct lttng_event
*event
= ctx
->priv
;
197 if (unlikely(ctx
->rflags
))
200 switch (lttng_chan
->header_type
) {
201 case 1: /* compact */
203 uint32_t id_time
= 0;
205 bt_bitfield_write(&id_time
, uint32_t,
207 LTTNG_COMPACT_EVENT_BITS
,
209 bt_bitfield_write(&id_time
, uint32_t,
210 LTTNG_COMPACT_EVENT_BITS
,
211 LTTNG_COMPACT_TSC_BITS
,
213 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
218 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
219 uint16_t id
= event_id
;
221 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
222 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
223 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
230 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
);
231 ctx_record(ctx
, lttng_chan
, event
->ctx
);
232 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
237 lttng_write_event_header_slow(config
, ctx
, event_id
);
241 void lttng_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
242 struct lib_ring_buffer_ctx
*ctx
,
245 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
246 struct lttng_event
*event
= ctx
->priv
;
248 switch (lttng_chan
->header_type
) {
249 case 1: /* compact */
250 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
251 uint32_t id_time
= 0;
253 bt_bitfield_write(&id_time
, uint32_t,
255 LTTNG_COMPACT_EVENT_BITS
,
257 bt_bitfield_write(&id_time
, uint32_t,
258 LTTNG_COMPACT_EVENT_BITS
,
259 LTTNG_COMPACT_TSC_BITS
, ctx
->tsc
);
260 lib_ring_buffer_write(config
, ctx
, &id_time
, sizeof(id_time
));
263 uint64_t timestamp
= ctx
->tsc
;
265 bt_bitfield_write(&id
, uint8_t,
267 LTTNG_COMPACT_EVENT_BITS
,
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
));
279 if (!(ctx
->rflags
& (RING_BUFFER_RFLAG_FULL_TSC
| LTTNG_RFLAG_EXTENDED
))) {
280 uint32_t timestamp
= (uint32_t) ctx
->tsc
;
281 uint16_t id
= event_id
;
283 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
284 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint32_t));
285 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
288 uint64_t timestamp
= ctx
->tsc
;
290 lib_ring_buffer_write(config
, ctx
, &id
, sizeof(id
));
291 /* Align extended struct on largest member */
292 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
293 lib_ring_buffer_write(config
, ctx
, &event_id
, sizeof(event_id
));
294 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(uint64_t));
295 lib_ring_buffer_write(config
, ctx
, ×tamp
, sizeof(timestamp
));
302 ctx_record(ctx
, lttng_chan
, lttng_chan
->ctx
);
303 ctx_record(ctx
, lttng_chan
, event
->ctx
);
304 lib_ring_buffer_align_ctx(ctx
, ctx
->largest_align
);
307 static const struct lib_ring_buffer_config client_config
;
309 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
311 return lib_ring_buffer_clock_read(chan
);
315 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
316 struct channel
*chan
, size_t offset
,
317 size_t *pre_header_padding
,
318 struct lib_ring_buffer_ctx
*ctx
)
320 return record_header_size(config
, chan
, offset
,
321 pre_header_padding
, ctx
);
325 * client_packet_header_size - called on buffer-switch to a new sub-buffer
327 * Return header size without padding after the structure. Don't use packed
328 * structure because gcc generates inefficient code on some architectures
331 static size_t client_packet_header_size(void)
333 return offsetof(struct packet_header
, ctx
.header_end
);
336 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
337 unsigned int subbuf_idx
)
339 struct channel
*chan
= buf
->backend
.chan
;
340 struct packet_header
*header
=
341 (struct packet_header
*)
342 lib_ring_buffer_offset_address(&buf
->backend
,
343 subbuf_idx
* chan
->backend
.subbuf_size
);
344 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
345 struct lttng_session
*session
= lttng_chan
->session
;
347 header
->magic
= CTF_MAGIC_NUMBER
;
348 memcpy(header
->uuid
, session
->uuid
.b
, sizeof(session
->uuid
));
349 header
->stream_id
= lttng_chan
->id
;
350 header
->ctx
.timestamp_begin
= tsc
;
351 header
->ctx
.timestamp_end
= 0;
352 header
->ctx
.content_size
= ~0ULL; /* for debugging */
353 header
->ctx
.packet_size
= ~0ULL;
354 header
->ctx
.events_discarded
= 0;
355 header
->ctx
.cpu_id
= buf
->backend
.cpu
;
359 * offset is assumed to never be 0 here : never deliver a completely empty
360 * subbuffer. data_size is between 1 and subbuf_size.
362 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
363 unsigned int subbuf_idx
, unsigned long data_size
)
365 struct channel
*chan
= buf
->backend
.chan
;
366 struct packet_header
*header
=
367 (struct packet_header
*)
368 lib_ring_buffer_offset_address(&buf
->backend
,
369 subbuf_idx
* chan
->backend
.subbuf_size
);
370 unsigned long records_lost
= 0;
372 header
->ctx
.timestamp_end
= tsc
;
373 header
->ctx
.content_size
=
374 (uint64_t) data_size
* CHAR_BIT
; /* in bits */
375 header
->ctx
.packet_size
=
376 (uint64_t) PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
377 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
378 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
379 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
380 header
->ctx
.events_discarded
= records_lost
;
383 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
384 int cpu
, const char *name
)
389 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
393 static struct packet_header
*client_packet_header(
394 const struct lib_ring_buffer_config
*config
,
395 struct lib_ring_buffer
*buf
)
397 return lib_ring_buffer_read_offset_address(&buf
->backend
, 0);
400 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
401 struct lib_ring_buffer
*buf
,
402 uint64_t *timestamp_begin
)
404 struct packet_header
*header
= client_packet_header(config
, buf
);
405 *timestamp_begin
= header
->ctx
.timestamp_begin
;
410 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
411 struct lib_ring_buffer
*buf
,
412 uint64_t *timestamp_end
)
414 struct packet_header
*header
= client_packet_header(config
, buf
);
415 *timestamp_end
= header
->ctx
.timestamp_end
;
420 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
421 struct lib_ring_buffer
*buf
,
422 uint64_t *events_discarded
)
424 struct packet_header
*header
= client_packet_header(config
, buf
);
425 *events_discarded
= header
->ctx
.events_discarded
;
430 static int client_content_size(const struct lib_ring_buffer_config
*config
,
431 struct lib_ring_buffer
*buf
,
432 uint64_t *content_size
)
434 struct packet_header
*header
= client_packet_header(config
, buf
);
435 *content_size
= header
->ctx
.content_size
;
440 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
441 struct lib_ring_buffer
*buf
,
442 uint64_t *packet_size
)
444 struct packet_header
*header
= client_packet_header(config
, buf
);
445 *packet_size
= header
->ctx
.packet_size
;
450 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
451 struct lib_ring_buffer
*buf
,
454 struct packet_header
*header
= client_packet_header(config
, buf
);
455 *stream_id
= header
->stream_id
;
460 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
461 struct lib_ring_buffer
*bufb
,
464 *ts
= config
->cb
.ring_buffer_clock_read(bufb
->backend
.chan
);
469 static const struct lib_ring_buffer_config client_config
= {
470 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
471 .cb
.record_header_size
= client_record_header_size
,
472 .cb
.subbuffer_header_size
= client_packet_header_size
,
473 .cb
.buffer_begin
= client_buffer_begin
,
474 .cb
.buffer_end
= client_buffer_end
,
475 .cb
.buffer_create
= client_buffer_create
,
476 .cb
.buffer_finalize
= client_buffer_finalize
,
478 .tsc_bits
= LTTNG_COMPACT_TSC_BITS
,
479 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
480 .sync
= RING_BUFFER_SYNC_PER_CPU
,
481 .mode
= RING_BUFFER_MODE_TEMPLATE
,
482 .backend
= RING_BUFFER_PAGE
,
483 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
484 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
485 .ipi
= RING_BUFFER_IPI_BARRIER
,
486 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
490 struct channel
*_channel_create(const char *name
,
491 struct lttng_channel
*lttng_chan
, void *buf_addr
,
492 size_t subbuf_size
, size_t num_subbuf
,
493 unsigned int switch_timer_interval
,
494 unsigned int read_timer_interval
)
496 return channel_create(&client_config
, name
, lttng_chan
, buf_addr
,
497 subbuf_size
, num_subbuf
, switch_timer_interval
,
498 read_timer_interval
);
502 void lttng_channel_destroy(struct channel
*chan
)
504 channel_destroy(chan
);
508 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
510 struct lib_ring_buffer
*buf
;
513 for_each_channel_cpu(cpu
, chan
) {
514 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
515 if (!lib_ring_buffer_open_read(buf
))
522 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
524 struct lib_ring_buffer
*buf
;
527 for_each_channel_cpu(cpu
, chan
) {
528 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
529 if (!atomic_long_read(&buf
->active_readers
))
536 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
538 lib_ring_buffer_release_read(buf
);
542 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
,
545 struct lttng_channel
*lttng_chan
= channel_get_private(ctx
->chan
);
548 cpu
= lib_ring_buffer_get_cpu(&client_config
);
553 switch (lttng_chan
->header_type
) {
554 case 1: /* compact */
556 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
559 if (event_id
> 65534)
560 ctx
->rflags
|= LTTNG_RFLAG_EXTENDED
;
566 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
569 lttng_write_event_header(&client_config
, ctx
, event_id
);
572 lib_ring_buffer_put_cpu(&client_config
);
577 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
579 lib_ring_buffer_commit(&client_config
, ctx
);
580 lib_ring_buffer_put_cpu(&client_config
);
584 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
587 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
591 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
592 const void __user
*src
, size_t len
)
594 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
598 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
601 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
605 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
607 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
609 return &buf
->write_wait
;
613 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
615 return &chan
->hp_wait
;
619 int lttng_is_finalized(struct channel
*chan
)
621 return lib_ring_buffer_channel_is_finalized(chan
);
625 int lttng_is_disabled(struct channel
*chan
)
627 return lib_ring_buffer_channel_is_disabled(chan
);
630 static struct lttng_transport lttng_relay_transport
= {
631 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
632 .owner
= THIS_MODULE
,
634 .channel_create
= _channel_create
,
635 .channel_destroy
= lttng_channel_destroy
,
636 .buffer_read_open
= lttng_buffer_read_open
,
637 .buffer_has_read_closed_stream
=
638 lttng_buffer_has_read_closed_stream
,
639 .buffer_read_close
= lttng_buffer_read_close
,
640 .event_reserve
= lttng_event_reserve
,
641 .event_commit
= lttng_event_commit
,
642 .event_write
= lttng_event_write
,
643 .event_write_from_user
= lttng_event_write_from_user
,
644 .event_memset
= lttng_event_memset
,
645 .packet_avail_size
= NULL
, /* Would be racy anyway */
646 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
647 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
648 .is_finalized
= lttng_is_finalized
,
649 .is_disabled
= lttng_is_disabled
,
650 .timestamp_begin
= client_timestamp_begin
,
651 .timestamp_end
= client_timestamp_end
,
652 .events_discarded
= client_events_discarded
,
653 .content_size
= client_content_size
,
654 .packet_size
= client_packet_size
,
655 .stream_id
= client_stream_id
,
656 .current_timestamp
= client_current_timestamp
,
660 static int __init
lttng_ring_buffer_client_init(void)
663 * This vmalloc sync all also takes care of the lib ring buffer
664 * vmalloc'd module pages when it is built as a module into LTTng.
666 wrapper_vmalloc_sync_all();
667 lttng_transport_register(<tng_relay_transport
);
671 module_init(lttng_ring_buffer_client_init
);
673 static void __exit
lttng_ring_buffer_client_exit(void)
675 lttng_transport_unregister(<tng_relay_transport
);
678 module_exit(lttng_ring_buffer_client_exit
);
680 MODULE_LICENSE("GPL and additional rights");
681 MODULE_AUTHOR("Mathieu Desnoyers");
682 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING