Skip to content

Commit f795830

Browse files
Yumin QiYumin Qi
authored andcommitted
Add total_space_rs, total reserved space to release_reserved_spaces and reserve_address_space_for_archives, made changes to check failed output on test.
1 parent be7c72f commit f795830

File tree

4 files changed

+56
-34
lines changed

4 files changed

+56
-34
lines changed

src/hotspot/share/memory/metaspaceShared.cpp

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,10 +1364,13 @@ MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, File
13641364
assert(static_mapinfo->mapping_end_offset() == dynamic_mapinfo->mapping_base_offset(), "no gap");
13651365
}
13661366

1367-
ReservedSpace archive_space_rs, class_space_rs;
1367+
ReservedSpace total_space_rs, archive_space_rs, class_space_rs;
13681368
MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1369-
char* mapped_base_address = reserve_address_space_for_archives(static_mapinfo, dynamic_mapinfo,
1370-
use_requested_addr, archive_space_rs,
1369+
char* mapped_base_address = reserve_address_space_for_archives(static_mapinfo,
1370+
dynamic_mapinfo,
1371+
use_requested_addr,
1372+
total_space_rs,
1373+
archive_space_rs,
13711374
class_space_rs);
13721375
if (mapped_base_address == NULL) {
13731376
result = MAP_ARCHIVE_MMAP_FAILURE;
@@ -1417,6 +1420,7 @@ MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, File
14171420
// this with use_requested_addr, since we're going to patch all the
14181421
// pointers anyway so there's no benefit to mmap.
14191422
if (use_requested_addr) {
1423+
assert(!total_space_rs.is_reserved(), "Should not be reserved for Windows");
14201424
log_info(cds)("Windows mmap workaround: releasing archive space.");
14211425
archive_space_rs.release();
14221426
}
@@ -1485,7 +1489,8 @@ MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, File
14851489
} else {
14861490
unmap_archive(static_mapinfo);
14871491
unmap_archive(dynamic_mapinfo);
1488-
release_reserved_spaces(archive_space_rs, class_space_rs);
1492+
log_debug(cds)("Released shared space " INTPTR_FORMAT, p2i(total_space_rs.base()));
1493+
release_reserved_spaces(total_space_rs, archive_space_rs, class_space_rs);
14891494
}
14901495

14911496
return result;
@@ -1534,6 +1539,9 @@ MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, File
15341539
// Return:
15351540
//
15361541
// - On success:
1542+
// - total_space_rs will be reserved as whole for archive_space_rs and
1543+
// class_space_rs if UseCompressedClassPointers is true (not Windows).
1544+
// On Windows, total_space_rs is not reserved.
15371545
// - archive_space_rs will be reserved and large enough to host static and
15381546
// if needed dynamic archive: [Base, A).
15391547
// archive_space_rs.base and size will be aligned to CDS reserve
@@ -1548,6 +1556,7 @@ MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, File
15481556
char* MetaspaceShared::reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
15491557
FileMapInfo* dynamic_mapinfo,
15501558
bool use_archive_base_addr,
1559+
ReservedSpace& total_space_rs,
15511560
ReservedSpace& archive_space_rs,
15521561
ReservedSpace& class_space_rs) {
15531562

@@ -1628,38 +1637,36 @@ char* MetaspaceShared::reserve_address_space_for_archives(FileMapInfo* static_ma
16281637
false /* large */, (char*)ccs_base);
16291638
}
16301639
if (!archive_space_rs.is_reserved() || !class_space_rs.is_reserved()) {
1631-
release_reserved_spaces(archive_space_rs, class_space_rs);
1640+
release_reserved_spaces(total_space_rs, archive_space_rs, class_space_rs);
16321641
return NULL;
16331642
}
16341643
} else {
1635-
ReservedSpace total_rs;
1636-
if (base_address != NULL) {
1637-
// Reserve at the given archive base address, or not at all.
1638-
total_rs = ReservedSpace(total_range_size, archive_space_alignment,
1639-
false /* bool large */, (char*) base_address);
1644+
if (use_archive_base_addr && base_address != nullptr) {
1645+
total_space_rs = ReservedSpace(total_range_size, archive_space_alignment,
1646+
false /* bool large */, (char*) base_address);
16401647
} else {
16411648
// Reserve at any address, but leave it up to the platform to choose a good one.
1642-
total_rs = Metaspace::reserve_address_space_for_compressed_classes(total_range_size);
1649+
total_space_rs = Metaspace::reserve_address_space_for_compressed_classes(total_range_size);
16431650
}
16441651

1645-
if (!total_rs.is_reserved()) {
1652+
if (!total_space_rs.is_reserved()) {
16461653
return NULL;
16471654
}
16481655

16491656
// Paranoid checks:
1650-
assert(base_address == NULL || (address)total_rs.base() == base_address,
1651-
"Sanity (" PTR_FORMAT " vs " PTR_FORMAT ")", p2i(base_address), p2i(total_rs.base()));
1652-
assert(is_aligned(total_rs.base(), archive_space_alignment), "Sanity");
1653-
assert(total_rs.size() == total_range_size, "Sanity");
1654-
assert(CompressedKlassPointers::is_valid_base((address)total_rs.base()), "Sanity");
1657+
assert(base_address == NULL || (address)total_space_rs.base() == base_address,
1658+
"Sanity (" PTR_FORMAT " vs " PTR_FORMAT ")", p2i(base_address), p2i(total_space_rs.base()));
1659+
assert(is_aligned(total_space_rs.base(), archive_space_alignment), "Sanity");
1660+
assert(total_space_rs.size() == total_range_size, "Sanity");
1661+
assert(CompressedKlassPointers::is_valid_base((address)total_space_rs.base()), "Sanity");
16551662

16561663
// Now split up the space into ccs and cds archive. For simplicity, just leave
1657-
// the gap reserved at the end of the archive space.
1658-
archive_space_rs = total_rs.first_part(ccs_begin_offset,
1659-
(size_t)os::vm_allocation_granularity(),
1660-
/*split=*/false);
1661-
class_space_rs = total_rs.last_part(ccs_begin_offset);
1662-
MemTracker::record_virtual_memory_split_reserved(total_rs.base(), total_rs.size(),
1664+
// the gap reserved at the end of the archive space. Do not do real splitting.
1665+
archive_space_rs = total_space_rs.first_part(ccs_begin_offset,
1666+
(size_t)os::vm_allocation_granularity(),
1667+
/*split=*/false);
1668+
class_space_rs = total_space_rs.last_part(ccs_begin_offset);
1669+
MemTracker::record_virtual_memory_split_reserved(total_space_rs.base(), total_space_rs.size(),
16631670
ccs_begin_offset);
16641671
}
16651672
assert(is_aligned(archive_space_rs.base(), archive_space_alignment), "Sanity");
@@ -1680,15 +1687,21 @@ char* MetaspaceShared::reserve_address_space_for_archives(FileMapInfo* static_ma
16801687

16811688
}
16821689

1683-
void MetaspaceShared::release_reserved_spaces(ReservedSpace& archive_space_rs,
1690+
void MetaspaceShared::release_reserved_spaces(ReservedSpace& total_space_rs,
1691+
ReservedSpace& archive_space_rs,
16841692
ReservedSpace& class_space_rs) {
1685-
if (archive_space_rs.is_reserved()) {
1686-
log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base()));
1687-
archive_space_rs.release();
1688-
}
1689-
if (class_space_rs.is_reserved()) {
1690-
log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1691-
class_space_rs.release();
1693+
if (total_space_rs.is_reserved()) {
1694+
log_debug(cds)("Released shared space (archive + class) " INTPTR_FORMAT, p2i(total_space_rs.base()));
1695+
total_space_rs.release();
1696+
} else {
1697+
if (archive_space_rs.is_reserved()) {
1698+
log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base()));
1699+
archive_space_rs.release();
1700+
}
1701+
if (class_space_rs.is_reserved()) {
1702+
log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1703+
class_space_rs.release();
1704+
}
16921705
}
16931706
}
16941707

src/hotspot/share/memory/metaspaceShared.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,12 @@ class MetaspaceShared : AllStatic {
288288
static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
289289
FileMapInfo* dynamic_mapinfo,
290290
bool use_archive_base_addr,
291+
ReservedSpace& total_space_rs,
291292
ReservedSpace& archive_space_rs,
292293
ReservedSpace& class_space_rs);
293-
static void release_reserved_spaces(ReservedSpace& archive_space_rs,
294-
ReservedSpace& class_space_rs);
294+
static void release_reserved_spaces(ReservedSpace& total_space_rs,
295+
ReservedSpace& archive_space_rs,
296+
ReservedSpace& class_space_rs);
295297
static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs);
296298
static void unmap_archive(FileMapInfo* mapinfo);
297299
};

src/hotspot/share/runtime/os.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,9 @@ bool os::release_memory(char* addr, size_t bytes) {
17231723
} else {
17241724
res = pd_release_memory(addr, bytes);
17251725
}
1726+
if (!res) {
1727+
log_info(os)("os::release_memory(" PTR_FORMAT ", " SIZE_FORMAT ") failed", p2i(addr), bytes);
1728+
}
17261729
return res;
17271730
}
17281731

test/hotspot/jtreg/runtime/cds/SharedBaseAddress.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public class SharedBaseAddress {
5050
"0", // always let OS pick the base address at runtime (ASLR for CDS archive)
5151
};
5252

53+
// failed pattern
54+
private static String failedPattern ="os::release_memory(.*,\\s.*)\\sfailed";
55+
5356
public static void main(String[] args) throws Exception {
5457

5558
for (String testEntry : testTable) {
@@ -68,7 +71,8 @@ public static void main(String[] args) throws Exception {
6871
OutputAnalyzer out = CDSTestUtils.runWithArchiveAndCheck(opts);
6972
if (testEntry.equals("0")) {
7073
out.shouldContain("Archive(s) were created with -XX:SharedBaseAddress=0. Always map at os-selected address.")
71-
.shouldContain("Try to map archive(s) at an alternative address");
74+
.shouldContain("Try to map archive(s) at an alternative address")
75+
.shouldNotMatch(failedPattern);
7276
}
7377
}
7478
}

0 commit comments

Comments
 (0)