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