rculfhash tests: modularize 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 int count_pipe[2];
173
174 static inline void loop_sleep(unsigned long l)
175 {
176 while(l-- != 0)
177 caa_cpu_relax();
178 }
179
180 extern int verbose_mode;
181
182 #define printf_verbose(fmt, args...) \
183 do { \
184 if (verbose_mode) \
185 printf(fmt, ## args); \
186 } while (0)
187
188 extern unsigned int cpu_affinities[NR_CPUS];
189 extern unsigned int next_aff;
190 extern int use_affinity;
191
192 extern pthread_mutex_t affinity_mutex;
193
194 #ifndef HAVE_CPU_SET_T
195 typedef unsigned long cpu_set_t;
196 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
197 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
198 #endif
199
200 void set_affinity(void);
201
202 /* rw test */
203 void test_hash_rw_sigusr1_handler(int signo);
204 void test_hash_rw_sigusr2_handler(int signo);
205 void *test_hash_rw_thr_reader(void *_count);
206 void *test_hash_rw_thr_writer(void *_count);
207
208 /*
209 * returns 0 if test should end.
210 */
211 static inline int test_duration_write(void)
212 {
213 return !test_stop;
214 }
215
216 static inline int test_duration_read(void)
217 {
218 return !test_stop;
219 }
220
221 extern unsigned long long __thread nr_writes;
222 extern unsigned long long __thread nr_reads;
223
224 extern unsigned int nr_readers;
225 extern unsigned int nr_writers;
226
227 void rcu_copy_mutex_lock(void);
228 void rcu_copy_mutex_unlock(void);
229
230 /*
231 * Hash function
232 * Source: http://burtleburtle.net/bob/c/lookup3.c
233 * Originally Public Domain
234 */
235
236 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
237
238 #define mix(a, b, c) \
239 do { \
240 a -= c; a ^= rot(c, 4); c += b; \
241 b -= a; b ^= rot(a, 6); a += c; \
242 c -= b; c ^= rot(b, 8); b += a; \
243 a -= c; a ^= rot(c, 16); c += b; \
244 b -= a; b ^= rot(a, 19); a += c; \
245 c -= b; c ^= rot(b, 4); b += a; \
246 } while (0)
247
248 #define final(a, b, c) \
249 { \
250 c ^= b; c -= rot(b, 14); \
251 a ^= c; a -= rot(c, 11); \
252 b ^= a; b -= rot(a, 25); \
253 c ^= b; c -= rot(b, 16); \
254 a ^= c; a -= rot(c, 4);\
255 b ^= a; b -= rot(a, 14); \
256 c ^= b; c -= rot(b, 24); \
257 }
258
259 static inline __attribute__((unused))
260 uint32_t hash_u32(
261 const uint32_t *k, /* the key, an array of uint32_t values */
262 size_t length, /* the length of the key, in uint32_ts */
263 uint32_t initval) /* the previous hash, or an arbitrary value */
264 {
265 uint32_t a, b, c;
266
267 /* Set up the internal state */
268 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
269
270 /*----------------------------------------- handle most of the key */
271 while (length > 3) {
272 a += k[0];
273 b += k[1];
274 c += k[2];
275 mix(a, b, c);
276 length -= 3;
277 k += 3;
278 }
279
280 /*----------------------------------- handle the last 3 uint32_t's */
281 switch (length) { /* all the case statements fall through */
282 case 3: c += k[2];
283 case 2: b += k[1];
284 case 1: a += k[0];
285 final(a, b, c);
286 case 0: /* case 0: nothing left to add */
287 break;
288 }
289 /*---------------------------------------------- report the result */
290 return c;
291 }
292
293 static inline
294 void hashword2(
295 const uint32_t *k, /* the key, an array of uint32_t values */
296 size_t length, /* the length of the key, in uint32_ts */
297 uint32_t *pc, /* IN: seed OUT: primary hash value */
298 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
299 {
300 uint32_t a, b, c;
301
302 /* Set up the internal state */
303 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
304 c += *pb;
305
306 /*----------------------------------------- handle most of the key */
307 while (length > 3) {
308 a += k[0];
309 b += k[1];
310 c += k[2];
311 mix(a, b, c);
312 length -= 3;
313 k += 3;
314 }
315
316 /*----------------------------------- handle the last 3 uint32_t's */
317 switch (length) { /* all the case statements fall through */
318 case 3: c += k[2];
319 case 2: b += k[1];
320 case 1: a += k[0];
321 final(a, b, c);
322 case 0: /* case 0: nothing left to add */
323 break;
324 }
325 /*---------------------------------------------- report the result */
326 *pc = c;
327 *pb = b;
328 }
329
330 #if (CAA_BITS_PER_LONG == 32)
331 static inline
332 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
333 {
334 unsigned int key = (unsigned int) _key;
335
336 assert(length == sizeof(unsigned int));
337 return hash_u32(&key, 1, seed);
338 }
339 #else
340 static inline
341 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
342 {
343 union {
344 uint64_t v64;
345 uint32_t v32[2];
346 } v;
347 union {
348 uint64_t v64;
349 uint32_t v32[2];
350 } key;
351
352 assert(length == sizeof(unsigned long));
353 v.v64 = (uint64_t) seed;
354 key.v64 = (uint64_t) _key;
355 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
356 return v.v64;
357 }
358 #endif
359
360 unsigned long test_compare(const void *key1, size_t key1_len,
361 const void *key2, size_t key2_len);
362
363 static inline
364 int test_match(struct cds_lfht_node *node, const void *key)
365 {
366 struct lfht_test_node *test_node = to_test_node(node);
367
368 return !test_compare(test_node->key, test_node->key_len,
369 key, sizeof(unsigned long));
370 }
371
372 static inline
373 void cds_lfht_test_lookup(struct cds_lfht *ht, void *key, size_t key_len,
374 struct cds_lfht_iter *iter)
375 {
376 assert(key_len == sizeof(unsigned long));
377
378 cds_lfht_lookup(ht, test_hash(key, key_len, TEST_HASH_SEED),
379 test_match, key, iter);
380 }
381
382 void free_node_cb(struct rcu_head *head);
383
384 #endif /* _TEST_URCU_HASH_H */
This page took 0.037108 seconds and 5 git commands to generate.