4 * Userspace RCU library - test program
6 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
24 #include "test_urcu_hash.h"
26 enum urcu_hash_addremove
{
30 }; /* 1: add, -1 remove, 0: random */
32 static enum urcu_hash_addremove addremove
; /* 1: add, -1 remove, 0: random */
34 void test_hash_rw_sigusr1_handler(int signo
)
38 printf("Add/Remove: random.\n");
39 addremove
= AR_RANDOM
;
42 printf("Add/Remove: remove only.\n");
43 addremove
= AR_REMOVE
;
46 printf("Add/Remove: add only.\n");
52 void test_hash_rw_sigusr2_handler(int signo
)
54 char msg
[1] = { 0x42 };
58 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
59 } while (ret
== -1L && errno
== EINTR
);
62 void *test_hash_rw_thr_reader(void *_count
)
64 unsigned long long *count
= _count
;
65 struct lfht_test_node
*node
;
66 struct cds_lfht_iter iter
;
68 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
69 "reader", pthread_self(), (unsigned long)gettid());
73 rcu_register_thread();
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
);
87 if (validate_lookup
) {
88 printf("[ERROR] Lookup cannot find initial node.\n");
91 URCU_TLS(lookup_fail
)++;
93 URCU_TLS(lookup_ok
)++;
96 if (caa_unlikely(rduration
))
97 loop_sleep(rduration
);
100 if (caa_unlikely(!test_duration_read()))
102 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
103 rcu_quiescent_state();
106 rcu_unregister_thread();
108 *count
= URCU_TLS(nr_reads
);
109 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
110 "reader", pthread_self(), (unsigned long)gettid());
111 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
112 pthread_self(), URCU_TLS(lookup_fail
),
113 URCU_TLS(lookup_ok
));
118 void *test_hash_rw_thr_writer(void *_count
)
120 struct lfht_test_node
*node
;
121 struct cds_lfht_node
*ret_node
;
122 struct cds_lfht_iter iter
;
123 struct wr_count
*count
= _count
;
126 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
127 "writer", pthread_self(), (unsigned long)gettid());
131 rcu_register_thread();
139 if ((addremove
== AR_ADD
|| add_only
)
140 || (addremove
== AR_RANDOM
&& rand_r(&URCU_TLS(rand_lookup
)) & 1)) {
141 node
= malloc(sizeof(struct lfht_test_node
));
142 lfht_test_node_init(node
,
143 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
147 ret_node
= cds_lfht_add_unique(test_ht
,
148 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
149 test_match
, node
->key
, &node
->node
);
152 ret_node
= cds_lfht_add_replace(test_ht
,
153 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
154 test_match
, node
->key
, &node
->node
);
156 cds_lfht_add(test_ht
,
157 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
161 if (add_unique
&& ret_node
!= &node
->node
) {
163 URCU_TLS(nr_addexist
)++;
165 if (add_replace
&& ret_node
) {
166 call_rcu(&to_test_node(ret_node
)->head
,
168 URCU_TLS(nr_addexist
)++;
176 cds_lfht_test_lookup(test_ht
,
177 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
178 sizeof(void *), &iter
);
179 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
182 node
= cds_lfht_iter_get_test_node(&iter
);
183 call_rcu(&node
->head
, free_node_cb
);
186 URCU_TLS(nr_delnoent
)++;
189 //if (URCU_TLS(nr_writes) % 100000 == 0) {
190 if (URCU_TLS(nr_writes
) % 1000 == 0) {
192 if (rand_r(&URCU_TLS(rand_lookup
)) & 1) {
193 ht_resize(test_ht
, 1);
195 ht_resize(test_ht
, -1);
200 URCU_TLS(nr_writes
)++;
201 if (caa_unlikely(!test_duration_write()))
203 if (caa_unlikely(wdelay
))
205 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
206 rcu_quiescent_state();
209 rcu_unregister_thread();
211 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
212 "writer", pthread_self(), (unsigned long)gettid());
213 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
214 "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add
),
215 URCU_TLS(nr_addexist
), URCU_TLS(nr_del
),
216 URCU_TLS(nr_delnoent
));
217 count
->update_ops
= URCU_TLS(nr_writes
);
218 count
->add
= URCU_TLS(nr_add
);
219 count
->add_exist
= URCU_TLS(nr_addexist
);
220 count
->remove
= URCU_TLS(nr_del
);
224 int test_hash_rw_populate_hash(void)
226 struct lfht_test_node
*node
;
227 struct cds_lfht_node
*ret_node
;
232 printf("Starting rw test\n");
234 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
235 printf("WARNING: required to populate %lu nodes (-k), but random "
236 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
237 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
240 while (URCU_TLS(nr_add
) < init_populate
) {
241 node
= malloc(sizeof(struct lfht_test_node
));
242 lfht_test_node_init(node
,
243 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % init_pool_size
) + init_pool_offset
),
247 ret_node
= cds_lfht_add_unique(test_ht
,
248 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
249 test_match
, node
->key
, &node
->node
);
252 ret_node
= cds_lfht_add_replace(test_ht
,
253 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
254 test_match
, node
->key
, &node
->node
);
256 cds_lfht_add(test_ht
,
257 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
261 if (add_unique
&& ret_node
!= &node
->node
) {
263 URCU_TLS(nr_addexist
)++;
265 if (add_replace
&& ret_node
) {
266 call_rcu(&to_test_node(ret_node
)->head
, free_node_cb
);
267 URCU_TLS(nr_addexist
)++;
272 URCU_TLS(nr_writes
)++;