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