4 * Userspace RCU library - RCU Judy Array
6 * Copyright (C) 2000 - 2002 Hewlett-Packard Company
7 * Copyright 2012-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <urcu/rcuja.h>
31 #include <urcu/compiler.h>
32 #include <urcu/arch.h>
33 #include <urcu-pointer.h>
34 #include <urcu/uatomic.h>
36 #include "rcuja-internal.h"
39 #define abs_int(a) ((int) (a) > 0 ? (int) (a) : -((int) (a)))
42 enum cds_ja_type_class
{
43 RCU_JA_LINEAR
= 0, /* Type A */
44 /* 32-bit: 1 to 25 children, 8 to 128 bytes */
45 /* 64-bit: 1 to 28 children, 16 to 256 bytes */
46 RCU_JA_POOL
= 1, /* Type B */
47 /* 32-bit: 26 to 100 children, 256 to 512 bytes */
48 /* 64-bit: 29 to 112 children, 512 to 1024 bytes */
49 RCU_JA_PIGEON
= 2, /* Type C */
50 /* 32-bit: 101 to 256 children, 1024 bytes */
51 /* 64-bit: 113 to 256 children, 2048 bytes */
52 /* Leaf nodes are implicit from their height in the tree */
55 RCU_JA_NULL
, /* not an encoded type, but keeps code regular */
59 enum cds_ja_type_class type_class
;
60 uint16_t min_child
; /* minimum number of children: 1 to 256 */
61 uint16_t max_child
; /* maximum number of children: 1 to 256 */
62 uint16_t max_linear_child
; /* per-pool max nr. children: 1 to 256 */
63 uint16_t order
; /* node size is (1 << order), in bytes */
64 uint16_t nr_pool_order
; /* number of pools */
65 uint16_t pool_size_order
; /* pool size */
69 * Iteration on the array to find the right node size for the number of
70 * children stops when it reaches .max_child == 256 (this is the largest
71 * possible node size, which contains 256 children).
72 * The min_child overlaps with the previous max_child to provide an
73 * hysteresis loop to reallocation for patterns of cyclic add/removal
74 * within the same node.
75 * The node the index within the following arrays is represented on 3
76 * bits. It identifies the node type, min/max number of children, and
78 * The max_child values for the RCU_JA_POOL below result from
79 * statistical approximation: over million populations, the max_child
80 * covers between 97% and 99% of the populations generated. Therefore, a
81 * fallback should exist to cover the rare extreme population unbalance
82 * cases, but it will not have a major impact on speed nor space
83 * consumption, since those are rare cases.
86 #if (CAA_BITS_PER_LONG < 64)
89 ja_type_0_max_child
= 1,
90 ja_type_1_max_child
= 3,
91 ja_type_2_max_child
= 6,
92 ja_type_3_max_child
= 12,
93 ja_type_4_max_child
= 25,
94 ja_type_5_max_child
= 48,
95 ja_type_6_max_child
= 92,
96 ja_type_7_max_child
= 256,
97 ja_type_8_max_child
= 0, /* NULL */
101 ja_type_0_max_linear_child
= 1,
102 ja_type_1_max_linear_child
= 3,
103 ja_type_2_max_linear_child
= 6,
104 ja_type_3_max_linear_child
= 12,
105 ja_type_4_max_linear_child
= 25,
106 ja_type_5_max_linear_child
= 24,
107 ja_type_6_max_linear_child
= 23,
111 ja_type_5_nr_pool_order
= 1,
112 ja_type_6_nr_pool_order
= 2,
115 const struct cds_ja_type ja_types
[] = {
116 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 3, },
117 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 4, },
118 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 5, },
119 { .type_class
= RCU_JA_LINEAR
, .min_child
= 4, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 6, },
120 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 7, },
122 /* Pools may fill sooner than max_child */
123 /* This pool is hardcoded at index 5. See ja_node_ptr(). */
124 { .type_class
= RCU_JA_POOL
, .min_child
= 20, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 8, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 7, },
125 /* This pool is hardcoded at index 6. See ja_node_ptr(). */
126 { .type_class
= RCU_JA_POOL
, .min_child
= 45, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 7, },
129 * Upon node removal below min_child, if child pool is filled
130 * beyond capacity, we roll back to pigeon.
132 { .type_class
= RCU_JA_PIGEON
, .min_child
= 83, .max_child
= ja_type_7_max_child
, .order
= 10, },
134 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
136 #else /* !(CAA_BITS_PER_LONG < 64) */
137 /* 64-bit pointers */
139 ja_type_0_max_child
= 1,
140 ja_type_1_max_child
= 3,
141 ja_type_2_max_child
= 7,
142 ja_type_3_max_child
= 14,
143 ja_type_4_max_child
= 28,
144 ja_type_5_max_child
= 54,
145 ja_type_6_max_child
= 104,
146 ja_type_7_max_child
= 256,
147 ja_type_8_max_child
= 256,
151 ja_type_0_max_linear_child
= 1,
152 ja_type_1_max_linear_child
= 3,
153 ja_type_2_max_linear_child
= 7,
154 ja_type_3_max_linear_child
= 14,
155 ja_type_4_max_linear_child
= 28,
156 ja_type_5_max_linear_child
= 27,
157 ja_type_6_max_linear_child
= 26,
161 ja_type_5_nr_pool_order
= 1,
162 ja_type_6_nr_pool_order
= 2,
165 const struct cds_ja_type ja_types
[] = {
166 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 4, },
167 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 5, },
168 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 6, },
169 { .type_class
= RCU_JA_LINEAR
, .min_child
= 5, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 7, },
170 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 8, },
172 /* Pools may fill sooner than max_child. */
173 /* This pool is hardcoded at index 5. See ja_node_ptr(). */
174 { .type_class
= RCU_JA_POOL
, .min_child
= 22, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 8, },
175 /* This pool is hardcoded at index 6. See ja_node_ptr(). */
176 { .type_class
= RCU_JA_POOL
, .min_child
= 51, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 10, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 8, },
179 * Upon node removal below min_child, if child pool is filled
180 * beyond capacity, we roll back to pigeon.
182 { .type_class
= RCU_JA_PIGEON
, .min_child
= 95, .max_child
= ja_type_7_max_child
, .order
= 11, },
184 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
186 #endif /* !(BITS_PER_LONG < 64) */
188 static inline __attribute__((unused
))
189 void static_array_size_check(void)
191 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) < JA_TYPE_MAX_NR
);
195 * The cds_ja_node contains the compressed node data needed for
196 * read-side. For linear and pool node configurations, it starts with a
197 * byte counting the number of children in the node. Then, the
198 * node-specific data is placed.
199 * The node mutex, if any is needed, protecting concurrent updated of
200 * each node is placed in a separate hash table indexed by node address.
201 * For the pigeon configuration, the number of children is also kept in
202 * a separate hash table, indexed by node address, because it is only
203 * required for updates.
206 #define DECLARE_LINEAR_NODE(index) \
209 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
210 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
213 #define DECLARE_POOL_NODE(index) \
217 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
218 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
219 } linear[1U << ja_type_## index ##_nr_pool_order]; \
222 struct cds_ja_inode
{
224 /* Linear configuration */
225 DECLARE_LINEAR_NODE(0) conf_0
;
226 DECLARE_LINEAR_NODE(1) conf_1
;
227 DECLARE_LINEAR_NODE(2) conf_2
;
228 DECLARE_LINEAR_NODE(3) conf_3
;
229 DECLARE_LINEAR_NODE(4) conf_4
;
231 /* Pool configuration */
232 DECLARE_POOL_NODE(5) conf_5
;
233 DECLARE_POOL_NODE(6) conf_6
;
235 /* Pigeon configuration */
237 struct cds_ja_inode_flag
*child
[ja_type_7_max_child
];
239 /* data aliasing nodes for computed accesses */
240 uint8_t data
[sizeof(struct cds_ja_inode_flag
*) * ja_type_7_max_child
];
245 JA_RECOMPACT_ADD_SAME
,
246 JA_RECOMPACT_ADD_NEXT
,
250 enum ja_lookup_inequality
{
263 struct cds_ja_inode
*_ja_node_mask_ptr(struct cds_ja_inode_flag
*node
)
265 return (struct cds_ja_inode
*) (((unsigned long) node
) & JA_PTR_MASK
);
268 unsigned long ja_node_type(struct cds_ja_inode_flag
*node
)
272 if (_ja_node_mask_ptr(node
) == NULL
) {
273 return NODE_INDEX_NULL
;
275 type
= (unsigned int) ((unsigned long) node
& JA_TYPE_MASK
);
276 assert(type
< (1UL << JA_TYPE_BITS
));
281 struct cds_ja_inode
*alloc_cds_ja_node(struct cds_ja
*ja
,
282 const struct cds_ja_type
*ja_type
)
284 size_t len
= 1U << ja_type
->order
;
288 ret
= posix_memalign(&p
, len
, len
);
293 uatomic_inc(&ja
->nr_nodes_allocated
);
297 void free_cds_ja_node(struct cds_ja
*ja
, struct cds_ja_inode
*node
)
301 uatomic_inc(&ja
->nr_nodes_freed
);
304 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
305 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
306 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
307 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
310 uint8_t *align_ptr_size(uint8_t *ptr
)
312 return (uint8_t *) JA_ALIGN((unsigned long) ptr
, sizeof(void *));
316 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type
*type
,
317 struct cds_ja_inode
*node
)
319 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
320 return rcu_dereference(node
->u
.data
[0]);
324 * The order in which values and pointers are does does not matter: if
325 * a value is missing, we return NULL. If a value is there, but its
326 * associated pointers is still NULL, we return NULL too.
329 struct cds_ja_inode_flag
*ja_linear_node_get_nth(const struct cds_ja_type
*type
,
330 struct cds_ja_inode
*node
,
331 struct cds_ja_inode_flag
***node_flag_ptr
,
336 struct cds_ja_inode_flag
**pointers
;
337 struct cds_ja_inode_flag
*ptr
;
340 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
342 nr_child
= ja_linear_node_get_nr_child(type
, node
);
343 cmm_smp_rmb(); /* read nr_child before values and pointers */
344 assert(nr_child
<= type
->max_linear_child
);
345 assert(type
->type_class
!= RCU_JA_LINEAR
|| nr_child
>= type
->min_child
);
347 values
= &node
->u
.data
[1];
348 for (i
= 0; i
< nr_child
; i
++) {
349 if (CMM_LOAD_SHARED(values
[i
]) == n
)
353 if (caa_unlikely(node_flag_ptr
))
354 *node_flag_ptr
= NULL
;
357 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
358 ptr
= rcu_dereference(pointers
[i
]);
359 if (caa_unlikely(node_flag_ptr
))
360 *node_flag_ptr
= &pointers
[i
];
365 struct cds_ja_inode_flag
*ja_linear_node_get_direction(const struct cds_ja_type
*type
,
366 struct cds_ja_inode
*node
,
367 int n
, uint8_t *result_key
,
368 enum ja_direction dir
)
372 struct cds_ja_inode_flag
**pointers
;
373 struct cds_ja_inode_flag
*ptr
= NULL
;
375 int match_idx
= -1, match_v
;
377 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
378 assert(dir
== JA_LEFT
|| dir
== JA_RIGHT
);
380 if (dir
== JA_LEFT
) {
383 match_v
= JA_ENTRY_PER_NODE
;
386 nr_child
= ja_linear_node_get_nr_child(type
, node
);
387 cmm_smp_rmb(); /* read nr_child before values and pointers */
388 assert(nr_child
<= type
->max_linear_child
);
389 assert(type
->type_class
!= RCU_JA_LINEAR
|| nr_child
>= type
->min_child
);
391 values
= &node
->u
.data
[1];
392 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
393 for (i
= 0; i
< nr_child
; i
++) {
396 v
= CMM_LOAD_SHARED(values
[i
]);
397 ptr
= CMM_LOAD_SHARED(pointers
[i
]);
400 if (dir
== JA_LEFT
) {
401 if ((int) v
< n
&& (int) v
> match_v
) {
406 if ((int) v
> n
&& (int) v
< match_v
) {
416 assert(match_v
>= 0 && match_v
< JA_ENTRY_PER_NODE
);
418 *result_key
= (uint8_t) match_v
;
419 ptr
= rcu_dereference(pointers
[match_idx
]);
424 void ja_linear_node_get_ith_pos(const struct cds_ja_type
*type
,
425 struct cds_ja_inode
*node
,
428 struct cds_ja_inode_flag
**iter
)
431 struct cds_ja_inode_flag
**pointers
;
433 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
434 assert(i
< ja_linear_node_get_nr_child(type
, node
));
436 values
= &node
->u
.data
[1];
438 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
443 struct cds_ja_inode_flag
*ja_pool_node_get_nth(const struct cds_ja_type
*type
,
444 struct cds_ja_inode
*node
,
445 struct cds_ja_inode_flag
*node_flag
,
446 struct cds_ja_inode_flag
***node_flag_ptr
,
449 struct cds_ja_inode
*linear
;
451 assert(type
->type_class
== RCU_JA_POOL
);
453 switch (type
->nr_pool_order
) {
456 unsigned long bitsel
, index
;
458 bitsel
= ja_node_pool_1d_bitsel(node_flag
);
459 assert(bitsel
< CHAR_BIT
);
460 index
= ((unsigned long) n
>> bitsel
) & 0x1;
461 linear
= (struct cds_ja_inode
*) &node
->u
.data
[index
<< type
->pool_size_order
];
466 unsigned long bitsel
[2], index
[2], rindex
;
468 ja_node_pool_2d_bitsel(node_flag
, bitsel
);
469 assert(bitsel
[0] < CHAR_BIT
);
470 assert(bitsel
[1] < CHAR_BIT
);
471 index
[0] = ((unsigned long) n
>> bitsel
[0]) & 0x1;
473 index
[1] = ((unsigned long) n
>> bitsel
[1]) & 0x1;
474 rindex
= index
[0] | index
[1];
475 linear
= (struct cds_ja_inode
*) &node
->u
.data
[rindex
<< type
->pool_size_order
];
482 return ja_linear_node_get_nth(type
, linear
, node_flag_ptr
, n
);
486 struct cds_ja_inode
*ja_pool_node_get_ith_pool(const struct cds_ja_type
*type
,
487 struct cds_ja_inode
*node
,
490 assert(type
->type_class
== RCU_JA_POOL
);
491 return (struct cds_ja_inode
*)
492 &node
->u
.data
[(unsigned int) i
<< type
->pool_size_order
];
496 struct cds_ja_inode_flag
*ja_pool_node_get_direction(const struct cds_ja_type
*type
,
497 struct cds_ja_inode
*node
,
498 int n
, uint8_t *result_key
,
499 enum ja_direction dir
)
501 unsigned int pool_nr
;
503 struct cds_ja_inode_flag
*match_node_flag
= NULL
;
505 assert(type
->type_class
== RCU_JA_POOL
);
506 assert(dir
== JA_LEFT
|| dir
== JA_RIGHT
);
508 if (dir
== JA_LEFT
) {
511 match_v
= JA_ENTRY_PER_NODE
;
514 for (pool_nr
= 0; pool_nr
< (1U << type
->nr_pool_order
); pool_nr
++) {
515 struct cds_ja_inode
*pool
=
516 ja_pool_node_get_ith_pool(type
,
519 ja_linear_node_get_nr_child(type
, pool
);
522 for (j
= 0; j
< nr_child
; j
++) {
523 struct cds_ja_inode_flag
*iter
;
526 ja_linear_node_get_ith_pos(type
, pool
,
530 if (dir
== JA_LEFT
) {
531 if ((int) v
< n
&& (int) v
> match_v
) {
533 match_node_flag
= iter
;
536 if ((int) v
> n
&& (int) v
< match_v
) {
538 match_node_flag
= iter
;
544 *result_key
= (uint8_t) match_v
;
545 return match_node_flag
;
549 struct cds_ja_inode_flag
*ja_pigeon_node_get_nth(const struct cds_ja_type
*type
,
550 struct cds_ja_inode
*node
,
551 struct cds_ja_inode_flag
***node_flag_ptr
,
554 struct cds_ja_inode_flag
**child_node_flag_ptr
;
555 struct cds_ja_inode_flag
*child_node_flag
;
557 assert(type
->type_class
== RCU_JA_PIGEON
);
558 child_node_flag_ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
559 child_node_flag
= rcu_dereference(*child_node_flag_ptr
);
560 dbg_printf("ja_pigeon_node_get_nth child_node_flag_ptr %p\n",
561 child_node_flag_ptr
);
562 if (caa_unlikely(node_flag_ptr
))
563 *node_flag_ptr
= child_node_flag_ptr
;
564 return child_node_flag
;
568 struct cds_ja_inode_flag
*ja_pigeon_node_get_direction(const struct cds_ja_type
*type
,
569 struct cds_ja_inode
*node
,
570 int n
, uint8_t *result_key
,
571 enum ja_direction dir
)
573 struct cds_ja_inode_flag
**child_node_flag_ptr
;
574 struct cds_ja_inode_flag
*child_node_flag
;
577 assert(type
->type_class
== RCU_JA_PIGEON
);
578 assert(dir
== JA_LEFT
|| dir
== JA_RIGHT
);
580 if (dir
== JA_LEFT
) {
581 /* n - 1 is first value left of n */
582 for (i
= n
- 1; i
>= 0; i
--) {
583 child_node_flag_ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[i
];
584 child_node_flag
= rcu_dereference(*child_node_flag_ptr
);
585 if (child_node_flag
) {
586 dbg_printf("ja_pigeon_node_get_left child_node_flag %p\n",
588 *result_key
= (uint8_t) i
;
589 return child_node_flag
;
593 /* n + 1 is first value right of n */
594 for (i
= n
+ 1; i
< JA_ENTRY_PER_NODE
; i
++) {
595 child_node_flag_ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[i
];
596 child_node_flag
= rcu_dereference(*child_node_flag_ptr
);
597 if (child_node_flag
) {
598 dbg_printf("ja_pigeon_node_get_right child_node_flag %p\n",
600 *result_key
= (uint8_t) i
;
601 return child_node_flag
;
609 struct cds_ja_inode_flag
*ja_pigeon_node_get_ith_pos(const struct cds_ja_type
*type
,
610 struct cds_ja_inode
*node
,
613 return ja_pigeon_node_get_nth(type
, node
, NULL
, i
);
617 * ja_node_get_nth: get nth item from a node.
618 * node_flag is already rcu_dereference'd.
621 struct cds_ja_inode_flag
*ja_node_get_nth(struct cds_ja_inode_flag
*node_flag
,
622 struct cds_ja_inode_flag
***node_flag_ptr
,
625 unsigned int type_index
;
626 struct cds_ja_inode
*node
;
627 const struct cds_ja_type
*type
;
629 node
= ja_node_ptr(node_flag
);
630 assert(node
!= NULL
);
631 type_index
= ja_node_type(node_flag
);
632 type
= &ja_types
[type_index
];
634 switch (type
->type_class
) {
636 return ja_linear_node_get_nth(type
, node
,
639 return ja_pool_node_get_nth(type
, node
, node_flag
,
642 return ja_pigeon_node_get_nth(type
, node
,
646 return (void *) -1UL;
651 struct cds_ja_inode_flag
*ja_node_get_direction(struct cds_ja_inode_flag
*node_flag
,
652 int n
, uint8_t *result_key
,
653 enum ja_direction dir
)
655 unsigned int type_index
;
656 struct cds_ja_inode
*node
;
657 const struct cds_ja_type
*type
;
659 node
= ja_node_ptr(node_flag
);
660 assert(node
!= NULL
);
661 type_index
= ja_node_type(node_flag
);
662 type
= &ja_types
[type_index
];
664 switch (type
->type_class
) {
666 return ja_linear_node_get_direction(type
, node
, n
, result_key
, dir
);
668 return ja_pool_node_get_direction(type
, node
, n
, result_key
, dir
);
670 return ja_pigeon_node_get_direction(type
, node
, n
, result_key
, dir
);
673 return (void *) -1UL;
678 struct cds_ja_inode_flag
*ja_node_get_leftright(struct cds_ja_inode_flag
*node_flag
,
679 unsigned int n
, uint8_t *result_key
,
680 enum ja_direction dir
)
682 return ja_node_get_direction(node_flag
, n
, result_key
, dir
);
686 struct cds_ja_inode_flag
*ja_node_get_minmax(struct cds_ja_inode_flag
*node_flag
,
688 enum ja_direction dir
)
692 return ja_node_get_direction(node_flag
,
693 -1, result_key
, JA_RIGHT
);
695 return ja_node_get_direction(node_flag
,
696 JA_ENTRY_PER_NODE
, result_key
, JA_LEFT
);
703 int ja_linear_node_set_nth(const struct cds_ja_type
*type
,
704 struct cds_ja_inode
*node
,
705 struct cds_ja_shadow_node
*shadow_node
,
707 struct cds_ja_inode_flag
*child_node_flag
)
710 uint8_t *values
, *nr_child_ptr
;
711 struct cds_ja_inode_flag
**pointers
;
712 unsigned int i
, unused
= 0;
714 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
716 nr_child_ptr
= &node
->u
.data
[0];
717 dbg_printf("linear set nth: n %u, nr_child_ptr %p\n",
718 (unsigned int) n
, nr_child_ptr
);
719 nr_child
= *nr_child_ptr
;
720 assert(nr_child
<= type
->max_linear_child
);
722 values
= &node
->u
.data
[1];
723 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
724 /* Check if node value is already populated */
725 for (i
= 0; i
< nr_child
; i
++) {
726 if (values
[i
] == n
) {
736 if (i
== nr_child
&& nr_child
>= type
->max_linear_child
) {
738 return -ERANGE
; /* recompact node */
740 return -ENOSPC
; /* No space left in this node type */
743 assert(pointers
[i
] == NULL
);
744 rcu_assign_pointer(pointers
[i
], child_node_flag
);
745 /* If we expanded the nr_child, increment it */
747 CMM_STORE_SHARED(values
[nr_child
], n
);
748 /* write pointer and value before nr_child */
750 CMM_STORE_SHARED(*nr_child_ptr
, nr_child
+ 1);
752 shadow_node
->nr_child
++;
753 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
754 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
755 (unsigned int) shadow_node
->nr_child
,
762 int ja_pool_node_set_nth(const struct cds_ja_type
*type
,
763 struct cds_ja_inode
*node
,
764 struct cds_ja_inode_flag
*node_flag
,
765 struct cds_ja_shadow_node
*shadow_node
,
767 struct cds_ja_inode_flag
*child_node_flag
)
769 struct cds_ja_inode
*linear
;
771 assert(type
->type_class
== RCU_JA_POOL
);
773 switch (type
->nr_pool_order
) {
776 unsigned long bitsel
, index
;
778 bitsel
= ja_node_pool_1d_bitsel(node_flag
);
779 assert(bitsel
< CHAR_BIT
);
780 index
= ((unsigned long) n
>> bitsel
) & 0x1;
781 linear
= (struct cds_ja_inode
*) &node
->u
.data
[index
<< type
->pool_size_order
];
786 unsigned long bitsel
[2], index
[2], rindex
;
788 ja_node_pool_2d_bitsel(node_flag
, bitsel
);
789 assert(bitsel
[0] < CHAR_BIT
);
790 assert(bitsel
[1] < CHAR_BIT
);
791 index
[0] = ((unsigned long) n
>> bitsel
[0]) & 0x1;
793 index
[1] = ((unsigned long) n
>> bitsel
[1]) & 0x1;
794 rindex
= index
[0] | index
[1];
795 linear
= (struct cds_ja_inode
*) &node
->u
.data
[rindex
<< type
->pool_size_order
];
803 return ja_linear_node_set_nth(type
, linear
, shadow_node
,
808 int ja_pigeon_node_set_nth(const struct cds_ja_type
*type
,
809 struct cds_ja_inode
*node
,
810 struct cds_ja_shadow_node
*shadow_node
,
812 struct cds_ja_inode_flag
*child_node_flag
)
814 struct cds_ja_inode_flag
**ptr
;
816 assert(type
->type_class
== RCU_JA_PIGEON
);
817 ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
820 rcu_assign_pointer(*ptr
, child_node_flag
);
821 shadow_node
->nr_child
++;
826 * _ja_node_set_nth: set nth item within a node. Return an error
827 * (negative error value) if it is already there.
830 int _ja_node_set_nth(const struct cds_ja_type
*type
,
831 struct cds_ja_inode
*node
,
832 struct cds_ja_inode_flag
*node_flag
,
833 struct cds_ja_shadow_node
*shadow_node
,
835 struct cds_ja_inode_flag
*child_node_flag
)
837 switch (type
->type_class
) {
839 return ja_linear_node_set_nth(type
, node
, shadow_node
, n
,
842 return ja_pool_node_set_nth(type
, node
, node_flag
, shadow_node
, n
,
845 return ja_pigeon_node_set_nth(type
, node
, shadow_node
, n
,
858 int ja_linear_node_clear_ptr(const struct cds_ja_type
*type
,
859 struct cds_ja_inode
*node
,
860 struct cds_ja_shadow_node
*shadow_node
,
861 struct cds_ja_inode_flag
**node_flag_ptr
)
864 uint8_t *nr_child_ptr
;
866 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
868 nr_child_ptr
= &node
->u
.data
[0];
869 nr_child
= *nr_child_ptr
;
870 assert(nr_child
<= type
->max_linear_child
);
872 if (type
->type_class
== RCU_JA_LINEAR
) {
873 assert(!shadow_node
->fallback_removal_count
);
874 if (shadow_node
->nr_child
<= type
->min_child
) {
875 /* We need to try recompacting the node */
879 dbg_printf("linear clear ptr: nr_child_ptr %p\n", nr_child_ptr
);
880 assert(*node_flag_ptr
!= NULL
);
881 rcu_assign_pointer(*node_flag_ptr
, NULL
);
883 * Value and nr_child are never changed (would cause ABA issue).
884 * Instead, we leave the pointer to NULL and recompact the node
885 * once in a while. It is allowed to set a NULL pointer to a new
886 * value without recompaction though.
887 * Only update the shadow node accounting.
889 shadow_node
->nr_child
--;
890 dbg_printf("linear clear ptr: %u child, shadow: %u child, for node %p shadow %p\n",
891 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
892 (unsigned int) shadow_node
->nr_child
,
898 int ja_pool_node_clear_ptr(const struct cds_ja_type
*type
,
899 struct cds_ja_inode
*node
,
900 struct cds_ja_inode_flag
*node_flag
,
901 struct cds_ja_shadow_node
*shadow_node
,
902 struct cds_ja_inode_flag
**node_flag_ptr
,
905 struct cds_ja_inode
*linear
;
907 assert(type
->type_class
== RCU_JA_POOL
);
909 if (shadow_node
->fallback_removal_count
) {
910 shadow_node
->fallback_removal_count
--;
912 /* We should try recompacting the node */
913 if (shadow_node
->nr_child
<= type
->min_child
)
917 switch (type
->nr_pool_order
) {
920 unsigned long bitsel
, index
;
922 bitsel
= ja_node_pool_1d_bitsel(node_flag
);
923 assert(bitsel
< CHAR_BIT
);
924 index
= ((unsigned long) n
>> bitsel
) & type
->nr_pool_order
;
925 linear
= (struct cds_ja_inode
*) &node
->u
.data
[index
<< type
->pool_size_order
];
930 unsigned long bitsel
[2], index
[2], rindex
;
932 ja_node_pool_2d_bitsel(node_flag
, bitsel
);
933 assert(bitsel
[0] < CHAR_BIT
);
934 assert(bitsel
[1] < CHAR_BIT
);
935 index
[0] = ((unsigned long) n
>> bitsel
[0]) & 0x1;
937 index
[1] = ((unsigned long) n
>> bitsel
[1]) & 0x1;
938 rindex
= index
[0] | index
[1];
939 linear
= (struct cds_ja_inode
*) &node
->u
.data
[rindex
<< type
->pool_size_order
];
947 return ja_linear_node_clear_ptr(type
, linear
, shadow_node
, node_flag_ptr
);
951 int ja_pigeon_node_clear_ptr(const struct cds_ja_type
*type
,
952 struct cds_ja_inode
*node
,
953 struct cds_ja_shadow_node
*shadow_node
,
954 struct cds_ja_inode_flag
**node_flag_ptr
)
956 assert(type
->type_class
== RCU_JA_PIGEON
);
958 if (shadow_node
->fallback_removal_count
) {
959 shadow_node
->fallback_removal_count
--;
961 /* We should try recompacting the node */
962 if (shadow_node
->nr_child
<= type
->min_child
)
965 dbg_printf("ja_pigeon_node_clear_ptr: clearing ptr: %p\n", *node_flag_ptr
);
966 rcu_assign_pointer(*node_flag_ptr
, NULL
);
967 shadow_node
->nr_child
--;
972 * _ja_node_clear_ptr: clear ptr item within a node. Return an error
973 * (negative error value) if it is not found (-ENOENT).
976 int _ja_node_clear_ptr(const struct cds_ja_type
*type
,
977 struct cds_ja_inode
*node
,
978 struct cds_ja_inode_flag
*node_flag
,
979 struct cds_ja_shadow_node
*shadow_node
,
980 struct cds_ja_inode_flag
**node_flag_ptr
,
983 switch (type
->type_class
) {
985 return ja_linear_node_clear_ptr(type
, node
, shadow_node
, node_flag_ptr
);
987 return ja_pool_node_clear_ptr(type
, node
, node_flag
, shadow_node
, node_flag_ptr
, n
);
989 return ja_pigeon_node_clear_ptr(type
, node
, shadow_node
, node_flag_ptr
);
1001 * Calculate bit distribution. Returns the bit (0 to 7) that splits the
1002 * distribution in two sub-distributions containing as much elements one
1003 * compared to the other.
1006 unsigned int ja_node_sum_distribution_1d(enum ja_recompact mode
,
1008 unsigned int type_index
,
1009 const struct cds_ja_type
*type
,
1010 struct cds_ja_inode
*node
,
1011 struct cds_ja_shadow_node
*shadow_node
,
1013 struct cds_ja_inode_flag
*child_node_flag
,
1014 struct cds_ja_inode_flag
**nullify_node_flag_ptr
)
1016 uint8_t nr_one
[JA_BITS_PER_BYTE
];
1017 unsigned int bitsel
= 0, bit_i
, overall_best_distance
= UINT_MAX
;
1018 unsigned int distrib_nr_child
= 0;
1020 memset(nr_one
, 0, sizeof(nr_one
));
1022 switch (type
->type_class
) {
1026 ja_linear_node_get_nr_child(type
, node
);
1029 for (i
= 0; i
< nr_child
; i
++) {
1030 struct cds_ja_inode_flag
*iter
;
1033 ja_linear_node_get_ith_pos(type
, node
, i
, &v
, &iter
);
1036 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1038 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1039 if (v
& (1U << bit_i
))
1048 unsigned int pool_nr
;
1050 for (pool_nr
= 0; pool_nr
< (1U << type
->nr_pool_order
); pool_nr
++) {
1051 struct cds_ja_inode
*pool
=
1052 ja_pool_node_get_ith_pool(type
,
1055 ja_linear_node_get_nr_child(type
, pool
);
1058 for (j
= 0; j
< nr_child
; j
++) {
1059 struct cds_ja_inode_flag
*iter
;
1062 ja_linear_node_get_ith_pos(type
, pool
,
1066 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1068 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1069 if (v
& (1U << bit_i
))
1081 assert(mode
== JA_RECOMPACT_DEL
);
1082 for (i
= 0; i
< JA_ENTRY_PER_NODE
; i
++) {
1083 struct cds_ja_inode_flag
*iter
;
1085 iter
= ja_pigeon_node_get_ith_pos(type
, node
, i
);
1088 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1090 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1091 if (i
& (1U << bit_i
))
1099 assert(mode
== JA_RECOMPACT_ADD_NEXT
);
1106 if (mode
== JA_RECOMPACT_ADD_NEXT
|| mode
== JA_RECOMPACT_ADD_SAME
) {
1107 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1108 if (n
& (1U << bit_i
))
1115 * The best bit selector is that for which the number of ones is
1116 * closest to half of the number of children in the
1117 * distribution. We calculate the distance using the double of
1118 * the sub-distribution sizes to eliminate truncation error.
1120 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1121 unsigned int distance_to_best
;
1123 distance_to_best
= abs_int(((unsigned int) nr_one
[bit_i
] << 1U) - distrib_nr_child
);
1124 if (distance_to_best
< overall_best_distance
) {
1125 overall_best_distance
= distance_to_best
;
1129 dbg_printf("1 dimension pool bit selection: (%u)\n", bitsel
);
1134 * Calculate bit distribution in two dimensions. Returns the two bits
1135 * (each 0 to 7) that splits the distribution in four sub-distributions
1136 * containing as much elements one compared to the other.
1139 void ja_node_sum_distribution_2d(enum ja_recompact mode
,
1141 unsigned int type_index
,
1142 const struct cds_ja_type
*type
,
1143 struct cds_ja_inode
*node
,
1144 struct cds_ja_shadow_node
*shadow_node
,
1146 struct cds_ja_inode_flag
*child_node_flag
,
1147 struct cds_ja_inode_flag
**nullify_node_flag_ptr
,
1148 unsigned int *_bitsel
)
1150 uint8_t nr_2d_11
[JA_BITS_PER_BYTE
][JA_BITS_PER_BYTE
],
1151 nr_2d_10
[JA_BITS_PER_BYTE
][JA_BITS_PER_BYTE
],
1152 nr_2d_01
[JA_BITS_PER_BYTE
][JA_BITS_PER_BYTE
],
1153 nr_2d_00
[JA_BITS_PER_BYTE
][JA_BITS_PER_BYTE
];
1154 unsigned int bitsel
[2] = { 0, 1 };
1155 unsigned int bit_i
, bit_j
;
1156 int overall_best_distance
= INT_MAX
;
1157 unsigned int distrib_nr_child
= 0;
1159 memset(nr_2d_11
, 0, sizeof(nr_2d_11
));
1160 memset(nr_2d_10
, 0, sizeof(nr_2d_10
));
1161 memset(nr_2d_01
, 0, sizeof(nr_2d_01
));
1162 memset(nr_2d_00
, 0, sizeof(nr_2d_00
));
1164 switch (type
->type_class
) {
1168 ja_linear_node_get_nr_child(type
, node
);
1171 for (i
= 0; i
< nr_child
; i
++) {
1172 struct cds_ja_inode_flag
*iter
;
1175 ja_linear_node_get_ith_pos(type
, node
, i
, &v
, &iter
);
1178 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1180 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1181 for (bit_j
= 0; bit_j
< bit_i
; bit_j
++) {
1182 if (v
& (1U << bit_i
)) {
1183 if (v
& (1U << bit_j
)) {
1184 nr_2d_11
[bit_i
][bit_j
]++;
1186 nr_2d_10
[bit_i
][bit_j
]++;
1189 if (v
& (1U << bit_j
)) {
1190 nr_2d_01
[bit_i
][bit_j
]++;
1192 nr_2d_00
[bit_i
][bit_j
]++;
1203 unsigned int pool_nr
;
1205 for (pool_nr
= 0; pool_nr
< (1U << type
->nr_pool_order
); pool_nr
++) {
1206 struct cds_ja_inode
*pool
=
1207 ja_pool_node_get_ith_pool(type
,
1210 ja_linear_node_get_nr_child(type
, pool
);
1213 for (j
= 0; j
< nr_child
; j
++) {
1214 struct cds_ja_inode_flag
*iter
;
1217 ja_linear_node_get_ith_pos(type
, pool
,
1221 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1223 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1224 for (bit_j
= 0; bit_j
< bit_i
; bit_j
++) {
1225 if (v
& (1U << bit_i
)) {
1226 if (v
& (1U << bit_j
)) {
1227 nr_2d_11
[bit_i
][bit_j
]++;
1229 nr_2d_10
[bit_i
][bit_j
]++;
1232 if (v
& (1U << bit_j
)) {
1233 nr_2d_01
[bit_i
][bit_j
]++;
1235 nr_2d_00
[bit_i
][bit_j
]++;
1249 assert(mode
== JA_RECOMPACT_DEL
);
1250 for (i
= 0; i
< JA_ENTRY_PER_NODE
; i
++) {
1251 struct cds_ja_inode_flag
*iter
;
1253 iter
= ja_pigeon_node_get_ith_pos(type
, node
, i
);
1256 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1258 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1259 for (bit_j
= 0; bit_j
< bit_i
; bit_j
++) {
1260 if (i
& (1U << bit_i
)) {
1261 if (i
& (1U << bit_j
)) {
1262 nr_2d_11
[bit_i
][bit_j
]++;
1264 nr_2d_10
[bit_i
][bit_j
]++;
1267 if (i
& (1U << bit_j
)) {
1268 nr_2d_01
[bit_i
][bit_j
]++;
1270 nr_2d_00
[bit_i
][bit_j
]++;
1280 assert(mode
== JA_RECOMPACT_ADD_NEXT
);
1287 if (mode
== JA_RECOMPACT_ADD_NEXT
|| mode
== JA_RECOMPACT_ADD_SAME
) {
1288 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1289 for (bit_j
= 0; bit_j
< bit_i
; bit_j
++) {
1290 if (n
& (1U << bit_i
)) {
1291 if (n
& (1U << bit_j
)) {
1292 nr_2d_11
[bit_i
][bit_j
]++;
1294 nr_2d_10
[bit_i
][bit_j
]++;
1297 if (n
& (1U << bit_j
)) {
1298 nr_2d_01
[bit_i
][bit_j
]++;
1300 nr_2d_00
[bit_i
][bit_j
]++;
1309 * The best bit selector is that for which the number of nodes
1310 * in each sub-class is closest to one-fourth of the number of
1311 * children in the distribution. We calculate the distance using
1312 * 4 times the size of the sub-distribution to eliminate
1315 for (bit_i
= 0; bit_i
< JA_BITS_PER_BYTE
; bit_i
++) {
1316 for (bit_j
= 0; bit_j
< bit_i
; bit_j
++) {
1317 int distance_to_best
[4];
1319 distance_to_best
[0] = ((unsigned int) nr_2d_11
[bit_i
][bit_j
] << 2U) - distrib_nr_child
;
1320 distance_to_best
[1] = ((unsigned int) nr_2d_10
[bit_i
][bit_j
] << 2U) - distrib_nr_child
;
1321 distance_to_best
[2] = ((unsigned int) nr_2d_01
[bit_i
][bit_j
] << 2U) - distrib_nr_child
;
1322 distance_to_best
[3] = ((unsigned int) nr_2d_00
[bit_i
][bit_j
] << 2U) - distrib_nr_child
;
1324 /* Consider worse distance above best */
1325 if (distance_to_best
[1] > 0 && distance_to_best
[1] > distance_to_best
[0])
1326 distance_to_best
[0] = distance_to_best
[1];
1327 if (distance_to_best
[2] > 0 && distance_to_best
[2] > distance_to_best
[0])
1328 distance_to_best
[0] = distance_to_best
[2];
1329 if (distance_to_best
[3] > 0 && distance_to_best
[3] > distance_to_best
[0])
1330 distance_to_best
[0] = distance_to_best
[3];
1333 * If our worse distance is better than overall,
1334 * we become new best candidate.
1336 if (distance_to_best
[0] < overall_best_distance
) {
1337 overall_best_distance
= distance_to_best
[0];
1344 dbg_printf("2 dimensions pool bit selection: (%u,%u)\n", bitsel
[0], bitsel
[1]);
1346 /* Return our bit selection */
1347 _bitsel
[0] = bitsel
[0];
1348 _bitsel
[1] = bitsel
[1];
1352 unsigned int find_nearest_type_index(unsigned int type_index
,
1353 unsigned int nr_nodes
)
1355 const struct cds_ja_type
*type
;
1357 assert(type_index
!= NODE_INDEX_NULL
);
1359 return NODE_INDEX_NULL
;
1361 type
= &ja_types
[type_index
];
1362 if (nr_nodes
< type
->min_child
)
1364 else if (nr_nodes
> type
->max_child
)
1373 * ja_node_recompact_add: recompact a node, adding a new child.
1374 * Return 0 on success, -EAGAIN if need to retry, or other negative
1375 * error value otherwise.
1378 int ja_node_recompact(enum ja_recompact mode
,
1380 unsigned int old_type_index
,
1381 const struct cds_ja_type
*old_type
,
1382 struct cds_ja_inode
*old_node
,
1383 struct cds_ja_shadow_node
*shadow_node
,
1384 struct cds_ja_inode_flag
**old_node_flag_ptr
, uint8_t n
,
1385 struct cds_ja_inode_flag
*child_node_flag
,
1386 struct cds_ja_inode_flag
**nullify_node_flag_ptr
,
1389 unsigned int new_type_index
;
1390 struct cds_ja_inode
*new_node
;
1391 struct cds_ja_shadow_node
*new_shadow_node
= NULL
;
1392 const struct cds_ja_type
*new_type
;
1393 struct cds_ja_inode_flag
*new_node_flag
, *old_node_flag
;
1397 old_node_flag
= *old_node_flag_ptr
;
1400 * Need to find nearest type index even for ADD_SAME, because
1401 * this recompaction, when applied to linear nodes, will garbage
1402 * collect dummy (NULL) entries, and can therefore cause a few
1403 * linear representations to be skipped.
1406 case JA_RECOMPACT_ADD_SAME
:
1407 new_type_index
= find_nearest_type_index(old_type_index
,
1408 shadow_node
->nr_child
+ 1);
1409 dbg_printf("Recompact for node with %u children\n",
1410 shadow_node
->nr_child
+ 1);
1412 case JA_RECOMPACT_ADD_NEXT
:
1413 if (!shadow_node
|| old_type_index
== NODE_INDEX_NULL
) {
1415 dbg_printf("Recompact for NULL\n");
1417 new_type_index
= find_nearest_type_index(old_type_index
,
1418 shadow_node
->nr_child
+ 1);
1419 dbg_printf("Recompact for node with %u children\n",
1420 shadow_node
->nr_child
+ 1);
1423 case JA_RECOMPACT_DEL
:
1424 new_type_index
= find_nearest_type_index(old_type_index
,
1425 shadow_node
->nr_child
- 1);
1426 dbg_printf("Recompact for node with %u children\n",
1427 shadow_node
->nr_child
- 1);
1433 retry
: /* for fallback */
1434 dbg_printf("Recompact from type %d to type %d\n",
1435 old_type_index
, new_type_index
);
1436 new_type
= &ja_types
[new_type_index
];
1437 if (new_type_index
!= NODE_INDEX_NULL
) {
1438 new_node
= alloc_cds_ja_node(ja
, new_type
);
1442 if (new_type
->type_class
== RCU_JA_POOL
) {
1443 switch (new_type
->nr_pool_order
) {
1446 unsigned int node_distrib_bitsel
;
1448 node_distrib_bitsel
=
1449 ja_node_sum_distribution_1d(mode
, ja
,
1450 old_type_index
, old_type
,
1451 old_node
, shadow_node
,
1453 nullify_node_flag_ptr
);
1454 assert(!((unsigned long) new_node
& JA_POOL_1D_MASK
));
1455 new_node_flag
= ja_node_flag_pool_1d(new_node
,
1456 new_type_index
, node_distrib_bitsel
);
1461 unsigned int node_distrib_bitsel
[2];
1463 ja_node_sum_distribution_2d(mode
, ja
,
1464 old_type_index
, old_type
,
1465 old_node
, shadow_node
,
1467 nullify_node_flag_ptr
,
1468 node_distrib_bitsel
);
1469 assert(!((unsigned long) new_node
& JA_POOL_1D_MASK
));
1470 assert(!((unsigned long) new_node
& JA_POOL_2D_MASK
));
1471 new_node_flag
= ja_node_flag_pool_2d(new_node
,
1472 new_type_index
, node_distrib_bitsel
);
1479 new_node_flag
= ja_node_flag(new_node
, new_type_index
);
1482 dbg_printf("Recompact inherit lock from %p\n", shadow_node
);
1483 new_shadow_node
= rcuja_shadow_set(ja
->ht
, new_node_flag
, shadow_node
, ja
, level
);
1484 if (!new_shadow_node
) {
1485 free_cds_ja_node(ja
, new_node
);
1489 new_shadow_node
->fallback_removal_count
=
1490 JA_FALLBACK_REMOVAL_COUNT
;
1493 new_node_flag
= NULL
;
1496 assert(mode
!= JA_RECOMPACT_ADD_NEXT
|| old_type
->type_class
!= RCU_JA_PIGEON
);
1498 if (new_type_index
== NODE_INDEX_NULL
)
1501 switch (old_type
->type_class
) {
1505 ja_linear_node_get_nr_child(old_type
, old_node
);
1508 for (i
= 0; i
< nr_child
; i
++) {
1509 struct cds_ja_inode_flag
*iter
;
1512 ja_linear_node_get_ith_pos(old_type
, old_node
, i
, &v
, &iter
);
1515 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1517 ret
= _ja_node_set_nth(new_type
, new_node
, new_node_flag
,
1520 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
1521 goto fallback_toosmall
;
1529 unsigned int pool_nr
;
1531 for (pool_nr
= 0; pool_nr
< (1U << old_type
->nr_pool_order
); pool_nr
++) {
1532 struct cds_ja_inode
*pool
=
1533 ja_pool_node_get_ith_pool(old_type
,
1536 ja_linear_node_get_nr_child(old_type
, pool
);
1539 for (j
= 0; j
< nr_child
; j
++) {
1540 struct cds_ja_inode_flag
*iter
;
1543 ja_linear_node_get_ith_pos(old_type
, pool
,
1547 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1549 ret
= _ja_node_set_nth(new_type
, new_node
, new_node_flag
,
1552 if (new_type
->type_class
== RCU_JA_POOL
1554 goto fallback_toosmall
;
1562 assert(mode
== JA_RECOMPACT_ADD_NEXT
);
1568 assert(mode
== JA_RECOMPACT_DEL
);
1569 for (i
= 0; i
< JA_ENTRY_PER_NODE
; i
++) {
1570 struct cds_ja_inode_flag
*iter
;
1572 iter
= ja_pigeon_node_get_ith_pos(old_type
, old_node
, i
);
1575 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
1577 ret
= _ja_node_set_nth(new_type
, new_node
, new_node_flag
,
1580 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
1581 goto fallback_toosmall
;
1594 if (mode
== JA_RECOMPACT_ADD_NEXT
|| mode
== JA_RECOMPACT_ADD_SAME
) {
1596 ret
= _ja_node_set_nth(new_type
, new_node
, new_node_flag
,
1598 n
, child_node_flag
);
1599 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
1600 goto fallback_toosmall
;
1606 dbg_printf("Using fallback for %u children, node type index: %u, mode %s\n",
1607 new_shadow_node
->nr_child
, old_type_index
, mode
== JA_RECOMPACT_ADD_NEXT
? "add_next" :
1608 (mode
== JA_RECOMPACT_DEL
? "del" : "add_same"));
1609 uatomic_inc(&ja
->node_fallback_count_distribution
[new_shadow_node
->nr_child
]);
1612 /* Return pointer to new recompacted node through old_node_flag_ptr */
1613 *old_node_flag_ptr
= new_node_flag
;
1617 flags
= RCUJA_SHADOW_CLEAR_FREE_NODE
;
1619 * It is OK to free the lock associated with a node
1620 * going to NULL, since we are holding the parent lock.
1621 * This synchronizes removal with re-add of that node.
1623 if (new_type_index
== NODE_INDEX_NULL
)
1624 flags
|= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
1625 ret
= rcuja_shadow_clear(ja
->ht
, old_node_flag
, shadow_node
,
1635 /* fallback if next pool is too small */
1636 assert(new_shadow_node
);
1637 ret
= rcuja_shadow_clear(ja
->ht
, new_node_flag
, new_shadow_node
,
1638 RCUJA_SHADOW_CLEAR_FREE_NODE
);
1642 case JA_RECOMPACT_ADD_SAME
:
1644 * JA_RECOMPACT_ADD_SAME is only triggered if a linear
1645 * node within a pool has unused entries. It should
1646 * therefore _never_ be too small.
1651 case JA_RECOMPACT_ADD_NEXT
:
1653 const struct cds_ja_type
*next_type
;
1656 * Recompaction attempt on add failed. Should only
1657 * happen if target node type is pool. Caused by
1658 * hard-to-split distribution. Recompact using the next
1659 * distribution size.
1661 assert(new_type
->type_class
== RCU_JA_POOL
);
1662 next_type
= &ja_types
[new_type_index
+ 1];
1664 * Try going to the next pool size if our population
1665 * fits within its range. This is not flagged as a
1668 if (shadow_node
->nr_child
+ 1 >= next_type
->min_child
1669 && shadow_node
->nr_child
+ 1 <= next_type
->max_child
) {
1674 dbg_printf("Add fallback to type %d\n", new_type_index
);
1675 uatomic_inc(&ja
->nr_fallback
);
1681 case JA_RECOMPACT_DEL
:
1683 * Recompaction attempt on delete failed. Should only
1684 * happen if target node type is pool. This is caused by
1685 * a hard-to-split distribution. Recompact on same node
1686 * size, but flag current node as "fallback" to ensure
1687 * we don't attempt recompaction before some activity
1688 * has reshuffled our node.
1690 assert(new_type
->type_class
== RCU_JA_POOL
);
1691 new_type_index
= old_type_index
;
1692 dbg_printf("Delete fallback keeping type %d\n", new_type_index
);
1693 uatomic_inc(&ja
->nr_fallback
);
1702 * Last resort fallback: pigeon.
1704 new_type_index
= (1UL << JA_TYPE_BITS
) - 1;
1705 dbg_printf("Fallback to type %d\n", new_type_index
);
1706 uatomic_inc(&ja
->nr_fallback
);
1712 * Return 0 on success, -EAGAIN if need to retry, or other negative
1713 * error value otherwise.
1716 int ja_node_set_nth(struct cds_ja
*ja
,
1717 struct cds_ja_inode_flag
**node_flag
, uint8_t n
,
1718 struct cds_ja_inode_flag
*child_node_flag
,
1719 struct cds_ja_shadow_node
*shadow_node
,
1723 unsigned int type_index
;
1724 const struct cds_ja_type
*type
;
1725 struct cds_ja_inode
*node
;
1727 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
1728 (unsigned int) n
, ja_node_ptr(*node_flag
), shadow_node
);
1730 node
= ja_node_ptr(*node_flag
);
1731 type_index
= ja_node_type(*node_flag
);
1732 type
= &ja_types
[type_index
];
1733 ret
= _ja_node_set_nth(type
, node
, *node_flag
, shadow_node
,
1734 n
, child_node_flag
);
1737 /* Not enough space in node, need to recompact to next type. */
1738 ret
= ja_node_recompact(JA_RECOMPACT_ADD_NEXT
, ja
, type_index
, type
, node
,
1739 shadow_node
, node_flag
, n
, child_node_flag
, NULL
, level
);
1742 /* Node needs to be recompacted. */
1743 ret
= ja_node_recompact(JA_RECOMPACT_ADD_SAME
, ja
, type_index
, type
, node
,
1744 shadow_node
, node_flag
, n
, child_node_flag
, NULL
, level
);
1751 * Return 0 on success, -EAGAIN if need to retry, or other negative
1752 * error value otherwise.
1755 int ja_node_clear_ptr(struct cds_ja
*ja
,
1756 struct cds_ja_inode_flag
**node_flag_ptr
, /* Pointer to location to nullify */
1757 struct cds_ja_inode_flag
**parent_node_flag_ptr
, /* Address of parent ptr in its parent */
1758 struct cds_ja_shadow_node
*shadow_node
, /* of parent */
1759 uint8_t n
, int level
)
1762 unsigned int type_index
;
1763 const struct cds_ja_type
*type
;
1764 struct cds_ja_inode
*node
;
1766 dbg_printf("ja_node_clear_ptr for node %p, shadow %p, target ptr %p\n",
1767 ja_node_ptr(*parent_node_flag_ptr
), shadow_node
, node_flag_ptr
);
1769 node
= ja_node_ptr(*parent_node_flag_ptr
);
1770 type_index
= ja_node_type(*parent_node_flag_ptr
);
1771 type
= &ja_types
[type_index
];
1772 ret
= _ja_node_clear_ptr(type
, node
, *parent_node_flag_ptr
, shadow_node
, node_flag_ptr
, n
);
1773 if (ret
== -EFBIG
) {
1774 /* Should try recompaction. */
1775 ret
= ja_node_recompact(JA_RECOMPACT_DEL
, ja
, type_index
, type
, node
,
1776 shadow_node
, parent_node_flag_ptr
, n
, NULL
,
1777 node_flag_ptr
, level
);
1782 struct cds_ja_node
*cds_ja_lookup(struct cds_ja
*ja
, uint64_t key
)
1784 unsigned int tree_depth
, i
;
1785 struct cds_ja_inode_flag
*node_flag
;
1787 if (caa_unlikely(key
> ja
->key_max
))
1789 tree_depth
= ja
->tree_depth
;
1790 node_flag
= rcu_dereference(ja
->root
);
1792 /* level 0: root node */
1793 if (!ja_node_ptr(node_flag
))
1796 for (i
= 1; i
< tree_depth
; i
++) {
1799 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
1800 node_flag
= ja_node_get_nth(node_flag
, NULL
, iter_key
);
1801 dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n",
1802 (unsigned int) iter_key
, node_flag
);
1803 if (!ja_node_ptr(node_flag
))
1807 /* Last level lookup succeded. We got an actual match. */
1808 return (struct cds_ja_node
*) node_flag
;
1812 struct cds_ja_node
*cds_ja_lookup_inequality(struct cds_ja
*ja
, uint64_t key
,
1813 uint64_t *result_key
, enum ja_lookup_inequality mode
)
1815 int tree_depth
, level
;
1816 struct cds_ja_inode_flag
*node_flag
, *cur_node_depth
[JA_MAX_DEPTH
];
1817 uint8_t cur_key
[JA_MAX_DEPTH
];
1818 uint64_t _result_key
= 0;
1819 enum ja_direction dir
;
1823 if (caa_unlikely(key
> ja
->key_max
|| key
== 0))
1827 if (caa_unlikely(key
>= ja
->key_max
))
1834 memset(cur_node_depth
, 0, sizeof(cur_node_depth
));
1835 memset(cur_key
, 0, sizeof(cur_key
));
1836 tree_depth
= ja
->tree_depth
;
1837 node_flag
= rcu_dereference(ja
->root
);
1838 cur_node_depth
[0] = node_flag
;
1840 /* level 0: root node */
1841 if (!ja_node_ptr(node_flag
))
1844 for (level
= 1; level
< tree_depth
; level
++) {
1847 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- level
- 1)));
1848 node_flag
= ja_node_get_nth(node_flag
, NULL
, iter_key
);
1849 if (!ja_node_ptr(node_flag
))
1851 cur_key
[level
- 1] = iter_key
;
1852 cur_node_depth
[level
] = node_flag
;
1853 dbg_printf("cds_ja_lookup_inequality iter key lookup %u finds node_flag %p\n",
1854 (unsigned int) iter_key
, node_flag
);
1857 if (level
== tree_depth
) {
1858 /* Last level lookup succeded. We got an equal match. */
1861 return (struct cds_ja_node
*) node_flag
;
1865 * Find highest value left/right of current node.
1866 * Current node is cur_node_depth[level].
1867 * Start at current level. If we cannot find any key left/right
1868 * of ours, go one level up, seek highest value left/right of
1869 * current (recursively), and when we find one, get the
1870 * rightmost/leftmost child of its rightmost/leftmost child
1883 for (; level
> 0; level
--) {
1886 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- level
- 1)));
1887 node_flag
= ja_node_get_leftright(cur_node_depth
[level
- 1],
1888 iter_key
, &cur_key
[level
- 1], dir
);
1889 dbg_printf("cds_ja_lookup_inequality find sibling from %u at %u finds node_flag %p\n",
1890 (unsigned int) iter_key
, (unsigned int) cur_key
[level
- 1],
1892 /* If found left/right sibling, find rightmost/leftmost child. */
1893 if (ja_node_ptr(node_flag
))
1898 /* Reached the root and could not find a left/right sibling. */
1905 * From this point, we are guaranteed to be able to find a
1906 * "below than"/"above than" match. ja_attach_node() and
1907 * ja_detach_node() both guarantee that it is not possible for a
1908 * lookup to reach a dead-end.
1912 * Find rightmost/leftmost child of rightmost/leftmost child
1925 for (; level
< tree_depth
; level
++) {
1926 node_flag
= ja_node_get_minmax(node_flag
, &cur_key
[level
- 1], dir
);
1927 dbg_printf("cds_ja_lookup_inequality find minmax at %u finds node_flag %p\n",
1928 (unsigned int) cur_key
[level
- 1],
1930 if (!ja_node_ptr(node_flag
))
1934 assert(level
== tree_depth
);
1937 for (level
= 1; level
< tree_depth
; level
++) {
1938 _result_key
|= ((uint64_t) cur_key
[level
- 1])
1939 << (JA_BITS_PER_BYTE
* (tree_depth
- level
- 1));
1941 *result_key
= _result_key
;
1943 return (struct cds_ja_node
*) node_flag
;
1946 struct cds_ja_node
*cds_ja_lookup_below_equal(struct cds_ja
*ja
,
1947 uint64_t key
, uint64_t *result_key
)
1949 dbg_printf("cds_ja_lookup_below_equal key %" PRIu64
"\n", key
);
1950 return cds_ja_lookup_inequality(ja
, key
, result_key
, JA_LOOKUP_BE
);
1953 struct cds_ja_node
*cds_ja_lookup_above_equal(struct cds_ja
*ja
,
1954 uint64_t key
, uint64_t *result_key
)
1956 dbg_printf("cds_ja_lookup_above_equal key %" PRIu64
"\n", key
);
1957 return cds_ja_lookup_inequality(ja
, key
, result_key
, JA_LOOKUP_AE
);
1961 * We reached an unpopulated node. Create it and the children we need,
1962 * and then attach the entire branch to the current node. This may
1963 * trigger recompaction of the current node. Locks needed: node lock
1964 * (for add), and, possibly, parent node lock (to update pointer due to
1965 * node recompaction).
1967 * First take node lock, check if recompaction is needed, then take
1968 * parent lock (if needed). Then we can proceed to create the new
1969 * branch. Publish the new branch, and release locks.
1970 * TODO: we currently always take the parent lock even when not needed.
1972 * ja_attach_node() ensures that a lookup will _never_ see a branch that
1973 * leads to a dead-end: before attaching a branch, the entire content of
1974 * the new branch is populated, thus creating a cluster, before
1975 * attaching the cluster to the rest of the tree, thus making it visible
1979 int ja_attach_node(struct cds_ja
*ja
,
1980 struct cds_ja_inode_flag
**attach_node_flag_ptr
,
1981 struct cds_ja_inode_flag
*attach_node_flag
,
1982 struct cds_ja_inode_flag
*parent_attach_node_flag
,
1983 struct cds_ja_inode_flag
**old_node_flag_ptr
,
1984 struct cds_ja_inode_flag
*old_node_flag
,
1987 struct cds_ja_node
*child_node
)
1989 struct cds_ja_shadow_node
*shadow_node
= NULL
,
1990 *parent_shadow_node
= NULL
;
1991 struct cds_ja_inode_flag
*iter_node_flag
, *iter_dest_node_flag
;
1993 struct cds_ja_inode_flag
*created_nodes
[JA_MAX_DEPTH
];
1994 int nr_created_nodes
= 0;
1996 dbg_printf("Attach node at level %u (old_node_flag %p, attach_node_flag_ptr %p attach_node_flag %p, parent_attach_node_flag %p)\n",
1997 level
, old_node_flag
, attach_node_flag_ptr
, attach_node_flag
, parent_attach_node_flag
);
1999 assert(!old_node_flag
);
2000 if (attach_node_flag
) {
2001 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, attach_node_flag
);
2007 if (parent_attach_node_flag
) {
2008 parent_shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
2009 parent_attach_node_flag
);
2010 if (!parent_shadow_node
) {
2016 if (old_node_flag_ptr
&& ja_node_ptr(*old_node_flag_ptr
)) {
2018 * Target node has been updated between RCU lookup and
2019 * lock acquisition. We need to re-try lookup and
2027 * Perform a lookup query to handle the case where
2028 * old_node_flag_ptr is NULL. We cannot use it to check if the
2029 * node has been populated between RCU lookup and mutex
2032 if (!old_node_flag_ptr
) {
2034 struct cds_ja_inode_flag
*lookup_node_flag
;
2035 struct cds_ja_inode_flag
**lookup_node_flag_ptr
;
2037 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- level
)));
2038 lookup_node_flag
= ja_node_get_nth(attach_node_flag
,
2039 &lookup_node_flag_ptr
,
2041 if (lookup_node_flag
) {
2047 if (attach_node_flag_ptr
&& ja_node_ptr(*attach_node_flag_ptr
) !=
2048 ja_node_ptr(attach_node_flag
)) {
2050 * Target node has been updated between RCU lookup and
2051 * lock acquisition. We need to re-try lookup and
2058 /* Create new branch, starting from bottom */
2059 iter_node_flag
= (struct cds_ja_inode_flag
*) child_node
;
2061 for (i
= ja
->tree_depth
- 1; i
>= (int) level
; i
--) {
2064 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- i
- 1)));
2065 dbg_printf("branch creation level %d, key %u\n",
2066 i
, (unsigned int) iter_key
);
2067 iter_dest_node_flag
= NULL
;
2068 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
2073 dbg_printf("branch creation error %d\n", ret
);
2076 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
2077 iter_node_flag
= iter_dest_node_flag
;
2081 /* Publish branch */
2084 * Attaching to root node.
2086 rcu_assign_pointer(ja
->root
, iter_node_flag
);
2090 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- level
)));
2091 dbg_printf("publish branch at level %d, key %u\n",
2092 level
- 1, (unsigned int) iter_key
);
2093 /* We need to use set_nth on the previous level. */
2094 iter_dest_node_flag
= attach_node_flag
;
2095 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
2098 shadow_node
, level
- 1);
2100 dbg_printf("branch publish error %d\n", ret
);
2106 rcu_assign_pointer(*attach_node_flag_ptr
, iter_dest_node_flag
);
2114 for (i
= 0; i
< nr_created_nodes
; i
++) {
2118 flags
= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
2120 flags
|= RCUJA_SHADOW_CLEAR_FREE_NODE
;
2121 tmpret
= rcuja_shadow_clear(ja
->ht
,
2129 if (parent_shadow_node
)
2130 rcuja_shadow_unlock(parent_shadow_node
);
2133 rcuja_shadow_unlock(shadow_node
);
2139 * Lock the parent containing the pointer to list of duplicates, and add
2140 * node to this list. Failure can happen if concurrent update changes
2141 * the parent before we get the lock. We return -EAGAIN in that case.
2142 * Return 0 on success, negative error value on failure.
2145 int ja_chain_node(struct cds_ja
*ja
,
2146 struct cds_ja_inode_flag
*parent_node_flag
,
2147 struct cds_ja_inode_flag
**node_flag_ptr
,
2148 struct cds_ja_inode_flag
*node_flag
,
2149 struct cds_ja_node
*node
)
2151 struct cds_ja_shadow_node
*shadow_node
;
2154 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, parent_node_flag
);
2158 if (ja_node_ptr(*node_flag_ptr
) != ja_node_ptr(node_flag
)) {
2163 * Add node to head of list. Safe against concurrent RCU read
2166 node
->next
= (struct cds_ja_node
*) node_flag
;
2167 rcu_assign_pointer(*node_flag_ptr
, (struct cds_ja_inode_flag
*) node
);
2169 rcuja_shadow_unlock(shadow_node
);
2174 int _cds_ja_add(struct cds_ja
*ja
, uint64_t key
,
2175 struct cds_ja_node
*node
,
2176 struct cds_ja_node
**unique_node_ret
)
2178 unsigned int tree_depth
, i
;
2179 struct cds_ja_inode_flag
*attach_node_flag
,
2183 *parent_attach_node_flag
;
2184 struct cds_ja_inode_flag
**attach_node_flag_ptr
,
2185 **parent_node_flag_ptr
,
2189 if (caa_unlikely(key
> ja
->key_max
)) {
2192 tree_depth
= ja
->tree_depth
;
2195 dbg_printf("cds_ja_add attempt: key %" PRIu64
", node %p\n",
2197 parent2_node_flag
= NULL
;
2199 (struct cds_ja_inode_flag
*) &ja
->root
; /* Use root ptr address as key for mutex */
2200 parent_node_flag_ptr
= NULL
;
2201 node_flag
= rcu_dereference(ja
->root
);
2202 node_flag_ptr
= &ja
->root
;
2204 /* Iterate on all internal levels */
2205 for (i
= 1; i
< tree_depth
; i
++) {
2208 if (!ja_node_ptr(node_flag
))
2210 dbg_printf("cds_ja_add iter parent2_node_flag %p parent_node_flag %p node_flag_ptr %p node_flag %p\n",
2211 parent2_node_flag
, parent_node_flag
, node_flag_ptr
, node_flag
);
2212 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
2213 parent2_node_flag
= parent_node_flag
;
2214 parent_node_flag
= node_flag
;
2215 parent_node_flag_ptr
= node_flag_ptr
;
2216 node_flag
= ja_node_get_nth(node_flag
,
2222 * We reached either bottom of tree or internal NULL node,
2223 * simply add node to last internal level, or chain it if key is
2226 if (!ja_node_ptr(node_flag
)) {
2227 dbg_printf("cds_ja_add NULL parent2_node_flag %p parent_node_flag %p node_flag_ptr %p node_flag %p\n",
2228 parent2_node_flag
, parent_node_flag
, node_flag_ptr
, node_flag
);
2230 attach_node_flag
= parent_node_flag
;
2231 attach_node_flag_ptr
= parent_node_flag_ptr
;
2232 parent_attach_node_flag
= parent2_node_flag
;
2234 ret
= ja_attach_node(ja
, attach_node_flag_ptr
,
2236 parent_attach_node_flag
,
2241 if (unique_node_ret
) {
2242 *unique_node_ret
= (struct cds_ja_node
*) ja_node_ptr(node_flag
);
2246 dbg_printf("cds_ja_add duplicate parent2_node_flag %p parent_node_flag %p node_flag_ptr %p node_flag %p\n",
2247 parent2_node_flag
, parent_node_flag
, node_flag_ptr
, node_flag
);
2249 attach_node_flag
= node_flag
;
2250 attach_node_flag_ptr
= node_flag_ptr
;
2251 parent_attach_node_flag
= parent_node_flag
;
2253 ret
= ja_chain_node(ja
,
2254 parent_attach_node_flag
,
2255 attach_node_flag_ptr
,
2259 if (ret
== -EAGAIN
|| ret
== -EEXIST
)
2265 int cds_ja_add(struct cds_ja
*ja
, uint64_t key
,
2266 struct cds_ja_node
*node
)
2268 return _cds_ja_add(ja
, key
, node
, NULL
);
2271 struct cds_ja_node
*cds_ja_add_unique(struct cds_ja
*ja
, uint64_t key
,
2272 struct cds_ja_node
*node
)
2275 struct cds_ja_node
*ret_node
;
2277 ret
= _cds_ja_add(ja
, key
, node
, &ret_node
);
2285 * Note: there is no need to lookup the pointer address associated with
2286 * each node's nth item after taking the lock: it's already been done by
2287 * cds_ja_del while holding the rcu read-side lock, and our node rules
2288 * ensure that when a match value -> pointer is found in a node, it is
2289 * _NEVER_ changed for that node without recompaction, and recompaction
2290 * reallocates the node.
2291 * However, when a child is removed from "linear" nodes, its pointer
2292 * is set to NULL. We therefore check, while holding the locks, if this
2293 * pointer is NULL, and return -ENOENT to the caller if it is the case.
2295 * ja_detach_node() ensures that a lookup will _never_ see a branch that
2296 * leads to a dead-end: when removing branch, it makes sure to perform
2297 * the "cut" at the highest node that has only one child, effectively
2298 * replacing it with a NULL pointer.
2301 int ja_detach_node(struct cds_ja
*ja
,
2302 struct cds_ja_inode_flag
**snapshot
,
2303 struct cds_ja_inode_flag
***snapshot_ptr
,
2304 uint8_t *snapshot_n
,
2307 struct cds_ja_node
*node
)
2309 struct cds_ja_shadow_node
*shadow_nodes
[JA_MAX_DEPTH
];
2310 struct cds_ja_inode_flag
**node_flag_ptr
= NULL
,
2311 *parent_node_flag
= NULL
,
2312 **parent_node_flag_ptr
= NULL
;
2313 struct cds_ja_inode_flag
*iter_node_flag
;
2314 int ret
, i
, nr_shadow
= 0, nr_clear
= 0, nr_branch
= 0;
2317 assert(nr_snapshot
== ja
->tree_depth
+ 1);
2320 * From the last internal level node going up, get the node
2321 * lock, check if the node has only one child left. If it is the
2322 * case, we continue iterating upward. When we reach a node
2323 * which has more that one child left, we lock the parent, and
2324 * proceed to the node deletion (removing its children too).
2326 for (i
= nr_snapshot
- 2; i
>= 1; i
--) {
2327 struct cds_ja_shadow_node
*shadow_node
;
2329 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
2335 shadow_nodes
[nr_shadow
++] = shadow_node
;
2338 * Check if node has been removed between RCU
2339 * lookup and lock acquisition.
2341 assert(snapshot_ptr
[i
+ 1]);
2342 if (ja_node_ptr(*snapshot_ptr
[i
+ 1])
2343 != ja_node_ptr(snapshot
[i
+ 1])) {
2348 assert(shadow_node
->nr_child
> 0);
2349 if (shadow_node
->nr_child
== 1 && i
> 1)
2352 if (shadow_node
->nr_child
> 1 || i
== 1) {
2353 /* Lock parent and break */
2354 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
2360 shadow_nodes
[nr_shadow
++] = shadow_node
;
2363 * Check if node has been removed between RCU
2364 * lookup and lock acquisition.
2366 assert(snapshot_ptr
[i
]);
2367 if (ja_node_ptr(*snapshot_ptr
[i
])
2368 != ja_node_ptr(snapshot
[i
])) {
2373 node_flag_ptr
= snapshot_ptr
[i
+ 1];
2374 n
= snapshot_n
[i
+ 1];
2375 parent_node_flag_ptr
= snapshot_ptr
[i
];
2376 parent_node_flag
= snapshot
[i
];
2380 * Lock parent's parent, in case we need
2381 * to recompact parent.
2383 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
2389 shadow_nodes
[nr_shadow
++] = shadow_node
;
2392 * Check if node has been removed between RCU
2393 * lookup and lock acquisition.
2395 assert(snapshot_ptr
[i
- 1]);
2396 if (ja_node_ptr(*snapshot_ptr
[i
- 1])
2397 != ja_node_ptr(snapshot
[i
- 1])) {
2408 * At this point, we want to delete all nodes that are about to
2409 * be removed from shadow_nodes (except the last one, which is
2410 * either the root or the parent of the upmost node with 1
2411 * child). OK to free lock here, because RCU read lock is held,
2412 * and free only performed in call_rcu.
2415 for (i
= 0; i
< nr_clear
; i
++) {
2416 ret
= rcuja_shadow_clear(ja
->ht
,
2417 shadow_nodes
[i
]->node_flag
,
2419 RCUJA_SHADOW_CLEAR_FREE_NODE
2420 | RCUJA_SHADOW_CLEAR_FREE_LOCK
);
2424 iter_node_flag
= parent_node_flag
;
2425 /* Remove from parent */
2426 ret
= ja_node_clear_ptr(ja
,
2427 node_flag_ptr
, /* Pointer to location to nullify */
2428 &iter_node_flag
, /* Old new parent ptr in its parent */
2429 shadow_nodes
[nr_branch
- 1], /* of parent */
2434 dbg_printf("ja_detach_node: publish %p instead of %p\n",
2435 iter_node_flag
, *parent_node_flag_ptr
);
2436 /* Update address of parent ptr in its parent */
2437 rcu_assign_pointer(*parent_node_flag_ptr
, iter_node_flag
);
2440 for (i
= 0; i
< nr_shadow
; i
++)
2441 rcuja_shadow_unlock(shadow_nodes
[i
]);
2446 int ja_unchain_node(struct cds_ja
*ja
,
2447 struct cds_ja_inode_flag
*parent_node_flag
,
2448 struct cds_ja_inode_flag
**node_flag_ptr
,
2449 struct cds_ja_inode_flag
*node_flag
,
2450 struct cds_ja_node
*node
)
2452 struct cds_ja_shadow_node
*shadow_node
;
2453 struct cds_ja_node
*iter_node
, **iter_node_ptr
, **prev_node_ptr
= NULL
;
2454 int ret
= 0, count
= 0, found
= 0;
2456 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, parent_node_flag
);
2459 if (ja_node_ptr(*node_flag_ptr
) != ja_node_ptr(node_flag
)) {
2464 * Find the previous node's next pointer pointing to our node,
2465 * so we can update it. Retry if another thread removed all but
2466 * one of duplicates since check (this check was performed
2467 * without lock). Ensure that the node we are about to remove is
2468 * still in the list (while holding lock). No need for RCU
2469 * traversal here since we hold the lock on the parent.
2471 iter_node_ptr
= (struct cds_ja_node
**) node_flag_ptr
;
2472 iter_node
= (struct cds_ja_node
*) ja_node_ptr(node_flag
);
2473 cds_ja_for_each_duplicate(iter_node
) {
2475 if (iter_node
== node
) {
2476 prev_node_ptr
= iter_node_ptr
;
2479 iter_node_ptr
= &iter_node
->next
;
2482 if (!found
|| count
== 1) {
2486 CMM_STORE_SHARED(*prev_node_ptr
, node
->next
);
2488 * Validate that we indeed removed the node from linked list.
2490 assert(ja_node_ptr(*node_flag_ptr
) != (struct cds_ja_inode
*) node
);
2492 rcuja_shadow_unlock(shadow_node
);
2497 * Called with RCU read lock held.
2499 int cds_ja_del(struct cds_ja
*ja
, uint64_t key
,
2500 struct cds_ja_node
*node
)
2502 unsigned int tree_depth
, i
;
2503 struct cds_ja_inode_flag
*snapshot
[JA_MAX_DEPTH
];
2504 struct cds_ja_inode_flag
**snapshot_ptr
[JA_MAX_DEPTH
];
2505 uint8_t snapshot_n
[JA_MAX_DEPTH
];
2506 struct cds_ja_inode_flag
*node_flag
;
2507 struct cds_ja_inode_flag
**prev_node_flag_ptr
,
2512 if (caa_unlikely(key
> ja
->key_max
))
2514 tree_depth
= ja
->tree_depth
;
2518 dbg_printf("cds_ja_del attempt: key %" PRIu64
", node %p\n",
2521 /* snapshot for level 0 is only for shadow node lookup */
2524 snapshot_ptr
[nr_snapshot
] = NULL
;
2525 snapshot
[nr_snapshot
++] = (struct cds_ja_inode_flag
*) &ja
->root
;
2526 node_flag
= rcu_dereference(ja
->root
);
2527 prev_node_flag_ptr
= &ja
->root
;
2528 node_flag_ptr
= &ja
->root
;
2530 /* Iterate on all internal levels */
2531 for (i
= 1; i
< tree_depth
; i
++) {
2534 dbg_printf("cds_ja_del iter node_flag %p\n",
2536 if (!ja_node_ptr(node_flag
)) {
2539 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
2540 snapshot_n
[nr_snapshot
+ 1] = iter_key
;
2541 snapshot_ptr
[nr_snapshot
] = prev_node_flag_ptr
;
2542 snapshot
[nr_snapshot
++] = node_flag
;
2543 node_flag
= ja_node_get_nth(node_flag
,
2547 prev_node_flag_ptr
= node_flag_ptr
;
2548 dbg_printf("cds_ja_del iter key lookup %u finds node_flag %p, prev_node_flag_ptr %p\n",
2549 (unsigned int) iter_key
, node_flag
,
2550 prev_node_flag_ptr
);
2553 * We reached bottom of tree, try to find the node we are trying
2554 * to remove. Fail if we cannot find it.
2556 if (!ja_node_ptr(node_flag
)) {
2557 dbg_printf("cds_ja_del: no node found for key %" PRIu64
"\n",
2561 struct cds_ja_node
*iter_node
, *match
= NULL
;
2564 iter_node
= (struct cds_ja_node
*) ja_node_ptr(node_flag
);
2565 cds_ja_for_each_duplicate_rcu(iter_node
) {
2566 dbg_printf("cds_ja_del: compare %p with iter_node %p\n", node
, iter_node
);
2567 if (iter_node
== node
)
2573 dbg_printf("cds_ja_del: no node match for node %p key %" PRIu64
"\n", node
, key
);
2579 * Removing last of duplicates. Last snapshot
2580 * does not have a shadow node (external leafs).
2582 snapshot_ptr
[nr_snapshot
] = prev_node_flag_ptr
;
2583 snapshot
[nr_snapshot
++] = node_flag
;
2584 ret
= ja_detach_node(ja
, snapshot
, snapshot_ptr
,
2585 snapshot_n
, nr_snapshot
, key
, node
);
2587 ret
= ja_unchain_node(ja
, snapshot
[nr_snapshot
- 1],
2588 node_flag_ptr
, node_flag
, match
);
2592 * Explanation of -ENOENT handling: caused by concurrent delete
2593 * between RCU lookup and actual removal. Need to re-do the
2594 * lookup and removal attempt.
2596 if (ret
== -EAGAIN
|| ret
== -ENOENT
)
2601 struct cds_ja
*_cds_ja_new(unsigned int key_bits
,
2602 const struct rcu_flavor_struct
*flavor
)
2606 struct cds_ja_shadow_node
*root_shadow_node
;
2608 ja
= calloc(sizeof(*ja
), 1);
2620 ja
->key_max
= (1ULL << key_bits
) - 1;
2623 ja
->key_max
= UINT64_MAX
;
2629 /* ja->root is NULL */
2630 /* tree_depth 0 is for pointer to root node */
2631 ja
->tree_depth
= (key_bits
>> JA_LOG2_BITS_PER_BYTE
) + 1;
2632 assert(ja
->tree_depth
<= JA_MAX_DEPTH
);
2633 ja
->ht
= rcuja_create_ht(flavor
);
2638 * Note: we should not free this node until judy array destroy.
2640 root_shadow_node
= rcuja_shadow_set(ja
->ht
,
2641 (struct cds_ja_inode_flag
*) &ja
->root
,
2643 if (!root_shadow_node
) {
2651 ret
= rcuja_delete_ht(ja
->ht
);
2661 void print_debug_fallback_distribution(struct cds_ja
*ja
)
2665 fprintf(stderr
, "Fallback node distribution:\n");
2666 for (i
= 0; i
< JA_ENTRY_PER_NODE
; i
++) {
2667 if (!ja
->node_fallback_count_distribution
[i
])
2669 fprintf(stderr
, " %3u: %4lu\n",
2670 i
, ja
->node_fallback_count_distribution
[i
]);
2675 int ja_final_checks(struct cds_ja
*ja
)
2677 double fallback_ratio
;
2678 unsigned long na
, nf
, nr_fallback
;
2681 fallback_ratio
= (double) uatomic_read(&ja
->nr_fallback
);
2682 fallback_ratio
/= (double) uatomic_read(&ja
->nr_nodes_allocated
);
2683 nr_fallback
= uatomic_read(&ja
->nr_fallback
);
2686 "[warning] RCU Judy Array used %lu fallback node(s) (ratio: %g)\n",
2687 uatomic_read(&ja
->nr_fallback
),
2690 na
= uatomic_read(&ja
->nr_nodes_allocated
);
2691 nf
= uatomic_read(&ja
->nr_nodes_freed
);
2692 dbg_printf("Nodes allocated: %lu, Nodes freed: %lu.\n", na
, nf
);
2694 print_debug_fallback_distribution(ja
);
2697 fprintf(stderr
, "[error] Judy array leaked %ld nodes. Allocated: %lu, freed: %lu.\n",
2698 (long) na
- nf
, na
, nf
);
2705 * There should be no more concurrent add, delete, nor look-up performed
2706 * on the Judy array while it is being destroyed (ensured by the
2709 int cds_ja_destroy(struct cds_ja
*ja
)
2711 const struct rcu_flavor_struct
*flavor
;
2714 flavor
= cds_lfht_rcu_flavor(ja
->ht
);
2715 rcuja_shadow_prune(ja
->ht
,
2716 RCUJA_SHADOW_CLEAR_FREE_NODE
| RCUJA_SHADOW_CLEAR_FREE_LOCK
);
2717 flavor
->thread_offline();
2718 ret
= rcuja_delete_ht(ja
->ht
);
2722 /* Wait for in-flight call_rcu free to complete. */
2725 flavor
->thread_online();
2726 ret
= ja_final_checks(ja
);