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