Commit | Line | Data |
---|---|---|
ce29b371 MJ |
1 | // SPDX-FileCopyrightText: 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // | |
3 | // SPDX-License-Identifier: GPL-2.0-or-later | |
4 | ||
18ca7a5b | 5 | /* |
18ca7a5b | 6 | * Userspace RCU library - test program |
18ca7a5b MD |
7 | */ |
8 | ||
18ca7a5b MD |
9 | #include "test_urcu_hash.h" |
10 | ||
11 | enum urcu_hash_addremove { | |
12 | AR_RANDOM = 0, | |
13 | AR_ADD = 1, | |
14 | AR_REMOVE = -1, | |
15 | }; /* 1: add, -1 remove, 0: random */ | |
16 | ||
17 | static enum urcu_hash_addremove addremove; /* 1: add, -1 remove, 0: random */ | |
18 | ||
70469b43 | 19 | void test_hash_rw_sigusr1_handler(int signo __attribute__((unused))) |
18ca7a5b MD |
20 | { |
21 | switch (addremove) { | |
22 | case AR_ADD: | |
23 | printf("Add/Remove: random.\n"); | |
24 | addremove = AR_RANDOM; | |
25 | break; | |
26 | case AR_RANDOM: | |
27 | printf("Add/Remove: remove only.\n"); | |
28 | addremove = AR_REMOVE; | |
29 | break; | |
30 | case AR_REMOVE: | |
31 | printf("Add/Remove: add only.\n"); | |
32 | addremove = AR_ADD; | |
33 | break; | |
34 | } | |
35 | } | |
36 | ||
70469b43 | 37 | void test_hash_rw_sigusr2_handler(int signo __attribute__((unused))) |
18ca7a5b MD |
38 | { |
39 | char msg[1] = { 0x42 }; | |
40 | ssize_t ret; | |
41 | ||
42 | do { | |
43 | ret = write(count_pipe[1], msg, 1); /* wakeup thread */ | |
44 | } while (ret == -1L && errno == EINTR); | |
45 | } | |
46 | ||
47 | void *test_hash_rw_thr_reader(void *_count) | |
48 | { | |
49 | unsigned long long *count = _count; | |
50 | struct lfht_test_node *node; | |
51 | struct cds_lfht_iter iter; | |
52 | ||
94df6318 MD |
53 | printf_verbose("thread_begin %s, tid %lu\n", |
54 | "reader", urcu_get_thread_id()); | |
18ca7a5b | 55 | |
98f483d2 MD |
56 | URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL); |
57 | ||
18ca7a5b MD |
58 | set_affinity(); |
59 | ||
60 | rcu_register_thread(); | |
61 | ||
62 | while (!test_go) | |
63 | { | |
64 | } | |
65 | cmm_smp_mb(); | |
66 | ||
67 | for (;;) { | |
68 | rcu_read_lock(); | |
69 | cds_lfht_test_lookup(test_ht, | |
bd252a04 | 70 | (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % lookup_pool_size) + lookup_pool_offset), |
18ca7a5b MD |
71 | sizeof(void *), &iter); |
72 | node = cds_lfht_iter_get_test_node(&iter); | |
73 | if (node == NULL) { | |
74 | if (validate_lookup) { | |
75 | printf("[ERROR] Lookup cannot find initial node.\n"); | |
76 | exit(-1); | |
77 | } | |
bd252a04 | 78 | URCU_TLS(lookup_fail)++; |
18ca7a5b | 79 | } else { |
bd252a04 | 80 | URCU_TLS(lookup_ok)++; |
18ca7a5b | 81 | } |
1de4df4b | 82 | rcu_debug_yield_read(); |
18ca7a5b MD |
83 | if (caa_unlikely(rduration)) |
84 | loop_sleep(rduration); | |
85 | rcu_read_unlock(); | |
bd252a04 | 86 | URCU_TLS(nr_reads)++; |
18ca7a5b MD |
87 | if (caa_unlikely(!test_duration_read())) |
88 | break; | |
bd252a04 | 89 | if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0)) |
18ca7a5b MD |
90 | rcu_quiescent_state(); |
91 | } | |
92 | ||
93 | rcu_unregister_thread(); | |
94 | ||
bd252a04 | 95 | *count = URCU_TLS(nr_reads); |
94df6318 MD |
96 | printf_verbose("thread_end %s, tid %lu\n", |
97 | "reader", urcu_get_thread_id()); | |
98 | printf_verbose("read tid : %lx, lookupfail %lu, lookupok %lu\n", | |
99 | urcu_get_thread_id(), | |
100 | URCU_TLS(lookup_fail), | |
bd252a04 | 101 | URCU_TLS(lookup_ok)); |
18ca7a5b MD |
102 | return ((void*)1); |
103 | ||
104 | } | |
105 | ||
106 | void *test_hash_rw_thr_writer(void *_count) | |
107 | { | |
108 | struct lfht_test_node *node; | |
18ca7a5b MD |
109 | struct cds_lfht_iter iter; |
110 | struct wr_count *count = _count; | |
111 | int ret; | |
112 | ||
94df6318 MD |
113 | printf_verbose("thread_begin %s, tid %lu\n", |
114 | "writer", urcu_get_thread_id()); | |
18ca7a5b | 115 | |
98f483d2 MD |
116 | URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL); |
117 | ||
18ca7a5b MD |
118 | set_affinity(); |
119 | ||
120 | rcu_register_thread(); | |
121 | ||
122 | while (!test_go) | |
123 | { | |
124 | } | |
125 | cmm_smp_mb(); | |
126 | ||
127 | for (;;) { | |
6a00c945 MD |
128 | struct cds_lfht_node *ret_node = NULL; |
129 | ||
18ca7a5b | 130 | if ((addremove == AR_ADD || add_only) |
bd252a04 | 131 | || (addremove == AR_RANDOM && rand_r(&URCU_TLS(rand_lookup)) & 1)) { |
18ca7a5b MD |
132 | node = malloc(sizeof(struct lfht_test_node)); |
133 | lfht_test_node_init(node, | |
bd252a04 | 134 | (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset), |
18ca7a5b MD |
135 | sizeof(void *)); |
136 | rcu_read_lock(); | |
137 | if (add_unique) { | |
138 | ret_node = cds_lfht_add_unique(test_ht, | |
139 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
140 | test_match, node->key, &node->node); | |
141 | } else { | |
142 | if (add_replace) | |
143 | ret_node = cds_lfht_add_replace(test_ht, | |
144 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
145 | test_match, node->key, &node->node); | |
146 | else | |
147 | cds_lfht_add(test_ht, | |
148 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
149 | &node->node); | |
150 | } | |
151 | rcu_read_unlock(); | |
152 | if (add_unique && ret_node != &node->node) { | |
153 | free(node); | |
bd252a04 | 154 | URCU_TLS(nr_addexist)++; |
18ca7a5b MD |
155 | } else { |
156 | if (add_replace && ret_node) { | |
157 | call_rcu(&to_test_node(ret_node)->head, | |
158 | free_node_cb); | |
bd252a04 | 159 | URCU_TLS(nr_addexist)++; |
18ca7a5b | 160 | } else { |
bd252a04 | 161 | URCU_TLS(nr_add)++; |
18ca7a5b MD |
162 | } |
163 | } | |
164 | } else { | |
165 | /* May delete */ | |
166 | rcu_read_lock(); | |
167 | cds_lfht_test_lookup(test_ht, | |
bd252a04 | 168 | (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset), |
18ca7a5b MD |
169 | sizeof(void *), &iter); |
170 | ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter)); | |
171 | rcu_read_unlock(); | |
172 | if (ret == 0) { | |
173 | node = cds_lfht_iter_get_test_node(&iter); | |
174 | call_rcu(&node->head, free_node_cb); | |
bd252a04 | 175 | URCU_TLS(nr_del)++; |
18ca7a5b | 176 | } else |
bd252a04 | 177 | URCU_TLS(nr_delnoent)++; |
18ca7a5b MD |
178 | } |
179 | #if 0 | |
bd252a04 MD |
180 | //if (URCU_TLS(nr_writes) % 100000 == 0) { |
181 | if (URCU_TLS(nr_writes) % 1000 == 0) { | |
18ca7a5b | 182 | rcu_read_lock(); |
bd252a04 | 183 | if (rand_r(&URCU_TLS(rand_lookup)) & 1) { |
18ca7a5b MD |
184 | ht_resize(test_ht, 1); |
185 | } else { | |
186 | ht_resize(test_ht, -1); | |
187 | } | |
188 | rcu_read_unlock(); | |
189 | } | |
190 | #endif //0 | |
bd252a04 | 191 | URCU_TLS(nr_writes)++; |
18ca7a5b MD |
192 | if (caa_unlikely(!test_duration_write())) |
193 | break; | |
194 | if (caa_unlikely(wdelay)) | |
195 | loop_sleep(wdelay); | |
bd252a04 | 196 | if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0)) |
18ca7a5b MD |
197 | rcu_quiescent_state(); |
198 | } | |
199 | ||
200 | rcu_unregister_thread(); | |
201 | ||
94df6318 MD |
202 | printf_verbose("thread_end %s, tid %lu\n", |
203 | "writer", urcu_get_thread_id()); | |
204 | printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, " | |
205 | "nr_delnoent %lu\n", urcu_get_thread_id(), | |
206 | URCU_TLS(nr_add), | |
207 | URCU_TLS(nr_addexist), | |
208 | URCU_TLS(nr_del), | |
bd252a04 MD |
209 | URCU_TLS(nr_delnoent)); |
210 | count->update_ops = URCU_TLS(nr_writes); | |
211 | count->add = URCU_TLS(nr_add); | |
212 | count->add_exist = URCU_TLS(nr_addexist); | |
213 | count->remove = URCU_TLS(nr_del); | |
18ca7a5b MD |
214 | return ((void*)2); |
215 | } | |
216 | ||
217 | int test_hash_rw_populate_hash(void) | |
218 | { | |
219 | struct lfht_test_node *node; | |
18ca7a5b MD |
220 | |
221 | if (!init_populate) | |
222 | return 0; | |
223 | ||
2af6e536 MD |
224 | printf("Starting rw test\n"); |
225 | ||
98f483d2 MD |
226 | URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL); |
227 | ||
18ca7a5b MD |
228 | if ((add_unique || add_replace) && init_populate * 10 > init_pool_size) { |
229 | printf("WARNING: required to populate %lu nodes (-k), but random " | |
230 | "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a " | |
231 | "larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size); | |
232 | } | |
233 | ||
bd252a04 | 234 | while (URCU_TLS(nr_add) < init_populate) { |
6a00c945 MD |
235 | struct cds_lfht_node *ret_node = NULL; |
236 | ||
18ca7a5b MD |
237 | node = malloc(sizeof(struct lfht_test_node)); |
238 | lfht_test_node_init(node, | |
bd252a04 | 239 | (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % init_pool_size) + init_pool_offset), |
18ca7a5b MD |
240 | sizeof(void *)); |
241 | rcu_read_lock(); | |
242 | if (add_unique) { | |
243 | ret_node = cds_lfht_add_unique(test_ht, | |
244 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
245 | test_match, node->key, &node->node); | |
246 | } else { | |
247 | if (add_replace) | |
248 | ret_node = cds_lfht_add_replace(test_ht, | |
249 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
250 | test_match, node->key, &node->node); | |
251 | else | |
252 | cds_lfht_add(test_ht, | |
253 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
254 | &node->node); | |
255 | } | |
256 | rcu_read_unlock(); | |
257 | if (add_unique && ret_node != &node->node) { | |
258 | free(node); | |
bd252a04 | 259 | URCU_TLS(nr_addexist)++; |
18ca7a5b MD |
260 | } else { |
261 | if (add_replace && ret_node) { | |
262 | call_rcu(&to_test_node(ret_node)->head, free_node_cb); | |
bd252a04 | 263 | URCU_TLS(nr_addexist)++; |
18ca7a5b | 264 | } else { |
bd252a04 | 265 | URCU_TLS(nr_add)++; |
18ca7a5b MD |
266 | } |
267 | } | |
bd252a04 | 268 | URCU_TLS(nr_writes)++; |
18ca7a5b MD |
269 | } |
270 | return 0; | |
271 | } |