blob: 1342e69542f345dada550d298f7bacf6f6fe75e9 [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001#ifndef _LINUX_RING_BUFFER_H
2#define _LINUX_RING_BUFFER_H
3
Vegard Nossum1744a212009-02-28 08:29:44 +01004#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005#include <linux/mm.h>
6#include <linux/seq_file.h>
7
8struct ring_buffer;
9struct ring_buffer_iter;
10
11/*
Wenji Huangc3706f02009-02-10 01:03:18 -050012 * Don't refer to this struct directly, use functions below.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040013 */
14struct ring_buffer_event {
Vegard Nossum1744a212009-02-28 08:29:44 +010015 kmemcheck_bitfield_begin(bitfield);
Lai Jiangshan334d4162009-04-24 11:27:05 +080016 u32 type_len:5, time_delta:27;
Vegard Nossum1744a212009-02-28 08:29:44 +010017 kmemcheck_bitfield_end(bitfield);
18
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040019 u32 array[];
20};
21
22/**
23 * enum ring_buffer_type - internal ring buffer types
24 *
Tom Zanussi2d622712009-03-22 03:30:49 -050025 * @RINGBUF_TYPE_PADDING: Left over page padding or discarded event
26 * If time_delta is 0:
27 * array is ignored
28 * size is variable depending on how much
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040029 * padding is needed
Tom Zanussi2d622712009-03-22 03:30:49 -050030 * If time_delta is non zero:
Lai Jiangshan334d4162009-04-24 11:27:05 +080031 * array[0] holds the actual length
32 * size = 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040033 *
34 * @RINGBUF_TYPE_TIME_EXTEND: Extend the time delta
35 * array[0] = time delta (28 .. 59)
36 * size = 8 bytes
37 *
38 * @RINGBUF_TYPE_TIME_STAMP: Sync time stamp with external clock
Lai Jiangshan361b73d2008-12-08 10:58:08 +080039 * array[0] = tv_nsec
40 * array[1..2] = tv_sec
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040041 * size = 16 bytes
42 *
Lai Jiangshan334d4162009-04-24 11:27:05 +080043 * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX:
44 * Data record
45 * If type_len is zero:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040046 * array[0] holds the actual length
Lai Jiangshan361b73d2008-12-08 10:58:08 +080047 * array[1..(length+3)/4] holds data
Lai Jiangshan334d4162009-04-24 11:27:05 +080048 * size = 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040049 * else
Lai Jiangshan334d4162009-04-24 11:27:05 +080050 * length = type_len << 2
Lai Jiangshan361b73d2008-12-08 10:58:08 +080051 * array[0..(length+3)/4-1] holds data
52 * size = 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040053 */
54enum ring_buffer_type {
Lai Jiangshan334d4162009-04-24 11:27:05 +080055 RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040056 RINGBUF_TYPE_PADDING,
57 RINGBUF_TYPE_TIME_EXTEND,
58 /* FIXME: RINGBUF_TYPE_TIME_STAMP not implemented */
59 RINGBUF_TYPE_TIME_STAMP,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040060};
61
62unsigned ring_buffer_event_length(struct ring_buffer_event *event);
63void *ring_buffer_event_data(struct ring_buffer_event *event);
64
Steven Rostedtfa1b47d2009-04-02 00:09:41 -040065/*
Steven Rostedtfa1b47d2009-04-02 00:09:41 -040066 * ring_buffer_discard_commit will remove an event that has not
67 * ben committed yet. If this is used, then ring_buffer_unlock_commit
68 * must not be called on the discarded event. This function
69 * will try to remove the event from the ring buffer completely
70 * if another event has not been written after it.
71 *
72 * Example use:
73 *
74 * if (some_condition)
75 * ring_buffer_discard_commit(buffer, event);
76 * else
77 * ring_buffer_unlock_commit(buffer, event);
78 */
79void ring_buffer_discard_commit(struct ring_buffer *buffer,
80 struct ring_buffer_event *event);
81
82/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040083 * size is in bytes for each per CPU buffer.
84 */
85struct ring_buffer *
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +020086__ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *key);
87
88/*
89 * Because the ring buffer is generic, if other users of the ring buffer get
90 * traced by ftrace, it can produce lockdep warnings. We need to keep each
91 * ring buffer's lock class separate.
92 */
93#define ring_buffer_alloc(size, flags) \
94({ \
95 static struct lock_class_key __key; \
96 __ring_buffer_alloc((size), (flags), &__key); \
97})
98
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -080099#define RING_BUFFER_ALL_CPUS -1
100
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400101void ring_buffer_free(struct ring_buffer *buffer);
102
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800103int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size, int cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400104
David Sharp750912f2010-12-08 13:46:47 -0800105void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val);
106
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200107struct ring_buffer_event *ring_buffer_lock_reserve(struct ring_buffer *buffer,
108 unsigned long length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400109int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200110 struct ring_buffer_event *event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400111int ring_buffer_write(struct ring_buffer *buffer,
112 unsigned long length, void *data);
113
114struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400115ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
116 unsigned long *lost_events);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400117struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400118ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
119 unsigned long *lost_events);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400120
121struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -0700122ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu);
123void ring_buffer_read_prepare_sync(void);
124void ring_buffer_read_start(struct ring_buffer_iter *iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400125void ring_buffer_read_finish(struct ring_buffer_iter *iter);
126
127struct ring_buffer_event *
128ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts);
129struct ring_buffer_event *
130ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts);
131void ring_buffer_iter_reset(struct ring_buffer_iter *iter);
132int ring_buffer_iter_empty(struct ring_buffer_iter *iter);
133
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800134unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400135
136void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu);
137void ring_buffer_reset(struct ring_buffer *buffer);
138
Steven Rostedt85bac322009-09-04 14:24:40 -0400139#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400140int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
141 struct ring_buffer *buffer_b, int cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -0400142#else
143static inline int
144ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
145 struct ring_buffer *buffer_b, int cpu)
146{
147 return -ENODEV;
148}
149#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400150
151int ring_buffer_empty(struct ring_buffer *buffer);
152int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu);
153
154void ring_buffer_record_disable(struct ring_buffer *buffer);
155void ring_buffer_record_enable(struct ring_buffer *buffer);
Steven Rostedt499e5472012-02-22 15:50:28 -0500156void ring_buffer_record_off(struct ring_buffer *buffer);
157void ring_buffer_record_on(struct ring_buffer *buffer);
158int ring_buffer_record_is_on(struct ring_buffer *buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400159void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu);
160void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu);
161
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -0700162u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700163unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400164unsigned long ring_buffer_entries(struct ring_buffer *buffer);
165unsigned long ring_buffer_overruns(struct ring_buffer *buffer);
Robert Richtere09373f2008-11-26 14:04:19 +0100166unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu);
167unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu);
Steven Rostedtf0d2c682009-04-29 13:43:37 -0400168unsigned long ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -0700169unsigned long ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -0500170unsigned long ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400171
Steven Rostedt37886f62009-03-17 17:22:06 -0400172u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu);
173void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
174 int cpu, u64 *ts);
175void ring_buffer_set_clock(struct ring_buffer *buffer,
176 u64 (*clock)(void));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400177
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500178size_t ring_buffer_page_len(void *page);
179
180
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700181void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu);
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500182void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data);
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500183int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page,
184 size_t len, int cpu, int full);
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500185
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400186struct trace_seq;
187
188int ring_buffer_print_entry_header(struct trace_seq *s);
189int ring_buffer_print_page_header(struct trace_seq *s);
190
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400191enum ring_buffer_flags {
192 RB_FL_OVERWRITE = 1 << 0,
193};
194
195#endif /* _LINUX_RING_BUFFER_H */