1 // SPDX-FileCopyrightText: 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
5 #ifndef _TEST_URCU_HASH_H
6 #define _TEST_URCU_HASH_H
9 * Userspace RCU library - test program
16 #include <sys/types.h>
23 #include <urcu/assert.h>
24 #include <urcu/tls-compat.h>
25 #include <compat-rand.h>
26 #include "thread-id.h"
27 #include "../common/debug-yield.h"
29 #define DEFAULT_HASH_SIZE 32
30 #define DEFAULT_MIN_ALLOC_SIZE 1
31 #define DEFAULT_RAND_POOL 1000000
34 * Note: the hash seed should be a random value for hash tables
35 * targeting production environments to provide protection against
36 * denial of service attacks. We keep it a static value within this test
37 * program to compare identical benchmark runs.
39 #define TEST_HASH_SEED 0x42UL
41 /* hardcoded number of CPUs */
45 #define poison_free(ptr) \
47 memset(ptr, 0x42, sizeof(*(ptr))); \
51 #define poison_free(ptr) free(ptr)
54 #ifndef DYNAMIC_LINK_TEST
57 #define debug_yield_read()
59 #include <urcu-qsbr.h>
60 #include <urcu/rculfhash.h>
61 #include <urcu-call-rcu.h>
64 unsigned long update_ops
;
66 unsigned long add_exist
;
70 extern DECLARE_URCU_TLS(unsigned int, rand_lookup
);
71 extern DECLARE_URCU_TLS(unsigned long, nr_add
);
72 extern DECLARE_URCU_TLS(unsigned long, nr_addexist
);
73 extern DECLARE_URCU_TLS(unsigned long, nr_del
);
74 extern DECLARE_URCU_TLS(unsigned long, nr_delnoent
);
75 extern DECLARE_URCU_TLS(unsigned long, lookup_fail
);
76 extern DECLARE_URCU_TLS(unsigned long, lookup_ok
);
78 extern struct cds_lfht
*test_ht
;
85 struct lfht_test_node
{
86 struct cds_lfht_node node
;
89 /* cache-cold for iteration */
93 static inline struct lfht_test_node
*
94 to_test_node(struct cds_lfht_node
*node
)
96 return caa_container_of(node
, struct lfht_test_node
, node
);
100 void lfht_test_node_init(struct lfht_test_node
*node
, void *key
,
103 cds_lfht_node_init(&node
->node
);
105 node
->key_len
= key_len
;
108 static inline struct lfht_test_node
*
109 cds_lfht_iter_get_test_node(struct cds_lfht_iter
*iter
)
111 return to_test_node(cds_lfht_iter_get_node(iter
));
114 extern unsigned long wdelay
;
116 extern unsigned long duration
;
118 /* read-side C.S. duration, in loops */
119 extern unsigned long rduration
;
121 extern unsigned long init_hash_size
;
122 extern unsigned long min_hash_alloc_size
;
123 extern unsigned long max_hash_buckets_size
;
124 extern unsigned long init_populate
;
125 extern int opt_auto_resize
;
126 extern int add_only
, add_unique
, add_replace
;
127 extern const struct cds_lfht_mm_type
*memory_backend
;
129 extern unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
130 extern unsigned long init_pool_size
,
133 extern int validate_lookup
;
135 extern unsigned long nr_hash_chains
;
137 extern int count_pipe
[2];
139 static inline void loop_sleep(unsigned long loops
)
145 extern int verbose_mode
;
147 #define printf_verbose(fmt, args...) \
150 printf(fmt, ## args); \
153 extern unsigned int cpu_affinities
[NR_CPUS
];
154 extern unsigned int next_aff
;
155 extern int use_affinity
;
157 extern pthread_mutex_t affinity_mutex
;
159 void set_affinity(void);
161 extern DECLARE_URCU_TLS(unsigned long long, nr_writes
);
162 extern DECLARE_URCU_TLS(unsigned long long, nr_reads
);
164 extern unsigned int nr_readers
;
165 extern unsigned int nr_writers
;
167 void rcu_copy_mutex_lock(void);
168 void rcu_copy_mutex_unlock(void);
172 * Source: http://burtleburtle.net/bob/c/lookup3.c
173 * Originally Public Domain
176 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
178 #define mix(a, b, c) \
180 a -= c; a ^= rot(c, 4); c += b; \
181 b -= a; b ^= rot(a, 6); a += c; \
182 c -= b; c ^= rot(b, 8); b += a; \
183 a -= c; a ^= rot(c, 16); c += b; \
184 b -= a; b ^= rot(a, 19); a += c; \
185 c -= b; c ^= rot(b, 4); b += a; \
188 #define final(a, b, c) \
190 c ^= b; c -= rot(b, 14); \
191 a ^= c; a -= rot(c, 11); \
192 b ^= a; b -= rot(a, 25); \
193 c ^= b; c -= rot(b, 16); \
194 a ^= c; a -= rot(c, 4);\
195 b ^= a; b -= rot(a, 14); \
196 c ^= b; c -= rot(b, 24); \
199 static inline __attribute__((unused
))
201 const uint32_t *k
, /* the key, an array of uint32_t values */
202 size_t length
, /* the length of the key, in uint32_ts */
203 uint32_t initval
) /* the previous hash, or an arbitrary value */
207 /* Set up the internal state */
208 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
210 /*----------------------------------------- handle most of the key */
220 /*----------------------------------- handle the last 3 uint32_t's */
221 switch (length
) { /* all the case statements fall through */
222 case 3: c
+= k
[2]; /* fall through */
223 case 2: b
+= k
[1]; /* fall through */
227 case 0: /* case 0: nothing left to add */
230 /*---------------------------------------------- report the result */
236 const uint32_t *k
, /* the key, an array of uint32_t values */
237 size_t length
, /* the length of the key, in uint32_ts */
238 uint32_t *pc
, /* IN: seed OUT: primary hash value */
239 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
243 /* Set up the internal state */
244 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
247 /*----------------------------------------- handle most of the key */
257 /*----------------------------------- handle the last 3 uint32_t's */
258 switch (length
) { /* all the case statements fall through */
259 case 3: c
+= k
[2]; /* fall through */
260 case 2: b
+= k
[1]; /* fall through */
264 case 0: /* case 0: nothing left to add */
267 /*---------------------------------------------- report the result */
272 #if (CAA_BITS_PER_LONG == 32)
274 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
276 unsigned int key
= (unsigned int) _key
;
278 urcu_posix_assert(length
== sizeof(unsigned int));
279 return hash_u32(&key
, 1, seed
);
283 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
294 urcu_posix_assert(length
== sizeof(unsigned long));
295 v
.v64
= (uint64_t) seed
;
296 key
.v64
= (uint64_t) _key
;
297 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
303 * Hash function with nr_hash_chains != 0 for testing purpose only!
304 * Creates very long hash chains, deteriorating the hash table into a
305 * few linked lists, depending on the nr_hash_chains value. The purpose
306 * of this test is to check how the hash table behaves with hash chains
307 * containing different values, which is a rare case in a normal hash
311 unsigned long test_hash(const void *_key
, size_t length
,
314 if (nr_hash_chains
== 0) {
315 return test_hash_mix(_key
, length
, seed
);
319 urcu_posix_assert(length
== sizeof(unsigned long));
320 v
= (unsigned long) _key
;
321 return v
% nr_hash_chains
;
325 unsigned long test_compare(const void *key1
, size_t key1_len
,
326 const void *key2
, size_t key2_len
);
329 int test_match(struct cds_lfht_node
*node
, const void *key
)
331 struct lfht_test_node
*test_node
= to_test_node(node
);
333 return !test_compare(test_node
->key
, test_node
->key_len
,
334 key
, sizeof(unsigned long));
338 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
339 struct cds_lfht_iter
*iter
)
341 urcu_posix_assert(key_len
== sizeof(unsigned long));
343 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
344 test_match
, key
, iter
);
347 void free_node_cb(struct rcu_head
*head
);
350 void test_hash_rw_sigusr1_handler(int signo
);
351 void test_hash_rw_sigusr2_handler(int signo
);
352 void *test_hash_rw_thr_reader(void *_count
);
353 void *test_hash_rw_thr_writer(void *_count
);
354 int test_hash_rw_populate_hash(void);
357 void test_hash_unique_sigusr1_handler(int signo
);
358 void test_hash_unique_sigusr2_handler(int signo
);
359 void *test_hash_unique_thr_reader(void *_count
);
360 void *test_hash_unique_thr_writer(void *_count
);
361 int test_hash_unique_populate_hash(void);
363 #endif /* _TEST_URCU_HASH_H */