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>
40 #include <urcu/tls-compat.h>
46 #define DEFAULT_HASH_SIZE 32
47 #define DEFAULT_MIN_ALLOC_SIZE 1
48 #define DEFAULT_RAND_POOL 1000000
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.
56 #define TEST_HASH_SEED 0x42UL
58 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
59 #define CACHE_LINE_SIZE 4096
61 /* hardcoded number of CPUs */
65 #define poison_free(ptr) \
67 memset(ptr, 0x42, sizeof(*(ptr))); \
71 #define poison_free(ptr) free(ptr)
76 #if defined(_syscall0)
77 _syscall0(pid_t
, gettid
)
78 #elif defined(__NR_gettid)
79 static inline pid_t
gettid(void)
81 return syscall(__NR_gettid
);
84 #warning "use pid as tid"
85 static inline pid_t
gettid(void)
91 #ifndef DYNAMIC_LINK_TEST
94 #define debug_yield_read()
96 #include <urcu-qsbr.h>
97 #include <urcu/rculfhash.h>
98 #include <urcu-call-rcu.h>
101 unsigned long update_ops
;
103 unsigned long add_exist
;
104 unsigned long remove
;
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
);
115 extern struct cds_lfht
*test_ht
;
122 struct lfht_test_node
{
123 struct cds_lfht_node node
;
125 unsigned int key_len
;
126 /* cache-cold for iteration */
127 struct rcu_head head
;
130 static inline struct lfht_test_node
*
131 to_test_node(struct cds_lfht_node
*node
)
133 return caa_container_of(node
, struct lfht_test_node
, node
);
137 void lfht_test_node_init(struct lfht_test_node
*node
, void *key
,
140 cds_lfht_node_init(&node
->node
);
142 node
->key_len
= key_len
;
145 static inline struct lfht_test_node
*
146 cds_lfht_iter_get_test_node(struct cds_lfht_iter
*iter
)
148 return to_test_node(cds_lfht_iter_get_node(iter
));
151 extern volatile int test_go
, test_stop
;
153 extern unsigned long wdelay
;
155 extern unsigned long duration
;
157 /* read-side C.S. duration, in loops */
158 extern unsigned long rduration
;
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
;
168 extern unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
169 extern unsigned long init_pool_size
,
172 extern int validate_lookup
;
174 extern unsigned long nr_hash_chains
;
176 extern int count_pipe
[2];
178 static inline void loop_sleep(unsigned long loops
)
184 extern int verbose_mode
;
186 #define printf_verbose(fmt, args...) \
189 printf(fmt, ## args); \
192 extern unsigned int cpu_affinities
[NR_CPUS
];
193 extern unsigned int next_aff
;
194 extern int use_affinity
;
196 extern pthread_mutex_t affinity_mutex
;
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)
204 void set_affinity(void);
207 * returns 0 if test should end.
209 static inline int test_duration_write(void)
214 static inline int test_duration_read(void)
219 extern DECLARE_URCU_TLS(unsigned long long, nr_writes
);
220 extern DECLARE_URCU_TLS(unsigned long long, nr_reads
);
222 extern unsigned int nr_readers
;
223 extern unsigned int nr_writers
;
225 void rcu_copy_mutex_lock(void);
226 void rcu_copy_mutex_unlock(void);
230 * Source: http://burtleburtle.net/bob/c/lookup3.c
231 * Originally Public Domain
234 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
236 #define mix(a, b, c) \
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; \
246 #define final(a, b, c) \
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); \
257 static inline __attribute__((unused
))
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 */
265 /* Set up the internal state */
266 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
268 /*----------------------------------------- handle most of the key */
278 /*----------------------------------- handle the last 3 uint32_t's */
279 switch (length
) { /* all the case statements fall through */
284 case 0: /* case 0: nothing left to add */
287 /*---------------------------------------------- report the result */
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 */
300 /* Set up the internal state */
301 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
304 /*----------------------------------------- handle most of the key */
314 /*----------------------------------- handle the last 3 uint32_t's */
315 switch (length
) { /* all the case statements fall through */
320 case 0: /* case 0: nothing left to add */
323 /*---------------------------------------------- report the result */
328 #if (CAA_BITS_PER_LONG == 32)
330 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
332 unsigned int key
= (unsigned int) _key
;
334 assert(length
== sizeof(unsigned int));
335 return hash_u32(&key
, 1, seed
);
339 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
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]);
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
367 unsigned long test_hash(const void *_key
, size_t length
,
370 if (nr_hash_chains
== 0) {
371 return test_hash_mix(_key
, length
, seed
);
375 assert(length
== sizeof(unsigned long));
376 v
= (unsigned long) _key
;
377 return v
% nr_hash_chains
;
381 unsigned long test_compare(const void *key1
, size_t key1_len
,
382 const void *key2
, size_t key2_len
);
385 int test_match(struct cds_lfht_node
*node
, const void *key
)
387 struct lfht_test_node
*test_node
= to_test_node(node
);
389 return !test_compare(test_node
->key
, test_node
->key_len
,
390 key
, sizeof(unsigned long));
394 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
395 struct cds_lfht_iter
*iter
)
397 assert(key_len
== sizeof(unsigned long));
399 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
400 test_match
, key
, iter
);
403 void free_node_cb(struct rcu_head
*head
);
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);
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);
419 #endif /* _TEST_URCU_HASH_H */