Use urcu/tls-compat.h
[urcu.git] / tests / test_urcu_hash.h
CommitLineData
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>
36#include <sched.h>
37#include <errno.h>
38#include <signal.h>
39
bd252a04
MD
40#include <urcu/tls-compat.h>
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)
79static inline pid_t gettid(void)
80{
81 return syscall(__NR_gettid);
82}
83#else
84#warning "use pid as tid"
85static 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
100struct wr_count {
101 unsigned long update_ops;
102 unsigned long add;
103 unsigned long add_exist;
104 unsigned long remove;
105};
106
bd252a04
MD
107extern DECLARE_URCU_TLS(unsigned int, rand_lookup);
108extern DECLARE_URCU_TLS(unsigned long, nr_add);
109extern DECLARE_URCU_TLS(unsigned long, nr_addexist);
110extern DECLARE_URCU_TLS(unsigned long, nr_del);
111extern DECLARE_URCU_TLS(unsigned long, nr_delnoent);
112extern DECLARE_URCU_TLS(unsigned long, lookup_fail);
113extern DECLARE_URCU_TLS(unsigned long, lookup_ok);
18ca7a5b
MD
114
115extern struct cds_lfht *test_ht;
116
117struct test_data {
118 int a;
119 int b;
120};
121
122struct 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
130static inline struct lfht_test_node *
131to_test_node(struct cds_lfht_node *node)
132{
133 return caa_container_of(node, struct lfht_test_node, node);
134}
135
136static inline
137void 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
145static inline struct lfht_test_node *
146cds_lfht_iter_get_test_node(struct cds_lfht_iter *iter)
147{
148 return to_test_node(cds_lfht_iter_get_node(iter));
149}
150
151extern volatile int test_go, test_stop;
152
153extern unsigned long wdelay;
154
155extern unsigned long duration;
156
157/* read-side C.S. duration, in loops */
158extern unsigned long rduration;
159
160extern unsigned long init_hash_size;
161extern unsigned long min_hash_alloc_size;
162extern unsigned long max_hash_buckets_size;
163extern unsigned long init_populate;
164extern int opt_auto_resize;
165extern int add_only, add_unique, add_replace;
166extern const struct cds_lfht_mm_type *memory_backend;
167
168extern unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
169extern unsigned long init_pool_size,
170 lookup_pool_size,
171 write_pool_size;
172extern int validate_lookup;
173
495913bf
MD
174extern unsigned long nr_hash_chains;
175
18ca7a5b
MD
176extern int count_pipe[2];
177
178static inline void loop_sleep(unsigned long l)
179{
180 while(l-- != 0)
181 caa_cpu_relax();
182}
183
184extern int verbose_mode;
185
186#define printf_verbose(fmt, args...) \
187 do { \
188 if (verbose_mode) \
189 printf(fmt, ## args); \
190 } while (0)
191
192extern unsigned int cpu_affinities[NR_CPUS];
193extern unsigned int next_aff;
194extern int use_affinity;
195
196extern pthread_mutex_t affinity_mutex;
197
198#ifndef HAVE_CPU_SET_T
199typedef 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
204void set_affinity(void);
205
18ca7a5b
MD
206/*
207 * returns 0 if test should end.
208 */
209static inline int test_duration_write(void)
210{
211 return !test_stop;
212}
213
214static inline int test_duration_read(void)
215{
216 return !test_stop;
217}
218
bd252a04
MD
219extern DECLARE_URCU_TLS(unsigned long long, nr_writes);
220extern DECLARE_URCU_TLS(unsigned long long, nr_reads);
18ca7a5b
MD
221
222extern unsigned int nr_readers;
223extern unsigned int nr_writers;
224
225void rcu_copy_mutex_lock(void);
226void 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) \
237do { \
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
257static inline __attribute__((unused))
258uint32_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
291static inline
292void 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)
329static inline
495913bf 330unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed)
18ca7a5b
MD
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
338static inline
495913bf 339unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed)
18ca7a5b
MD
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
495913bf
MD
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 */
366static inline
367unsigned 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
18ca7a5b
MD
381unsigned long test_compare(const void *key1, size_t key1_len,
382 const void *key2, size_t key2_len);
383
384static inline
385int 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
393static inline
394void 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
403void free_node_cb(struct rcu_head *head);
404
f52c1ef7
MD
405/* rw test */
406void test_hash_rw_sigusr1_handler(int signo);
407void test_hash_rw_sigusr2_handler(int signo);
408void *test_hash_rw_thr_reader(void *_count);
409void *test_hash_rw_thr_writer(void *_count);
410int test_hash_rw_populate_hash(void);
411
20adf780
MD
412/* unique test */
413void test_hash_unique_sigusr1_handler(int signo);
414void test_hash_unique_sigusr2_handler(int signo);
415void *test_hash_unique_thr_reader(void *_count);
416void *test_hash_unique_thr_writer(void *_count);
417int test_hash_unique_populate_hash(void);
418
18ca7a5b 419#endif /* _TEST_URCU_HASH_H */
This page took 0.036552 seconds and 4 git commands to generate.