Fix tests: finer-grained use of CPU_SET, CPU_ZERO and cpu_set_t
[urcu.git] / tests / test_urcu_hash_unique.c
1 /*
2 * test_urcu_hash_unique.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23 #define _GNU_SOURCE
24 #include "test_urcu_hash.h"
25
26 enum urcu_hash_addremove {
27 AR_RANDOM = 0,
28 AR_ADD = 1,
29 AR_REMOVE = -1,
30 }; /* 1: add, -1 remove, 0: random */
31
32 static enum urcu_hash_addremove addremove; /* 1: add, -1 remove, 0: random */
33
34 void test_hash_unique_sigusr1_handler(int signo)
35 {
36 switch (addremove) {
37 case AR_ADD:
38 printf("Add/Remove: random.\n");
39 addremove = AR_RANDOM;
40 break;
41 case AR_RANDOM:
42 printf("Add/Remove: remove only.\n");
43 addremove = AR_REMOVE;
44 break;
45 case AR_REMOVE:
46 printf("Add/Remove: add only.\n");
47 addremove = AR_ADD;
48 break;
49 }
50 }
51
52 void test_hash_unique_sigusr2_handler(int signo)
53 {
54 char msg[1] = { 0x42 };
55 ssize_t ret;
56
57 do {
58 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
59 } while (ret == -1L && errno == EINTR);
60 }
61
62 void *test_hash_unique_thr_reader(void *_count)
63 {
64 unsigned long long *count = _count;
65
66 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
67 "reader", (unsigned long) pthread_self(),
68 (unsigned long) gettid());
69
70 set_affinity();
71
72 rcu_register_thread();
73
74 while (!test_go)
75 {
76 }
77 cmm_smp_mb();
78
79 for (;;) {
80 struct lfht_test_node *node;
81 struct cds_lfht_iter iter;
82 /*
83 * iterate on whole table, ensuring that no duplicate is
84 * found.
85 */
86 rcu_read_lock();
87 cds_lfht_for_each_entry(test_ht, &iter, node, node) {
88 struct cds_lfht_iter dup_iter;
89
90 dup_iter = 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);
95 }
96 }
97 rcu_read_unlock();
98
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()))
104 break;
105 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
106 rcu_quiescent_state();
107 }
108
109 rcu_unregister_thread();
110
111 *count = URCU_TLS(nr_reads);
112 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
113 "reader", (unsigned long) pthread_self(),
114 (unsigned long) gettid());
115 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
116 pthread_self(), URCU_TLS(lookup_fail),
117 URCU_TLS(lookup_ok));
118 return ((void*)1);
119
120 }
121
122 void *test_hash_unique_thr_writer(void *_count)
123 {
124 struct lfht_test_node *node;
125 struct cds_lfht_node *ret_node;
126 struct cds_lfht_iter iter;
127 struct wr_count *count = _count;
128 int ret;
129 int loc_add_unique;
130
131 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
132 "writer", (unsigned long) pthread_self(),
133 (unsigned long) gettid());
134
135 set_affinity();
136
137 rcu_register_thread();
138
139 while (!test_go)
140 {
141 }
142 cmm_smp_mb();
143
144 for (;;) {
145 /*
146 * add unique/add replace with new node key from range.
147 */
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),
153 sizeof(void *));
154 rcu_read_lock();
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);
160 } else {
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),
167 &node->node);
168 ret_node = NULL;
169 #endif //0
170 }
171 rcu_read_unlock();
172 if (loc_add_unique) {
173 if (ret_node != &node->node) {
174 free(node);
175 URCU_TLS(nr_addexist)++;
176 } else {
177 URCU_TLS(nr_add)++;
178 }
179 } else {
180 if (ret_node) {
181 call_rcu(&to_test_node(ret_node)->head,
182 free_node_cb);
183 URCU_TLS(nr_addexist)++;
184 } else {
185 URCU_TLS(nr_add)++;
186 }
187 }
188 } else {
189 /* May delete */
190 rcu_read_lock();
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));
195 rcu_read_unlock();
196 if (ret == 0) {
197 node = cds_lfht_iter_get_test_node(&iter);
198 call_rcu(&node->head, free_node_cb);
199 URCU_TLS(nr_del)++;
200 } else
201 URCU_TLS(nr_delnoent)++;
202 }
203 #if 0
204 //if (URCU_TLS(nr_writes) % 100000 == 0) {
205 if (URCU_TLS(nr_writes) % 1000 == 0) {
206 rcu_read_lock();
207 if (rand_r(&URCU_TLS(rand_lookup)) & 1) {
208 ht_resize(test_ht, 1);
209 } else {
210 ht_resize(test_ht, -1);
211 }
212 rcu_read_unlock();
213 }
214 #endif //0
215 URCU_TLS(nr_writes)++;
216 if (caa_unlikely(!test_duration_write()))
217 break;
218 if (caa_unlikely(wdelay))
219 loop_sleep(wdelay);
220 if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
221 rcu_quiescent_state();
222 }
223
224 rcu_unregister_thread();
225
226 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
227 "writer", (unsigned long) pthread_self(),
228 (unsigned long) gettid());
229 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
230 "nr_delnoent %lu\n", (unsigned long) pthread_self(), URCU_TLS(nr_add),
231 URCU_TLS(nr_addexist), URCU_TLS(nr_del),
232 URCU_TLS(nr_delnoent));
233 count->update_ops = URCU_TLS(nr_writes);
234 count->add = URCU_TLS(nr_add);
235 count->add_exist = URCU_TLS(nr_addexist);
236 count->remove = URCU_TLS(nr_del);
237 return ((void*)2);
238 }
239
240 int test_hash_unique_populate_hash(void)
241 {
242 struct lfht_test_node *node;
243 struct cds_lfht_node *ret_node;
244
245 printf("Starting uniqueness test.\n");
246
247 if (!init_populate)
248 return 0;
249
250 if (init_populate * 10 > init_pool_size) {
251 printf("WARNING: required to populate %lu nodes (-k), but random "
252 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
253 "larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size);
254 }
255
256 while (URCU_TLS(nr_add) < init_populate) {
257 node = malloc(sizeof(struct lfht_test_node));
258 lfht_test_node_init(node,
259 (void *)(((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % init_pool_size) + init_pool_offset),
260 sizeof(void *));
261 rcu_read_lock();
262 ret_node = cds_lfht_add_replace(test_ht,
263 test_hash(node->key, node->key_len, TEST_HASH_SEED),
264 test_match, node->key, &node->node);
265 rcu_read_unlock();
266 if (ret_node) {
267 call_rcu(&to_test_node(ret_node)->head, free_node_cb);
268 URCU_TLS(nr_addexist)++;
269 } else {
270 URCU_TLS(nr_add)++;
271 }
272 URCU_TLS(nr_writes)++;
273 }
274 return 0;
275 }
This page took 0.046072 seconds and 4 git commands to generate.