tests: Use uatomic for accessing global states
[urcu.git] / tests / benchmark / test_urcu_hash_unique.c
CommitLineData
ce29b371
MJ
1// SPDX-FileCopyrightText: 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: GPL-2.0-or-later
4
20adf780 5/*
20adf780 6 * Userspace RCU library - test program
20adf780
MD
7 */
8
20adf780
MD
9#include "test_urcu_hash.h"
10
11enum urcu_hash_addremove {
12 AR_RANDOM = 0,
13 AR_ADD = 1,
14 AR_REMOVE = -1,
15}; /* 1: add, -1 remove, 0: random */
16
17static enum urcu_hash_addremove addremove; /* 1: add, -1 remove, 0: random */
18
70469b43 19void test_hash_unique_sigusr1_handler(int signo __attribute__((unused)))
20adf780
MD
20{
21 switch (addremove) {
22 case AR_ADD:
23 printf("Add/Remove: random.\n");
24 addremove = AR_RANDOM;
25 break;
26 case AR_RANDOM:
27 printf("Add/Remove: remove only.\n");
28 addremove = AR_REMOVE;
29 break;
30 case AR_REMOVE:
31 printf("Add/Remove: add only.\n");
32 addremove = AR_ADD;
33 break;
34 }
35}
36
70469b43 37void test_hash_unique_sigusr2_handler(int signo __attribute__((unused)))
20adf780
MD
38{
39 char msg[1] = { 0x42 };
40 ssize_t ret;
41
42 do {
43 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
44 } while (ret == -1L && errno == EINTR);
45}
46
47void *test_hash_unique_thr_reader(void *_count)
48{
49 unsigned long long *count = _count;
50
94df6318
MD
51 printf_verbose("thread_begin %s, tid %lu\n",
52 "reader", urcu_get_thread_id());
20adf780 53
98f483d2
MD
54 URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL);
55
20adf780
MD
56 set_affinity();
57
58 rcu_register_thread();
59
60 while (!test_go)
61 {
62 }
63 cmm_smp_mb();
64
65 for (;;) {
66 struct lfht_test_node *node;
67 struct cds_lfht_iter iter;
68 /*
69 * iterate on whole table, ensuring that no duplicate is
70 * found.
71 */
72 rcu_read_lock();
73 cds_lfht_for_each_entry(test_ht, &iter, node, node) {
74 struct cds_lfht_iter dup_iter;
75
76 dup_iter = iter;
77 cds_lfht_next_duplicate(test_ht, test_match,
78 node->key, &dup_iter);
79 if (dup_iter.node != NULL) {
80 printf("[ERROR] Duplicate key %p found\n", node->key);
81 }
82 }
83 rcu_read_unlock();
84
1de4df4b 85 rcu_debug_yield_read();
20adf780
MD
86 if (caa_unlikely(rduration))
87 loop_sleep(rduration);
bd252a04 88 URCU_TLS(nr_reads)++;
20adf780
MD
89 if (caa_unlikely(!test_duration_read()))
90 break;
bd252a04 91 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
20adf780
MD
92 rcu_quiescent_state();
93 }
94
95 rcu_unregister_thread();
96
bd252a04 97 *count = URCU_TLS(nr_reads);
94df6318
MD
98 printf_verbose("thread_end %s, tid %lu\n",
99 "reader", urcu_get_thread_id());
100 printf_verbose("read tid : %lu, lookupfail %lu, lookupok %lu\n",
101 urcu_get_thread_id(), URCU_TLS(lookup_fail),
bd252a04 102 URCU_TLS(lookup_ok));
20adf780
MD
103 return ((void*)1);
104
105}
106
107void *test_hash_unique_thr_writer(void *_count)
108{
109 struct lfht_test_node *node;
110 struct cds_lfht_node *ret_node;
111 struct cds_lfht_iter iter;
112 struct wr_count *count = _count;
113 int ret;
114 int loc_add_unique;
115
94df6318
MD
116 printf_verbose("thread_begin %s, tid %lu\n",
117 "writer", urcu_get_thread_id());
20adf780 118
98f483d2
MD
119 URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL);
120
20adf780
MD
121 set_affinity();
122
123 rcu_register_thread();
124
125 while (!test_go)
126 {
127 }
128 cmm_smp_mb();
129
130 for (;;) {
131 /*
132 * add unique/add replace with new node key from range.
133 */
134 if (1 || (addremove == AR_ADD || add_only)
bd252a04 135 || (addremove == AR_RANDOM && rand_r(&URCU_TLS(rand_lookup)) & 1)) {
20adf780
MD
136 node = malloc(sizeof(struct lfht_test_node));
137 lfht_test_node_init(node,
bd252a04 138 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset),
20adf780
MD
139 sizeof(void *));
140 rcu_read_lock();
bd252a04 141 loc_add_unique = rand_r(&URCU_TLS(rand_lookup)) & 1;
20adf780
MD
142 if (loc_add_unique) {
143 ret_node = cds_lfht_add_unique(test_ht,
144 test_hash(node->key, node->key_len, TEST_HASH_SEED),
145 test_match, node->key, &node->node);
146 } else {
147 ret_node = cds_lfht_add_replace(test_ht,
148 test_hash(node->key, node->key_len, TEST_HASH_SEED),
149 test_match, node->key, &node->node);
150#if 0 //generate an error on purpose
151 cds_lfht_add(test_ht,
152 test_hash(node->key, node->key_len, TEST_HASH_SEED),
153 &node->node);
154 ret_node = NULL;
155#endif //0
156 }
157 rcu_read_unlock();
158 if (loc_add_unique) {
159 if (ret_node != &node->node) {
160 free(node);
bd252a04 161 URCU_TLS(nr_addexist)++;
20adf780 162 } else {
bd252a04 163 URCU_TLS(nr_add)++;
20adf780
MD
164 }
165 } else {
166 if (ret_node) {
167 call_rcu(&to_test_node(ret_node)->head,
168 free_node_cb);
bd252a04 169 URCU_TLS(nr_addexist)++;
20adf780 170 } else {
bd252a04 171 URCU_TLS(nr_add)++;
20adf780
MD
172 }
173 }
174 } else {
175 /* May delete */
176 rcu_read_lock();
177 cds_lfht_test_lookup(test_ht,
bd252a04 178 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset),
20adf780
MD
179 sizeof(void *), &iter);
180 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
181 rcu_read_unlock();
182 if (ret == 0) {
183 node = cds_lfht_iter_get_test_node(&iter);
184 call_rcu(&node->head, free_node_cb);
bd252a04 185 URCU_TLS(nr_del)++;
20adf780 186 } else
bd252a04 187 URCU_TLS(nr_delnoent)++;
20adf780
MD
188 }
189#if 0
bd252a04
MD
190 //if (URCU_TLS(nr_writes) % 100000 == 0) {
191 if (URCU_TLS(nr_writes) % 1000 == 0) {
20adf780 192 rcu_read_lock();
bd252a04 193 if (rand_r(&URCU_TLS(rand_lookup)) & 1) {
20adf780
MD
194 ht_resize(test_ht, 1);
195 } else {
196 ht_resize(test_ht, -1);
197 }
198 rcu_read_unlock();
199 }
200#endif //0
bd252a04 201 URCU_TLS(nr_writes)++;
20adf780
MD
202 if (caa_unlikely(!test_duration_write()))
203 break;
204 if (caa_unlikely(wdelay))
205 loop_sleep(wdelay);
bd252a04 206 if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
20adf780
MD
207 rcu_quiescent_state();
208 }
209
210 rcu_unregister_thread();
211
94df6318
MD
212 printf_verbose("thread_end %s, tid %lu\n",
213 "writer", urcu_get_thread_id());
214 printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, "
215 "nr_delnoent %lu\n", urcu_get_thread_id(),
216 URCU_TLS(nr_add),
217 URCU_TLS(nr_addexist),
218 URCU_TLS(nr_del),
bd252a04
MD
219 URCU_TLS(nr_delnoent));
220 count->update_ops = URCU_TLS(nr_writes);
221 count->add = URCU_TLS(nr_add);
222 count->add_exist = URCU_TLS(nr_addexist);
223 count->remove = URCU_TLS(nr_del);
20adf780
MD
224 return ((void*)2);
225}
226
227int test_hash_unique_populate_hash(void)
228{
229 struct lfht_test_node *node;
230 struct cds_lfht_node *ret_node;
231
232 printf("Starting uniqueness test.\n");
233
98f483d2
MD
234 URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL);
235
20adf780
MD
236 if (!init_populate)
237 return 0;
238
239 if (init_populate * 10 > init_pool_size) {
240 printf("WARNING: required to populate %lu nodes (-k), but random "
241"pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
242"larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size);
243 }
244
bd252a04 245 while (URCU_TLS(nr_add) < init_populate) {
20adf780
MD
246 node = malloc(sizeof(struct lfht_test_node));
247 lfht_test_node_init(node,
bd252a04 248 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % init_pool_size) + init_pool_offset),
20adf780
MD
249 sizeof(void *));
250 rcu_read_lock();
251 ret_node = cds_lfht_add_replace(test_ht,
252 test_hash(node->key, node->key_len, TEST_HASH_SEED),
253 test_match, node->key, &node->node);
254 rcu_read_unlock();
255 if (ret_node) {
256 call_rcu(&to_test_node(ret_node)->head, free_node_cb);
bd252a04 257 URCU_TLS(nr_addexist)++;
20adf780 258 } else {
bd252a04 259 URCU_TLS(nr_add)++;
20adf780 260 }
bd252a04 261 URCU_TLS(nr_writes)++;
20adf780
MD
262 }
263 return 0;
264}
This page took 0.050982 seconds and 4 git commands to generate.