Port ring buffer to userspace, part 1
[lttng-ust.git] / libringbuffer / backend_internal.h
CommitLineData
852c2936
MD
1#ifndef _LINUX_RING_BUFFER_BACKEND_INTERNAL_H
2#define _LINUX_RING_BUFFER_BACKEND_INTERNAL_H
3
4/*
5 * linux/ringbuffer/backend_internal.h
6 *
7 * Copyright (C) 2008-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Ring buffer backend (internal helpers).
10 *
11 * Dual LGPL v2.1/GPL v2 license.
12 */
13
14641deb
MD
14#include <unistd.h>
15#include <urcu/compiler.h>
16
4931a13e
MD
17#include "config.h"
18#include "backend_types.h"
19#include "frontend_types.h"
852c2936
MD
20
21/* Ring buffer backend API presented to the frontend */
22
23/* Ring buffer and channel backend create/free */
24
25int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend *bufb,
26 struct channel_backend *chan, int cpu);
27void channel_backend_unregister_notifiers(struct channel_backend *chanb);
28void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend *bufb);
29int channel_backend_init(struct channel_backend *chanb,
30 const char *name,
31 const struct lib_ring_buffer_config *config,
32 void *priv, size_t subbuf_size,
33 size_t num_subbuf);
34void channel_backend_free(struct channel_backend *chanb);
35
36void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend *bufb);
37void channel_backend_reset(struct channel_backend *chanb);
38
39int lib_ring_buffer_backend_init(void);
40void lib_ring_buffer_backend_exit(void);
41
42extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend *bufb,
43 size_t offset, const void *src, size_t len,
44 ssize_t pagecpy);
45
46/*
47 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
48 * exchanged atomically.
49 *
50 * Top half word, except lowest bit, belongs to "offset", which is used to keep
51 * to count the produced buffers. For overwrite mode, this provides the
52 * consumer with the capacity to read subbuffers in order, handling the
53 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
54 * systems) concurrently with a single execution of get_subbuf (between offset
55 * sampling and subbuffer ID exchange).
56 */
57
14641deb 58#define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1)
852c2936
MD
59
60#define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
61#define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
62#define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
63/*
64 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
65 */
66#define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
67#define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
68#define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
69/*
70 * In overwrite mode: lowest half of word is used for index.
71 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
72 * In producer-consumer mode: whole word used for index.
73 */
74#define SB_ID_INDEX_SHIFT 0
75#define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
76#define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
77
78/*
79 * Construct the subbuffer id from offset, index and noref. Use only the index
80 * for producer-consumer mode (offset and noref are only used in overwrite
81 * mode).
82 */
83static inline
84unsigned long subbuffer_id(const struct lib_ring_buffer_config *config,
85 unsigned long offset, unsigned long noref,
86 unsigned long index)
87{
88 if (config->mode == RING_BUFFER_OVERWRITE)
89 return (offset << SB_ID_OFFSET_SHIFT)
90 | (noref << SB_ID_NOREF_SHIFT)
91 | index;
92 else
93 return index;
94}
95
96/*
97 * Compare offset with the offset contained within id. Return 1 if the offset
98 * bits are identical, else 0.
99 */
100static inline
101int subbuffer_id_compare_offset(const struct lib_ring_buffer_config *config,
102 unsigned long id, unsigned long offset)
103{
104 return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT);
105}
106
107static inline
108unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config *config,
109 unsigned long id)
110{
111 if (config->mode == RING_BUFFER_OVERWRITE)
112 return id & SB_ID_INDEX_MASK;
113 else
114 return id;
115}
116
117static inline
118unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config *config,
119 unsigned long id)
120{
121 if (config->mode == RING_BUFFER_OVERWRITE)
122 return !!(id & SB_ID_NOREF_MASK);
123 else
124 return 1;
125}
126
127/*
128 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
129 * needed.
130 */
131static inline
132void subbuffer_id_set_noref(const struct lib_ring_buffer_config *config,
133 unsigned long *id)
134{
135 if (config->mode == RING_BUFFER_OVERWRITE)
136 *id |= SB_ID_NOREF_MASK;
137}
138
139static inline
140void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config *config,
141 unsigned long *id, unsigned long offset)
142{
143 unsigned long tmp;
144
145 if (config->mode == RING_BUFFER_OVERWRITE) {
146 tmp = *id;
147 tmp &= ~SB_ID_OFFSET_MASK;
148 tmp |= offset << SB_ID_OFFSET_SHIFT;
149 tmp |= SB_ID_NOREF_MASK;
150 /* Volatile store, read concurrently by readers. */
14641deb 151 CMM_ACCESS_ONCE(*id) = tmp;
852c2936
MD
152 }
153}
154
155/* No volatile access, since already used locally */
156static inline
157void subbuffer_id_clear_noref(const struct lib_ring_buffer_config *config,
158 unsigned long *id)
159{
160 if (config->mode == RING_BUFFER_OVERWRITE)
161 *id &= ~SB_ID_NOREF_MASK;
162}
163
164/*
165 * For overwrite mode, cap the number of subbuffers per buffer to:
166 * 2^16 on 32-bit architectures
167 * 2^32 on 64-bit architectures
168 * This is required to fit in the index part of the ID. Return 0 on success,
169 * -EPERM on failure.
170 */
171static inline
172int subbuffer_id_check_index(const struct lib_ring_buffer_config *config,
173 unsigned long num_subbuf)
174{
175 if (config->mode == RING_BUFFER_OVERWRITE)
176 return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0;
177 else
178 return 0;
179}
180
181static inline
182void subbuffer_count_record(const struct lib_ring_buffer_config *config,
183 struct lib_ring_buffer_backend *bufb,
184 unsigned long idx)
185{
186 unsigned long sb_bindex;
187
188 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
189 v_inc(config, &bufb->array[sb_bindex]->records_commit);
190}
191
192/*
193 * Reader has exclusive subbuffer access for record consumption. No need to
194 * perform the decrement atomically.
195 */
196static inline
197void subbuffer_consume_record(const struct lib_ring_buffer_config *config,
198 struct lib_ring_buffer_backend *bufb)
199{
200 unsigned long sb_bindex;
201
202 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
203 CHAN_WARN_ON(bufb->chan,
204 !v_read(config, &bufb->array[sb_bindex]->records_unread));
205 /* Non-atomic decrement protected by exclusive subbuffer access */
206 _v_dec(config, &bufb->array[sb_bindex]->records_unread);
207 v_inc(config, &bufb->records_read);
208}
209
210static inline
211unsigned long subbuffer_get_records_count(
212 const struct lib_ring_buffer_config *config,
213 struct lib_ring_buffer_backend *bufb,
214 unsigned long idx)
215{
216 unsigned long sb_bindex;
217
218 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
219 return v_read(config, &bufb->array[sb_bindex]->records_commit);
220}
221
222/*
223 * Must be executed at subbuffer delivery when the writer has _exclusive_
224 * subbuffer access. See ring_buffer_check_deliver() for details.
225 * ring_buffer_get_records_count() must be called to get the records count
226 * before this function, because it resets the records_commit count.
227 */
228static inline
229unsigned long subbuffer_count_records_overrun(
230 const struct lib_ring_buffer_config *config,
231 struct lib_ring_buffer_backend *bufb,
232 unsigned long idx)
233{
234 struct lib_ring_buffer_backend_pages *pages;
235 unsigned long overruns, sb_bindex;
236
237 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
238 pages = bufb->array[sb_bindex];
239 overruns = v_read(config, &pages->records_unread);
240 v_set(config, &pages->records_unread,
241 v_read(config, &pages->records_commit));
242 v_set(config, &pages->records_commit, 0);
243
244 return overruns;
245}
246
247static inline
248void subbuffer_set_data_size(const struct lib_ring_buffer_config *config,
249 struct lib_ring_buffer_backend *bufb,
250 unsigned long idx,
251 unsigned long data_size)
252{
253 struct lib_ring_buffer_backend_pages *pages;
254 unsigned long sb_bindex;
255
256 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
257 pages = bufb->array[sb_bindex];
258 pages->data_size = data_size;
259}
260
261static inline
262unsigned long subbuffer_get_read_data_size(
263 const struct lib_ring_buffer_config *config,
264 struct lib_ring_buffer_backend *bufb)
265{
266 struct lib_ring_buffer_backend_pages *pages;
267 unsigned long sb_bindex;
268
269 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
270 pages = bufb->array[sb_bindex];
271 return pages->data_size;
272}
273
274static inline
275unsigned long subbuffer_get_data_size(
276 const struct lib_ring_buffer_config *config,
277 struct lib_ring_buffer_backend *bufb,
278 unsigned long idx)
279{
280 struct lib_ring_buffer_backend_pages *pages;
281 unsigned long sb_bindex;
282
283 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
284 pages = bufb->array[sb_bindex];
285 return pages->data_size;
286}
287
288/**
289 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
290 * writer.
291 */
292static inline
293void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config *config,
294 struct lib_ring_buffer_backend *bufb,
295 unsigned long idx)
296{
297 unsigned long id, new_id;
298
299 if (config->mode != RING_BUFFER_OVERWRITE)
300 return;
301
302 /*
303 * Performing a volatile access to read the sb_pages, because we want to
304 * read a coherent version of the pointer and the associated noref flag.
305 */
14641deb 306 id = CMM_ACCESS_ONCE(bufb->buf_wsb[idx].id);
852c2936
MD
307 for (;;) {
308 /* This check is called on the fast path for each record. */
309 if (likely(!subbuffer_id_is_noref(config, id))) {
310 /*
311 * Store after load dependency ordering the writes to
312 * the subbuffer after load and test of the noref flag
313 * matches the memory barrier implied by the cmpxchg()
314 * in update_read_sb_index().
315 */
316 return; /* Already writing to this buffer */
317 }
318 new_id = id;
319 subbuffer_id_clear_noref(config, &new_id);
14641deb 320 new_id = uatomic_cmpxchg(&bufb->buf_wsb[idx].id, id, new_id);
852c2936
MD
321 if (likely(new_id == id))
322 break;
323 id = new_id;
324 }
325}
326
327/**
328 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
329 * called by writer.
330 */
331static inline
332void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config *config,
333 struct lib_ring_buffer_backend *bufb,
334 unsigned long idx, unsigned long offset)
335{
336 if (config->mode != RING_BUFFER_OVERWRITE)
337 return;
338
339 /*
340 * Because ring_buffer_set_noref() is only called by a single thread
341 * (the one which updated the cc_sb value), there are no concurrent
342 * updates to take care of: other writers have not updated cc_sb, so
343 * they cannot set the noref flag, and concurrent readers cannot modify
344 * the pointer because the noref flag is not set yet.
345 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
346 * to the subbuffer before this set noref operation.
347 * subbuffer_set_noref() uses a volatile store to deal with concurrent
348 * readers of the noref flag.
349 */
350 CHAN_WARN_ON(bufb->chan,
351 subbuffer_id_is_noref(config, bufb->buf_wsb[idx].id));
352 /*
353 * Memory barrier that ensures counter stores are ordered before set
354 * noref and offset.
355 */
14641deb 356 cmm_smp_mb();
852c2936
MD
357 subbuffer_id_set_noref_offset(config, &bufb->buf_wsb[idx].id, offset);
358}
359
360/**
361 * update_read_sb_index - Read-side subbuffer index update.
362 */
363static inline
364int update_read_sb_index(const struct lib_ring_buffer_config *config,
365 struct lib_ring_buffer_backend *bufb,
366 struct channel_backend *chanb,
367 unsigned long consumed_idx,
368 unsigned long consumed_count)
369{
370 unsigned long old_id, new_id;
371
372 if (config->mode == RING_BUFFER_OVERWRITE) {
373 /*
374 * Exchange the target writer subbuffer with our own unused
14641deb 375 * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the
852c2936
MD
376 * old_wpage, because the value read will be confirmed by the
377 * following cmpxchg().
378 */
379 old_id = bufb->buf_wsb[consumed_idx].id;
380 if (unlikely(!subbuffer_id_is_noref(config, old_id)))
381 return -EAGAIN;
382 /*
383 * Make sure the offset count we are expecting matches the one
384 * indicated by the writer.
385 */
386 if (unlikely(!subbuffer_id_compare_offset(config, old_id,
387 consumed_count)))
388 return -EAGAIN;
389 CHAN_WARN_ON(bufb->chan,
390 !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
391 subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
392 consumed_count);
14641deb 393 new_id = uatomic_cmpxchg(&bufb->buf_wsb[consumed_idx].id, old_id,
852c2936
MD
394 bufb->buf_rsb.id);
395 if (unlikely(old_id != new_id))
396 return -EAGAIN;
397 bufb->buf_rsb.id = new_id;
398 } else {
399 /* No page exchange, use the writer page directly */
400 bufb->buf_rsb.id = bufb->buf_wsb[consumed_idx].id;
401 }
402 return 0;
403}
404
405/*
406 * Use the architecture-specific memcpy implementation for constant-sized
407 * inputs, but rely on an inline memcpy for length statically unknown.
408 * The function call to memcpy is just way too expensive for a fast path.
409 */
410#define lib_ring_buffer_do_copy(config, dest, src, len) \
411do { \
412 size_t __len = (len); \
413 if (__builtin_constant_p(len)) \
414 memcpy(dest, src, __len); \
415 else \
416 inline_memcpy(dest, src, __len); \
417} while (0)
418
419#endif /* _LINUX_RING_BUFFER_BACKEND_INTERNAL_H */
This page took 0.037565 seconds and 4 git commands to generate.