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