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