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.
30 #include <sys/types.h>
38 #include <urcu/tls-compat.h>
39 #include <compat-rand.h>
41 #include "thread-id.h"
42 #include "../common/debug-yield.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 /* hardcoded number of CPUs */
60 #define poison_free(ptr) \
62 memset(ptr, 0x42, sizeof(*(ptr))); \
66 #define poison_free(ptr) free(ptr)
69 #ifndef DYNAMIC_LINK_TEST
72 #define debug_yield_read()
74 #include <urcu-qsbr.h>
75 #include <urcu/rculfhash.h>
76 #include <urcu-call-rcu.h>
79 unsigned long update_ops
;
81 unsigned long add_exist
;
85 extern DECLARE_URCU_TLS(unsigned int, rand_lookup
);
86 extern DECLARE_URCU_TLS(unsigned long, nr_add
);
87 extern DECLARE_URCU_TLS(unsigned long, nr_addexist
);
88 extern DECLARE_URCU_TLS(unsigned long, nr_del
);
89 extern DECLARE_URCU_TLS(unsigned long, nr_delnoent
);
90 extern DECLARE_URCU_TLS(unsigned long, lookup_fail
);
91 extern DECLARE_URCU_TLS(unsigned long, lookup_ok
);
93 extern struct cds_lfht
*test_ht
;
100 struct lfht_test_node
{
101 struct cds_lfht_node node
;
103 unsigned int key_len
;
104 /* cache-cold for iteration */
105 struct rcu_head head
;
108 static inline struct lfht_test_node
*
109 to_test_node(struct cds_lfht_node
*node
)
111 return caa_container_of(node
, struct lfht_test_node
, node
);
115 void lfht_test_node_init(struct lfht_test_node
*node
, void *key
,
118 cds_lfht_node_init(&node
->node
);
120 node
->key_len
= key_len
;
123 static inline struct lfht_test_node
*
124 cds_lfht_iter_get_test_node(struct cds_lfht_iter
*iter
)
126 return to_test_node(cds_lfht_iter_get_node(iter
));
129 extern volatile int test_go
, test_stop
;
131 extern unsigned long wdelay
;
133 extern unsigned long duration
;
135 /* read-side C.S. duration, in loops */
136 extern unsigned long rduration
;
138 extern unsigned long init_hash_size
;
139 extern unsigned long min_hash_alloc_size
;
140 extern unsigned long max_hash_buckets_size
;
141 extern unsigned long init_populate
;
142 extern int opt_auto_resize
;
143 extern int add_only
, add_unique
, add_replace
;
144 extern const struct cds_lfht_mm_type
*memory_backend
;
146 extern unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
147 extern unsigned long init_pool_size
,
150 extern int validate_lookup
;
152 extern unsigned long nr_hash_chains
;
154 extern int count_pipe
[2];
156 static inline void loop_sleep(unsigned long loops
)
162 extern int verbose_mode
;
164 #define printf_verbose(fmt, args...) \
167 printf(fmt, ## args); \
170 extern unsigned int cpu_affinities
[NR_CPUS
];
171 extern unsigned int next_aff
;
172 extern int use_affinity
;
174 extern pthread_mutex_t affinity_mutex
;
176 void set_affinity(void);
179 * returns 0 if test should end.
181 static inline int test_duration_write(void)
186 static inline int test_duration_read(void)
191 extern DECLARE_URCU_TLS(unsigned long long, nr_writes
);
192 extern DECLARE_URCU_TLS(unsigned long long, nr_reads
);
194 extern unsigned int nr_readers
;
195 extern unsigned int nr_writers
;
197 void rcu_copy_mutex_lock(void);
198 void rcu_copy_mutex_unlock(void);
202 * Source: http://burtleburtle.net/bob/c/lookup3.c
203 * Originally Public Domain
206 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
208 #define mix(a, b, c) \
210 a -= c; a ^= rot(c, 4); c += b; \
211 b -= a; b ^= rot(a, 6); a += c; \
212 c -= b; c ^= rot(b, 8); b += a; \
213 a -= c; a ^= rot(c, 16); c += b; \
214 b -= a; b ^= rot(a, 19); a += c; \
215 c -= b; c ^= rot(b, 4); b += a; \
218 #define final(a, b, c) \
220 c ^= b; c -= rot(b, 14); \
221 a ^= c; a -= rot(c, 11); \
222 b ^= a; b -= rot(a, 25); \
223 c ^= b; c -= rot(b, 16); \
224 a ^= c; a -= rot(c, 4);\
225 b ^= a; b -= rot(a, 14); \
226 c ^= b; c -= rot(b, 24); \
229 static inline __attribute__((unused
))
231 const uint32_t *k
, /* the key, an array of uint32_t values */
232 size_t length
, /* the length of the key, in uint32_ts */
233 uint32_t initval
) /* the previous hash, or an arbitrary value */
237 /* Set up the internal state */
238 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
240 /*----------------------------------------- handle most of the key */
250 /*----------------------------------- handle the last 3 uint32_t's */
251 switch (length
) { /* all the case statements fall through */
256 case 0: /* case 0: nothing left to add */
259 /*---------------------------------------------- report the result */
265 const uint32_t *k
, /* the key, an array of uint32_t values */
266 size_t length
, /* the length of the key, in uint32_ts */
267 uint32_t *pc
, /* IN: seed OUT: primary hash value */
268 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
272 /* Set up the internal state */
273 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
276 /*----------------------------------------- handle most of the key */
286 /*----------------------------------- handle the last 3 uint32_t's */
287 switch (length
) { /* all the case statements fall through */
292 case 0: /* case 0: nothing left to add */
295 /*---------------------------------------------- report the result */
300 #if (CAA_BITS_PER_LONG == 32)
302 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
304 unsigned int key
= (unsigned int) _key
;
306 assert(length
== sizeof(unsigned int));
307 return hash_u32(&key
, 1, seed
);
311 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
322 assert(length
== sizeof(unsigned long));
323 v
.v64
= (uint64_t) seed
;
324 key
.v64
= (uint64_t) _key
;
325 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
331 * Hash function with nr_hash_chains != 0 for testing purpose only!
332 * Creates very long hash chains, deteriorating the hash table into a
333 * few linked lists, depending on the nr_hash_chains value. The purpose
334 * of this test is to check how the hash table behaves with hash chains
335 * containing different values, which is a rare case in a normal hash
339 unsigned long test_hash(const void *_key
, size_t length
,
342 if (nr_hash_chains
== 0) {
343 return test_hash_mix(_key
, length
, seed
);
347 assert(length
== sizeof(unsigned long));
348 v
= (unsigned long) _key
;
349 return v
% nr_hash_chains
;
353 unsigned long test_compare(const void *key1
, size_t key1_len
,
354 const void *key2
, size_t key2_len
);
357 int test_match(struct cds_lfht_node
*node
, const void *key
)
359 struct lfht_test_node
*test_node
= to_test_node(node
);
361 return !test_compare(test_node
->key
, test_node
->key_len
,
362 key
, sizeof(unsigned long));
366 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
367 struct cds_lfht_iter
*iter
)
369 assert(key_len
== sizeof(unsigned long));
371 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
372 test_match
, key
, iter
);
375 void free_node_cb(struct rcu_head
*head
);
378 void test_hash_rw_sigusr1_handler(int signo
);
379 void test_hash_rw_sigusr2_handler(int signo
);
380 void *test_hash_rw_thr_reader(void *_count
);
381 void *test_hash_rw_thr_writer(void *_count
);
382 int test_hash_rw_populate_hash(void);
385 void test_hash_unique_sigusr1_handler(int signo
);
386 void test_hash_unique_sigusr2_handler(int signo
);
387 void *test_hash_unique_thr_reader(void *_count
);
388 void *test_hash_unique_thr_writer(void *_count
);
389 int test_hash_unique_populate_hash(void);
391 #endif /* _TEST_URCU_HASH_H */