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