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.
23 #include "test_urcu_hash.h"
25 enum urcu_hash_addremove
{
29 }; /* 1: add, -1 remove, 0: random */
31 static enum urcu_hash_addremove addremove
; /* 1: add, -1 remove, 0: random */
33 void test_hash_unique_sigusr1_handler(int signo
__attribute__((unused
)))
37 printf("Add/Remove: random.\n");
38 addremove
= AR_RANDOM
;
41 printf("Add/Remove: remove only.\n");
42 addremove
= AR_REMOVE
;
45 printf("Add/Remove: add only.\n");
51 void test_hash_unique_sigusr2_handler(int signo
__attribute__((unused
)))
53 char msg
[1] = { 0x42 };
57 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
58 } while (ret
== -1L && errno
== EINTR
);
61 void *test_hash_unique_thr_reader(void *_count
)
63 unsigned long long *count
= _count
;
65 printf_verbose("thread_begin %s, tid %lu\n",
66 "reader", urcu_get_thread_id());
68 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
72 rcu_register_thread();
80 struct lfht_test_node
*node
;
81 struct cds_lfht_iter iter
;
83 * iterate on whole table, ensuring that no duplicate is
87 cds_lfht_for_each_entry(test_ht
, &iter
, node
, node
) {
88 struct cds_lfht_iter dup_iter
;
91 cds_lfht_next_duplicate(test_ht
, test_match
,
92 node
->key
, &dup_iter
);
93 if (dup_iter
.node
!= NULL
) {
94 printf("[ERROR] Duplicate key %p found\n", node
->key
);
99 rcu_debug_yield_read();
100 if (caa_unlikely(rduration
))
101 loop_sleep(rduration
);
102 URCU_TLS(nr_reads
)++;
103 if (caa_unlikely(!test_duration_read()))
105 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
106 rcu_quiescent_state();
109 rcu_unregister_thread();
111 *count
= URCU_TLS(nr_reads
);
112 printf_verbose("thread_end %s, tid %lu\n",
113 "reader", urcu_get_thread_id());
114 printf_verbose("read tid : %lu, lookupfail %lu, lookupok %lu\n",
115 urcu_get_thread_id(), URCU_TLS(lookup_fail
),
116 URCU_TLS(lookup_ok
));
121 void *test_hash_unique_thr_writer(void *_count
)
123 struct lfht_test_node
*node
;
124 struct cds_lfht_node
*ret_node
;
125 struct cds_lfht_iter iter
;
126 struct wr_count
*count
= _count
;
130 printf_verbose("thread_begin %s, tid %lu\n",
131 "writer", urcu_get_thread_id());
133 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
137 rcu_register_thread();
146 * add unique/add replace with new node key from range.
148 if (1 || (addremove
== AR_ADD
|| add_only
)
149 || (addremove
== AR_RANDOM
&& rand_r(&URCU_TLS(rand_lookup
)) & 1)) {
150 node
= malloc(sizeof(struct lfht_test_node
));
151 lfht_test_node_init(node
,
152 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
155 loc_add_unique
= rand_r(&URCU_TLS(rand_lookup
)) & 1;
156 if (loc_add_unique
) {
157 ret_node
= cds_lfht_add_unique(test_ht
,
158 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
159 test_match
, node
->key
, &node
->node
);
161 ret_node
= cds_lfht_add_replace(test_ht
,
162 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
163 test_match
, node
->key
, &node
->node
);
164 #if 0 //generate an error on purpose
165 cds_lfht_add(test_ht
,
166 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
172 if (loc_add_unique
) {
173 if (ret_node
!= &node
->node
) {
175 URCU_TLS(nr_addexist
)++;
181 call_rcu(&to_test_node(ret_node
)->head
,
183 URCU_TLS(nr_addexist
)++;
191 cds_lfht_test_lookup(test_ht
,
192 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
),
193 sizeof(void *), &iter
);
194 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
197 node
= cds_lfht_iter_get_test_node(&iter
);
198 call_rcu(&node
->head
, free_node_cb
);
201 URCU_TLS(nr_delnoent
)++;
204 //if (URCU_TLS(nr_writes) % 100000 == 0) {
205 if (URCU_TLS(nr_writes
) % 1000 == 0) {
207 if (rand_r(&URCU_TLS(rand_lookup
)) & 1) {
208 ht_resize(test_ht
, 1);
210 ht_resize(test_ht
, -1);
215 URCU_TLS(nr_writes
)++;
216 if (caa_unlikely(!test_duration_write()))
218 if (caa_unlikely(wdelay
))
220 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
221 rcu_quiescent_state();
224 rcu_unregister_thread();
226 printf_verbose("thread_end %s, tid %lu\n",
227 "writer", urcu_get_thread_id());
228 printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, "
229 "nr_delnoent %lu\n", urcu_get_thread_id(),
231 URCU_TLS(nr_addexist
),
233 URCU_TLS(nr_delnoent
));
234 count
->update_ops
= URCU_TLS(nr_writes
);
235 count
->add
= URCU_TLS(nr_add
);
236 count
->add_exist
= URCU_TLS(nr_addexist
);
237 count
->remove
= URCU_TLS(nr_del
);
241 int test_hash_unique_populate_hash(void)
243 struct lfht_test_node
*node
;
244 struct cds_lfht_node
*ret_node
;
246 printf("Starting uniqueness test.\n");
248 URCU_TLS(rand_lookup
) = urcu_get_thread_id() ^ time(NULL
);
253 if (init_populate
* 10 > init_pool_size
) {
254 printf("WARNING: required to populate %lu nodes (-k), but random "
255 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
256 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
259 while (URCU_TLS(nr_add
) < init_populate
) {
260 node
= malloc(sizeof(struct lfht_test_node
));
261 lfht_test_node_init(node
,
262 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % init_pool_size
) + init_pool_offset
),
265 ret_node
= cds_lfht_add_replace(test_ht
,
266 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
267 test_match
, node
->key
, &node
->node
);
270 call_rcu(&to_test_node(ret_node
)->head
, free_node_cb
);
271 URCU_TLS(nr_addexist
)++;
275 URCU_TLS(nr_writes
)++;