RCU Judy Array Design and initial files
[userspace-rcu.git] / rcuja / design.txt
CommitLineData
61009379
MD
1RCU Judy Array Design
2Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3March 8, 2012
4
5Initial ideas based on the released Judy Shop Manual
6(http://judy.sourceforge.net/). Judy was invented by Doug Baskins and
7implemented by Hewlett-Packard.
8
9Thresholds and RCU-specific analysis is introduced in this document.
10
11Advantages of using Judy Array (compressed nodes) for RCU tree:
12- no rebalancing
13- no transplant
14- RCU-friendly!
15- favor cache-line alignment of structures
16
17Disadvantage:
18- updates that need to reallocate nodes are slower than, e.g. non-rcu
19 red-black trees.
20
21Choice: Using 256 entries intermediate nodes (index can be represented
22on 8 bits): 4 levels on 32-bit, 8 levels on 64-bit
23
24
25* Node types (from less dense node to most dense)
26
27
28- empty node:
29
30Parent pointer is NULL.
31
32
33- Type A: sequential search in value and pointer arrays
34
35+ Add/removal just needs to update value and pointer array, single-entry
36 (non-RCU...). For RCU, we might need to update the entire node anyway.
37- Requires sequential search through all value array for lookup fail
38 test.
39
40Filled at 3 entries max 64-bit
418 bits indicating number of children
42Array of 8-bit values followed by array of associated pointers.
4364-bit: 1 byte + 3 bytes + 4 bytes pad + 3*8 = 32 bytes
44
45-> up to this point on 64-bit, sequential lookup and pointer read fit in
46a 32-byte cache line.
47 - lookup fail&success: 1 cache-line.
48
49Filled at 6 entries max 32-bit, 7 entries max 64-bit
508 bits indicating number of children
51Array of 8-bit values followed by array of associated pointers.
5232-bit: 1 byte + 6 bytes + 1 byte pad + 6*4bytes = 32 bytes
5364-bit: 1 byte + 7 bytes + 7*8 = 64 bytes
54
55-> up to this point on 32-bit, sequential lookup and pointer read fit in
56a 32-byte cache line.
57 - lookup fail&success: 1 cache-line.
58
59Filled at 12 entries max 32-bit, 14 entries max 64-bit
608 bits indicating number of children
61Array of 8-bit values followed by array of associated pointers.
6232-bit: 1 byte + 12 bytes + 3 bytes pad + 12*4bytes = 64 bytes
6364-bit: 1 byte + 14 bytes + 1 byte pad + 14*8 = 128 bytes
64
65Filled at 25 entries max 32-bit, 28 entries max 64-bit
668 bits indicating number of children
67Array of 8-bit values followed by array of associated pointers.
6832-bit: 1 byte + 25 bytes + 2 bytes pad + 25*4bytes = 128 bytes
6964-bit: 1 byte + 28 bytes + 3 bytes pad + 28*8 = 256 bytes
70
71---> up to this point, on both 32-bit and 64-bit, the sequential lookup
72in values array fits in a 32-byte cache line.
73 - lookup failure: 1 cache line.
74 - lookup success: 2 cache lines.
75
76The two below are listed for completeness sake, but because they require
772 32-byte cache lines for lookup, these are deemed inappropriate.
78
79Filled at 51 entries max 32-bit, 56 entries max 64-bit
808 bits indicating number of children
81Array of 8-bit values followed by array of associated pointers.
8232-bit: 1 byte + 51 bytes + 51*4bytes = 256 bytes
8364-bit: 1 byte + 56 bytes + 7 bytes pad + 56*8 = 512 bytes
84
85Filled at 102 entries max 32-bit, 113 entries max 64-bit
868 bits indicating number of children
87Array of 8-bit values followed by array of associated pointers.
8832-bit: 1 byte + 102 bytes + 1 byte pad + 102*4bytes = 512 bytes
8964-bit: 1 byte + 113 bytes + 6 bytes pad + 113*8 = 1024 bytes
90
91
92- Type B: bitmap, followed by pointers array.
93
94bitmask (256 entries -> 256 bits -> 32 bytes) of populated children
95followed by an array of children pointers, in same order as appears in
96the bitmask
97
98+ Allows lookup failure to use 32-byte cache-line only. (1 cacheline)
99+ Allows lookup success to use 32-byte cache-line (bitmap),
100 followed by direct lookup into pointer array. (2 cachelines)
101
102Filled at 8 entries 32-bit, 12 entries 64-bit
10332-bit: 32 + (4*8) -> 64 bytes
10464-bit: 32 + (8*12) -> 128 bytes
105
106Filled at 24 entries 32-bit, 28 entries 64-bit
10732-bit: 32 + (4*24) -> 128 bytes
10864-bit: 32 + (8*28) -> 256 bytes
109
110Filled at 56 entries 32-bit, 60 entries 64-bit
11132-bit: 32 + (4*56) -> 256 bytes
11264-bit: 32 + (8*60) -> 512 bytes
113
114Filled at 120 entries 32-bit, 124 entries 64-bit
11532-bit: 32 + (4*95) -> 512 bytes
11664-bit: 32 + (8*124) -> 1024 bytes
117
118
119- Type C: pigeon-hole array
120
121Filled at 47.2%/48.8% or more (32-bit: 121 entries+, 64-bit: 125 entries+)
122Array of children node pointers. Pointers NULL if no child at index.
12332-bit: 4*256 = 1024 bytes
12464-bit: 8*256 = 2048 bytes
125
126
127* Analysis of the thresholds:
128
129Analysis of number of cache-lines touched for each node, per-node-type,
130depending on the number of children per node, as we increment the number
131of children from 0 to 256. Through this, we choose number of children
132thresholds at which it is worthwhile to use a different node type.
133
134Per node:
135
136- ALWAYS 1 cache line hit for lookup failure (all cases)
137
13832-bit
139
140- Unexisting
141
1420 children
143
144- Type A: sequential search in value and pointer arrays
145- 1 cache line hit for lookup success
146- 32 bytes storage
147
148up to 6 children
149
150- 2 cache line hit for lookup success
151- 64 bytes storage
152
153up to 12 children
154
155- Type B: bitmap, followed by pointers array.
156- 2 cache line hit for lookup success
157- 128 bytes storage
158
159up to 24 children
160
161- 256 bytes storage
162up to 56 children
163
164- 512 bytes storage
165up to 120 children
166
167- Type C: pigeon-hole array
168- 1 cache line hit for lookup success
169- 1024 bytes storage
170
171up to 256 children
172
173
17464-bit
175
176- Unexisting
177
1780 children
179
180- Type A: sequential search in value and pointer arrays
181- 1 cache line hit for lookup success
182- 32 bytes storage
183
184up to 3 children
185
186- 2 cache line hit for lookup success
187- 64 bytes storage
188
189up to 7 children
190
191- 128 bytes storage
192
193up to 14 children
194
195- Type B: bitmap, followed by pointers array.
196- 2 cache line hit for lookup success
197- 256 bytes storage
198
199up to 28 children
200
201- 512 bytes storage
202up to 60 children
203
204- 1024 bytes storage
205up to 124 children
206
207- Type C: pigeon-hole array
208- 1 cache line hit for lookup success
209- 2048 bytes storage
210
211up to 256 children
212
213
214* Analysis of node type encoding and node pointers:
215
216Lookups are _always_ from the top of the tree going down. This
217facilitates RCU replacement as we only keep track of pointers going
218downward.
219
220Type of node encoded in the parent's pointer. Need to reserve 2
221least-significant bits.
222
223Types of children:
224
225enum child_type {
226 LINEAR = 0, /* Type A */
227 /* 32-bit: 1 to 12 children, 32 to 64 bytes */
228 /* 64-bit: 1 to 14 children, 32 to 128 bytes */
229 BITMAP = 1, /* Type B */
230 /* 32-bit: 13 to 120 children, 128 to 512 bytes */
231 /* 64-bit: 15 to 124 children, 256 to 1024 bytes */
232 PIGEON = 2, /* Type C */
233 /* 32-bit: 121 to 256 children, 1024 bytes */
234 /* 64-bit: 125 to 256 children, 2048 bytes */
235 LEAF = 3, /* Leaf node */
236};
237
238If entire pointer is NULL, children is empty.
239
240
241* Lookup and Update Algorithms
242
243Let's propose a quite simple scheme that uses a mutex on nodes to manage
244update concurrency. It's certainly not optimal in terms of concurrency
245management within a node, but it has the advantage of being simple to
246implement and understand.
247
248We need to keep a count of the number of children nodes (for each node),
249to keep track of when the node type thresholds are reached. It would be
250important to put an hysteresis loop so we don't change between node
251types too often for a loop on add/removal of the same node.
252
253We acquire locks from child to parent, nested. We take all locks
254required to perform a given update in the tree (but no more) to keep it
255consistent with respect to number of children per node.
256
257If check for node being gc'd (always under node lock) fails, we simply
258need to release the lock and lookup the node again.
259
260
261- Leaf lookup
262
263rcu_read_lock()
264
265RCU-lookup each level of the tree. If level is not populated, fail.
266Until we reach the leaf node.
267
268rcu_read_unlock()
269
270
271- Leaf insertion
272
273A) Lookup
274
275rcu_read_lock()
276RCU-lookup insert position. Find location in tree where nodes are
277missing for this insertion. If leaf is already present, insert fails,
278releasing the rcu read lock. The insert location consists of a parent
279node to which we want to attach a new node.
280
281B) Lock
282
283RCU-lookup parent node. Take the parent lock. If the parent needs to be
284reallocated to make room for this insertion, RCU-lookup parent-parent
285node and take the parent-parent lock. For each lock taken, check if
286node is being gc'd. If gc'd, release lock, re-RCU-lookup this node, and
287retry.
288
289C) Create
290
291Construct the whole branch from the new topmost intermediate node down
292to the new leaf node we are inserting.
293
294D) Populate:
295 - If parent node reallocation is needed:
296 Reallocate the parent node, adding the new branch to it, and
297 increment its node count.
298 set gc flag in old nodes.
299 call_rcu free for all old nodes.
300 Populate new parent node with rcu_assign_pointer.
301 - Else:
302 Increment parent node count.
303 Use rcu_assign_pointer to populate this new branch into the parent
304 node.
305
306E) Locks
307
308Release parent and (if taken) parent-parent locks.
309rcu_read_unlock()
310
311
312- Leaf removal
313
314A) Lookup
315
316rcu_read_lock()
317RCU-lookup leaf to remove. If leaf is missing, fail and release rcu
318read lock.
319
320B) Lock
321
322RCU-lookup parent. Take the parent lock. If the parent needs to be
323reallocated because it would be too large for the decremented number of
324children, RCU-lookup parent-parent and take the parent-parent lock. Do
325so recursively until no node reallocation is needed, or until root is
326reached.
327
328For each lock taken, check if node is being gc'd. If gc'd, release lock,
329re-RCU-lookup this node, and retry.
330
331C) Create
332
333The branch (or portion of branch) consisting of taken locks necessarily
334has a simple node removal or update as operation to do on its top node.
335
336If the operation is a node removal, then, necessarily, the entire branch
337under the node removal operation will simply disappear. No node
338allocation is needed.
339
340Else, if the operation is a child node reallocation, the child node will
341necessarily do a node removal. So _its_ entire child branch will
342disappear. So reallocate this child node without the removed branch
343(remember to decrement its nr children count).
344
345D) Populate
346
347No reallocation case: simply set the appropriate child pointer in the
348topmost locked node to NULL. Decrement its nr children count.
349
350Reallocation case: set the child pointer in the topmost locked node to
351the newly allocated node.
352set old nodes gc flag.
353call_rcu free for all old nodes.
354
355E) Locks
356
357Release all locks.
358rcu_read_unlock()
359
360
361For the various types of nodes:
362
363- sequential search (type A)
364 - RCU replacement: mutex
365 - Entry update: mutex
366
367- bitmap followed by pointer array (type B)
368 - RCU replacement: mutex
369 - Entry update: mutex
370
371- pigeon hole array (type C)
372 - RCU replacement: mutex
373 - Entry update: mutex
This page took 0.034822 seconds and 4 git commands to generate.