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();
66 cds_lfht_test_lookup(test_ht
,
67 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % lookup_pool_size
) + lookup_pool_offset
),
68 sizeof(void *), &iter
);
69 node
= cds_lfht_iter_get_test_node(&iter
);
71 if (validate_lookup
) {
72 printf("[ERROR] Lookup cannot find initial node.\n");
75 URCU_TLS(lookup_fail
)++;
77 URCU_TLS(lookup_ok
)++;
79 rcu_debug_yield_read();
80 if (caa_unlikely(rduration
))
81 loop_sleep(rduration
);
84 if (caa_unlikely(!test_duration_read()))
86 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
87 rcu_quiescent_state();
90 rcu_unregister_thread();
92 *count
= URCU_TLS(nr_reads
);
93 printf_verbose("thread_end %s, tid %lu\n",
94 "reader", urcu_get_thread_id());
95 printf_verbose("read tid : %lx, lookupfail %lu, lookupok %lu\n",
97 URCU_TLS(lookup_fail
),
103 void *test_hash_rw_thr_writer(void *_count
)
105 struct lfht_test_node
*node
;
106 struct cds_lfht_iter iter
;
107 struct wr_count
*count
= _count
;
110 printf_verbose("thread_begin %s, tid %lu\n",
111 "writer", urcu_get_thread_id());
113 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
117 rcu_register_thread();
122 struct cds_lfht_node
*ret_node
= NULL
;
124 if ((addremove
== AR_ADD
|| add_only
)
125 || (addremove
== AR_RANDOM
&& rand_r(&URCU_TLS(rand_lookup
)) & 1)) {
126 node
= malloc(sizeof(struct lfht_test_node
));
127 lfht_test_node_init(node
,
128 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
132 ret_node
= cds_lfht_add_unique(test_ht
,
133 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
134 test_match
, node
->key
, &node
->node
);
137 ret_node
= cds_lfht_add_replace(test_ht
,
138 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
139 test_match
, node
->key
, &node
->node
);
141 cds_lfht_add(test_ht
,
142 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
146 if (add_unique
&& ret_node
!= &node
->node
) {
148 URCU_TLS(nr_addexist
)++;
150 if (add_replace
&& ret_node
) {
151 call_rcu(&to_test_node(ret_node
)->head
,
153 URCU_TLS(nr_addexist
)++;
161 cds_lfht_test_lookup(test_ht
,
162 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
163 sizeof(void *), &iter
);
164 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
167 node
= cds_lfht_iter_get_test_node(&iter
);
168 call_rcu(&node
->head
, free_node_cb
);
171 URCU_TLS(nr_delnoent
)++;
174 //if (URCU_TLS(nr_writes) % 100000 == 0) {
175 if (URCU_TLS(nr_writes
) % 1000 == 0) {
177 if (rand_r(&URCU_TLS(rand_lookup
)) & 1) {
178 ht_resize(test_ht
, 1);
180 ht_resize(test_ht
, -1);
185 URCU_TLS(nr_writes
)++;
186 if (caa_unlikely(!test_duration_write()))
188 if (caa_unlikely(wdelay
))
190 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
191 rcu_quiescent_state();
194 rcu_unregister_thread();
196 printf_verbose("thread_end %s, tid %lu\n",
197 "writer", urcu_get_thread_id());
198 printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, "
199 "nr_delnoent %lu\n", urcu_get_thread_id(),
201 URCU_TLS(nr_addexist
),
203 URCU_TLS(nr_delnoent
));
204 count
->update_ops
= URCU_TLS(nr_writes
);
205 count
->add
= URCU_TLS(nr_add
);
206 count
->add_exist
= URCU_TLS(nr_addexist
);
207 count
->remove
= URCU_TLS(nr_del
);
211 int test_hash_rw_populate_hash(void)
213 struct lfht_test_node
*node
;
218 printf("Starting rw test\n");
220 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
222 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
223 printf("WARNING: required to populate %lu nodes (-k), but random "
224 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
225 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
228 while (URCU_TLS(nr_add
) < init_populate
) {
229 struct cds_lfht_node
*ret_node
= NULL
;
231 node
= malloc(sizeof(struct lfht_test_node
));
232 lfht_test_node_init(node
,
233 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % init_pool_size
) + init_pool_offset
),
237 ret_node
= cds_lfht_add_unique(test_ht
,
238 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
239 test_match
, node
->key
, &node
->node
);
242 ret_node
= cds_lfht_add_replace(test_ht
,
243 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
244 test_match
, node
->key
, &node
->node
);
246 cds_lfht_add(test_ht
,
247 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
251 if (add_unique
&& ret_node
!= &node
->node
) {
253 URCU_TLS(nr_addexist
)++;
255 if (add_replace
&& ret_node
) {
256 call_rcu(&to_test_node(ret_node
)->head
, free_node_cb
);
257 URCU_TLS(nr_addexist
)++;
262 URCU_TLS(nr_writes
)++;