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>
22 * A fast way to enable or disable all ring buffers is to
23 * call tracing_on or tracing_off. Turning off the ring buffers
24 * prevents all ring buffers from being recorded to.
25 * Turning this switch on, makes it OK to write to the
26 * ring buffer, if the ring buffer is enabled itself.
28 * There's three layers that must be on in order to write
31 * 1) This global flag must be set.
32 * 2) The ring buffer must be enabled for recording.
33 * 3) The per cpu buffer must be enabled for recording.
35 * In case of an anomaly, this global flag has a bit set that
36 * will permantly disable all ring buffers.
40 * Global flag to disable all recording to ring buffers
41 * This has two bits: ON, DISABLED
45 * 0 0 : ring buffers are off
46 * 1 0 : ring buffers are on
47 * X 1 : ring buffers are permanently disabled
51 RB_BUFFERS_ON_BIT = 0,
52 RB_BUFFERS_DISABLED_BIT = 1,
56 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
57 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
60 static long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
63 * tracing_on - enable all tracing buffers
65 * This function enables all tracing buffers that may have been
66 * disabled with tracing_off.
70 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
74 * tracing_off - turn off all tracing buffers
76 * This function stops all tracing buffers from recording data.
77 * It does not disable any overhead the tracers themselves may
78 * be causing. This function simply causes all recording to
79 * the ring buffers to fail.
81 void tracing_off(void)
83 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
87 * tracing_off_permanent - permanently disable ring buffers
89 * This function, once called, will disable all ring buffers
92 void tracing_off_permanent(void)
94 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
99 /* Up this if you want to test the TIME_EXTENTS and normalization */
100 #define DEBUG_SHIFT 0
103 u64 ring_buffer_time_stamp(int cpu)
107 preempt_disable_notrace();
108 /* shift to debug/test normalization and TIME_EXTENTS */
109 time = sched_clock() << DEBUG_SHIFT;
110 preempt_enable_no_resched_notrace();
115 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
117 /* Just stupid testing the normalize function and deltas */
121 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
122 #define RB_ALIGNMENT_SHIFT 2
123 #define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT)
124 #define RB_MAX_SMALL_DATA 28
127 RB_LEN_TIME_EXTEND = 8,
128 RB_LEN_TIME_STAMP = 16,
131 /* inline for ring buffer fast paths */
132 static inline unsigned
133 rb_event_length(struct ring_buffer_event *event)
137 switch (event->type) {
138 case RINGBUF_TYPE_PADDING:
142 case RINGBUF_TYPE_TIME_EXTEND:
143 return RB_LEN_TIME_EXTEND;
145 case RINGBUF_TYPE_TIME_STAMP:
146 return RB_LEN_TIME_STAMP;
148 case RINGBUF_TYPE_DATA:
150 length = event->len << RB_ALIGNMENT_SHIFT;
152 length = event->array[0];
153 return length + RB_EVNT_HDR_SIZE;
162 * ring_buffer_event_length - return the length of the event
163 * @event: the event to get the length of
165 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
167 return rb_event_length(event);
170 /* inline for ring buffer fast paths */
172 rb_event_data(struct ring_buffer_event *event)
174 BUG_ON(event->type != RINGBUF_TYPE_DATA);
175 /* If length is in len field, then array[0] has the data */
177 return (void *)&event->array[0];
178 /* Otherwise length is in array[0] and array[1] has the data */
179 return (void *)&event->array[1];
183 * ring_buffer_event_data - return the data of the event
184 * @event: the event to get the data from
186 void *ring_buffer_event_data(struct ring_buffer_event *event)
188 return rb_event_data(event);
191 #define for_each_buffer_cpu(buffer, cpu) \
192 for_each_cpu_mask(cpu, buffer->cpumask)
195 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
196 #define TS_DELTA_TEST (~TS_MASK)
198 struct buffer_data_page {
199 u64 time_stamp; /* page time stamp */
200 local_t commit; /* write commited index */
201 unsigned char data[]; /* data of buffer page */
205 local_t write; /* index for next write */
206 unsigned read; /* index for next read */
207 struct list_head list; /* list of free pages */
208 struct buffer_data_page *page; /* Actual data page */
211 static void rb_init_page(struct buffer_data_page *bpage)
213 local_set(&bpage->commit, 0);
217 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
220 static inline void free_buffer_page(struct buffer_page *bpage)
223 free_page((unsigned long)bpage->page);
228 * We need to fit the time_stamp delta into 27 bits.
230 static inline int test_time_stamp(u64 delta)
232 if (delta & TS_DELTA_TEST)
237 #define BUF_PAGE_SIZE (PAGE_SIZE - sizeof(struct buffer_data_page))
240 * head_page == tail_page && head == tail then buffer is empty.
242 struct ring_buffer_per_cpu {
244 struct ring_buffer *buffer;
245 spinlock_t reader_lock; /* serialize readers */
247 struct lock_class_key lock_key;
248 struct list_head pages;
249 struct buffer_page *head_page; /* read from head */
250 struct buffer_page *tail_page; /* write to tail */
251 struct buffer_page *commit_page; /* commited pages */
252 struct buffer_page *reader_page;
253 unsigned long overrun;
254 unsigned long entries;
257 atomic_t record_disabled;
265 atomic_t record_disabled;
269 struct ring_buffer_per_cpu **buffers;
272 struct ring_buffer_iter {
273 struct ring_buffer_per_cpu *cpu_buffer;
275 struct buffer_page *head_page;
279 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
280 #define RB_WARN_ON(buffer, cond) \
282 int _____ret = unlikely(cond); \
284 atomic_inc(&buffer->record_disabled); \
291 * check_pages - integrity check of buffer pages
292 * @cpu_buffer: CPU buffer with pages to test
294 * As a safty measure we check to make sure the data pages have not
297 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
299 struct list_head *head = &cpu_buffer->pages;
300 struct buffer_page *bpage, *tmp;
302 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
304 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
307 list_for_each_entry_safe(bpage, tmp, head, list) {
308 if (RB_WARN_ON(cpu_buffer,
309 bpage->list.next->prev != &bpage->list))
311 if (RB_WARN_ON(cpu_buffer,
312 bpage->list.prev->next != &bpage->list))
319 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
322 struct list_head *head = &cpu_buffer->pages;
323 struct buffer_page *bpage, *tmp;
328 for (i = 0; i < nr_pages; i++) {
329 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
330 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
333 list_add(&bpage->list, &pages);
335 addr = __get_free_page(GFP_KERNEL);
338 bpage->page = (void *)addr;
339 rb_init_page(bpage->page);
342 list_splice(&pages, head);
344 rb_check_pages(cpu_buffer);
349 list_for_each_entry_safe(bpage, tmp, &pages, list) {
350 list_del_init(&bpage->list);
351 free_buffer_page(bpage);
356 static struct ring_buffer_per_cpu *
357 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
359 struct ring_buffer_per_cpu *cpu_buffer;
360 struct buffer_page *bpage;
364 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
365 GFP_KERNEL, cpu_to_node(cpu));
369 cpu_buffer->cpu = cpu;
370 cpu_buffer->buffer = buffer;
371 spin_lock_init(&cpu_buffer->reader_lock);
372 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
373 INIT_LIST_HEAD(&cpu_buffer->pages);
375 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
376 GFP_KERNEL, cpu_to_node(cpu));
378 goto fail_free_buffer;
380 cpu_buffer->reader_page = bpage;
381 addr = __get_free_page(GFP_KERNEL);
383 goto fail_free_reader;
384 bpage->page = (void *)addr;
385 rb_init_page(bpage->page);
387 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
389 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
391 goto fail_free_reader;
393 cpu_buffer->head_page
394 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
395 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
400 free_buffer_page(cpu_buffer->reader_page);
407 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
409 struct list_head *head = &cpu_buffer->pages;
410 struct buffer_page *bpage, *tmp;
412 list_del_init(&cpu_buffer->reader_page->list);
413 free_buffer_page(cpu_buffer->reader_page);
415 list_for_each_entry_safe(bpage, tmp, head, list) {
416 list_del_init(&bpage->list);
417 free_buffer_page(bpage);
423 * Causes compile errors if the struct buffer_page gets bigger
424 * than the struct page.
426 extern int ring_buffer_page_too_big(void);
429 * ring_buffer_alloc - allocate a new ring_buffer
430 * @size: the size in bytes that is needed.
431 * @flags: attributes to set for the ring buffer.
433 * Currently the only flag that is available is the RB_FL_OVERWRITE
434 * flag. This flag means that the buffer will overwrite old data
435 * when the buffer wraps. If this flag is not set, the buffer will
436 * drop data when the tail hits the head.
438 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
440 struct ring_buffer *buffer;
444 /* Paranoid! Optimizes out when all is well */
445 if (sizeof(struct buffer_page) > sizeof(struct page))
446 ring_buffer_page_too_big();
449 /* keep it in its own cache line */
450 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
455 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
456 buffer->flags = flags;
458 /* need at least two pages */
459 if (buffer->pages == 1)
462 buffer->cpumask = cpu_possible_map;
463 buffer->cpus = nr_cpu_ids;
465 bsize = sizeof(void *) * nr_cpu_ids;
466 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
468 if (!buffer->buffers)
469 goto fail_free_buffer;
471 for_each_buffer_cpu(buffer, cpu) {
472 buffer->buffers[cpu] =
473 rb_allocate_cpu_buffer(buffer, cpu);
474 if (!buffer->buffers[cpu])
475 goto fail_free_buffers;
478 mutex_init(&buffer->mutex);
483 for_each_buffer_cpu(buffer, cpu) {
484 if (buffer->buffers[cpu])
485 rb_free_cpu_buffer(buffer->buffers[cpu]);
487 kfree(buffer->buffers);
495 * ring_buffer_free - free a ring buffer.
496 * @buffer: the buffer to free.
499 ring_buffer_free(struct ring_buffer *buffer)
503 for_each_buffer_cpu(buffer, cpu)
504 rb_free_cpu_buffer(buffer->buffers[cpu]);
509 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
512 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
514 struct buffer_page *bpage;
518 atomic_inc(&cpu_buffer->record_disabled);
521 for (i = 0; i < nr_pages; i++) {
522 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
524 p = cpu_buffer->pages.next;
525 bpage = list_entry(p, struct buffer_page, list);
526 list_del_init(&bpage->list);
527 free_buffer_page(bpage);
529 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
532 rb_reset_cpu(cpu_buffer);
534 rb_check_pages(cpu_buffer);
536 atomic_dec(&cpu_buffer->record_disabled);
541 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
542 struct list_head *pages, unsigned nr_pages)
544 struct buffer_page *bpage;
548 atomic_inc(&cpu_buffer->record_disabled);
551 for (i = 0; i < nr_pages; i++) {
552 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
555 bpage = list_entry(p, struct buffer_page, list);
556 list_del_init(&bpage->list);
557 list_add_tail(&bpage->list, &cpu_buffer->pages);
559 rb_reset_cpu(cpu_buffer);
561 rb_check_pages(cpu_buffer);
563 atomic_dec(&cpu_buffer->record_disabled);
567 * ring_buffer_resize - resize the ring buffer
568 * @buffer: the buffer to resize.
569 * @size: the new size.
571 * The tracer is responsible for making sure that the buffer is
572 * not being used while changing the size.
573 * Note: We may be able to change the above requirement by using
574 * RCU synchronizations.
576 * Minimum size is 2 * BUF_PAGE_SIZE.
578 * Returns -1 on failure.
580 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
582 struct ring_buffer_per_cpu *cpu_buffer;
583 unsigned nr_pages, rm_pages, new_pages;
584 struct buffer_page *bpage, *tmp;
585 unsigned long buffer_size;
591 * Always succeed at resizing a non-existent buffer:
596 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
597 size *= BUF_PAGE_SIZE;
598 buffer_size = buffer->pages * BUF_PAGE_SIZE;
600 /* we need a minimum of two pages */
601 if (size < BUF_PAGE_SIZE * 2)
602 size = BUF_PAGE_SIZE * 2;
604 if (size == buffer_size)
607 mutex_lock(&buffer->mutex);
609 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
611 if (size < buffer_size) {
613 /* easy case, just free pages */
614 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
615 mutex_unlock(&buffer->mutex);
619 rm_pages = buffer->pages - nr_pages;
621 for_each_buffer_cpu(buffer, cpu) {
622 cpu_buffer = buffer->buffers[cpu];
623 rb_remove_pages(cpu_buffer, rm_pages);
629 * This is a bit more difficult. We only want to add pages
630 * when we can allocate enough for all CPUs. We do this
631 * by allocating all the pages and storing them on a local
632 * link list. If we succeed in our allocation, then we
633 * add these pages to the cpu_buffers. Otherwise we just free
634 * them all and return -ENOMEM;
636 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
637 mutex_unlock(&buffer->mutex);
641 new_pages = nr_pages - buffer->pages;
643 for_each_buffer_cpu(buffer, cpu) {
644 for (i = 0; i < new_pages; i++) {
645 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
647 GFP_KERNEL, cpu_to_node(cpu));
650 list_add(&bpage->list, &pages);
651 addr = __get_free_page(GFP_KERNEL);
654 bpage->page = (void *)addr;
655 rb_init_page(bpage->page);
659 for_each_buffer_cpu(buffer, cpu) {
660 cpu_buffer = buffer->buffers[cpu];
661 rb_insert_pages(cpu_buffer, &pages, new_pages);
664 if (RB_WARN_ON(buffer, !list_empty(&pages))) {
665 mutex_unlock(&buffer->mutex);
670 buffer->pages = nr_pages;
671 mutex_unlock(&buffer->mutex);
676 list_for_each_entry_safe(bpage, tmp, &pages, list) {
677 list_del_init(&bpage->list);
678 free_buffer_page(bpage);
680 mutex_unlock(&buffer->mutex);
684 static inline int rb_null_event(struct ring_buffer_event *event)
686 return event->type == RINGBUF_TYPE_PADDING;
690 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
692 return bpage->data + index;
695 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
697 return bpage->page->data + index;
700 static inline struct ring_buffer_event *
701 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
703 return __rb_page_index(cpu_buffer->reader_page,
704 cpu_buffer->reader_page->read);
707 static inline struct ring_buffer_event *
708 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
710 return __rb_page_index(cpu_buffer->head_page,
711 cpu_buffer->head_page->read);
714 static inline struct ring_buffer_event *
715 rb_iter_head_event(struct ring_buffer_iter *iter)
717 return __rb_page_index(iter->head_page, iter->head);
720 static inline unsigned rb_page_write(struct buffer_page *bpage)
722 return local_read(&bpage->write);
725 static inline unsigned rb_page_commit(struct buffer_page *bpage)
727 return local_read(&bpage->page->commit);
730 /* Size is determined by what has been commited */
731 static inline unsigned rb_page_size(struct buffer_page *bpage)
733 return rb_page_commit(bpage);
736 static inline unsigned
737 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
739 return rb_page_commit(cpu_buffer->commit_page);
742 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
744 return rb_page_commit(cpu_buffer->head_page);
748 * When the tail hits the head and the buffer is in overwrite mode,
749 * the head jumps to the next page and all content on the previous
750 * page is discarded. But before doing so, we update the overrun
751 * variable of the buffer.
753 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
755 struct ring_buffer_event *event;
758 for (head = 0; head < rb_head_size(cpu_buffer);
759 head += rb_event_length(event)) {
761 event = __rb_page_index(cpu_buffer->head_page, head);
762 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
764 /* Only count data entries */
765 if (event->type != RINGBUF_TYPE_DATA)
767 cpu_buffer->overrun++;
768 cpu_buffer->entries--;
772 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
773 struct buffer_page **bpage)
775 struct list_head *p = (*bpage)->list.next;
777 if (p == &cpu_buffer->pages)
780 *bpage = list_entry(p, struct buffer_page, list);
783 static inline unsigned
784 rb_event_index(struct ring_buffer_event *event)
786 unsigned long addr = (unsigned long)event;
788 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
792 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
793 struct ring_buffer_event *event)
795 unsigned long addr = (unsigned long)event;
798 index = rb_event_index(event);
801 return cpu_buffer->commit_page->page == (void *)addr &&
802 rb_commit_index(cpu_buffer) == index;
806 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
807 struct ring_buffer_event *event)
809 unsigned long addr = (unsigned long)event;
812 index = rb_event_index(event);
815 while (cpu_buffer->commit_page->page != (void *)addr) {
816 if (RB_WARN_ON(cpu_buffer,
817 cpu_buffer->commit_page == cpu_buffer->tail_page))
819 cpu_buffer->commit_page->page->commit =
820 cpu_buffer->commit_page->write;
821 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
822 cpu_buffer->write_stamp =
823 cpu_buffer->commit_page->page->time_stamp;
826 /* Now set the commit to the event's index */
827 local_set(&cpu_buffer->commit_page->page->commit, index);
831 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
834 * We only race with interrupts and NMIs on this CPU.
835 * If we own the commit event, then we can commit
836 * all others that interrupted us, since the interruptions
837 * are in stack format (they finish before they come
838 * back to us). This allows us to do a simple loop to
839 * assign the commit to the tail.
842 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
843 cpu_buffer->commit_page->page->commit =
844 cpu_buffer->commit_page->write;
845 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
846 cpu_buffer->write_stamp =
847 cpu_buffer->commit_page->page->time_stamp;
848 /* add barrier to keep gcc from optimizing too much */
851 while (rb_commit_index(cpu_buffer) !=
852 rb_page_write(cpu_buffer->commit_page)) {
853 cpu_buffer->commit_page->page->commit =
854 cpu_buffer->commit_page->write;
858 /* again, keep gcc from optimizing */
862 * If an interrupt came in just after the first while loop
863 * and pushed the tail page forward, we will be left with
864 * a dangling commit that will never go forward.
866 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
870 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
872 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
873 cpu_buffer->reader_page->read = 0;
876 static inline void rb_inc_iter(struct ring_buffer_iter *iter)
878 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
881 * The iterator could be on the reader page (it starts there).
882 * But the head could have moved, since the reader was
883 * found. Check for this case and assign the iterator
884 * to the head page instead of next.
886 if (iter->head_page == cpu_buffer->reader_page)
887 iter->head_page = cpu_buffer->head_page;
889 rb_inc_page(cpu_buffer, &iter->head_page);
891 iter->read_stamp = iter->head_page->page->time_stamp;
896 * ring_buffer_update_event - update event type and data
897 * @event: the even to update
898 * @type: the type of event
899 * @length: the size of the event field in the ring buffer
901 * Update the type and data fields of the event. The length
902 * is the actual size that is written to the ring buffer,
903 * and with this, we can determine what to place into the
907 rb_update_event(struct ring_buffer_event *event,
908 unsigned type, unsigned length)
914 case RINGBUF_TYPE_PADDING:
917 case RINGBUF_TYPE_TIME_EXTEND:
919 (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1))
920 >> RB_ALIGNMENT_SHIFT;
923 case RINGBUF_TYPE_TIME_STAMP:
925 (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1))
926 >> RB_ALIGNMENT_SHIFT;
929 case RINGBUF_TYPE_DATA:
930 length -= RB_EVNT_HDR_SIZE;
931 if (length > RB_MAX_SMALL_DATA) {
933 event->array[0] = length;
936 (length + (RB_ALIGNMENT-1))
937 >> RB_ALIGNMENT_SHIFT;
944 static inline unsigned rb_calculate_event_length(unsigned length)
946 struct ring_buffer_event event; /* Used only for sizeof array */
948 /* zero length can cause confusions */
952 if (length > RB_MAX_SMALL_DATA)
953 length += sizeof(event.array[0]);
955 length += RB_EVNT_HDR_SIZE;
956 length = ALIGN(length, RB_ALIGNMENT);
961 static struct ring_buffer_event *
962 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
963 unsigned type, unsigned long length, u64 *ts)
965 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
966 unsigned long tail, write;
967 struct ring_buffer *buffer = cpu_buffer->buffer;
968 struct ring_buffer_event *event;
971 commit_page = cpu_buffer->commit_page;
972 /* we just need to protect against interrupts */
974 tail_page = cpu_buffer->tail_page;
975 write = local_add_return(length, &tail_page->write);
976 tail = write - length;
978 /* See if we shot pass the end of this buffer page */
979 if (write > BUF_PAGE_SIZE) {
980 struct buffer_page *next_page = tail_page;
982 local_irq_save(flags);
983 __raw_spin_lock(&cpu_buffer->lock);
985 rb_inc_page(cpu_buffer, &next_page);
987 head_page = cpu_buffer->head_page;
988 reader_page = cpu_buffer->reader_page;
990 /* we grabbed the lock before incrementing */
991 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
995 * If for some reason, we had an interrupt storm that made
996 * it all the way around the buffer, bail, and warn
999 if (unlikely(next_page == commit_page)) {
1004 if (next_page == head_page) {
1005 if (!(buffer->flags & RB_FL_OVERWRITE)) {
1007 if (tail <= BUF_PAGE_SIZE)
1008 local_set(&tail_page->write, tail);
1012 /* tail_page has not moved yet? */
1013 if (tail_page == cpu_buffer->tail_page) {
1014 /* count overflows */
1015 rb_update_overflow(cpu_buffer);
1017 rb_inc_page(cpu_buffer, &head_page);
1018 cpu_buffer->head_page = head_page;
1019 cpu_buffer->head_page->read = 0;
1024 * If the tail page is still the same as what we think
1025 * it is, then it is up to us to update the tail
1028 if (tail_page == cpu_buffer->tail_page) {
1029 local_set(&next_page->write, 0);
1030 local_set(&next_page->page->commit, 0);
1031 cpu_buffer->tail_page = next_page;
1033 /* reread the time stamp */
1034 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1035 cpu_buffer->tail_page->page->time_stamp = *ts;
1039 * The actual tail page has moved forward.
1041 if (tail < BUF_PAGE_SIZE) {
1042 /* Mark the rest of the page with padding */
1043 event = __rb_page_index(tail_page, tail);
1044 event->type = RINGBUF_TYPE_PADDING;
1047 if (tail <= BUF_PAGE_SIZE)
1048 /* Set the write back to the previous setting */
1049 local_set(&tail_page->write, tail);
1052 * If this was a commit entry that failed,
1053 * increment that too
1055 if (tail_page == cpu_buffer->commit_page &&
1056 tail == rb_commit_index(cpu_buffer)) {
1057 rb_set_commit_to_write(cpu_buffer);
1060 __raw_spin_unlock(&cpu_buffer->lock);
1061 local_irq_restore(flags);
1063 /* fail and let the caller try again */
1064 return ERR_PTR(-EAGAIN);
1067 /* We reserved something on the buffer */
1069 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1072 event = __rb_page_index(tail_page, tail);
1073 rb_update_event(event, type, length);
1076 * If this is a commit and the tail is zero, then update
1077 * this page's time stamp.
1079 if (!tail && rb_is_commit(cpu_buffer, event))
1080 cpu_buffer->commit_page->page->time_stamp = *ts;
1085 __raw_spin_unlock(&cpu_buffer->lock);
1086 local_irq_restore(flags);
1091 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1092 u64 *ts, u64 *delta)
1094 struct ring_buffer_event *event;
1098 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1099 printk(KERN_WARNING "Delta way too big! %llu"
1100 " ts=%llu write stamp = %llu\n",
1101 (unsigned long long)*delta,
1102 (unsigned long long)*ts,
1103 (unsigned long long)cpu_buffer->write_stamp);
1108 * The delta is too big, we to add a
1111 event = __rb_reserve_next(cpu_buffer,
1112 RINGBUF_TYPE_TIME_EXTEND,
1118 if (PTR_ERR(event) == -EAGAIN)
1121 /* Only a commited time event can update the write stamp */
1122 if (rb_is_commit(cpu_buffer, event)) {
1124 * If this is the first on the page, then we need to
1125 * update the page itself, and just put in a zero.
1127 if (rb_event_index(event)) {
1128 event->time_delta = *delta & TS_MASK;
1129 event->array[0] = *delta >> TS_SHIFT;
1131 cpu_buffer->commit_page->page->time_stamp = *ts;
1132 event->time_delta = 0;
1133 event->array[0] = 0;
1135 cpu_buffer->write_stamp = *ts;
1136 /* let the caller know this was the commit */
1139 /* Darn, this is just wasted space */
1140 event->time_delta = 0;
1141 event->array[0] = 0;
1150 static struct ring_buffer_event *
1151 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1152 unsigned type, unsigned long length)
1154 struct ring_buffer_event *event;
1161 * We allow for interrupts to reenter here and do a trace.
1162 * If one does, it will cause this original code to loop
1163 * back here. Even with heavy interrupts happening, this
1164 * should only happen a few times in a row. If this happens
1165 * 1000 times in a row, there must be either an interrupt
1166 * storm or we have something buggy.
1169 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1172 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1175 * Only the first commit can update the timestamp.
1176 * Yes there is a race here. If an interrupt comes in
1177 * just after the conditional and it traces too, then it
1178 * will also check the deltas. More than one timestamp may
1179 * also be made. But only the entry that did the actual
1180 * commit will be something other than zero.
1182 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1183 rb_page_write(cpu_buffer->tail_page) ==
1184 rb_commit_index(cpu_buffer)) {
1186 delta = ts - cpu_buffer->write_stamp;
1188 /* make sure this delta is calculated here */
1191 /* Did the write stamp get updated already? */
1192 if (unlikely(ts < cpu_buffer->write_stamp))
1195 if (test_time_stamp(delta)) {
1197 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1199 if (commit == -EBUSY)
1202 if (commit == -EAGAIN)
1205 RB_WARN_ON(cpu_buffer, commit < 0);
1208 /* Non commits have zero deltas */
1211 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1212 if (PTR_ERR(event) == -EAGAIN)
1216 if (unlikely(commit))
1218 * Ouch! We needed a timestamp and it was commited. But
1219 * we didn't get our event reserved.
1221 rb_set_commit_to_write(cpu_buffer);
1226 * If the timestamp was commited, make the commit our entry
1227 * now so that we will update it when needed.
1230 rb_set_commit_event(cpu_buffer, event);
1231 else if (!rb_is_commit(cpu_buffer, event))
1234 event->time_delta = delta;
1239 static DEFINE_PER_CPU(int, rb_need_resched);
1242 * ring_buffer_lock_reserve - reserve a part of the buffer
1243 * @buffer: the ring buffer to reserve from
1244 * @length: the length of the data to reserve (excluding event header)
1245 * @flags: a pointer to save the interrupt flags
1247 * Returns a reseverd event on the ring buffer to copy directly to.
1248 * The user of this interface will need to get the body to write into
1249 * and can use the ring_buffer_event_data() interface.
1251 * The length is the length of the data needed, not the event length
1252 * which also includes the event header.
1254 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1255 * If NULL is returned, then nothing has been allocated or locked.
1257 struct ring_buffer_event *
1258 ring_buffer_lock_reserve(struct ring_buffer *buffer,
1259 unsigned long length,
1260 unsigned long *flags)
1262 struct ring_buffer_per_cpu *cpu_buffer;
1263 struct ring_buffer_event *event;
1266 if (ring_buffer_flags != RB_BUFFERS_ON)
1269 if (atomic_read(&buffer->record_disabled))
1272 /* If we are tracing schedule, we don't want to recurse */
1273 resched = ftrace_preempt_disable();
1275 cpu = raw_smp_processor_id();
1277 if (!cpu_isset(cpu, buffer->cpumask))
1280 cpu_buffer = buffer->buffers[cpu];
1282 if (atomic_read(&cpu_buffer->record_disabled))
1285 length = rb_calculate_event_length(length);
1286 if (length > BUF_PAGE_SIZE)
1289 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1294 * Need to store resched state on this cpu.
1295 * Only the first needs to.
1298 if (preempt_count() == 1)
1299 per_cpu(rb_need_resched, cpu) = resched;
1304 ftrace_preempt_enable(resched);
1308 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1309 struct ring_buffer_event *event)
1311 cpu_buffer->entries++;
1313 /* Only process further if we own the commit */
1314 if (!rb_is_commit(cpu_buffer, event))
1317 cpu_buffer->write_stamp += event->time_delta;
1319 rb_set_commit_to_write(cpu_buffer);
1323 * ring_buffer_unlock_commit - commit a reserved
1324 * @buffer: The buffer to commit to
1325 * @event: The event pointer to commit.
1326 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1328 * This commits the data to the ring buffer, and releases any locks held.
1330 * Must be paired with ring_buffer_lock_reserve.
1332 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1333 struct ring_buffer_event *event,
1334 unsigned long flags)
1336 struct ring_buffer_per_cpu *cpu_buffer;
1337 int cpu = raw_smp_processor_id();
1339 cpu_buffer = buffer->buffers[cpu];
1341 rb_commit(cpu_buffer, event);
1344 * Only the last preempt count needs to restore preemption.
1346 if (preempt_count() == 1)
1347 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1349 preempt_enable_no_resched_notrace();
1355 * ring_buffer_write - write data to the buffer without reserving
1356 * @buffer: The ring buffer to write to.
1357 * @length: The length of the data being written (excluding the event header)
1358 * @data: The data to write to the buffer.
1360 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1361 * one function. If you already have the data to write to the buffer, it
1362 * may be easier to simply call this function.
1364 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1365 * and not the length of the event which would hold the header.
1367 int ring_buffer_write(struct ring_buffer *buffer,
1368 unsigned long length,
1371 struct ring_buffer_per_cpu *cpu_buffer;
1372 struct ring_buffer_event *event;
1373 unsigned long event_length;
1378 if (ring_buffer_flags != RB_BUFFERS_ON)
1381 if (atomic_read(&buffer->record_disabled))
1384 resched = ftrace_preempt_disable();
1386 cpu = raw_smp_processor_id();
1388 if (!cpu_isset(cpu, buffer->cpumask))
1391 cpu_buffer = buffer->buffers[cpu];
1393 if (atomic_read(&cpu_buffer->record_disabled))
1396 event_length = rb_calculate_event_length(length);
1397 event = rb_reserve_next_event(cpu_buffer,
1398 RINGBUF_TYPE_DATA, event_length);
1402 body = rb_event_data(event);
1404 memcpy(body, data, length);
1406 rb_commit(cpu_buffer, event);
1410 ftrace_preempt_enable(resched);
1415 static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1417 struct buffer_page *reader = cpu_buffer->reader_page;
1418 struct buffer_page *head = cpu_buffer->head_page;
1419 struct buffer_page *commit = cpu_buffer->commit_page;
1421 return reader->read == rb_page_commit(reader) &&
1422 (commit == reader ||
1424 head->read == rb_page_commit(commit)));
1428 * ring_buffer_record_disable - stop all writes into the buffer
1429 * @buffer: The ring buffer to stop writes to.
1431 * This prevents all writes to the buffer. Any attempt to write
1432 * to the buffer after this will fail and return NULL.
1434 * The caller should call synchronize_sched() after this.
1436 void ring_buffer_record_disable(struct ring_buffer *buffer)
1438 atomic_inc(&buffer->record_disabled);
1442 * ring_buffer_record_enable - enable writes to the buffer
1443 * @buffer: The ring buffer to enable writes
1445 * Note, multiple disables will need the same number of enables
1446 * to truely enable the writing (much like preempt_disable).
1448 void ring_buffer_record_enable(struct ring_buffer *buffer)
1450 atomic_dec(&buffer->record_disabled);
1454 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1455 * @buffer: The ring buffer to stop writes to.
1456 * @cpu: The CPU buffer to stop
1458 * This prevents all writes to the buffer. Any attempt to write
1459 * to the buffer after this will fail and return NULL.
1461 * The caller should call synchronize_sched() after this.
1463 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1465 struct ring_buffer_per_cpu *cpu_buffer;
1467 if (!cpu_isset(cpu, buffer->cpumask))
1470 cpu_buffer = buffer->buffers[cpu];
1471 atomic_inc(&cpu_buffer->record_disabled);
1475 * ring_buffer_record_enable_cpu - enable writes to the buffer
1476 * @buffer: The ring buffer to enable writes
1477 * @cpu: The CPU to enable.
1479 * Note, multiple disables will need the same number of enables
1480 * to truely enable the writing (much like preempt_disable).
1482 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1484 struct ring_buffer_per_cpu *cpu_buffer;
1486 if (!cpu_isset(cpu, buffer->cpumask))
1489 cpu_buffer = buffer->buffers[cpu];
1490 atomic_dec(&cpu_buffer->record_disabled);
1494 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1495 * @buffer: The ring buffer
1496 * @cpu: The per CPU buffer to get the entries from.
1498 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1500 struct ring_buffer_per_cpu *cpu_buffer;
1502 if (!cpu_isset(cpu, buffer->cpumask))
1505 cpu_buffer = buffer->buffers[cpu];
1506 return cpu_buffer->entries;
1510 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1511 * @buffer: The ring buffer
1512 * @cpu: The per CPU buffer to get the number of overruns from
1514 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1516 struct ring_buffer_per_cpu *cpu_buffer;
1518 if (!cpu_isset(cpu, buffer->cpumask))
1521 cpu_buffer = buffer->buffers[cpu];
1522 return cpu_buffer->overrun;
1526 * ring_buffer_entries - get the number of entries in a buffer
1527 * @buffer: The ring buffer
1529 * Returns the total number of entries in the ring buffer
1532 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1534 struct ring_buffer_per_cpu *cpu_buffer;
1535 unsigned long entries = 0;
1538 /* if you care about this being correct, lock the buffer */
1539 for_each_buffer_cpu(buffer, cpu) {
1540 cpu_buffer = buffer->buffers[cpu];
1541 entries += cpu_buffer->entries;
1548 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1549 * @buffer: The ring buffer
1551 * Returns the total number of overruns in the ring buffer
1554 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1556 struct ring_buffer_per_cpu *cpu_buffer;
1557 unsigned long overruns = 0;
1560 /* if you care about this being correct, lock the buffer */
1561 for_each_buffer_cpu(buffer, cpu) {
1562 cpu_buffer = buffer->buffers[cpu];
1563 overruns += cpu_buffer->overrun;
1569 static void rb_iter_reset(struct ring_buffer_iter *iter)
1571 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1573 /* Iterator usage is expected to have record disabled */
1574 if (list_empty(&cpu_buffer->reader_page->list)) {
1575 iter->head_page = cpu_buffer->head_page;
1576 iter->head = cpu_buffer->head_page->read;
1578 iter->head_page = cpu_buffer->reader_page;
1579 iter->head = cpu_buffer->reader_page->read;
1582 iter->read_stamp = cpu_buffer->read_stamp;
1584 iter->read_stamp = iter->head_page->page->time_stamp;
1588 * ring_buffer_iter_reset - reset an iterator
1589 * @iter: The iterator to reset
1591 * Resets the iterator, so that it will start from the beginning
1594 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1596 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1597 unsigned long flags;
1599 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1600 rb_iter_reset(iter);
1601 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1605 * ring_buffer_iter_empty - check if an iterator has no more to read
1606 * @iter: The iterator to check
1608 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1610 struct ring_buffer_per_cpu *cpu_buffer;
1612 cpu_buffer = iter->cpu_buffer;
1614 return iter->head_page == cpu_buffer->commit_page &&
1615 iter->head == rb_commit_index(cpu_buffer);
1619 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1620 struct ring_buffer_event *event)
1624 switch (event->type) {
1625 case RINGBUF_TYPE_PADDING:
1628 case RINGBUF_TYPE_TIME_EXTEND:
1629 delta = event->array[0];
1631 delta += event->time_delta;
1632 cpu_buffer->read_stamp += delta;
1635 case RINGBUF_TYPE_TIME_STAMP:
1636 /* FIXME: not implemented */
1639 case RINGBUF_TYPE_DATA:
1640 cpu_buffer->read_stamp += event->time_delta;
1650 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1651 struct ring_buffer_event *event)
1655 switch (event->type) {
1656 case RINGBUF_TYPE_PADDING:
1659 case RINGBUF_TYPE_TIME_EXTEND:
1660 delta = event->array[0];
1662 delta += event->time_delta;
1663 iter->read_stamp += delta;
1666 case RINGBUF_TYPE_TIME_STAMP:
1667 /* FIXME: not implemented */
1670 case RINGBUF_TYPE_DATA:
1671 iter->read_stamp += event->time_delta;
1680 static struct buffer_page *
1681 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1683 struct buffer_page *reader = NULL;
1684 unsigned long flags;
1687 local_irq_save(flags);
1688 __raw_spin_lock(&cpu_buffer->lock);
1692 * This should normally only loop twice. But because the
1693 * start of the reader inserts an empty page, it causes
1694 * a case where we will loop three times. There should be no
1695 * reason to loop four times (that I know of).
1697 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
1702 reader = cpu_buffer->reader_page;
1704 /* If there's more to read, return this page */
1705 if (cpu_buffer->reader_page->read < rb_page_size(reader))
1708 /* Never should we have an index greater than the size */
1709 if (RB_WARN_ON(cpu_buffer,
1710 cpu_buffer->reader_page->read > rb_page_size(reader)))
1713 /* check if we caught up to the tail */
1715 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
1719 * Splice the empty reader page into the list around the head.
1720 * Reset the reader page to size zero.
1723 reader = cpu_buffer->head_page;
1724 cpu_buffer->reader_page->list.next = reader->list.next;
1725 cpu_buffer->reader_page->list.prev = reader->list.prev;
1727 local_set(&cpu_buffer->reader_page->write, 0);
1728 local_set(&cpu_buffer->reader_page->page->commit, 0);
1730 /* Make the reader page now replace the head */
1731 reader->list.prev->next = &cpu_buffer->reader_page->list;
1732 reader->list.next->prev = &cpu_buffer->reader_page->list;
1735 * If the tail is on the reader, then we must set the head
1736 * to the inserted page, otherwise we set it one before.
1738 cpu_buffer->head_page = cpu_buffer->reader_page;
1740 if (cpu_buffer->commit_page != reader)
1741 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1743 /* Finally update the reader page to the new head */
1744 cpu_buffer->reader_page = reader;
1745 rb_reset_reader_page(cpu_buffer);
1750 __raw_spin_unlock(&cpu_buffer->lock);
1751 local_irq_restore(flags);
1756 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1758 struct ring_buffer_event *event;
1759 struct buffer_page *reader;
1762 reader = rb_get_reader_page(cpu_buffer);
1764 /* This function should not be called when buffer is empty */
1765 if (RB_WARN_ON(cpu_buffer, !reader))
1768 event = rb_reader_event(cpu_buffer);
1770 if (event->type == RINGBUF_TYPE_DATA)
1771 cpu_buffer->entries--;
1773 rb_update_read_stamp(cpu_buffer, event);
1775 length = rb_event_length(event);
1776 cpu_buffer->reader_page->read += length;
1779 static void rb_advance_iter(struct ring_buffer_iter *iter)
1781 struct ring_buffer *buffer;
1782 struct ring_buffer_per_cpu *cpu_buffer;
1783 struct ring_buffer_event *event;
1786 cpu_buffer = iter->cpu_buffer;
1787 buffer = cpu_buffer->buffer;
1790 * Check if we are at the end of the buffer.
1792 if (iter->head >= rb_page_size(iter->head_page)) {
1793 if (RB_WARN_ON(buffer,
1794 iter->head_page == cpu_buffer->commit_page))
1800 event = rb_iter_head_event(iter);
1802 length = rb_event_length(event);
1805 * This should not be called to advance the header if we are
1806 * at the tail of the buffer.
1808 if (RB_WARN_ON(cpu_buffer,
1809 (iter->head_page == cpu_buffer->commit_page) &&
1810 (iter->head + length > rb_commit_index(cpu_buffer))))
1813 rb_update_iter_read_stamp(iter, event);
1815 iter->head += length;
1817 /* check for end of page padding */
1818 if ((iter->head >= rb_page_size(iter->head_page)) &&
1819 (iter->head_page != cpu_buffer->commit_page))
1820 rb_advance_iter(iter);
1823 static struct ring_buffer_event *
1824 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1826 struct ring_buffer_per_cpu *cpu_buffer;
1827 struct ring_buffer_event *event;
1828 struct buffer_page *reader;
1831 if (!cpu_isset(cpu, buffer->cpumask))
1834 cpu_buffer = buffer->buffers[cpu];
1838 * We repeat when a timestamp is encountered. It is possible
1839 * to get multiple timestamps from an interrupt entering just
1840 * as one timestamp is about to be written. The max times
1841 * that this can happen is the number of nested interrupts we
1842 * can have. Nesting 10 deep of interrupts is clearly
1845 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1848 reader = rb_get_reader_page(cpu_buffer);
1852 event = rb_reader_event(cpu_buffer);
1854 switch (event->type) {
1855 case RINGBUF_TYPE_PADDING:
1856 RB_WARN_ON(cpu_buffer, 1);
1857 rb_advance_reader(cpu_buffer);
1860 case RINGBUF_TYPE_TIME_EXTEND:
1861 /* Internal data, OK to advance */
1862 rb_advance_reader(cpu_buffer);
1865 case RINGBUF_TYPE_TIME_STAMP:
1866 /* FIXME: not implemented */
1867 rb_advance_reader(cpu_buffer);
1870 case RINGBUF_TYPE_DATA:
1872 *ts = cpu_buffer->read_stamp + event->time_delta;
1873 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1884 static struct ring_buffer_event *
1885 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1887 struct ring_buffer *buffer;
1888 struct ring_buffer_per_cpu *cpu_buffer;
1889 struct ring_buffer_event *event;
1892 if (ring_buffer_iter_empty(iter))
1895 cpu_buffer = iter->cpu_buffer;
1896 buffer = cpu_buffer->buffer;
1900 * We repeat when a timestamp is encountered. It is possible
1901 * to get multiple timestamps from an interrupt entering just
1902 * as one timestamp is about to be written. The max times
1903 * that this can happen is the number of nested interrupts we
1904 * can have. Nesting 10 deep of interrupts is clearly
1907 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1910 if (rb_per_cpu_empty(cpu_buffer))
1913 event = rb_iter_head_event(iter);
1915 switch (event->type) {
1916 case RINGBUF_TYPE_PADDING:
1920 case RINGBUF_TYPE_TIME_EXTEND:
1921 /* Internal data, OK to advance */
1922 rb_advance_iter(iter);
1925 case RINGBUF_TYPE_TIME_STAMP:
1926 /* FIXME: not implemented */
1927 rb_advance_iter(iter);
1930 case RINGBUF_TYPE_DATA:
1932 *ts = iter->read_stamp + event->time_delta;
1933 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1945 * ring_buffer_peek - peek at the next event to be read
1946 * @buffer: The ring buffer to read
1947 * @cpu: The cpu to peak at
1948 * @ts: The timestamp counter of this event.
1950 * This will return the event that will be read next, but does
1951 * not consume the data.
1953 struct ring_buffer_event *
1954 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1956 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1957 struct ring_buffer_event *event;
1958 unsigned long flags;
1960 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1961 event = rb_buffer_peek(buffer, cpu, ts);
1962 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1968 * ring_buffer_iter_peek - peek at the next event to be read
1969 * @iter: The ring buffer iterator
1970 * @ts: The timestamp counter of this event.
1972 * This will return the event that will be read next, but does
1973 * not increment the iterator.
1975 struct ring_buffer_event *
1976 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1978 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1979 struct ring_buffer_event *event;
1980 unsigned long flags;
1982 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1983 event = rb_iter_peek(iter, ts);
1984 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1990 * ring_buffer_consume - return an event and consume it
1991 * @buffer: The ring buffer to get the next event from
1993 * Returns the next event in the ring buffer, and that event is consumed.
1994 * Meaning, that sequential reads will keep returning a different event,
1995 * and eventually empty the ring buffer if the producer is slower.
1997 struct ring_buffer_event *
1998 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2000 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2001 struct ring_buffer_event *event;
2002 unsigned long flags;
2004 if (!cpu_isset(cpu, buffer->cpumask))
2007 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2009 event = rb_buffer_peek(buffer, cpu, ts);
2013 rb_advance_reader(cpu_buffer);
2016 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2022 * ring_buffer_read_start - start a non consuming read of the buffer
2023 * @buffer: The ring buffer to read from
2024 * @cpu: The cpu buffer to iterate over
2026 * This starts up an iteration through the buffer. It also disables
2027 * the recording to the buffer until the reading is finished.
2028 * This prevents the reading from being corrupted. This is not
2029 * a consuming read, so a producer is not expected.
2031 * Must be paired with ring_buffer_finish.
2033 struct ring_buffer_iter *
2034 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2036 struct ring_buffer_per_cpu *cpu_buffer;
2037 struct ring_buffer_iter *iter;
2038 unsigned long flags;
2040 if (!cpu_isset(cpu, buffer->cpumask))
2043 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2047 cpu_buffer = buffer->buffers[cpu];
2049 iter->cpu_buffer = cpu_buffer;
2051 atomic_inc(&cpu_buffer->record_disabled);
2052 synchronize_sched();
2054 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2055 __raw_spin_lock(&cpu_buffer->lock);
2056 rb_iter_reset(iter);
2057 __raw_spin_unlock(&cpu_buffer->lock);
2058 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2064 * ring_buffer_finish - finish reading the iterator of the buffer
2065 * @iter: The iterator retrieved by ring_buffer_start
2067 * This re-enables the recording to the buffer, and frees the
2071 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2073 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2075 atomic_dec(&cpu_buffer->record_disabled);
2080 * ring_buffer_read - read the next item in the ring buffer by the iterator
2081 * @iter: The ring buffer iterator
2082 * @ts: The time stamp of the event read.
2084 * This reads the next event in the ring buffer and increments the iterator.
2086 struct ring_buffer_event *
2087 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2089 struct ring_buffer_event *event;
2090 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2091 unsigned long flags;
2093 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2094 event = rb_iter_peek(iter, ts);
2098 rb_advance_iter(iter);
2100 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2106 * ring_buffer_size - return the size of the ring buffer (in bytes)
2107 * @buffer: The ring buffer.
2109 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2111 return BUF_PAGE_SIZE * buffer->pages;
2115 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2117 cpu_buffer->head_page
2118 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2119 local_set(&cpu_buffer->head_page->write, 0);
2120 local_set(&cpu_buffer->head_page->page->commit, 0);
2122 cpu_buffer->head_page->read = 0;
2124 cpu_buffer->tail_page = cpu_buffer->head_page;
2125 cpu_buffer->commit_page = cpu_buffer->head_page;
2127 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2128 local_set(&cpu_buffer->reader_page->write, 0);
2129 local_set(&cpu_buffer->reader_page->page->commit, 0);
2130 cpu_buffer->reader_page->read = 0;
2132 cpu_buffer->overrun = 0;
2133 cpu_buffer->entries = 0;
2137 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2138 * @buffer: The ring buffer to reset a per cpu buffer of
2139 * @cpu: The CPU buffer to be reset
2141 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2143 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2144 unsigned long flags;
2146 if (!cpu_isset(cpu, buffer->cpumask))
2149 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2151 __raw_spin_lock(&cpu_buffer->lock);
2153 rb_reset_cpu(cpu_buffer);
2155 __raw_spin_unlock(&cpu_buffer->lock);
2157 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2161 * ring_buffer_reset - reset a ring buffer
2162 * @buffer: The ring buffer to reset all cpu buffers
2164 void ring_buffer_reset(struct ring_buffer *buffer)
2168 for_each_buffer_cpu(buffer, cpu)
2169 ring_buffer_reset_cpu(buffer, cpu);
2173 * rind_buffer_empty - is the ring buffer empty?
2174 * @buffer: The ring buffer to test
2176 int ring_buffer_empty(struct ring_buffer *buffer)
2178 struct ring_buffer_per_cpu *cpu_buffer;
2181 /* yes this is racy, but if you don't like the race, lock the buffer */
2182 for_each_buffer_cpu(buffer, cpu) {
2183 cpu_buffer = buffer->buffers[cpu];
2184 if (!rb_per_cpu_empty(cpu_buffer))
2191 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2192 * @buffer: The ring buffer
2193 * @cpu: The CPU buffer to test
2195 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2197 struct ring_buffer_per_cpu *cpu_buffer;
2199 if (!cpu_isset(cpu, buffer->cpumask))
2202 cpu_buffer = buffer->buffers[cpu];
2203 return rb_per_cpu_empty(cpu_buffer);
2207 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2208 * @buffer_a: One buffer to swap with
2209 * @buffer_b: The other buffer to swap with
2211 * This function is useful for tracers that want to take a "snapshot"
2212 * of a CPU buffer and has another back up buffer lying around.
2213 * it is expected that the tracer handles the cpu buffer not being
2214 * used at the moment.
2216 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2217 struct ring_buffer *buffer_b, int cpu)
2219 struct ring_buffer_per_cpu *cpu_buffer_a;
2220 struct ring_buffer_per_cpu *cpu_buffer_b;
2222 if (!cpu_isset(cpu, buffer_a->cpumask) ||
2223 !cpu_isset(cpu, buffer_b->cpumask))
2226 /* At least make sure the two buffers are somewhat the same */
2227 if (buffer_a->pages != buffer_b->pages)
2230 cpu_buffer_a = buffer_a->buffers[cpu];
2231 cpu_buffer_b = buffer_b->buffers[cpu];
2234 * We can't do a synchronize_sched here because this
2235 * function can be called in atomic context.
2236 * Normally this will be called from the same CPU as cpu.
2237 * If not it's up to the caller to protect this.
2239 atomic_inc(&cpu_buffer_a->record_disabled);
2240 atomic_inc(&cpu_buffer_b->record_disabled);
2242 buffer_a->buffers[cpu] = cpu_buffer_b;
2243 buffer_b->buffers[cpu] = cpu_buffer_a;
2245 cpu_buffer_b->buffer = buffer_a;
2246 cpu_buffer_a->buffer = buffer_b;
2248 atomic_dec(&cpu_buffer_a->record_disabled);
2249 atomic_dec(&cpu_buffer_b->record_disabled);
2254 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2255 struct buffer_data_page *bpage)
2257 struct ring_buffer_event *event;
2260 __raw_spin_lock(&cpu_buffer->lock);
2261 for (head = 0; head < local_read(&bpage->commit);
2262 head += rb_event_length(event)) {
2264 event = __rb_data_page_index(bpage, head);
2265 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2267 /* Only count data entries */
2268 if (event->type != RINGBUF_TYPE_DATA)
2270 cpu_buffer->entries--;
2272 __raw_spin_unlock(&cpu_buffer->lock);
2276 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2277 * @buffer: the buffer to allocate for.
2279 * This function is used in conjunction with ring_buffer_read_page.
2280 * When reading a full page from the ring buffer, these functions
2281 * can be used to speed up the process. The calling function should
2282 * allocate a few pages first with this function. Then when it
2283 * needs to get pages from the ring buffer, it passes the result
2284 * of this function into ring_buffer_read_page, which will swap
2285 * the page that was allocated, with the read page of the buffer.
2288 * The page allocated, or NULL on error.
2290 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2293 struct buffer_data_page *bpage;
2295 addr = __get_free_page(GFP_KERNEL);
2299 bpage = (void *)addr;
2305 * ring_buffer_free_read_page - free an allocated read page
2306 * @buffer: the buffer the page was allocate for
2307 * @data: the page to free
2309 * Free a page allocated from ring_buffer_alloc_read_page.
2311 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2313 free_page((unsigned long)data);
2317 * ring_buffer_read_page - extract a page from the ring buffer
2318 * @buffer: buffer to extract from
2319 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2320 * @cpu: the cpu of the buffer to extract
2321 * @full: should the extraction only happen when the page is full.
2323 * This function will pull out a page from the ring buffer and consume it.
2324 * @data_page must be the address of the variable that was returned
2325 * from ring_buffer_alloc_read_page. This is because the page might be used
2326 * to swap with a page in the ring buffer.
2329 * rpage = ring_buffer_alloc_page(buffer);
2332 * ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
2334 * process_page(rpage);
2336 * When @full is set, the function will not return true unless
2337 * the writer is off the reader page.
2339 * Note: it is up to the calling functions to handle sleeps and wakeups.
2340 * The ring buffer can be used anywhere in the kernel and can not
2341 * blindly call wake_up. The layer that uses the ring buffer must be
2342 * responsible for that.
2345 * 1 if data has been transferred
2346 * 0 if no data has been transferred.
2348 int ring_buffer_read_page(struct ring_buffer *buffer,
2349 void **data_page, int cpu, int full)
2351 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2352 struct ring_buffer_event *event;
2353 struct buffer_data_page *bpage;
2354 unsigned long flags;
2364 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2367 * rb_buffer_peek will get the next ring buffer if
2368 * the current reader page is empty.
2370 event = rb_buffer_peek(buffer, cpu, NULL);
2374 /* check for data */
2375 if (!local_read(&cpu_buffer->reader_page->page->commit))
2378 * If the writer is already off of the read page, then simply
2379 * switch the read page with the given page. Otherwise
2380 * we need to copy the data from the reader to the writer.
2382 if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
2383 unsigned int read = cpu_buffer->reader_page->read;
2387 /* The writer is still on the reader page, we must copy */
2388 bpage = cpu_buffer->reader_page->page;
2390 cpu_buffer->reader_page->page->data + read,
2391 local_read(&bpage->commit) - read);
2393 /* consume what was read */
2394 cpu_buffer->reader_page += read;
2397 /* swap the pages */
2398 rb_init_page(bpage);
2399 bpage = cpu_buffer->reader_page->page;
2400 cpu_buffer->reader_page->page = *data_page;
2401 cpu_buffer->reader_page->read = 0;
2406 /* update the entry counter */
2407 rb_remove_entries(cpu_buffer, bpage);
2409 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2415 rb_simple_read(struct file *filp, char __user *ubuf,
2416 size_t cnt, loff_t *ppos)
2418 long *p = filp->private_data;
2422 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2423 r = sprintf(buf, "permanently disabled\n");
2425 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2427 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2431 rb_simple_write(struct file *filp, const char __user *ubuf,
2432 size_t cnt, loff_t *ppos)
2434 long *p = filp->private_data;
2439 if (cnt >= sizeof(buf))
2442 if (copy_from_user(&buf, ubuf, cnt))
2447 ret = strict_strtoul(buf, 10, &val);
2452 set_bit(RB_BUFFERS_ON_BIT, p);
2454 clear_bit(RB_BUFFERS_ON_BIT, p);
2461 static struct file_operations rb_simple_fops = {
2462 .open = tracing_open_generic,
2463 .read = rb_simple_read,
2464 .write = rb_simple_write,
2468 static __init int rb_init_debugfs(void)
2470 struct dentry *d_tracer;
2471 struct dentry *entry;
2473 d_tracer = tracing_init_dentry();
2475 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2476 &ring_buffer_flags, &rb_simple_fops);
2478 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2483 fs_initcall(rb_init_debugfs);