| 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 <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> |
| 35 | #include <errno.h> |
| 36 | #include <signal.h> |
| 37 | |
| 38 | #include <urcu/tls-compat.h> |
| 39 | #include <compat-rand.h> |
| 40 | #include "cpuset.h" |
| 41 | #include "thread-id.h" |
| 42 | #include "../common/debug-yield.h" |
| 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 | /* 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 | |
| 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 | |
| 78 | struct wr_count { |
| 79 | unsigned long update_ops; |
| 80 | unsigned long add; |
| 81 | unsigned long add_exist; |
| 82 | unsigned long remove; |
| 83 | }; |
| 84 | |
| 85 | extern DECLARE_URCU_TLS(unsigned int, rand_lookup); |
| 86 | extern DECLARE_URCU_TLS(unsigned long, nr_add); |
| 87 | extern DECLARE_URCU_TLS(unsigned long, nr_addexist); |
| 88 | extern DECLARE_URCU_TLS(unsigned long, nr_del); |
| 89 | extern DECLARE_URCU_TLS(unsigned long, nr_delnoent); |
| 90 | extern DECLARE_URCU_TLS(unsigned long, lookup_fail); |
| 91 | extern DECLARE_URCU_TLS(unsigned long, lookup_ok); |
| 92 | |
| 93 | extern struct cds_lfht *test_ht; |
| 94 | |
| 95 | struct test_data { |
| 96 | int a; |
| 97 | int b; |
| 98 | }; |
| 99 | |
| 100 | struct 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 | |
| 108 | static inline struct lfht_test_node * |
| 109 | to_test_node(struct cds_lfht_node *node) |
| 110 | { |
| 111 | return caa_container_of(node, struct lfht_test_node, node); |
| 112 | } |
| 113 | |
| 114 | static inline |
| 115 | void 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 | |
| 123 | static inline struct lfht_test_node * |
| 124 | cds_lfht_iter_get_test_node(struct cds_lfht_iter *iter) |
| 125 | { |
| 126 | return to_test_node(cds_lfht_iter_get_node(iter)); |
| 127 | } |
| 128 | |
| 129 | extern volatile int test_go, test_stop; |
| 130 | |
| 131 | extern unsigned long wdelay; |
| 132 | |
| 133 | extern unsigned long duration; |
| 134 | |
| 135 | /* read-side C.S. duration, in loops */ |
| 136 | extern unsigned long rduration; |
| 137 | |
| 138 | extern unsigned long init_hash_size; |
| 139 | extern unsigned long min_hash_alloc_size; |
| 140 | extern unsigned long max_hash_buckets_size; |
| 141 | extern unsigned long init_populate; |
| 142 | extern int opt_auto_resize; |
| 143 | extern int add_only, add_unique, add_replace; |
| 144 | extern const struct cds_lfht_mm_type *memory_backend; |
| 145 | |
| 146 | extern unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset; |
| 147 | extern unsigned long init_pool_size, |
| 148 | lookup_pool_size, |
| 149 | write_pool_size; |
| 150 | extern int validate_lookup; |
| 151 | |
| 152 | extern unsigned long nr_hash_chains; |
| 153 | |
| 154 | extern int count_pipe[2]; |
| 155 | |
| 156 | static inline void loop_sleep(unsigned long loops) |
| 157 | { |
| 158 | while (loops-- != 0) |
| 159 | caa_cpu_relax(); |
| 160 | } |
| 161 | |
| 162 | extern int verbose_mode; |
| 163 | |
| 164 | #define printf_verbose(fmt, args...) \ |
| 165 | do { \ |
| 166 | if (verbose_mode) \ |
| 167 | printf(fmt, ## args); \ |
| 168 | } while (0) |
| 169 | |
| 170 | extern unsigned int cpu_affinities[NR_CPUS]; |
| 171 | extern unsigned int next_aff; |
| 172 | extern int use_affinity; |
| 173 | |
| 174 | extern pthread_mutex_t affinity_mutex; |
| 175 | |
| 176 | void set_affinity(void); |
| 177 | |
| 178 | /* |
| 179 | * returns 0 if test should end. |
| 180 | */ |
| 181 | static inline int test_duration_write(void) |
| 182 | { |
| 183 | return !test_stop; |
| 184 | } |
| 185 | |
| 186 | static inline int test_duration_read(void) |
| 187 | { |
| 188 | return !test_stop; |
| 189 | } |
| 190 | |
| 191 | extern DECLARE_URCU_TLS(unsigned long long, nr_writes); |
| 192 | extern DECLARE_URCU_TLS(unsigned long long, nr_reads); |
| 193 | |
| 194 | extern unsigned int nr_readers; |
| 195 | extern unsigned int nr_writers; |
| 196 | |
| 197 | void rcu_copy_mutex_lock(void); |
| 198 | void 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) \ |
| 209 | do { \ |
| 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 | |
| 229 | static inline __attribute__((unused)) |
| 230 | uint32_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 | |
| 263 | static inline |
| 264 | void 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) |
| 301 | static inline |
| 302 | unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed) |
| 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 |
| 310 | static inline |
| 311 | unsigned long test_hash_mix(const void *_key, size_t length, unsigned long seed) |
| 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 | |
| 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 | */ |
| 338 | static inline |
| 339 | unsigned 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 | |
| 353 | unsigned long test_compare(const void *key1, size_t key1_len, |
| 354 | const void *key2, size_t key2_len); |
| 355 | |
| 356 | static inline |
| 357 | int 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 | |
| 365 | static inline |
| 366 | void 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 | |
| 375 | void free_node_cb(struct rcu_head *head); |
| 376 | |
| 377 | /* rw test */ |
| 378 | void test_hash_rw_sigusr1_handler(int signo); |
| 379 | void test_hash_rw_sigusr2_handler(int signo); |
| 380 | void *test_hash_rw_thr_reader(void *_count); |
| 381 | void *test_hash_rw_thr_writer(void *_count); |
| 382 | int test_hash_rw_populate_hash(void); |
| 383 | |
| 384 | /* unique test */ |
| 385 | void test_hash_unique_sigusr1_handler(int signo); |
| 386 | void test_hash_unique_sigusr2_handler(int signo); |
| 387 | void *test_hash_unique_thr_reader(void *_count); |
| 388 | void *test_hash_unique_thr_writer(void *_count); |
| 389 | int test_hash_unique_populate_hash(void); |
| 390 | |
| 391 | #endif /* _TEST_URCU_HASH_H */ |