Back to home page

OSCL-LXR

 
 

    


0001 ================
0002 Circular Buffers
0003 ================
0004 
0005 :Author: David Howells <dhowells@redhat.com>
0006 :Author: Paul E. McKenney <paulmck@linux.ibm.com>
0007 
0008 
0009 Linux provides a number of features that can be used to implement circular
0010 buffering.  There are two sets of such features:
0011 
0012  (1) Convenience functions for determining information about power-of-2 sized
0013      buffers.
0014 
0015  (2) Memory barriers for when the producer and the consumer of objects in the
0016      buffer don't want to share a lock.
0017 
0018 To use these facilities, as discussed below, there needs to be just one
0019 producer and just one consumer.  It is possible to handle multiple producers by
0020 serialising them, and to handle multiple consumers by serialising them.
0021 
0022 
0023 .. Contents:
0024 
0025  (*) What is a circular buffer?
0026 
0027  (*) Measuring power-of-2 buffers.
0028 
0029  (*) Using memory barriers with circular buffers.
0030      - The producer.
0031      - The consumer.
0032 
0033 
0034 
0035 What is a circular buffer?
0036 ==========================
0037 
0038 First of all, what is a circular buffer?  A circular buffer is a buffer of
0039 fixed, finite size into which there are two indices:
0040 
0041  (1) A 'head' index - the point at which the producer inserts items into the
0042      buffer.
0043 
0044  (2) A 'tail' index - the point at which the consumer finds the next item in
0045      the buffer.
0046 
0047 Typically when the tail pointer is equal to the head pointer, the buffer is
0048 empty; and the buffer is full when the head pointer is one less than the tail
0049 pointer.
0050 
0051 The head index is incremented when items are added, and the tail index when
0052 items are removed.  The tail index should never jump the head index, and both
0053 indices should be wrapped to 0 when they reach the end of the buffer, thus
0054 allowing an infinite amount of data to flow through the buffer.
0055 
0056 Typically, items will all be of the same unit size, but this isn't strictly
0057 required to use the techniques below.  The indices can be increased by more
0058 than 1 if multiple items or variable-sized items are to be included in the
0059 buffer, provided that neither index overtakes the other.  The implementer must
0060 be careful, however, as a region more than one unit in size may wrap the end of
0061 the buffer and be broken into two segments.
0062 
0063 Measuring power-of-2 buffers
0064 ============================
0065 
0066 Calculation of the occupancy or the remaining capacity of an arbitrarily sized
0067 circular buffer would normally be a slow operation, requiring the use of a
0068 modulus (divide) instruction.  However, if the buffer is of a power-of-2 size,
0069 then a much quicker bitwise-AND instruction can be used instead.
0070 
0071 Linux provides a set of macros for handling power-of-2 circular buffers.  These
0072 can be made use of by::
0073 
0074         #include <linux/circ_buf.h>
0075 
0076 The macros are:
0077 
0078  (#) Measure the remaining capacity of a buffer::
0079 
0080         CIRC_SPACE(head_index, tail_index, buffer_size);
0081 
0082      This returns the amount of space left in the buffer[1] into which items
0083      can be inserted.
0084 
0085 
0086  (#) Measure the maximum consecutive immediate space in a buffer::
0087 
0088         CIRC_SPACE_TO_END(head_index, tail_index, buffer_size);
0089 
0090      This returns the amount of consecutive space left in the buffer[1] into
0091      which items can be immediately inserted without having to wrap back to the
0092      beginning of the buffer.
0093 
0094 
0095  (#) Measure the occupancy of a buffer::
0096 
0097         CIRC_CNT(head_index, tail_index, buffer_size);
0098 
0099      This returns the number of items currently occupying a buffer[2].
0100 
0101 
0102  (#) Measure the non-wrapping occupancy of a buffer::
0103 
0104         CIRC_CNT_TO_END(head_index, tail_index, buffer_size);
0105 
0106      This returns the number of consecutive items[2] that can be extracted from
0107      the buffer without having to wrap back to the beginning of the buffer.
0108 
0109 
0110 Each of these macros will nominally return a value between 0 and buffer_size-1,
0111 however:
0112 
0113  (1) CIRC_SPACE*() are intended to be used in the producer.  To the producer
0114      they will return a lower bound as the producer controls the head index,
0115      but the consumer may still be depleting the buffer on another CPU and
0116      moving the tail index.
0117 
0118      To the consumer it will show an upper bound as the producer may be busy
0119      depleting the space.
0120 
0121  (2) CIRC_CNT*() are intended to be used in the consumer.  To the consumer they
0122      will return a lower bound as the consumer controls the tail index, but the
0123      producer may still be filling the buffer on another CPU and moving the
0124      head index.
0125 
0126      To the producer it will show an upper bound as the consumer may be busy
0127      emptying the buffer.
0128 
0129  (3) To a third party, the order in which the writes to the indices by the
0130      producer and consumer become visible cannot be guaranteed as they are
0131      independent and may be made on different CPUs - so the result in such a
0132      situation will merely be a guess, and may even be negative.
0133 
0134 Using memory barriers with circular buffers
0135 ===========================================
0136 
0137 By using memory barriers in conjunction with circular buffers, you can avoid
0138 the need to:
0139 
0140  (1) use a single lock to govern access to both ends of the buffer, thus
0141      allowing the buffer to be filled and emptied at the same time; and
0142 
0143  (2) use atomic counter operations.
0144 
0145 There are two sides to this: the producer that fills the buffer, and the
0146 consumer that empties it.  Only one thing should be filling a buffer at any one
0147 time, and only one thing should be emptying a buffer at any one time, but the
0148 two sides can operate simultaneously.
0149 
0150 
0151 The producer
0152 ------------
0153 
0154 The producer will look something like this::
0155 
0156         spin_lock(&producer_lock);
0157 
0158         unsigned long head = buffer->head;
0159         /* The spin_unlock() and next spin_lock() provide needed ordering. */
0160         unsigned long tail = READ_ONCE(buffer->tail);
0161 
0162         if (CIRC_SPACE(head, tail, buffer->size) >= 1) {
0163                 /* insert one item into the buffer */
0164                 struct item *item = buffer[head];
0165 
0166                 produce_item(item);
0167 
0168                 smp_store_release(buffer->head,
0169                                   (head + 1) & (buffer->size - 1));
0170 
0171                 /* wake_up() will make sure that the head is committed before
0172                  * waking anyone up */
0173                 wake_up(consumer);
0174         }
0175 
0176         spin_unlock(&producer_lock);
0177 
0178 This will instruct the CPU that the contents of the new item must be written
0179 before the head index makes it available to the consumer and then instructs the
0180 CPU that the revised head index must be written before the consumer is woken.
0181 
0182 Note that wake_up() does not guarantee any sort of barrier unless something
0183 is actually awakened.  We therefore cannot rely on it for ordering.  However,
0184 there is always one element of the array left empty.  Therefore, the
0185 producer must produce two elements before it could possibly corrupt the
0186 element currently being read by the consumer.  Therefore, the unlock-lock
0187 pair between consecutive invocations of the consumer provides the necessary
0188 ordering between the read of the index indicating that the consumer has
0189 vacated a given element and the write by the producer to that same element.
0190 
0191 
0192 The Consumer
0193 ------------
0194 
0195 The consumer will look something like this::
0196 
0197         spin_lock(&consumer_lock);
0198 
0199         /* Read index before reading contents at that index. */
0200         unsigned long head = smp_load_acquire(buffer->head);
0201         unsigned long tail = buffer->tail;
0202 
0203         if (CIRC_CNT(head, tail, buffer->size) >= 1) {
0204 
0205                 /* extract one item from the buffer */
0206                 struct item *item = buffer[tail];
0207 
0208                 consume_item(item);
0209 
0210                 /* Finish reading descriptor before incrementing tail. */
0211                 smp_store_release(buffer->tail,
0212                                   (tail + 1) & (buffer->size - 1));
0213         }
0214 
0215         spin_unlock(&consumer_lock);
0216 
0217 This will instruct the CPU to make sure the index is up to date before reading
0218 the new item, and then it shall make sure the CPU has finished reading the item
0219 before it writes the new tail pointer, which will erase the item.
0220 
0221 Note the use of READ_ONCE() and smp_load_acquire() to read the
0222 opposition index.  This prevents the compiler from discarding and
0223 reloading its cached value.  This isn't strictly needed if you can
0224 be sure that the opposition index will _only_ be used the once.
0225 The smp_load_acquire() additionally forces the CPU to order against
0226 subsequent memory references.  Similarly, smp_store_release() is used
0227 in both algorithms to write the thread's index.  This documents the
0228 fact that we are writing to something that can be read concurrently,
0229 prevents the compiler from tearing the store, and enforces ordering
0230 against previous accesses.
0231 
0232 
0233 Further reading
0234 ===============
0235 
0236 See also Documentation/memory-barriers.txt for a description of Linux's memory
0237 barrier facilities.