1 // SPDX-FileCopyrightText: 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Userspace RCU library - test program
9 #include "test_urcu_hash.h"
11 enum urcu_hash_addremove
{
15 }; /* 1: add, -1 remove, 0: random */
17 static enum urcu_hash_addremove addremove
; /* 1: add, -1 remove, 0: random */
19 void test_hash_rw_sigusr1_handler(int signo
__attribute__((unused
)))
23 printf("Add/Remove: random.\n");
24 addremove
= AR_RANDOM
;
27 printf("Add/Remove: remove only.\n");
28 addremove
= AR_REMOVE
;
31 printf("Add/Remove: add only.\n");
37 void test_hash_rw_sigusr2_handler(int signo
__attribute__((unused
)))
39 char msg
[1] = { 0x42 };
43 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
44 } while (ret
== -1L && errno
== EINTR
);
47 void *test_hash_rw_thr_reader(void *_count
)
49 unsigned long long *count
= _count
;
50 struct lfht_test_node
*node
;
51 struct cds_lfht_iter iter
;
53 printf_verbose("thread_begin %s, tid %lu\n",
54 "reader", urcu_get_thread_id());
56 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
60 rcu_register_thread();
69 cds_lfht_test_lookup(test_ht
,
70 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % lookup_pool_size
) + lookup_pool_offset
),
71 sizeof(void *), &iter
);
72 node
= cds_lfht_iter_get_test_node(&iter
);
74 if (validate_lookup
) {
75 printf("[ERROR] Lookup cannot find initial node.\n");
78 URCU_TLS(lookup_fail
)++;
80 URCU_TLS(lookup_ok
)++;
82 rcu_debug_yield_read();
83 if (caa_unlikely(rduration
))
84 loop_sleep(rduration
);
87 if (caa_unlikely(!test_duration_read()))
89 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
90 rcu_quiescent_state();
93 rcu_unregister_thread();
95 *count
= URCU_TLS(nr_reads
);
96 printf_verbose("thread_end %s, tid %lu\n",
97 "reader", urcu_get_thread_id());
98 printf_verbose("read tid : %lx, lookupfail %lu, lookupok %lu\n",
100 URCU_TLS(lookup_fail
),
101 URCU_TLS(lookup_ok
));
106 void *test_hash_rw_thr_writer(void *_count
)
108 struct lfht_test_node
*node
;
109 struct cds_lfht_iter iter
;
110 struct wr_count
*count
= _count
;
113 printf_verbose("thread_begin %s, tid %lu\n",
114 "writer", urcu_get_thread_id());
116 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
120 rcu_register_thread();
128 struct cds_lfht_node
*ret_node
= NULL
;
130 if ((addremove
== AR_ADD
|| add_only
)
131 || (addremove
== AR_RANDOM
&& rand_r(&URCU_TLS(rand_lookup
)) & 1)) {
132 node
= malloc(sizeof(struct lfht_test_node
));
133 lfht_test_node_init(node
,
134 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
138 ret_node
= cds_lfht_add_unique(test_ht
,
139 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
140 test_match
, node
->key
, &node
->node
);
143 ret_node
= cds_lfht_add_replace(test_ht
,
144 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
145 test_match
, node
->key
, &node
->node
);
147 cds_lfht_add(test_ht
,
148 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
152 if (add_unique
&& ret_node
!= &node
->node
) {
154 URCU_TLS(nr_addexist
)++;
156 if (add_replace
&& ret_node
) {
157 call_rcu(&to_test_node(ret_node
)->head
,
159 URCU_TLS(nr_addexist
)++;
167 cds_lfht_test_lookup(test_ht
,
168 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
169 sizeof(void *), &iter
);
170 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
173 node
= cds_lfht_iter_get_test_node(&iter
);
174 call_rcu(&node
->head
, free_node_cb
);
177 URCU_TLS(nr_delnoent
)++;
180 //if (URCU_TLS(nr_writes) % 100000 == 0) {
181 if (URCU_TLS(nr_writes
) % 1000 == 0) {
183 if (rand_r(&URCU_TLS(rand_lookup
)) & 1) {
184 ht_resize(test_ht
, 1);
186 ht_resize(test_ht
, -1);
191 URCU_TLS(nr_writes
)++;
192 if (caa_unlikely(!test_duration_write()))
194 if (caa_unlikely(wdelay
))
196 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
197 rcu_quiescent_state();
200 rcu_unregister_thread();
202 printf_verbose("thread_end %s, tid %lu\n",
203 "writer", urcu_get_thread_id());
204 printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, "
205 "nr_delnoent %lu\n", urcu_get_thread_id(),
207 URCU_TLS(nr_addexist
),
209 URCU_TLS(nr_delnoent
));
210 count
->update_ops
= URCU_TLS(nr_writes
);
211 count
->add
= URCU_TLS(nr_add
);
212 count
->add_exist
= URCU_TLS(nr_addexist
);
213 count
->remove
= URCU_TLS(nr_del
);
217 int test_hash_rw_populate_hash(void)
219 struct lfht_test_node
*node
;
224 printf("Starting rw test\n");
226 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
228 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
229 printf("WARNING: required to populate %lu nodes (-k), but random "
230 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
231 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
234 while (URCU_TLS(nr_add
) < init_populate
) {
235 struct cds_lfht_node
*ret_node
= NULL
;
237 node
= malloc(sizeof(struct lfht_test_node
));
238 lfht_test_node_init(node
,
239 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % init_pool_size
) + init_pool_offset
),
243 ret_node
= cds_lfht_add_unique(test_ht
,
244 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
245 test_match
, node
->key
, &node
->node
);
248 ret_node
= cds_lfht_add_replace(test_ht
,
249 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
250 test_match
, node
->key
, &node
->node
);
252 cds_lfht_add(test_ht
,
253 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
257 if (add_unique
&& ret_node
!= &node
->node
) {
259 URCU_TLS(nr_addexist
)++;
261 if (add_replace
&& ret_node
) {
262 call_rcu(&to_test_node(ret_node
)->head
, free_node_cb
);
263 URCU_TLS(nr_addexist
)++;
268 URCU_TLS(nr_writes
)++;