1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
|
#include "stress-ng.h"
#include "core-attribute.h"
#include "core-builtin.h"
#include "core-hash.h"
#include <math.h>
#if defined(HAVE_XXHASH_H)
#include <xxhash.h>
#endif
#define STRESS_HASH_N_BUCKETS (256)
#define STRESS_HASH_N_KEYS (128)
typedef struct {
double duration;
double chi_squared;
uint64_t total;
} stress_hash_stats_t;
typedef struct {
uint64_t buckets[STRESS_HASH_N_BUCKETS] ALIGNED(32);
char buffer[STRESS_HASH_N_KEYS + 64] ALIGNED(32);
} stress_bucket_t;
struct stress_hash_method_info;
typedef struct stress_hash_method_info stress_hash_method_info_t;
typedef uint32_t (*stress_hash_func)(const char *str, const size_t len);
typedef int (*stress_method_func)(const char *name, const stress_hash_method_info_t *hmi, stress_bucket_t *bucket);
struct stress_hash_method_info {
const char *name;
const stress_method_func func;
stress_hash_stats_t *stats;
};
static const stress_help_t help[] = {
{ NULL, "hash N", "start N workers that exercise various hash functions" },
{ NULL, "hash-method M", "specify stress hash method M, default is all" },
{ NULL, "hash-ops N", "stop after N hash bogo operations" },
{ NULL, NULL, NULL }
};
static int stress_hash_generic(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket,
const stress_hash_func hash_func,
const uint32_t le_result,
const uint32_t be_result)
{
double sum = 0.0, n, m, divisor;
uint32_t i_sum = 0;
size_t i;
const uint32_t result = stress_little_endian() ? le_result: be_result;
stress_hash_stats_t *stats = hmi->stats;
double t1, t2;
const bool verify = !!(g_opt_flags & OPT_FLAGS_VERIFY);
if (verify)
stress_mwc_seed();
(void)shim_memset(bucket->buckets, 0, sizeof(bucket->buckets));
stress_uint8rnd4((uint8_t *)bucket->buffer, STRESS_HASH_N_KEYS);
for (i = 0; i < STRESS_HASH_N_KEYS; i++)
bucket->buffer[i] = (bucket->buffer[i] & 0x3f) + ' ';
t1 = stress_time_now();
for (i = STRESS_HASH_N_KEYS - 1; i; i--) {
uint32_t hash;
bucket->buffer[i] = '\0';
hash = hash_func(bucket->buffer, i);
i_sum += hash;
hash %= STRESS_HASH_N_BUCKETS;
bucket->buckets[hash]++;
stats->total++;
}
t2 = stress_time_now();
stats->duration += (t2 - t1);
for (i = 0; i < STRESS_HASH_N_BUCKETS; i++) {
const double bi = (double)bucket->buckets[i];
sum += (bi * (bi + 1.0)) / 2.0;
}
n = (double)STRESS_HASH_N_KEYS;
m = (double)STRESS_HASH_N_BUCKETS;
divisor = (n / (2.0 * m)) * (n + (2.0 * m) - 1.0);
stats->chi_squared = sum / divisor;
if (verify && (i_sum != result)) {
pr_fail("%s: error detected, failed hash checksum %s, "
"expected %" PRIx32 ", got %" PRIx32 "\n",
name, hmi->name, result, i_sum);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static int stress_hash_method_adler32(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_adler32, 0xe0d8c860, 0xe0d8c860);
}
static uint32_t stress_hash_jenkin_wrapper(const char *str, const size_t len)
{
return (uint32_t)stress_hash_jenkin((const uint8_t *)str, len);
}
static int stress_hash_method_jenkin(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_jenkin_wrapper, 0xa6705071, 0xa6705071);
}
static uint32_t stress_hash_murmur3_32_wrapper(const char *str, const size_t len)
{
const uint32_t seed = 0xf12b35e1;
return (uint32_t)stress_hash_murmur3_32((const uint8_t *)str, len, seed);
}
static int stress_hash_method_murmur3_32(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_murmur3_32_wrapper, 0x54b572fa, 0xc250b788);
}
static uint32_t PURE stress_hash_pjw_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_pjw(str);
}
static int stress_hash_method_pjw(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_pjw_wrapper, 0xa89a91c0, 0xa89a91c0);
}
static uint32_t PURE stress_hash_djb2a_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_djb2a(str);
}
static int stress_hash_method_djb2a(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_djb2a_wrapper, 0x6a60cb5a, 0x6a60cb5a);
}
static uint32_t PURE stress_hash_fnv1a_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_fnv1a(str);
}
static int stress_hash_method_fnv1a(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_fnv1a_wrapper, 0x8ef17e80, 0x8ef17e80);
}
static uint32_t PURE stress_hash_sdbm_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_sdbm(str);
}
static int stress_hash_method_sdbm(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_sdbm_wrapper, 0x46357819, 0x46357819);
}
static uint32_t PURE stress_hash_nhash_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_nhash(str);
}
static int stress_hash_method_nhash(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_nhash_wrapper, 0x1cc86e3, 0x1cc86e3);
}
static uint32_t PURE stress_hash_crc32c_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_crc32c(str);
}
static int stress_hash_method_crc32c(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_crc32c_wrapper, 0x923ab2b3, 0x923ab2b3);
}
static uint32_t PURE OPTIMIZE3 stress_hash_xor(const char *str, const size_t len)
{
register uint32_t sum = 0;
(void)len;
while (*str) {
register uint32_t top = sum >> 31;
sum ^= (uint8_t)*str++;
sum <<= 1;
sum |= top;
}
return sum;
}
static int stress_hash_method_xor(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_xor, 0xe6d601eb, 0xe6d601eb);
}
static int stress_hash_method_muladd32(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_muladd32, 0x7f0a8d4d, 0x7f0a8d4d);
}
static int stress_hash_method_muladd64(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_muladd64, 0x99109f5c, 0x99109f5c);
}
static uint32_t PURE stress_hash_kandr_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_kandr(str);
}
static int stress_hash_method_kandr(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_kandr_wrapper, 0x1e197d9, 0x1e197d9);
}
static uint32_t PURE stress_hash_coffin_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_coffin(str);
}
static int stress_hash_method_coffin(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_coffin_wrapper, 0xdc02e07b, 0xdc02e07b);
}
static uint32_t PURE stress_hash_coffin32_wrapper_le(const char *str, const size_t len)
{
(void)len;
return stress_hash_coffin32_le(str, len);
}
static uint32_t PURE stress_hash_coffin32_wrapper_be(const char *str, const size_t len)
{
(void)len;
return stress_hash_coffin32_be(str, len);
}
static int stress_hash_method_coffin32(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
const stress_hash_func wrapper = stress_little_endian() ?
stress_hash_coffin32_wrapper_le :
stress_hash_coffin32_wrapper_be;
return stress_hash_generic(name, hmi, bucket, wrapper, 0xdc02e07b, 0xdc02e07b);
}
static uint32_t PURE stress_hash_x17_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_x17(str);
}
static int stress_hash_method_x17(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_x17_wrapper, 0xd5c97ec8, 0xd5c97ec8);
}
#if defined(HAVE_XXHASH_H) && \
defined(HAVE_LIB_XXHASH)
static uint32_t PURE stress_hash_xxh64_wrapper(const char *str, const size_t len)
{
return (uint32_t)XXH64(str, len, 0xf261eab7);
}
static int stress_hash_method_xxh64(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_xxh64_wrapper, 0x5a23bbc6, 0x5a23bbc6);
}
#endif
static uint32_t PURE stress_hash_loselose_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_loselose(str);
}
static int stress_hash_method_loselose(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_loselose_wrapper, 0x0007c7e1, 0x0007c7e1);
}
static int stress_hash_method_knuth(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_knuth, 0xe944fc94, 0xe944fc94);
}
static int stress_hash_method_mid5(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_mid5, 0xe4b74962, 0xe4b74962);
}
static int stress_hash_method_mulxror32(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_mulxror32, 0x4d98dd32, 0xf0dce8de);
}
static int stress_hash_method_mulxror64(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_mulxror64, 0x8d38b213, 0x458932cd);
}
static int stress_hash_method_xorror64(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_xorror64, 0xe49ed85f, 0x3d414fee);
}
static int stress_hash_method_xorror32(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_xorror32, 0x4fddf545, 0x5be5cd40);
}
static uint32_t PURE stress_hash_sedgwick_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_sedgwick(str);
}
static int stress_hash_method_sedgwick(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_sedgwick_wrapper, 0x266c1ca9, 0x266c1ca9);
}
static uint32_t PURE stress_hash_sobel_wrapper(const char *str, const size_t len)
{
(void)len;
return stress_hash_sobel(str);
}
static int stress_hash_method_sobel(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
return stress_hash_generic(name, hmi, bucket, stress_hash_sobel_wrapper, 0x2a7cdb61, 0x2a7cdb61);
}
static OPTIMIZE3 int stress_hash_all(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket);
static stress_hash_method_info_t hash_methods[] = {
{ "all", stress_hash_all, NULL },
{ "adler32", stress_hash_method_adler32, NULL },
{ "coffin", stress_hash_method_coffin, NULL },
{ "coffin32", stress_hash_method_coffin32, NULL },
{ "crc32c", stress_hash_method_crc32c, NULL },
{ "djb2a", stress_hash_method_djb2a, NULL },
{ "fnv1a", stress_hash_method_fnv1a, NULL },
{ "jenkin", stress_hash_method_jenkin, NULL },
{ "kandr", stress_hash_method_kandr, NULL },
{ "knuth", stress_hash_method_knuth, NULL },
{ "loselose", stress_hash_method_loselose, NULL },
{ "mid5", stress_hash_method_mid5, NULL },
{ "muladd32", stress_hash_method_muladd32, NULL },
{ "muladd64", stress_hash_method_muladd64, NULL },
{ "mulxror32", stress_hash_method_mulxror32, NULL },
{ "mulxror64", stress_hash_method_mulxror64, NULL },
{ "murmur3_32", stress_hash_method_murmur3_32, NULL },
{ "nhash", stress_hash_method_nhash, NULL },
{ "pjw", stress_hash_method_pjw, NULL },
{ "sdbm", stress_hash_method_sdbm, NULL },
{ "sedgwick", stress_hash_method_sedgwick, NULL },
{ "sobel", stress_hash_method_sobel, NULL },
{ "x17", stress_hash_method_x17, NULL },
{ "xor", stress_hash_method_xor, NULL },
{ "xorror32", stress_hash_method_xorror32, NULL },
{ "xorror64", stress_hash_method_xorror64, NULL },
#if defined(HAVE_XXHASH_H) && \
defined(HAVE_LIB_XXHASH)
{ "xxh64", stress_hash_method_xxh64, NULL },
#endif
};
#define NUM_HASH_METHODS (SIZEOF_ARRAY(hash_methods))
static OPTIMIZE3 int stress_hash_all(
const char *name,
const stress_hash_method_info_t *hmi,
stress_bucket_t *bucket)
{
static size_t i = 1;
const stress_hash_method_info_t *h = &hash_methods[i];
int rc;
(void)hmi;
rc = h->func(name, h, bucket);
i++;
if (UNLIKELY(i >= NUM_HASH_METHODS))
i = 1;
return rc;
}
static stress_hash_stats_t hash_stats[NUM_HASH_METHODS];
static int OPTIMIZE3 stress_hash(stress_args_t *args)
{
size_t i;
const stress_hash_method_info_t *hm;
size_t hash_method = 0;
stress_bucket_t bucket;
int rc = EXIT_SUCCESS;
(void)stress_get_setting("hash-method", &hash_method);
hm = &hash_methods[hash_method];
for (i = 0; i < NUM_HASH_METHODS; i++) {
hash_stats[i].duration = 0.0;
hash_stats[i].total = false;
hash_stats[i].chi_squared = 0.0;
hash_methods[i].stats = &hash_stats[i];
}
if (args->instance == 0)
pr_dbg("%s: using method '%s'\n", args->name, hm->name);
stress_set_proc_state(args->name, STRESS_STATE_SYNC_WAIT);
stress_sync_start_wait(args);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
if (hm->func(args->name, hm, &bucket) == EXIT_FAILURE) {
rc = EXIT_FAILURE;
break;
}
stress_bogo_inc(args);
} while (stress_continue(args));
if (args->instance == 0) {
pr_block_begin();
pr_inf("%s: %12.12s %15s %10s\n", args->name, "hash", "hashes/sec", "chi squared");
for (i = 1; i < NUM_HASH_METHODS; i++) {
const stress_hash_stats_t *stats = hash_methods[i].stats;
if ((stats->duration > 0.0) && (stats->total > 0)) {
const double rate = (double)((stats->duration > 0.0) ?
(double)stats->total / stats->duration : (double)0.0);
pr_inf("%s: %12.12s %15.2f %10.2f\n",
args->name, hash_methods[i].name, rate, stats->chi_squared);
}
}
pr_block_end();
}
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return rc;
}
static const char *stress_hash_method(const size_t i)
{
return (i < NUM_HASH_METHODS) ? hash_methods[i].name : NULL;
}
static const stress_opt_t opts[] = {
{ OPT_hash_method, "hash-method", TYPE_ID_SIZE_T_METHOD, 0, 0, stress_hash_method },
END_OPT,
};
const stressor_info_t stress_hash_info = {
.stressor = stress_hash,
.class = CLASS_CPU | CLASS_INTEGER | CLASS_COMPUTE | CLASS_SEARCH,
.opts = opts,
.verify = VERIFY_OPTIONAL,
.help = help
};
|