Commit | Line | Data |
---|---|---|
20adf780 MD |
1 | /* |
2 | * test_urcu_hash_unique.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_unique_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_unique_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_unique_thr_reader(void *_count) | |
63 | { | |
64 | unsigned long long *count = _count; | |
65 | ||
94df6318 MD |
66 | printf_verbose("thread_begin %s, tid %lu\n", |
67 | "reader", urcu_get_thread_id()); | |
20adf780 | 68 | |
98f483d2 MD |
69 | URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL); |
70 | ||
20adf780 MD |
71 | set_affinity(); |
72 | ||
73 | rcu_register_thread(); | |
74 | ||
75 | while (!test_go) | |
76 | { | |
77 | } | |
78 | cmm_smp_mb(); | |
79 | ||
80 | for (;;) { | |
81 | struct lfht_test_node *node; | |
82 | struct cds_lfht_iter iter; | |
83 | /* | |
84 | * iterate on whole table, ensuring that no duplicate is | |
85 | * found. | |
86 | */ | |
87 | rcu_read_lock(); | |
88 | cds_lfht_for_each_entry(test_ht, &iter, node, node) { | |
89 | struct cds_lfht_iter dup_iter; | |
90 | ||
91 | dup_iter = iter; | |
92 | cds_lfht_next_duplicate(test_ht, test_match, | |
93 | node->key, &dup_iter); | |
94 | if (dup_iter.node != NULL) { | |
95 | printf("[ERROR] Duplicate key %p found\n", node->key); | |
96 | } | |
97 | } | |
98 | rcu_read_unlock(); | |
99 | ||
1de4df4b | 100 | rcu_debug_yield_read(); |
20adf780 MD |
101 | if (caa_unlikely(rduration)) |
102 | loop_sleep(rduration); | |
bd252a04 | 103 | URCU_TLS(nr_reads)++; |
20adf780 MD |
104 | if (caa_unlikely(!test_duration_read())) |
105 | break; | |
bd252a04 | 106 | if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0)) |
20adf780 MD |
107 | rcu_quiescent_state(); |
108 | } | |
109 | ||
110 | rcu_unregister_thread(); | |
111 | ||
bd252a04 | 112 | *count = URCU_TLS(nr_reads); |
94df6318 MD |
113 | printf_verbose("thread_end %s, tid %lu\n", |
114 | "reader", urcu_get_thread_id()); | |
115 | printf_verbose("read tid : %lu, lookupfail %lu, lookupok %lu\n", | |
116 | urcu_get_thread_id(), URCU_TLS(lookup_fail), | |
bd252a04 | 117 | URCU_TLS(lookup_ok)); |
20adf780 MD |
118 | return ((void*)1); |
119 | ||
120 | } | |
121 | ||
122 | void *test_hash_unique_thr_writer(void *_count) | |
123 | { | |
124 | struct lfht_test_node *node; | |
125 | struct cds_lfht_node *ret_node; | |
126 | struct cds_lfht_iter iter; | |
127 | struct wr_count *count = _count; | |
128 | int ret; | |
129 | int loc_add_unique; | |
130 | ||
94df6318 MD |
131 | printf_verbose("thread_begin %s, tid %lu\n", |
132 | "writer", urcu_get_thread_id()); | |
20adf780 | 133 | |
98f483d2 MD |
134 | URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL); |
135 | ||
20adf780 MD |
136 | set_affinity(); |
137 | ||
138 | rcu_register_thread(); | |
139 | ||
140 | while (!test_go) | |
141 | { | |
142 | } | |
143 | cmm_smp_mb(); | |
144 | ||
145 | for (;;) { | |
146 | /* | |
147 | * add unique/add replace with new node key from range. | |
148 | */ | |
149 | if (1 || (addremove == AR_ADD || add_only) | |
bd252a04 | 150 | || (addremove == AR_RANDOM && rand_r(&URCU_TLS(rand_lookup)) & 1)) { |
20adf780 MD |
151 | node = malloc(sizeof(struct lfht_test_node)); |
152 | lfht_test_node_init(node, | |
bd252a04 | 153 | (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset), |
20adf780 MD |
154 | sizeof(void *)); |
155 | rcu_read_lock(); | |
bd252a04 | 156 | loc_add_unique = rand_r(&URCU_TLS(rand_lookup)) & 1; |
20adf780 MD |
157 | if (loc_add_unique) { |
158 | ret_node = cds_lfht_add_unique(test_ht, | |
159 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
160 | test_match, node->key, &node->node); | |
161 | } else { | |
162 | ret_node = cds_lfht_add_replace(test_ht, | |
163 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
164 | test_match, node->key, &node->node); | |
165 | #if 0 //generate an error on purpose | |
166 | cds_lfht_add(test_ht, | |
167 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
168 | &node->node); | |
169 | ret_node = NULL; | |
170 | #endif //0 | |
171 | } | |
172 | rcu_read_unlock(); | |
173 | if (loc_add_unique) { | |
174 | if (ret_node != &node->node) { | |
175 | free(node); | |
bd252a04 | 176 | URCU_TLS(nr_addexist)++; |
20adf780 | 177 | } else { |
bd252a04 | 178 | URCU_TLS(nr_add)++; |
20adf780 MD |
179 | } |
180 | } else { | |
181 | if (ret_node) { | |
182 | call_rcu(&to_test_node(ret_node)->head, | |
183 | free_node_cb); | |
bd252a04 | 184 | URCU_TLS(nr_addexist)++; |
20adf780 | 185 | } else { |
bd252a04 | 186 | URCU_TLS(nr_add)++; |
20adf780 MD |
187 | } |
188 | } | |
189 | } else { | |
190 | /* May delete */ | |
191 | rcu_read_lock(); | |
192 | cds_lfht_test_lookup(test_ht, | |
bd252a04 | 193 | (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset), |
20adf780 MD |
194 | sizeof(void *), &iter); |
195 | ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter)); | |
196 | rcu_read_unlock(); | |
197 | if (ret == 0) { | |
198 | node = cds_lfht_iter_get_test_node(&iter); | |
199 | call_rcu(&node->head, free_node_cb); | |
bd252a04 | 200 | URCU_TLS(nr_del)++; |
20adf780 | 201 | } else |
bd252a04 | 202 | URCU_TLS(nr_delnoent)++; |
20adf780 MD |
203 | } |
204 | #if 0 | |
bd252a04 MD |
205 | //if (URCU_TLS(nr_writes) % 100000 == 0) { |
206 | if (URCU_TLS(nr_writes) % 1000 == 0) { | |
20adf780 | 207 | rcu_read_lock(); |
bd252a04 | 208 | if (rand_r(&URCU_TLS(rand_lookup)) & 1) { |
20adf780 MD |
209 | ht_resize(test_ht, 1); |
210 | } else { | |
211 | ht_resize(test_ht, -1); | |
212 | } | |
213 | rcu_read_unlock(); | |
214 | } | |
215 | #endif //0 | |
bd252a04 | 216 | URCU_TLS(nr_writes)++; |
20adf780 MD |
217 | if (caa_unlikely(!test_duration_write())) |
218 | break; | |
219 | if (caa_unlikely(wdelay)) | |
220 | loop_sleep(wdelay); | |
bd252a04 | 221 | if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0)) |
20adf780 MD |
222 | rcu_quiescent_state(); |
223 | } | |
224 | ||
225 | rcu_unregister_thread(); | |
226 | ||
94df6318 MD |
227 | printf_verbose("thread_end %s, tid %lu\n", |
228 | "writer", urcu_get_thread_id()); | |
229 | printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, " | |
230 | "nr_delnoent %lu\n", urcu_get_thread_id(), | |
231 | URCU_TLS(nr_add), | |
232 | URCU_TLS(nr_addexist), | |
233 | URCU_TLS(nr_del), | |
bd252a04 MD |
234 | URCU_TLS(nr_delnoent)); |
235 | count->update_ops = URCU_TLS(nr_writes); | |
236 | count->add = URCU_TLS(nr_add); | |
237 | count->add_exist = URCU_TLS(nr_addexist); | |
238 | count->remove = URCU_TLS(nr_del); | |
20adf780 MD |
239 | return ((void*)2); |
240 | } | |
241 | ||
242 | int test_hash_unique_populate_hash(void) | |
243 | { | |
244 | struct lfht_test_node *node; | |
245 | struct cds_lfht_node *ret_node; | |
246 | ||
247 | printf("Starting uniqueness test.\n"); | |
248 | ||
98f483d2 MD |
249 | URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL); |
250 | ||
20adf780 MD |
251 | if (!init_populate) |
252 | return 0; | |
253 | ||
254 | if (init_populate * 10 > init_pool_size) { | |
255 | printf("WARNING: required to populate %lu nodes (-k), but random " | |
256 | "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a " | |
257 | "larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size); | |
258 | } | |
259 | ||
bd252a04 | 260 | while (URCU_TLS(nr_add) < init_populate) { |
20adf780 MD |
261 | node = malloc(sizeof(struct lfht_test_node)); |
262 | lfht_test_node_init(node, | |
bd252a04 | 263 | (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % init_pool_size) + init_pool_offset), |
20adf780 MD |
264 | sizeof(void *)); |
265 | rcu_read_lock(); | |
266 | ret_node = cds_lfht_add_replace(test_ht, | |
267 | test_hash(node->key, node->key_len, TEST_HASH_SEED), | |
268 | test_match, node->key, &node->node); | |
269 | rcu_read_unlock(); | |
270 | if (ret_node) { | |
271 | call_rcu(&to_test_node(ret_node)->head, free_node_cb); | |
bd252a04 | 272 | URCU_TLS(nr_addexist)++; |
20adf780 | 273 | } else { |
bd252a04 | 274 | URCU_TLS(nr_add)++; |
20adf780 | 275 | } |
bd252a04 | 276 | URCU_TLS(nr_writes)++; |
20adf780 MD |
277 | } |
278 | return 0; | |
279 | } |