shmem: Convert find_swap_entry to XArray
[linux/fpc-iii.git] / tools / testing / radix-tree / test.c
blob470419bfd49d1898ae8aedfe2257f94972c4c2f3
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <stdio.h>
5 #include <linux/types.h>
6 #include <linux/kernel.h>
7 #include <linux/bitops.h>
9 #include "test.h"
11 struct item *
12 item_tag_set(struct radix_tree_root *root, unsigned long index, int tag)
14 return radix_tree_tag_set(root, index, tag);
17 struct item *
18 item_tag_clear(struct radix_tree_root *root, unsigned long index, int tag)
20 return radix_tree_tag_clear(root, index, tag);
23 int item_tag_get(struct radix_tree_root *root, unsigned long index, int tag)
25 return radix_tree_tag_get(root, index, tag);
28 int __item_insert(struct radix_tree_root *root, struct item *item)
30 return __radix_tree_insert(root, item->index, item->order, item);
33 struct item *item_create(unsigned long index, unsigned int order)
35 struct item *ret = malloc(sizeof(*ret));
37 ret->index = index;
38 ret->order = order;
39 return ret;
42 int item_insert_order(struct radix_tree_root *root, unsigned long index,
43 unsigned order)
45 struct item *item = item_create(index, order);
46 int err = __item_insert(root, item);
47 if (err)
48 free(item);
49 return err;
52 int item_insert(struct radix_tree_root *root, unsigned long index)
54 return item_insert_order(root, index, 0);
57 void item_sanity(struct item *item, unsigned long index)
59 unsigned long mask;
60 assert(!radix_tree_is_internal_node(item));
61 assert(item->order < BITS_PER_LONG);
62 mask = (1UL << item->order) - 1;
63 assert((item->index | mask) == (index | mask));
66 int item_delete(struct radix_tree_root *root, unsigned long index)
68 struct item *item = radix_tree_delete(root, index);
70 if (item) {
71 item_sanity(item, index);
72 free(item);
73 return 1;
75 return 0;
78 static void item_free_rcu(struct rcu_head *head)
80 struct item *item = container_of(head, struct item, rcu_head);
82 free(item);
85 int item_delete_rcu(struct radix_tree_root *root, unsigned long index)
87 struct item *item = radix_tree_delete(root, index);
89 if (item) {
90 item_sanity(item, index);
91 call_rcu(&item->rcu_head, item_free_rcu);
92 return 1;
94 return 0;
97 void item_check_present(struct radix_tree_root *root, unsigned long index)
99 struct item *item;
101 item = radix_tree_lookup(root, index);
102 assert(item != NULL);
103 item_sanity(item, index);
106 struct item *item_lookup(struct radix_tree_root *root, unsigned long index)
108 return radix_tree_lookup(root, index);
111 void item_check_absent(struct radix_tree_root *root, unsigned long index)
113 struct item *item;
115 item = radix_tree_lookup(root, index);
116 assert(item == NULL);
120 * Scan only the passed (start, start+nr] for present items
122 void item_gang_check_present(struct radix_tree_root *root,
123 unsigned long start, unsigned long nr,
124 int chunk, int hop)
126 struct item *items[chunk];
127 unsigned long into;
129 for (into = 0; into < nr; ) {
130 int nfound;
131 int nr_to_find = chunk;
132 int i;
134 if (nr_to_find > (nr - into))
135 nr_to_find = nr - into;
137 nfound = radix_tree_gang_lookup(root, (void **)items,
138 start + into, nr_to_find);
139 assert(nfound == nr_to_find);
140 for (i = 0; i < nfound; i++)
141 assert(items[i]->index == start + into + i);
142 into += hop;
147 * Scan the entire tree, only expecting present items (start, start+nr]
149 void item_full_scan(struct radix_tree_root *root, unsigned long start,
150 unsigned long nr, int chunk)
152 struct item *items[chunk];
153 unsigned long into = 0;
154 unsigned long this_index = start;
155 int nfound;
156 int i;
158 // printf("%s(0x%08lx, 0x%08lx, %d)\n", __FUNCTION__, start, nr, chunk);
160 while ((nfound = radix_tree_gang_lookup(root, (void **)items, into,
161 chunk))) {
162 // printf("At 0x%08lx, nfound=%d\n", into, nfound);
163 for (i = 0; i < nfound; i++) {
164 assert(items[i]->index == this_index);
165 this_index++;
167 // printf("Found 0x%08lx->0x%08lx\n",
168 // items[0]->index, items[nfound-1]->index);
169 into = this_index;
171 if (chunk)
172 assert(this_index == start + nr);
173 nfound = radix_tree_gang_lookup(root, (void **)items,
174 this_index, chunk);
175 assert(nfound == 0);
178 /* Use the same pattern as tag_pages_for_writeback() in mm/page-writeback.c */
179 int tag_tagged_items(struct radix_tree_root *root, pthread_mutex_t *lock,
180 unsigned long start, unsigned long end, unsigned batch,
181 unsigned iftag, unsigned thentag)
183 unsigned long tagged = 0;
184 struct radix_tree_iter iter;
185 void **slot;
187 if (batch == 0)
188 batch = 1;
190 if (lock)
191 pthread_mutex_lock(lock);
192 radix_tree_for_each_tagged(slot, root, &iter, start, iftag) {
193 if (iter.index > end)
194 break;
195 radix_tree_iter_tag_set(root, &iter, thentag);
196 tagged++;
197 if ((tagged % batch) != 0)
198 continue;
199 slot = radix_tree_iter_resume(slot, &iter);
200 if (lock) {
201 pthread_mutex_unlock(lock);
202 rcu_barrier();
203 pthread_mutex_lock(lock);
206 if (lock)
207 pthread_mutex_unlock(lock);
209 return tagged;
212 static int verify_node(struct radix_tree_node *slot, unsigned int tag,
213 int tagged)
215 int anyset = 0;
216 int i;
217 int j;
219 slot = entry_to_node(slot);
221 /* Verify consistency at this level */
222 for (i = 0; i < RADIX_TREE_TAG_LONGS; i++) {
223 if (slot->tags[tag][i]) {
224 anyset = 1;
225 break;
228 if (tagged != anyset) {
229 printf("tag: %u, shift %u, tagged: %d, anyset: %d\n",
230 tag, slot->shift, tagged, anyset);
231 for (j = 0; j < RADIX_TREE_MAX_TAGS; j++) {
232 printf("tag %d: ", j);
233 for (i = 0; i < RADIX_TREE_TAG_LONGS; i++)
234 printf("%016lx ", slot->tags[j][i]);
235 printf("\n");
237 return 1;
239 assert(tagged == anyset);
241 /* Go for next level */
242 if (slot->shift > 0) {
243 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
244 if (slot->slots[i])
245 if (verify_node(slot->slots[i], tag,
246 !!test_bit(i, slot->tags[tag]))) {
247 printf("Failure at off %d\n", i);
248 for (j = 0; j < RADIX_TREE_MAX_TAGS; j++) {
249 printf("tag %d: ", j);
250 for (i = 0; i < RADIX_TREE_TAG_LONGS; i++)
251 printf("%016lx ", slot->tags[j][i]);
252 printf("\n");
254 return 1;
257 return 0;
260 void verify_tag_consistency(struct radix_tree_root *root, unsigned int tag)
262 struct radix_tree_node *node = root->xa_head;
263 if (!radix_tree_is_internal_node(node))
264 return;
265 verify_node(node, tag, !!root_tag_get(root, tag));
268 void item_kill_tree(struct radix_tree_root *root)
270 struct radix_tree_iter iter;
271 void **slot;
272 struct item *items[32];
273 int nfound;
275 radix_tree_for_each_slot(slot, root, &iter, 0) {
276 if (xa_is_value(*slot))
277 radix_tree_delete(root, iter.index);
280 while ((nfound = radix_tree_gang_lookup(root, (void **)items, 0, 32))) {
281 int i;
283 for (i = 0; i < nfound; i++) {
284 void *ret;
286 ret = radix_tree_delete(root, items[i]->index);
287 assert(ret == items[i]);
288 free(items[i]);
291 assert(radix_tree_gang_lookup(root, (void **)items, 0, 32) == 0);
292 assert(root->xa_head == NULL);
295 void tree_verify_min_height(struct radix_tree_root *root, int maxindex)
297 unsigned shift;
298 struct radix_tree_node *node = root->xa_head;
299 if (!radix_tree_is_internal_node(node)) {
300 assert(maxindex == 0);
301 return;
304 node = entry_to_node(node);
305 assert(maxindex <= node_maxindex(node));
307 shift = node->shift;
308 if (shift > 0)
309 assert(maxindex > shift_maxindex(shift - RADIX_TREE_MAP_SHIFT));
310 else
311 assert(maxindex > 0);