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 /* Global flag to disable all recording to ring buffers */
22 static int ring_buffers_off __read_mostly;
25 * tracing_on - enable all tracing buffers
27 * This function enables all tracing buffers that may have been
28 * disabled with tracing_off.
36 * tracing_off - turn off all tracing buffers
38 * This function stops all tracing buffers from recording data.
39 * It does not disable any overhead the tracers themselves may
40 * be causing. This function simply causes all recording to
41 * the ring buffers to fail.
43 void tracing_off(void)
50 /* Up this if you want to test the TIME_EXTENTS and normalization */
54 u64 ring_buffer_time_stamp(int cpu)
58 preempt_disable_notrace();
59 /* shift to debug/test normalization and TIME_EXTENTS */
60 time = sched_clock() << DEBUG_SHIFT;
61 preempt_enable_notrace();
66 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
68 /* Just stupid testing the normalize function and deltas */
72 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
73 #define RB_ALIGNMENT_SHIFT 2
74 #define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT)
75 #define RB_MAX_SMALL_DATA 28
78 RB_LEN_TIME_EXTEND = 8,
79 RB_LEN_TIME_STAMP = 16,
82 /* inline for ring buffer fast paths */
83 static inline unsigned
84 rb_event_length(struct ring_buffer_event *event)
88 switch (event->type) {
89 case RINGBUF_TYPE_PADDING:
93 case RINGBUF_TYPE_TIME_EXTEND:
94 return RB_LEN_TIME_EXTEND;
96 case RINGBUF_TYPE_TIME_STAMP:
97 return RB_LEN_TIME_STAMP;
99 case RINGBUF_TYPE_DATA:
101 length = event->len << RB_ALIGNMENT_SHIFT;
103 length = event->array[0];
104 return length + RB_EVNT_HDR_SIZE;
113 * ring_buffer_event_length - return the length of the event
114 * @event: the event to get the length of
116 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
118 return rb_event_length(event);
121 /* inline for ring buffer fast paths */
123 rb_event_data(struct ring_buffer_event *event)
125 BUG_ON(event->type != RINGBUF_TYPE_DATA);
126 /* If length is in len field, then array[0] has the data */
128 return (void *)&event->array[0];
129 /* Otherwise length is in array[0] and array[1] has the data */
130 return (void *)&event->array[1];
134 * ring_buffer_event_data - return the data of the event
135 * @event: the event to get the data from
137 void *ring_buffer_event_data(struct ring_buffer_event *event)
139 return rb_event_data(event);
142 #define for_each_buffer_cpu(buffer, cpu) \
143 for_each_cpu_mask(cpu, buffer->cpumask)
146 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
147 #define TS_DELTA_TEST (~TS_MASK)
150 * This hack stolen from mm/slob.c.
151 * We can store per page timing information in the page frame of the page.
152 * Thanks to Peter Zijlstra for suggesting this idea.
155 u64 time_stamp; /* page time stamp */
156 local_t write; /* index for next write */
157 local_t commit; /* write commited index */
158 unsigned read; /* index for next read */
159 struct list_head list; /* list of free pages */
160 void *page; /* Actual data page */
164 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
167 static inline void free_buffer_page(struct buffer_page *bpage)
170 free_page((unsigned long)bpage->page);
175 * We need to fit the time_stamp delta into 27 bits.
177 static inline int test_time_stamp(u64 delta)
179 if (delta & TS_DELTA_TEST)
184 #define BUF_PAGE_SIZE PAGE_SIZE
187 * head_page == tail_page && head == tail then buffer is empty.
189 struct ring_buffer_per_cpu {
191 struct ring_buffer *buffer;
192 spinlock_t reader_lock; /* serialize readers */
194 struct lock_class_key lock_key;
195 struct list_head pages;
196 struct buffer_page *head_page; /* read from head */
197 struct buffer_page *tail_page; /* write to tail */
198 struct buffer_page *commit_page; /* commited pages */
199 struct buffer_page *reader_page;
200 unsigned long overrun;
201 unsigned long entries;
204 atomic_t record_disabled;
213 atomic_t record_disabled;
217 struct ring_buffer_per_cpu **buffers;
220 struct ring_buffer_iter {
221 struct ring_buffer_per_cpu *cpu_buffer;
223 struct buffer_page *head_page;
227 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
228 #define RB_WARN_ON(buffer, cond) \
230 int _____ret = unlikely(cond); \
232 atomic_inc(&buffer->record_disabled); \
239 * check_pages - integrity check of buffer pages
240 * @cpu_buffer: CPU buffer with pages to test
242 * As a safty measure we check to make sure the data pages have not
245 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
247 struct list_head *head = &cpu_buffer->pages;
248 struct buffer_page *page, *tmp;
250 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
252 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
255 list_for_each_entry_safe(page, tmp, head, list) {
256 if (RB_WARN_ON(cpu_buffer,
257 page->list.next->prev != &page->list))
259 if (RB_WARN_ON(cpu_buffer,
260 page->list.prev->next != &page->list))
267 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
270 struct list_head *head = &cpu_buffer->pages;
271 struct buffer_page *page, *tmp;
276 for (i = 0; i < nr_pages; i++) {
277 page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()),
278 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
281 list_add(&page->list, &pages);
283 addr = __get_free_page(GFP_KERNEL);
286 page->page = (void *)addr;
289 list_splice(&pages, head);
291 rb_check_pages(cpu_buffer);
296 list_for_each_entry_safe(page, tmp, &pages, list) {
297 list_del_init(&page->list);
298 free_buffer_page(page);
303 static struct ring_buffer_per_cpu *
304 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
306 struct ring_buffer_per_cpu *cpu_buffer;
307 struct buffer_page *page;
311 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
312 GFP_KERNEL, cpu_to_node(cpu));
316 cpu_buffer->cpu = cpu;
317 cpu_buffer->buffer = buffer;
318 spin_lock_init(&cpu_buffer->reader_lock);
319 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
320 INIT_LIST_HEAD(&cpu_buffer->pages);
322 page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()),
323 GFP_KERNEL, cpu_to_node(cpu));
325 goto fail_free_buffer;
327 cpu_buffer->reader_page = page;
328 addr = __get_free_page(GFP_KERNEL);
330 goto fail_free_reader;
331 page->page = (void *)addr;
333 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
335 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
337 goto fail_free_reader;
339 cpu_buffer->head_page
340 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
341 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
346 free_buffer_page(cpu_buffer->reader_page);
353 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
355 struct list_head *head = &cpu_buffer->pages;
356 struct buffer_page *page, *tmp;
358 list_del_init(&cpu_buffer->reader_page->list);
359 free_buffer_page(cpu_buffer->reader_page);
361 list_for_each_entry_safe(page, tmp, head, list) {
362 list_del_init(&page->list);
363 free_buffer_page(page);
369 * Causes compile errors if the struct buffer_page gets bigger
370 * than the struct page.
372 extern int ring_buffer_page_too_big(void);
375 * ring_buffer_alloc - allocate a new ring_buffer
376 * @size: the size in bytes that is needed.
377 * @flags: attributes to set for the ring buffer.
379 * Currently the only flag that is available is the RB_FL_OVERWRITE
380 * flag. This flag means that the buffer will overwrite old data
381 * when the buffer wraps. If this flag is not set, the buffer will
382 * drop data when the tail hits the head.
384 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
386 struct ring_buffer *buffer;
390 /* Paranoid! Optimizes out when all is well */
391 if (sizeof(struct buffer_page) > sizeof(struct page))
392 ring_buffer_page_too_big();
395 /* keep it in its own cache line */
396 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
401 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
402 buffer->flags = flags;
404 /* need at least two pages */
405 if (buffer->pages == 1)
408 buffer->cpumask = cpu_possible_map;
409 buffer->cpus = nr_cpu_ids;
411 bsize = sizeof(void *) * nr_cpu_ids;
412 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
414 if (!buffer->buffers)
415 goto fail_free_buffer;
417 for_each_buffer_cpu(buffer, cpu) {
418 buffer->buffers[cpu] =
419 rb_allocate_cpu_buffer(buffer, cpu);
420 if (!buffer->buffers[cpu])
421 goto fail_free_buffers;
424 mutex_init(&buffer->mutex);
429 for_each_buffer_cpu(buffer, cpu) {
430 if (buffer->buffers[cpu])
431 rb_free_cpu_buffer(buffer->buffers[cpu]);
433 kfree(buffer->buffers);
441 * ring_buffer_free - free a ring buffer.
442 * @buffer: the buffer to free.
445 ring_buffer_free(struct ring_buffer *buffer)
449 for_each_buffer_cpu(buffer, cpu)
450 rb_free_cpu_buffer(buffer->buffers[cpu]);
455 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
458 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
460 struct buffer_page *page;
464 atomic_inc(&cpu_buffer->record_disabled);
467 for (i = 0; i < nr_pages; i++) {
468 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
470 p = cpu_buffer->pages.next;
471 page = list_entry(p, struct buffer_page, list);
472 list_del_init(&page->list);
473 free_buffer_page(page);
475 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
478 rb_reset_cpu(cpu_buffer);
480 rb_check_pages(cpu_buffer);
482 atomic_dec(&cpu_buffer->record_disabled);
487 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
488 struct list_head *pages, unsigned nr_pages)
490 struct buffer_page *page;
494 atomic_inc(&cpu_buffer->record_disabled);
497 for (i = 0; i < nr_pages; i++) {
498 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
501 page = list_entry(p, struct buffer_page, list);
502 list_del_init(&page->list);
503 list_add_tail(&page->list, &cpu_buffer->pages);
505 rb_reset_cpu(cpu_buffer);
507 rb_check_pages(cpu_buffer);
509 atomic_dec(&cpu_buffer->record_disabled);
513 * ring_buffer_resize - resize the ring buffer
514 * @buffer: the buffer to resize.
515 * @size: the new size.
517 * The tracer is responsible for making sure that the buffer is
518 * not being used while changing the size.
519 * Note: We may be able to change the above requirement by using
520 * RCU synchronizations.
522 * Minimum size is 2 * BUF_PAGE_SIZE.
524 * Returns -1 on failure.
526 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
528 struct ring_buffer_per_cpu *cpu_buffer;
529 unsigned nr_pages, rm_pages, new_pages;
530 struct buffer_page *page, *tmp;
531 unsigned long buffer_size;
537 * Always succeed at resizing a non-existent buffer:
542 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
543 size *= BUF_PAGE_SIZE;
544 buffer_size = buffer->pages * BUF_PAGE_SIZE;
546 /* we need a minimum of two pages */
547 if (size < BUF_PAGE_SIZE * 2)
548 size = BUF_PAGE_SIZE * 2;
550 if (size == buffer_size)
553 mutex_lock(&buffer->mutex);
555 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
557 if (size < buffer_size) {
559 /* easy case, just free pages */
560 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
561 mutex_unlock(&buffer->mutex);
565 rm_pages = buffer->pages - nr_pages;
567 for_each_buffer_cpu(buffer, cpu) {
568 cpu_buffer = buffer->buffers[cpu];
569 rb_remove_pages(cpu_buffer, rm_pages);
575 * This is a bit more difficult. We only want to add pages
576 * when we can allocate enough for all CPUs. We do this
577 * by allocating all the pages and storing them on a local
578 * link list. If we succeed in our allocation, then we
579 * add these pages to the cpu_buffers. Otherwise we just free
580 * them all and return -ENOMEM;
582 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
583 mutex_unlock(&buffer->mutex);
587 new_pages = nr_pages - buffer->pages;
589 for_each_buffer_cpu(buffer, cpu) {
590 for (i = 0; i < new_pages; i++) {
591 page = kzalloc_node(ALIGN(sizeof(*page),
593 GFP_KERNEL, cpu_to_node(cpu));
596 list_add(&page->list, &pages);
597 addr = __get_free_page(GFP_KERNEL);
600 page->page = (void *)addr;
604 for_each_buffer_cpu(buffer, cpu) {
605 cpu_buffer = buffer->buffers[cpu];
606 rb_insert_pages(cpu_buffer, &pages, new_pages);
609 if (RB_WARN_ON(buffer, !list_empty(&pages))) {
610 mutex_unlock(&buffer->mutex);
615 buffer->pages = nr_pages;
616 mutex_unlock(&buffer->mutex);
621 list_for_each_entry_safe(page, tmp, &pages, list) {
622 list_del_init(&page->list);
623 free_buffer_page(page);
625 mutex_unlock(&buffer->mutex);
629 static inline int rb_null_event(struct ring_buffer_event *event)
631 return event->type == RINGBUF_TYPE_PADDING;
634 static inline void *__rb_page_index(struct buffer_page *page, unsigned index)
636 return page->page + index;
639 static inline struct ring_buffer_event *
640 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
642 return __rb_page_index(cpu_buffer->reader_page,
643 cpu_buffer->reader_page->read);
646 static inline struct ring_buffer_event *
647 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
649 return __rb_page_index(cpu_buffer->head_page,
650 cpu_buffer->head_page->read);
653 static inline struct ring_buffer_event *
654 rb_iter_head_event(struct ring_buffer_iter *iter)
656 return __rb_page_index(iter->head_page, iter->head);
659 static inline unsigned rb_page_write(struct buffer_page *bpage)
661 return local_read(&bpage->write);
664 static inline unsigned rb_page_commit(struct buffer_page *bpage)
666 return local_read(&bpage->commit);
669 /* Size is determined by what has been commited */
670 static inline unsigned rb_page_size(struct buffer_page *bpage)
672 return rb_page_commit(bpage);
675 static inline unsigned
676 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
678 return rb_page_commit(cpu_buffer->commit_page);
681 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
683 return rb_page_commit(cpu_buffer->head_page);
687 * When the tail hits the head and the buffer is in overwrite mode,
688 * the head jumps to the next page and all content on the previous
689 * page is discarded. But before doing so, we update the overrun
690 * variable of the buffer.
692 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
694 struct ring_buffer_event *event;
697 for (head = 0; head < rb_head_size(cpu_buffer);
698 head += rb_event_length(event)) {
700 event = __rb_page_index(cpu_buffer->head_page, head);
701 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
703 /* Only count data entries */
704 if (event->type != RINGBUF_TYPE_DATA)
706 cpu_buffer->overrun++;
707 cpu_buffer->entries--;
711 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
712 struct buffer_page **page)
714 struct list_head *p = (*page)->list.next;
716 if (p == &cpu_buffer->pages)
719 *page = list_entry(p, struct buffer_page, list);
722 static inline unsigned
723 rb_event_index(struct ring_buffer_event *event)
725 unsigned long addr = (unsigned long)event;
727 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
731 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
732 struct ring_buffer_event *event)
734 unsigned long addr = (unsigned long)event;
737 index = rb_event_index(event);
740 return cpu_buffer->commit_page->page == (void *)addr &&
741 rb_commit_index(cpu_buffer) == index;
745 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
746 struct ring_buffer_event *event)
748 unsigned long addr = (unsigned long)event;
751 index = rb_event_index(event);
754 while (cpu_buffer->commit_page->page != (void *)addr) {
755 if (RB_WARN_ON(cpu_buffer,
756 cpu_buffer->commit_page == cpu_buffer->tail_page))
758 cpu_buffer->commit_page->commit =
759 cpu_buffer->commit_page->write;
760 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
761 cpu_buffer->write_stamp = cpu_buffer->commit_page->time_stamp;
764 /* Now set the commit to the event's index */
765 local_set(&cpu_buffer->commit_page->commit, index);
769 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
772 * We only race with interrupts and NMIs on this CPU.
773 * If we own the commit event, then we can commit
774 * all others that interrupted us, since the interruptions
775 * are in stack format (they finish before they come
776 * back to us). This allows us to do a simple loop to
777 * assign the commit to the tail.
779 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
780 cpu_buffer->commit_page->commit =
781 cpu_buffer->commit_page->write;
782 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
783 cpu_buffer->write_stamp = cpu_buffer->commit_page->time_stamp;
784 /* add barrier to keep gcc from optimizing too much */
787 while (rb_commit_index(cpu_buffer) !=
788 rb_page_write(cpu_buffer->commit_page)) {
789 cpu_buffer->commit_page->commit =
790 cpu_buffer->commit_page->write;
795 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
797 cpu_buffer->read_stamp = cpu_buffer->reader_page->time_stamp;
798 cpu_buffer->reader_page->read = 0;
801 static inline void rb_inc_iter(struct ring_buffer_iter *iter)
803 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
806 * The iterator could be on the reader page (it starts there).
807 * But the head could have moved, since the reader was
808 * found. Check for this case and assign the iterator
809 * to the head page instead of next.
811 if (iter->head_page == cpu_buffer->reader_page)
812 iter->head_page = cpu_buffer->head_page;
814 rb_inc_page(cpu_buffer, &iter->head_page);
816 iter->read_stamp = iter->head_page->time_stamp;
821 * ring_buffer_update_event - update event type and data
822 * @event: the even to update
823 * @type: the type of event
824 * @length: the size of the event field in the ring buffer
826 * Update the type and data fields of the event. The length
827 * is the actual size that is written to the ring buffer,
828 * and with this, we can determine what to place into the
832 rb_update_event(struct ring_buffer_event *event,
833 unsigned type, unsigned length)
839 case RINGBUF_TYPE_PADDING:
842 case RINGBUF_TYPE_TIME_EXTEND:
844 (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1))
845 >> RB_ALIGNMENT_SHIFT;
848 case RINGBUF_TYPE_TIME_STAMP:
850 (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1))
851 >> RB_ALIGNMENT_SHIFT;
854 case RINGBUF_TYPE_DATA:
855 length -= RB_EVNT_HDR_SIZE;
856 if (length > RB_MAX_SMALL_DATA) {
858 event->array[0] = length;
861 (length + (RB_ALIGNMENT-1))
862 >> RB_ALIGNMENT_SHIFT;
869 static inline unsigned rb_calculate_event_length(unsigned length)
871 struct ring_buffer_event event; /* Used only for sizeof array */
873 /* zero length can cause confusions */
877 if (length > RB_MAX_SMALL_DATA)
878 length += sizeof(event.array[0]);
880 length += RB_EVNT_HDR_SIZE;
881 length = ALIGN(length, RB_ALIGNMENT);
886 static struct ring_buffer_event *
887 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
888 unsigned type, unsigned long length, u64 *ts)
890 struct buffer_page *tail_page, *head_page, *reader_page;
891 unsigned long tail, write;
892 struct ring_buffer *buffer = cpu_buffer->buffer;
893 struct ring_buffer_event *event;
896 tail_page = cpu_buffer->tail_page;
897 write = local_add_return(length, &tail_page->write);
898 tail = write - length;
900 /* See if we shot pass the end of this buffer page */
901 if (write > BUF_PAGE_SIZE) {
902 struct buffer_page *next_page = tail_page;
904 local_irq_save(flags);
905 __raw_spin_lock(&cpu_buffer->lock);
907 rb_inc_page(cpu_buffer, &next_page);
909 head_page = cpu_buffer->head_page;
910 reader_page = cpu_buffer->reader_page;
912 /* we grabbed the lock before incrementing */
913 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
917 * If for some reason, we had an interrupt storm that made
918 * it all the way around the buffer, bail, and warn
921 if (unlikely(next_page == cpu_buffer->commit_page)) {
926 if (next_page == head_page) {
927 if (!(buffer->flags & RB_FL_OVERWRITE)) {
929 if (tail <= BUF_PAGE_SIZE)
930 local_set(&tail_page->write, tail);
934 /* tail_page has not moved yet? */
935 if (tail_page == cpu_buffer->tail_page) {
936 /* count overflows */
937 rb_update_overflow(cpu_buffer);
939 rb_inc_page(cpu_buffer, &head_page);
940 cpu_buffer->head_page = head_page;
941 cpu_buffer->head_page->read = 0;
946 * If the tail page is still the same as what we think
947 * it is, then it is up to us to update the tail
950 if (tail_page == cpu_buffer->tail_page) {
951 local_set(&next_page->write, 0);
952 local_set(&next_page->commit, 0);
953 cpu_buffer->tail_page = next_page;
955 /* reread the time stamp */
956 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
957 cpu_buffer->tail_page->time_stamp = *ts;
961 * The actual tail page has moved forward.
963 if (tail < BUF_PAGE_SIZE) {
964 /* Mark the rest of the page with padding */
965 event = __rb_page_index(tail_page, tail);
966 event->type = RINGBUF_TYPE_PADDING;
969 if (tail <= BUF_PAGE_SIZE)
970 /* Set the write back to the previous setting */
971 local_set(&tail_page->write, tail);
974 * If this was a commit entry that failed,
977 if (tail_page == cpu_buffer->commit_page &&
978 tail == rb_commit_index(cpu_buffer)) {
979 rb_set_commit_to_write(cpu_buffer);
982 __raw_spin_unlock(&cpu_buffer->lock);
983 local_irq_restore(flags);
985 /* fail and let the caller try again */
986 return ERR_PTR(-EAGAIN);
989 /* We reserved something on the buffer */
991 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
994 event = __rb_page_index(tail_page, tail);
995 rb_update_event(event, type, length);
998 * If this is a commit and the tail is zero, then update
999 * this page's time stamp.
1001 if (!tail && rb_is_commit(cpu_buffer, event))
1002 cpu_buffer->commit_page->time_stamp = *ts;
1007 __raw_spin_unlock(&cpu_buffer->lock);
1008 local_irq_restore(flags);
1013 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1014 u64 *ts, u64 *delta)
1016 struct ring_buffer_event *event;
1020 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1021 printk(KERN_WARNING "Delta way too big! %llu"
1022 " ts=%llu write stamp = %llu\n",
1023 (unsigned long long)*delta,
1024 (unsigned long long)*ts,
1025 (unsigned long long)cpu_buffer->write_stamp);
1030 * The delta is too big, we to add a
1033 event = __rb_reserve_next(cpu_buffer,
1034 RINGBUF_TYPE_TIME_EXTEND,
1040 if (PTR_ERR(event) == -EAGAIN)
1043 /* Only a commited time event can update the write stamp */
1044 if (rb_is_commit(cpu_buffer, event)) {
1046 * If this is the first on the page, then we need to
1047 * update the page itself, and just put in a zero.
1049 if (rb_event_index(event)) {
1050 event->time_delta = *delta & TS_MASK;
1051 event->array[0] = *delta >> TS_SHIFT;
1053 cpu_buffer->commit_page->time_stamp = *ts;
1054 event->time_delta = 0;
1055 event->array[0] = 0;
1057 cpu_buffer->write_stamp = *ts;
1058 /* let the caller know this was the commit */
1061 /* Darn, this is just wasted space */
1062 event->time_delta = 0;
1063 event->array[0] = 0;
1072 static struct ring_buffer_event *
1073 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1074 unsigned type, unsigned long length)
1076 struct ring_buffer_event *event;
1083 * We allow for interrupts to reenter here and do a trace.
1084 * If one does, it will cause this original code to loop
1085 * back here. Even with heavy interrupts happening, this
1086 * should only happen a few times in a row. If this happens
1087 * 1000 times in a row, there must be either an interrupt
1088 * storm or we have something buggy.
1091 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1094 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1097 * Only the first commit can update the timestamp.
1098 * Yes there is a race here. If an interrupt comes in
1099 * just after the conditional and it traces too, then it
1100 * will also check the deltas. More than one timestamp may
1101 * also be made. But only the entry that did the actual
1102 * commit will be something other than zero.
1104 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1105 rb_page_write(cpu_buffer->tail_page) ==
1106 rb_commit_index(cpu_buffer)) {
1108 delta = ts - cpu_buffer->write_stamp;
1110 /* make sure this delta is calculated here */
1113 /* Did the write stamp get updated already? */
1114 if (unlikely(ts < cpu_buffer->write_stamp))
1117 if (test_time_stamp(delta)) {
1119 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1121 if (commit == -EBUSY)
1124 if (commit == -EAGAIN)
1127 RB_WARN_ON(cpu_buffer, commit < 0);
1130 /* Non commits have zero deltas */
1133 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1134 if (PTR_ERR(event) == -EAGAIN)
1138 if (unlikely(commit))
1140 * Ouch! We needed a timestamp and it was commited. But
1141 * we didn't get our event reserved.
1143 rb_set_commit_to_write(cpu_buffer);
1148 * If the timestamp was commited, make the commit our entry
1149 * now so that we will update it when needed.
1152 rb_set_commit_event(cpu_buffer, event);
1153 else if (!rb_is_commit(cpu_buffer, event))
1156 event->time_delta = delta;
1161 static DEFINE_PER_CPU(int, rb_need_resched);
1164 * ring_buffer_lock_reserve - reserve a part of the buffer
1165 * @buffer: the ring buffer to reserve from
1166 * @length: the length of the data to reserve (excluding event header)
1167 * @flags: a pointer to save the interrupt flags
1169 * Returns a reseverd event on the ring buffer to copy directly to.
1170 * The user of this interface will need to get the body to write into
1171 * and can use the ring_buffer_event_data() interface.
1173 * The length is the length of the data needed, not the event length
1174 * which also includes the event header.
1176 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1177 * If NULL is returned, then nothing has been allocated or locked.
1179 struct ring_buffer_event *
1180 ring_buffer_lock_reserve(struct ring_buffer *buffer,
1181 unsigned long length,
1182 unsigned long *flags)
1184 struct ring_buffer_per_cpu *cpu_buffer;
1185 struct ring_buffer_event *event;
1188 if (ring_buffers_off)
1191 if (atomic_read(&buffer->record_disabled))
1194 /* If we are tracing schedule, we don't want to recurse */
1195 resched = ftrace_preempt_disable();
1197 cpu = raw_smp_processor_id();
1199 if (!cpu_isset(cpu, buffer->cpumask))
1202 cpu_buffer = buffer->buffers[cpu];
1204 if (atomic_read(&cpu_buffer->record_disabled))
1207 length = rb_calculate_event_length(length);
1208 if (length > BUF_PAGE_SIZE)
1211 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1216 * Need to store resched state on this cpu.
1217 * Only the first needs to.
1220 if (preempt_count() == 1)
1221 per_cpu(rb_need_resched, cpu) = resched;
1226 ftrace_preempt_enable(resched);
1230 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1231 struct ring_buffer_event *event)
1233 cpu_buffer->entries++;
1235 /* Only process further if we own the commit */
1236 if (!rb_is_commit(cpu_buffer, event))
1239 cpu_buffer->write_stamp += event->time_delta;
1241 rb_set_commit_to_write(cpu_buffer);
1245 * ring_buffer_unlock_commit - commit a reserved
1246 * @buffer: The buffer to commit to
1247 * @event: The event pointer to commit.
1248 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1250 * This commits the data to the ring buffer, and releases any locks held.
1252 * Must be paired with ring_buffer_lock_reserve.
1254 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1255 struct ring_buffer_event *event,
1256 unsigned long flags)
1258 struct ring_buffer_per_cpu *cpu_buffer;
1259 int cpu = raw_smp_processor_id();
1261 cpu_buffer = buffer->buffers[cpu];
1263 rb_commit(cpu_buffer, event);
1266 * Only the last preempt count needs to restore preemption.
1268 if (preempt_count() == 1)
1269 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1271 preempt_enable_no_resched_notrace();
1277 * ring_buffer_write - write data to the buffer without reserving
1278 * @buffer: The ring buffer to write to.
1279 * @length: The length of the data being written (excluding the event header)
1280 * @data: The data to write to the buffer.
1282 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1283 * one function. If you already have the data to write to the buffer, it
1284 * may be easier to simply call this function.
1286 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1287 * and not the length of the event which would hold the header.
1289 int ring_buffer_write(struct ring_buffer *buffer,
1290 unsigned long length,
1293 struct ring_buffer_per_cpu *cpu_buffer;
1294 struct ring_buffer_event *event;
1295 unsigned long event_length;
1300 if (ring_buffers_off)
1303 if (atomic_read(&buffer->record_disabled))
1306 resched = ftrace_preempt_disable();
1308 cpu = raw_smp_processor_id();
1310 if (!cpu_isset(cpu, buffer->cpumask))
1313 cpu_buffer = buffer->buffers[cpu];
1315 if (atomic_read(&cpu_buffer->record_disabled))
1318 event_length = rb_calculate_event_length(length);
1319 event = rb_reserve_next_event(cpu_buffer,
1320 RINGBUF_TYPE_DATA, event_length);
1324 body = rb_event_data(event);
1326 memcpy(body, data, length);
1328 rb_commit(cpu_buffer, event);
1332 ftrace_preempt_enable(resched);
1337 static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1339 struct buffer_page *reader = cpu_buffer->reader_page;
1340 struct buffer_page *head = cpu_buffer->head_page;
1341 struct buffer_page *commit = cpu_buffer->commit_page;
1343 return reader->read == rb_page_commit(reader) &&
1344 (commit == reader ||
1346 head->read == rb_page_commit(commit)));
1350 * ring_buffer_record_disable - stop all writes into the buffer
1351 * @buffer: The ring buffer to stop writes to.
1353 * This prevents all writes to the buffer. Any attempt to write
1354 * to the buffer after this will fail and return NULL.
1356 * The caller should call synchronize_sched() after this.
1358 void ring_buffer_record_disable(struct ring_buffer *buffer)
1360 atomic_inc(&buffer->record_disabled);
1364 * ring_buffer_record_enable - enable writes to the buffer
1365 * @buffer: The ring buffer to enable writes
1367 * Note, multiple disables will need the same number of enables
1368 * to truely enable the writing (much like preempt_disable).
1370 void ring_buffer_record_enable(struct ring_buffer *buffer)
1372 atomic_dec(&buffer->record_disabled);
1376 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1377 * @buffer: The ring buffer to stop writes to.
1378 * @cpu: The CPU buffer to stop
1380 * This prevents all writes to the buffer. Any attempt to write
1381 * to the buffer after this will fail and return NULL.
1383 * The caller should call synchronize_sched() after this.
1385 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1387 struct ring_buffer_per_cpu *cpu_buffer;
1389 if (!cpu_isset(cpu, buffer->cpumask))
1392 cpu_buffer = buffer->buffers[cpu];
1393 atomic_inc(&cpu_buffer->record_disabled);
1397 * ring_buffer_record_enable_cpu - enable writes to the buffer
1398 * @buffer: The ring buffer to enable writes
1399 * @cpu: The CPU to enable.
1401 * Note, multiple disables will need the same number of enables
1402 * to truely enable the writing (much like preempt_disable).
1404 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1406 struct ring_buffer_per_cpu *cpu_buffer;
1408 if (!cpu_isset(cpu, buffer->cpumask))
1411 cpu_buffer = buffer->buffers[cpu];
1412 atomic_dec(&cpu_buffer->record_disabled);
1416 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1417 * @buffer: The ring buffer
1418 * @cpu: The per CPU buffer to get the entries from.
1420 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1422 struct ring_buffer_per_cpu *cpu_buffer;
1424 if (!cpu_isset(cpu, buffer->cpumask))
1427 cpu_buffer = buffer->buffers[cpu];
1428 return cpu_buffer->entries;
1432 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1433 * @buffer: The ring buffer
1434 * @cpu: The per CPU buffer to get the number of overruns from
1436 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1438 struct ring_buffer_per_cpu *cpu_buffer;
1440 if (!cpu_isset(cpu, buffer->cpumask))
1443 cpu_buffer = buffer->buffers[cpu];
1444 return cpu_buffer->overrun;
1448 * ring_buffer_entries - get the number of entries in a buffer
1449 * @buffer: The ring buffer
1451 * Returns the total number of entries in the ring buffer
1454 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1456 struct ring_buffer_per_cpu *cpu_buffer;
1457 unsigned long entries = 0;
1460 /* if you care about this being correct, lock the buffer */
1461 for_each_buffer_cpu(buffer, cpu) {
1462 cpu_buffer = buffer->buffers[cpu];
1463 entries += cpu_buffer->entries;
1470 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1471 * @buffer: The ring buffer
1473 * Returns the total number of overruns in the ring buffer
1476 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1478 struct ring_buffer_per_cpu *cpu_buffer;
1479 unsigned long overruns = 0;
1482 /* if you care about this being correct, lock the buffer */
1483 for_each_buffer_cpu(buffer, cpu) {
1484 cpu_buffer = buffer->buffers[cpu];
1485 overruns += cpu_buffer->overrun;
1491 static void rb_iter_reset(struct ring_buffer_iter *iter)
1493 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1495 /* Iterator usage is expected to have record disabled */
1496 if (list_empty(&cpu_buffer->reader_page->list)) {
1497 iter->head_page = cpu_buffer->head_page;
1498 iter->head = cpu_buffer->head_page->read;
1500 iter->head_page = cpu_buffer->reader_page;
1501 iter->head = cpu_buffer->reader_page->read;
1504 iter->read_stamp = cpu_buffer->read_stamp;
1506 iter->read_stamp = iter->head_page->time_stamp;
1510 * ring_buffer_iter_reset - reset an iterator
1511 * @iter: The iterator to reset
1513 * Resets the iterator, so that it will start from the beginning
1516 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1518 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1519 unsigned long flags;
1521 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1522 rb_iter_reset(iter);
1523 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1527 * ring_buffer_iter_empty - check if an iterator has no more to read
1528 * @iter: The iterator to check
1530 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1532 struct ring_buffer_per_cpu *cpu_buffer;
1534 cpu_buffer = iter->cpu_buffer;
1536 return iter->head_page == cpu_buffer->commit_page &&
1537 iter->head == rb_commit_index(cpu_buffer);
1541 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1542 struct ring_buffer_event *event)
1546 switch (event->type) {
1547 case RINGBUF_TYPE_PADDING:
1550 case RINGBUF_TYPE_TIME_EXTEND:
1551 delta = event->array[0];
1553 delta += event->time_delta;
1554 cpu_buffer->read_stamp += delta;
1557 case RINGBUF_TYPE_TIME_STAMP:
1558 /* FIXME: not implemented */
1561 case RINGBUF_TYPE_DATA:
1562 cpu_buffer->read_stamp += event->time_delta;
1572 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1573 struct ring_buffer_event *event)
1577 switch (event->type) {
1578 case RINGBUF_TYPE_PADDING:
1581 case RINGBUF_TYPE_TIME_EXTEND:
1582 delta = event->array[0];
1584 delta += event->time_delta;
1585 iter->read_stamp += delta;
1588 case RINGBUF_TYPE_TIME_STAMP:
1589 /* FIXME: not implemented */
1592 case RINGBUF_TYPE_DATA:
1593 iter->read_stamp += event->time_delta;
1602 static struct buffer_page *
1603 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1605 struct buffer_page *reader = NULL;
1606 unsigned long flags;
1609 local_irq_save(flags);
1610 __raw_spin_lock(&cpu_buffer->lock);
1614 * This should normally only loop twice. But because the
1615 * start of the reader inserts an empty page, it causes
1616 * a case where we will loop three times. There should be no
1617 * reason to loop four times (that I know of).
1619 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
1624 reader = cpu_buffer->reader_page;
1626 /* If there's more to read, return this page */
1627 if (cpu_buffer->reader_page->read < rb_page_size(reader))
1630 /* Never should we have an index greater than the size */
1631 if (RB_WARN_ON(cpu_buffer,
1632 cpu_buffer->reader_page->read > rb_page_size(reader)))
1635 /* check if we caught up to the tail */
1637 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
1641 * Splice the empty reader page into the list around the head.
1642 * Reset the reader page to size zero.
1645 reader = cpu_buffer->head_page;
1646 cpu_buffer->reader_page->list.next = reader->list.next;
1647 cpu_buffer->reader_page->list.prev = reader->list.prev;
1649 local_set(&cpu_buffer->reader_page->write, 0);
1650 local_set(&cpu_buffer->reader_page->commit, 0);
1652 /* Make the reader page now replace the head */
1653 reader->list.prev->next = &cpu_buffer->reader_page->list;
1654 reader->list.next->prev = &cpu_buffer->reader_page->list;
1657 * If the tail is on the reader, then we must set the head
1658 * to the inserted page, otherwise we set it one before.
1660 cpu_buffer->head_page = cpu_buffer->reader_page;
1662 if (cpu_buffer->commit_page != reader)
1663 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1665 /* Finally update the reader page to the new head */
1666 cpu_buffer->reader_page = reader;
1667 rb_reset_reader_page(cpu_buffer);
1672 __raw_spin_unlock(&cpu_buffer->lock);
1673 local_irq_restore(flags);
1678 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1680 struct ring_buffer_event *event;
1681 struct buffer_page *reader;
1684 reader = rb_get_reader_page(cpu_buffer);
1686 /* This function should not be called when buffer is empty */
1687 if (RB_WARN_ON(cpu_buffer, !reader))
1690 event = rb_reader_event(cpu_buffer);
1692 if (event->type == RINGBUF_TYPE_DATA)
1693 cpu_buffer->entries--;
1695 rb_update_read_stamp(cpu_buffer, event);
1697 length = rb_event_length(event);
1698 cpu_buffer->reader_page->read += length;
1701 static void rb_advance_iter(struct ring_buffer_iter *iter)
1703 struct ring_buffer *buffer;
1704 struct ring_buffer_per_cpu *cpu_buffer;
1705 struct ring_buffer_event *event;
1708 cpu_buffer = iter->cpu_buffer;
1709 buffer = cpu_buffer->buffer;
1712 * Check if we are at the end of the buffer.
1714 if (iter->head >= rb_page_size(iter->head_page)) {
1715 if (RB_WARN_ON(buffer,
1716 iter->head_page == cpu_buffer->commit_page))
1722 event = rb_iter_head_event(iter);
1724 length = rb_event_length(event);
1727 * This should not be called to advance the header if we are
1728 * at the tail of the buffer.
1730 if (RB_WARN_ON(cpu_buffer,
1731 (iter->head_page == cpu_buffer->commit_page) &&
1732 (iter->head + length > rb_commit_index(cpu_buffer))))
1735 rb_update_iter_read_stamp(iter, event);
1737 iter->head += length;
1739 /* check for end of page padding */
1740 if ((iter->head >= rb_page_size(iter->head_page)) &&
1741 (iter->head_page != cpu_buffer->commit_page))
1742 rb_advance_iter(iter);
1745 static struct ring_buffer_event *
1746 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1748 struct ring_buffer_per_cpu *cpu_buffer;
1749 struct ring_buffer_event *event;
1750 struct buffer_page *reader;
1753 if (!cpu_isset(cpu, buffer->cpumask))
1756 cpu_buffer = buffer->buffers[cpu];
1760 * We repeat when a timestamp is encountered. It is possible
1761 * to get multiple timestamps from an interrupt entering just
1762 * as one timestamp is about to be written. The max times
1763 * that this can happen is the number of nested interrupts we
1764 * can have. Nesting 10 deep of interrupts is clearly
1767 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1770 reader = rb_get_reader_page(cpu_buffer);
1774 event = rb_reader_event(cpu_buffer);
1776 switch (event->type) {
1777 case RINGBUF_TYPE_PADDING:
1778 RB_WARN_ON(cpu_buffer, 1);
1779 rb_advance_reader(cpu_buffer);
1782 case RINGBUF_TYPE_TIME_EXTEND:
1783 /* Internal data, OK to advance */
1784 rb_advance_reader(cpu_buffer);
1787 case RINGBUF_TYPE_TIME_STAMP:
1788 /* FIXME: not implemented */
1789 rb_advance_reader(cpu_buffer);
1792 case RINGBUF_TYPE_DATA:
1794 *ts = cpu_buffer->read_stamp + event->time_delta;
1795 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1806 static struct ring_buffer_event *
1807 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1809 struct ring_buffer *buffer;
1810 struct ring_buffer_per_cpu *cpu_buffer;
1811 struct ring_buffer_event *event;
1814 if (ring_buffer_iter_empty(iter))
1817 cpu_buffer = iter->cpu_buffer;
1818 buffer = cpu_buffer->buffer;
1822 * We repeat when a timestamp is encountered. It is possible
1823 * to get multiple timestamps from an interrupt entering just
1824 * as one timestamp is about to be written. The max times
1825 * that this can happen is the number of nested interrupts we
1826 * can have. Nesting 10 deep of interrupts is clearly
1829 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1832 if (rb_per_cpu_empty(cpu_buffer))
1835 event = rb_iter_head_event(iter);
1837 switch (event->type) {
1838 case RINGBUF_TYPE_PADDING:
1842 case RINGBUF_TYPE_TIME_EXTEND:
1843 /* Internal data, OK to advance */
1844 rb_advance_iter(iter);
1847 case RINGBUF_TYPE_TIME_STAMP:
1848 /* FIXME: not implemented */
1849 rb_advance_iter(iter);
1852 case RINGBUF_TYPE_DATA:
1854 *ts = iter->read_stamp + event->time_delta;
1855 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1867 * ring_buffer_peek - peek at the next event to be read
1868 * @buffer: The ring buffer to read
1869 * @cpu: The cpu to peak at
1870 * @ts: The timestamp counter of this event.
1872 * This will return the event that will be read next, but does
1873 * not consume the data.
1875 struct ring_buffer_event *
1876 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1878 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1879 struct ring_buffer_event *event;
1880 unsigned long flags;
1882 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1883 event = rb_buffer_peek(buffer, cpu, ts);
1884 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1890 * ring_buffer_iter_peek - peek at the next event to be read
1891 * @iter: The ring buffer iterator
1892 * @ts: The timestamp counter of this event.
1894 * This will return the event that will be read next, but does
1895 * not increment the iterator.
1897 struct ring_buffer_event *
1898 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1900 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1901 struct ring_buffer_event *event;
1902 unsigned long flags;
1904 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1905 event = rb_iter_peek(iter, ts);
1906 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1912 * ring_buffer_consume - return an event and consume it
1913 * @buffer: The ring buffer to get the next event from
1915 * Returns the next event in the ring buffer, and that event is consumed.
1916 * Meaning, that sequential reads will keep returning a different event,
1917 * and eventually empty the ring buffer if the producer is slower.
1919 struct ring_buffer_event *
1920 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
1922 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1923 struct ring_buffer_event *event;
1924 unsigned long flags;
1926 if (!cpu_isset(cpu, buffer->cpumask))
1929 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1931 event = rb_buffer_peek(buffer, cpu, ts);
1935 rb_advance_reader(cpu_buffer);
1938 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1944 * ring_buffer_read_start - start a non consuming read of the buffer
1945 * @buffer: The ring buffer to read from
1946 * @cpu: The cpu buffer to iterate over
1948 * This starts up an iteration through the buffer. It also disables
1949 * the recording to the buffer until the reading is finished.
1950 * This prevents the reading from being corrupted. This is not
1951 * a consuming read, so a producer is not expected.
1953 * Must be paired with ring_buffer_finish.
1955 struct ring_buffer_iter *
1956 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
1958 struct ring_buffer_per_cpu *cpu_buffer;
1959 struct ring_buffer_iter *iter;
1960 unsigned long flags;
1962 if (!cpu_isset(cpu, buffer->cpumask))
1965 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1969 cpu_buffer = buffer->buffers[cpu];
1971 iter->cpu_buffer = cpu_buffer;
1973 atomic_inc(&cpu_buffer->record_disabled);
1974 synchronize_sched();
1976 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1977 __raw_spin_lock(&cpu_buffer->lock);
1978 rb_iter_reset(iter);
1979 __raw_spin_unlock(&cpu_buffer->lock);
1980 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1986 * ring_buffer_finish - finish reading the iterator of the buffer
1987 * @iter: The iterator retrieved by ring_buffer_start
1989 * This re-enables the recording to the buffer, and frees the
1993 ring_buffer_read_finish(struct ring_buffer_iter *iter)
1995 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1997 atomic_dec(&cpu_buffer->record_disabled);
2002 * ring_buffer_read - read the next item in the ring buffer by the iterator
2003 * @iter: The ring buffer iterator
2004 * @ts: The time stamp of the event read.
2006 * This reads the next event in the ring buffer and increments the iterator.
2008 struct ring_buffer_event *
2009 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2011 struct ring_buffer_event *event;
2012 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2013 unsigned long flags;
2015 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2016 event = rb_iter_peek(iter, ts);
2020 rb_advance_iter(iter);
2022 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2028 * ring_buffer_size - return the size of the ring buffer (in bytes)
2029 * @buffer: The ring buffer.
2031 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2033 return BUF_PAGE_SIZE * buffer->pages;
2037 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2039 cpu_buffer->head_page
2040 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2041 local_set(&cpu_buffer->head_page->write, 0);
2042 local_set(&cpu_buffer->head_page->commit, 0);
2044 cpu_buffer->head_page->read = 0;
2046 cpu_buffer->tail_page = cpu_buffer->head_page;
2047 cpu_buffer->commit_page = cpu_buffer->head_page;
2049 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2050 local_set(&cpu_buffer->reader_page->write, 0);
2051 local_set(&cpu_buffer->reader_page->commit, 0);
2052 cpu_buffer->reader_page->read = 0;
2054 cpu_buffer->overrun = 0;
2055 cpu_buffer->entries = 0;
2059 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2060 * @buffer: The ring buffer to reset a per cpu buffer of
2061 * @cpu: The CPU buffer to be reset
2063 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2065 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2066 unsigned long flags;
2068 if (!cpu_isset(cpu, buffer->cpumask))
2071 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2073 __raw_spin_lock(&cpu_buffer->lock);
2075 rb_reset_cpu(cpu_buffer);
2077 __raw_spin_unlock(&cpu_buffer->lock);
2079 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2083 * ring_buffer_reset - reset a ring buffer
2084 * @buffer: The ring buffer to reset all cpu buffers
2086 void ring_buffer_reset(struct ring_buffer *buffer)
2090 for_each_buffer_cpu(buffer, cpu)
2091 ring_buffer_reset_cpu(buffer, cpu);
2095 * rind_buffer_empty - is the ring buffer empty?
2096 * @buffer: The ring buffer to test
2098 int ring_buffer_empty(struct ring_buffer *buffer)
2100 struct ring_buffer_per_cpu *cpu_buffer;
2103 /* yes this is racy, but if you don't like the race, lock the buffer */
2104 for_each_buffer_cpu(buffer, cpu) {
2105 cpu_buffer = buffer->buffers[cpu];
2106 if (!rb_per_cpu_empty(cpu_buffer))
2113 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2114 * @buffer: The ring buffer
2115 * @cpu: The CPU buffer to test
2117 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2119 struct ring_buffer_per_cpu *cpu_buffer;
2121 if (!cpu_isset(cpu, buffer->cpumask))
2124 cpu_buffer = buffer->buffers[cpu];
2125 return rb_per_cpu_empty(cpu_buffer);
2129 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2130 * @buffer_a: One buffer to swap with
2131 * @buffer_b: The other buffer to swap with
2133 * This function is useful for tracers that want to take a "snapshot"
2134 * of a CPU buffer and has another back up buffer lying around.
2135 * it is expected that the tracer handles the cpu buffer not being
2136 * used at the moment.
2138 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2139 struct ring_buffer *buffer_b, int cpu)
2141 struct ring_buffer_per_cpu *cpu_buffer_a;
2142 struct ring_buffer_per_cpu *cpu_buffer_b;
2144 if (!cpu_isset(cpu, buffer_a->cpumask) ||
2145 !cpu_isset(cpu, buffer_b->cpumask))
2148 /* At least make sure the two buffers are somewhat the same */
2149 if (buffer_a->size != buffer_b->size ||
2150 buffer_a->pages != buffer_b->pages)
2153 cpu_buffer_a = buffer_a->buffers[cpu];
2154 cpu_buffer_b = buffer_b->buffers[cpu];
2157 * We can't do a synchronize_sched here because this
2158 * function can be called in atomic context.
2159 * Normally this will be called from the same CPU as cpu.
2160 * If not it's up to the caller to protect this.
2162 atomic_inc(&cpu_buffer_a->record_disabled);
2163 atomic_inc(&cpu_buffer_b->record_disabled);
2165 buffer_a->buffers[cpu] = cpu_buffer_b;
2166 buffer_b->buffers[cpu] = cpu_buffer_a;
2168 cpu_buffer_b->buffer = buffer_a;
2169 cpu_buffer_a->buffer = buffer_b;
2171 atomic_dec(&cpu_buffer_a->record_disabled);
2172 atomic_dec(&cpu_buffer_b->record_disabled);
2178 rb_simple_read(struct file *filp, char __user *ubuf,
2179 size_t cnt, loff_t *ppos)
2181 int *p = filp->private_data;
2185 /* !ring_buffers_off == tracing_on */
2186 r = sprintf(buf, "%d\n", !*p);
2188 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2192 rb_simple_write(struct file *filp, const char __user *ubuf,
2193 size_t cnt, loff_t *ppos)
2195 int *p = filp->private_data;
2200 if (cnt >= sizeof(buf))
2203 if (copy_from_user(&buf, ubuf, cnt))
2208 ret = strict_strtoul(buf, 10, &val);
2212 /* !ring_buffers_off == tracing_on */
2220 static struct file_operations rb_simple_fops = {
2221 .open = tracing_open_generic,
2222 .read = rb_simple_read,
2223 .write = rb_simple_write,
2227 static __init int rb_init_debugfs(void)
2229 struct dentry *d_tracer;
2230 struct dentry *entry;
2232 d_tracer = tracing_init_dentry();
2234 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2235 &ring_buffers_off, &rb_simple_fops);
2237 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2242 fs_initcall(rb_init_debugfs);