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
|
/*
* 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 "memory/virtualspace.hpp"
#include "oops/oop.hpp"
#include "runtime/os.hpp"
#include "utilities/align.hpp"
#include "concurrentTestRunner.inline.hpp"
#include "unittest.hpp"
namespace {
class MemoryReleaser {
ReservedSpace* const _rs;
public:
MemoryReleaser(ReservedSpace* rs) : _rs(rs) { }
~MemoryReleaser() {
if (_rs->special()) {
EXPECT_TRUE(os::release_memory_special(_rs->base(), _rs->size()));
} else {
EXPECT_TRUE(os::release_memory(_rs->base(), _rs->size()));
}
}
};
static void small_page_write(void* addr, size_t size) {
size_t page_size = os::vm_page_size();
char* end = (char*) addr + size;
for (char* p = (char*) addr; p < end; p += page_size) {
*p = 1;
}
}
// have to use these functions, as gtest's _PRED macros don't like is_aligned
// nor (is_aligned<size_t, size_t>)
static bool is_size_aligned(size_t size, size_t alignment) {
return is_aligned(size, alignment);
}
static bool is_ptr_aligned(void* ptr, size_t alignment) {
return is_aligned(ptr, alignment);
}
static void test_reserved_size(size_t size) {
ASSERT_PRED2(is_size_aligned, size, os::vm_allocation_granularity());
ReservedSpace rs(size);
MemoryReleaser releaser(&rs);
EXPECT_TRUE(rs.base() != nullptr) << "rs.special: " << rs.special();
EXPECT_EQ(size, rs.size()) << "rs.special: " << rs.special();
if (rs.special()) {
small_page_write(rs.base(), size);
}
}
static void test_reserved_size_alignment(size_t size, size_t alignment) {
ASSERT_PRED2(is_size_aligned, size, alignment) << "Incorrect input parameters";
size_t page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();
ReservedSpace rs(size, alignment, page_size, (char *) nullptr);
ASSERT_TRUE(rs.base() != nullptr) << "rs.special = " << rs.special();
ASSERT_EQ(size, rs.size()) << "rs.special = " << rs.special();
EXPECT_PRED2(is_ptr_aligned, rs.base(), alignment)
<< "aligned sizes should always give aligned addresses";
EXPECT_PRED2(is_ptr_aligned, (void*) rs.size(), alignment)
<< "aligned sizes should always give aligned addresses";
if (rs.special()) {
small_page_write(rs.base(), size);
}
}
static void test_reserved_size_alignment_page_type(size_t size, size_t alignment, bool maybe_large) {
if (size < alignment) {
// Tests might set -XX:LargePageSizeInBytes=<small pages> and cause unexpected input arguments for this test.
ASSERT_EQ(os::vm_page_size(), os::large_page_size()) << "Test needs further refinement";
return;
}
ASSERT_PRED2(is_size_aligned, size, os::vm_allocation_granularity()) << "Must be at least AG aligned";
ASSERT_PRED2(is_size_aligned, size, alignment) << "Must be at least AG aligned";
bool large = maybe_large && UseLargePages && size >= os::large_page_size();
size_t page_size = large ? os::large_page_size() : os::vm_page_size();
ReservedSpace rs(size, alignment, page_size);
MemoryReleaser releaser(&rs);
EXPECT_TRUE(rs.base() != nullptr) << "rs.special: " << rs.special();
EXPECT_EQ(size, rs.size()) << "rs.special: " << rs.special();
if (rs.special()) {
small_page_write(rs.base(), size);
}
}
}
TEST_VM(ReservedSpace, size_alignment) {
size_t size = 2 * 1024 * 1024;
size_t ag = os::vm_allocation_granularity();
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment(size, ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment(size * 2, ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment(size * 10, ag));
}
TEST_VM(ReservedSpace, size) {
size_t size = 2 * 1024 * 1024;
size_t ag = os::vm_allocation_granularity();
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 1));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 10));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size - ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size + ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2 - ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2 + ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 3));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 3 - ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 3 + ag));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 10));
EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 10 + size / 2));
}
TEST_VM(ReservedSpace, size_alignment_page_type) {
size_t ag = os::vm_allocation_granularity();
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag, ag , false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 2, ag , false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 3, ag , false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 2, ag * 2, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 4, ag * 2, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 8, ag * 2, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 4, ag * 4, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 8, ag * 4, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 16, ag * 4, false));
}
TEST_VM(ReservedSpace, size_alignment_page_type_large_page) {
if (!UseLargePages) {
return;
}
size_t ag = os::vm_allocation_granularity();
size_t lp = os::large_page_size();
// Without large pages
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp, ag * 4, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, ag * 4, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, ag * 4, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp, lp , false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp , false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 3, lp , false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp * 2, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, lp * 2, false));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 8, lp * 2, false));
// With large pages
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp, ag * 4 , true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, ag * 4, true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, ag * 4, true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp, lp , true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp , true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 3, lp , true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp * 2, true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, lp * 2, true));
EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 8, lp * 2, true));
}
namespace {
enum TestLargePages {
Default,
Disable,
Reserve,
Commit
};
class ReservedSpaceReleaser {
ReservedSpace* const _rs;
public:
ReservedSpaceReleaser(ReservedSpace* rs) : _rs(rs) { }
~ReservedSpaceReleaser() {
_rs->release();
}
};
ReservedSpace reserve_memory(size_t reserve_size_aligned, TestLargePages mode) {
switch(mode) {
default:
case Default:
case Reserve:
return ReservedSpace(reserve_size_aligned);
case Disable:
case Commit:
return ReservedSpace(reserve_size_aligned,
os::vm_allocation_granularity(),
os::vm_page_size());
}
}
bool initialize_virtual_space(VirtualSpace& vs, ReservedSpace rs, TestLargePages mode) {
switch(mode) {
default:
case Default:
case Reserve:
return vs.initialize(rs, 0);
case Disable:
return vs.initialize_with_granularity(rs, 0, os::vm_page_size());
case Commit:
return vs.initialize_with_granularity(rs, 0, os::page_size_for_region_unaligned(rs.size(), 1));
}
}
void test_virtual_space_actual_committed_space(size_t reserve_size, size_t commit_size,
TestLargePages mode = Default) {
size_t granularity = os::vm_allocation_granularity();
size_t reserve_size_aligned = align_up(reserve_size, granularity);
ReservedSpace reserved = reserve_memory(reserve_size_aligned, mode);
ReservedSpaceReleaser releaser(&reserved);
ASSERT_TRUE(reserved.is_reserved());
VirtualSpace vs;
ASSERT_TRUE(initialize_virtual_space(vs, reserved, mode)) << "Failed to initialize VirtualSpace";
vs.expand_by(commit_size, false);
if (vs.special()) {
EXPECT_EQ(reserve_size_aligned, vs.actual_committed_size());
} else {
EXPECT_GE(vs.actual_committed_size(), commit_size);
// Approximate the commit granularity.
// Make sure that we don't commit using large pages
// if large pages has been disabled for this VirtualSpace.
size_t commit_granularity = (mode == Disable || !UseLargePages) ?
os::vm_page_size() : os::large_page_size();
EXPECT_LT(vs.actual_committed_size(), commit_size + commit_granularity);
}
}
}
TEST_VM(VirtualSpace, actual_committed_space) {
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(4 * K, 0));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(4 * K, 4 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K, 0));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K, 4 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K, 8 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 0));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 4 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 8 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 12 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 0));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 32 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 64 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M, 0));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M, 4 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M, 64 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M, 1 * M));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M, 2 * M));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M));
}
TEST_VM(VirtualSpace, actual_committed_space_one_large_page) {
if (!UseLargePages) {
return;
}
size_t large_page_size = os::large_page_size();
ReservedSpace reserved(large_page_size, large_page_size, large_page_size);
ReservedSpaceReleaser releaser(&reserved);
ASSERT_TRUE(reserved.is_reserved());
VirtualSpace vs;
ASSERT_TRUE(vs.initialize(reserved, 0)) << "Failed to initialize VirtualSpace";
vs.expand_by(large_page_size, false);
EXPECT_EQ(large_page_size, vs.actual_committed_size());
}
TEST_VM(VirtualSpace, disable_large_pages) {
if (!UseLargePages) {
return;
}
// These test cases verify that if we force VirtualSpace to disable large pages
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0, Disable));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K, Disable));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K, Disable));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M, Disable));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M, Disable));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M, Disable));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Disable));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0, Reserve));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K, Reserve));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K, Reserve));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M, Reserve));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M, Reserve));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M, Reserve));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Reserve));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0, Commit));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K, Commit));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K, Commit));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M, Commit));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M, Commit));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M, Commit));
EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Commit));
}
// ========================= concurrent virtual space memory tests
// This class have been imported from the original "internal VM test" with minor modification,
// specifically using GTest asserts instead of native HotSpot asserts.
class TestReservedSpace : AllStatic {
public:
static void small_page_write(void* addr, size_t size) {
size_t page_size = os::vm_page_size();
char* end = (char*)addr + size;
for (char* p = (char*)addr; p < end; p += page_size) {
*p = 1;
}
}
static void release_memory_for_test(ReservedSpace rs) {
if (rs.special()) {
EXPECT_TRUE(os::release_memory_special(rs.base(), rs.size()));
} else {
EXPECT_TRUE(os::release_memory(rs.base(), rs.size()));
}
}
static void test_reserved_space1(size_t size, size_t alignment) {
ASSERT_TRUE(is_aligned(size, alignment)) << "Incorrect input parameters";
size_t page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();
ReservedSpace rs(size, // size
alignment, // alignment
page_size, // page size
(char *)nullptr); // requested_address
EXPECT_TRUE(rs.base() != nullptr);
EXPECT_EQ(rs.size(), size) << "rs.size: " << rs.size();
EXPECT_TRUE(is_aligned(rs.base(), alignment)) << "aligned sizes should always give aligned addresses";
EXPECT_TRUE(is_aligned(rs.size(), alignment)) << "aligned sizes should always give aligned addresses";
if (rs.special()) {
small_page_write(rs.base(), size);
}
release_memory_for_test(rs);
}
static void test_reserved_space2(size_t size) {
ASSERT_TRUE(is_aligned(size, os::vm_allocation_granularity())) << "Must be at least AG aligned";
ReservedSpace rs(size);
EXPECT_TRUE(rs.base() != nullptr);
EXPECT_EQ(rs.size(), size) << "rs.size: " << rs.size();
if (rs.special()) {
small_page_write(rs.base(), size);
}
release_memory_for_test(rs);
}
static void test_reserved_space3(size_t size, size_t alignment, bool maybe_large) {
if (size < alignment) {
// Tests might set -XX:LargePageSizeInBytes=<small pages> and cause unexpected input arguments for this test.
ASSERT_EQ(os::vm_page_size(), os::large_page_size()) << "Test needs further refinement";
return;
}
EXPECT_TRUE(is_aligned(size, os::vm_allocation_granularity())) << "Must be at least AG aligned";
EXPECT_TRUE(is_aligned(size, alignment)) << "Must be at least aligned against alignment";
bool large = maybe_large && UseLargePages && size >= os::large_page_size();
size_t page_size = large ? os::large_page_size() : os::vm_page_size();
ReservedSpace rs(size, alignment, page_size);
EXPECT_TRUE(rs.base() != nullptr);
EXPECT_EQ(rs.size(), size) << "rs.size: " << rs.size();
if (rs.special()) {
small_page_write(rs.base(), size);
}
release_memory_for_test(rs);
}
static void test_reserved_space1() {
size_t size = 2 * 1024 * 1024;
size_t ag = os::vm_allocation_granularity();
test_reserved_space1(size, ag);
test_reserved_space1(size * 2, ag);
test_reserved_space1(size * 10, ag);
}
static void test_reserved_space2() {
size_t size = 2 * 1024 * 1024;
size_t ag = os::vm_allocation_granularity();
test_reserved_space2(size * 1);
test_reserved_space2(size * 2);
test_reserved_space2(size * 10);
test_reserved_space2(ag);
test_reserved_space2(size - ag);
test_reserved_space2(size);
test_reserved_space2(size + ag);
test_reserved_space2(size * 2);
test_reserved_space2(size * 2 - ag);
test_reserved_space2(size * 2 + ag);
test_reserved_space2(size * 3);
test_reserved_space2(size * 3 - ag);
test_reserved_space2(size * 3 + ag);
test_reserved_space2(size * 10);
test_reserved_space2(size * 10 + size / 2);
}
static void test_reserved_space3() {
size_t ag = os::vm_allocation_granularity();
test_reserved_space3(ag, ag , false);
test_reserved_space3(ag * 2, ag , false);
test_reserved_space3(ag * 3, ag , false);
test_reserved_space3(ag * 2, ag * 2, false);
test_reserved_space3(ag * 4, ag * 2, false);
test_reserved_space3(ag * 8, ag * 2, false);
test_reserved_space3(ag * 4, ag * 4, false);
test_reserved_space3(ag * 8, ag * 4, false);
test_reserved_space3(ag * 16, ag * 4, false);
if (UseLargePages) {
size_t lp = os::large_page_size();
// Without large pages
test_reserved_space3(lp, ag * 4, false);
test_reserved_space3(lp * 2, ag * 4, false);
test_reserved_space3(lp * 4, ag * 4, false);
test_reserved_space3(lp, lp , false);
test_reserved_space3(lp * 2, lp , false);
test_reserved_space3(lp * 3, lp , false);
test_reserved_space3(lp * 2, lp * 2, false);
test_reserved_space3(lp * 4, lp * 2, false);
test_reserved_space3(lp * 8, lp * 2, false);
// With large pages
test_reserved_space3(lp, ag * 4 , true);
test_reserved_space3(lp * 2, ag * 4, true);
test_reserved_space3(lp * 4, ag * 4, true);
test_reserved_space3(lp, lp , true);
test_reserved_space3(lp * 2, lp , true);
test_reserved_space3(lp * 3, lp , true);
test_reserved_space3(lp * 2, lp * 2, true);
test_reserved_space3(lp * 4, lp * 2, true);
test_reserved_space3(lp * 8, lp * 2, true);
}
}
static void test_reserved_space() {
test_reserved_space1();
test_reserved_space2();
test_reserved_space3();
}
};
class TestVirtualSpace : AllStatic {
enum TestLargePages {
Default,
Disable,
Reserve,
Commit
};
static ReservedSpace reserve_memory(size_t reserve_size_aligned, TestLargePages mode) {
switch(mode) {
default:
case Default:
case Reserve:
return ReservedSpace(reserve_size_aligned);
case Disable:
case Commit:
return ReservedSpace(reserve_size_aligned,
os::vm_allocation_granularity(),
os::vm_page_size());
}
}
static bool initialize_virtual_space(VirtualSpace& vs, ReservedSpace rs, TestLargePages mode) {
switch(mode) {
default:
case Default:
case Reserve:
return vs.initialize(rs, 0);
case Disable:
return vs.initialize_with_granularity(rs, 0, os::vm_page_size());
case Commit:
return vs.initialize_with_granularity(rs, 0, os::page_size_for_region_unaligned(rs.size(), 1));
}
}
public:
static void test_virtual_space_actual_committed_space(size_t reserve_size, size_t commit_size,
TestLargePages mode = Default) {
size_t granularity = os::vm_allocation_granularity();
size_t reserve_size_aligned = align_up(reserve_size, granularity);
ReservedSpace reserved = reserve_memory(reserve_size_aligned, mode);
EXPECT_TRUE(reserved.is_reserved());
VirtualSpace vs;
bool initialized = initialize_virtual_space(vs, reserved, mode);
EXPECT_TRUE(initialized) << "Failed to initialize VirtualSpace";
vs.expand_by(commit_size, false);
if (vs.special()) {
EXPECT_EQ(vs.actual_committed_size(), reserve_size_aligned);
} else {
EXPECT_GE(vs.actual_committed_size(), commit_size);
// Approximate the commit granularity.
// Make sure that we don't commit using large pages
// if large pages has been disabled for this VirtualSpace.
size_t commit_granularity = (mode == Disable || !UseLargePages) ?
os::vm_page_size() : os::large_page_size();
EXPECT_LT(vs.actual_committed_size(), commit_size + commit_granularity);
}
reserved.release();
}
static void test_virtual_space_actual_committed_space_one_large_page() {
if (!UseLargePages) {
return;
}
size_t large_page_size = os::large_page_size();
ReservedSpace reserved(large_page_size, large_page_size, large_page_size);
EXPECT_TRUE(reserved.is_reserved());
VirtualSpace vs;
bool initialized = vs.initialize(reserved, 0);
EXPECT_TRUE(initialized) << "Failed to initialize VirtualSpace";
vs.expand_by(large_page_size, false);
EXPECT_EQ(vs.actual_committed_size(), large_page_size);
reserved.release();
}
static void test_virtual_space_actual_committed_space() {
test_virtual_space_actual_committed_space(4 * K, 0);
test_virtual_space_actual_committed_space(4 * K, 4 * K);
test_virtual_space_actual_committed_space(8 * K, 0);
test_virtual_space_actual_committed_space(8 * K, 4 * K);
test_virtual_space_actual_committed_space(8 * K, 8 * K);
test_virtual_space_actual_committed_space(12 * K, 0);
test_virtual_space_actual_committed_space(12 * K, 4 * K);
test_virtual_space_actual_committed_space(12 * K, 8 * K);
test_virtual_space_actual_committed_space(12 * K, 12 * K);
test_virtual_space_actual_committed_space(64 * K, 0);
test_virtual_space_actual_committed_space(64 * K, 32 * K);
test_virtual_space_actual_committed_space(64 * K, 64 * K);
test_virtual_space_actual_committed_space(2 * M, 0);
test_virtual_space_actual_committed_space(2 * M, 4 * K);
test_virtual_space_actual_committed_space(2 * M, 64 * K);
test_virtual_space_actual_committed_space(2 * M, 1 * M);
test_virtual_space_actual_committed_space(2 * M, 2 * M);
test_virtual_space_actual_committed_space(10 * M, 0);
test_virtual_space_actual_committed_space(10 * M, 4 * K);
test_virtual_space_actual_committed_space(10 * M, 8 * K);
test_virtual_space_actual_committed_space(10 * M, 1 * M);
test_virtual_space_actual_committed_space(10 * M, 2 * M);
test_virtual_space_actual_committed_space(10 * M, 5 * M);
test_virtual_space_actual_committed_space(10 * M, 10 * M);
}
static void test_virtual_space_disable_large_pages() {
if (!UseLargePages) {
return;
}
// These test cases verify that if we force VirtualSpace to disable large pages
test_virtual_space_actual_committed_space(10 * M, 0, Disable);
test_virtual_space_actual_committed_space(10 * M, 4 * K, Disable);
test_virtual_space_actual_committed_space(10 * M, 8 * K, Disable);
test_virtual_space_actual_committed_space(10 * M, 1 * M, Disable);
test_virtual_space_actual_committed_space(10 * M, 2 * M, Disable);
test_virtual_space_actual_committed_space(10 * M, 5 * M, Disable);
test_virtual_space_actual_committed_space(10 * M, 10 * M, Disable);
test_virtual_space_actual_committed_space(10 * M, 0, Reserve);
test_virtual_space_actual_committed_space(10 * M, 4 * K, Reserve);
test_virtual_space_actual_committed_space(10 * M, 8 * K, Reserve);
test_virtual_space_actual_committed_space(10 * M, 1 * M, Reserve);
test_virtual_space_actual_committed_space(10 * M, 2 * M, Reserve);
test_virtual_space_actual_committed_space(10 * M, 5 * M, Reserve);
test_virtual_space_actual_committed_space(10 * M, 10 * M, Reserve);
test_virtual_space_actual_committed_space(10 * M, 0, Commit);
test_virtual_space_actual_committed_space(10 * M, 4 * K, Commit);
test_virtual_space_actual_committed_space(10 * M, 8 * K, Commit);
test_virtual_space_actual_committed_space(10 * M, 1 * M, Commit);
test_virtual_space_actual_committed_space(10 * M, 2 * M, Commit);
test_virtual_space_actual_committed_space(10 * M, 5 * M, Commit);
test_virtual_space_actual_committed_space(10 * M, 10 * M, Commit);
}
static void test_virtual_space() {
test_virtual_space_actual_committed_space();
test_virtual_space_actual_committed_space_one_large_page();
test_virtual_space_disable_large_pages();
}
};
class ReservedSpaceRunnable : public TestRunnable {
public:
void runUnitTest() const {
TestReservedSpace::test_reserved_space();
}
};
TEST_VM(VirtualSpace, os_reserve_space_concurrent) {
ReservedSpaceRunnable runnable;
ConcurrentTestRunner testRunner(&runnable, 5, 3000);
testRunner.run();
}
class VirtualSpaceRunnable : public TestRunnable {
public:
void runUnitTest() const {
TestVirtualSpace::test_virtual_space();
}
};
TEST_VM(VirtualSpace, os_virtual_space_concurrent) {
VirtualSpaceRunnable runnable;
ConcurrentTestRunner testRunner(&runnable, 5, 3000);
testRunner.run();
}
|