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