4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ring_buffer.h>
7 #include <linux/spinlock.h>
8 #include <linux/debugfs.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
11 #include <linux/percpu.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h> /* used for sched_clock() (for now) */
14 #include <linux/init.h>
15 #include <linux/hash.h>
16 #include <linux/list.h>
21 /* Up this if you want to test the TIME_EXTENTS and normalization */
25 u64
ring_buffer_time_stamp(int cpu
)
27 /* shift to debug/test normalization and TIME_EXTENTS */
28 return sched_clock() << DEBUG_SHIFT
;
31 void ring_buffer_normalize_time_stamp(int cpu
, u64
*ts
)
33 /* Just stupid testing the normalize function and deltas */
37 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
38 #define RB_ALIGNMENT_SHIFT 2
39 #define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT)
40 #define RB_MAX_SMALL_DATA 28
43 RB_LEN_TIME_EXTEND
= 8,
44 RB_LEN_TIME_STAMP
= 16,
47 /* inline for ring buffer fast paths */
48 static inline unsigned
49 rb_event_length(struct ring_buffer_event
*event
)
53 switch (event
->type
) {
54 case RINGBUF_TYPE_PADDING
:
58 case RINGBUF_TYPE_TIME_EXTEND
:
59 return RB_LEN_TIME_EXTEND
;
61 case RINGBUF_TYPE_TIME_STAMP
:
62 return RB_LEN_TIME_STAMP
;
64 case RINGBUF_TYPE_DATA
:
66 length
= event
->len
<< RB_ALIGNMENT_SHIFT
;
68 length
= event
->array
[0];
69 return length
+ RB_EVNT_HDR_SIZE
;
78 * ring_buffer_event_length - return the length of the event
79 * @event: the event to get the length of
81 unsigned ring_buffer_event_length(struct ring_buffer_event
*event
)
83 return rb_event_length(event
);
86 /* inline for ring buffer fast paths */
88 rb_event_data(struct ring_buffer_event
*event
)
90 BUG_ON(event
->type
!= RINGBUF_TYPE_DATA
);
91 /* If length is in len field, then array[0] has the data */
93 return (void *)&event
->array
[0];
94 /* Otherwise length is in array[0] and array[1] has the data */
95 return (void *)&event
->array
[1];
99 * ring_buffer_event_data - return the data of the event
100 * @event: the event to get the data from
102 void *ring_buffer_event_data(struct ring_buffer_event
*event
)
104 return rb_event_data(event
);
107 #define for_each_buffer_cpu(buffer, cpu) \
108 for_each_cpu_mask(cpu, buffer->cpumask)
111 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
112 #define TS_DELTA_TEST (~TS_MASK)
115 * This hack stolen from mm/slob.c.
116 * We can store per page timing information in the page frame of the page.
117 * Thanks to Peter Zijlstra for suggesting this idea.
120 u64 time_stamp
; /* page time stamp */
121 local_t write
; /* index for next write */
122 local_t commit
; /* write commited index */
123 unsigned read
; /* index for next read */
124 struct list_head list
; /* list of free pages */
125 void *page
; /* Actual data page */
129 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
132 static inline void free_buffer_page(struct buffer_page
*bpage
)
135 free_page((unsigned long)bpage
->page
);
140 * We need to fit the time_stamp delta into 27 bits.
142 static inline int test_time_stamp(u64 delta
)
144 if (delta
& TS_DELTA_TEST
)
149 #define BUF_PAGE_SIZE PAGE_SIZE
152 * head_page == tail_page && head == tail then buffer is empty.
154 struct ring_buffer_per_cpu
{
156 struct ring_buffer
*buffer
;
157 spinlock_t reader_lock
; /* serialize readers */
159 struct lock_class_key lock_key
;
160 struct list_head pages
;
161 struct buffer_page
*head_page
; /* read from head */
162 struct buffer_page
*tail_page
; /* write to tail */
163 struct buffer_page
*commit_page
; /* commited pages */
164 struct buffer_page
*reader_page
;
165 unsigned long overrun
;
166 unsigned long entries
;
169 atomic_t record_disabled
;
178 atomic_t record_disabled
;
182 struct ring_buffer_per_cpu
**buffers
;
185 struct ring_buffer_iter
{
186 struct ring_buffer_per_cpu
*cpu_buffer
;
188 struct buffer_page
*head_page
;
192 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
193 #define RB_WARN_ON(buffer, cond) \
195 if (unlikely(cond)) { \
196 atomic_inc(&buffer->record_disabled); \
201 #define RB_WARN_ON_RET(buffer, cond) \
203 if (unlikely(cond)) { \
204 atomic_inc(&buffer->record_disabled); \
210 #define RB_WARN_ON_RET_INT(buffer, cond) \
212 if (unlikely(cond)) { \
213 atomic_inc(&buffer->record_disabled); \
219 #define RB_WARN_ON_RET_NULL(buffer, cond) \
221 if (unlikely(cond)) { \
222 atomic_inc(&buffer->record_disabled); \
228 #define RB_WARN_ON_ONCE(buffer, cond) \
231 if (unlikely(cond) && !once) { \
233 atomic_inc(&buffer->record_disabled); \
238 /* buffer must be ring_buffer not per_cpu */
239 #define RB_WARN_ON_UNLOCK(buffer, cond) \
241 if (unlikely(cond)) { \
242 mutex_unlock(&buffer->mutex); \
243 atomic_inc(&buffer->record_disabled); \
250 * check_pages - integrity check of buffer pages
251 * @cpu_buffer: CPU buffer with pages to test
253 * As a safty measure we check to make sure the data pages have not
256 static int rb_check_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
258 struct list_head
*head
= &cpu_buffer
->pages
;
259 struct buffer_page
*page
, *tmp
;
261 RB_WARN_ON_RET_INT(cpu_buffer
, head
->next
->prev
!= head
);
262 RB_WARN_ON_RET_INT(cpu_buffer
, head
->prev
->next
!= head
);
264 list_for_each_entry_safe(page
, tmp
, head
, list
) {
265 RB_WARN_ON_RET_INT(cpu_buffer
,
266 page
->list
.next
->prev
!= &page
->list
);
267 RB_WARN_ON_RET_INT(cpu_buffer
,
268 page
->list
.prev
->next
!= &page
->list
);
274 static int rb_allocate_pages(struct ring_buffer_per_cpu
*cpu_buffer
,
277 struct list_head
*head
= &cpu_buffer
->pages
;
278 struct buffer_page
*page
, *tmp
;
283 for (i
= 0; i
< nr_pages
; i
++) {
284 page
= kzalloc_node(ALIGN(sizeof(*page
), cache_line_size()),
285 GFP_KERNEL
, cpu_to_node(cpu_buffer
->cpu
));
288 list_add(&page
->list
, &pages
);
290 addr
= __get_free_page(GFP_KERNEL
);
293 page
->page
= (void *)addr
;
296 list_splice(&pages
, head
);
298 rb_check_pages(cpu_buffer
);
303 list_for_each_entry_safe(page
, tmp
, &pages
, list
) {
304 list_del_init(&page
->list
);
305 free_buffer_page(page
);
310 static struct ring_buffer_per_cpu
*
311 rb_allocate_cpu_buffer(struct ring_buffer
*buffer
, int cpu
)
313 struct ring_buffer_per_cpu
*cpu_buffer
;
314 struct buffer_page
*page
;
318 cpu_buffer
= kzalloc_node(ALIGN(sizeof(*cpu_buffer
), cache_line_size()),
319 GFP_KERNEL
, cpu_to_node(cpu
));
323 cpu_buffer
->cpu
= cpu
;
324 cpu_buffer
->buffer
= buffer
;
325 spin_lock_init(&cpu_buffer
->reader_lock
);
326 cpu_buffer
->lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
327 INIT_LIST_HEAD(&cpu_buffer
->pages
);
329 page
= kzalloc_node(ALIGN(sizeof(*page
), cache_line_size()),
330 GFP_KERNEL
, cpu_to_node(cpu
));
332 goto fail_free_buffer
;
334 cpu_buffer
->reader_page
= page
;
335 addr
= __get_free_page(GFP_KERNEL
);
337 goto fail_free_reader
;
338 page
->page
= (void *)addr
;
340 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
342 ret
= rb_allocate_pages(cpu_buffer
, buffer
->pages
);
344 goto fail_free_reader
;
346 cpu_buffer
->head_page
347 = list_entry(cpu_buffer
->pages
.next
, struct buffer_page
, list
);
348 cpu_buffer
->tail_page
= cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
353 free_buffer_page(cpu_buffer
->reader_page
);
360 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu
*cpu_buffer
)
362 struct list_head
*head
= &cpu_buffer
->pages
;
363 struct buffer_page
*page
, *tmp
;
365 list_del_init(&cpu_buffer
->reader_page
->list
);
366 free_buffer_page(cpu_buffer
->reader_page
);
368 list_for_each_entry_safe(page
, tmp
, head
, list
) {
369 list_del_init(&page
->list
);
370 free_buffer_page(page
);
376 * Causes compile errors if the struct buffer_page gets bigger
377 * than the struct page.
379 extern int ring_buffer_page_too_big(void);
382 * ring_buffer_alloc - allocate a new ring_buffer
383 * @size: the size in bytes that is needed.
384 * @flags: attributes to set for the ring buffer.
386 * Currently the only flag that is available is the RB_FL_OVERWRITE
387 * flag. This flag means that the buffer will overwrite old data
388 * when the buffer wraps. If this flag is not set, the buffer will
389 * drop data when the tail hits the head.
391 struct ring_buffer
*ring_buffer_alloc(unsigned long size
, unsigned flags
)
393 struct ring_buffer
*buffer
;
397 /* Paranoid! Optimizes out when all is well */
398 if (sizeof(struct buffer_page
) > sizeof(struct page
))
399 ring_buffer_page_too_big();
402 /* keep it in its own cache line */
403 buffer
= kzalloc(ALIGN(sizeof(*buffer
), cache_line_size()),
408 buffer
->pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
409 buffer
->flags
= flags
;
411 /* need at least two pages */
412 if (buffer
->pages
== 1)
415 buffer
->cpumask
= cpu_possible_map
;
416 buffer
->cpus
= nr_cpu_ids
;
418 bsize
= sizeof(void *) * nr_cpu_ids
;
419 buffer
->buffers
= kzalloc(ALIGN(bsize
, cache_line_size()),
421 if (!buffer
->buffers
)
422 goto fail_free_buffer
;
424 for_each_buffer_cpu(buffer
, cpu
) {
425 buffer
->buffers
[cpu
] =
426 rb_allocate_cpu_buffer(buffer
, cpu
);
427 if (!buffer
->buffers
[cpu
])
428 goto fail_free_buffers
;
431 mutex_init(&buffer
->mutex
);
436 for_each_buffer_cpu(buffer
, cpu
) {
437 if (buffer
->buffers
[cpu
])
438 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
440 kfree(buffer
->buffers
);
448 * ring_buffer_free - free a ring buffer.
449 * @buffer: the buffer to free.
452 ring_buffer_free(struct ring_buffer
*buffer
)
456 for_each_buffer_cpu(buffer
, cpu
)
457 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
462 static void rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
);
465 rb_remove_pages(struct ring_buffer_per_cpu
*cpu_buffer
, unsigned nr_pages
)
467 struct buffer_page
*page
;
471 atomic_inc(&cpu_buffer
->record_disabled
);
474 for (i
= 0; i
< nr_pages
; i
++) {
475 RB_WARN_ON_RET(cpu_buffer
, list_empty(&cpu_buffer
->pages
));
476 p
= cpu_buffer
->pages
.next
;
477 page
= list_entry(p
, struct buffer_page
, list
);
478 list_del_init(&page
->list
);
479 free_buffer_page(page
);
481 RB_WARN_ON_RET(cpu_buffer
, list_empty(&cpu_buffer
->pages
));
483 rb_reset_cpu(cpu_buffer
);
485 rb_check_pages(cpu_buffer
);
487 atomic_dec(&cpu_buffer
->record_disabled
);
492 rb_insert_pages(struct ring_buffer_per_cpu
*cpu_buffer
,
493 struct list_head
*pages
, unsigned nr_pages
)
495 struct buffer_page
*page
;
499 atomic_inc(&cpu_buffer
->record_disabled
);
502 for (i
= 0; i
< nr_pages
; i
++) {
503 RB_WARN_ON_RET(cpu_buffer
, list_empty(pages
));
505 page
= list_entry(p
, struct buffer_page
, list
);
506 list_del_init(&page
->list
);
507 list_add_tail(&page
->list
, &cpu_buffer
->pages
);
509 rb_reset_cpu(cpu_buffer
);
511 rb_check_pages(cpu_buffer
);
513 atomic_dec(&cpu_buffer
->record_disabled
);
517 * ring_buffer_resize - resize the ring buffer
518 * @buffer: the buffer to resize.
519 * @size: the new size.
521 * The tracer is responsible for making sure that the buffer is
522 * not being used while changing the size.
523 * Note: We may be able to change the above requirement by using
524 * RCU synchronizations.
526 * Minimum size is 2 * BUF_PAGE_SIZE.
528 * Returns -1 on failure.
530 int ring_buffer_resize(struct ring_buffer
*buffer
, unsigned long size
)
532 struct ring_buffer_per_cpu
*cpu_buffer
;
533 unsigned nr_pages
, rm_pages
, new_pages
;
534 struct buffer_page
*page
, *tmp
;
535 unsigned long buffer_size
;
540 size
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
541 size
*= BUF_PAGE_SIZE
;
542 buffer_size
= buffer
->pages
* BUF_PAGE_SIZE
;
544 /* we need a minimum of two pages */
545 if (size
< BUF_PAGE_SIZE
* 2)
546 size
= BUF_PAGE_SIZE
* 2;
548 if (size
== buffer_size
)
551 mutex_lock(&buffer
->mutex
);
553 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
555 if (size
< buffer_size
) {
557 /* easy case, just free pages */
558 RB_WARN_ON_UNLOCK(buffer
, nr_pages
>= buffer
->pages
);
560 rm_pages
= buffer
->pages
- nr_pages
;
562 for_each_buffer_cpu(buffer
, cpu
) {
563 cpu_buffer
= buffer
->buffers
[cpu
];
564 rb_remove_pages(cpu_buffer
, rm_pages
);
570 * This is a bit more difficult. We only want to add pages
571 * when we can allocate enough for all CPUs. We do this
572 * by allocating all the pages and storing them on a local
573 * link list. If we succeed in our allocation, then we
574 * add these pages to the cpu_buffers. Otherwise we just free
575 * them all and return -ENOMEM;
577 RB_WARN_ON_UNLOCK(buffer
, nr_pages
<= buffer
->pages
);
579 new_pages
= nr_pages
- buffer
->pages
;
581 for_each_buffer_cpu(buffer
, cpu
) {
582 for (i
= 0; i
< new_pages
; i
++) {
583 page
= kzalloc_node(ALIGN(sizeof(*page
),
585 GFP_KERNEL
, cpu_to_node(cpu
));
588 list_add(&page
->list
, &pages
);
589 addr
= __get_free_page(GFP_KERNEL
);
592 page
->page
= (void *)addr
;
596 for_each_buffer_cpu(buffer
, cpu
) {
597 cpu_buffer
= buffer
->buffers
[cpu
];
598 rb_insert_pages(cpu_buffer
, &pages
, new_pages
);
601 RB_WARN_ON_UNLOCK(buffer
, !list_empty(&pages
));
604 buffer
->pages
= nr_pages
;
605 mutex_unlock(&buffer
->mutex
);
610 list_for_each_entry_safe(page
, tmp
, &pages
, list
) {
611 list_del_init(&page
->list
);
612 free_buffer_page(page
);
617 static inline int rb_null_event(struct ring_buffer_event
*event
)
619 return event
->type
== RINGBUF_TYPE_PADDING
;
622 static inline void *__rb_page_index(struct buffer_page
*page
, unsigned index
)
624 return page
->page
+ index
;
627 static inline struct ring_buffer_event
*
628 rb_reader_event(struct ring_buffer_per_cpu
*cpu_buffer
)
630 return __rb_page_index(cpu_buffer
->reader_page
,
631 cpu_buffer
->reader_page
->read
);
634 static inline struct ring_buffer_event
*
635 rb_head_event(struct ring_buffer_per_cpu
*cpu_buffer
)
637 return __rb_page_index(cpu_buffer
->head_page
,
638 cpu_buffer
->head_page
->read
);
641 static inline struct ring_buffer_event
*
642 rb_iter_head_event(struct ring_buffer_iter
*iter
)
644 return __rb_page_index(iter
->head_page
, iter
->head
);
647 static inline unsigned rb_page_write(struct buffer_page
*bpage
)
649 return local_read(&bpage
->write
);
652 static inline unsigned rb_page_commit(struct buffer_page
*bpage
)
654 return local_read(&bpage
->commit
);
657 /* Size is determined by what has been commited */
658 static inline unsigned rb_page_size(struct buffer_page
*bpage
)
660 return rb_page_commit(bpage
);
663 static inline unsigned
664 rb_commit_index(struct ring_buffer_per_cpu
*cpu_buffer
)
666 return rb_page_commit(cpu_buffer
->commit_page
);
669 static inline unsigned rb_head_size(struct ring_buffer_per_cpu
*cpu_buffer
)
671 return rb_page_commit(cpu_buffer
->head_page
);
675 * When the tail hits the head and the buffer is in overwrite mode,
676 * the head jumps to the next page and all content on the previous
677 * page is discarded. But before doing so, we update the overrun
678 * variable of the buffer.
680 static void rb_update_overflow(struct ring_buffer_per_cpu
*cpu_buffer
)
682 struct ring_buffer_event
*event
;
685 for (head
= 0; head
< rb_head_size(cpu_buffer
);
686 head
+= rb_event_length(event
)) {
688 event
= __rb_page_index(cpu_buffer
->head_page
, head
);
689 RB_WARN_ON_RET(cpu_buffer
, rb_null_event(event
));
690 /* Only count data entries */
691 if (event
->type
!= RINGBUF_TYPE_DATA
)
693 cpu_buffer
->overrun
++;
694 cpu_buffer
->entries
--;
698 static inline void rb_inc_page(struct ring_buffer_per_cpu
*cpu_buffer
,
699 struct buffer_page
**page
)
701 struct list_head
*p
= (*page
)->list
.next
;
703 if (p
== &cpu_buffer
->pages
)
706 *page
= list_entry(p
, struct buffer_page
, list
);
709 static inline unsigned
710 rb_event_index(struct ring_buffer_event
*event
)
712 unsigned long addr
= (unsigned long)event
;
714 return (addr
& ~PAGE_MASK
) - (PAGE_SIZE
- BUF_PAGE_SIZE
);
718 rb_is_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
719 struct ring_buffer_event
*event
)
721 unsigned long addr
= (unsigned long)event
;
724 index
= rb_event_index(event
);
727 return cpu_buffer
->commit_page
->page
== (void *)addr
&&
728 rb_commit_index(cpu_buffer
) == index
;
732 rb_set_commit_event(struct ring_buffer_per_cpu
*cpu_buffer
,
733 struct ring_buffer_event
*event
)
735 unsigned long addr
= (unsigned long)event
;
738 index
= rb_event_index(event
);
741 while (cpu_buffer
->commit_page
->page
!= (void *)addr
) {
742 RB_WARN_ON(cpu_buffer
,
743 cpu_buffer
->commit_page
== cpu_buffer
->tail_page
);
744 cpu_buffer
->commit_page
->commit
=
745 cpu_buffer
->commit_page
->write
;
746 rb_inc_page(cpu_buffer
, &cpu_buffer
->commit_page
);
747 cpu_buffer
->write_stamp
= cpu_buffer
->commit_page
->time_stamp
;
750 /* Now set the commit to the event's index */
751 local_set(&cpu_buffer
->commit_page
->commit
, index
);
755 rb_set_commit_to_write(struct ring_buffer_per_cpu
*cpu_buffer
)
758 * We only race with interrupts and NMIs on this CPU.
759 * If we own the commit event, then we can commit
760 * all others that interrupted us, since the interruptions
761 * are in stack format (they finish before they come
762 * back to us). This allows us to do a simple loop to
763 * assign the commit to the tail.
765 while (cpu_buffer
->commit_page
!= cpu_buffer
->tail_page
) {
766 cpu_buffer
->commit_page
->commit
=
767 cpu_buffer
->commit_page
->write
;
768 rb_inc_page(cpu_buffer
, &cpu_buffer
->commit_page
);
769 cpu_buffer
->write_stamp
= cpu_buffer
->commit_page
->time_stamp
;
770 /* add barrier to keep gcc from optimizing too much */
773 while (rb_commit_index(cpu_buffer
) !=
774 rb_page_write(cpu_buffer
->commit_page
)) {
775 cpu_buffer
->commit_page
->commit
=
776 cpu_buffer
->commit_page
->write
;
781 static void rb_reset_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
783 cpu_buffer
->read_stamp
= cpu_buffer
->reader_page
->time_stamp
;
784 cpu_buffer
->reader_page
->read
= 0;
787 static inline void rb_inc_iter(struct ring_buffer_iter
*iter
)
789 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
792 * The iterator could be on the reader page (it starts there).
793 * But the head could have moved, since the reader was
794 * found. Check for this case and assign the iterator
795 * to the head page instead of next.
797 if (iter
->head_page
== cpu_buffer
->reader_page
)
798 iter
->head_page
= cpu_buffer
->head_page
;
800 rb_inc_page(cpu_buffer
, &iter
->head_page
);
802 iter
->read_stamp
= iter
->head_page
->time_stamp
;
807 * ring_buffer_update_event - update event type and data
808 * @event: the even to update
809 * @type: the type of event
810 * @length: the size of the event field in the ring buffer
812 * Update the type and data fields of the event. The length
813 * is the actual size that is written to the ring buffer,
814 * and with this, we can determine what to place into the
818 rb_update_event(struct ring_buffer_event
*event
,
819 unsigned type
, unsigned length
)
825 case RINGBUF_TYPE_PADDING
:
828 case RINGBUF_TYPE_TIME_EXTEND
:
830 (RB_LEN_TIME_EXTEND
+ (RB_ALIGNMENT
-1))
831 >> RB_ALIGNMENT_SHIFT
;
834 case RINGBUF_TYPE_TIME_STAMP
:
836 (RB_LEN_TIME_STAMP
+ (RB_ALIGNMENT
-1))
837 >> RB_ALIGNMENT_SHIFT
;
840 case RINGBUF_TYPE_DATA
:
841 length
-= RB_EVNT_HDR_SIZE
;
842 if (length
> RB_MAX_SMALL_DATA
) {
844 event
->array
[0] = length
;
847 (length
+ (RB_ALIGNMENT
-1))
848 >> RB_ALIGNMENT_SHIFT
;
855 static inline unsigned rb_calculate_event_length(unsigned length
)
857 struct ring_buffer_event event
; /* Used only for sizeof array */
859 /* zero length can cause confusions */
863 if (length
> RB_MAX_SMALL_DATA
)
864 length
+= sizeof(event
.array
[0]);
866 length
+= RB_EVNT_HDR_SIZE
;
867 length
= ALIGN(length
, RB_ALIGNMENT
);
872 static struct ring_buffer_event
*
873 __rb_reserve_next(struct ring_buffer_per_cpu
*cpu_buffer
,
874 unsigned type
, unsigned long length
, u64
*ts
)
876 struct buffer_page
*tail_page
, *head_page
, *reader_page
;
877 unsigned long tail
, write
;
878 struct ring_buffer
*buffer
= cpu_buffer
->buffer
;
879 struct ring_buffer_event
*event
;
882 tail_page
= cpu_buffer
->tail_page
;
883 write
= local_add_return(length
, &tail_page
->write
);
884 tail
= write
- length
;
886 /* See if we shot pass the end of this buffer page */
887 if (write
> BUF_PAGE_SIZE
) {
888 struct buffer_page
*next_page
= tail_page
;
890 local_irq_save(flags
);
891 __raw_spin_lock(&cpu_buffer
->lock
);
893 rb_inc_page(cpu_buffer
, &next_page
);
895 head_page
= cpu_buffer
->head_page
;
896 reader_page
= cpu_buffer
->reader_page
;
898 /* we grabbed the lock before incrementing */
899 RB_WARN_ON(cpu_buffer
, next_page
== reader_page
);
902 * If for some reason, we had an interrupt storm that made
903 * it all the way around the buffer, bail, and warn
906 if (unlikely(next_page
== cpu_buffer
->commit_page
)) {
911 if (next_page
== head_page
) {
912 if (!(buffer
->flags
& RB_FL_OVERWRITE
)) {
914 if (tail
<= BUF_PAGE_SIZE
)
915 local_set(&tail_page
->write
, tail
);
919 /* tail_page has not moved yet? */
920 if (tail_page
== cpu_buffer
->tail_page
) {
921 /* count overflows */
922 rb_update_overflow(cpu_buffer
);
924 rb_inc_page(cpu_buffer
, &head_page
);
925 cpu_buffer
->head_page
= head_page
;
926 cpu_buffer
->head_page
->read
= 0;
931 * If the tail page is still the same as what we think
932 * it is, then it is up to us to update the tail
935 if (tail_page
== cpu_buffer
->tail_page
) {
936 local_set(&next_page
->write
, 0);
937 local_set(&next_page
->commit
, 0);
938 cpu_buffer
->tail_page
= next_page
;
940 /* reread the time stamp */
941 *ts
= ring_buffer_time_stamp(cpu_buffer
->cpu
);
942 cpu_buffer
->tail_page
->time_stamp
= *ts
;
946 * The actual tail page has moved forward.
948 if (tail
< BUF_PAGE_SIZE
) {
949 /* Mark the rest of the page with padding */
950 event
= __rb_page_index(tail_page
, tail
);
951 event
->type
= RINGBUF_TYPE_PADDING
;
954 if (tail
<= BUF_PAGE_SIZE
)
955 /* Set the write back to the previous setting */
956 local_set(&tail_page
->write
, tail
);
959 * If this was a commit entry that failed,
962 if (tail_page
== cpu_buffer
->commit_page
&&
963 tail
== rb_commit_index(cpu_buffer
)) {
964 rb_set_commit_to_write(cpu_buffer
);
967 __raw_spin_unlock(&cpu_buffer
->lock
);
968 local_irq_restore(flags
);
970 /* fail and let the caller try again */
971 return ERR_PTR(-EAGAIN
);
974 /* We reserved something on the buffer */
976 RB_WARN_ON_RET_NULL(cpu_buffer
, write
> BUF_PAGE_SIZE
);
978 event
= __rb_page_index(tail_page
, tail
);
979 rb_update_event(event
, type
, length
);
982 * If this is a commit and the tail is zero, then update
983 * this page's time stamp.
985 if (!tail
&& rb_is_commit(cpu_buffer
, event
))
986 cpu_buffer
->commit_page
->time_stamp
= *ts
;
991 __raw_spin_unlock(&cpu_buffer
->lock
);
992 local_irq_restore(flags
);
997 rb_add_time_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
1000 struct ring_buffer_event
*event
;
1004 if (unlikely(*delta
> (1ULL << 59) && !once
++)) {
1005 printk(KERN_WARNING
"Delta way too big! %llu"
1006 " ts=%llu write stamp = %llu\n",
1007 (unsigned long long)*delta
,
1008 (unsigned long long)*ts
,
1009 (unsigned long long)cpu_buffer
->write_stamp
);
1014 * The delta is too big, we to add a
1017 event
= __rb_reserve_next(cpu_buffer
,
1018 RINGBUF_TYPE_TIME_EXTEND
,
1024 if (PTR_ERR(event
) == -EAGAIN
)
1027 /* Only a commited time event can update the write stamp */
1028 if (rb_is_commit(cpu_buffer
, event
)) {
1030 * If this is the first on the page, then we need to
1031 * update the page itself, and just put in a zero.
1033 if (rb_event_index(event
)) {
1034 event
->time_delta
= *delta
& TS_MASK
;
1035 event
->array
[0] = *delta
>> TS_SHIFT
;
1037 cpu_buffer
->commit_page
->time_stamp
= *ts
;
1038 event
->time_delta
= 0;
1039 event
->array
[0] = 0;
1041 cpu_buffer
->write_stamp
= *ts
;
1042 /* let the caller know this was the commit */
1045 /* Darn, this is just wasted space */
1046 event
->time_delta
= 0;
1047 event
->array
[0] = 0;
1056 static struct ring_buffer_event
*
1057 rb_reserve_next_event(struct ring_buffer_per_cpu
*cpu_buffer
,
1058 unsigned type
, unsigned long length
)
1060 struct ring_buffer_event
*event
;
1067 * We allow for interrupts to reenter here and do a trace.
1068 * If one does, it will cause this original code to loop
1069 * back here. Even with heavy interrupts happening, this
1070 * should only happen a few times in a row. If this happens
1071 * 1000 times in a row, there must be either an interrupt
1072 * storm or we have something buggy.
1075 if (unlikely(++nr_loops
> 1000)) {
1076 RB_WARN_ON(cpu_buffer
, 1);
1080 ts
= ring_buffer_time_stamp(cpu_buffer
->cpu
);
1083 * Only the first commit can update the timestamp.
1084 * Yes there is a race here. If an interrupt comes in
1085 * just after the conditional and it traces too, then it
1086 * will also check the deltas. More than one timestamp may
1087 * also be made. But only the entry that did the actual
1088 * commit will be something other than zero.
1090 if (cpu_buffer
->tail_page
== cpu_buffer
->commit_page
&&
1091 rb_page_write(cpu_buffer
->tail_page
) ==
1092 rb_commit_index(cpu_buffer
)) {
1094 delta
= ts
- cpu_buffer
->write_stamp
;
1096 /* make sure this delta is calculated here */
1099 /* Did the write stamp get updated already? */
1100 if (unlikely(ts
< cpu_buffer
->write_stamp
))
1103 if (test_time_stamp(delta
)) {
1105 commit
= rb_add_time_stamp(cpu_buffer
, &ts
, &delta
);
1107 if (commit
== -EBUSY
)
1110 if (commit
== -EAGAIN
)
1113 RB_WARN_ON(cpu_buffer
, commit
< 0);
1116 /* Non commits have zero deltas */
1119 event
= __rb_reserve_next(cpu_buffer
, type
, length
, &ts
);
1120 if (PTR_ERR(event
) == -EAGAIN
)
1124 if (unlikely(commit
))
1126 * Ouch! We needed a timestamp and it was commited. But
1127 * we didn't get our event reserved.
1129 rb_set_commit_to_write(cpu_buffer
);
1134 * If the timestamp was commited, make the commit our entry
1135 * now so that we will update it when needed.
1138 rb_set_commit_event(cpu_buffer
, event
);
1139 else if (!rb_is_commit(cpu_buffer
, event
))
1142 event
->time_delta
= delta
;
1147 static DEFINE_PER_CPU(int, rb_need_resched
);
1150 * ring_buffer_lock_reserve - reserve a part of the buffer
1151 * @buffer: the ring buffer to reserve from
1152 * @length: the length of the data to reserve (excluding event header)
1153 * @flags: a pointer to save the interrupt flags
1155 * Returns a reseverd event on the ring buffer to copy directly to.
1156 * The user of this interface will need to get the body to write into
1157 * and can use the ring_buffer_event_data() interface.
1159 * The length is the length of the data needed, not the event length
1160 * which also includes the event header.
1162 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1163 * If NULL is returned, then nothing has been allocated or locked.
1165 struct ring_buffer_event
*
1166 ring_buffer_lock_reserve(struct ring_buffer
*buffer
,
1167 unsigned long length
,
1168 unsigned long *flags
)
1170 struct ring_buffer_per_cpu
*cpu_buffer
;
1171 struct ring_buffer_event
*event
;
1174 if (atomic_read(&buffer
->record_disabled
))
1177 /* If we are tracing schedule, we don't want to recurse */
1178 resched
= ftrace_preempt_disable();
1180 cpu
= raw_smp_processor_id();
1182 if (!cpu_isset(cpu
, buffer
->cpumask
))
1185 cpu_buffer
= buffer
->buffers
[cpu
];
1187 if (atomic_read(&cpu_buffer
->record_disabled
))
1190 length
= rb_calculate_event_length(length
);
1191 if (length
> BUF_PAGE_SIZE
)
1194 event
= rb_reserve_next_event(cpu_buffer
, RINGBUF_TYPE_DATA
, length
);
1199 * Need to store resched state on this cpu.
1200 * Only the first needs to.
1203 if (preempt_count() == 1)
1204 per_cpu(rb_need_resched
, cpu
) = resched
;
1209 ftrace_preempt_enable(resched
);
1213 static void rb_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
1214 struct ring_buffer_event
*event
)
1216 cpu_buffer
->entries
++;
1218 /* Only process further if we own the commit */
1219 if (!rb_is_commit(cpu_buffer
, event
))
1222 cpu_buffer
->write_stamp
+= event
->time_delta
;
1224 rb_set_commit_to_write(cpu_buffer
);
1228 * ring_buffer_unlock_commit - commit a reserved
1229 * @buffer: The buffer to commit to
1230 * @event: The event pointer to commit.
1231 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1233 * This commits the data to the ring buffer, and releases any locks held.
1235 * Must be paired with ring_buffer_lock_reserve.
1237 int ring_buffer_unlock_commit(struct ring_buffer
*buffer
,
1238 struct ring_buffer_event
*event
,
1239 unsigned long flags
)
1241 struct ring_buffer_per_cpu
*cpu_buffer
;
1242 int cpu
= raw_smp_processor_id();
1244 cpu_buffer
= buffer
->buffers
[cpu
];
1246 rb_commit(cpu_buffer
, event
);
1249 * Only the last preempt count needs to restore preemption.
1251 if (preempt_count() == 1)
1252 ftrace_preempt_enable(per_cpu(rb_need_resched
, cpu
));
1254 preempt_enable_no_resched_notrace();
1260 * ring_buffer_write - write data to the buffer without reserving
1261 * @buffer: The ring buffer to write to.
1262 * @length: The length of the data being written (excluding the event header)
1263 * @data: The data to write to the buffer.
1265 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1266 * one function. If you already have the data to write to the buffer, it
1267 * may be easier to simply call this function.
1269 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1270 * and not the length of the event which would hold the header.
1272 int ring_buffer_write(struct ring_buffer
*buffer
,
1273 unsigned long length
,
1276 struct ring_buffer_per_cpu
*cpu_buffer
;
1277 struct ring_buffer_event
*event
;
1278 unsigned long event_length
;
1283 if (atomic_read(&buffer
->record_disabled
))
1286 resched
= ftrace_preempt_disable();
1288 cpu
= raw_smp_processor_id();
1290 if (!cpu_isset(cpu
, buffer
->cpumask
))
1293 cpu_buffer
= buffer
->buffers
[cpu
];
1295 if (atomic_read(&cpu_buffer
->record_disabled
))
1298 event_length
= rb_calculate_event_length(length
);
1299 event
= rb_reserve_next_event(cpu_buffer
,
1300 RINGBUF_TYPE_DATA
, event_length
);
1304 body
= rb_event_data(event
);
1306 memcpy(body
, data
, length
);
1308 rb_commit(cpu_buffer
, event
);
1312 ftrace_preempt_enable(resched
);
1317 static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu
*cpu_buffer
)
1319 struct buffer_page
*reader
= cpu_buffer
->reader_page
;
1320 struct buffer_page
*head
= cpu_buffer
->head_page
;
1321 struct buffer_page
*commit
= cpu_buffer
->commit_page
;
1323 return reader
->read
== rb_page_commit(reader
) &&
1324 (commit
== reader
||
1326 head
->read
== rb_page_commit(commit
)));
1330 * ring_buffer_record_disable - stop all writes into the buffer
1331 * @buffer: The ring buffer to stop writes to.
1333 * This prevents all writes to the buffer. Any attempt to write
1334 * to the buffer after this will fail and return NULL.
1336 * The caller should call synchronize_sched() after this.
1338 void ring_buffer_record_disable(struct ring_buffer
*buffer
)
1340 atomic_inc(&buffer
->record_disabled
);
1344 * ring_buffer_record_enable - enable writes to the buffer
1345 * @buffer: The ring buffer to enable writes
1347 * Note, multiple disables will need the same number of enables
1348 * to truely enable the writing (much like preempt_disable).
1350 void ring_buffer_record_enable(struct ring_buffer
*buffer
)
1352 atomic_dec(&buffer
->record_disabled
);
1356 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1357 * @buffer: The ring buffer to stop writes to.
1358 * @cpu: The CPU buffer to stop
1360 * This prevents all writes to the buffer. Any attempt to write
1361 * to the buffer after this will fail and return NULL.
1363 * The caller should call synchronize_sched() after this.
1365 void ring_buffer_record_disable_cpu(struct ring_buffer
*buffer
, int cpu
)
1367 struct ring_buffer_per_cpu
*cpu_buffer
;
1369 if (!cpu_isset(cpu
, buffer
->cpumask
))
1372 cpu_buffer
= buffer
->buffers
[cpu
];
1373 atomic_inc(&cpu_buffer
->record_disabled
);
1377 * ring_buffer_record_enable_cpu - enable writes to the buffer
1378 * @buffer: The ring buffer to enable writes
1379 * @cpu: The CPU to enable.
1381 * Note, multiple disables will need the same number of enables
1382 * to truely enable the writing (much like preempt_disable).
1384 void ring_buffer_record_enable_cpu(struct ring_buffer
*buffer
, int cpu
)
1386 struct ring_buffer_per_cpu
*cpu_buffer
;
1388 if (!cpu_isset(cpu
, buffer
->cpumask
))
1391 cpu_buffer
= buffer
->buffers
[cpu
];
1392 atomic_dec(&cpu_buffer
->record_disabled
);
1396 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1397 * @buffer: The ring buffer
1398 * @cpu: The per CPU buffer to get the entries from.
1400 unsigned long ring_buffer_entries_cpu(struct ring_buffer
*buffer
, int cpu
)
1402 struct ring_buffer_per_cpu
*cpu_buffer
;
1404 if (!cpu_isset(cpu
, buffer
->cpumask
))
1407 cpu_buffer
= buffer
->buffers
[cpu
];
1408 return cpu_buffer
->entries
;
1412 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1413 * @buffer: The ring buffer
1414 * @cpu: The per CPU buffer to get the number of overruns from
1416 unsigned long ring_buffer_overrun_cpu(struct ring_buffer
*buffer
, int cpu
)
1418 struct ring_buffer_per_cpu
*cpu_buffer
;
1420 if (!cpu_isset(cpu
, buffer
->cpumask
))
1423 cpu_buffer
= buffer
->buffers
[cpu
];
1424 return cpu_buffer
->overrun
;
1428 * ring_buffer_entries - get the number of entries in a buffer
1429 * @buffer: The ring buffer
1431 * Returns the total number of entries in the ring buffer
1434 unsigned long ring_buffer_entries(struct ring_buffer
*buffer
)
1436 struct ring_buffer_per_cpu
*cpu_buffer
;
1437 unsigned long entries
= 0;
1440 /* if you care about this being correct, lock the buffer */
1441 for_each_buffer_cpu(buffer
, cpu
) {
1442 cpu_buffer
= buffer
->buffers
[cpu
];
1443 entries
+= cpu_buffer
->entries
;
1450 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1451 * @buffer: The ring buffer
1453 * Returns the total number of overruns in the ring buffer
1456 unsigned long ring_buffer_overruns(struct ring_buffer
*buffer
)
1458 struct ring_buffer_per_cpu
*cpu_buffer
;
1459 unsigned long overruns
= 0;
1462 /* if you care about this being correct, lock the buffer */
1463 for_each_buffer_cpu(buffer
, cpu
) {
1464 cpu_buffer
= buffer
->buffers
[cpu
];
1465 overruns
+= cpu_buffer
->overrun
;
1472 * ring_buffer_iter_reset - reset an iterator
1473 * @iter: The iterator to reset
1475 * Resets the iterator, so that it will start from the beginning
1478 void ring_buffer_iter_reset(struct ring_buffer_iter
*iter
)
1480 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1481 unsigned long flags
;
1483 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
1485 /* Iterator usage is expected to have record disabled */
1486 if (list_empty(&cpu_buffer
->reader_page
->list
)) {
1487 iter
->head_page
= cpu_buffer
->head_page
;
1488 iter
->head
= cpu_buffer
->head_page
->read
;
1490 iter
->head_page
= cpu_buffer
->reader_page
;
1491 iter
->head
= cpu_buffer
->reader_page
->read
;
1494 iter
->read_stamp
= cpu_buffer
->read_stamp
;
1496 iter
->read_stamp
= iter
->head_page
->time_stamp
;
1498 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
1502 * ring_buffer_iter_empty - check if an iterator has no more to read
1503 * @iter: The iterator to check
1505 int ring_buffer_iter_empty(struct ring_buffer_iter
*iter
)
1507 struct ring_buffer_per_cpu
*cpu_buffer
;
1509 cpu_buffer
= iter
->cpu_buffer
;
1511 return iter
->head_page
== cpu_buffer
->commit_page
&&
1512 iter
->head
== rb_commit_index(cpu_buffer
);
1516 rb_update_read_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
1517 struct ring_buffer_event
*event
)
1521 switch (event
->type
) {
1522 case RINGBUF_TYPE_PADDING
:
1525 case RINGBUF_TYPE_TIME_EXTEND
:
1526 delta
= event
->array
[0];
1528 delta
+= event
->time_delta
;
1529 cpu_buffer
->read_stamp
+= delta
;
1532 case RINGBUF_TYPE_TIME_STAMP
:
1533 /* FIXME: not implemented */
1536 case RINGBUF_TYPE_DATA
:
1537 cpu_buffer
->read_stamp
+= event
->time_delta
;
1547 rb_update_iter_read_stamp(struct ring_buffer_iter
*iter
,
1548 struct ring_buffer_event
*event
)
1552 switch (event
->type
) {
1553 case RINGBUF_TYPE_PADDING
:
1556 case RINGBUF_TYPE_TIME_EXTEND
:
1557 delta
= event
->array
[0];
1559 delta
+= event
->time_delta
;
1560 iter
->read_stamp
+= delta
;
1563 case RINGBUF_TYPE_TIME_STAMP
:
1564 /* FIXME: not implemented */
1567 case RINGBUF_TYPE_DATA
:
1568 iter
->read_stamp
+= event
->time_delta
;
1577 static struct buffer_page
*
1578 rb_get_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
1580 struct buffer_page
*reader
= NULL
;
1581 unsigned long flags
;
1584 local_irq_save(flags
);
1585 __raw_spin_lock(&cpu_buffer
->lock
);
1589 * This should normally only loop twice. But because the
1590 * start of the reader inserts an empty page, it causes
1591 * a case where we will loop three times. There should be no
1592 * reason to loop four times (that I know of).
1594 if (unlikely(++nr_loops
> 3)) {
1595 RB_WARN_ON(cpu_buffer
, 1);
1600 reader
= cpu_buffer
->reader_page
;
1602 /* If there's more to read, return this page */
1603 if (cpu_buffer
->reader_page
->read
< rb_page_size(reader
))
1606 /* Never should we have an index greater than the size */
1607 RB_WARN_ON(cpu_buffer
,
1608 cpu_buffer
->reader_page
->read
> rb_page_size(reader
));
1610 /* check if we caught up to the tail */
1612 if (cpu_buffer
->commit_page
== cpu_buffer
->reader_page
)
1616 * Splice the empty reader page into the list around the head.
1617 * Reset the reader page to size zero.
1620 reader
= cpu_buffer
->head_page
;
1621 cpu_buffer
->reader_page
->list
.next
= reader
->list
.next
;
1622 cpu_buffer
->reader_page
->list
.prev
= reader
->list
.prev
;
1624 local_set(&cpu_buffer
->reader_page
->write
, 0);
1625 local_set(&cpu_buffer
->reader_page
->commit
, 0);
1627 /* Make the reader page now replace the head */
1628 reader
->list
.prev
->next
= &cpu_buffer
->reader_page
->list
;
1629 reader
->list
.next
->prev
= &cpu_buffer
->reader_page
->list
;
1632 * If the tail is on the reader, then we must set the head
1633 * to the inserted page, otherwise we set it one before.
1635 cpu_buffer
->head_page
= cpu_buffer
->reader_page
;
1637 if (cpu_buffer
->commit_page
!= reader
)
1638 rb_inc_page(cpu_buffer
, &cpu_buffer
->head_page
);
1640 /* Finally update the reader page to the new head */
1641 cpu_buffer
->reader_page
= reader
;
1642 rb_reset_reader_page(cpu_buffer
);
1647 __raw_spin_unlock(&cpu_buffer
->lock
);
1648 local_irq_restore(flags
);
1653 static void rb_advance_reader(struct ring_buffer_per_cpu
*cpu_buffer
)
1655 struct ring_buffer_event
*event
;
1656 struct buffer_page
*reader
;
1659 reader
= rb_get_reader_page(cpu_buffer
);
1661 /* This function should not be called when buffer is empty */
1662 RB_WARN_ON_RET(cpu_buffer
, !reader
);
1664 event
= rb_reader_event(cpu_buffer
);
1666 if (event
->type
== RINGBUF_TYPE_DATA
)
1667 cpu_buffer
->entries
--;
1669 rb_update_read_stamp(cpu_buffer
, event
);
1671 length
= rb_event_length(event
);
1672 cpu_buffer
->reader_page
->read
+= length
;
1675 static void rb_advance_iter(struct ring_buffer_iter
*iter
)
1677 struct ring_buffer
*buffer
;
1678 struct ring_buffer_per_cpu
*cpu_buffer
;
1679 struct ring_buffer_event
*event
;
1682 cpu_buffer
= iter
->cpu_buffer
;
1683 buffer
= cpu_buffer
->buffer
;
1686 * Check if we are at the end of the buffer.
1688 if (iter
->head
>= rb_page_size(iter
->head_page
)) {
1689 RB_WARN_ON_RET(buffer
,
1690 iter
->head_page
== cpu_buffer
->commit_page
);
1695 event
= rb_iter_head_event(iter
);
1697 length
= rb_event_length(event
);
1700 * This should not be called to advance the header if we are
1701 * at the tail of the buffer.
1703 RB_WARN_ON_RET(cpu_buffer
,
1704 (iter
->head_page
== cpu_buffer
->commit_page
) &&
1705 (iter
->head
+ length
> rb_commit_index(cpu_buffer
)));
1707 rb_update_iter_read_stamp(iter
, event
);
1709 iter
->head
+= length
;
1711 /* check for end of page padding */
1712 if ((iter
->head
>= rb_page_size(iter
->head_page
)) &&
1713 (iter
->head_page
!= cpu_buffer
->commit_page
))
1714 rb_advance_iter(iter
);
1717 static struct ring_buffer_event
*
1718 rb_buffer_peek(struct ring_buffer
*buffer
, int cpu
, u64
*ts
)
1720 struct ring_buffer_per_cpu
*cpu_buffer
;
1721 struct ring_buffer_event
*event
;
1722 struct buffer_page
*reader
;
1725 if (!cpu_isset(cpu
, buffer
->cpumask
))
1728 cpu_buffer
= buffer
->buffers
[cpu
];
1732 * We repeat when a timestamp is encountered. It is possible
1733 * to get multiple timestamps from an interrupt entering just
1734 * as one timestamp is about to be written. The max times
1735 * that this can happen is the number of nested interrupts we
1736 * can have. Nesting 10 deep of interrupts is clearly
1739 if (unlikely(++nr_loops
> 10)) {
1740 RB_WARN_ON(cpu_buffer
, 1);
1744 reader
= rb_get_reader_page(cpu_buffer
);
1748 event
= rb_reader_event(cpu_buffer
);
1750 switch (event
->type
) {
1751 case RINGBUF_TYPE_PADDING
:
1752 RB_WARN_ON(cpu_buffer
, 1);
1753 rb_advance_reader(cpu_buffer
);
1756 case RINGBUF_TYPE_TIME_EXTEND
:
1757 /* Internal data, OK to advance */
1758 rb_advance_reader(cpu_buffer
);
1761 case RINGBUF_TYPE_TIME_STAMP
:
1762 /* FIXME: not implemented */
1763 rb_advance_reader(cpu_buffer
);
1766 case RINGBUF_TYPE_DATA
:
1768 *ts
= cpu_buffer
->read_stamp
+ event
->time_delta
;
1769 ring_buffer_normalize_time_stamp(cpu_buffer
->cpu
, ts
);
1780 static struct ring_buffer_event
*
1781 rb_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
1783 struct ring_buffer
*buffer
;
1784 struct ring_buffer_per_cpu
*cpu_buffer
;
1785 struct ring_buffer_event
*event
;
1788 if (ring_buffer_iter_empty(iter
))
1791 cpu_buffer
= iter
->cpu_buffer
;
1792 buffer
= cpu_buffer
->buffer
;
1796 * We repeat when a timestamp is encountered. It is possible
1797 * to get multiple timestamps from an interrupt entering just
1798 * as one timestamp is about to be written. The max times
1799 * that this can happen is the number of nested interrupts we
1800 * can have. Nesting 10 deep of interrupts is clearly
1803 if (unlikely(++nr_loops
> 10)) {
1804 RB_WARN_ON(cpu_buffer
, 1);
1808 if (rb_per_cpu_empty(cpu_buffer
))
1811 event
= rb_iter_head_event(iter
);
1813 switch (event
->type
) {
1814 case RINGBUF_TYPE_PADDING
:
1818 case RINGBUF_TYPE_TIME_EXTEND
:
1819 /* Internal data, OK to advance */
1820 rb_advance_iter(iter
);
1823 case RINGBUF_TYPE_TIME_STAMP
:
1824 /* FIXME: not implemented */
1825 rb_advance_iter(iter
);
1828 case RINGBUF_TYPE_DATA
:
1830 *ts
= iter
->read_stamp
+ event
->time_delta
;
1831 ring_buffer_normalize_time_stamp(cpu_buffer
->cpu
, ts
);
1843 * ring_buffer_peek - peek at the next event to be read
1844 * @buffer: The ring buffer to read
1845 * @cpu: The cpu to peak at
1846 * @ts: The timestamp counter of this event.
1848 * This will return the event that will be read next, but does
1849 * not consume the data.
1851 struct ring_buffer_event
*
1852 ring_buffer_peek(struct ring_buffer
*buffer
, int cpu
, u64
*ts
)
1854 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
1855 struct ring_buffer_event
*event
;
1856 unsigned long flags
;
1858 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
1859 event
= rb_buffer_peek(buffer
, cpu
, ts
);
1860 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
1866 * ring_buffer_iter_peek - peek at the next event to be read
1867 * @iter: The ring buffer iterator
1868 * @ts: The timestamp counter of this event.
1870 * This will return the event that will be read next, but does
1871 * not increment the iterator.
1873 struct ring_buffer_event
*
1874 ring_buffer_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
1876 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1877 struct ring_buffer_event
*event
;
1878 unsigned long flags
;
1880 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
1881 event
= rb_iter_peek(iter
, ts
);
1882 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
1888 * ring_buffer_consume - return an event and consume it
1889 * @buffer: The ring buffer to get the next event from
1891 * Returns the next event in the ring buffer, and that event is consumed.
1892 * Meaning, that sequential reads will keep returning a different event,
1893 * and eventually empty the ring buffer if the producer is slower.
1895 struct ring_buffer_event
*
1896 ring_buffer_consume(struct ring_buffer
*buffer
, int cpu
, u64
*ts
)
1898 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
1899 struct ring_buffer_event
*event
;
1900 unsigned long flags
;
1902 if (!cpu_isset(cpu
, buffer
->cpumask
))
1905 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
1907 event
= rb_buffer_peek(buffer
, cpu
, ts
);
1911 rb_advance_reader(cpu_buffer
);
1914 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
1920 * ring_buffer_read_start - start a non consuming read of the buffer
1921 * @buffer: The ring buffer to read from
1922 * @cpu: The cpu buffer to iterate over
1924 * This starts up an iteration through the buffer. It also disables
1925 * the recording to the buffer until the reading is finished.
1926 * This prevents the reading from being corrupted. This is not
1927 * a consuming read, so a producer is not expected.
1929 * Must be paired with ring_buffer_finish.
1931 struct ring_buffer_iter
*
1932 ring_buffer_read_start(struct ring_buffer
*buffer
, int cpu
)
1934 struct ring_buffer_per_cpu
*cpu_buffer
;
1935 struct ring_buffer_iter
*iter
;
1936 unsigned long flags
;
1938 if (!cpu_isset(cpu
, buffer
->cpumask
))
1941 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
1945 cpu_buffer
= buffer
->buffers
[cpu
];
1947 iter
->cpu_buffer
= cpu_buffer
;
1949 atomic_inc(&cpu_buffer
->record_disabled
);
1950 synchronize_sched();
1952 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
1953 __raw_spin_lock(&cpu_buffer
->lock
);
1954 ring_buffer_iter_reset(iter
);
1955 __raw_spin_unlock(&cpu_buffer
->lock
);
1956 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
1962 * ring_buffer_finish - finish reading the iterator of the buffer
1963 * @iter: The iterator retrieved by ring_buffer_start
1965 * This re-enables the recording to the buffer, and frees the
1969 ring_buffer_read_finish(struct ring_buffer_iter
*iter
)
1971 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1973 atomic_dec(&cpu_buffer
->record_disabled
);
1978 * ring_buffer_read - read the next item in the ring buffer by the iterator
1979 * @iter: The ring buffer iterator
1980 * @ts: The time stamp of the event read.
1982 * This reads the next event in the ring buffer and increments the iterator.
1984 struct ring_buffer_event
*
1985 ring_buffer_read(struct ring_buffer_iter
*iter
, u64
*ts
)
1987 struct ring_buffer_event
*event
;
1988 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1989 unsigned long flags
;
1991 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
1992 event
= rb_iter_peek(iter
, ts
);
1996 rb_advance_iter(iter
);
1998 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2004 * ring_buffer_size - return the size of the ring buffer (in bytes)
2005 * @buffer: The ring buffer.
2007 unsigned long ring_buffer_size(struct ring_buffer
*buffer
)
2009 return BUF_PAGE_SIZE
* buffer
->pages
;
2013 rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
)
2015 cpu_buffer
->head_page
2016 = list_entry(cpu_buffer
->pages
.next
, struct buffer_page
, list
);
2017 local_set(&cpu_buffer
->head_page
->write
, 0);
2018 local_set(&cpu_buffer
->head_page
->commit
, 0);
2020 cpu_buffer
->head_page
->read
= 0;
2022 cpu_buffer
->tail_page
= cpu_buffer
->head_page
;
2023 cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
2025 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
2026 local_set(&cpu_buffer
->reader_page
->write
, 0);
2027 local_set(&cpu_buffer
->reader_page
->commit
, 0);
2028 cpu_buffer
->reader_page
->read
= 0;
2030 cpu_buffer
->overrun
= 0;
2031 cpu_buffer
->entries
= 0;
2035 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2036 * @buffer: The ring buffer to reset a per cpu buffer of
2037 * @cpu: The CPU buffer to be reset
2039 void ring_buffer_reset_cpu(struct ring_buffer
*buffer
, int cpu
)
2041 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
2042 unsigned long flags
;
2044 if (!cpu_isset(cpu
, buffer
->cpumask
))
2047 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2049 __raw_spin_lock(&cpu_buffer
->lock
);
2051 rb_reset_cpu(cpu_buffer
);
2053 __raw_spin_unlock(&cpu_buffer
->lock
);
2055 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2059 * ring_buffer_reset - reset a ring buffer
2060 * @buffer: The ring buffer to reset all cpu buffers
2062 void ring_buffer_reset(struct ring_buffer
*buffer
)
2066 for_each_buffer_cpu(buffer
, cpu
)
2067 ring_buffer_reset_cpu(buffer
, cpu
);
2071 * rind_buffer_empty - is the ring buffer empty?
2072 * @buffer: The ring buffer to test
2074 int ring_buffer_empty(struct ring_buffer
*buffer
)
2076 struct ring_buffer_per_cpu
*cpu_buffer
;
2079 /* yes this is racy, but if you don't like the race, lock the buffer */
2080 for_each_buffer_cpu(buffer
, cpu
) {
2081 cpu_buffer
= buffer
->buffers
[cpu
];
2082 if (!rb_per_cpu_empty(cpu_buffer
))
2089 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2090 * @buffer: The ring buffer
2091 * @cpu: The CPU buffer to test
2093 int ring_buffer_empty_cpu(struct ring_buffer
*buffer
, int cpu
)
2095 struct ring_buffer_per_cpu
*cpu_buffer
;
2097 if (!cpu_isset(cpu
, buffer
->cpumask
))
2100 cpu_buffer
= buffer
->buffers
[cpu
];
2101 return rb_per_cpu_empty(cpu_buffer
);
2105 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2106 * @buffer_a: One buffer to swap with
2107 * @buffer_b: The other buffer to swap with
2109 * This function is useful for tracers that want to take a "snapshot"
2110 * of a CPU buffer and has another back up buffer lying around.
2111 * it is expected that the tracer handles the cpu buffer not being
2112 * used at the moment.
2114 int ring_buffer_swap_cpu(struct ring_buffer
*buffer_a
,
2115 struct ring_buffer
*buffer_b
, int cpu
)
2117 struct ring_buffer_per_cpu
*cpu_buffer_a
;
2118 struct ring_buffer_per_cpu
*cpu_buffer_b
;
2120 if (!cpu_isset(cpu
, buffer_a
->cpumask
) ||
2121 !cpu_isset(cpu
, buffer_b
->cpumask
))
2124 /* At least make sure the two buffers are somewhat the same */
2125 if (buffer_a
->size
!= buffer_b
->size
||
2126 buffer_a
->pages
!= buffer_b
->pages
)
2129 cpu_buffer_a
= buffer_a
->buffers
[cpu
];
2130 cpu_buffer_b
= buffer_b
->buffers
[cpu
];
2133 * We can't do a synchronize_sched here because this
2134 * function can be called in atomic context.
2135 * Normally this will be called from the same CPU as cpu.
2136 * If not it's up to the caller to protect this.
2138 atomic_inc(&cpu_buffer_a
->record_disabled
);
2139 atomic_inc(&cpu_buffer_b
->record_disabled
);
2141 buffer_a
->buffers
[cpu
] = cpu_buffer_b
;
2142 buffer_b
->buffers
[cpu
] = cpu_buffer_a
;
2144 cpu_buffer_b
->buffer
= buffer_a
;
2145 cpu_buffer_a
->buffer
= buffer_b
;
2147 atomic_dec(&cpu_buffer_a
->record_disabled
);
2148 atomic_dec(&cpu_buffer_b
->record_disabled
);