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 * returns 0 if test should end.
205 static inline int test_duration_write(void)
210 static inline int test_duration_read(void)
215 extern unsigned long long __thread nr_writes
;
216 extern unsigned long long __thread nr_reads
;
218 extern unsigned int nr_readers
;
219 extern unsigned int nr_writers
;
221 void rcu_copy_mutex_lock(void);
222 void rcu_copy_mutex_unlock(void);
226 * Source: http://burtleburtle.net/bob/c/lookup3.c
227 * Originally Public Domain
230 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
232 #define mix(a, b, c) \
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; \
242 #define final(a, b, c) \
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); \
253 static inline __attribute__((unused
))
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 */
261 /* Set up the internal state */
262 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
264 /*----------------------------------------- handle most of the key */
274 /*----------------------------------- handle the last 3 uint32_t's */
275 switch (length
) { /* all the case statements fall through */
280 case 0: /* case 0: nothing left to add */
283 /*---------------------------------------------- report the result */
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 */
296 /* Set up the internal state */
297 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
300 /*----------------------------------------- handle most of the key */
310 /*----------------------------------- handle the last 3 uint32_t's */
311 switch (length
) { /* all the case statements fall through */
316 case 0: /* case 0: nothing left to add */
319 /*---------------------------------------------- report the result */
324 #if (CAA_BITS_PER_LONG == 32)
326 unsigned long test_hash(const void *_key
, size_t length
, unsigned long seed
)
328 unsigned int key
= (unsigned int) _key
;
330 assert(length
== sizeof(unsigned int));
331 return hash_u32(&key
, 1, seed
);
335 unsigned long test_hash(const void *_key
, size_t length
, unsigned long seed
)
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]);
354 unsigned long test_compare(const void *key1
, size_t key1_len
,
355 const void *key2
, size_t key2_len
);
358 int test_match(struct cds_lfht_node
*node
, const void *key
)
360 struct lfht_test_node
*test_node
= to_test_node(node
);
362 return !test_compare(test_node
->key
, test_node
->key_len
,
363 key
, sizeof(unsigned long));
367 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
368 struct cds_lfht_iter
*iter
)
370 assert(key_len
== sizeof(unsigned long));
372 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
373 test_match
, key
, iter
);
376 void free_node_cb(struct rcu_head
*head
);
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);
385 #endif /* _TEST_URCU_HASH_H */