lfstack: implement test
[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 #include <urcu/tls-compat.h>
41
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
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);
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
174 extern unsigned long nr_hash_chains;
175
176 extern int count_pipe[2];
177
178 static inline void loop_sleep(unsigned long loops)
179 {
180 while (loops-- != 0)
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
198 #ifndef HAVE_CPU_SET_T
199 typedef unsigned long cpu_set_t;
200 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
201 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
202 #endif
203
204 void set_affinity(void);
205
206 /*
207 * returns 0 if test should end.
208 */
209 static inline int test_duration_write(void)
210 {
211 return !test_stop;
212 }
213
214 static inline int test_duration_read(void)
215 {
216 return !test_stop;
217 }
218
219 extern DECLARE_URCU_TLS(unsigned long long, nr_writes);
220 extern DECLARE_URCU_TLS(unsigned long long, nr_reads);
221
222 extern unsigned int nr_readers;
223 extern unsigned int nr_writers;
224
225 void rcu_copy_mutex_lock(void);
226 void rcu_copy_mutex_unlock(void);
227
228 /*
229 * Hash function
230 * Source: http://burtleburtle.net/bob/c/lookup3.c
231 * Originally Public Domain
232 */
233
234 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
235
236 #define mix(a, b, c) \
237 do { \
238 a -= c; a ^= rot(c, 4); c += b; \
239 b -= a; b ^= rot(a, 6); a += c; \
240 c -= b; c ^= rot(b, 8); b += a; \
241 a -= c; a ^= rot(c, 16); c += b; \
242 b -= a; b ^= rot(a, 19); a += c; \
243 c -= b; c ^= rot(b, 4); b += a; \
244 } while (0)
245
246 #define final(a, b, c) \
247 { \
248 c ^= b; c -= rot(b, 14); \
249 a ^= c; a -= rot(c, 11); \
250 b ^= a; b -= rot(a, 25); \
251 c ^= b; c -= rot(b, 16); \
252 a ^= c; a -= rot(c, 4);\
253 b ^= a; b -= rot(a, 14); \
254 c ^= b; c -= rot(b, 24); \
255 }
256
257 static inline __attribute__((unused))
258 uint32_t hash_u32(
259 const uint32_t *k, /* the key, an array of uint32_t values */
260 size_t length, /* the length of the key, in uint32_ts */
261 uint32_t initval) /* the previous hash, or an arbitrary value */
262 {
263 uint32_t a, b, c;
264
265 /* Set up the internal state */
266 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
267
268 /*----------------------------------------- handle most of the key */
269 while (length > 3) {
270 a += k[0];
271 b += k[1];
272 c += k[2];
273 mix(a, b, c);
274 length -= 3;
275 k += 3;
276 }
277
278 /*----------------------------------- handle the last 3 uint32_t's */
279 switch (length) { /* all the case statements fall through */
280 case 3: c += k[2];
281 case 2: b += k[1];
282 case 1: a += k[0];
283 final(a, b, c);
284 case 0: /* case 0: nothing left to add */
285 break;
286 }
287 /*---------------------------------------------- report the result */
288 return c;
289 }
290
291 static inline
292 void hashword2(
293 const uint32_t *k, /* the key, an array of uint32_t values */
294 size_t length, /* the length of the key, in uint32_ts */
295 uint32_t *pc, /* IN: seed OUT: primary hash value */
296 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
297 {
298 uint32_t a, b, c;
299
300 /* Set up the internal state */
301 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
302 c += *pb;
303
304 /*----------------------------------------- handle most of the key */
305 while (length > 3) {
306 a += k[0];
307 b += k[1];
308 c += k[2];
309 mix(a, b, c);
310 length -= 3;
311 k += 3;
312 }
313
314 /*----------------------------------- handle the last 3 uint32_t's */
315 switch (length) { /* all the case statements fall through */
316 case 3: c += k[2];
317 case 2: b += k[1];
318 case 1: a += k[0];
319 final(a, b, c);
320 case 0: /* case 0: nothing left to add */
321 break;
322 }
323 /*---------------------------------------------- report the result */
324 *pc = c;
325 *pb = b;
326 }
327
328 #if (CAA_BITS_PER_LONG == 32)
329 static inline
330 unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed)
331 {
332 unsigned int key = (unsigned int) _key;
333
334 assert(length == sizeof(unsigned int));
335 return hash_u32(&key, 1, seed);
336 }
337 #else
338 static inline
339 unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed)
340 {
341 union {
342 uint64_t v64;
343 uint32_t v32[2];
344 } v;
345 union {
346 uint64_t v64;
347 uint32_t v32[2];
348 } key;
349
350 assert(length == sizeof(unsigned long));
351 v.v64 = (uint64_t) seed;
352 key.v64 = (uint64_t) _key;
353 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
354 return v.v64;
355 }
356 #endif
357
358 /*
359 * Hash function with nr_hash_chains != 0 for testing purpose only!
360 * Creates very long hash chains, deteriorating the hash table into a
361 * few linked lists, depending on the nr_hash_chains value. The purpose
362 * of this test is to check how the hash table behaves with hash chains
363 * containing different values, which is a rare case in a normal hash
364 * table.
365 */
366 static inline
367 unsigned long test_hash(const void *_key, size_t length,
368 unsigned long seed)
369 {
370 if (nr_hash_chains == 0) {
371 return test_hash_mix(_key, length, seed);
372 } else {
373 unsigned long v;
374
375 assert(length == sizeof(unsigned long));
376 v = (unsigned long) _key;
377 return v % nr_hash_chains;
378 }
379 }
380
381 unsigned long test_compare(const void *key1, size_t key1_len,
382 const void *key2, size_t key2_len);
383
384 static inline
385 int test_match(struct cds_lfht_node *node, const void *key)
386 {
387 struct lfht_test_node *test_node = to_test_node(node);
388
389 return !test_compare(test_node->key, test_node->key_len,
390 key, sizeof(unsigned long));
391 }
392
393 static inline
394 void cds_lfht_test_lookup(struct cds_lfht *ht, void *key, size_t key_len,
395 struct cds_lfht_iter *iter)
396 {
397 assert(key_len == sizeof(unsigned long));
398
399 cds_lfht_lookup(ht, test_hash(key, key_len, TEST_HASH_SEED),
400 test_match, key, iter);
401 }
402
403 void free_node_cb(struct rcu_head *head);
404
405 /* rw test */
406 void test_hash_rw_sigusr1_handler(int signo);
407 void test_hash_rw_sigusr2_handler(int signo);
408 void *test_hash_rw_thr_reader(void *_count);
409 void *test_hash_rw_thr_writer(void *_count);
410 int test_hash_rw_populate_hash(void);
411
412 /* unique test */
413 void test_hash_unique_sigusr1_handler(int signo);
414 void test_hash_unique_sigusr2_handler(int signo);
415 void *test_hash_unique_thr_reader(void *_count);
416 void *test_hash_unique_thr_writer(void *_count);
417 int test_hash_unique_populate_hash(void);
418
419 #endif /* _TEST_URCU_HASH_H */
This page took 0.044042 seconds and 4 git commands to generate.