2 * test_urcu_hash_unique.c
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_unique_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_unique_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_unique_thr_reader(void *_count
)
64 unsigned long long *count
= _count
;
66 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
67 "reader", pthread_self(), (unsigned long)gettid());
71 rcu_register_thread();
79 struct lfht_test_node
*node
;
80 struct cds_lfht_iter iter
;
82 * iterate on whole table, ensuring that no duplicate is
86 cds_lfht_for_each_entry(test_ht
, &iter
, node
, node
) {
87 struct cds_lfht_iter dup_iter
;
90 cds_lfht_next_duplicate(test_ht
, test_match
,
91 node
->key
, &dup_iter
);
92 if (dup_iter
.node
!= NULL
) {
93 printf("[ERROR] Duplicate key %p found\n", node
->key
);
99 if (caa_unlikely(rduration
))
100 loop_sleep(rduration
);
102 if (caa_unlikely(!test_duration_read()))
104 if (caa_unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
105 rcu_quiescent_state();
108 rcu_unregister_thread();
111 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
112 "reader", pthread_self(), (unsigned long)gettid());
113 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
114 pthread_self(), lookup_fail
, lookup_ok
);
119 void *test_hash_unique_thr_writer(void *_count
)
121 struct lfht_test_node
*node
;
122 struct cds_lfht_node
*ret_node
;
123 struct cds_lfht_iter iter
;
124 struct wr_count
*count
= _count
;
128 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
129 "writer", pthread_self(), (unsigned long)gettid());
133 rcu_register_thread();
142 * add unique/add replace with new node key from range.
144 if (1 || (addremove
== AR_ADD
|| add_only
)
145 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
146 node
= malloc(sizeof(struct lfht_test_node
));
147 lfht_test_node_init(node
,
148 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
151 loc_add_unique
= rand_r(&rand_lookup
) & 1;
152 if (loc_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
);
157 ret_node
= cds_lfht_add_replace(test_ht
,
158 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
159 test_match
, node
->key
, &node
->node
);
160 #if 0 //generate an error on purpose
161 cds_lfht_add(test_ht
,
162 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
168 if (loc_add_unique
) {
169 if (ret_node
!= &node
->node
) {
177 call_rcu(&to_test_node(ret_node
)->head
,
187 cds_lfht_test_lookup(test_ht
,
188 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
189 sizeof(void *), &iter
);
190 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
193 node
= cds_lfht_iter_get_test_node(&iter
);
194 call_rcu(&node
->head
, free_node_cb
);
200 //if (nr_writes % 100000 == 0) {
201 if (nr_writes
% 1000 == 0) {
203 if (rand_r(&rand_lookup
) & 1) {
204 ht_resize(test_ht
, 1);
206 ht_resize(test_ht
, -1);
212 if (caa_unlikely(!test_duration_write()))
214 if (caa_unlikely(wdelay
))
216 if (caa_unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
217 rcu_quiescent_state();
220 rcu_unregister_thread();
222 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
223 "writer", pthread_self(), (unsigned long)gettid());
224 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
225 "nr_delnoent %lu\n", pthread_self(), nr_add
,
226 nr_addexist
, nr_del
, nr_delnoent
);
227 count
->update_ops
= nr_writes
;
229 count
->add_exist
= nr_addexist
;
230 count
->remove
= nr_del
;
234 int test_hash_unique_populate_hash(void)
236 struct lfht_test_node
*node
;
237 struct cds_lfht_node
*ret_node
;
239 printf("Starting uniqueness test.\n");
244 if (init_populate
* 10 > init_pool_size
) {
245 printf("WARNING: required to populate %lu nodes (-k), but random "
246 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
247 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
250 while (nr_add
< init_populate
) {
251 node
= malloc(sizeof(struct lfht_test_node
));
252 lfht_test_node_init(node
,
253 (void *)(((unsigned long) rand_r(&rand_lookup
) % init_pool_size
) + init_pool_offset
),
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
);
261 call_rcu(&to_test_node(ret_node
)->head
, free_node_cb
);