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