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