rculfhash tests: modularize tests
[urcu.git] / tests / test_urcu_hash_rw.c
CommitLineData
18ca7a5b
MD
1/*
2 * test_urcu_hash_rw.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
26enum urcu_hash_addremove {
27 AR_RANDOM = 0,
28 AR_ADD = 1,
29 AR_REMOVE = -1,
30}; /* 1: add, -1 remove, 0: random */
31
32static enum urcu_hash_addremove addremove; /* 1: add, -1 remove, 0: random */
33
34void test_hash_rw_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
52void test_hash_rw_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
62void *test_hash_rw_thr_reader(void *_count)
63{
64 unsigned long long *count = _count;
65 struct lfht_test_node *node;
66 struct cds_lfht_iter iter;
67
68 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
69 "reader", pthread_self(), (unsigned long)gettid());
70
71 set_affinity();
72
73 rcu_register_thread();
74
75 while (!test_go)
76 {
77 }
78 cmm_smp_mb();
79
80 for (;;) {
81 rcu_read_lock();
82 cds_lfht_test_lookup(test_ht,
83 (void *)(((unsigned long) rand_r(&rand_lookup) % lookup_pool_size) + lookup_pool_offset),
84 sizeof(void *), &iter);
85 node = cds_lfht_iter_get_test_node(&iter);
86 if (node == NULL) {
87 if (validate_lookup) {
88 printf("[ERROR] Lookup cannot find initial node.\n");
89 exit(-1);
90 }
91 lookup_fail++;
92 } else {
93 lookup_ok++;
94 }
95 debug_yield_read();
96 if (caa_unlikely(rduration))
97 loop_sleep(rduration);
98 rcu_read_unlock();
99 nr_reads++;
100 if (caa_unlikely(!test_duration_read()))
101 break;
102 if (caa_unlikely((nr_reads & ((1 << 10) - 1)) == 0))
103 rcu_quiescent_state();
104 }
105
106 rcu_unregister_thread();
107
108 *count = nr_reads;
109 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
110 "reader", pthread_self(), (unsigned long)gettid());
111 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
112 pthread_self(), lookup_fail, lookup_ok);
113 return ((void*)1);
114
115}
116
117void *test_hash_rw_thr_writer(void *_count)
118{
119 struct lfht_test_node *node;
120 struct cds_lfht_node *ret_node;
121 struct cds_lfht_iter iter;
122 struct wr_count *count = _count;
123 int ret;
124
125 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
126 "writer", pthread_self(), (unsigned long)gettid());
127
128 set_affinity();
129
130 rcu_register_thread();
131
132 while (!test_go)
133 {
134 }
135 cmm_smp_mb();
136
137 for (;;) {
138 if ((addremove == AR_ADD || add_only)
139 || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) {
140 node = malloc(sizeof(struct lfht_test_node));
141 lfht_test_node_init(node,
142 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
143 sizeof(void *));
144 rcu_read_lock();
145 if (add_unique) {
146 ret_node = cds_lfht_add_unique(test_ht,
147 test_hash(node->key, node->key_len, TEST_HASH_SEED),
148 test_match, node->key, &node->node);
149 } else {
150 if (add_replace)
151 ret_node = cds_lfht_add_replace(test_ht,
152 test_hash(node->key, node->key_len, TEST_HASH_SEED),
153 test_match, node->key, &node->node);
154 else
155 cds_lfht_add(test_ht,
156 test_hash(node->key, node->key_len, TEST_HASH_SEED),
157 &node->node);
158 }
159 rcu_read_unlock();
160 if (add_unique && ret_node != &node->node) {
161 free(node);
162 nr_addexist++;
163 } else {
164 if (add_replace && ret_node) {
165 call_rcu(&to_test_node(ret_node)->head,
166 free_node_cb);
167 nr_addexist++;
168 } else {
169 nr_add++;
170 }
171 }
172 } else {
173 /* May delete */
174 rcu_read_lock();
175 cds_lfht_test_lookup(test_ht,
176 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
177 sizeof(void *), &iter);
178 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
179 rcu_read_unlock();
180 if (ret == 0) {
181 node = cds_lfht_iter_get_test_node(&iter);
182 call_rcu(&node->head, free_node_cb);
183 nr_del++;
184 } else
185 nr_delnoent++;
186 }
187#if 0
188 //if (nr_writes % 100000 == 0) {
189 if (nr_writes % 1000 == 0) {
190 rcu_read_lock();
191 if (rand_r(&rand_lookup) & 1) {
192 ht_resize(test_ht, 1);
193 } else {
194 ht_resize(test_ht, -1);
195 }
196 rcu_read_unlock();
197 }
198#endif //0
199 nr_writes++;
200 if (caa_unlikely(!test_duration_write()))
201 break;
202 if (caa_unlikely(wdelay))
203 loop_sleep(wdelay);
204 if (caa_unlikely((nr_writes & ((1 << 10) - 1)) == 0))
205 rcu_quiescent_state();
206 }
207
208 rcu_unregister_thread();
209
210 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
211 "writer", pthread_self(), (unsigned long)gettid());
212 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
213 "nr_delnoent %lu\n", pthread_self(), nr_add,
214 nr_addexist, nr_del, nr_delnoent);
215 count->update_ops = nr_writes;
216 count->add = nr_add;
217 count->add_exist = nr_addexist;
218 count->remove = nr_del;
219 return ((void*)2);
220}
221
222int test_hash_rw_populate_hash(void)
223{
224 struct lfht_test_node *node;
225 struct cds_lfht_node *ret_node;
226
227 if (!init_populate)
228 return 0;
229
230 if ((add_unique || add_replace) && init_populate * 10 > init_pool_size) {
231 printf("WARNING: required to populate %lu nodes (-k), but random "
232"pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
233"larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size);
234 }
235
236 while (nr_add < init_populate) {
237 node = malloc(sizeof(struct lfht_test_node));
238 lfht_test_node_init(node,
239 (void *)(((unsigned long) rand_r(&rand_lookup) % init_pool_size) + init_pool_offset),
240 sizeof(void *));
241 rcu_read_lock();
242 if (add_unique) {
243 ret_node = cds_lfht_add_unique(test_ht,
244 test_hash(node->key, node->key_len, TEST_HASH_SEED),
245 test_match, node->key, &node->node);
246 } else {
247 if (add_replace)
248 ret_node = cds_lfht_add_replace(test_ht,
249 test_hash(node->key, node->key_len, TEST_HASH_SEED),
250 test_match, node->key, &node->node);
251 else
252 cds_lfht_add(test_ht,
253 test_hash(node->key, node->key_len, TEST_HASH_SEED),
254 &node->node);
255 }
256 rcu_read_unlock();
257 if (add_unique && ret_node != &node->node) {
258 free(node);
259 nr_addexist++;
260 } else {
261 if (add_replace && ret_node) {
262 call_rcu(&to_test_node(ret_node)->head, free_node_cb);
263 nr_addexist++;
264 } else {
265 nr_add++;
266 }
267 }
268 nr_writes++;
269 }
270 return 0;
271}
This page took 0.032028 seconds and 4 git commands to generate.