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