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 volatile int test_go
, test_stop
;
116 extern unsigned long wdelay
;
118 extern unsigned long duration
;
120 /* read-side C.S. duration, in loops */
121 extern unsigned long rduration
;
123 extern unsigned long init_hash_size
;
124 extern unsigned long min_hash_alloc_size
;
125 extern unsigned long max_hash_buckets_size
;
126 extern unsigned long init_populate
;
127 extern int opt_auto_resize
;
128 extern int add_only
, add_unique
, add_replace
;
129 extern const struct cds_lfht_mm_type
*memory_backend
;
131 extern unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
132 extern unsigned long init_pool_size
,
135 extern int validate_lookup
;
137 extern unsigned long nr_hash_chains
;
139 extern int count_pipe
[2];
141 static inline void loop_sleep(unsigned long loops
)
147 extern int verbose_mode
;
149 #define printf_verbose(fmt, args...) \
152 printf(fmt, ## args); \
155 extern unsigned int cpu_affinities
[NR_CPUS
];
156 extern unsigned int next_aff
;
157 extern int use_affinity
;
159 extern pthread_mutex_t affinity_mutex
;
161 void set_affinity(void);
164 * returns 0 if test should end.
166 static inline int test_duration_write(void)
171 static inline int test_duration_read(void)
176 extern DECLARE_URCU_TLS(unsigned long long, nr_writes
);
177 extern DECLARE_URCU_TLS(unsigned long long, nr_reads
);
179 extern unsigned int nr_readers
;
180 extern unsigned int nr_writers
;
182 void rcu_copy_mutex_lock(void);
183 void rcu_copy_mutex_unlock(void);
187 * Source: http://burtleburtle.net/bob/c/lookup3.c
188 * Originally Public Domain
191 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
193 #define mix(a, b, c) \
195 a -= c; a ^= rot(c, 4); c += b; \
196 b -= a; b ^= rot(a, 6); a += c; \
197 c -= b; c ^= rot(b, 8); b += a; \
198 a -= c; a ^= rot(c, 16); c += b; \
199 b -= a; b ^= rot(a, 19); a += c; \
200 c -= b; c ^= rot(b, 4); b += a; \
203 #define final(a, b, c) \
205 c ^= b; c -= rot(b, 14); \
206 a ^= c; a -= rot(c, 11); \
207 b ^= a; b -= rot(a, 25); \
208 c ^= b; c -= rot(b, 16); \
209 a ^= c; a -= rot(c, 4);\
210 b ^= a; b -= rot(a, 14); \
211 c ^= b; c -= rot(b, 24); \
214 static inline __attribute__((unused
))
216 const uint32_t *k
, /* the key, an array of uint32_t values */
217 size_t length
, /* the length of the key, in uint32_ts */
218 uint32_t initval
) /* the previous hash, or an arbitrary value */
222 /* Set up the internal state */
223 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
225 /*----------------------------------------- handle most of the key */
235 /*----------------------------------- handle the last 3 uint32_t's */
236 switch (length
) { /* all the case statements fall through */
237 case 3: c
+= k
[2]; /* fall through */
238 case 2: b
+= k
[1]; /* fall through */
242 case 0: /* case 0: nothing left to add */
245 /*---------------------------------------------- report the result */
251 const uint32_t *k
, /* the key, an array of uint32_t values */
252 size_t length
, /* the length of the key, in uint32_ts */
253 uint32_t *pc
, /* IN: seed OUT: primary hash value */
254 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
258 /* Set up the internal state */
259 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
262 /*----------------------------------------- handle most of the key */
272 /*----------------------------------- handle the last 3 uint32_t's */
273 switch (length
) { /* all the case statements fall through */
274 case 3: c
+= k
[2]; /* fall through */
275 case 2: b
+= k
[1]; /* fall through */
279 case 0: /* case 0: nothing left to add */
282 /*---------------------------------------------- report the result */
287 #if (CAA_BITS_PER_LONG == 32)
289 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
291 unsigned int key
= (unsigned int) _key
;
293 urcu_posix_assert(length
== sizeof(unsigned int));
294 return hash_u32(&key
, 1, seed
);
298 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
309 urcu_posix_assert(length
== sizeof(unsigned long));
310 v
.v64
= (uint64_t) seed
;
311 key
.v64
= (uint64_t) _key
;
312 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
318 * Hash function with nr_hash_chains != 0 for testing purpose only!
319 * Creates very long hash chains, deteriorating the hash table into a
320 * few linked lists, depending on the nr_hash_chains value. The purpose
321 * of this test is to check how the hash table behaves with hash chains
322 * containing different values, which is a rare case in a normal hash
326 unsigned long test_hash(const void *_key
, size_t length
,
329 if (nr_hash_chains
== 0) {
330 return test_hash_mix(_key
, length
, seed
);
334 urcu_posix_assert(length
== sizeof(unsigned long));
335 v
= (unsigned long) _key
;
336 return v
% nr_hash_chains
;
340 unsigned long test_compare(const void *key1
, size_t key1_len
,
341 const void *key2
, size_t key2_len
);
344 int test_match(struct cds_lfht_node
*node
, const void *key
)
346 struct lfht_test_node
*test_node
= to_test_node(node
);
348 return !test_compare(test_node
->key
, test_node
->key_len
,
349 key
, sizeof(unsigned long));
353 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
354 struct cds_lfht_iter
*iter
)
356 urcu_posix_assert(key_len
== sizeof(unsigned long));
358 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
359 test_match
, key
, iter
);
362 void free_node_cb(struct rcu_head
*head
);
365 void test_hash_rw_sigusr1_handler(int signo
);
366 void test_hash_rw_sigusr2_handler(int signo
);
367 void *test_hash_rw_thr_reader(void *_count
);
368 void *test_hash_rw_thr_writer(void *_count
);
369 int test_hash_rw_populate_hash(void);
372 void test_hash_unique_sigusr1_handler(int signo
);
373 void test_hash_unique_sigusr2_handler(int signo
);
374 void *test_hash_unique_thr_reader(void *_count
);
375 void *test_hash_unique_thr_writer(void *_count
);
376 int test_hash_unique_populate_hash(void);
378 #endif /* _TEST_URCU_HASH_H */