1 #ifndef _TEST_URCU_HASH_H
2 #define _TEST_URCU_HASH_H
7 * Userspace RCU library - test program
9 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
26 #include "../config.h"
31 #include <sys/types.h>
44 #define DEFAULT_HASH_SIZE 32
45 #define DEFAULT_MIN_ALLOC_SIZE 1
46 #define DEFAULT_RAND_POOL 1000000
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.
54 #define TEST_HASH_SEED 0x42UL
56 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
57 #define CACHE_LINE_SIZE 4096
59 /* hardcoded number of CPUs */
63 #define poison_free(ptr) \
65 memset(ptr, 0x42, sizeof(*(ptr))); \
69 #define poison_free(ptr) free(ptr)
74 #if defined(_syscall0)
75 _syscall0(pid_t
, gettid
)
76 #elif defined(__NR_gettid)
77 static inline pid_t
gettid(void)
79 return syscall(__NR_gettid
);
82 #warning "use pid as tid"
83 static inline pid_t
gettid(void)
89 #ifndef DYNAMIC_LINK_TEST
92 #define debug_yield_read()
94 #include <urcu-qsbr.h>
95 #include <urcu/rculfhash.h>
96 #include <urcu-call-rcu.h>
99 unsigned long update_ops
;
101 unsigned long add_exist
;
102 unsigned long remove
;
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
;
113 extern struct cds_lfht
*test_ht
;
120 struct lfht_test_node
{
121 struct cds_lfht_node node
;
123 unsigned int key_len
;
124 /* cache-cold for iteration */
125 struct rcu_head head
;
128 static inline struct lfht_test_node
*
129 to_test_node(struct cds_lfht_node
*node
)
131 return caa_container_of(node
, struct lfht_test_node
, node
);
135 void lfht_test_node_init(struct lfht_test_node
*node
, void *key
,
138 cds_lfht_node_init(&node
->node
);
140 node
->key_len
= key_len
;
143 static inline struct lfht_test_node
*
144 cds_lfht_iter_get_test_node(struct cds_lfht_iter
*iter
)
146 return to_test_node(cds_lfht_iter_get_node(iter
));
149 extern volatile int test_go
, test_stop
;
151 extern unsigned long wdelay
;
153 extern unsigned long duration
;
155 /* read-side C.S. duration, in loops */
156 extern unsigned long rduration
;
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
;
166 extern unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
167 extern unsigned long init_pool_size
,
170 extern int validate_lookup
;
172 extern int count_pipe
[2];
174 static inline void loop_sleep(unsigned long l
)
180 extern int verbose_mode
;
182 #define printf_verbose(fmt, args...) \
185 printf(fmt, ## args); \
188 extern unsigned int cpu_affinities
[NR_CPUS
];
189 extern unsigned int next_aff
;
190 extern int use_affinity
;
192 extern pthread_mutex_t affinity_mutex
;
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)
200 void set_affinity(void);
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
);
209 * returns 0 if test should end.
211 static inline int test_duration_write(void)
216 static inline int test_duration_read(void)
221 extern unsigned long long __thread nr_writes
;
222 extern unsigned long long __thread nr_reads
;
224 extern unsigned int nr_readers
;
225 extern unsigned int nr_writers
;
227 void rcu_copy_mutex_lock(void);
228 void rcu_copy_mutex_unlock(void);
232 * Source: http://burtleburtle.net/bob/c/lookup3.c
233 * Originally Public Domain
236 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
238 #define mix(a, b, c) \
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; \
248 #define final(a, b, c) \
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); \
259 static inline __attribute__((unused
))
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 */
267 /* Set up the internal state */
268 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
270 /*----------------------------------------- handle most of the key */
280 /*----------------------------------- handle the last 3 uint32_t's */
281 switch (length
) { /* all the case statements fall through */
286 case 0: /* case 0: nothing left to add */
289 /*---------------------------------------------- report the result */
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 */
302 /* Set up the internal state */
303 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
306 /*----------------------------------------- handle most of the key */
316 /*----------------------------------- handle the last 3 uint32_t's */
317 switch (length
) { /* all the case statements fall through */
322 case 0: /* case 0: nothing left to add */
325 /*---------------------------------------------- report the result */
330 #if (CAA_BITS_PER_LONG == 32)
332 unsigned long test_hash(const void *_key
, size_t length
, unsigned long seed
)
334 unsigned int key
= (unsigned int) _key
;
336 assert(length
== sizeof(unsigned int));
337 return hash_u32(&key
, 1, seed
);
341 unsigned long test_hash(const void *_key
, size_t length
, unsigned long seed
)
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]);
360 unsigned long test_compare(const void *key1
, size_t key1_len
,
361 const void *key2
, size_t key2_len
);
364 int test_match(struct cds_lfht_node
*node
, const void *key
)
366 struct lfht_test_node
*test_node
= to_test_node(node
);
368 return !test_compare(test_node
->key
, test_node
->key_len
,
369 key
, sizeof(unsigned long));
373 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
374 struct cds_lfht_iter
*iter
)
376 assert(key_len
== sizeof(unsigned long));
378 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
379 test_match
, key
, iter
);
382 void free_node_cb(struct rcu_head
*head
);
384 #endif /* _TEST_URCU_HASH_H */