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