1 #ifndef _LINUX_RING_BUFFER_FRONTEND_H
2 #define _LINUX_RING_BUFFER_FRONTEND_H
5 * linux/ringbuffer/frontend.h
7 * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Ring Buffer Library Synchronization Header (API).
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 * See ring_buffer_frontend.c for more information on wait-free algorithms.
16 * Dual LGPL v2.1/GPL v2 license.
19 #include <urcu/compiler.h>
20 #include <urcu/uatomic.h>
23 /* Internal helpers */
24 #include "frontend_internal.h"
26 /* Buffer creation/removal and setup operations */
29 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
30 * padding to let readers get those sub-buffers. Used for live streaming.
32 * read_timer_interval is the time interval (in us) to wake up pending readers.
34 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
35 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
36 * be set to NULL for other backends.
40 struct shm_handle
*channel_create(const struct lib_ring_buffer_config
*config
,
41 const char *name
, void *priv
,
43 size_t subbuf_size
, size_t num_subbuf
,
44 unsigned int switch_timer_interval
,
45 unsigned int read_timer_interval
);
48 * channel_destroy returns the private data pointer. It finalizes all channel's
49 * buffers, waits for readers to release all references, and destroys the
53 void *channel_destroy(struct channel
*chan
, struct shm_handle
*handle
);
56 /* Buffer read operations */
59 * Iteration on channel cpumask needs to issue a read barrier to match the write
60 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
61 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
62 * only performed at channel destruction.
64 #define for_each_channel_cpu(cpu, chan) \
65 for_each_possible_cpu(cpu)
67 extern struct lib_ring_buffer
*channel_get_ring_buffer(
68 const struct lib_ring_buffer_config
*config
,
69 struct channel
*chan
, int cpu
,
70 struct shm_handle
*handle
);
71 extern int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
,
72 struct shm_handle
*handle
);
73 extern void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
,
74 struct shm_handle
*handle
);
77 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
79 extern int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
80 unsigned long *consumed
,
81 unsigned long *produced
,
82 struct shm_handle
*handle
);
83 extern void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
84 unsigned long consumed_new
,
85 struct shm_handle
*handle
);
87 extern int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
88 unsigned long consumed
,
89 struct shm_handle
*handle
);
90 extern void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
,
91 struct shm_handle
*handle
);
94 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
95 * to read sub-buffers sequentially.
97 static inline int lib_ring_buffer_get_next_subbuf(struct lib_ring_buffer
*buf
,
98 struct shm_handle
*handle
)
102 ret
= lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
103 &buf
->prod_snapshot
, handle
);
106 ret
= lib_ring_buffer_get_subbuf(buf
, buf
->cons_snapshot
, handle
);
111 void lib_ring_buffer_put_next_subbuf(struct lib_ring_buffer
*buf
,
112 struct shm_handle
*handle
)
114 lib_ring_buffer_put_subbuf(buf
, handle
);
115 lib_ring_buffer_move_consumer(buf
, subbuf_align(buf
->cons_snapshot
,
116 shmp(handle
, buf
->backend
.chan
)), handle
);
119 extern void channel_reset(struct channel
*chan
);
120 extern void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
,
121 struct shm_handle
*handle
);
124 unsigned long lib_ring_buffer_get_offset(const struct lib_ring_buffer_config
*config
,
125 struct lib_ring_buffer
*buf
)
127 return v_read(config
, &buf
->offset
);
131 unsigned long lib_ring_buffer_get_consumed(const struct lib_ring_buffer_config
*config
,
132 struct lib_ring_buffer
*buf
)
134 return uatomic_read(&buf
->consumed
);
138 * Must call lib_ring_buffer_is_finalized before reading counters (memory
139 * ordering enforced with respect to trace teardown).
142 int lib_ring_buffer_is_finalized(const struct lib_ring_buffer_config
*config
,
143 struct lib_ring_buffer
*buf
)
145 int finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
147 * Read finalized before counters.
154 int lib_ring_buffer_channel_is_finalized(const struct channel
*chan
)
156 return chan
->finalized
;
160 int lib_ring_buffer_channel_is_disabled(const struct channel
*chan
)
162 return uatomic_read(&chan
->record_disabled
);
166 unsigned long lib_ring_buffer_get_read_data_size(
167 const struct lib_ring_buffer_config
*config
,
168 struct lib_ring_buffer
*buf
,
169 struct shm_handle
*handle
)
171 return subbuffer_get_read_data_size(config
, &buf
->backend
, handle
);
175 unsigned long lib_ring_buffer_get_records_count(
176 const struct lib_ring_buffer_config
*config
,
177 struct lib_ring_buffer
*buf
)
179 return v_read(config
, &buf
->records_count
);
183 unsigned long lib_ring_buffer_get_records_overrun(
184 const struct lib_ring_buffer_config
*config
,
185 struct lib_ring_buffer
*buf
)
187 return v_read(config
, &buf
->records_overrun
);
191 unsigned long lib_ring_buffer_get_records_lost_full(
192 const struct lib_ring_buffer_config
*config
,
193 struct lib_ring_buffer
*buf
)
195 return v_read(config
, &buf
->records_lost_full
);
199 unsigned long lib_ring_buffer_get_records_lost_wrap(
200 const struct lib_ring_buffer_config
*config
,
201 struct lib_ring_buffer
*buf
)
203 return v_read(config
, &buf
->records_lost_wrap
);
207 unsigned long lib_ring_buffer_get_records_lost_big(
208 const struct lib_ring_buffer_config
*config
,
209 struct lib_ring_buffer
*buf
)
211 return v_read(config
, &buf
->records_lost_big
);
215 unsigned long lib_ring_buffer_get_records_read(
216 const struct lib_ring_buffer_config
*config
,
217 struct lib_ring_buffer
*buf
)
219 return v_read(config
, &buf
->backend
.records_read
);
222 #endif /* _LINUX_RING_BUFFER_FRONTEND_H */
This page took 0.04022 seconds and 4 git commands to generate.