ring: introduce RTS ring mode
[dpdk.git] / doc / guides / prog_guide / ring_lib.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2010-2014 Intel Corporation.
3
4 .. _Ring_Library:
5
6 Ring Library
7 ============
8
9 The ring allows the management of queues.
10 Instead of having a linked list of infinite size, the rte_ring has the following properties:
11
12 *   FIFO
13
14 *   Maximum size is fixed, the objects are stored in a table
15
16 *   Objects can be pointers or elements of multiple of 4 byte size
17
18 *   Lockless implementation
19
20 *   Multi-consumer or single-consumer dequeue
21
22 *   Multi-producer or single-producer enqueue
23
24 *   Bulk dequeue - Dequeues the specified count of objects if successful; otherwise fails
25
26 *   Bulk enqueue - Enqueues the specified count of objects if successful; otherwise fails
27
28 *   Burst dequeue - Dequeue the maximum available objects if the specified count cannot be fulfilled
29
30 *   Burst enqueue - Enqueue the maximum available objects if the specified count cannot be fulfilled
31
32 The advantages of this data structure over a linked list queue are as follows:
33
34 *   Faster; only requires a single 32 bit Compare-And-Swap instruction instead of several pointer size Compare-And-Swap instructions.
35
36 *   Simpler than a full lockless queue.
37
38 *   Adapted to bulk enqueue/dequeue operations.
39     As objects are stored in a table, a dequeue of several objects will not produce as many cache misses as in a linked queue.
40     Also, a bulk dequeue of many objects does not cost more than a dequeue of a simple object.
41
42 The disadvantages:
43
44 *   Size is fixed
45
46 *   Having many rings costs more in terms of memory than a linked list queue. An empty ring contains at least N objects.
47
48 A simplified representation of a Ring is shown in with consumer and producer head and tail pointers to objects stored in the data structure.
49
50 .. _figure_ring1:
51
52 .. figure:: img/ring1.*
53
54    Ring Structure
55
56
57 References for Ring Implementation in FreeBSD*
58 ----------------------------------------------
59
60 The following code was added in FreeBSD 8.0, and is used in some network device drivers (at least in Intel drivers):
61
62     * `bufring.h in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/sys/buf_ring.h?revision=199625&amp;view=markup>`_
63
64     * `bufring.c in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/kern/subr_bufring.c?revision=199625&amp;view=markup>`_
65
66 Lockless Ring Buffer in Linux*
67 ------------------------------
68
69 The following is a link describing the `Linux Lockless Ring Buffer Design <http://lwn.net/Articles/340400/>`_.
70
71 Additional Features
72 -------------------
73
74 Name
75 ~~~~
76
77 A ring is identified by a unique name.
78 It is not possible to create two rings with the same name (rte_ring_create() returns NULL if this is attempted).
79
80 Use Cases
81 ---------
82
83 Use cases for the Ring library include:
84
85     *  Communication between applications in the DPDK
86
87     *  Used by memory pool allocator
88
89 Anatomy of a Ring Buffer
90 ------------------------
91
92 This section explains how a ring buffer operates.
93 The ring structure is composed of two head and tail couples; one is used by producers and one is used by the consumers.
94 The figures of the following sections refer to them as prod_head, prod_tail, cons_head and cons_tail.
95
96 Each figure represents a simplified state of the ring, which is a circular buffer.
97 The content of the function local variables is represented on the top of the figure,
98 and the content of ring structure is represented on the bottom of the figure.
99
100 Single Producer Enqueue
101 ~~~~~~~~~~~~~~~~~~~~~~~
102
103 This section explains what occurs when a producer adds an object to the ring.
104 In this example, only the producer head and tail (prod_head and prod_tail) are modified,
105 and there is only one producer.
106
107 The initial state is to have a prod_head and prod_tail pointing at the same location.
108
109 Enqueue First Step
110 ^^^^^^^^^^^^^^^^^^
111
112 First, *ring->prod_head* and ring->cons_tail are copied in local variables.
113 The prod_next local variable points to the next element of the table, or several elements after in case of bulk enqueue.
114
115 If there is not enough room in the ring (this is detected by checking cons_tail), it returns an error.
116
117
118 .. _figure_ring-enqueue1:
119
120 .. figure:: img/ring-enqueue1.*
121
122    Enqueue first step
123
124
125 Enqueue Second Step
126 ^^^^^^^^^^^^^^^^^^^
127
128 The second step is to modify *ring->prod_head* in ring structure to point to the same location as prod_next.
129
130 The added object is copied in the ring (obj4).
131
132
133 .. _figure_ring-enqueue2:
134
135 .. figure:: img/ring-enqueue2.*
136
137    Enqueue second step
138
139
140 Enqueue Last Step
141 ^^^^^^^^^^^^^^^^^
142
143 Once the object is added in the ring, ring->prod_tail in the ring structure is modified to point to the same location as *ring->prod_head*.
144 The enqueue operation is finished.
145
146
147 .. _figure_ring-enqueue3:
148
149 .. figure:: img/ring-enqueue3.*
150
151    Enqueue last step
152
153
154 Single Consumer Dequeue
155 ~~~~~~~~~~~~~~~~~~~~~~~
156
157 This section explains what occurs when a consumer dequeues an object from the ring.
158 In this example, only the consumer head and tail (cons_head and cons_tail) are modified and there is only one consumer.
159
160 The initial state is to have a cons_head and cons_tail pointing at the same location.
161
162 Dequeue First Step
163 ^^^^^^^^^^^^^^^^^^
164
165 First, ring->cons_head and ring->prod_tail are copied in local variables.
166 The cons_next local variable points to the next element of the table, or several elements after in the case of bulk dequeue.
167
168 If there are not enough objects in the ring (this is detected by checking prod_tail), it returns an error.
169
170
171 .. _figure_ring-dequeue1:
172
173 .. figure:: img/ring-dequeue1.*
174
175    Dequeue last step
176
177
178 Dequeue Second Step
179 ^^^^^^^^^^^^^^^^^^^
180
181 The second step is to modify ring->cons_head in the ring structure to point to the same location as cons_next.
182
183 The dequeued object (obj1) is copied in the pointer given by the user.
184
185
186 .. _figure_ring-dequeue2:
187
188 .. figure:: img/ring-dequeue2.*
189
190    Dequeue second step
191
192
193 Dequeue Last Step
194 ^^^^^^^^^^^^^^^^^
195
196 Finally, ring->cons_tail in the ring structure is modified to point to the same location as ring->cons_head.
197 The dequeue operation is finished.
198
199
200 .. _figure_ring-dequeue3:
201
202 .. figure:: img/ring-dequeue3.*
203
204    Dequeue last step
205
206
207 Multiple Producers Enqueue
208 ~~~~~~~~~~~~~~~~~~~~~~~~~~
209
210 This section explains what occurs when two producers concurrently add an object to the ring.
211 In this example, only the producer head and tail (prod_head and prod_tail) are modified.
212
213 The initial state is to have a prod_head and prod_tail pointing at the same location.
214
215 Multiple Producers Enqueue First Step
216 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
217
218 On both cores, *ring->prod_head* and ring->cons_tail are copied in local variables.
219 The prod_next local variable points to the next element of the table,
220 or several elements after in the case of bulk enqueue.
221
222 If there is not enough room in the ring (this is detected by checking cons_tail), it returns an error.
223
224
225 .. _figure_ring-mp-enqueue1:
226
227 .. figure:: img/ring-mp-enqueue1.*
228
229    Multiple producer enqueue first step
230
231
232 Multiple Producers Enqueue Second Step
233 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
234
235 The second step is to modify ring->prod_head in the ring structure to point to the same location as prod_next.
236 This operation is done using a Compare And Swap (CAS) instruction, which does the following operations atomically:
237
238 *   If ring->prod_head is different to local variable prod_head,
239     the CAS operation fails, and the code restarts at first step.
240
241 *   Otherwise, ring->prod_head is set to local prod_next,
242     the CAS operation is successful, and processing continues.
243
244 In the figure, the operation succeeded on core 1, and step one restarted on core 2.
245
246
247 .. _figure_ring-mp-enqueue2:
248
249 .. figure:: img/ring-mp-enqueue2.*
250
251    Multiple producer enqueue second step
252
253
254 Multiple Producers Enqueue Third Step
255 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
256
257 The CAS operation is retried on core 2 with success.
258
259 The core 1 updates one element of the ring(obj4), and the core 2 updates another one (obj5).
260
261
262 .. _figure_ring-mp-enqueue3:
263
264 .. figure:: img/ring-mp-enqueue3.*
265
266    Multiple producer enqueue third step
267
268
269 Multiple Producers Enqueue Fourth Step
270 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
271
272 Each core now wants to update ring->prod_tail.
273 A core can only update it if ring->prod_tail is equal to the prod_head local variable.
274 This is only true on core 1. The operation is finished on core 1.
275
276
277 .. _figure_ring-mp-enqueue4:
278
279 .. figure:: img/ring-mp-enqueue4.*
280
281    Multiple producer enqueue fourth step
282
283
284 Multiple Producers Enqueue Last Step
285 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
286
287 Once ring->prod_tail is updated by core 1, core 2 is allowed to update it too.
288 The operation is also finished on core 2.
289
290
291 .. _figure_ring-mp-enqueue5:
292
293 .. figure:: img/ring-mp-enqueue5.*
294
295    Multiple producer enqueue last step
296
297
298 Modulo 32-bit Indexes
299 ~~~~~~~~~~~~~~~~~~~~~
300
301 In the preceding figures, the prod_head, prod_tail, cons_head and cons_tail indexes are represented by arrows.
302 In the actual implementation, these values are not between 0 and size(ring)-1 as would be assumed.
303 The indexes are between 0 and 2^32 -1, and we mask their value when we access the object table (the ring itself).
304 32-bit modulo also implies that operations on indexes (such as, add/subtract) will automatically do 2^32 modulo
305 if the result overflows the 32-bit number range.
306
307 The following are two examples that help to explain how indexes are used in a ring.
308
309 .. note::
310
311     To simplify the explanation, operations with modulo 16-bit are used instead of modulo 32-bit.
312     In addition, the four indexes are defined as unsigned 16-bit integers,
313     as opposed to unsigned 32-bit integers in the more realistic case.
314
315
316 .. _figure_ring-modulo1:
317
318 .. figure:: img/ring-modulo1.*
319
320    Modulo 32-bit indexes - Example 1
321
322
323 This ring contains 11000 entries.
324
325
326 .. _figure_ring-modulo2:
327
328 .. figure:: img/ring-modulo2.*
329
330       Modulo 32-bit indexes - Example 2
331
332
333 This ring contains 12536 entries.
334
335 .. note::
336
337     For ease of understanding, we use modulo 65536 operations in the above examples.
338     In real execution cases, this is redundant for low efficiency, but is done automatically when the result overflows.
339
340 The code always maintains a distance between producer and consumer between 0 and size(ring)-1.
341 Thanks to this property, we can do subtractions between 2 index values in a modulo-32bit base:
342 that's why the overflow of the indexes is not a problem.
343
344 At any time, entries and free_entries are between 0 and size(ring)-1,
345 even if only the first term of subtraction has overflowed:
346
347 .. code-block:: c
348
349     uint32_t entries = (prod_tail - cons_head);
350     uint32_t free_entries = (mask + cons_tail -prod_head);
351
352 Producer/consumer synchronization modes
353 ---------------------------------------
354
355 rte_ring supports different synchronization modes for producers and consumers.
356 These modes can be specified at ring creation/init time via ``flags``
357 parameter.
358 That should help users to configure ring in the most suitable way for his
359 specific usage scenarios.
360 Currently supported modes:
361
362 MP/MC (default one)
363 ~~~~~~~~~~~~~~~~~~~
364
365 Multi-producer (/multi-consumer) mode. This is a default enqueue (/dequeue)
366 mode for the ring. In this mode multiple threads can enqueue (/dequeue)
367 objects to (/from) the ring. For 'classic' DPDK deployments (with one thread
368 per core) this is usually the most suitable and fastest synchronization mode.
369 As a well known limitation - it can perform quite pure on some overcommitted
370 scenarios.
371
372 SP/SC
373 ~~~~~
374 Single-producer (/single-consumer) mode. In this mode only one thread at a time
375 is allowed to enqueue (/dequeue) objects to (/from) the ring.
376
377 MP_RTS/MC_RTS
378 ~~~~~~~~~~~~~
379
380 Multi-producer (/multi-consumer) with Relaxed Tail Sync (RTS) mode.
381 The main difference from the original MP/MC algorithm is that
382 tail value is increased not by every thread that finished enqueue/dequeue,
383 but only by the last one.
384 That allows threads to avoid spinning on ring tail value,
385 leaving actual tail value change to the last thread at a given instance.
386 That technique helps to avoid the Lock-Waiter-Preemption (LWP) problem on tail
387 update and improves average enqueue/dequeue times on overcommitted systems.
388 To achieve that RTS requires 2 64-bit CAS for each enqueue(/dequeue) operation:
389 one for head update, second for tail update.
390 In comparison the original MP/MC algorithm requires one 32-bit CAS
391 for head update and waiting/spinning on tail value.
392
393 References
394 ----------
395
396     *   `bufring.h in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/sys/buf_ring.h?revision=199625&amp;view=markup>`_ (version 8)
397
398     *   `bufring.c in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/kern/subr_bufring.c?revision=199625&amp;view=markup>`_ (version 8)
399
400     *   `Linux Lockless Ring Buffer Design <http://lwn.net/Articles/340400/>`_