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 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
|
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "precompiled.hpp"
#include "gc/shared/workerThread.hpp"
#include "runtime/mutex.hpp"
#include "runtime/os.hpp"
#include "runtime/semaphore.hpp"
#include "runtime/thread.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vmOperations.hpp"
#include "utilities/concurrentHashTable.inline.hpp"
#include "utilities/concurrentHashTableTasks.inline.hpp"
#include "threadHelper.inline.hpp"
#include "unittest.hpp"
// NOTE: On win32 gtest asserts are not mt-safe.
// Amusingly as long as they do not assert they are mt-safe.
#define SIZE_32 5
struct Pointer : public AllStatic {
typedef uintptr_t Value;
static uintx get_hash(const Value& value, bool* dead_hash) {
return (uintx)value;
}
static void* allocate_node(void* context, size_t size, const Value& value) {
return os::malloc(size, mtTest);
}
static void free_node(void* context, void* memory, const Value& value) {
os::free(memory);
}
};
struct Allocator {
struct TableElement{
TableElement * volatile _next;
uintptr_t _value;
};
const uint nelements = 5;
TableElement* elements;
uint cur_index;
Allocator() : cur_index(0) {
elements = (TableElement*)os::malloc(nelements * sizeof(TableElement), mtTest);
}
void* allocate_node() {
return (void*)&elements[cur_index++];
}
void free_node(void* value) { /* Arena allocator. Ignore freed nodes*/ }
void reset() {
cur_index = 0;
}
~Allocator() {
os::free(elements);
}
};
struct Config : public AllStatic {
typedef uintptr_t Value;
static uintx get_hash(const Value& value, bool* dead_hash) {
return (uintx)value;
}
static void* allocate_node(void* context, size_t size, const Value& value) {
Allocator* mm = (Allocator*)context;
return mm->allocate_node();
}
static void free_node(void* context, void* memory, const Value& value) {
Allocator* mm = (Allocator*)context;
mm->free_node(memory);
}
};
typedef ConcurrentHashTable<Pointer, mtInternal> SimpleTestTable;
typedef ConcurrentHashTable<Pointer, mtInternal>::MultiGetHandle SimpleTestGetHandle;
typedef ConcurrentHashTable<Config, mtInternal> CustomTestTable;
struct SimpleTestLookup {
uintptr_t _val;
SimpleTestLookup(uintptr_t val) : _val(val) {}
uintx get_hash() {
return Pointer::get_hash(_val, nullptr);
}
bool equals(const uintptr_t* value) {
return _val == *value;
}
bool is_dead(const uintptr_t* value) {
return false;
}
};
struct ValueGet {
uintptr_t _return;
ValueGet() : _return(0) {}
void operator()(uintptr_t* value) {
EXPECT_NE(value, (uintptr_t*)nullptr) << "expected valid value";
_return = *value;
}
uintptr_t get_value() const {
return _return;
}
};
template <typename T=SimpleTestTable>
static uintptr_t cht_get_copy(T* cht, Thread* thr, SimpleTestLookup stl) {
ValueGet vg;
cht->get(thr, stl, vg);
return vg.get_value();
}
template <typename T=SimpleTestTable>
static void cht_find(Thread* thr, T* cht, uintptr_t val) {
SimpleTestLookup stl(val);
ValueGet vg;
EXPECT_EQ(cht->get(thr, stl, vg), true) << "Getting an old value failed.";
EXPECT_EQ(val, vg.get_value()) << "Getting an old value failed.";
}
template <typename T=SimpleTestTable>
static void cht_insert_and_find(Thread* thr, T* cht, uintptr_t val) {
SimpleTestLookup stl(val);
EXPECT_EQ(cht->insert(thr, stl, val), true) << "Inserting an unique value failed.";
cht_find(thr, cht, val);
}
static void cht_insert(Thread* thr) {
uintptr_t val = 0x2;
SimpleTestLookup stl(val);
SimpleTestTable* cht = new SimpleTestTable();
EXPECT_TRUE(cht->insert(thr, stl, val)) << "Insert unique value failed.";
EXPECT_EQ(cht_get_copy(cht, thr, stl), val) << "Getting an existing value failed.";
EXPECT_TRUE(cht->remove(thr, stl)) << "Removing an existing value failed.";
EXPECT_FALSE(cht->remove(thr, stl)) << "Removing an already removed item succeeded.";
EXPECT_NE(cht_get_copy(cht, thr, stl), val) << "Getting a removed value succeeded.";
delete cht;
}
static void cht_insert_get(Thread* thr) {
uintptr_t val = 0x2;
SimpleTestLookup stl(val);
SimpleTestTable* cht = new SimpleTestTable();
ValueGet vg;
EXPECT_TRUE(cht->insert_get(thr, stl, val, vg)) << "Insert unique value failed.";
EXPECT_EQ(val, vg.get_value()) << "Getting an inserted value failed.";
ValueGet vg_dup;
EXPECT_FALSE(cht->insert_get(thr, stl, val, vg_dup)) << "Insert duplicate value succeeded.";
EXPECT_EQ(val, vg_dup.get_value()) << "Getting an existing value failed.";
delete cht;
}
static void cht_get_insert(Thread* thr) {
uintptr_t val = 0x2;
SimpleTestLookup stl(val);
SimpleTestTable* cht = new SimpleTestTable();
{
SCOPED_TRACE("First");
cht_insert_and_find(thr, cht, val);
}
EXPECT_EQ(cht_get_copy(cht, thr, stl), val) << "Get an old value failed";
EXPECT_TRUE(cht->remove(thr, stl)) << "Removing existing value failed.";
EXPECT_NE(cht_get_copy(cht, thr, stl), val) << "Got an already removed item.";
{
SCOPED_TRACE("Second");
cht_insert_and_find(thr, cht, val);
}
delete cht;
}
static bool getinsert_bulkdelete_eval(uintptr_t* val) {
EXPECT_TRUE(*val > 0 && *val < 4) << "Val wrong for this test.";
return (*val & 0x1); // Delete all values ending with first bit set.
}
static void getinsert_bulkdelete_del(uintptr_t* val) {
EXPECT_EQ(*val & 0x1, (uintptr_t)1) << "Deleting wrong value.";
}
static void cht_getinsert_bulkdelete_insert_verified(Thread* thr, SimpleTestTable* cht, uintptr_t val,
bool verify_expect_get, bool verify_expect_inserted) {
SimpleTestLookup stl(val);
if (verify_expect_inserted) {
cht_insert_and_find(thr, cht, val);
}
if (verify_expect_get) {
cht_find(thr, cht, val);
}
}
static void cht_getinsert_bulkdelete(Thread* thr) {
SimpleTestTable a[] = {SimpleTestTable(), SimpleTestTable(2, 2, 14) /* force long lists in the buckets*/ };
const unsigned iter = 1000;
for (auto& table: a) {
for (unsigned i = 0; i < iter; ++i) {
uintptr_t val1 = i * 10 + 1;
uintptr_t val2 = i * 10 + 2;
uintptr_t val3 = i * 10 + 3;
SimpleTestLookup stl1(val1), stl2(val2), stl3(val3);
cht_getinsert_bulkdelete_insert_verified(thr, &table, val1, false, true);
cht_getinsert_bulkdelete_insert_verified(thr, &table, val2, false, true);
cht_getinsert_bulkdelete_insert_verified(thr, &table, val3, false, true);
EXPECT_TRUE(table.remove(thr, stl2)) << "Remove did not find value.";
cht_getinsert_bulkdelete_insert_verified(thr, &table, val1, true, false); // val1 should be present
cht_getinsert_bulkdelete_insert_verified(thr, &table, val2, false, true); // val2 should be inserted
cht_getinsert_bulkdelete_insert_verified(thr, &table, val3, true, false); // val3 should be present
EXPECT_EQ(cht_get_copy(&table, thr, stl1), val1) << "Get did not find value.";
EXPECT_EQ(cht_get_copy(&table, thr, stl2), val2) << "Get did not find value.";
EXPECT_EQ(cht_get_copy(&table, thr, stl3), val3) << "Get did not find value.";
}
unsigned delete_count = 0;
unsigned scan_count = 0;
auto eval_odd_f = [](uintptr_t* val) { return *val & 0x1; };
auto eval_true_f = [](uintptr_t* val) { return true; };
auto scan_count_f = [&scan_count](uintptr_t* val) { scan_count++; return true; };
auto delete_count_f = [&delete_count](uintptr_t* val) { delete_count++; };
table.bulk_delete(thr, eval_odd_f, delete_count_f);
EXPECT_EQ(iter*2, delete_count) << "All odd values should have been deleted";
table.do_scan(thr, scan_count_f);
EXPECT_EQ(iter, scan_count) << "All odd values should have been deleted";
for (unsigned i = 0; i < iter; ++i) {
uintptr_t val1 = i * 10 + 1;
uintptr_t val2 = i * 10 + 2;
uintptr_t val3 = i * 10 + 3;
SimpleTestLookup stl1(val1), stl2(val2), stl3(val3);
EXPECT_EQ(cht_get_copy(&table, thr, stl1), (uintptr_t)0) << "Odd value should not exist.";
EXPECT_FALSE(table.remove(thr, stl1)) << "Odd value should not exist.";
EXPECT_EQ(cht_get_copy(&table, thr, stl2), val2) << "Even value should not have been removed.";
EXPECT_EQ(cht_get_copy(&table, thr, stl3), (uintptr_t)0) << "Add value should not exists.";
EXPECT_FALSE(table.remove(thr, stl3)) << "Odd value should not exists.";
}
scan_count = 0;
table.do_scan(thr, scan_count_f);
EXPECT_EQ(iter, scan_count) << "All values should have been deleted";
delete_count = 0;
table.bulk_delete(thr, eval_true_f, delete_count_f);
EXPECT_EQ(iter, delete_count) << "All odd values should have been deleted";
scan_count = 0;
table.do_scan(thr, scan_count_f);
EXPECT_EQ(0u, scan_count) << "All values should have been deleted";
}
}
static void cht_getinsert_bulkdelete_task(Thread* thr) {
uintptr_t val1 = 1;
uintptr_t val2 = 2;
uintptr_t val3 = 3;
SimpleTestLookup stl1(val1), stl2(val2), stl3(val3);
SimpleTestTable* cht = new SimpleTestTable();
cht_getinsert_bulkdelete_insert_verified(thr, cht, val1, false, true);
cht_getinsert_bulkdelete_insert_verified(thr, cht, val2, false, true);
cht_getinsert_bulkdelete_insert_verified(thr, cht, val3, false, true);
EXPECT_TRUE(cht->remove(thr, stl2)) << "Remove did not find value.";
cht_getinsert_bulkdelete_insert_verified(thr, cht, val1, true, false); // val1 should be present
cht_getinsert_bulkdelete_insert_verified(thr, cht, val2, false, true); // val2 should be inserted
cht_getinsert_bulkdelete_insert_verified(thr, cht, val3, true, false); // val3 should be present
EXPECT_EQ(cht_get_copy(cht, thr, stl1), val1) << "Get did not find value.";
EXPECT_EQ(cht_get_copy(cht, thr, stl2), val2) << "Get did not find value.";
EXPECT_EQ(cht_get_copy(cht, thr, stl3), val3) << "Get did not find value.";
// Removes all odd values.
SimpleTestTable::BulkDeleteTask bdt(cht);
if (bdt.prepare(thr)) {
while(bdt.do_task(thr, getinsert_bulkdelete_eval, getinsert_bulkdelete_del)) {
bdt.pause(thr);
bdt.cont(thr);
}
bdt.done(thr);
}
EXPECT_EQ(cht_get_copy(cht, thr, stl1), (uintptr_t)0) << "Odd value should not exist.";
EXPECT_FALSE(cht->remove(thr, stl1)) << "Odd value should not exist.";
EXPECT_EQ(cht_get_copy(cht, thr, stl2), val2) << "Even value should not have been removed.";
EXPECT_EQ(cht_get_copy(cht, thr, stl3), (uintptr_t)0) << "Add value should not exists.";
EXPECT_FALSE(cht->remove(thr, stl3)) << "Odd value should not exists.";
delete cht;
}
static void cht_reset_shrink(Thread* thr) {
uintptr_t val1 = 1;
uintptr_t val2 = 2;
uintptr_t val3 = 3;
SimpleTestLookup stl1(val1), stl2(val2), stl3(val3);
Allocator mem_allocator;
const uint initial_log_table_size = 4;
CustomTestTable* cht = new CustomTestTable(Mutex::nosafepoint-2, &mem_allocator);
cht_insert_and_find(thr, cht, val1);
cht_insert_and_find(thr, cht, val2);
cht_insert_and_find(thr, cht, val3);
cht->unsafe_reset();
mem_allocator.reset();
EXPECT_EQ(cht_get_copy(cht, thr, stl1), (uintptr_t)0) << "Table should have been reset";
// Re-inserted values should not be considered duplicates; table was reset.
cht_insert_and_find(thr, cht, val1);
cht_insert_and_find(thr, cht, val2);
cht_insert_and_find(thr, cht, val3);
cht->unsafe_reset();
delete cht;
}
static void cht_scope(Thread* thr) {
uintptr_t val = 0x2;
SimpleTestLookup stl(val);
SimpleTestTable* cht = new SimpleTestTable();
EXPECT_TRUE(cht->insert(thr, stl, val)) << "Insert unique value failed.";
{
SimpleTestGetHandle get_handle(thr, cht);
EXPECT_EQ(*get_handle.get(stl), val) << "Getting a pre-existing value failed.";
}
// We do remove here to make sure the value-handle 'unlocked' the table when leaving the scope.
EXPECT_TRUE(cht->remove(thr, stl)) << "Removing a pre-existing value failed.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl) == val) << "Got a removed value.";
delete cht;
}
struct ChtScan {
size_t _count;
ChtScan() : _count(0) {}
bool operator()(uintptr_t* val) {
EXPECT_EQ(*val, (uintptr_t)0x2) << "Got an unknown value.";
EXPECT_EQ(_count, 0u) << "Only one value should be in table.";
_count++;
return true; /* continue scan */
}
};
static void cht_scan(Thread* thr) {
uintptr_t val = 0x2;
SimpleTestLookup stl(val);
ChtScan scan;
SimpleTestTable* cht = new SimpleTestTable();
EXPECT_TRUE(cht->insert(thr, stl, val)) << "Insert unique value failed.";
EXPECT_EQ(cht->try_scan(thr, scan), true) << "Scanning an non-growing/shrinking table should work.";
EXPECT_TRUE(cht->remove(thr, stl)) << "Removing a pre-existing value failed.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl) == val) << "Got a removed value.";
delete cht;
}
struct ChtCountScan {
size_t _count;
ChtCountScan() : _count(0) {}
bool operator()(uintptr_t* val) {
_count++;
return true; /* continue scan */
}
};
static void cht_move_to(Thread* thr) {
uintptr_t val1 = 0x2;
uintptr_t val2 = 0xe0000002;
uintptr_t val3 = 0x3;
SimpleTestLookup stl1(val1), stl2(val2), stl3(val3);
SimpleTestTable* from_cht = new SimpleTestTable();
EXPECT_TRUE(from_cht->insert(thr, stl1, val1)) << "Insert unique value failed.";
EXPECT_TRUE(from_cht->insert(thr, stl2, val2)) << "Insert unique value failed.";
EXPECT_TRUE(from_cht->insert(thr, stl3, val3)) << "Insert unique value failed.";
SimpleTestTable* to_cht = new SimpleTestTable();
EXPECT_TRUE(from_cht->try_move_nodes_to(thr, to_cht)) << "Moving nodes to new table failed";
ChtCountScan scan_old;
EXPECT_TRUE(from_cht->try_scan(thr, scan_old)) << "Scanning table should work.";
EXPECT_EQ(scan_old._count, (size_t)0) << "All items should be moved";
ChtCountScan scan_new;
EXPECT_TRUE(to_cht->try_scan(thr, scan_new)) << "Scanning table should work.";
EXPECT_EQ(scan_new._count, (size_t)3) << "All items should be moved";
EXPECT_TRUE(cht_get_copy(to_cht, thr, stl1) == val1) << "Getting an inserted value should work.";
EXPECT_TRUE(cht_get_copy(to_cht, thr, stl2) == val2) << "Getting an inserted value should work.";
EXPECT_TRUE(cht_get_copy(to_cht, thr, stl3) == val3) << "Getting an inserted value should work.";
}
static void cht_grow(Thread* thr) {
uintptr_t val = 0x2;
uintptr_t val2 = 0x22;
uintptr_t val3 = 0x222;
SimpleTestLookup stl(val), stl2(val2), stl3(val3);
SimpleTestTable* cht = new SimpleTestTable();
EXPECT_TRUE(cht->insert(thr, stl, val)) << "Insert unique value failed.";
EXPECT_TRUE(cht->insert(thr, stl2, val2)) << "Insert unique value failed.";
EXPECT_TRUE(cht->insert(thr, stl3, val3)) << "Insert unique value failed.";
EXPECT_FALSE(cht->insert(thr, stl3, val3)) << "Insert duplicate value should have failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an inserted value should work.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl2) == val2) << "Getting an inserted value should work.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl3) == val3) << "Getting an inserted value should work.";
EXPECT_TRUE(cht->remove(thr, stl2)) << "Removing an inserted value should work.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an inserted value should work.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl2) == val2) << "Getting a removed value should have failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl3) == val3) << "Getting an inserted value should work.";
EXPECT_TRUE(cht->grow(thr)) << "Growing uncontended should not fail.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an item after grow failed.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl2) == val2) << "Getting a removed value after grow should have failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl3) == val3) << "Getting an item after grow failed.";
EXPECT_TRUE(cht->insert(thr, stl2, val2)) << "Insert unique value failed.";
EXPECT_TRUE(cht->remove(thr, stl3)) << "Removing an inserted value should work.";
EXPECT_TRUE(cht->shrink(thr)) << "Shrinking uncontended should not fail.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an item after shrink failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl2) == val2) << "Getting an item after shrink failed.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl3) == val3) << "Getting a removed value after shrink should have failed.";
delete cht;
}
static void cht_task_grow(Thread* thr) {
uintptr_t val = 0x2;
uintptr_t val2 = 0x22;
uintptr_t val3 = 0x222;
SimpleTestLookup stl(val), stl2(val2), stl3(val3);
SimpleTestTable* cht = new SimpleTestTable();
EXPECT_TRUE(cht->insert(thr, stl, val)) << "Insert unique value failed.";
EXPECT_TRUE(cht->insert(thr, stl2, val2)) << "Insert unique value failed.";
EXPECT_TRUE(cht->insert(thr, stl3, val3)) << "Insert unique value failed.";
EXPECT_FALSE(cht->insert(thr, stl3, val3)) << "Insert duplicate value should have failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an inserted value should work.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl2) == val2) << "Getting an inserted value should work.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl3) == val3) << "Getting an inserted value should work.";
EXPECT_TRUE(cht->remove(thr, stl2)) << "Removing an inserted value should work.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an inserted value should work.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl2) == val2) << "Getting a removed value should have failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl3) == val3) << "Getting an inserted value should work.";
SimpleTestTable::GrowTask gt(cht);
EXPECT_TRUE(gt.prepare(thr)) << "Growing uncontended should not fail.";
while(gt.do_task(thr)) { /* grow */ }
gt.done(thr);
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an item after grow failed.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl2) == val2) << "Getting a removed value after grow should have failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl3) == val3) << "Getting an item after grow failed.";
EXPECT_TRUE(cht->insert(thr, stl2, val2)) << "Insert unique value failed.";
EXPECT_TRUE(cht->remove(thr, stl3)) << "Removing an inserted value should work.";
EXPECT_TRUE(cht->shrink(thr)) << "Shrinking uncontended should not fail.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl) == val) << "Getting an item after shrink failed.";
EXPECT_TRUE(cht_get_copy(cht, thr, stl2) == val2) << "Getting an item after shrink failed.";
EXPECT_FALSE(cht_get_copy(cht, thr, stl3) == val3) << "Getting a removed value after shrink should have failed.";
delete cht;
}
TEST_VM(ConcurrentHashTable, basic_insert) {
nomt_test_doer(cht_insert);
}
TEST_VM(ConcurrentHashTable, basic_get_insert) {
nomt_test_doer(cht_get_insert);
}
TEST_VM(ConcurrentHashTable, basic_insert_get) {
nomt_test_doer(cht_insert_get);
}
TEST_VM(ConcurrentHashTable, basic_scope) {
nomt_test_doer(cht_scope);
}
TEST_VM(ConcurrentHashTable, basic_get_insert_bulk_delete) {
nomt_test_doer(cht_getinsert_bulkdelete);
}
TEST_VM(ConcurrentHashTable, basic_get_insert_bulk_delete_task) {
nomt_test_doer(cht_getinsert_bulkdelete_task);
}
TEST_VM(ConcurrentHashTable, basic_reset_shrink) {
nomt_test_doer(cht_reset_shrink);
}
TEST_VM(ConcurrentHashTable, basic_scan) {
nomt_test_doer(cht_scan);
}
TEST_VM(ConcurrentHashTable, basic_move_to) {
nomt_test_doer(cht_move_to);
}
TEST_VM(ConcurrentHashTable, basic_grow) {
nomt_test_doer(cht_grow);
}
TEST_VM(ConcurrentHashTable, task_grow) {
nomt_test_doer(cht_task_grow);
}
//#############################################################################################
class TestInterface : public AllStatic {
public:
typedef uintptr_t Value;
static uintx get_hash(const Value& value, bool* dead_hash) {
return (uintx)(value + 18446744073709551557ul) * 18446744073709551557ul;
}
static void* allocate_node(void* context, size_t size, const Value& value) {
return AllocateHeap(size, mtInternal);
}
static void free_node(void* context, void* memory, const Value& value) {
FreeHeap(memory);
}
};
typedef ConcurrentHashTable<TestInterface, mtInternal> TestTable;
typedef ConcurrentHashTable<TestInterface, mtInternal>::MultiGetHandle TestGetHandle;
struct TestLookup {
uintptr_t _val;
TestLookup(uintptr_t val) : _val(val) {}
uintx get_hash() {
return TestInterface::get_hash(_val, nullptr);
}
bool equals(const uintptr_t* value) {
return _val == *value;
}
bool is_dead(const uintptr_t* value) {
return false;
}
};
static uintptr_t cht_get_copy(TestTable* cht, Thread* thr, TestLookup tl) {
ValueGet vg;
cht->get(thr, tl, vg);
return vg.get_value();
}
class CHTTestThread : public JavaTestThread {
public:
uintptr_t _start;
uintptr_t _stop;
TestTable *_cht;
jlong _stop_ms;
CHTTestThread(uintptr_t start, uintptr_t stop, TestTable* cht, Semaphore* post)
: JavaTestThread(post), _start(start), _stop(stop), _cht(cht) {}
virtual void premain() {}
void main_run() {
premain();
_stop_ms = os::javaTimeMillis() + 2000; // 2 seconds max test time
while (keep_looping() && test_loop()) { /* */ }
postmain();
}
virtual void postmain() {}
virtual bool keep_looping() {
return _stop_ms > os::javaTimeMillis();
};
virtual bool test_loop() = 0;
virtual ~CHTTestThread() {}
};
class ValueSaver {
uintptr_t* _vals;
size_t _it;
size_t _size;
public:
ValueSaver() : _it(0), _size(1024) {
_vals = NEW_C_HEAP_ARRAY(uintptr_t, _size, mtInternal);
}
bool operator()(uintptr_t* val) {
_vals[_it++] = *val;
if (_it == _size) {
_size *= 2;
_vals = REALLOC_RESOURCE_ARRAY(uintptr_t, _vals, _size/2, _size);
}
return true;
}
void check() {
for (size_t i = 0; i < _it; i++) {
size_t count = 0;
for (size_t j = (i + 1u); j < _it; j++) {
if (_vals[i] == _vals[j]) {
count++;
}
}
EXPECT_EQ(count, 0u);
}
}
};
static void integrity_check(Thread* thr, TestTable* cht)
{
ValueSaver vs;
cht->do_scan(thr, vs);
vs.check();
}
//#############################################################################################
// All threads are working on different items
// This item should only be delete by this thread
// Thus get_unsafe is safe for this test.
class SimpleInserterThread : public CHTTestThread {
public:
static volatile bool _exit;
SimpleInserterThread(uintptr_t start, uintptr_t stop, TestTable* cht, Semaphore* post)
: CHTTestThread(start, stop, cht, post) {};
virtual ~SimpleInserterThread(){}
bool keep_looping() {
return !_exit;
}
bool test_loop() {
bool grow;
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_TRUE(_cht->insert(this, tl, v, &grow)) << "Inserting an unique value should work.";
}
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_TRUE(cht_get_copy(_cht, this, tl) == v) << "Getting an previously inserted value unsafe failed.";
}
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_TRUE(_cht->remove(this, tl)) << "Removing an existing value failed.";
}
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_TRUE(cht_get_copy(_cht, this, tl) == 0) << "Got a removed value.";
}
return true;
}
};
volatile bool SimpleInserterThread::_exit = false;
class RunnerSimpleInserterThread : public CHTTestThread {
public:
Semaphore _done;
RunnerSimpleInserterThread(Semaphore* post) : CHTTestThread(0, 0, nullptr, post) {
_cht = new TestTable(SIZE_32, SIZE_32);
};
virtual ~RunnerSimpleInserterThread(){}
void premain() {
SimpleInserterThread* ins1 = new SimpleInserterThread((uintptr_t)0x100, (uintptr_t) 0x1FF, _cht, &_done);
SimpleInserterThread* ins2 = new SimpleInserterThread((uintptr_t)0x200, (uintptr_t) 0x2FF, _cht, &_done);
SimpleInserterThread* ins3 = new SimpleInserterThread((uintptr_t)0x300, (uintptr_t) 0x3FF, _cht, &_done);
SimpleInserterThread* ins4 = new SimpleInserterThread((uintptr_t)0x400, (uintptr_t) 0x4FF, _cht, &_done);
for (uintptr_t v = 0x500; v < 0x5FF; v++ ) {
TestLookup tl(v);
EXPECT_TRUE(_cht->insert(this, tl, v)) << "Inserting an unique value should work.";
}
ins1->doit();
ins2->doit();
ins3->doit();
ins4->doit();
}
bool test_loop() {
for (uintptr_t v = 0x500; v < 0x5FF; v++ ) {
TestLookup tl(v);
EXPECT_TRUE(cht_get_copy(_cht, this, tl) == v) << "Getting an previously inserted value unsafe failed.";;
}
return true;
}
void postmain() {
SimpleInserterThread::_exit = true;
for (int i = 0; i < 4; i++) {
_done.wait();
}
for (uintptr_t v = 0x500; v < 0x5FF; v++ ) {
TestLookup tl(v);
EXPECT_TRUE(_cht->remove(this, tl)) << "Removing an existing value failed.";
}
integrity_check(this, _cht);
delete _cht;
}
};
TEST_VM(ConcurrentHashTable, concurrent_simple) {
SimpleInserterThread::_exit = false;
mt_test_doer<RunnerSimpleInserterThread>();
}
//#############################################################################################
// In this test we try to get a 'bad' value
class DeleteInserterThread : public CHTTestThread {
public:
static volatile bool _exit;
DeleteInserterThread(uintptr_t start, uintptr_t stop, TestTable* cht, Semaphore* post) : CHTTestThread(start, stop, cht, post) {};
virtual ~DeleteInserterThread(){}
bool keep_looping() {
return !_exit;
}
bool test_loop() {
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
_cht->insert(this, tl, v);
}
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
_cht->remove(this, tl);
}
return true;
}
};
volatile bool DeleteInserterThread::_exit = true;
class RunnerDeleteInserterThread : public CHTTestThread {
public:
Semaphore _done;
RunnerDeleteInserterThread(Semaphore* post) : CHTTestThread(0, 0, nullptr, post) {
_cht = new TestTable(SIZE_32, SIZE_32);
};
virtual ~RunnerDeleteInserterThread(){}
void premain() {
DeleteInserterThread* ins1 = new DeleteInserterThread((uintptr_t)0x1, (uintptr_t) 0xFFF, _cht, &_done);
DeleteInserterThread* ins2 = new DeleteInserterThread((uintptr_t)0x1, (uintptr_t) 0xFFF, _cht, &_done);
DeleteInserterThread* ins3 = new DeleteInserterThread((uintptr_t)0x1, (uintptr_t) 0xFFF, _cht, &_done);
DeleteInserterThread* ins4 = new DeleteInserterThread((uintptr_t)0x1, (uintptr_t) 0xFFF, _cht, &_done);
ins1->doit();
ins2->doit();
ins3->doit();
ins4->doit();
}
bool test_loop() {
for (uintptr_t v = 0x1; v < 0xFFF; v++ ) {
uintptr_t tv;
if (v & 0x1) {
TestLookup tl(v);
tv = cht_get_copy(_cht, this, tl);
} else {
TestLookup tl(v);
TestGetHandle value_handle(this, _cht);
uintptr_t* tmp = value_handle.get(tl);
tv = tmp != nullptr ? *tmp : 0;
}
EXPECT_TRUE(tv == 0 || tv == v) << "Got unknown value.";
}
return true;
}
void postmain() {
DeleteInserterThread::_exit = true;
for (int i = 0; i < 4; i++) {
_done.wait();
}
integrity_check(this, _cht);
delete _cht;
}
};
TEST_VM(ConcurrentHashTable, concurrent_deletes) {
DeleteInserterThread::_exit = false;
mt_test_doer<RunnerDeleteInserterThread>();
}
//#############################################################################################
#define START_SIZE 13
#define END_SIZE 17
#define START (uintptr_t)0x10000
#define RANGE (uintptr_t)0xFFFF
#define GSTEST_THREAD_COUNT 5
class GSInserterThread: public CHTTestThread {
public:
static volatile bool _shrink;
GSInserterThread(uintptr_t start, uintptr_t stop, TestTable* cht, Semaphore* post) : CHTTestThread(start, stop, cht, post) {};
virtual ~GSInserterThread(){}
bool keep_looping() {
return !(_shrink && _cht->get_size_log2(this) == START_SIZE);
}
bool test_loop() {
bool grow;
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_TRUE(_cht->insert(this, tl, v, &grow)) << "Inserting an unique value should work.";
if (grow && !_shrink) {
_cht->grow(this);
}
}
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_TRUE(cht_get_copy(_cht, this, tl) == v) << "Getting an previously inserted value unsafe failed.";
}
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_TRUE(_cht->remove(this, tl)) << "Removing an existing value failed.";
}
if (_shrink) {
_cht->shrink(this);
}
for (uintptr_t v = _start; v <= _stop; v++) {
TestLookup tl(v);
EXPECT_FALSE(cht_get_copy(_cht, this, tl) == v) << "Getting a removed value should have failed.";
}
if (!_shrink && _cht->get_size_log2(this) == END_SIZE) {
_shrink = true;
}
return true;
}
};
volatile bool GSInserterThread::_shrink = false;
class GSScannerThread : public CHTTestThread {
public:
GSScannerThread(uintptr_t start, uintptr_t stop, TestTable* cht, Semaphore* post) : CHTTestThread(start, stop, cht, post) {};
virtual ~GSScannerThread(){}
bool operator()(uintptr_t* val) {
if (*val >= this->_start && *val <= this->_stop) {
return false;
}
// continue scan
return true;
}
bool test_loop() {
_cht->try_scan(this, *this);
os::naked_short_sleep(5);
return true;
}
};
class RunnerGSInserterThread : public CHTTestThread {
public:
uintptr_t _start;
uintptr_t _range;
Semaphore _done;
RunnerGSInserterThread(Semaphore* post) : CHTTestThread(0, 0, nullptr, post) {
_cht = new TestTable(START_SIZE, END_SIZE, 2);
};
virtual ~RunnerGSInserterThread(){}
void premain() {
volatile bool timeout = false;
_start = START;
_range = RANGE;
CHTTestThread* tt[GSTEST_THREAD_COUNT];
tt[0] = new GSInserterThread(_start, _start + _range, _cht, &_done);
_start += _range + 1;
tt[1] = new GSInserterThread(_start, _start + _range, _cht, &_done);
_start += _range + 1;
tt[2] = new GSInserterThread(_start, _start + _range, _cht, &_done);
_start += _range + 1;
tt[3] = new GSInserterThread(_start, _start + _range, _cht, &_done);
tt[4] = new GSScannerThread(_start, _start + _range, _cht, &_done);
_start += _range + 1;
for (uintptr_t v = _start; v <= (_start + _range); v++ ) {
TestLookup tl(v);
EXPECT_TRUE(_cht->insert(this, tl, v)) << "Inserting an unique value should work.";
}
for (int i = 0; i < GSTEST_THREAD_COUNT; i++) {
tt[i]->doit();
}
}
bool test_loop() {
for (uintptr_t v = _start; v <= (_start + _range); v++ ) {
TestLookup tl(v);
EXPECT_TRUE(cht_get_copy(_cht, this, tl) == v) << "Getting an previously inserted value unsafe failed.";
}
return true;
}
void postmain() {
GSInserterThread::_shrink = true;
for (uintptr_t v = _start; v <= (_start + _range); v++ ) {
TestLookup tl(v);
EXPECT_TRUE(_cht->remove(this, tl)) << "Removing an existing value failed.";
}
for (int i = 0; i < GSTEST_THREAD_COUNT; i++) {
_done.wait();
}
EXPECT_TRUE(_cht->get_size_log2(this) == START_SIZE) << "Not at start size.";
Count cnt;
_cht->do_scan(this, cnt);
EXPECT_TRUE(cnt._cnt == 0) << "Items still in table";
delete _cht;
}
struct Count {
Count() : _cnt(0) {}
size_t _cnt;
bool operator()(uintptr_t*) { _cnt++; return true; };
};
};
TEST_VM(ConcurrentHashTable, concurrent_scan_grow_shrink) {
GSInserterThread::_shrink = false;
mt_test_doer<RunnerGSInserterThread>();
}
//#############################################################################################
#define GI_BD_GI_BD_START_SIZE 13
#define GI_BD_END_SIZE 17
#define GI_BD_START (uintptr_t)0x1
#define GI_BD_RANGE (uintptr_t)0x3FFFF
#define GI_BD_TEST_THREAD_COUNT 4
class GI_BD_InserterThread: public CHTTestThread {
public:
static volatile bool _shrink;
uintptr_t _br;
GI_BD_InserterThread(uintptr_t start, uintptr_t stop, TestTable* cht, Semaphore* post, uintptr_t br)
: CHTTestThread(start, stop, cht, post), _br(br) {};
virtual ~GI_BD_InserterThread(){}
bool keep_looping() {
return !(_shrink && _cht->get_size_log2(this) == GI_BD_GI_BD_START_SIZE);
}
bool test_loop() {
bool grow;
MyDel del(_br);
for (uintptr_t v = _start; v <= _stop; v++) {
{
TestLookup tl(v);
ValueGet vg;
do {
if (_cht->get(this, tl, vg, &grow)) {
EXPECT_EQ(v, vg.get_value()) << "Getting an old value failed.";
break;
}
if (_cht->insert(this, tl, v, &grow)) {
break;
}
} while(true);
}
if (grow && !_shrink) {
_cht->grow(this);
}
}
if (_shrink) {
_cht->shrink(this);
}
_cht->try_bulk_delete(this, *this, del);
if (!_shrink && _cht->is_max_size_reached()) {
_shrink = true;
}
_cht->bulk_delete(this, *this, del);
return true;
}
bool operator()(uintptr_t* val) {
return (*val & _br) == 1;
}
struct MyDel {
MyDel(uintptr_t &br) : _br(br) {};
uintptr_t &_br;
void operator()(uintptr_t* val) {
EXPECT_EQ((*val & _br), _br) << "Removing an item that should not have been removed.";
}
};
};
volatile bool GI_BD_InserterThread::_shrink = false;
class RunnerGI_BD_InserterThread : public CHTTestThread {
public:
Semaphore _done;
uintptr_t _start;
uintptr_t _range;
RunnerGI_BD_InserterThread(Semaphore* post) : CHTTestThread(0, 0, nullptr, post) {
_cht = new TestTable(GI_BD_GI_BD_START_SIZE, GI_BD_END_SIZE, 2);
};
virtual ~RunnerGI_BD_InserterThread(){}
void premain() {
_start = GI_BD_START;
_range = GI_BD_RANGE;
CHTTestThread* tt[GI_BD_TEST_THREAD_COUNT];
tt[0] = new GI_BD_InserterThread(_start, _start + _range, _cht, &_done, (uintptr_t)0x1);
tt[1] = new GI_BD_InserterThread(_start, _start + _range, _cht, &_done, (uintptr_t)0x2);
tt[2] = new GI_BD_InserterThread(_start, _start + _range, _cht, &_done, (uintptr_t)0x4);
tt[3] = new GI_BD_InserterThread(_start, _start + _range, _cht, &_done, (uintptr_t)0x8);
for (uintptr_t v = _start; v <= (_start + _range); v++ ) {
TestLookup tl(v);
EXPECT_TRUE(_cht->insert(this, tl, v)) << "Inserting an unique value should work.";
}
for (int i =0; i < GI_BD_TEST_THREAD_COUNT; i++) {
tt[i]->doit();
}
}
bool test_loop() {
for (uintptr_t v = _start; v <= (_start + _range); v++ ) {
TestLookup tl(v);
if (v & 0xF) {
cht_get_copy(_cht, this, tl);
} else {
EXPECT_EQ(cht_get_copy(_cht, this, tl), v) << "Item ending with 0xX0 should never be removed.";
}
}
return true;
}
void postmain() {
GI_BD_InserterThread::_shrink = true;
for (uintptr_t v = _start; v <= (_start + _range); v++ ) {
TestLookup tl(v);
if (v & 0xF) {
_cht->remove(this, tl);
} else {
EXPECT_TRUE(_cht->remove(this, tl)) << "Removing item ending with 0xX0 should always work.";
}
}
for (int i = 0; i < GI_BD_TEST_THREAD_COUNT; i++) {
_done.wait();
}
EXPECT_TRUE(_cht->get_size_log2(this) == GI_BD_GI_BD_START_SIZE) << "We have not shrunk back to start size.";
delete _cht;
}
};
TEST_VM(ConcurrentHashTable, concurrent_get_insert_bulk_delete) {
GI_BD_InserterThread::_shrink = false;
mt_test_doer<RunnerGI_BD_InserterThread>();
}
//#############################################################################################
class MT_BD_Thread : public JavaTestThread {
TestTable::BulkDeleteTask* _bd;
Semaphore run;
public:
MT_BD_Thread(Semaphore* post)
: JavaTestThread(post) {}
virtual ~MT_BD_Thread() {}
void main_run() {
run.wait();
MyDel del;
while(_bd->do_task(this, *this, del));
}
void set_bd_task(TestTable::BulkDeleteTask* bd) {
_bd = bd;
run.signal();
}
bool operator()(uintptr_t* val) {
return true;
}
struct MyDel {
void operator()(uintptr_t* val) {
}
};
};
class Driver_BD_Thread : public JavaTestThread {
public:
Semaphore _done;
Driver_BD_Thread(Semaphore* post) : JavaTestThread(post) {
};
virtual ~Driver_BD_Thread(){}
void main_run() {
Semaphore done(0);
TestTable* cht = new TestTable(16, 16, 2);
for (uintptr_t v = 1; v < 99999; v++ ) {
TestLookup tl(v);
EXPECT_TRUE(cht->insert(this, tl, v)) << "Inserting an unique value should work.";
}
// Must create and start threads before acquiring mutex inside BulkDeleteTask.
MT_BD_Thread* tt[4];
for (int i = 0; i < 4; i++) {
tt[i] = new MT_BD_Thread(&done);
tt[i]->doit();
}
TestTable::BulkDeleteTask bdt(cht, true /* mt */ );
EXPECT_TRUE(bdt.prepare(this)) << "Uncontended prepare must work.";
for (int i = 0; i < 4; i++) {
tt[i]->set_bd_task(&bdt);
}
for (uintptr_t v = 1; v < 99999; v++ ) {
TestLookup tl(v);
cht_get_copy(cht, this, tl);
}
for (int i = 0; i < 4; i++) {
done.wait();
}
bdt.done(this);
cht->do_scan(this, *this);
}
bool operator()(uintptr_t* val) {
EXPECT_TRUE(false) << "No items should left";
return true;
}
};
TEST_VM(ConcurrentHashTable, concurrent_mt_bulk_delete) {
mt_test_doer<Driver_BD_Thread>();
}
class CHTParallelScanTask: public WorkerTask {
TestTable* _cht;
TestTable::ScanTask* _scan_task;
size_t *_total_scanned;
public:
CHTParallelScanTask(TestTable* cht,
TestTable::ScanTask* bc,
size_t *total_scanned) :
WorkerTask("CHT Parallel Scan"),
_cht(cht),
_scan_task(bc),
_total_scanned(total_scanned)
{ }
void work(uint worker_id) {
ChtCountScan par_scan;
_scan_task->do_safepoint_scan(par_scan);
Atomic::add(_total_scanned, par_scan._count);
}
};
class CHTWorkers : AllStatic {
static WorkerThreads* _workers;
static WorkerThreads* workers() {
if (_workers == nullptr) {
_workers = new WorkerThreads("CHT Workers", MaxWorkers);
_workers->initialize_workers();
_workers->set_active_workers(MaxWorkers);
}
return _workers;
}
public:
static const uint MaxWorkers = 8;
static void run_task(WorkerTask* task) {
workers()->run_task(task);
}
};
WorkerThreads* CHTWorkers::_workers = nullptr;
class CHTParallelScan: public VM_GTestExecuteAtSafepoint {
TestTable* _cht;
uintptr_t _num_items;
public:
CHTParallelScan(TestTable* cht, uintptr_t num_items) :
_cht(cht), _num_items(num_items)
{}
void doit() {
size_t total_scanned = 0;
TestTable::ScanTask scan_task(_cht, 64);
CHTParallelScanTask task(_cht, &scan_task, &total_scanned);
CHTWorkers::run_task(&task);
EXPECT_TRUE(total_scanned == (size_t)_num_items) << " Should scan all inserted items: " << total_scanned;
}
};
TEST_VM(ConcurrentHashTable, concurrent_par_scan) {
TestTable* cht = new TestTable(16, 16, 2);
uintptr_t num_items = 999999;
for (uintptr_t v = 1; v <= num_items; v++ ) {
TestLookup tl(v);
EXPECT_TRUE(cht->insert(JavaThread::current(), tl, v)) << "Inserting an unique value should work.";
}
// Run the test at a safepoint.
CHTParallelScan op(cht, num_items);
ThreadInVMfromNative invm(JavaThread::current());
VMThread::execute(&op);
}
|