radixtree.c revision 1.18 1 1.18 ad /* $NetBSD: radixtree.c,v 1.18 2019/12/05 18:32:25 ad Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.18 ad * Copyright (c)2011,2012,2013 YAMAMOTO Takashi,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt */
28 1.1 yamt
29 1.1 yamt /*
30 1.17 yamt * radixtree.c
31 1.1 yamt *
32 1.18 ad * Overview:
33 1.18 ad *
34 1.18 ad * This is an implementation of radix tree, whose keys are uint64_t and leafs
35 1.17 yamt * are user provided pointers.
36 1.17 yamt *
37 1.18 ad * Leaf nodes are just void * and this implementation doesn't care about
38 1.18 ad * what they actually point to. However, this implementation has an assumption
39 1.18 ad * about their alignment. Specifically, this implementation assumes that their
40 1.18 ad * 2 LSBs are always zero and uses them for internal accounting.
41 1.18 ad *
42 1.18 ad * Intermediate nodes and memory allocation:
43 1.18 ad *
44 1.18 ad * Intermediate nodes are automatically allocated and freed internally and
45 1.18 ad * basically users don't need to care about them. The allocation is done via
46 1.18 ad * pool_cache_get(9) for _KERNEL, malloc(3) for userland, and alloc() for
47 1.18 ad * _STANDALONE environment. Only radix_tree_insert_node function can allocate
48 1.18 ad * memory for intermediate nodes and thus can fail for ENOMEM.
49 1.18 ad *
50 1.18 ad * Memory Efficiency:
51 1.18 ad *
52 1.18 ad * It's designed to work efficiently with dense index distribution.
53 1.18 ad * The memory consumption (number of necessary intermediate nodes) heavily
54 1.18 ad * depends on the index distribution. Basically, more dense index distribution
55 1.18 ad * consumes less nodes per item. Approximately,
56 1.18 ad *
57 1.18 ad * - the best case: about RADIX_TREE_PTR_PER_NODE items per intermediate node.
58 1.18 ad * it would look like the following.
59 1.18 ad *
60 1.18 ad * root (t_height=1)
61 1.18 ad * |
62 1.18 ad * v
63 1.18 ad * [ | | | ] (intermediate node. RADIX_TREE_PTR_PER_NODE=4 in this fig)
64 1.18 ad * | | | |
65 1.18 ad * v v v v
66 1.18 ad * p p p p (items)
67 1.18 ad *
68 1.18 ad * - the worst case: RADIX_TREE_MAX_HEIGHT intermediate nodes per item.
69 1.18 ad * it would look like the following if RADIX_TREE_MAX_HEIGHT=3.
70 1.18 ad *
71 1.18 ad * root (t_height=3)
72 1.18 ad * |
73 1.18 ad * v
74 1.18 ad * [ | | | ]
75 1.18 ad * |
76 1.18 ad * v
77 1.18 ad * [ | | | ]
78 1.18 ad * |
79 1.18 ad * v
80 1.18 ad * [ | | | ]
81 1.18 ad * |
82 1.18 ad * v
83 1.18 ad * p
84 1.18 ad *
85 1.18 ad * The height of tree (t_height) is dynamic. It's smaller if only small
86 1.18 ad * index values are used. As an extreme case, if only index 0 is used,
87 1.18 ad * the corresponding value is directly stored in the root of the tree
88 1.18 ad * (struct radix_tree) without allocating any intermediate nodes. In that
89 1.18 ad * case, t_height=0.
90 1.18 ad *
91 1.18 ad * Gang lookup:
92 1.17 yamt *
93 1.18 ad * This implementation provides a way to scan many nodes quickly via
94 1.17 yamt * radix_tree_gang_lookup_node function and its varients.
95 1.17 yamt *
96 1.18 ad * Tags:
97 1.18 ad *
98 1.18 ad * This implementation provides tagging functionality, which allows quick
99 1.18 ad * scanning of a subset of leaf nodes. Leaf nodes are untagged when inserted
100 1.18 ad * into the tree and can be tagged by radix_tree_set_tag function.
101 1.18 ad * radix_tree_gang_lookup_tagged_node function and its variants returns only
102 1.18 ad * leaf nodes with the given tag. To reduce amount of nodes to visit for
103 1.17 yamt * these functions, this implementation keeps tagging information in internal
104 1.17 yamt * intermediate nodes and quickly skips uninterested parts of a tree.
105 1.18 ad *
106 1.18 ad * A tree has RADIX_TREE_TAG_ID_MAX independent tag spaces, each of which are
107 1.18 ad * identified by an zero-origin numbers, tagid. For the current implementation,
108 1.18 ad * RADIX_TREE_TAG_ID_MAX is 2. A set of tags is described as a bitmask tagmask,
109 1.18 ad * which is a bitwise OR of (1 << tagid).
110 1.1 yamt */
111 1.1 yamt
112 1.1 yamt #include <sys/cdefs.h>
113 1.1 yamt
114 1.2 yamt #if defined(_KERNEL) || defined(_STANDALONE)
115 1.18 ad __KERNEL_RCSID(0, "$NetBSD: radixtree.c,v 1.18 2019/12/05 18:32:25 ad Exp $");
116 1.1 yamt #include <sys/param.h>
117 1.3 yamt #include <sys/errno.h>
118 1.1 yamt #include <sys/pool.h>
119 1.1 yamt #include <sys/radixtree.h>
120 1.3 yamt #include <lib/libkern/libkern.h>
121 1.3 yamt #if defined(_STANDALONE)
122 1.3 yamt #include <lib/libsa/stand.h>
123 1.3 yamt #endif /* defined(_STANDALONE) */
124 1.2 yamt #else /* defined(_KERNEL) || defined(_STANDALONE) */
125 1.18 ad __RCSID("$NetBSD: radixtree.c,v 1.18 2019/12/05 18:32:25 ad Exp $");
126 1.1 yamt #include <assert.h>
127 1.1 yamt #include <errno.h>
128 1.1 yamt #include <stdbool.h>
129 1.1 yamt #include <stdlib.h>
130 1.8 yamt #include <string.h>
131 1.1 yamt #if 1
132 1.1 yamt #define KASSERT assert
133 1.1 yamt #else
134 1.1 yamt #define KASSERT(a) /* nothing */
135 1.1 yamt #endif
136 1.2 yamt #endif /* defined(_KERNEL) || defined(_STANDALONE) */
137 1.1 yamt
138 1.1 yamt #include <sys/radixtree.h>
139 1.1 yamt
140 1.1 yamt #define RADIX_TREE_BITS_PER_HEIGHT 4 /* XXX tune */
141 1.1 yamt #define RADIX_TREE_PTR_PER_NODE (1 << RADIX_TREE_BITS_PER_HEIGHT)
142 1.1 yamt #define RADIX_TREE_MAX_HEIGHT (64 / RADIX_TREE_BITS_PER_HEIGHT)
143 1.15 yamt #define RADIX_TREE_INVALID_HEIGHT (RADIX_TREE_MAX_HEIGHT + 1)
144 1.2 yamt __CTASSERT((64 % RADIX_TREE_BITS_PER_HEIGHT) == 0);
145 1.1 yamt
146 1.2 yamt __CTASSERT(((1 << RADIX_TREE_TAG_ID_MAX) & (sizeof(int) - 1)) == 0);
147 1.1 yamt #define RADIX_TREE_TAG_MASK ((1 << RADIX_TREE_TAG_ID_MAX) - 1)
148 1.1 yamt
149 1.1 yamt static inline void *
150 1.1 yamt entry_ptr(void *p)
151 1.1 yamt {
152 1.1 yamt
153 1.1 yamt return (void *)((uintptr_t)p & ~RADIX_TREE_TAG_MASK);
154 1.1 yamt }
155 1.1 yamt
156 1.1 yamt static inline unsigned int
157 1.1 yamt entry_tagmask(void *p)
158 1.1 yamt {
159 1.1 yamt
160 1.1 yamt return (uintptr_t)p & RADIX_TREE_TAG_MASK;
161 1.1 yamt }
162 1.1 yamt
163 1.1 yamt static inline void *
164 1.1 yamt entry_compose(void *p, unsigned int tagmask)
165 1.1 yamt {
166 1.1 yamt
167 1.1 yamt return (void *)((uintptr_t)p | tagmask);
168 1.1 yamt }
169 1.1 yamt
170 1.1 yamt static inline bool
171 1.1 yamt entry_match_p(void *p, unsigned int tagmask)
172 1.1 yamt {
173 1.1 yamt
174 1.1 yamt KASSERT(entry_ptr(p) != NULL || entry_tagmask(p) == 0);
175 1.1 yamt if (p == NULL) {
176 1.1 yamt return false;
177 1.1 yamt }
178 1.1 yamt if (tagmask == 0) {
179 1.1 yamt return true;
180 1.1 yamt }
181 1.1 yamt return (entry_tagmask(p) & tagmask) != 0;
182 1.1 yamt }
183 1.1 yamt
184 1.1 yamt /*
185 1.1 yamt * radix_tree_node: an intermediate node
186 1.1 yamt *
187 1.1 yamt * we don't care the type of leaf nodes. they are just void *.
188 1.1 yamt */
189 1.1 yamt
190 1.1 yamt struct radix_tree_node {
191 1.1 yamt void *n_ptrs[RADIX_TREE_PTR_PER_NODE];
192 1.1 yamt unsigned int n_nptrs; /* # of non-NULL pointers in n_ptrs */
193 1.1 yamt };
194 1.1 yamt
195 1.7 yamt /*
196 1.7 yamt * any_children_tagmask:
197 1.7 yamt *
198 1.7 yamt * return OR'ed tagmask of the given node's children.
199 1.7 yamt */
200 1.7 yamt
201 1.1 yamt static unsigned int
202 1.13 yamt any_children_tagmask(const struct radix_tree_node *n)
203 1.1 yamt {
204 1.1 yamt unsigned int mask;
205 1.1 yamt int i;
206 1.1 yamt
207 1.1 yamt mask = 0;
208 1.1 yamt for (i = 0; i < RADIX_TREE_PTR_PER_NODE; i++) {
209 1.1 yamt mask |= (unsigned int)(uintptr_t)n->n_ptrs[i];
210 1.1 yamt }
211 1.1 yamt return mask & RADIX_TREE_TAG_MASK;
212 1.1 yamt }
213 1.1 yamt
214 1.1 yamt /*
215 1.1 yamt * p_refs[0].pptr == &t->t_root
216 1.1 yamt * :
217 1.1 yamt * p_refs[n].pptr == &(*p_refs[n-1])->n_ptrs[x]
218 1.1 yamt * :
219 1.1 yamt * :
220 1.1 yamt * p_refs[t->t_height].pptr == &leaf_pointer
221 1.1 yamt */
222 1.1 yamt
223 1.1 yamt struct radix_tree_path {
224 1.1 yamt struct radix_tree_node_ref {
225 1.1 yamt void **pptr;
226 1.1 yamt } p_refs[RADIX_TREE_MAX_HEIGHT + 1]; /* +1 for the root ptr */
227 1.15 yamt /*
228 1.15 yamt * p_lastidx is either the index of the last valid element of p_refs[]
229 1.15 yamt * or RADIX_TREE_INVALID_HEIGHT.
230 1.15 yamt * RADIX_TREE_INVALID_HEIGHT means that radix_tree_lookup_ptr found
231 1.15 yamt * that the height of the tree is not enough to cover the given index.
232 1.15 yamt */
233 1.10 yamt unsigned int p_lastidx;
234 1.1 yamt };
235 1.1 yamt
236 1.1 yamt static inline void **
237 1.13 yamt path_pptr(const struct radix_tree *t, const struct radix_tree_path *p,
238 1.1 yamt unsigned int height)
239 1.1 yamt {
240 1.1 yamt
241 1.1 yamt KASSERT(height <= t->t_height);
242 1.1 yamt return p->p_refs[height].pptr;
243 1.1 yamt }
244 1.1 yamt
245 1.1 yamt static inline struct radix_tree_node *
246 1.13 yamt path_node(const struct radix_tree * t, const struct radix_tree_path *p,
247 1.13 yamt unsigned int height)
248 1.1 yamt {
249 1.1 yamt
250 1.1 yamt KASSERT(height <= t->t_height);
251 1.1 yamt return entry_ptr(*path_pptr(t, p, height));
252 1.1 yamt }
253 1.1 yamt
254 1.1 yamt /*
255 1.1 yamt * radix_tree_init_tree:
256 1.1 yamt *
257 1.18 ad * Initialize a tree.
258 1.1 yamt */
259 1.1 yamt
260 1.1 yamt void
261 1.1 yamt radix_tree_init_tree(struct radix_tree *t)
262 1.1 yamt {
263 1.1 yamt
264 1.1 yamt t->t_height = 0;
265 1.1 yamt t->t_root = NULL;
266 1.1 yamt }
267 1.1 yamt
268 1.1 yamt /*
269 1.18 ad * radix_tree_fini_tree:
270 1.1 yamt *
271 1.18 ad * Finish using a tree.
272 1.1 yamt */
273 1.1 yamt
274 1.1 yamt void
275 1.1 yamt radix_tree_fini_tree(struct radix_tree *t)
276 1.1 yamt {
277 1.1 yamt
278 1.1 yamt KASSERT(t->t_root == NULL);
279 1.1 yamt KASSERT(t->t_height == 0);
280 1.1 yamt }
281 1.1 yamt
282 1.18 ad /*
283 1.18 ad * radix_tree_empty_tree_p:
284 1.18 ad *
285 1.18 ad * Return if the tree is empty.
286 1.18 ad */
287 1.18 ad
288 1.9 yamt bool
289 1.9 yamt radix_tree_empty_tree_p(struct radix_tree *t)
290 1.9 yamt {
291 1.9 yamt
292 1.9 yamt return t->t_root == NULL;
293 1.9 yamt }
294 1.9 yamt
295 1.18 ad /*
296 1.18 ad * radix_tree_empty_tree_p:
297 1.18 ad *
298 1.18 ad * Return true if the tree has any nodes with the given tag. Otherwise
299 1.18 ad * return false.
300 1.18 ad *
301 1.18 ad * It's illegal to call this function with tagmask 0.
302 1.18 ad */
303 1.18 ad
304 1.16 yamt bool
305 1.18 ad radix_tree_empty_tagged_tree_p(struct radix_tree *t, unsigned int tagmask)
306 1.16 yamt {
307 1.16 yamt
308 1.18 ad KASSERT(tagmask != 0);
309 1.16 yamt return (entry_tagmask(t->t_root) & tagmask) == 0;
310 1.16 yamt }
311 1.16 yamt
312 1.3 yamt static void
313 1.3 yamt radix_tree_node_init(struct radix_tree_node *n)
314 1.3 yamt {
315 1.3 yamt
316 1.3 yamt memset(n, 0, sizeof(*n));
317 1.3 yamt }
318 1.3 yamt
319 1.1 yamt #if defined(_KERNEL)
320 1.2 yamt pool_cache_t radix_tree_node_cache __read_mostly;
321 1.1 yamt
322 1.1 yamt static int
323 1.1 yamt radix_tree_node_ctor(void *dummy, void *item, int flags)
324 1.1 yamt {
325 1.1 yamt struct radix_tree_node *n = item;
326 1.1 yamt
327 1.1 yamt KASSERT(dummy == NULL);
328 1.3 yamt radix_tree_node_init(n);
329 1.1 yamt return 0;
330 1.1 yamt }
331 1.1 yamt
332 1.1 yamt /*
333 1.1 yamt * radix_tree_init:
334 1.1 yamt *
335 1.1 yamt * initialize the subsystem.
336 1.1 yamt */
337 1.1 yamt
338 1.1 yamt void
339 1.1 yamt radix_tree_init(void)
340 1.1 yamt {
341 1.1 yamt
342 1.1 yamt radix_tree_node_cache = pool_cache_init(sizeof(struct radix_tree_node),
343 1.1 yamt 0, 0, 0, "radix_tree_node", NULL, IPL_NONE, radix_tree_node_ctor,
344 1.1 yamt NULL, NULL);
345 1.1 yamt KASSERT(radix_tree_node_cache != NULL);
346 1.1 yamt }
347 1.1 yamt #endif /* defined(_KERNEL) */
348 1.1 yamt
349 1.1 yamt static bool __unused
350 1.1 yamt radix_tree_node_clean_p(const struct radix_tree_node *n)
351 1.1 yamt {
352 1.1 yamt unsigned int i;
353 1.1 yamt
354 1.1 yamt if (n->n_nptrs != 0) {
355 1.1 yamt return false;
356 1.1 yamt }
357 1.1 yamt for (i = 0; i < RADIX_TREE_PTR_PER_NODE; i++) {
358 1.1 yamt if (n->n_ptrs[i] != NULL) {
359 1.1 yamt return false;
360 1.1 yamt }
361 1.1 yamt }
362 1.1 yamt return true;
363 1.1 yamt }
364 1.1 yamt
365 1.1 yamt static struct radix_tree_node *
366 1.1 yamt radix_tree_alloc_node(void)
367 1.1 yamt {
368 1.1 yamt struct radix_tree_node *n;
369 1.1 yamt
370 1.1 yamt #if defined(_KERNEL)
371 1.18 ad /*
372 1.18 ad * note that pool_cache_get can block.
373 1.18 ad */
374 1.1 yamt n = pool_cache_get(radix_tree_node_cache, PR_NOWAIT);
375 1.1 yamt #else /* defined(_KERNEL) */
376 1.3 yamt #if defined(_STANDALONE)
377 1.3 yamt n = alloc(sizeof(*n));
378 1.3 yamt #else /* defined(_STANDALONE) */
379 1.3 yamt n = malloc(sizeof(*n));
380 1.3 yamt #endif /* defined(_STANDALONE) */
381 1.3 yamt if (n != NULL) {
382 1.3 yamt radix_tree_node_init(n);
383 1.3 yamt }
384 1.1 yamt #endif /* defined(_KERNEL) */
385 1.1 yamt KASSERT(n == NULL || radix_tree_node_clean_p(n));
386 1.1 yamt return n;
387 1.1 yamt }
388 1.1 yamt
389 1.1 yamt static void
390 1.1 yamt radix_tree_free_node(struct radix_tree_node *n)
391 1.1 yamt {
392 1.1 yamt
393 1.1 yamt KASSERT(radix_tree_node_clean_p(n));
394 1.1 yamt #if defined(_KERNEL)
395 1.1 yamt pool_cache_put(radix_tree_node_cache, n);
396 1.3 yamt #elif defined(_STANDALONE)
397 1.3 yamt dealloc(n, sizeof(*n));
398 1.3 yamt #else
399 1.1 yamt free(n);
400 1.3 yamt #endif
401 1.1 yamt }
402 1.1 yamt
403 1.1 yamt static int
404 1.1 yamt radix_tree_grow(struct radix_tree *t, unsigned int newheight)
405 1.1 yamt {
406 1.1 yamt const unsigned int tagmask = entry_tagmask(t->t_root);
407 1.1 yamt
408 1.1 yamt KASSERT(newheight <= 64 / RADIX_TREE_BITS_PER_HEIGHT);
409 1.1 yamt if (t->t_root == NULL) {
410 1.1 yamt t->t_height = newheight;
411 1.1 yamt return 0;
412 1.1 yamt }
413 1.1 yamt while (t->t_height < newheight) {
414 1.1 yamt struct radix_tree_node *n;
415 1.1 yamt
416 1.1 yamt n = radix_tree_alloc_node();
417 1.1 yamt if (n == NULL) {
418 1.1 yamt /*
419 1.1 yamt * don't bother to revert our changes.
420 1.1 yamt * the caller will likely retry.
421 1.1 yamt */
422 1.1 yamt return ENOMEM;
423 1.1 yamt }
424 1.1 yamt n->n_nptrs = 1;
425 1.1 yamt n->n_ptrs[0] = t->t_root;
426 1.1 yamt t->t_root = entry_compose(n, tagmask);
427 1.1 yamt t->t_height++;
428 1.1 yamt }
429 1.1 yamt return 0;
430 1.1 yamt }
431 1.1 yamt
432 1.5 yamt /*
433 1.5 yamt * radix_tree_lookup_ptr:
434 1.5 yamt *
435 1.5 yamt * an internal helper function used for various exported functions.
436 1.5 yamt *
437 1.5 yamt * return the pointer to store the node for the given index.
438 1.5 yamt *
439 1.5 yamt * if alloc is true, try to allocate the storage. (note for _KERNEL:
440 1.5 yamt * in that case, this function can block.) if the allocation failed or
441 1.5 yamt * alloc is false, return NULL.
442 1.5 yamt *
443 1.5 yamt * if path is not NULL, fill it for the caller's investigation.
444 1.5 yamt *
445 1.5 yamt * if tagmask is not zero, search only for nodes with the tag set.
446 1.15 yamt * note that, however, this function doesn't check the tagmask for the leaf
447 1.15 yamt * pointer. it's a caller's responsibility to investigate the value which
448 1.15 yamt * is pointed by the returned pointer if necessary.
449 1.5 yamt *
450 1.5 yamt * while this function is a bit large, as it's called with some constant
451 1.5 yamt * arguments, inlining might have benefits. anyway, a compiler will decide.
452 1.5 yamt */
453 1.5 yamt
454 1.1 yamt static inline void **
455 1.1 yamt radix_tree_lookup_ptr(struct radix_tree *t, uint64_t idx,
456 1.1 yamt struct radix_tree_path *path, bool alloc, const unsigned int tagmask)
457 1.1 yamt {
458 1.1 yamt struct radix_tree_node *n;
459 1.1 yamt int hshift = RADIX_TREE_BITS_PER_HEIGHT * t->t_height;
460 1.1 yamt int shift;
461 1.1 yamt void **vpp;
462 1.1 yamt const uint64_t mask = (UINT64_C(1) << RADIX_TREE_BITS_PER_HEIGHT) - 1;
463 1.1 yamt struct radix_tree_node_ref *refs = NULL;
464 1.1 yamt
465 1.5 yamt /*
466 1.5 yamt * check unsupported combinations
467 1.5 yamt */
468 1.1 yamt KASSERT(tagmask == 0 || !alloc);
469 1.1 yamt KASSERT(path == NULL || !alloc);
470 1.1 yamt vpp = &t->t_root;
471 1.1 yamt if (path != NULL) {
472 1.1 yamt refs = path->p_refs;
473 1.1 yamt refs->pptr = vpp;
474 1.1 yamt }
475 1.1 yamt n = NULL;
476 1.1 yamt for (shift = 64 - RADIX_TREE_BITS_PER_HEIGHT; shift >= 0;) {
477 1.1 yamt struct radix_tree_node *c;
478 1.1 yamt void *entry;
479 1.1 yamt const uint64_t i = (idx >> shift) & mask;
480 1.1 yamt
481 1.1 yamt if (shift >= hshift) {
482 1.1 yamt unsigned int newheight;
483 1.1 yamt
484 1.1 yamt KASSERT(vpp == &t->t_root);
485 1.1 yamt if (i == 0) {
486 1.1 yamt shift -= RADIX_TREE_BITS_PER_HEIGHT;
487 1.1 yamt continue;
488 1.1 yamt }
489 1.1 yamt if (!alloc) {
490 1.1 yamt if (path != NULL) {
491 1.1 yamt KASSERT((refs - path->p_refs) == 0);
492 1.15 yamt path->p_lastidx =
493 1.15 yamt RADIX_TREE_INVALID_HEIGHT;
494 1.1 yamt }
495 1.1 yamt return NULL;
496 1.1 yamt }
497 1.1 yamt newheight = shift / RADIX_TREE_BITS_PER_HEIGHT + 1;
498 1.1 yamt if (radix_tree_grow(t, newheight)) {
499 1.1 yamt return NULL;
500 1.1 yamt }
501 1.1 yamt hshift = RADIX_TREE_BITS_PER_HEIGHT * t->t_height;
502 1.1 yamt }
503 1.1 yamt entry = *vpp;
504 1.1 yamt c = entry_ptr(entry);
505 1.1 yamt if (c == NULL ||
506 1.1 yamt (tagmask != 0 &&
507 1.1 yamt (entry_tagmask(entry) & tagmask) == 0)) {
508 1.1 yamt if (!alloc) {
509 1.1 yamt if (path != NULL) {
510 1.1 yamt path->p_lastidx = refs - path->p_refs;
511 1.1 yamt }
512 1.1 yamt return NULL;
513 1.1 yamt }
514 1.1 yamt c = radix_tree_alloc_node();
515 1.1 yamt if (c == NULL) {
516 1.1 yamt return NULL;
517 1.1 yamt }
518 1.1 yamt *vpp = c;
519 1.1 yamt if (n != NULL) {
520 1.1 yamt KASSERT(n->n_nptrs < RADIX_TREE_PTR_PER_NODE);
521 1.1 yamt n->n_nptrs++;
522 1.1 yamt }
523 1.1 yamt }
524 1.1 yamt n = c;
525 1.1 yamt vpp = &n->n_ptrs[i];
526 1.1 yamt if (path != NULL) {
527 1.1 yamt refs++;
528 1.1 yamt refs->pptr = vpp;
529 1.1 yamt }
530 1.1 yamt shift -= RADIX_TREE_BITS_PER_HEIGHT;
531 1.1 yamt }
532 1.1 yamt if (alloc) {
533 1.1 yamt KASSERT(*vpp == NULL);
534 1.1 yamt if (n != NULL) {
535 1.1 yamt KASSERT(n->n_nptrs < RADIX_TREE_PTR_PER_NODE);
536 1.1 yamt n->n_nptrs++;
537 1.1 yamt }
538 1.1 yamt }
539 1.1 yamt if (path != NULL) {
540 1.1 yamt path->p_lastidx = refs - path->p_refs;
541 1.1 yamt }
542 1.1 yamt return vpp;
543 1.1 yamt }
544 1.1 yamt
545 1.1 yamt /*
546 1.1 yamt * radix_tree_insert_node:
547 1.1 yamt *
548 1.18 ad * Insert the node at the given index.
549 1.18 ad *
550 1.18 ad * It's illegal to insert NULL. It's illegal to insert a non-aligned pointer.
551 1.1 yamt *
552 1.18 ad * This function returns ENOMEM if necessary memory allocation failed.
553 1.18 ad * Otherwise, this function returns 0.
554 1.1 yamt *
555 1.18 ad * Note that inserting a node can involves memory allocation for intermediate
556 1.18 ad * nodes. If _KERNEL, it's done with no-sleep IPL_NONE memory allocation.
557 1.4 yamt *
558 1.18 ad * For the newly inserted node, all tags are cleared.
559 1.1 yamt */
560 1.1 yamt
561 1.1 yamt int
562 1.1 yamt radix_tree_insert_node(struct radix_tree *t, uint64_t idx, void *p)
563 1.1 yamt {
564 1.1 yamt void **vpp;
565 1.1 yamt
566 1.1 yamt KASSERT(p != NULL);
567 1.18 ad KASSERT(entry_tagmask(entry_compose(p, 0)) == 0);
568 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, true, 0);
569 1.1 yamt if (vpp == NULL) {
570 1.1 yamt return ENOMEM;
571 1.1 yamt }
572 1.1 yamt KASSERT(*vpp == NULL);
573 1.1 yamt *vpp = p;
574 1.1 yamt return 0;
575 1.1 yamt }
576 1.1 yamt
577 1.4 yamt /*
578 1.4 yamt * radix_tree_replace_node:
579 1.4 yamt *
580 1.18 ad * Replace a node at the given index with the given node and return the
581 1.18 ad * replaced one.
582 1.18 ad *
583 1.18 ad * It's illegal to try to replace a node which has not been inserted.
584 1.4 yamt *
585 1.18 ad * This function keeps tags intact.
586 1.4 yamt */
587 1.4 yamt
588 1.1 yamt void *
589 1.1 yamt radix_tree_replace_node(struct radix_tree *t, uint64_t idx, void *p)
590 1.1 yamt {
591 1.1 yamt void **vpp;
592 1.1 yamt void *oldp;
593 1.1 yamt
594 1.1 yamt KASSERT(p != NULL);
595 1.18 ad KASSERT(entry_tagmask(entry_compose(p, 0)) == 0);
596 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
597 1.1 yamt KASSERT(vpp != NULL);
598 1.1 yamt oldp = *vpp;
599 1.1 yamt KASSERT(oldp != NULL);
600 1.1 yamt *vpp = entry_compose(p, entry_tagmask(*vpp));
601 1.1 yamt return entry_ptr(oldp);
602 1.1 yamt }
603 1.1 yamt
604 1.1 yamt /*
605 1.1 yamt * radix_tree_remove_node:
606 1.1 yamt *
607 1.18 ad * Remove the node at the given index.
608 1.18 ad *
609 1.18 ad * It's illegal to try to remove a node which has not been inserted.
610 1.1 yamt */
611 1.1 yamt
612 1.1 yamt void *
613 1.1 yamt radix_tree_remove_node(struct radix_tree *t, uint64_t idx)
614 1.1 yamt {
615 1.1 yamt struct radix_tree_path path;
616 1.1 yamt void **vpp;
617 1.1 yamt void *oldp;
618 1.1 yamt int i;
619 1.1 yamt
620 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, &path, false, 0);
621 1.1 yamt KASSERT(vpp != NULL);
622 1.1 yamt oldp = *vpp;
623 1.1 yamt KASSERT(oldp != NULL);
624 1.1 yamt KASSERT(path.p_lastidx == t->t_height);
625 1.1 yamt KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
626 1.1 yamt *vpp = NULL;
627 1.1 yamt for (i = t->t_height - 1; i >= 0; i--) {
628 1.1 yamt void *entry;
629 1.1 yamt struct radix_tree_node ** const pptr =
630 1.1 yamt (struct radix_tree_node **)path_pptr(t, &path, i);
631 1.1 yamt struct radix_tree_node *n;
632 1.1 yamt
633 1.1 yamt KASSERT(pptr != NULL);
634 1.1 yamt entry = *pptr;
635 1.1 yamt n = entry_ptr(entry);
636 1.1 yamt KASSERT(n != NULL);
637 1.1 yamt KASSERT(n->n_nptrs > 0);
638 1.1 yamt n->n_nptrs--;
639 1.1 yamt if (n->n_nptrs > 0) {
640 1.1 yamt break;
641 1.1 yamt }
642 1.1 yamt radix_tree_free_node(n);
643 1.1 yamt *pptr = NULL;
644 1.1 yamt }
645 1.1 yamt /*
646 1.1 yamt * fix up height
647 1.1 yamt */
648 1.1 yamt if (i < 0) {
649 1.1 yamt KASSERT(t->t_root == NULL);
650 1.1 yamt t->t_height = 0;
651 1.1 yamt }
652 1.1 yamt /*
653 1.1 yamt * update tags
654 1.1 yamt */
655 1.1 yamt for (; i >= 0; i--) {
656 1.1 yamt void *entry;
657 1.1 yamt struct radix_tree_node ** const pptr =
658 1.1 yamt (struct radix_tree_node **)path_pptr(t, &path, i);
659 1.1 yamt struct radix_tree_node *n;
660 1.1 yamt unsigned int newmask;
661 1.1 yamt
662 1.1 yamt KASSERT(pptr != NULL);
663 1.1 yamt entry = *pptr;
664 1.1 yamt n = entry_ptr(entry);
665 1.1 yamt KASSERT(n != NULL);
666 1.1 yamt KASSERT(n->n_nptrs > 0);
667 1.1 yamt newmask = any_children_tagmask(n);
668 1.1 yamt if (newmask == entry_tagmask(entry)) {
669 1.1 yamt break;
670 1.1 yamt }
671 1.1 yamt *pptr = entry_compose(n, newmask);
672 1.1 yamt }
673 1.1 yamt /*
674 1.1 yamt * XXX is it worth to try to reduce height?
675 1.1 yamt * if we do that, make radix_tree_grow rollback its change as well.
676 1.1 yamt */
677 1.1 yamt return entry_ptr(oldp);
678 1.1 yamt }
679 1.1 yamt
680 1.1 yamt /*
681 1.1 yamt * radix_tree_lookup_node:
682 1.1 yamt *
683 1.18 ad * Returns the node at the given index.
684 1.18 ad * Returns NULL if nothing is found at the given index.
685 1.1 yamt */
686 1.1 yamt
687 1.1 yamt void *
688 1.1 yamt radix_tree_lookup_node(struct radix_tree *t, uint64_t idx)
689 1.1 yamt {
690 1.1 yamt void **vpp;
691 1.1 yamt
692 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
693 1.1 yamt if (vpp == NULL) {
694 1.1 yamt return NULL;
695 1.1 yamt }
696 1.1 yamt return entry_ptr(*vpp);
697 1.1 yamt }
698 1.1 yamt
699 1.1 yamt static inline void
700 1.1 yamt gang_lookup_init(struct radix_tree *t, uint64_t idx,
701 1.1 yamt struct radix_tree_path *path, const unsigned int tagmask)
702 1.1 yamt {
703 1.18 ad void **vpp __unused;
704 1.1 yamt
705 1.18 ad #if defined(DIAGNOSTIC)
706 1.18 ad vpp =
707 1.18 ad #endif /* defined(DIAGNOSTIC) */
708 1.18 ad radix_tree_lookup_ptr(t, idx, path, false, tagmask);
709 1.1 yamt KASSERT(vpp == NULL ||
710 1.1 yamt vpp == path_pptr(t, path, path->p_lastidx));
711 1.1 yamt KASSERT(&t->t_root == path_pptr(t, path, 0));
712 1.15 yamt KASSERT(path->p_lastidx == RADIX_TREE_INVALID_HEIGHT ||
713 1.15 yamt path->p_lastidx == t->t_height ||
714 1.15 yamt !entry_match_p(*path_pptr(t, path, path->p_lastidx), tagmask));
715 1.1 yamt }
716 1.1 yamt
717 1.15 yamt /*
718 1.15 yamt * gang_lookup_scan:
719 1.15 yamt *
720 1.15 yamt * a helper routine for radix_tree_gang_lookup_node and its variants.
721 1.15 yamt */
722 1.15 yamt
723 1.1 yamt static inline unsigned int
724 1.15 yamt __attribute__((__always_inline__))
725 1.1 yamt gang_lookup_scan(struct radix_tree *t, struct radix_tree_path *path,
726 1.18 ad void **results, const unsigned int maxresults, const unsigned int tagmask,
727 1.18 ad const bool reverse, const bool dense)
728 1.1 yamt {
729 1.15 yamt
730 1.15 yamt /*
731 1.15 yamt * we keep the path updated only for lastidx-1.
732 1.15 yamt * vpp is what path_pptr(t, path, lastidx) would be.
733 1.15 yamt */
734 1.1 yamt void **vpp;
735 1.10 yamt unsigned int nfound;
736 1.1 yamt unsigned int lastidx;
737 1.15 yamt /*
738 1.15 yamt * set up scan direction dependant constants so that we can iterate
739 1.15 yamt * n_ptrs as the following.
740 1.15 yamt *
741 1.15 yamt * for (i = first; i != guard; i += step)
742 1.15 yamt * visit n->n_ptrs[i];
743 1.15 yamt */
744 1.15 yamt const int step = reverse ? -1 : 1;
745 1.15 yamt const unsigned int first = reverse ? RADIX_TREE_PTR_PER_NODE - 1 : 0;
746 1.15 yamt const unsigned int last = reverse ? 0 : RADIX_TREE_PTR_PER_NODE - 1;
747 1.15 yamt const unsigned int guard = last + step;
748 1.1 yamt
749 1.1 yamt KASSERT(maxresults > 0);
750 1.15 yamt KASSERT(&t->t_root == path_pptr(t, path, 0));
751 1.1 yamt lastidx = path->p_lastidx;
752 1.15 yamt KASSERT(lastidx == RADIX_TREE_INVALID_HEIGHT ||
753 1.15 yamt lastidx == t->t_height ||
754 1.15 yamt !entry_match_p(*path_pptr(t, path, lastidx), tagmask));
755 1.15 yamt nfound = 0;
756 1.15 yamt if (lastidx == RADIX_TREE_INVALID_HEIGHT) {
757 1.18 ad /*
758 1.18 ad * requested idx is beyond the right-most node.
759 1.18 ad */
760 1.18 ad if (reverse && !dense) {
761 1.15 yamt lastidx = 0;
762 1.15 yamt vpp = path_pptr(t, path, lastidx);
763 1.15 yamt goto descend;
764 1.15 yamt }
765 1.1 yamt return 0;
766 1.1 yamt }
767 1.1 yamt vpp = path_pptr(t, path, lastidx);
768 1.1 yamt while (/*CONSTCOND*/true) {
769 1.1 yamt struct radix_tree_node *n;
770 1.10 yamt unsigned int i;
771 1.1 yamt
772 1.1 yamt if (entry_match_p(*vpp, tagmask)) {
773 1.1 yamt KASSERT(lastidx == t->t_height);
774 1.1 yamt /*
775 1.15 yamt * record the matching non-NULL leaf.
776 1.1 yamt */
777 1.1 yamt results[nfound] = entry_ptr(*vpp);
778 1.1 yamt nfound++;
779 1.1 yamt if (nfound == maxresults) {
780 1.1 yamt return nfound;
781 1.1 yamt }
782 1.18 ad } else if (dense) {
783 1.18 ad return nfound;
784 1.1 yamt }
785 1.1 yamt scan_siblings:
786 1.1 yamt /*
787 1.15 yamt * try to find the next matching non-NULL sibling.
788 1.1 yamt */
789 1.15 yamt if (lastidx == 0) {
790 1.15 yamt /*
791 1.15 yamt * the root has no siblings.
792 1.15 yamt * we've done.
793 1.15 yamt */
794 1.15 yamt KASSERT(vpp == &t->t_root);
795 1.15 yamt break;
796 1.15 yamt }
797 1.1 yamt n = path_node(t, path, lastidx - 1);
798 1.1 yamt if (*vpp != NULL && n->n_nptrs == 1) {
799 1.1 yamt /*
800 1.15 yamt * optimization; if the node has only a single pointer
801 1.15 yamt * and we've already visited it, there's no point to
802 1.15 yamt * keep scanning in this node.
803 1.1 yamt */
804 1.1 yamt goto no_siblings;
805 1.1 yamt }
806 1.15 yamt for (i = vpp - n->n_ptrs + step; i != guard; i += step) {
807 1.15 yamt KASSERT(i < RADIX_TREE_PTR_PER_NODE);
808 1.1 yamt if (entry_match_p(n->n_ptrs[i], tagmask)) {
809 1.1 yamt vpp = &n->n_ptrs[i];
810 1.1 yamt break;
811 1.1 yamt }
812 1.1 yamt }
813 1.15 yamt if (i == guard) {
814 1.1 yamt no_siblings:
815 1.1 yamt /*
816 1.1 yamt * not found. go to parent.
817 1.1 yamt */
818 1.1 yamt lastidx--;
819 1.1 yamt vpp = path_pptr(t, path, lastidx);
820 1.1 yamt goto scan_siblings;
821 1.1 yamt }
822 1.15 yamt descend:
823 1.1 yamt /*
824 1.15 yamt * following the left-most (or right-most in the case of
825 1.15 yamt * reverse scan) child node, decend until reaching the leaf or
826 1.15 yamt * an non-matching entry.
827 1.1 yamt */
828 1.1 yamt while (entry_match_p(*vpp, tagmask) && lastidx < t->t_height) {
829 1.15 yamt /*
830 1.15 yamt * save vpp in the path so that we can come back to this
831 1.15 yamt * node after finishing visiting children.
832 1.15 yamt */
833 1.15 yamt path->p_refs[lastidx].pptr = vpp;
834 1.1 yamt n = entry_ptr(*vpp);
835 1.15 yamt vpp = &n->n_ptrs[first];
836 1.1 yamt lastidx++;
837 1.1 yamt }
838 1.1 yamt }
839 1.15 yamt return nfound;
840 1.1 yamt }
841 1.1 yamt
842 1.1 yamt /*
843 1.1 yamt * radix_tree_gang_lookup_node:
844 1.1 yamt *
845 1.18 ad * Scan the tree starting from the given index in the ascending order and
846 1.18 ad * return found nodes.
847 1.18 ad *
848 1.1 yamt * results should be an array large enough to hold maxresults pointers.
849 1.18 ad * This function returns the number of nodes found, up to maxresults.
850 1.18 ad * Returning less than maxresults means there are no more nodes in the tree.
851 1.1 yamt *
852 1.18 ad * If dense == true, this function stops scanning when it founds a hole of
853 1.18 ad * indexes. I.e. an index for which radix_tree_lookup_node would returns NULL.
854 1.18 ad * If dense == false, this function skips holes and continue scanning until
855 1.18 ad * maxresults nodes are found or it reaches the limit of the index range.
856 1.18 ad *
857 1.18 ad * The result of this function is semantically equivalent to what could be
858 1.1 yamt * obtained by repeated calls of radix_tree_lookup_node with increasing index.
859 1.18 ad * but this function is expected to be computationally cheaper when looking up
860 1.18 ad * multiple nodes at once. Especially, it's expected to be much cheaper when
861 1.18 ad * node indexes are distributed sparsely.
862 1.18 ad *
863 1.18 ad * Note that this function doesn't return index values of found nodes.
864 1.18 ad * Thus, in the case of dense == false, if index values are important for
865 1.18 ad * a caller, it's the caller's responsibility to check them, typically
866 1.18 ad * by examinining the returned nodes using some caller-specific knowledge
867 1.18 ad * about them.
868 1.18 ad * In the case of dense == true, a node returned via results[N] is always for
869 1.18 ad * the index (idx + N).
870 1.1 yamt */
871 1.1 yamt
872 1.1 yamt unsigned int
873 1.1 yamt radix_tree_gang_lookup_node(struct radix_tree *t, uint64_t idx,
874 1.18 ad void **results, unsigned int maxresults, bool dense)
875 1.1 yamt {
876 1.1 yamt struct radix_tree_path path;
877 1.1 yamt
878 1.1 yamt gang_lookup_init(t, idx, &path, 0);
879 1.18 ad return gang_lookup_scan(t, &path, results, maxresults, 0, false, dense);
880 1.15 yamt }
881 1.15 yamt
882 1.15 yamt /*
883 1.15 yamt * radix_tree_gang_lookup_node_reverse:
884 1.15 yamt *
885 1.18 ad * Same as radix_tree_gang_lookup_node except that this one scans the
886 1.18 ad * tree in the reverse order. I.e. descending index values.
887 1.15 yamt */
888 1.15 yamt
889 1.15 yamt unsigned int
890 1.15 yamt radix_tree_gang_lookup_node_reverse(struct radix_tree *t, uint64_t idx,
891 1.18 ad void **results, unsigned int maxresults, bool dense)
892 1.15 yamt {
893 1.15 yamt struct radix_tree_path path;
894 1.15 yamt
895 1.15 yamt gang_lookup_init(t, idx, &path, 0);
896 1.18 ad return gang_lookup_scan(t, &path, results, maxresults, 0, true, dense);
897 1.1 yamt }
898 1.1 yamt
899 1.1 yamt /*
900 1.1 yamt * radix_tree_gang_lookup_tagged_node:
901 1.1 yamt *
902 1.18 ad * Same as radix_tree_gang_lookup_node except that this one only returns
903 1.1 yamt * nodes tagged with tagid.
904 1.18 ad *
905 1.18 ad * It's illegal to call this function with tagmask 0.
906 1.1 yamt */
907 1.1 yamt
908 1.1 yamt unsigned int
909 1.1 yamt radix_tree_gang_lookup_tagged_node(struct radix_tree *t, uint64_t idx,
910 1.18 ad void **results, unsigned int maxresults, bool dense, unsigned int tagmask)
911 1.1 yamt {
912 1.1 yamt struct radix_tree_path path;
913 1.1 yamt
914 1.18 ad KASSERT(tagmask != 0);
915 1.1 yamt gang_lookup_init(t, idx, &path, tagmask);
916 1.18 ad return gang_lookup_scan(t, &path, results, maxresults, tagmask, false,
917 1.18 ad dense);
918 1.15 yamt }
919 1.15 yamt
920 1.15 yamt /*
921 1.15 yamt * radix_tree_gang_lookup_tagged_node_reverse:
922 1.15 yamt *
923 1.18 ad * Same as radix_tree_gang_lookup_tagged_node except that this one scans the
924 1.18 ad * tree in the reverse order. I.e. descending index values.
925 1.15 yamt */
926 1.15 yamt
927 1.15 yamt unsigned int
928 1.15 yamt radix_tree_gang_lookup_tagged_node_reverse(struct radix_tree *t, uint64_t idx,
929 1.18 ad void **results, unsigned int maxresults, bool dense, unsigned int tagmask)
930 1.15 yamt {
931 1.15 yamt struct radix_tree_path path;
932 1.15 yamt
933 1.18 ad KASSERT(tagmask != 0);
934 1.15 yamt gang_lookup_init(t, idx, &path, tagmask);
935 1.18 ad return gang_lookup_scan(t, &path, results, maxresults, tagmask, true,
936 1.18 ad dense);
937 1.1 yamt }
938 1.1 yamt
939 1.4 yamt /*
940 1.4 yamt * radix_tree_get_tag:
941 1.4 yamt *
942 1.18 ad * Return the tagmask for the node at the given index.
943 1.18 ad *
944 1.18 ad * It's illegal to call this function for a node which has not been inserted.
945 1.4 yamt */
946 1.4 yamt
947 1.18 ad unsigned int
948 1.18 ad radix_tree_get_tag(struct radix_tree *t, uint64_t idx, unsigned int tagmask)
949 1.1 yamt {
950 1.18 ad /*
951 1.18 ad * the following two implementations should behave same.
952 1.18 ad * the former one was chosen because it seems faster.
953 1.18 ad */
954 1.1 yamt #if 1
955 1.1 yamt void **vpp;
956 1.1 yamt
957 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, tagmask);
958 1.1 yamt if (vpp == NULL) {
959 1.1 yamt return false;
960 1.1 yamt }
961 1.1 yamt KASSERT(*vpp != NULL);
962 1.18 ad return (entry_tagmask(*vpp) & tagmask);
963 1.1 yamt #else
964 1.1 yamt void **vpp;
965 1.1 yamt
966 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
967 1.1 yamt KASSERT(vpp != NULL);
968 1.18 ad return (entry_tagmask(*vpp) & tagmask);
969 1.1 yamt #endif
970 1.1 yamt }
971 1.1 yamt
972 1.4 yamt /*
973 1.4 yamt * radix_tree_set_tag:
974 1.4 yamt *
975 1.18 ad * Set the tag for the node at the given index.
976 1.18 ad *
977 1.18 ad * It's illegal to call this function for a node which has not been inserted.
978 1.18 ad * It's illegal to call this function with tagmask 0.
979 1.4 yamt */
980 1.4 yamt
981 1.1 yamt void
982 1.18 ad radix_tree_set_tag(struct radix_tree *t, uint64_t idx, unsigned int tagmask)
983 1.1 yamt {
984 1.1 yamt struct radix_tree_path path;
985 1.18 ad void **vpp __unused;
986 1.1 yamt int i;
987 1.1 yamt
988 1.18 ad KASSERT(tagmask != 0);
989 1.18 ad #if defined(DIAGNOSTIC)
990 1.18 ad vpp =
991 1.18 ad #endif /* defined(DIAGNOSTIC) */
992 1.18 ad radix_tree_lookup_ptr(t, idx, &path, false, 0);
993 1.1 yamt KASSERT(vpp != NULL);
994 1.1 yamt KASSERT(*vpp != NULL);
995 1.1 yamt KASSERT(path.p_lastidx == t->t_height);
996 1.1 yamt KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
997 1.1 yamt for (i = t->t_height; i >= 0; i--) {
998 1.1 yamt void ** const pptr = (void **)path_pptr(t, &path, i);
999 1.1 yamt void *entry;
1000 1.1 yamt
1001 1.1 yamt KASSERT(pptr != NULL);
1002 1.1 yamt entry = *pptr;
1003 1.1 yamt if ((entry_tagmask(entry) & tagmask) != 0) {
1004 1.1 yamt break;
1005 1.1 yamt }
1006 1.1 yamt *pptr = (void *)((uintptr_t)entry | tagmask);
1007 1.1 yamt }
1008 1.1 yamt }
1009 1.1 yamt
1010 1.4 yamt /*
1011 1.4 yamt * radix_tree_clear_tag:
1012 1.4 yamt *
1013 1.18 ad * Clear the tag for the node at the given index.
1014 1.18 ad *
1015 1.18 ad * It's illegal to call this function for a node which has not been inserted.
1016 1.18 ad * It's illegal to call this function with tagmask 0.
1017 1.4 yamt */
1018 1.4 yamt
1019 1.1 yamt void
1020 1.18 ad radix_tree_clear_tag(struct radix_tree *t, uint64_t idx, unsigned int tagmask)
1021 1.1 yamt {
1022 1.1 yamt struct radix_tree_path path;
1023 1.1 yamt void **vpp;
1024 1.1 yamt int i;
1025 1.1 yamt
1026 1.18 ad KASSERT(tagmask != 0);
1027 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, &path, false, 0);
1028 1.1 yamt KASSERT(vpp != NULL);
1029 1.1 yamt KASSERT(*vpp != NULL);
1030 1.1 yamt KASSERT(path.p_lastidx == t->t_height);
1031 1.1 yamt KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
1032 1.7 yamt /*
1033 1.7 yamt * if already cleared, nothing to do
1034 1.7 yamt */
1035 1.1 yamt if ((entry_tagmask(*vpp) & tagmask) == 0) {
1036 1.1 yamt return;
1037 1.1 yamt }
1038 1.7 yamt /*
1039 1.7 yamt * clear the tag only if no children have the tag.
1040 1.7 yamt */
1041 1.1 yamt for (i = t->t_height; i >= 0; i--) {
1042 1.1 yamt void ** const pptr = (void **)path_pptr(t, &path, i);
1043 1.1 yamt void *entry;
1044 1.1 yamt
1045 1.1 yamt KASSERT(pptr != NULL);
1046 1.1 yamt entry = *pptr;
1047 1.1 yamt KASSERT((entry_tagmask(entry) & tagmask) != 0);
1048 1.1 yamt *pptr = entry_compose(entry_ptr(entry),
1049 1.1 yamt entry_tagmask(entry) & ~tagmask);
1050 1.7 yamt /*
1051 1.7 yamt * check if we should proceed to process the next level.
1052 1.7 yamt */
1053 1.7 yamt if (0 < i) {
1054 1.1 yamt struct radix_tree_node *n = path_node(t, &path, i - 1);
1055 1.1 yamt
1056 1.1 yamt if ((any_children_tagmask(n) & tagmask) != 0) {
1057 1.1 yamt break;
1058 1.1 yamt }
1059 1.1 yamt }
1060 1.1 yamt }
1061 1.1 yamt }
1062 1.1 yamt
1063 1.1 yamt #if defined(UNITTEST)
1064 1.1 yamt
1065 1.1 yamt #include <inttypes.h>
1066 1.1 yamt #include <stdio.h>
1067 1.1 yamt
1068 1.1 yamt static void
1069 1.1 yamt radix_tree_dump_node(const struct radix_tree *t, void *vp,
1070 1.1 yamt uint64_t offset, unsigned int height)
1071 1.1 yamt {
1072 1.1 yamt struct radix_tree_node *n;
1073 1.1 yamt unsigned int i;
1074 1.1 yamt
1075 1.1 yamt for (i = 0; i < t->t_height - height; i++) {
1076 1.1 yamt printf(" ");
1077 1.1 yamt }
1078 1.1 yamt if (entry_tagmask(vp) == 0) {
1079 1.1 yamt printf("[%" PRIu64 "] %p", offset, entry_ptr(vp));
1080 1.1 yamt } else {
1081 1.1 yamt printf("[%" PRIu64 "] %p (tagmask=0x%x)", offset, entry_ptr(vp),
1082 1.1 yamt entry_tagmask(vp));
1083 1.1 yamt }
1084 1.1 yamt if (height == 0) {
1085 1.1 yamt printf(" (leaf)\n");
1086 1.1 yamt return;
1087 1.1 yamt }
1088 1.1 yamt n = entry_ptr(vp);
1089 1.1 yamt assert(any_children_tagmask(n) == entry_tagmask(vp));
1090 1.1 yamt printf(" (%u children)\n", n->n_nptrs);
1091 1.1 yamt for (i = 0; i < __arraycount(n->n_ptrs); i++) {
1092 1.1 yamt void *c;
1093 1.1 yamt
1094 1.1 yamt c = n->n_ptrs[i];
1095 1.1 yamt if (c == NULL) {
1096 1.1 yamt continue;
1097 1.1 yamt }
1098 1.1 yamt radix_tree_dump_node(t, c,
1099 1.1 yamt offset + i * (UINT64_C(1) <<
1100 1.1 yamt (RADIX_TREE_BITS_PER_HEIGHT * (height - 1))), height - 1);
1101 1.1 yamt }
1102 1.1 yamt }
1103 1.1 yamt
1104 1.1 yamt void radix_tree_dump(const struct radix_tree *);
1105 1.1 yamt
1106 1.1 yamt void
1107 1.1 yamt radix_tree_dump(const struct radix_tree *t)
1108 1.1 yamt {
1109 1.1 yamt
1110 1.1 yamt printf("tree %p height=%u\n", t, t->t_height);
1111 1.1 yamt radix_tree_dump_node(t, t->t_root, 0, t->t_height);
1112 1.1 yamt }
1113 1.1 yamt
1114 1.1 yamt static void
1115 1.1 yamt test1(void)
1116 1.1 yamt {
1117 1.1 yamt struct radix_tree s;
1118 1.1 yamt struct radix_tree *t = &s;
1119 1.1 yamt void *results[3];
1120 1.1 yamt
1121 1.1 yamt radix_tree_init_tree(t);
1122 1.1 yamt radix_tree_dump(t);
1123 1.1 yamt assert(radix_tree_lookup_node(t, 0) == NULL);
1124 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == NULL);
1125 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 0);
1126 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 0);
1127 1.18 ad assert(radix_tree_gang_lookup_node(t, 1000, results, 3, false) == 0);
1128 1.18 ad assert(radix_tree_gang_lookup_node(t, 1000, results, 3, true) == 0);
1129 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, false) ==
1130 1.18 ad 0);
1131 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, true) ==
1132 1.18 ad 0);
1133 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, false)
1134 1.18 ad == 0);
1135 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, true)
1136 1.18 ad == 0);
1137 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, false, 1)
1138 1.18 ad == 0);
1139 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, true, 1)
1140 1.18 ad == 0);
1141 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 1000, results, 3, false, 1)
1142 1.18 ad == 0);
1143 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 1000, results, 3, true, 1)
1144 1.15 yamt == 0);
1145 1.18 ad assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
1146 1.18 ad false, 1) == 0);
1147 1.18 ad assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
1148 1.18 ad true, 1) == 0);
1149 1.15 yamt assert(radix_tree_gang_lookup_tagged_node_reverse(t, 1000, results, 3,
1150 1.18 ad false, 1) == 0);
1151 1.18 ad assert(radix_tree_gang_lookup_tagged_node_reverse(t, 1000, results, 3,
1152 1.18 ad true, 1) == 0);
1153 1.15 yamt assert(radix_tree_empty_tree_p(t));
1154 1.16 yamt assert(radix_tree_empty_tagged_tree_p(t, 1));
1155 1.18 ad assert(radix_tree_empty_tagged_tree_p(t, 2));
1156 1.15 yamt assert(radix_tree_insert_node(t, 0, (void *)0xdeadbea0) == 0);
1157 1.15 yamt assert(!radix_tree_empty_tree_p(t));
1158 1.16 yamt assert(radix_tree_empty_tagged_tree_p(t, 1));
1159 1.18 ad assert(radix_tree_empty_tagged_tree_p(t, 2));
1160 1.15 yamt assert(radix_tree_lookup_node(t, 0) == (void *)0xdeadbea0);
1161 1.15 yamt assert(radix_tree_lookup_node(t, 1000) == NULL);
1162 1.15 yamt memset(results, 0, sizeof(results));
1163 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 1);
1164 1.18 ad assert(results[0] == (void *)0xdeadbea0);
1165 1.18 ad memset(results, 0, sizeof(results));
1166 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 1);
1167 1.15 yamt assert(results[0] == (void *)0xdeadbea0);
1168 1.18 ad assert(radix_tree_gang_lookup_node(t, 1000, results, 3, false) == 0);
1169 1.18 ad assert(radix_tree_gang_lookup_node(t, 1000, results, 3, true) == 0);
1170 1.15 yamt memset(results, 0, sizeof(results));
1171 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, false) ==
1172 1.18 ad 1);
1173 1.15 yamt assert(results[0] == (void *)0xdeadbea0);
1174 1.15 yamt memset(results, 0, sizeof(results));
1175 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, true) ==
1176 1.18 ad 1);
1177 1.15 yamt assert(results[0] == (void *)0xdeadbea0);
1178 1.18 ad memset(results, 0, sizeof(results));
1179 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, false)
1180 1.18 ad == 1);
1181 1.18 ad assert(results[0] == (void *)0xdeadbea0);
1182 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, true)
1183 1.18 ad == 0);
1184 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, false, 1)
1185 1.15 yamt == 0);
1186 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, true, 1)
1187 1.15 yamt == 0);
1188 1.18 ad assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
1189 1.18 ad false, 1) == 0);
1190 1.18 ad assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
1191 1.18 ad true, 1) == 0);
1192 1.1 yamt assert(radix_tree_insert_node(t, 1000, (void *)0xdeadbea0) == 0);
1193 1.15 yamt assert(radix_tree_remove_node(t, 0) == (void *)0xdeadbea0);
1194 1.15 yamt assert(!radix_tree_empty_tree_p(t));
1195 1.1 yamt radix_tree_dump(t);
1196 1.15 yamt assert(radix_tree_lookup_node(t, 0) == NULL);
1197 1.15 yamt assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
1198 1.15 yamt memset(results, 0, sizeof(results));
1199 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 1);
1200 1.15 yamt assert(results[0] == (void *)0xdeadbea0);
1201 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 0);
1202 1.15 yamt memset(results, 0, sizeof(results));
1203 1.18 ad assert(radix_tree_gang_lookup_node(t, 1000, results, 3, false) == 1);
1204 1.15 yamt assert(results[0] == (void *)0xdeadbea0);
1205 1.15 yamt memset(results, 0, sizeof(results));
1206 1.18 ad assert(radix_tree_gang_lookup_node(t, 1000, results, 3, true) == 1);
1207 1.15 yamt assert(results[0] == (void *)0xdeadbea0);
1208 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, false)
1209 1.15 yamt == 0);
1210 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, true)
1211 1.15 yamt == 0);
1212 1.18 ad memset(results, 0, sizeof(results));
1213 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, false)
1214 1.18 ad == 1);
1215 1.18 ad memset(results, 0, sizeof(results));
1216 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, true)
1217 1.18 ad == 1);
1218 1.18 ad assert(results[0] == (void *)0xdeadbea0);
1219 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, false, 1)
1220 1.18 ad == 0);
1221 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, true, 1)
1222 1.18 ad == 0);
1223 1.18 ad assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
1224 1.18 ad false, 1) == 0);
1225 1.18 ad assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
1226 1.18 ad true, 1) == 0);
1227 1.18 ad assert(!radix_tree_get_tag(t, 1000, 1));
1228 1.18 ad assert(!radix_tree_get_tag(t, 1000, 2));
1229 1.18 ad assert(radix_tree_get_tag(t, 1000, 2 | 1) == 0);
1230 1.18 ad assert(radix_tree_empty_tagged_tree_p(t, 1));
1231 1.18 ad assert(radix_tree_empty_tagged_tree_p(t, 2));
1232 1.18 ad radix_tree_set_tag(t, 1000, 2);
1233 1.1 yamt assert(!radix_tree_get_tag(t, 1000, 1));
1234 1.18 ad assert(radix_tree_get_tag(t, 1000, 2));
1235 1.18 ad assert(radix_tree_get_tag(t, 1000, 2 | 1) == 2);
1236 1.16 yamt assert(radix_tree_empty_tagged_tree_p(t, 1));
1237 1.18 ad assert(!radix_tree_empty_tagged_tree_p(t, 2));
1238 1.1 yamt radix_tree_dump(t);
1239 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
1240 1.1 yamt assert(radix_tree_insert_node(t, 0, (void *)0xbea0) == 0);
1241 1.1 yamt radix_tree_dump(t);
1242 1.1 yamt assert(radix_tree_lookup_node(t, 0) == (void *)0xbea0);
1243 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
1244 1.1 yamt assert(radix_tree_insert_node(t, UINT64_C(10000000000), (void *)0xdea0)
1245 1.1 yamt == 0);
1246 1.1 yamt radix_tree_dump(t);
1247 1.1 yamt assert(radix_tree_lookup_node(t, 0) == (void *)0xbea0);
1248 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
1249 1.1 yamt assert(radix_tree_lookup_node(t, UINT64_C(10000000000)) ==
1250 1.1 yamt (void *)0xdea0);
1251 1.1 yamt radix_tree_dump(t);
1252 1.18 ad assert(!radix_tree_get_tag(t, 0, 2));
1253 1.18 ad assert(radix_tree_get_tag(t, 1000, 2));
1254 1.1 yamt assert(!radix_tree_get_tag(t, UINT64_C(10000000000), 1));
1255 1.18 ad radix_tree_set_tag(t, 0, 2);;
1256 1.18 ad radix_tree_set_tag(t, UINT64_C(10000000000), 2);
1257 1.1 yamt radix_tree_dump(t);
1258 1.18 ad assert(radix_tree_get_tag(t, 0, 2));
1259 1.18 ad assert(radix_tree_get_tag(t, 1000, 2));
1260 1.18 ad assert(radix_tree_get_tag(t, UINT64_C(10000000000), 2));
1261 1.18 ad radix_tree_clear_tag(t, 0, 2);;
1262 1.18 ad radix_tree_clear_tag(t, UINT64_C(10000000000), 2);
1263 1.1 yamt radix_tree_dump(t);
1264 1.18 ad assert(!radix_tree_get_tag(t, 0, 2));
1265 1.18 ad assert(radix_tree_get_tag(t, 1000, 2));
1266 1.18 ad assert(!radix_tree_get_tag(t, UINT64_C(10000000000), 2));
1267 1.1 yamt radix_tree_dump(t);
1268 1.1 yamt assert(radix_tree_replace_node(t, 1000, (void *)0x12345678) ==
1269 1.1 yamt (void *)0xdeadbea0);
1270 1.18 ad assert(!radix_tree_get_tag(t, 1000, 1));
1271 1.18 ad assert(radix_tree_get_tag(t, 1000, 2));
1272 1.18 ad assert(radix_tree_get_tag(t, 1000, 2 | 1) == 2);
1273 1.18 ad memset(results, 0, sizeof(results));
1274 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 3);
1275 1.1 yamt assert(results[0] == (void *)0xbea0);
1276 1.1 yamt assert(results[1] == (void *)0x12345678);
1277 1.1 yamt assert(results[2] == (void *)0xdea0);
1278 1.18 ad memset(results, 0, sizeof(results));
1279 1.18 ad assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 1);
1280 1.18 ad assert(results[0] == (void *)0xbea0);
1281 1.18 ad memset(results, 0, sizeof(results));
1282 1.18 ad assert(radix_tree_gang_lookup_node(t, 1, results, 3, false) == 2);
1283 1.1 yamt assert(results[0] == (void *)0x12345678);
1284 1.1 yamt assert(results[1] == (void *)0xdea0);
1285 1.18 ad assert(radix_tree_gang_lookup_node(t, 1, results, 3, true) == 0);
1286 1.18 ad memset(results, 0, sizeof(results));
1287 1.18 ad assert(radix_tree_gang_lookup_node(t, 1001, results, 3, false) == 1);
1288 1.1 yamt assert(results[0] == (void *)0xdea0);
1289 1.18 ad assert(radix_tree_gang_lookup_node(t, 1001, results, 3, true) == 0);
1290 1.18 ad assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000001), results, 3,
1291 1.18 ad false) == 0);
1292 1.18 ad assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000001), results, 3,
1293 1.18 ad true) == 0);
1294 1.18 ad assert(radix_tree_gang_lookup_node(t, UINT64_C(1000000000000), results,
1295 1.18 ad 3, false) == 0);
1296 1.1 yamt assert(radix_tree_gang_lookup_node(t, UINT64_C(1000000000000), results,
1297 1.18 ad 3, true) == 0);
1298 1.18 ad memset(results, 0, sizeof(results));
1299 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 100, false, 2)
1300 1.18 ad == 1);
1301 1.1 yamt assert(results[0] == (void *)0x12345678);
1302 1.18 ad assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 100, true, 2)
1303 1.18 ad == 0);
1304 1.1 yamt assert(entry_tagmask(t->t_root) != 0);
1305 1.1 yamt assert(radix_tree_remove_node(t, 1000) == (void *)0x12345678);
1306 1.1 yamt assert(entry_tagmask(t->t_root) == 0);
1307 1.1 yamt radix_tree_dump(t);
1308 1.18 ad assert(radix_tree_insert_node(t, UINT64_C(10000000001), (void *)0xfff0)
1309 1.18 ad == 0);
1310 1.18 ad memset(results, 0, sizeof(results));
1311 1.18 ad assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000000), results, 3,
1312 1.18 ad false) == 2);
1313 1.18 ad assert(results[0] == (void *)0xdea0);
1314 1.18 ad assert(results[1] == (void *)0xfff0);
1315 1.18 ad memset(results, 0, sizeof(results));
1316 1.18 ad assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000000), results, 3,
1317 1.18 ad true) == 2);
1318 1.18 ad assert(results[0] == (void *)0xdea0);
1319 1.18 ad assert(results[1] == (void *)0xfff0);
1320 1.18 ad memset(results, 0, sizeof(results));
1321 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, UINT64_C(10000000001),
1322 1.18 ad results, 3, false) == 3);
1323 1.18 ad assert(results[0] == (void *)0xfff0);
1324 1.18 ad assert(results[1] == (void *)0xdea0);
1325 1.18 ad assert(results[2] == (void *)0xbea0);
1326 1.18 ad memset(results, 0, sizeof(results));
1327 1.18 ad assert(radix_tree_gang_lookup_node_reverse(t, UINT64_C(10000000001),
1328 1.18 ad results, 3, true) == 2);
1329 1.18 ad assert(results[0] == (void *)0xfff0);
1330 1.18 ad assert(results[1] == (void *)0xdea0);
1331 1.1 yamt assert(radix_tree_remove_node(t, UINT64_C(10000000000)) ==
1332 1.1 yamt (void *)0xdea0);
1333 1.18 ad assert(radix_tree_remove_node(t, UINT64_C(10000000001)) ==
1334 1.18 ad (void *)0xfff0);
1335 1.1 yamt radix_tree_dump(t);
1336 1.1 yamt assert(radix_tree_remove_node(t, 0) == (void *)0xbea0);
1337 1.1 yamt radix_tree_dump(t);
1338 1.1 yamt radix_tree_fini_tree(t);
1339 1.1 yamt }
1340 1.1 yamt
1341 1.1 yamt #include <sys/time.h>
1342 1.1 yamt
1343 1.1 yamt struct testnode {
1344 1.1 yamt uint64_t idx;
1345 1.12 yamt bool tagged[RADIX_TREE_TAG_ID_MAX];
1346 1.1 yamt };
1347 1.1 yamt
1348 1.1 yamt static void
1349 1.11 yamt printops(const char *title, const char *name, int tag, unsigned int n,
1350 1.11 yamt const struct timeval *stv, const struct timeval *etv)
1351 1.1 yamt {
1352 1.1 yamt uint64_t s = stv->tv_sec * 1000000 + stv->tv_usec;
1353 1.1 yamt uint64_t e = etv->tv_sec * 1000000 + etv->tv_usec;
1354 1.1 yamt
1355 1.11 yamt printf("RESULT %s %s %d %lf op/s\n", title, name, tag,
1356 1.11 yamt (double)n / (e - s) * 1000000);
1357 1.1 yamt }
1358 1.1 yamt
1359 1.1 yamt #define TEST2_GANG_LOOKUP_NODES 16
1360 1.1 yamt
1361 1.1 yamt static bool
1362 1.18 ad test2_should_tag(unsigned int i, unsigned int tagid)
1363 1.1 yamt {
1364 1.1 yamt
1365 1.1 yamt if (tagid == 0) {
1366 1.18 ad return (i % 4) == 0; /* 25% */
1367 1.1 yamt } else {
1368 1.11 yamt return (i % 7) == 0; /* 14% */
1369 1.1 yamt }
1370 1.18 ad return 1;
1371 1.18 ad }
1372 1.18 ad
1373 1.18 ad static void
1374 1.18 ad check_tag_count(const unsigned int *ntagged, unsigned int tagmask,
1375 1.18 ad unsigned int count)
1376 1.18 ad {
1377 1.18 ad unsigned int tag;
1378 1.18 ad
1379 1.18 ad for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1380 1.18 ad if ((tagmask & (1 << tag)) == 0) {
1381 1.18 ad continue;
1382 1.18 ad }
1383 1.18 ad if (((tagmask - 1) & tagmask) == 0) {
1384 1.18 ad assert(count == ntagged[tag]);
1385 1.18 ad } else {
1386 1.18 ad assert(count >= ntagged[tag]);
1387 1.18 ad }
1388 1.18 ad }
1389 1.1 yamt }
1390 1.1 yamt
1391 1.1 yamt static void
1392 1.11 yamt test2(const char *title, bool dense)
1393 1.1 yamt {
1394 1.1 yamt struct radix_tree s;
1395 1.1 yamt struct radix_tree *t = &s;
1396 1.1 yamt struct testnode *n;
1397 1.1 yamt unsigned int i;
1398 1.1 yamt unsigned int nnodes = 100000;
1399 1.1 yamt unsigned int removed;
1400 1.18 ad unsigned int tag;
1401 1.18 ad unsigned int tagmask;
1402 1.1 yamt unsigned int ntagged[RADIX_TREE_TAG_ID_MAX];
1403 1.1 yamt struct testnode *nodes;
1404 1.1 yamt struct timeval stv;
1405 1.1 yamt struct timeval etv;
1406 1.1 yamt
1407 1.1 yamt nodes = malloc(nnodes * sizeof(*nodes));
1408 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1409 1.1 yamt ntagged[tag] = 0;
1410 1.1 yamt }
1411 1.1 yamt radix_tree_init_tree(t);
1412 1.1 yamt for (i = 0; i < nnodes; i++) {
1413 1.1 yamt n = &nodes[i];
1414 1.1 yamt n->idx = random();
1415 1.1 yamt if (sizeof(long) == 4) {
1416 1.1 yamt n->idx <<= 32;
1417 1.1 yamt n->idx |= (uint32_t)random();
1418 1.1 yamt }
1419 1.1 yamt if (dense) {
1420 1.1 yamt n->idx %= nnodes * 2;
1421 1.1 yamt }
1422 1.1 yamt while (radix_tree_lookup_node(t, n->idx) != NULL) {
1423 1.1 yamt n->idx++;
1424 1.1 yamt }
1425 1.1 yamt radix_tree_insert_node(t, n->idx, n);
1426 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1427 1.18 ad tagmask = 1 << tag;
1428 1.18 ad
1429 1.12 yamt n->tagged[tag] = test2_should_tag(i, tag);
1430 1.12 yamt if (n->tagged[tag]) {
1431 1.18 ad radix_tree_set_tag(t, n->idx, tagmask);
1432 1.1 yamt ntagged[tag]++;
1433 1.1 yamt }
1434 1.18 ad assert((n->tagged[tag] ? tagmask : 0) ==
1435 1.18 ad radix_tree_get_tag(t, n->idx, tagmask));
1436 1.1 yamt }
1437 1.1 yamt }
1438 1.1 yamt
1439 1.1 yamt gettimeofday(&stv, NULL);
1440 1.1 yamt for (i = 0; i < nnodes; i++) {
1441 1.1 yamt n = &nodes[i];
1442 1.1 yamt assert(radix_tree_lookup_node(t, n->idx) == n);
1443 1.1 yamt }
1444 1.1 yamt gettimeofday(&etv, NULL);
1445 1.11 yamt printops(title, "lookup", 0, nnodes, &stv, &etv);
1446 1.1 yamt
1447 1.18 ad for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
1448 1.12 yamt unsigned int count = 0;
1449 1.12 yamt
1450 1.1 yamt gettimeofday(&stv, NULL);
1451 1.1 yamt for (i = 0; i < nnodes; i++) {
1452 1.18 ad unsigned int tagged;
1453 1.12 yamt
1454 1.1 yamt n = &nodes[i];
1455 1.18 ad tagged = radix_tree_get_tag(t, n->idx, tagmask);
1456 1.18 ad assert((tagged & ~tagmask) == 0);
1457 1.18 ad for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1458 1.18 ad assert((tagmask & (1 << tag)) == 0 ||
1459 1.18 ad n->tagged[tag] == !!(tagged & (1 << tag)));
1460 1.18 ad }
1461 1.12 yamt if (tagged) {
1462 1.12 yamt count++;
1463 1.12 yamt }
1464 1.1 yamt }
1465 1.1 yamt gettimeofday(&etv, NULL);
1466 1.18 ad check_tag_count(ntagged, tagmask, count);
1467 1.18 ad printops(title, "get_tag", tagmask, nnodes, &stv, &etv);
1468 1.1 yamt }
1469 1.1 yamt
1470 1.1 yamt gettimeofday(&stv, NULL);
1471 1.1 yamt for (i = 0; i < nnodes; i++) {
1472 1.1 yamt n = &nodes[i];
1473 1.1 yamt radix_tree_remove_node(t, n->idx);
1474 1.1 yamt }
1475 1.1 yamt gettimeofday(&etv, NULL);
1476 1.11 yamt printops(title, "remove", 0, nnodes, &stv, &etv);
1477 1.1 yamt
1478 1.1 yamt gettimeofday(&stv, NULL);
1479 1.1 yamt for (i = 0; i < nnodes; i++) {
1480 1.1 yamt n = &nodes[i];
1481 1.1 yamt radix_tree_insert_node(t, n->idx, n);
1482 1.1 yamt }
1483 1.1 yamt gettimeofday(&etv, NULL);
1484 1.11 yamt printops(title, "insert", 0, nnodes, &stv, &etv);
1485 1.1 yamt
1486 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1487 1.18 ad tagmask = 1 << tag;
1488 1.18 ad
1489 1.1 yamt ntagged[tag] = 0;
1490 1.1 yamt gettimeofday(&stv, NULL);
1491 1.1 yamt for (i = 0; i < nnodes; i++) {
1492 1.1 yamt n = &nodes[i];
1493 1.12 yamt if (n->tagged[tag]) {
1494 1.18 ad radix_tree_set_tag(t, n->idx, tagmask);
1495 1.1 yamt ntagged[tag]++;
1496 1.1 yamt }
1497 1.1 yamt }
1498 1.1 yamt gettimeofday(&etv, NULL);
1499 1.11 yamt printops(title, "set_tag", tag, ntagged[tag], &stv, &etv);
1500 1.1 yamt }
1501 1.1 yamt
1502 1.1 yamt gettimeofday(&stv, NULL);
1503 1.1 yamt {
1504 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1505 1.1 yamt uint64_t nextidx;
1506 1.1 yamt unsigned int nfound;
1507 1.1 yamt unsigned int total;
1508 1.1 yamt
1509 1.1 yamt nextidx = 0;
1510 1.1 yamt total = 0;
1511 1.1 yamt while ((nfound = radix_tree_gang_lookup_node(t, nextidx,
1512 1.18 ad (void *)results, __arraycount(results), false)) > 0) {
1513 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1514 1.1 yamt total += nfound;
1515 1.15 yamt if (nextidx == 0) {
1516 1.15 yamt break;
1517 1.15 yamt }
1518 1.1 yamt }
1519 1.1 yamt assert(total == nnodes);
1520 1.1 yamt }
1521 1.1 yamt gettimeofday(&etv, NULL);
1522 1.11 yamt printops(title, "ganglookup", 0, nnodes, &stv, &etv);
1523 1.1 yamt
1524 1.15 yamt gettimeofday(&stv, NULL);
1525 1.15 yamt {
1526 1.15 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1527 1.15 yamt uint64_t nextidx;
1528 1.15 yamt unsigned int nfound;
1529 1.15 yamt unsigned int total;
1530 1.15 yamt
1531 1.15 yamt nextidx = UINT64_MAX;
1532 1.15 yamt total = 0;
1533 1.15 yamt while ((nfound = radix_tree_gang_lookup_node_reverse(t, nextidx,
1534 1.18 ad (void *)results, __arraycount(results), false)) > 0) {
1535 1.15 yamt nextidx = results[nfound - 1]->idx - 1;
1536 1.15 yamt total += nfound;
1537 1.15 yamt if (nextidx == UINT64_MAX) {
1538 1.15 yamt break;
1539 1.15 yamt }
1540 1.15 yamt }
1541 1.15 yamt assert(total == nnodes);
1542 1.15 yamt }
1543 1.15 yamt gettimeofday(&etv, NULL);
1544 1.15 yamt printops(title, "ganglookup_reverse", 0, nnodes, &stv, &etv);
1545 1.15 yamt
1546 1.18 ad for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
1547 1.18 ad unsigned int total = 0;
1548 1.18 ad
1549 1.1 yamt gettimeofday(&stv, NULL);
1550 1.1 yamt {
1551 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1552 1.1 yamt uint64_t nextidx;
1553 1.1 yamt unsigned int nfound;
1554 1.1 yamt
1555 1.1 yamt nextidx = 0;
1556 1.1 yamt while ((nfound = radix_tree_gang_lookup_tagged_node(t,
1557 1.1 yamt nextidx, (void *)results, __arraycount(results),
1558 1.18 ad false, tagmask)) > 0) {
1559 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1560 1.1 yamt total += nfound;
1561 1.1 yamt }
1562 1.1 yamt }
1563 1.1 yamt gettimeofday(&etv, NULL);
1564 1.18 ad check_tag_count(ntagged, tagmask, total);
1565 1.18 ad assert(tagmask != 0 || total == 0);
1566 1.18 ad printops(title, "ganglookup_tag", tagmask, total, &stv, &etv);
1567 1.1 yamt }
1568 1.1 yamt
1569 1.18 ad for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
1570 1.18 ad unsigned int total = 0;
1571 1.18 ad
1572 1.15 yamt gettimeofday(&stv, NULL);
1573 1.15 yamt {
1574 1.15 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1575 1.15 yamt uint64_t nextidx;
1576 1.15 yamt unsigned int nfound;
1577 1.15 yamt
1578 1.15 yamt nextidx = UINT64_MAX;
1579 1.15 yamt while ((nfound =
1580 1.15 yamt radix_tree_gang_lookup_tagged_node_reverse(t,
1581 1.15 yamt nextidx, (void *)results, __arraycount(results),
1582 1.18 ad false, tagmask)) > 0) {
1583 1.15 yamt nextidx = results[nfound - 1]->idx - 1;
1584 1.15 yamt total += nfound;
1585 1.15 yamt if (nextidx == UINT64_MAX) {
1586 1.15 yamt break;
1587 1.15 yamt }
1588 1.15 yamt }
1589 1.15 yamt }
1590 1.15 yamt gettimeofday(&etv, NULL);
1591 1.18 ad check_tag_count(ntagged, tagmask, total);
1592 1.18 ad assert(tagmask != 0 || total == 0);
1593 1.18 ad printops(title, "ganglookup_tag_reverse", tagmask, total,
1594 1.15 yamt &stv, &etv);
1595 1.15 yamt }
1596 1.15 yamt
1597 1.1 yamt removed = 0;
1598 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1599 1.1 yamt unsigned int total;
1600 1.1 yamt
1601 1.1 yamt total = 0;
1602 1.18 ad tagmask = 1 << tag;
1603 1.1 yamt gettimeofday(&stv, NULL);
1604 1.1 yamt {
1605 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1606 1.1 yamt uint64_t nextidx;
1607 1.1 yamt unsigned int nfound;
1608 1.1 yamt
1609 1.1 yamt nextidx = 0;
1610 1.1 yamt while ((nfound = radix_tree_gang_lookup_tagged_node(t,
1611 1.1 yamt nextidx, (void *)results, __arraycount(results),
1612 1.18 ad false, tagmask)) > 0) {
1613 1.1 yamt for (i = 0; i < nfound; i++) {
1614 1.1 yamt radix_tree_remove_node(t,
1615 1.1 yamt results[i]->idx);
1616 1.1 yamt }
1617 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1618 1.1 yamt total += nfound;
1619 1.15 yamt if (nextidx == 0) {
1620 1.15 yamt break;
1621 1.15 yamt }
1622 1.1 yamt }
1623 1.18 ad }
1624 1.18 ad gettimeofday(&etv, NULL);
1625 1.18 ad if (tag == 0) {
1626 1.18 ad check_tag_count(ntagged, tagmask, total);
1627 1.18 ad } else {
1628 1.1 yamt assert(total <= ntagged[tag]);
1629 1.1 yamt }
1630 1.18 ad printops(title, "ganglookup_tag+remove", tagmask, total, &stv,
1631 1.11 yamt &etv);
1632 1.1 yamt removed += total;
1633 1.1 yamt }
1634 1.1 yamt
1635 1.1 yamt gettimeofday(&stv, NULL);
1636 1.1 yamt {
1637 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1638 1.1 yamt uint64_t nextidx;
1639 1.1 yamt unsigned int nfound;
1640 1.1 yamt unsigned int total;
1641 1.1 yamt
1642 1.1 yamt nextidx = 0;
1643 1.1 yamt total = 0;
1644 1.1 yamt while ((nfound = radix_tree_gang_lookup_node(t, nextidx,
1645 1.18 ad (void *)results, __arraycount(results), false)) > 0) {
1646 1.1 yamt for (i = 0; i < nfound; i++) {
1647 1.1 yamt assert(results[i] == radix_tree_remove_node(t,
1648 1.1 yamt results[i]->idx));
1649 1.1 yamt }
1650 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1651 1.1 yamt total += nfound;
1652 1.15 yamt if (nextidx == 0) {
1653 1.15 yamt break;
1654 1.15 yamt }
1655 1.1 yamt }
1656 1.1 yamt assert(total == nnodes - removed);
1657 1.1 yamt }
1658 1.1 yamt gettimeofday(&etv, NULL);
1659 1.11 yamt printops(title, "ganglookup+remove", 0, nnodes - removed, &stv, &etv);
1660 1.1 yamt
1661 1.16 yamt assert(radix_tree_empty_tree_p(t));
1662 1.18 ad for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
1663 1.18 ad assert(radix_tree_empty_tagged_tree_p(t, tagmask));
1664 1.18 ad }
1665 1.1 yamt radix_tree_fini_tree(t);
1666 1.1 yamt free(nodes);
1667 1.1 yamt }
1668 1.1 yamt
1669 1.1 yamt int
1670 1.1 yamt main(int argc, char *argv[])
1671 1.1 yamt {
1672 1.1 yamt
1673 1.1 yamt test1();
1674 1.11 yamt test2("dense", true);
1675 1.11 yamt test2("sparse", false);
1676 1.1 yamt return 0;
1677 1.1 yamt }
1678 1.1 yamt
1679 1.1 yamt #endif /* defined(UNITTEST) */
1680