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