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