Fix: Move rand-compat to private src dir
[urcu.git] / tests / benchmark / test_urcu_hash.h
CommitLineData
18ca7a5b
MD
1#ifndef _TEST_URCU_HASH_H
2#define _TEST_URCU_HASH_H
3
4/*
5 * test_urcu_hash.h
6 *
7 * Userspace RCU library - test program
8 *
9 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
10 *
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.
15 *
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.
20 *
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.
24 */
25
18ca7a5b
MD
26#include <stdio.h>
27#include <pthread.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32#include <unistd.h>
33#include <stdio.h>
34#include <assert.h>
18ca7a5b
MD
35#include <errno.h>
36#include <signal.h>
37
bd252a04 38#include <urcu/tls-compat.h>
094c8c59 39#include <compat-rand.h>
2953b501 40#include "cpuset.h"
94df6318 41#include "thread-id.h"
2650042a 42#include "../common/debug-yield.h"
18ca7a5b
MD
43
44#define DEFAULT_HASH_SIZE 32
45#define DEFAULT_MIN_ALLOC_SIZE 1
46#define DEFAULT_RAND_POOL 1000000
47
48/*
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.
53 */
54#define TEST_HASH_SEED 0x42UL
55
18ca7a5b
MD
56/* hardcoded number of CPUs */
57#define NR_CPUS 16384
58
59#ifdef POISON_FREE
60#define poison_free(ptr) \
61 do { \
62 memset(ptr, 0x42, sizeof(*(ptr))); \
63 free(ptr); \
64 } while (0)
65#else
66#define poison_free(ptr) free(ptr)
67#endif
68
18ca7a5b
MD
69#ifndef DYNAMIC_LINK_TEST
70#define _LGPL_SOURCE
71#else
72#define debug_yield_read()
73#endif
74#include <urcu-qsbr.h>
75#include <urcu/rculfhash.h>
76#include <urcu-call-rcu.h>
77
78struct wr_count {
79 unsigned long update_ops;
80 unsigned long add;
81 unsigned long add_exist;
82 unsigned long remove;
83};
84
bd252a04
MD
85extern DECLARE_URCU_TLS(unsigned int, rand_lookup);
86extern DECLARE_URCU_TLS(unsigned long, nr_add);
87extern DECLARE_URCU_TLS(unsigned long, nr_addexist);
88extern DECLARE_URCU_TLS(unsigned long, nr_del);
89extern DECLARE_URCU_TLS(unsigned long, nr_delnoent);
90extern DECLARE_URCU_TLS(unsigned long, lookup_fail);
91extern DECLARE_URCU_TLS(unsigned long, lookup_ok);
18ca7a5b
MD
92
93extern struct cds_lfht *test_ht;
94
95struct test_data {
96 int a;
97 int b;
98};
99
100struct lfht_test_node {
101 struct cds_lfht_node node;
102 void *key;
103 unsigned int key_len;
104 /* cache-cold for iteration */
105 struct rcu_head head;
106};
107
108static inline struct lfht_test_node *
109to_test_node(struct cds_lfht_node *node)
110{
111 return caa_container_of(node, struct lfht_test_node, node);
112}
113
114static inline
115void lfht_test_node_init(struct lfht_test_node *node, void *key,
116 size_t key_len)
117{
118 cds_lfht_node_init(&node->node);
119 node->key = key;
120 node->key_len = key_len;
121}
122
123static inline struct lfht_test_node *
124cds_lfht_iter_get_test_node(struct cds_lfht_iter *iter)
125{
126 return to_test_node(cds_lfht_iter_get_node(iter));
127}
128
129extern volatile int test_go, test_stop;
130
131extern unsigned long wdelay;
132
133extern unsigned long duration;
134
135/* read-side C.S. duration, in loops */
136extern unsigned long rduration;
137
138extern unsigned long init_hash_size;
139extern unsigned long min_hash_alloc_size;
140extern unsigned long max_hash_buckets_size;
141extern unsigned long init_populate;
142extern int opt_auto_resize;
143extern int add_only, add_unique, add_replace;
144extern const struct cds_lfht_mm_type *memory_backend;
145
146extern unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
147extern unsigned long init_pool_size,
148 lookup_pool_size,
149 write_pool_size;
150extern int validate_lookup;
151
495913bf
MD
152extern unsigned long nr_hash_chains;
153
18ca7a5b
MD
154extern int count_pipe[2];
155
ab0aacbe 156static inline void loop_sleep(unsigned long loops)
18ca7a5b 157{
ab0aacbe 158 while (loops-- != 0)
18ca7a5b
MD
159 caa_cpu_relax();
160}
161
162extern int verbose_mode;
163
164#define printf_verbose(fmt, args...) \
165 do { \
166 if (verbose_mode) \
167 printf(fmt, ## args); \
168 } while (0)
169
170extern unsigned int cpu_affinities[NR_CPUS];
171extern unsigned int next_aff;
172extern int use_affinity;
173
174extern pthread_mutex_t affinity_mutex;
175
18ca7a5b
MD
176void set_affinity(void);
177
18ca7a5b
MD
178/*
179 * returns 0 if test should end.
180 */
181static inline int test_duration_write(void)
182{
183 return !test_stop;
184}
185
186static inline int test_duration_read(void)
187{
188 return !test_stop;
189}
190
bd252a04
MD
191extern DECLARE_URCU_TLS(unsigned long long, nr_writes);
192extern DECLARE_URCU_TLS(unsigned long long, nr_reads);
18ca7a5b
MD
193
194extern unsigned int nr_readers;
195extern unsigned int nr_writers;
196
197void rcu_copy_mutex_lock(void);
198void rcu_copy_mutex_unlock(void);
199
200/*
201 * Hash function
202 * Source: http://burtleburtle.net/bob/c/lookup3.c
203 * Originally Public Domain
204 */
205
206#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
207
208#define mix(a, b, c) \
209do { \
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; \
216} while (0)
217
218#define final(a, b, c) \
219{ \
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); \
227}
228
229static inline __attribute__((unused))
230uint32_t hash_u32(
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 */
234{
235 uint32_t a, b, c;
236
237 /* Set up the internal state */
238 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
239
240 /*----------------------------------------- handle most of the key */
241 while (length > 3) {
242 a += k[0];
243 b += k[1];
244 c += k[2];
245 mix(a, b, c);
246 length -= 3;
247 k += 3;
248 }
249
250 /*----------------------------------- handle the last 3 uint32_t's */
251 switch (length) { /* all the case statements fall through */
252 case 3: c += k[2];
253 case 2: b += k[1];
254 case 1: a += k[0];
255 final(a, b, c);
256 case 0: /* case 0: nothing left to add */
257 break;
258 }
259 /*---------------------------------------------- report the result */
260 return c;
261}
262
263static inline
264void hashword2(
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 */
269{
270 uint32_t a, b, c;
271
272 /* Set up the internal state */
273 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
274 c += *pb;
275
276 /*----------------------------------------- handle most of the key */
277 while (length > 3) {
278 a += k[0];
279 b += k[1];
280 c += k[2];
281 mix(a, b, c);
282 length -= 3;
283 k += 3;
284 }
285
286 /*----------------------------------- handle the last 3 uint32_t's */
287 switch (length) { /* all the case statements fall through */
288 case 3: c += k[2];
289 case 2: b += k[1];
290 case 1: a += k[0];
291 final(a, b, c);
292 case 0: /* case 0: nothing left to add */
293 break;
294 }
295 /*---------------------------------------------- report the result */
296 *pc = c;
297 *pb = b;
298}
299
300#if (CAA_BITS_PER_LONG == 32)
301static inline
495913bf 302unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed)
18ca7a5b
MD
303{
304 unsigned int key = (unsigned int) _key;
305
306 assert(length == sizeof(unsigned int));
307 return hash_u32(&key, 1, seed);
308}
309#else
310static inline
495913bf 311unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed)
18ca7a5b
MD
312{
313 union {
314 uint64_t v64;
315 uint32_t v32[2];
316 } v;
317 union {
318 uint64_t v64;
319 uint32_t v32[2];
320 } key;
321
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]);
326 return v.v64;
327}
328#endif
329
495913bf
MD
330/*
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
336 * table.
337 */
338static inline
339unsigned long test_hash(const void *_key, size_t length,
340 unsigned long seed)
341{
342 if (nr_hash_chains == 0) {
343 return test_hash_mix(_key, length, seed);
344 } else {
345 unsigned long v;
346
347 assert(length == sizeof(unsigned long));
348 v = (unsigned long) _key;
349 return v % nr_hash_chains;
350 }
351}
352
18ca7a5b
MD
353unsigned long test_compare(const void *key1, size_t key1_len,
354 const void *key2, size_t key2_len);
355
356static inline
357int test_match(struct cds_lfht_node *node, const void *key)
358{
359 struct lfht_test_node *test_node = to_test_node(node);
360
361 return !test_compare(test_node->key, test_node->key_len,
362 key, sizeof(unsigned long));
363}
364
365static inline
366void cds_lfht_test_lookup(struct cds_lfht *ht, void *key, size_t key_len,
367 struct cds_lfht_iter *iter)
368{
369 assert(key_len == sizeof(unsigned long));
370
371 cds_lfht_lookup(ht, test_hash(key, key_len, TEST_HASH_SEED),
372 test_match, key, iter);
373}
374
375void free_node_cb(struct rcu_head *head);
376
f52c1ef7
MD
377/* rw test */
378void test_hash_rw_sigusr1_handler(int signo);
379void test_hash_rw_sigusr2_handler(int signo);
380void *test_hash_rw_thr_reader(void *_count);
381void *test_hash_rw_thr_writer(void *_count);
382int test_hash_rw_populate_hash(void);
383
20adf780
MD
384/* unique test */
385void test_hash_unique_sigusr1_handler(int signo);
386void test_hash_unique_sigusr2_handler(int signo);
387void *test_hash_unique_thr_reader(void *_count);
388void *test_hash_unique_thr_writer(void *_count);
389int test_hash_unique_populate_hash(void);
390
18ca7a5b 391#endif /* _TEST_URCU_HASH_H */
This page took 0.07611 seconds and 4 git commands to generate.