1450cb7faf415d7e73f53b07101b534382474b53
1 #ifndef _LIB_RING_BUFFER_FRONTEND_H
2 #define _LIB_RING_BUFFER_FRONTEND_H
5 * lib/ringbuffer/frontend.h
7 * Ring Buffer Library Synchronization Header (API).
9 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
28 * See ring_buffer_frontend.c for more information on wait-free algorithms.
31 #include <linux/pipe_fs_i.h>
32 #include <linux/rcupdate.h>
33 #include <linux/cpumask.h>
34 #include <linux/module.h>
35 #include <linux/bitops.h>
36 #include <linux/splice.h>
37 #include <linux/string.h>
38 #include <linux/timer.h>
39 #include <linux/sched.h>
40 #include <linux/cache.h>
41 #include <linux/time.h>
42 #include <linux/slab.h>
43 #include <linux/init.h>
44 #include <linux/stat.h>
45 #include <linux/cpu.h>
48 #include <asm/atomic.h>
49 #include <asm/local.h>
51 /* Internal helpers */
52 #include <wrapper/ringbuffer/frontend_internal.h>
54 /* Buffer creation/removal and setup operations */
57 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
58 * padding to let readers get those sub-buffers. Used for live streaming.
60 * read_timer_interval is the time interval (in us) to wake up pending readers.
62 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
63 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
64 * be set to NULL for other backends.
68 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
69 const char *name
, void *priv
,
71 size_t subbuf_size
, size_t num_subbuf
,
72 unsigned int switch_timer_interval
,
73 unsigned int read_timer_interval
);
76 * channel_destroy returns the private data pointer. It finalizes all channel's
77 * buffers, waits for readers to release all references, and destroys the
81 void *channel_destroy(struct channel
*chan
);
84 /* Buffer read operations */
87 * Iteration on channel cpumask needs to issue a read barrier to match the write
88 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
89 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
90 * only performed at channel destruction.
92 #define for_each_channel_cpu(cpu, chan) \
94 ({ (cpu) = cpumask_next(cpu, (chan)->backend.cpumask); \
95 smp_read_barrier_depends(); (cpu) < nr_cpu_ids; });)
97 extern struct lib_ring_buffer
*channel_get_ring_buffer(
98 const struct lib_ring_buffer_config
*config
,
99 struct channel
*chan
, int cpu
);
100 extern int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
);
101 extern void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
);
104 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
106 extern int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
107 unsigned long *consumed
,
108 unsigned long *produced
);
109 extern int lib_ring_buffer_snapshot_sample_positions(
110 struct lib_ring_buffer
*buf
,
111 unsigned long *consumed
,
112 unsigned long *produced
);
113 extern void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
114 unsigned long consumed_new
);
116 extern int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
117 unsigned long consumed
);
118 extern void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
);
120 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
);
121 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
);
124 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
125 * to read sub-buffers sequentially.
127 static inline int lib_ring_buffer_get_next_subbuf(struct lib_ring_buffer
*buf
)
131 ret
= lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
132 &buf
->prod_snapshot
);
135 ret
= lib_ring_buffer_get_subbuf(buf
, buf
->cons_snapshot
);
139 static inline void lib_ring_buffer_put_next_subbuf(struct lib_ring_buffer
*buf
)
141 lib_ring_buffer_put_subbuf(buf
);
142 lib_ring_buffer_move_consumer(buf
, subbuf_align(buf
->cons_snapshot
,
146 extern void channel_reset(struct channel
*chan
);
147 extern void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
);
150 unsigned long lib_ring_buffer_get_offset(const struct lib_ring_buffer_config
*config
,
151 struct lib_ring_buffer
*buf
)
153 return v_read(config
, &buf
->offset
);
157 unsigned long lib_ring_buffer_get_consumed(const struct lib_ring_buffer_config
*config
,
158 struct lib_ring_buffer
*buf
)
160 return atomic_long_read(&buf
->consumed
);
164 * Must call lib_ring_buffer_is_finalized before reading counters (memory
165 * ordering enforced with respect to trace teardown).
168 int lib_ring_buffer_is_finalized(const struct lib_ring_buffer_config
*config
,
169 struct lib_ring_buffer
*buf
)
171 int finalized
= READ_ONCE(buf
->finalized
);
173 * Read finalized before counters.
180 int lib_ring_buffer_channel_is_finalized(const struct channel
*chan
)
182 return chan
->finalized
;
186 int lib_ring_buffer_channel_is_disabled(const struct channel
*chan
)
188 return atomic_read(&chan
->record_disabled
);
192 unsigned long lib_ring_buffer_get_read_data_size(
193 const struct lib_ring_buffer_config
*config
,
194 struct lib_ring_buffer
*buf
)
196 return subbuffer_get_read_data_size(config
, &buf
->backend
);
200 unsigned long lib_ring_buffer_get_records_count(
201 const struct lib_ring_buffer_config
*config
,
202 struct lib_ring_buffer
*buf
)
204 return v_read(config
, &buf
->records_count
);
208 unsigned long lib_ring_buffer_get_records_overrun(
209 const struct lib_ring_buffer_config
*config
,
210 struct lib_ring_buffer
*buf
)
212 return v_read(config
, &buf
->records_overrun
);
216 unsigned long lib_ring_buffer_get_records_lost_full(
217 const struct lib_ring_buffer_config
*config
,
218 struct lib_ring_buffer
*buf
)
220 return v_read(config
, &buf
->records_lost_full
);
224 unsigned long lib_ring_buffer_get_records_lost_wrap(
225 const struct lib_ring_buffer_config
*config
,
226 struct lib_ring_buffer
*buf
)
228 return v_read(config
, &buf
->records_lost_wrap
);
232 unsigned long lib_ring_buffer_get_records_lost_big(
233 const struct lib_ring_buffer_config
*config
,
234 struct lib_ring_buffer
*buf
)
236 return v_read(config
, &buf
->records_lost_big
);
240 unsigned long lib_ring_buffer_get_records_read(
241 const struct lib_ring_buffer_config
*config
,
242 struct lib_ring_buffer
*buf
)
244 return v_read(config
, &buf
->backend
.records_read
);
247 #endif /* _LIB_RING_BUFFER_FRONTEND_H */
This page took 0.032614 seconds and 3 git commands to generate.