Commit | Line | Data |
---|---|---|
18ca7a5b MD |
1 | #ifndef _TEST_URCU_HASH_H |
2 | #define _TEST_URCU_HASH_H | |
3 | ||
4 | /* | |
5 | * test_urcu_hash.h | |
6 | * | |
7 | * Userspace RCU library - test program | |
8 | * | |
9 | * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
22 | * with this program; if not, write to the Free Software Foundation, Inc., | |
23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
24 | */ | |
25 | ||
18ca7a5b MD |
26 | #include <stdio.h> |
27 | #include <pthread.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/wait.h> | |
32 | #include <unistd.h> | |
33 | #include <stdio.h> | |
34 | #include <assert.h> | |
18ca7a5b MD |
35 | #include <errno.h> |
36 | #include <signal.h> | |
37 | ||
bd252a04 | 38 | #include <urcu/tls-compat.h> |
094c8c59 | 39 | #include <compat-rand.h> |
2953b501 | 40 | #include "cpuset.h" |
94df6318 | 41 | #include "thread-id.h" |
2650042a | 42 | #include "../common/debug-yield.h" |
18ca7a5b MD |
43 | |
44 | #define DEFAULT_HASH_SIZE 32 | |
45 | #define DEFAULT_MIN_ALLOC_SIZE 1 | |
46 | #define DEFAULT_RAND_POOL 1000000 | |
47 | ||
48 | /* | |
49 | * Note: the hash seed should be a random value for hash tables | |
50 | * targeting production environments to provide protection against | |
51 | * denial of service attacks. We keep it a static value within this test | |
52 | * program to compare identical benchmark runs. | |
53 | */ | |
54 | #define TEST_HASH_SEED 0x42UL | |
55 | ||
18ca7a5b MD |
56 | /* hardcoded number of CPUs */ |
57 | #define NR_CPUS 16384 | |
58 | ||
59 | #ifdef POISON_FREE | |
60 | #define poison_free(ptr) \ | |
61 | do { \ | |
62 | memset(ptr, 0x42, sizeof(*(ptr))); \ | |
63 | free(ptr); \ | |
64 | } while (0) | |
65 | #else | |
66 | #define poison_free(ptr) free(ptr) | |
67 | #endif | |
68 | ||
18ca7a5b MD |
69 | #ifndef DYNAMIC_LINK_TEST |
70 | #define _LGPL_SOURCE | |
71 | #else | |
72 | #define debug_yield_read() | |
73 | #endif | |
74 | #include <urcu-qsbr.h> | |
75 | #include <urcu/rculfhash.h> | |
76 | #include <urcu-call-rcu.h> | |
77 | ||
78 | struct wr_count { | |
79 | unsigned long update_ops; | |
80 | unsigned long add; | |
81 | unsigned long add_exist; | |
82 | unsigned long remove; | |
83 | }; | |
84 | ||
bd252a04 MD |
85 | extern DECLARE_URCU_TLS(unsigned int, rand_lookup); |
86 | extern DECLARE_URCU_TLS(unsigned long, nr_add); | |
87 | extern DECLARE_URCU_TLS(unsigned long, nr_addexist); | |
88 | extern DECLARE_URCU_TLS(unsigned long, nr_del); | |
89 | extern DECLARE_URCU_TLS(unsigned long, nr_delnoent); | |
90 | extern DECLARE_URCU_TLS(unsigned long, lookup_fail); | |
91 | extern DECLARE_URCU_TLS(unsigned long, lookup_ok); | |
18ca7a5b MD |
92 | |
93 | extern struct cds_lfht *test_ht; | |
94 | ||
95 | struct test_data { | |
96 | int a; | |
97 | int b; | |
98 | }; | |
99 | ||
100 | struct lfht_test_node { | |
101 | struct cds_lfht_node node; | |
102 | void *key; | |
103 | unsigned int key_len; | |
104 | /* cache-cold for iteration */ | |
105 | struct rcu_head head; | |
106 | }; | |
107 | ||
108 | static inline struct lfht_test_node * | |
109 | to_test_node(struct cds_lfht_node *node) | |
110 | { | |
111 | return caa_container_of(node, struct lfht_test_node, node); | |
112 | } | |
113 | ||
114 | static inline | |
115 | void lfht_test_node_init(struct lfht_test_node *node, void *key, | |
116 | size_t key_len) | |
117 | { | |
118 | cds_lfht_node_init(&node->node); | |
119 | node->key = key; | |
120 | node->key_len = key_len; | |
121 | } | |
122 | ||
123 | static inline struct lfht_test_node * | |
124 | cds_lfht_iter_get_test_node(struct cds_lfht_iter *iter) | |
125 | { | |
126 | return to_test_node(cds_lfht_iter_get_node(iter)); | |
127 | } | |
128 | ||
129 | extern volatile int test_go, test_stop; | |
130 | ||
131 | extern unsigned long wdelay; | |
132 | ||
133 | extern unsigned long duration; | |
134 | ||
135 | /* read-side C.S. duration, in loops */ | |
136 | extern unsigned long rduration; | |
137 | ||
138 | extern unsigned long init_hash_size; | |
139 | extern unsigned long min_hash_alloc_size; | |
140 | extern unsigned long max_hash_buckets_size; | |
141 | extern unsigned long init_populate; | |
142 | extern int opt_auto_resize; | |
143 | extern int add_only, add_unique, add_replace; | |
144 | extern const struct cds_lfht_mm_type *memory_backend; | |
145 | ||
146 | extern unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset; | |
147 | extern unsigned long init_pool_size, | |
148 | lookup_pool_size, | |
149 | write_pool_size; | |
150 | extern int validate_lookup; | |
151 | ||
495913bf MD |
152 | extern unsigned long nr_hash_chains; |
153 | ||
18ca7a5b MD |
154 | extern int count_pipe[2]; |
155 | ||
ab0aacbe | 156 | static inline void loop_sleep(unsigned long loops) |
18ca7a5b | 157 | { |
ab0aacbe | 158 | while (loops-- != 0) |
18ca7a5b MD |
159 | caa_cpu_relax(); |
160 | } | |
161 | ||
162 | extern int verbose_mode; | |
163 | ||
164 | #define printf_verbose(fmt, args...) \ | |
165 | do { \ | |
166 | if (verbose_mode) \ | |
167 | printf(fmt, ## args); \ | |
168 | } while (0) | |
169 | ||
170 | extern unsigned int cpu_affinities[NR_CPUS]; | |
171 | extern unsigned int next_aff; | |
172 | extern int use_affinity; | |
173 | ||
174 | extern pthread_mutex_t affinity_mutex; | |
175 | ||
18ca7a5b MD |
176 | void set_affinity(void); |
177 | ||
18ca7a5b MD |
178 | /* |
179 | * returns 0 if test should end. | |
180 | */ | |
181 | static inline int test_duration_write(void) | |
182 | { | |
183 | return !test_stop; | |
184 | } | |
185 | ||
186 | static inline int test_duration_read(void) | |
187 | { | |
188 | return !test_stop; | |
189 | } | |
190 | ||
bd252a04 MD |
191 | extern DECLARE_URCU_TLS(unsigned long long, nr_writes); |
192 | extern DECLARE_URCU_TLS(unsigned long long, nr_reads); | |
18ca7a5b MD |
193 | |
194 | extern unsigned int nr_readers; | |
195 | extern unsigned int nr_writers; | |
196 | ||
197 | void rcu_copy_mutex_lock(void); | |
198 | void rcu_copy_mutex_unlock(void); | |
199 | ||
200 | /* | |
201 | * Hash function | |
202 | * Source: http://burtleburtle.net/bob/c/lookup3.c | |
203 | * Originally Public Domain | |
204 | */ | |
205 | ||
206 | #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | |
207 | ||
208 | #define mix(a, b, c) \ | |
209 | do { \ | |
210 | a -= c; a ^= rot(c, 4); c += b; \ | |
211 | b -= a; b ^= rot(a, 6); a += c; \ | |
212 | c -= b; c ^= rot(b, 8); b += a; \ | |
213 | a -= c; a ^= rot(c, 16); c += b; \ | |
214 | b -= a; b ^= rot(a, 19); a += c; \ | |
215 | c -= b; c ^= rot(b, 4); b += a; \ | |
216 | } while (0) | |
217 | ||
218 | #define final(a, b, c) \ | |
219 | { \ | |
220 | c ^= b; c -= rot(b, 14); \ | |
221 | a ^= c; a -= rot(c, 11); \ | |
222 | b ^= a; b -= rot(a, 25); \ | |
223 | c ^= b; c -= rot(b, 16); \ | |
224 | a ^= c; a -= rot(c, 4);\ | |
225 | b ^= a; b -= rot(a, 14); \ | |
226 | c ^= b; c -= rot(b, 24); \ | |
227 | } | |
228 | ||
229 | static inline __attribute__((unused)) | |
230 | uint32_t hash_u32( | |
231 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
232 | size_t length, /* the length of the key, in uint32_ts */ | |
233 | uint32_t initval) /* the previous hash, or an arbitrary value */ | |
234 | { | |
235 | uint32_t a, b, c; | |
236 | ||
237 | /* Set up the internal state */ | |
238 | a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval; | |
239 | ||
240 | /*----------------------------------------- handle most of the key */ | |
241 | while (length > 3) { | |
242 | a += k[0]; | |
243 | b += k[1]; | |
244 | c += k[2]; | |
245 | mix(a, b, c); | |
246 | length -= 3; | |
247 | k += 3; | |
248 | } | |
249 | ||
250 | /*----------------------------------- handle the last 3 uint32_t's */ | |
251 | switch (length) { /* all the case statements fall through */ | |
8771d88d MJ |
252 | case 3: c += k[2]; /* fall through */ |
253 | case 2: b += k[1]; /* fall through */ | |
18ca7a5b MD |
254 | case 1: a += k[0]; |
255 | final(a, b, c); | |
8771d88d | 256 | /* fall through */ |
18ca7a5b MD |
257 | case 0: /* case 0: nothing left to add */ |
258 | break; | |
259 | } | |
260 | /*---------------------------------------------- report the result */ | |
261 | return c; | |
262 | } | |
263 | ||
264 | static inline | |
265 | void hashword2( | |
266 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
267 | size_t length, /* the length of the key, in uint32_ts */ | |
268 | uint32_t *pc, /* IN: seed OUT: primary hash value */ | |
269 | uint32_t *pb) /* IN: more seed OUT: secondary hash value */ | |
270 | { | |
271 | uint32_t a, b, c; | |
272 | ||
273 | /* Set up the internal state */ | |
274 | a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc; | |
275 | c += *pb; | |
276 | ||
277 | /*----------------------------------------- handle most of the key */ | |
278 | while (length > 3) { | |
279 | a += k[0]; | |
280 | b += k[1]; | |
281 | c += k[2]; | |
282 | mix(a, b, c); | |
283 | length -= 3; | |
284 | k += 3; | |
285 | } | |
286 | ||
287 | /*----------------------------------- handle the last 3 uint32_t's */ | |
288 | switch (length) { /* all the case statements fall through */ | |
447c9339 MJ |
289 | case 3: c += k[2]; /* fall through */ |
290 | case 2: b += k[1]; /* fall through */ | |
18ca7a5b MD |
291 | case 1: a += k[0]; |
292 | final(a, b, c); | |
447c9339 | 293 | /* fall through */ |
18ca7a5b MD |
294 | case 0: /* case 0: nothing left to add */ |
295 | break; | |
296 | } | |
297 | /*---------------------------------------------- report the result */ | |
298 | *pc = c; | |
299 | *pb = b; | |
300 | } | |
301 | ||
302 | #if (CAA_BITS_PER_LONG == 32) | |
303 | static inline | |
495913bf | 304 | unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed) |
18ca7a5b MD |
305 | { |
306 | unsigned int key = (unsigned int) _key; | |
307 | ||
308 | assert(length == sizeof(unsigned int)); | |
309 | return hash_u32(&key, 1, seed); | |
310 | } | |
311 | #else | |
312 | static inline | |
495913bf | 313 | unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed) |
18ca7a5b MD |
314 | { |
315 | union { | |
316 | uint64_t v64; | |
317 | uint32_t v32[2]; | |
318 | } v; | |
319 | union { | |
320 | uint64_t v64; | |
321 | uint32_t v32[2]; | |
322 | } key; | |
323 | ||
324 | assert(length == sizeof(unsigned long)); | |
325 | v.v64 = (uint64_t) seed; | |
326 | key.v64 = (uint64_t) _key; | |
327 | hashword2(key.v32, 2, &v.v32[0], &v.v32[1]); | |
328 | return v.v64; | |
329 | } | |
330 | #endif | |
331 | ||
495913bf MD |
332 | /* |
333 | * Hash function with nr_hash_chains != 0 for testing purpose only! | |
334 | * Creates very long hash chains, deteriorating the hash table into a | |
335 | * few linked lists, depending on the nr_hash_chains value. The purpose | |
336 | * of this test is to check how the hash table behaves with hash chains | |
337 | * containing different values, which is a rare case in a normal hash | |
338 | * table. | |
339 | */ | |
340 | static inline | |
341 | unsigned long test_hash(const void *_key, size_t length, | |
342 | unsigned long seed) | |
343 | { | |
344 | if (nr_hash_chains == 0) { | |
345 | return test_hash_mix(_key, length, seed); | |
346 | } else { | |
347 | unsigned long v; | |
348 | ||
349 | assert(length == sizeof(unsigned long)); | |
350 | v = (unsigned long) _key; | |
351 | return v % nr_hash_chains; | |
352 | } | |
353 | } | |
354 | ||
18ca7a5b MD |
355 | unsigned long test_compare(const void *key1, size_t key1_len, |
356 | const void *key2, size_t key2_len); | |
357 | ||
358 | static inline | |
359 | int test_match(struct cds_lfht_node *node, const void *key) | |
360 | { | |
361 | struct lfht_test_node *test_node = to_test_node(node); | |
362 | ||
363 | return !test_compare(test_node->key, test_node->key_len, | |
364 | key, sizeof(unsigned long)); | |
365 | } | |
366 | ||
367 | static inline | |
368 | void cds_lfht_test_lookup(struct cds_lfht *ht, void *key, size_t key_len, | |
369 | struct cds_lfht_iter *iter) | |
370 | { | |
371 | assert(key_len == sizeof(unsigned long)); | |
372 | ||
373 | cds_lfht_lookup(ht, test_hash(key, key_len, TEST_HASH_SEED), | |
374 | test_match, key, iter); | |
375 | } | |
376 | ||
377 | void free_node_cb(struct rcu_head *head); | |
378 | ||
f52c1ef7 MD |
379 | /* rw test */ |
380 | void test_hash_rw_sigusr1_handler(int signo); | |
381 | void test_hash_rw_sigusr2_handler(int signo); | |
382 | void *test_hash_rw_thr_reader(void *_count); | |
383 | void *test_hash_rw_thr_writer(void *_count); | |
384 | int test_hash_rw_populate_hash(void); | |
385 | ||
20adf780 MD |
386 | /* unique test */ |
387 | void test_hash_unique_sigusr1_handler(int signo); | |
388 | void test_hash_unique_sigusr2_handler(int signo); | |
389 | void *test_hash_unique_thr_reader(void *_count); | |
390 | void *test_hash_unique_thr_writer(void *_count); | |
391 | int test_hash_unique_populate_hash(void); | |
392 | ||
18ca7a5b | 393 | #endif /* _TEST_URCU_HASH_H */ |