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>
39 #include <urcu/tls-compat.h>
41 #include "thread-id.h"
43 #define DEFAULT_HASH_SIZE 32
44 #define DEFAULT_MIN_ALLOC_SIZE 1
45 #define DEFAULT_RAND_POOL 1000000
48 * Note: the hash seed should be a random value for hash tables
49 * targeting production environments to provide protection against
50 * denial of service attacks. We keep it a static value within this test
51 * program to compare identical benchmark runs.
53 #define TEST_HASH_SEED 0x42UL
55 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
56 #define CACHE_LINE_SIZE 4096
58 /* hardcoded number of CPUs */
62 #define poison_free(ptr) \
64 memset(ptr, 0x42, sizeof(*(ptr))); \
68 #define poison_free(ptr) free(ptr)
71 #ifndef DYNAMIC_LINK_TEST
74 #define debug_yield_read()
76 #include <urcu-qsbr.h>
77 #include <urcu/rculfhash.h>
78 #include <urcu-call-rcu.h>
81 unsigned long update_ops
;
83 unsigned long add_exist
;
87 extern DECLARE_URCU_TLS(unsigned int, rand_lookup
);
88 extern DECLARE_URCU_TLS(unsigned long, nr_add
);
89 extern DECLARE_URCU_TLS(unsigned long, nr_addexist
);
90 extern DECLARE_URCU_TLS(unsigned long, nr_del
);
91 extern DECLARE_URCU_TLS(unsigned long, nr_delnoent
);
92 extern DECLARE_URCU_TLS(unsigned long, lookup_fail
);
93 extern DECLARE_URCU_TLS(unsigned long, lookup_ok
);
95 extern struct cds_lfht
*test_ht
;
102 struct lfht_test_node
{
103 struct cds_lfht_node node
;
105 unsigned int key_len
;
106 /* cache-cold for iteration */
107 struct rcu_head head
;
110 static inline struct lfht_test_node
*
111 to_test_node(struct cds_lfht_node
*node
)
113 return caa_container_of(node
, struct lfht_test_node
, node
);
117 void lfht_test_node_init(struct lfht_test_node
*node
, void *key
,
120 cds_lfht_node_init(&node
->node
);
122 node
->key_len
= key_len
;
125 static inline struct lfht_test_node
*
126 cds_lfht_iter_get_test_node(struct cds_lfht_iter
*iter
)
128 return to_test_node(cds_lfht_iter_get_node(iter
));
131 extern volatile int test_go
, test_stop
;
133 extern unsigned long wdelay
;
135 extern unsigned long duration
;
137 /* read-side C.S. duration, in loops */
138 extern unsigned long rduration
;
140 extern unsigned long init_hash_size
;
141 extern unsigned long min_hash_alloc_size
;
142 extern unsigned long max_hash_buckets_size
;
143 extern unsigned long init_populate
;
144 extern int opt_auto_resize
;
145 extern int add_only
, add_unique
, add_replace
;
146 extern const struct cds_lfht_mm_type
*memory_backend
;
148 extern unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
149 extern unsigned long init_pool_size
,
152 extern int validate_lookup
;
154 extern unsigned long nr_hash_chains
;
156 extern int count_pipe
[2];
158 static inline void loop_sleep(unsigned long loops
)
164 extern int verbose_mode
;
166 #define printf_verbose(fmt, args...) \
169 printf(fmt, ## args); \
172 extern unsigned int cpu_affinities
[NR_CPUS
];
173 extern unsigned int next_aff
;
174 extern int use_affinity
;
176 extern pthread_mutex_t affinity_mutex
;
178 void set_affinity(void);
181 * returns 0 if test should end.
183 static inline int test_duration_write(void)
188 static inline int test_duration_read(void)
193 extern DECLARE_URCU_TLS(unsigned long long, nr_writes
);
194 extern DECLARE_URCU_TLS(unsigned long long, nr_reads
);
196 extern unsigned int nr_readers
;
197 extern unsigned int nr_writers
;
199 void rcu_copy_mutex_lock(void);
200 void rcu_copy_mutex_unlock(void);
204 * Source: http://burtleburtle.net/bob/c/lookup3.c
205 * Originally Public Domain
208 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
210 #define mix(a, b, c) \
212 a -= c; a ^= rot(c, 4); c += b; \
213 b -= a; b ^= rot(a, 6); a += c; \
214 c -= b; c ^= rot(b, 8); b += a; \
215 a -= c; a ^= rot(c, 16); c += b; \
216 b -= a; b ^= rot(a, 19); a += c; \
217 c -= b; c ^= rot(b, 4); b += a; \
220 #define final(a, b, c) \
222 c ^= b; c -= rot(b, 14); \
223 a ^= c; a -= rot(c, 11); \
224 b ^= a; b -= rot(a, 25); \
225 c ^= b; c -= rot(b, 16); \
226 a ^= c; a -= rot(c, 4);\
227 b ^= a; b -= rot(a, 14); \
228 c ^= b; c -= rot(b, 24); \
231 static inline __attribute__((unused
))
233 const uint32_t *k
, /* the key, an array of uint32_t values */
234 size_t length
, /* the length of the key, in uint32_ts */
235 uint32_t initval
) /* the previous hash, or an arbitrary value */
239 /* Set up the internal state */
240 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
242 /*----------------------------------------- handle most of the key */
252 /*----------------------------------- handle the last 3 uint32_t's */
253 switch (length
) { /* all the case statements fall through */
258 case 0: /* case 0: nothing left to add */
261 /*---------------------------------------------- report the result */
267 const uint32_t *k
, /* the key, an array of uint32_t values */
268 size_t length
, /* the length of the key, in uint32_ts */
269 uint32_t *pc
, /* IN: seed OUT: primary hash value */
270 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
274 /* Set up the internal state */
275 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
278 /*----------------------------------------- handle most of the key */
288 /*----------------------------------- handle the last 3 uint32_t's */
289 switch (length
) { /* all the case statements fall through */
294 case 0: /* case 0: nothing left to add */
297 /*---------------------------------------------- report the result */
302 #if (CAA_BITS_PER_LONG == 32)
304 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
306 unsigned int key
= (unsigned int) _key
;
308 assert(length
== sizeof(unsigned int));
309 return hash_u32(&key
, 1, seed
);
313 unsigned long test_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
324 assert(length
== sizeof(unsigned long));
325 v
.v64
= (uint64_t) seed
;
326 key
.v64
= (uint64_t) _key
;
327 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
333 * Hash function with nr_hash_chains != 0 for testing purpose only!
334 * Creates very long hash chains, deteriorating the hash table into a
335 * few linked lists, depending on the nr_hash_chains value. The purpose
336 * of this test is to check how the hash table behaves with hash chains
337 * containing different values, which is a rare case in a normal hash
341 unsigned long test_hash(const void *_key
, size_t length
,
344 if (nr_hash_chains
== 0) {
345 return test_hash_mix(_key
, length
, seed
);
349 assert(length
== sizeof(unsigned long));
350 v
= (unsigned long) _key
;
351 return v
% nr_hash_chains
;
355 unsigned long test_compare(const void *key1
, size_t key1_len
,
356 const void *key2
, size_t key2_len
);
359 int test_match(struct cds_lfht_node
*node
, const void *key
)
361 struct lfht_test_node
*test_node
= to_test_node(node
);
363 return !test_compare(test_node
->key
, test_node
->key_len
,
364 key
, sizeof(unsigned long));
368 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
369 struct cds_lfht_iter
*iter
)
371 assert(key_len
== sizeof(unsigned long));
373 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
374 test_match
, key
, iter
);
377 void free_node_cb(struct rcu_head
*head
);
380 void test_hash_rw_sigusr1_handler(int signo
);
381 void test_hash_rw_sigusr2_handler(int signo
);
382 void *test_hash_rw_thr_reader(void *_count
);
383 void *test_hash_rw_thr_writer(void *_count
);
384 int test_hash_rw_populate_hash(void);
387 void test_hash_unique_sigusr1_handler(int signo
);
388 void test_hash_unique_sigusr2_handler(int signo
);
389 void *test_hash_unique_thr_reader(void *_count
);
390 void *test_hash_unique_thr_writer(void *_count
);
391 int test_hash_unique_populate_hash(void);
393 #endif /* _TEST_URCU_HASH_H */