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>
19 /* Up this if you want to test the TIME_EXTENTS and normalization */
23 u64 ring_buffer_time_stamp(int cpu)
25 /* shift to debug/test normalization and TIME_EXTENTS */
26 return sched_clock() << DEBUG_SHIFT;
29 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
31 /* Just stupid testing the normalize function and deltas */
35 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
36 #define RB_ALIGNMENT_SHIFT 2
37 #define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT)
38 #define RB_MAX_SMALL_DATA 28
41 RB_LEN_TIME_EXTEND = 8,
42 RB_LEN_TIME_STAMP = 16,
45 /* inline for ring buffer fast paths */
46 static inline unsigned
47 rb_event_length(struct ring_buffer_event *event)
51 switch (event->type) {
52 case RINGBUF_TYPE_PADDING:
56 case RINGBUF_TYPE_TIME_EXTEND:
57 return RB_LEN_TIME_EXTEND;
59 case RINGBUF_TYPE_TIME_STAMP:
60 return RB_LEN_TIME_STAMP;
62 case RINGBUF_TYPE_DATA:
64 length = event->len << RB_ALIGNMENT_SHIFT;
66 length = event->array[0];
67 return length + RB_EVNT_HDR_SIZE;
76 * ring_buffer_event_length - return the length of the event
77 * @event: the event to get the length of
79 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
81 return rb_event_length(event);
84 /* inline for ring buffer fast paths */
86 rb_event_data(struct ring_buffer_event *event)
88 BUG_ON(event->type != RINGBUF_TYPE_DATA);
89 /* If length is in len field, then array[0] has the data */
91 return (void *)&event->array[0];
92 /* Otherwise length is in array[0] and array[1] has the data */
93 return (void *)&event->array[1];
97 * ring_buffer_event_data - return the data of the event
98 * @event: the event to get the data from
100 void *ring_buffer_event_data(struct ring_buffer_event *event)
102 return rb_event_data(event);
105 #define for_each_buffer_cpu(buffer, cpu) \
106 for_each_cpu_mask(cpu, buffer->cpumask)
109 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
110 #define TS_DELTA_TEST (~TS_MASK)
113 * This hack stolen from mm/slob.c.
114 * We can store per page timing information in the page frame of the page.
115 * Thanks to Peter Zijlstra for suggesting this idea.
118 u64 time_stamp; /* page time stamp */
119 unsigned size; /* size of page data */
120 struct list_head list; /* list of free pages */
121 void *page; /* Actual data page */
125 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
128 static inline void free_buffer_page(struct buffer_page *bpage)
131 __free_page(bpage->page);
136 * We need to fit the time_stamp delta into 27 bits.
138 static inline int test_time_stamp(u64 delta)
140 if (delta & TS_DELTA_TEST)
145 #define BUF_PAGE_SIZE PAGE_SIZE
148 * head_page == tail_page && head == tail then buffer is empty.
150 struct ring_buffer_per_cpu {
152 struct ring_buffer *buffer;
154 struct lock_class_key lock_key;
155 struct list_head pages;
156 unsigned long head; /* read from head */
157 unsigned long tail; /* write to tail */
158 unsigned long reader;
159 struct buffer_page *head_page;
160 struct buffer_page *tail_page;
161 struct buffer_page *reader_page;
162 unsigned long overrun;
163 unsigned long entries;
166 atomic_t record_disabled;
175 atomic_t record_disabled;
179 struct ring_buffer_per_cpu **buffers;
182 struct ring_buffer_iter {
183 struct ring_buffer_per_cpu *cpu_buffer;
185 struct buffer_page *head_page;
189 #define RB_WARN_ON(buffer, cond) \
190 if (unlikely(cond)) { \
191 atomic_inc(&buffer->record_disabled); \
197 * check_pages - integrity check of buffer pages
198 * @cpu_buffer: CPU buffer with pages to test
200 * As a safty measure we check to make sure the data pages have not
203 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
205 struct list_head *head = &cpu_buffer->pages;
206 struct buffer_page *page, *tmp;
208 RB_WARN_ON(cpu_buffer, head->next->prev != head);
209 RB_WARN_ON(cpu_buffer, head->prev->next != head);
211 list_for_each_entry_safe(page, tmp, head, list) {
212 RB_WARN_ON(cpu_buffer, page->list.next->prev != &page->list);
213 RB_WARN_ON(cpu_buffer, page->list.prev->next != &page->list);
219 static unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
221 return cpu_buffer->head_page->size;
224 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
227 struct list_head *head = &cpu_buffer->pages;
228 struct buffer_page *page, *tmp;
233 for (i = 0; i < nr_pages; i++) {
234 page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()),
235 GFP_KERNEL, cpu_to_node(cpu));
238 list_add(&page->list, &pages);
240 addr = __get_free_page(GFP_KERNEL);
243 page->page = (void *)addr;
246 list_splice(&pages, head);
248 rb_check_pages(cpu_buffer);
253 list_for_each_entry_safe(page, tmp, &pages, list) {
254 list_del_init(&page->list);
255 free_buffer_page(page);
260 static struct ring_buffer_per_cpu *
261 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
263 struct ring_buffer_per_cpu *cpu_buffer;
264 struct buffer_page *page;
268 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
269 GFP_KERNEL, cpu_to_node(cpu));
273 cpu_buffer->cpu = cpu;
274 cpu_buffer->buffer = buffer;
275 spin_lock_init(&cpu_buffer->lock);
276 INIT_LIST_HEAD(&cpu_buffer->pages);
278 page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()),
279 GFP_KERNEL, cpu_to_node(cpu));
281 goto fail_free_buffer;
283 cpu_buffer->reader_page = page;
284 addr = __get_free_page(GFP_KERNEL);
286 goto fail_free_reader;
287 page->page = (void *)addr;
289 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
290 cpu_buffer->reader_page->size = 0;
292 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
294 goto fail_free_reader;
296 cpu_buffer->head_page
297 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
298 cpu_buffer->tail_page
299 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
304 free_buffer_page(cpu_buffer->reader_page);
311 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
313 struct list_head *head = &cpu_buffer->pages;
314 struct buffer_page *page, *tmp;
316 list_del_init(&cpu_buffer->reader_page->list);
317 free_buffer_page(cpu_buffer->reader_page);
319 list_for_each_entry_safe(page, tmp, head, list) {
320 list_del_init(&page->list);
321 free_buffer_page(page);
327 * Causes compile errors if the struct buffer_page gets bigger
328 * than the struct page.
330 extern int ring_buffer_page_too_big(void);
333 * ring_buffer_alloc - allocate a new ring_buffer
334 * @size: the size in bytes that is needed.
335 * @flags: attributes to set for the ring buffer.
337 * Currently the only flag that is available is the RB_FL_OVERWRITE
338 * flag. This flag means that the buffer will overwrite old data
339 * when the buffer wraps. If this flag is not set, the buffer will
340 * drop data when the tail hits the head.
342 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
344 struct ring_buffer *buffer;
348 /* Paranoid! Optimizes out when all is well */
349 if (sizeof(struct buffer_page) > sizeof(struct page))
350 ring_buffer_page_too_big();
353 /* keep it in its own cache line */
354 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
359 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
360 buffer->flags = flags;
362 /* need at least two pages */
363 if (buffer->pages == 1)
366 buffer->cpumask = cpu_possible_map;
367 buffer->cpus = nr_cpu_ids;
369 bsize = sizeof(void *) * nr_cpu_ids;
370 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
372 if (!buffer->buffers)
373 goto fail_free_buffer;
375 for_each_buffer_cpu(buffer, cpu) {
376 buffer->buffers[cpu] =
377 rb_allocate_cpu_buffer(buffer, cpu);
378 if (!buffer->buffers[cpu])
379 goto fail_free_buffers;
382 mutex_init(&buffer->mutex);
387 for_each_buffer_cpu(buffer, cpu) {
388 if (buffer->buffers[cpu])
389 rb_free_cpu_buffer(buffer->buffers[cpu]);
391 kfree(buffer->buffers);
399 * ring_buffer_free - free a ring buffer.
400 * @buffer: the buffer to free.
403 ring_buffer_free(struct ring_buffer *buffer)
407 for_each_buffer_cpu(buffer, cpu)
408 rb_free_cpu_buffer(buffer->buffers[cpu]);
413 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
416 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
418 struct buffer_page *page;
422 atomic_inc(&cpu_buffer->record_disabled);
425 for (i = 0; i < nr_pages; i++) {
426 BUG_ON(list_empty(&cpu_buffer->pages));
427 p = cpu_buffer->pages.next;
428 page = list_entry(p, struct buffer_page, list);
429 list_del_init(&page->list);
430 free_buffer_page(page);
432 BUG_ON(list_empty(&cpu_buffer->pages));
434 rb_reset_cpu(cpu_buffer);
436 rb_check_pages(cpu_buffer);
438 atomic_dec(&cpu_buffer->record_disabled);
443 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
444 struct list_head *pages, unsigned nr_pages)
446 struct buffer_page *page;
450 atomic_inc(&cpu_buffer->record_disabled);
453 for (i = 0; i < nr_pages; i++) {
454 BUG_ON(list_empty(pages));
456 page = list_entry(p, struct buffer_page, list);
457 list_del_init(&page->list);
458 list_add_tail(&page->list, &cpu_buffer->pages);
460 rb_reset_cpu(cpu_buffer);
462 rb_check_pages(cpu_buffer);
464 atomic_dec(&cpu_buffer->record_disabled);
468 * ring_buffer_resize - resize the ring buffer
469 * @buffer: the buffer to resize.
470 * @size: the new size.
472 * The tracer is responsible for making sure that the buffer is
473 * not being used while changing the size.
474 * Note: We may be able to change the above requirement by using
475 * RCU synchronizations.
477 * Minimum size is 2 * BUF_PAGE_SIZE.
479 * Returns -1 on failure.
481 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
483 struct ring_buffer_per_cpu *cpu_buffer;
484 unsigned nr_pages, rm_pages, new_pages;
485 struct buffer_page *page, *tmp;
486 unsigned long buffer_size;
491 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
492 size *= BUF_PAGE_SIZE;
493 buffer_size = buffer->pages * BUF_PAGE_SIZE;
495 /* we need a minimum of two pages */
496 if (size < BUF_PAGE_SIZE * 2)
497 size = BUF_PAGE_SIZE * 2;
499 if (size == buffer_size)
502 mutex_lock(&buffer->mutex);
504 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
506 if (size < buffer_size) {
508 /* easy case, just free pages */
509 BUG_ON(nr_pages >= buffer->pages);
511 rm_pages = buffer->pages - nr_pages;
513 for_each_buffer_cpu(buffer, cpu) {
514 cpu_buffer = buffer->buffers[cpu];
515 rb_remove_pages(cpu_buffer, rm_pages);
521 * This is a bit more difficult. We only want to add pages
522 * when we can allocate enough for all CPUs. We do this
523 * by allocating all the pages and storing them on a local
524 * link list. If we succeed in our allocation, then we
525 * add these pages to the cpu_buffers. Otherwise we just free
526 * them all and return -ENOMEM;
528 BUG_ON(nr_pages <= buffer->pages);
529 new_pages = nr_pages - buffer->pages;
531 for_each_buffer_cpu(buffer, cpu) {
532 for (i = 0; i < new_pages; i++) {
533 page = kzalloc_node(ALIGN(sizeof(*page),
535 GFP_KERNEL, cpu_to_node(cpu));
538 list_add(&page->list, &pages);
539 addr = __get_free_page(GFP_KERNEL);
542 page->page = (void *)addr;
546 for_each_buffer_cpu(buffer, cpu) {
547 cpu_buffer = buffer->buffers[cpu];
548 rb_insert_pages(cpu_buffer, &pages, new_pages);
551 BUG_ON(!list_empty(&pages));
554 buffer->pages = nr_pages;
555 mutex_unlock(&buffer->mutex);
560 list_for_each_entry_safe(page, tmp, &pages, list) {
561 list_del_init(&page->list);
562 free_buffer_page(page);
567 static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
569 return (cpu_buffer->reader == cpu_buffer->reader_page->size &&
570 (cpu_buffer->tail_page == cpu_buffer->reader_page ||
571 (cpu_buffer->tail_page == cpu_buffer->head_page &&
572 cpu_buffer->head == cpu_buffer->tail)));
575 static inline int rb_null_event(struct ring_buffer_event *event)
577 return event->type == RINGBUF_TYPE_PADDING;
580 static inline void *rb_page_index(struct buffer_page *page, unsigned index)
582 return page->page + index;
585 static inline struct ring_buffer_event *
586 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
588 return rb_page_index(cpu_buffer->reader_page,
592 static inline struct ring_buffer_event *
593 rb_iter_head_event(struct ring_buffer_iter *iter)
595 return rb_page_index(iter->head_page,
600 * When the tail hits the head and the buffer is in overwrite mode,
601 * the head jumps to the next page and all content on the previous
602 * page is discarded. But before doing so, we update the overrun
603 * variable of the buffer.
605 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
607 struct ring_buffer_event *event;
610 for (head = 0; head < rb_head_size(cpu_buffer);
611 head += rb_event_length(event)) {
613 event = rb_page_index(cpu_buffer->head_page, head);
614 BUG_ON(rb_null_event(event));
615 /* Only count data entries */
616 if (event->type != RINGBUF_TYPE_DATA)
618 cpu_buffer->overrun++;
619 cpu_buffer->entries--;
623 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
624 struct buffer_page **page)
626 struct list_head *p = (*page)->list.next;
628 if (p == &cpu_buffer->pages)
631 *page = list_entry(p, struct buffer_page, list);
635 rb_add_stamp(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
637 cpu_buffer->tail_page->time_stamp = *ts;
638 cpu_buffer->write_stamp = *ts;
641 static void rb_reset_head_page(struct ring_buffer_per_cpu *cpu_buffer)
643 cpu_buffer->head = 0;
646 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
648 cpu_buffer->read_stamp = cpu_buffer->reader_page->time_stamp;
649 cpu_buffer->reader = 0;
652 static inline void rb_inc_iter(struct ring_buffer_iter *iter)
654 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
657 * The iterator could be on the reader page (it starts there).
658 * But the head could have moved, since the reader was
659 * found. Check for this case and assign the iterator
660 * to the head page instead of next.
662 if (iter->head_page == cpu_buffer->reader_page)
663 iter->head_page = cpu_buffer->head_page;
665 rb_inc_page(cpu_buffer, &iter->head_page);
667 iter->read_stamp = iter->head_page->time_stamp;
672 * ring_buffer_update_event - update event type and data
673 * @event: the even to update
674 * @type: the type of event
675 * @length: the size of the event field in the ring buffer
677 * Update the type and data fields of the event. The length
678 * is the actual size that is written to the ring buffer,
679 * and with this, we can determine what to place into the
683 rb_update_event(struct ring_buffer_event *event,
684 unsigned type, unsigned length)
690 case RINGBUF_TYPE_PADDING:
693 case RINGBUF_TYPE_TIME_EXTEND:
695 (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1))
696 >> RB_ALIGNMENT_SHIFT;
699 case RINGBUF_TYPE_TIME_STAMP:
701 (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1))
702 >> RB_ALIGNMENT_SHIFT;
705 case RINGBUF_TYPE_DATA:
706 length -= RB_EVNT_HDR_SIZE;
707 if (length > RB_MAX_SMALL_DATA) {
709 event->array[0] = length;
712 (length + (RB_ALIGNMENT-1))
713 >> RB_ALIGNMENT_SHIFT;
720 static inline unsigned rb_calculate_event_length(unsigned length)
722 struct ring_buffer_event event; /* Used only for sizeof array */
724 /* zero length can cause confusions */
728 if (length > RB_MAX_SMALL_DATA)
729 length += sizeof(event.array[0]);
731 length += RB_EVNT_HDR_SIZE;
732 length = ALIGN(length, RB_ALIGNMENT);
737 static struct ring_buffer_event *
738 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
739 unsigned type, unsigned long length, u64 *ts)
741 struct buffer_page *tail_page, *head_page, *reader_page;
743 struct ring_buffer *buffer = cpu_buffer->buffer;
744 struct ring_buffer_event *event;
746 /* No locking needed for tail page */
747 tail_page = cpu_buffer->tail_page;
748 tail = cpu_buffer->tail;
750 if (tail + length > BUF_PAGE_SIZE) {
751 struct buffer_page *next_page = tail_page;
753 spin_lock(&cpu_buffer->lock);
754 rb_inc_page(cpu_buffer, &next_page);
756 head_page = cpu_buffer->head_page;
757 reader_page = cpu_buffer->reader_page;
759 /* we grabbed the lock before incrementing */
760 WARN_ON(next_page == reader_page);
762 if (next_page == head_page) {
763 if (!(buffer->flags & RB_FL_OVERWRITE)) {
764 spin_unlock(&cpu_buffer->lock);
768 /* count overflows */
769 rb_update_overflow(cpu_buffer);
771 rb_inc_page(cpu_buffer, &head_page);
772 cpu_buffer->head_page = head_page;
773 rb_reset_head_page(cpu_buffer);
776 if (tail != BUF_PAGE_SIZE) {
777 event = rb_page_index(tail_page, tail);
779 event->type = RINGBUF_TYPE_PADDING;
782 tail_page->size = tail;
783 tail_page = next_page;
786 cpu_buffer->tail_page = tail_page;
787 cpu_buffer->tail = tail;
788 rb_add_stamp(cpu_buffer, ts);
789 spin_unlock(&cpu_buffer->lock);
792 BUG_ON(tail + length > BUF_PAGE_SIZE);
794 event = rb_page_index(tail_page, tail);
795 rb_update_event(event, type, length);
801 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
804 struct ring_buffer_event *event;
807 if (unlikely(*delta > (1ULL << 59) && !once++)) {
808 printk(KERN_WARNING "Delta way too big! %llu"
809 " ts=%llu write stamp = %llu\n",
810 *delta, *ts, cpu_buffer->write_stamp);
815 * The delta is too big, we to add a
818 event = __rb_reserve_next(cpu_buffer,
819 RINGBUF_TYPE_TIME_EXTEND,
825 /* check to see if we went to the next page */
826 if (cpu_buffer->tail) {
827 /* Still on same page, update timestamp */
828 event->time_delta = *delta & TS_MASK;
829 event->array[0] = *delta >> TS_SHIFT;
830 /* commit the time event */
832 rb_event_length(event);
833 cpu_buffer->write_stamp = *ts;
840 static struct ring_buffer_event *
841 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
842 unsigned type, unsigned long length)
844 struct ring_buffer_event *event;
847 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
849 if (cpu_buffer->tail) {
850 delta = ts - cpu_buffer->write_stamp;
852 if (test_time_stamp(delta)) {
855 ret = rb_add_time_stamp(cpu_buffer, &ts, &delta);
860 spin_lock(&cpu_buffer->lock);
861 rb_add_stamp(cpu_buffer, &ts);
862 spin_unlock(&cpu_buffer->lock);
866 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
870 /* If the reserve went to the next page, our delta is zero */
871 if (!cpu_buffer->tail)
874 event->time_delta = delta;
880 * ring_buffer_lock_reserve - reserve a part of the buffer
881 * @buffer: the ring buffer to reserve from
882 * @length: the length of the data to reserve (excluding event header)
883 * @flags: a pointer to save the interrupt flags
885 * Returns a reseverd event on the ring buffer to copy directly to.
886 * The user of this interface will need to get the body to write into
887 * and can use the ring_buffer_event_data() interface.
889 * The length is the length of the data needed, not the event length
890 * which also includes the event header.
892 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
893 * If NULL is returned, then nothing has been allocated or locked.
895 struct ring_buffer_event *
896 ring_buffer_lock_reserve(struct ring_buffer *buffer,
897 unsigned long length,
898 unsigned long *flags)
900 struct ring_buffer_per_cpu *cpu_buffer;
901 struct ring_buffer_event *event;
904 if (atomic_read(&buffer->record_disabled))
907 local_irq_save(*flags);
908 cpu = raw_smp_processor_id();
910 if (!cpu_isset(cpu, buffer->cpumask))
913 cpu_buffer = buffer->buffers[cpu];
915 if (atomic_read(&cpu_buffer->record_disabled))
918 length = rb_calculate_event_length(length);
919 if (length > BUF_PAGE_SIZE)
922 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
929 local_irq_restore(*flags);
933 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
934 struct ring_buffer_event *event)
936 cpu_buffer->tail += rb_event_length(event);
937 cpu_buffer->tail_page->size = cpu_buffer->tail;
938 cpu_buffer->write_stamp += event->time_delta;
939 cpu_buffer->entries++;
943 * ring_buffer_unlock_commit - commit a reserved
944 * @buffer: The buffer to commit to
945 * @event: The event pointer to commit.
946 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
948 * This commits the data to the ring buffer, and releases any locks held.
950 * Must be paired with ring_buffer_lock_reserve.
952 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
953 struct ring_buffer_event *event,
956 struct ring_buffer_per_cpu *cpu_buffer;
957 int cpu = raw_smp_processor_id();
959 cpu_buffer = buffer->buffers[cpu];
961 rb_commit(cpu_buffer, event);
963 local_irq_restore(flags);
969 * ring_buffer_write - write data to the buffer without reserving
970 * @buffer: The ring buffer to write to.
971 * @length: The length of the data being written (excluding the event header)
972 * @data: The data to write to the buffer.
974 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
975 * one function. If you already have the data to write to the buffer, it
976 * may be easier to simply call this function.
978 * Note, like ring_buffer_lock_reserve, the length is the length of the data
979 * and not the length of the event which would hold the header.
981 int ring_buffer_write(struct ring_buffer *buffer,
982 unsigned long length,
985 struct ring_buffer_per_cpu *cpu_buffer;
986 struct ring_buffer_event *event;
987 unsigned long event_length, flags;
992 if (atomic_read(&buffer->record_disabled))
995 local_irq_save(flags);
996 cpu = raw_smp_processor_id();
998 if (!cpu_isset(cpu, buffer->cpumask))
1001 cpu_buffer = buffer->buffers[cpu];
1003 if (atomic_read(&cpu_buffer->record_disabled))
1006 event_length = rb_calculate_event_length(length);
1007 event = rb_reserve_next_event(cpu_buffer,
1008 RINGBUF_TYPE_DATA, event_length);
1012 body = rb_event_data(event);
1014 memcpy(body, data, length);
1016 rb_commit(cpu_buffer, event);
1020 local_irq_restore(flags);
1026 * ring_buffer_record_disable - stop all writes into the buffer
1027 * @buffer: The ring buffer to stop writes to.
1029 * This prevents all writes to the buffer. Any attempt to write
1030 * to the buffer after this will fail and return NULL.
1032 * The caller should call synchronize_sched() after this.
1034 void ring_buffer_record_disable(struct ring_buffer *buffer)
1036 atomic_inc(&buffer->record_disabled);
1040 * ring_buffer_record_enable - enable writes to the buffer
1041 * @buffer: The ring buffer to enable writes
1043 * Note, multiple disables will need the same number of enables
1044 * to truely enable the writing (much like preempt_disable).
1046 void ring_buffer_record_enable(struct ring_buffer *buffer)
1048 atomic_dec(&buffer->record_disabled);
1052 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1053 * @buffer: The ring buffer to stop writes to.
1054 * @cpu: The CPU buffer to stop
1056 * This prevents all writes to the buffer. Any attempt to write
1057 * to the buffer after this will fail and return NULL.
1059 * The caller should call synchronize_sched() after this.
1061 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1063 struct ring_buffer_per_cpu *cpu_buffer;
1065 if (!cpu_isset(cpu, buffer->cpumask))
1068 cpu_buffer = buffer->buffers[cpu];
1069 atomic_inc(&cpu_buffer->record_disabled);
1073 * ring_buffer_record_enable_cpu - enable writes to the buffer
1074 * @buffer: The ring buffer to enable writes
1075 * @cpu: The CPU to enable.
1077 * Note, multiple disables will need the same number of enables
1078 * to truely enable the writing (much like preempt_disable).
1080 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1082 struct ring_buffer_per_cpu *cpu_buffer;
1084 if (!cpu_isset(cpu, buffer->cpumask))
1087 cpu_buffer = buffer->buffers[cpu];
1088 atomic_dec(&cpu_buffer->record_disabled);
1092 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1093 * @buffer: The ring buffer
1094 * @cpu: The per CPU buffer to get the entries from.
1096 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1098 struct ring_buffer_per_cpu *cpu_buffer;
1100 if (!cpu_isset(cpu, buffer->cpumask))
1103 cpu_buffer = buffer->buffers[cpu];
1104 return cpu_buffer->entries;
1108 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1109 * @buffer: The ring buffer
1110 * @cpu: The per CPU buffer to get the number of overruns from
1112 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1114 struct ring_buffer_per_cpu *cpu_buffer;
1116 if (!cpu_isset(cpu, buffer->cpumask))
1119 cpu_buffer = buffer->buffers[cpu];
1120 return cpu_buffer->overrun;
1124 * ring_buffer_entries - get the number of entries in a buffer
1125 * @buffer: The ring buffer
1127 * Returns the total number of entries in the ring buffer
1130 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1132 struct ring_buffer_per_cpu *cpu_buffer;
1133 unsigned long entries = 0;
1136 /* if you care about this being correct, lock the buffer */
1137 for_each_buffer_cpu(buffer, cpu) {
1138 cpu_buffer = buffer->buffers[cpu];
1139 entries += cpu_buffer->entries;
1146 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1147 * @buffer: The ring buffer
1149 * Returns the total number of overruns in the ring buffer
1152 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1154 struct ring_buffer_per_cpu *cpu_buffer;
1155 unsigned long overruns = 0;
1158 /* if you care about this being correct, lock the buffer */
1159 for_each_buffer_cpu(buffer, cpu) {
1160 cpu_buffer = buffer->buffers[cpu];
1161 overruns += cpu_buffer->overrun;
1168 * ring_buffer_iter_reset - reset an iterator
1169 * @iter: The iterator to reset
1171 * Resets the iterator, so that it will start from the beginning
1174 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1176 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1178 /* Iterator usage is expected to have record disabled */
1179 if (list_empty(&cpu_buffer->reader_page->list)) {
1180 iter->head_page = cpu_buffer->head_page;
1181 iter->head = cpu_buffer->head;
1183 iter->head_page = cpu_buffer->reader_page;
1184 iter->head = cpu_buffer->reader;
1187 iter->read_stamp = cpu_buffer->read_stamp;
1189 iter->read_stamp = iter->head_page->time_stamp;
1193 * ring_buffer_iter_empty - check if an iterator has no more to read
1194 * @iter: The iterator to check
1196 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1198 struct ring_buffer_per_cpu *cpu_buffer;
1200 cpu_buffer = iter->cpu_buffer;
1202 return iter->head_page == cpu_buffer->tail_page &&
1203 iter->head == cpu_buffer->tail;
1207 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1208 struct ring_buffer_event *event)
1212 switch (event->type) {
1213 case RINGBUF_TYPE_PADDING:
1216 case RINGBUF_TYPE_TIME_EXTEND:
1217 delta = event->array[0];
1219 delta += event->time_delta;
1220 cpu_buffer->read_stamp += delta;
1223 case RINGBUF_TYPE_TIME_STAMP:
1224 /* FIXME: not implemented */
1227 case RINGBUF_TYPE_DATA:
1228 cpu_buffer->read_stamp += event->time_delta;
1238 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1239 struct ring_buffer_event *event)
1243 switch (event->type) {
1244 case RINGBUF_TYPE_PADDING:
1247 case RINGBUF_TYPE_TIME_EXTEND:
1248 delta = event->array[0];
1250 delta += event->time_delta;
1251 iter->read_stamp += delta;
1254 case RINGBUF_TYPE_TIME_STAMP:
1255 /* FIXME: not implemented */
1258 case RINGBUF_TYPE_DATA:
1259 iter->read_stamp += event->time_delta;
1268 static struct buffer_page *
1269 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1271 struct buffer_page *reader = NULL;
1272 unsigned long flags;
1274 spin_lock_irqsave(&cpu_buffer->lock, flags);
1277 reader = cpu_buffer->reader_page;
1279 /* If there's more to read, return this page */
1280 if (cpu_buffer->reader < reader->size)
1283 /* Never should we have an index greater than the size */
1284 WARN_ON(cpu_buffer->reader > reader->size);
1286 /* check if we caught up to the tail */
1288 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1292 * Splice the empty reader page into the list around the head.
1293 * Reset the reader page to size zero.
1296 reader = cpu_buffer->head_page;
1297 cpu_buffer->reader_page->list.next = reader->list.next;
1298 cpu_buffer->reader_page->list.prev = reader->list.prev;
1299 cpu_buffer->reader_page->size = 0;
1301 /* Make the reader page now replace the head */
1302 reader->list.prev->next = &cpu_buffer->reader_page->list;
1303 reader->list.next->prev = &cpu_buffer->reader_page->list;
1306 * If the tail is on the reader, then we must set the head
1307 * to the inserted page, otherwise we set it one before.
1309 cpu_buffer->head_page = cpu_buffer->reader_page;
1311 if (cpu_buffer->tail_page != reader)
1312 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1314 /* Finally update the reader page to the new head */
1315 cpu_buffer->reader_page = reader;
1316 rb_reset_reader_page(cpu_buffer);
1321 spin_unlock_irqrestore(&cpu_buffer->lock, flags);
1326 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1328 struct ring_buffer_event *event;
1329 struct buffer_page *reader;
1332 reader = rb_get_reader_page(cpu_buffer);
1334 /* This function should not be called when buffer is empty */
1337 event = rb_reader_event(cpu_buffer);
1339 if (event->type == RINGBUF_TYPE_DATA)
1340 cpu_buffer->entries--;
1342 rb_update_read_stamp(cpu_buffer, event);
1344 length = rb_event_length(event);
1345 cpu_buffer->reader += length;
1348 static void rb_advance_iter(struct ring_buffer_iter *iter)
1350 struct ring_buffer *buffer;
1351 struct ring_buffer_per_cpu *cpu_buffer;
1352 struct ring_buffer_event *event;
1355 cpu_buffer = iter->cpu_buffer;
1356 buffer = cpu_buffer->buffer;
1359 * Check if we are at the end of the buffer.
1361 if (iter->head >= iter->head_page->size) {
1362 BUG_ON(iter->head_page == cpu_buffer->tail_page);
1367 event = rb_iter_head_event(iter);
1369 length = rb_event_length(event);
1372 * This should not be called to advance the header if we are
1373 * at the tail of the buffer.
1375 BUG_ON((iter->head_page == cpu_buffer->tail_page) &&
1376 (iter->head + length > cpu_buffer->tail));
1378 rb_update_iter_read_stamp(iter, event);
1380 iter->head += length;
1382 /* check for end of page padding */
1383 if ((iter->head >= iter->head_page->size) &&
1384 (iter->head_page != cpu_buffer->tail_page))
1385 rb_advance_iter(iter);
1389 * ring_buffer_peek - peek at the next event to be read
1390 * @buffer: The ring buffer to read
1391 * @cpu: The cpu to peak at
1392 * @ts: The timestamp counter of this event.
1394 * This will return the event that will be read next, but does
1395 * not consume the data.
1397 struct ring_buffer_event *
1398 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1400 struct ring_buffer_per_cpu *cpu_buffer;
1401 struct ring_buffer_event *event;
1402 struct buffer_page *reader;
1404 if (!cpu_isset(cpu, buffer->cpumask))
1407 cpu_buffer = buffer->buffers[cpu];
1410 reader = rb_get_reader_page(cpu_buffer);
1414 event = rb_reader_event(cpu_buffer);
1416 switch (event->type) {
1417 case RINGBUF_TYPE_PADDING:
1419 rb_advance_reader(cpu_buffer);
1422 case RINGBUF_TYPE_TIME_EXTEND:
1423 /* Internal data, OK to advance */
1424 rb_advance_reader(cpu_buffer);
1427 case RINGBUF_TYPE_TIME_STAMP:
1428 /* FIXME: not implemented */
1429 rb_advance_reader(cpu_buffer);
1432 case RINGBUF_TYPE_DATA:
1434 *ts = cpu_buffer->read_stamp + event->time_delta;
1435 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1447 * ring_buffer_iter_peek - peek at the next event to be read
1448 * @iter: The ring buffer iterator
1449 * @ts: The timestamp counter of this event.
1451 * This will return the event that will be read next, but does
1452 * not increment the iterator.
1454 struct ring_buffer_event *
1455 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1457 struct ring_buffer *buffer;
1458 struct ring_buffer_per_cpu *cpu_buffer;
1459 struct ring_buffer_event *event;
1461 if (ring_buffer_iter_empty(iter))
1464 cpu_buffer = iter->cpu_buffer;
1465 buffer = cpu_buffer->buffer;
1468 if (rb_per_cpu_empty(cpu_buffer))
1471 event = rb_iter_head_event(iter);
1473 switch (event->type) {
1474 case RINGBUF_TYPE_PADDING:
1478 case RINGBUF_TYPE_TIME_EXTEND:
1479 /* Internal data, OK to advance */
1480 rb_advance_iter(iter);
1483 case RINGBUF_TYPE_TIME_STAMP:
1484 /* FIXME: not implemented */
1485 rb_advance_iter(iter);
1488 case RINGBUF_TYPE_DATA:
1490 *ts = iter->read_stamp + event->time_delta;
1491 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1503 * ring_buffer_consume - return an event and consume it
1504 * @buffer: The ring buffer to get the next event from
1506 * Returns the next event in the ring buffer, and that event is consumed.
1507 * Meaning, that sequential reads will keep returning a different event,
1508 * and eventually empty the ring buffer if the producer is slower.
1510 struct ring_buffer_event *
1511 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
1513 struct ring_buffer_per_cpu *cpu_buffer;
1514 struct ring_buffer_event *event;
1516 if (!cpu_isset(cpu, buffer->cpumask))
1519 event = ring_buffer_peek(buffer, cpu, ts);
1523 cpu_buffer = buffer->buffers[cpu];
1524 rb_advance_reader(cpu_buffer);
1530 * ring_buffer_read_start - start a non consuming read of the buffer
1531 * @buffer: The ring buffer to read from
1532 * @cpu: The cpu buffer to iterate over
1534 * This starts up an iteration through the buffer. It also disables
1535 * the recording to the buffer until the reading is finished.
1536 * This prevents the reading from being corrupted. This is not
1537 * a consuming read, so a producer is not expected.
1539 * Must be paired with ring_buffer_finish.
1541 struct ring_buffer_iter *
1542 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
1544 struct ring_buffer_per_cpu *cpu_buffer;
1545 struct ring_buffer_iter *iter;
1546 unsigned long flags;
1548 if (!cpu_isset(cpu, buffer->cpumask))
1551 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1555 cpu_buffer = buffer->buffers[cpu];
1557 iter->cpu_buffer = cpu_buffer;
1559 atomic_inc(&cpu_buffer->record_disabled);
1560 synchronize_sched();
1562 spin_lock_irqsave(&cpu_buffer->lock, flags);
1563 ring_buffer_iter_reset(iter);
1564 spin_unlock_irqrestore(&cpu_buffer->lock, flags);
1570 * ring_buffer_finish - finish reading the iterator of the buffer
1571 * @iter: The iterator retrieved by ring_buffer_start
1573 * This re-enables the recording to the buffer, and frees the
1577 ring_buffer_read_finish(struct ring_buffer_iter *iter)
1579 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1581 atomic_dec(&cpu_buffer->record_disabled);
1586 * ring_buffer_read - read the next item in the ring buffer by the iterator
1587 * @iter: The ring buffer iterator
1588 * @ts: The time stamp of the event read.
1590 * This reads the next event in the ring buffer and increments the iterator.
1592 struct ring_buffer_event *
1593 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
1595 struct ring_buffer_event *event;
1597 event = ring_buffer_iter_peek(iter, ts);
1601 rb_advance_iter(iter);
1607 * ring_buffer_size - return the size of the ring buffer (in bytes)
1608 * @buffer: The ring buffer.
1610 unsigned long ring_buffer_size(struct ring_buffer *buffer)
1612 return BUF_PAGE_SIZE * buffer->pages;
1616 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
1618 cpu_buffer->head_page
1619 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
1620 cpu_buffer->head_page->size = 0;
1621 cpu_buffer->tail_page = cpu_buffer->head_page;
1622 cpu_buffer->tail_page->size = 0;
1623 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1624 cpu_buffer->reader_page->size = 0;
1626 cpu_buffer->head = cpu_buffer->tail = cpu_buffer->reader = 0;
1628 cpu_buffer->overrun = 0;
1629 cpu_buffer->entries = 0;
1633 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
1634 * @buffer: The ring buffer to reset a per cpu buffer of
1635 * @cpu: The CPU buffer to be reset
1637 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
1639 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1640 unsigned long flags;
1642 if (!cpu_isset(cpu, buffer->cpumask))
1645 spin_lock_irqsave(&cpu_buffer->lock, flags);
1647 rb_reset_cpu(cpu_buffer);
1649 spin_unlock_irqrestore(&cpu_buffer->lock, flags);
1653 * ring_buffer_reset - reset a ring buffer
1654 * @buffer: The ring buffer to reset all cpu buffers
1656 void ring_buffer_reset(struct ring_buffer *buffer)
1660 for_each_buffer_cpu(buffer, cpu)
1661 ring_buffer_reset_cpu(buffer, cpu);
1665 * rind_buffer_empty - is the ring buffer empty?
1666 * @buffer: The ring buffer to test
1668 int ring_buffer_empty(struct ring_buffer *buffer)
1670 struct ring_buffer_per_cpu *cpu_buffer;
1673 /* yes this is racy, but if you don't like the race, lock the buffer */
1674 for_each_buffer_cpu(buffer, cpu) {
1675 cpu_buffer = buffer->buffers[cpu];
1676 if (!rb_per_cpu_empty(cpu_buffer))
1683 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
1684 * @buffer: The ring buffer
1685 * @cpu: The CPU buffer to test
1687 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
1689 struct ring_buffer_per_cpu *cpu_buffer;
1691 if (!cpu_isset(cpu, buffer->cpumask))
1694 cpu_buffer = buffer->buffers[cpu];
1695 return rb_per_cpu_empty(cpu_buffer);
1699 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
1700 * @buffer_a: One buffer to swap with
1701 * @buffer_b: The other buffer to swap with
1703 * This function is useful for tracers that want to take a "snapshot"
1704 * of a CPU buffer and has another back up buffer lying around.
1705 * it is expected that the tracer handles the cpu buffer not being
1706 * used at the moment.
1708 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
1709 struct ring_buffer *buffer_b, int cpu)
1711 struct ring_buffer_per_cpu *cpu_buffer_a;
1712 struct ring_buffer_per_cpu *cpu_buffer_b;
1714 if (!cpu_isset(cpu, buffer_a->cpumask) ||
1715 !cpu_isset(cpu, buffer_b->cpumask))
1718 /* At least make sure the two buffers are somewhat the same */
1719 if (buffer_a->size != buffer_b->size ||
1720 buffer_a->pages != buffer_b->pages)
1723 cpu_buffer_a = buffer_a->buffers[cpu];
1724 cpu_buffer_b = buffer_b->buffers[cpu];
1727 * We can't do a synchronize_sched here because this
1728 * function can be called in atomic context.
1729 * Normally this will be called from the same CPU as cpu.
1730 * If not it's up to the caller to protect this.
1732 atomic_inc(&cpu_buffer_a->record_disabled);
1733 atomic_inc(&cpu_buffer_b->record_disabled);
1735 buffer_a->buffers[cpu] = cpu_buffer_b;
1736 buffer_b->buffers[cpu] = cpu_buffer_a;
1738 cpu_buffer_b->buffer = buffer_a;
1739 cpu_buffer_a->buffer = buffer_b;
1741 atomic_dec(&cpu_buffer_a->record_disabled);
1742 atomic_dec(&cpu_buffer_b->record_disabled);