Path: blob/master/src/hotspot/share/oops/compressedOops.cpp
40951 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "logging/log.hpp"26#include "logging/logStream.hpp"27#include "memory/memRegion.hpp"28#include "memory/resourceArea.hpp"29#include "memory/universe.hpp"30#include "memory/virtualspace.hpp"31#include "oops/compressedOops.hpp"32#include "gc/shared/collectedHeap.hpp"33#include "runtime/arguments.hpp"34#include "runtime/globals.hpp"3536// For UseCompressedOops.37NarrowPtrStruct CompressedOops::_narrow_oop = { NULL, 0, true };38MemRegion CompressedOops::_heap_address_range;3940// Choose the heap base address and oop encoding mode41// when compressed oops are used:42// Unscaled - Use 32-bits oops without encoding when43// NarrowOopHeapBaseMin + heap_size < 4Gb44// ZeroBased - Use zero based compressed oops with encoding when45// NarrowOopHeapBaseMin + heap_size < 32Gb46// HeapBased - Use compressed oops with heap base + encoding.47void CompressedOops::initialize(const ReservedHeapSpace& heap_space) {48#ifdef _LP6449// Subtract a page because something can get allocated at heap base.50// This also makes implicit null checking work, because the51// memory+1 page below heap_base needs to cause a signal.52// See needs_explicit_null_check.53// Only set the heap base for compressed oops because it indicates54// compressed oops for pstack code.55if ((uint64_t)heap_space.end() > UnscaledOopHeapMax) {56// Didn't reserve heap below 4Gb. Must shift.57set_shift(LogMinObjAlignmentInBytes);58}59if ((uint64_t)heap_space.end() <= OopEncodingHeapMax) {60// Did reserve heap below 32Gb. Can use base == 0;61set_base(0);62} else {63set_base((address)heap_space.compressed_oop_base());64}6566_heap_address_range = heap_space.region();6768LogTarget(Debug, gc, heap, coops) lt;69if (lt.is_enabled()) {70ResourceMark rm;71LogStream ls(lt);72print_mode(&ls);73}7475// Tell tests in which mode we run.76Arguments::PropertyList_add(new SystemProperty("java.vm.compressedOopsMode",77mode_to_string(mode()),78false));7980// base() is one page below the heap.81assert((intptr_t)base() <= ((intptr_t)_heap_address_range.start() - os::vm_page_size()) ||82base() == NULL, "invalid value");83assert(shift() == LogMinObjAlignmentInBytes ||84shift() == 0, "invalid value");85#endif86}8788void CompressedOops::set_base(address base) {89assert(UseCompressedOops, "no compressed oops?");90_narrow_oop._base = base;91}9293void CompressedOops::set_shift(int shift) {94_narrow_oop._shift = shift;95}9697void CompressedOops::set_use_implicit_null_checks(bool use) {98assert(UseCompressedOops, "no compressed ptrs?");99_narrow_oop._use_implicit_null_checks = use;100}101102bool CompressedOops::is_in(void* addr) {103return _heap_address_range.contains(addr);104}105106bool CompressedOops::is_in(MemRegion mr) {107return _heap_address_range.contains(mr);108}109110CompressedOops::Mode CompressedOops::mode() {111if (base_disjoint()) {112return DisjointBaseNarrowOop;113}114115if (base() != 0) {116return HeapBasedNarrowOop;117}118119if (shift() != 0) {120return ZeroBasedNarrowOop;121}122123return UnscaledNarrowOop;124}125126const char* CompressedOops::mode_to_string(Mode mode) {127switch (mode) {128case UnscaledNarrowOop:129return "32-bit";130case ZeroBasedNarrowOop:131return "Zero based";132case DisjointBaseNarrowOop:133return "Non-zero disjoint base";134case HeapBasedNarrowOop:135return "Non-zero based";136default:137ShouldNotReachHere();138return "";139}140}141142// Test whether bits of addr and possible offsets into the heap overlap.143bool CompressedOops::is_disjoint_heap_base_address(address addr) {144return (((uint64_t)(intptr_t)addr) &145(((uint64_t)UCONST64(0xFFFFffffFFFFffff)) >> (32-LogMinObjAlignmentInBytes))) == 0;146}147148// Check for disjoint base compressed oops.149bool CompressedOops::base_disjoint() {150return _narrow_oop._base != NULL && is_disjoint_heap_base_address(_narrow_oop._base);151}152153// Check for real heapbased compressed oops.154// We must subtract the base as the bits overlap.155// If we negate above function, we also get unscaled and zerobased.156bool CompressedOops::base_overlaps() {157return _narrow_oop._base != NULL && !is_disjoint_heap_base_address(_narrow_oop._base);158}159160void CompressedOops::print_mode(outputStream* st) {161st->print("Heap address: " PTR_FORMAT ", size: " SIZE_FORMAT " MB",162p2i(_heap_address_range.start()), _heap_address_range.byte_size()/M);163164st->print(", Compressed Oops mode: %s", mode_to_string(mode()));165166if (base() != 0) {167st->print(": " PTR_FORMAT, p2i(base()));168}169170if (shift() != 0) {171st->print(", Oop shift amount: %d", shift());172}173174if (!use_implicit_null_checks()) {175st->print(", no protected page in front of the heap");176}177st->cr();178}179180// For UseCompressedClassPointers.181NarrowPtrStruct CompressedKlassPointers::_narrow_klass = { NULL, 0, true };182183// CompressedClassSpaceSize set to 1GB, but appear 3GB away from _narrow_ptrs_base during CDS dump.184// (Todo: we should #ifdef out CompressedKlassPointers for 32bit completely and fix all call sites which185// are compiled for 32bit to LP64_ONLY).186size_t CompressedKlassPointers::_range = 0;187188189// Given an address range [addr, addr+len) which the encoding is supposed to190// cover, choose base, shift and range.191// The address range is the expected range of uncompressed Klass pointers we192// will encounter (and the implicit promise that there will be no Klass193// structures outside this range).194void CompressedKlassPointers::initialize(address addr, size_t len) {195#ifdef _LP64196assert(is_valid_base(addr), "Address must be a valid encoding base");197address const end = addr + len;198199address base;200int shift;201size_t range;202203if (UseSharedSpaces || DumpSharedSpaces) {204205// Special requirements if CDS is active:206// Encoding base and shift must be the same between dump and run time.207// CDS takes care that the SharedBaseAddress and CompressedClassSpaceSize208// are the same. Archive size will be probably different at runtime, but209// it can only be smaller than at, never larger, since archives get210// shrunk at the end of the dump process.211// From that it follows that the range [addr, len) we are handed in at212// runtime will start at the same address then at dumptime, and its len213// may be smaller at runtime then it was at dump time.214//215// To be very careful here, we avoid any optimizations and just keep using216// the same address and shift value. Specifically we avoid using zero-based217// encoding. We also set the expected value range to 4G (encoding range218// cannot be larger than that).219220base = addr;221222// JDK-8265705223// This is a temporary fix for aarch64: there, if the range-to-be-encoded is located224// below 32g, either encoding base should be zero or base should be aligned to 4G225// and shift should be zero. The simplest way to fix this for now is to force226// shift to zero for both runtime and dumptime.227// Note however that this is not a perfect solution. Ideally this whole function228// should be CDS agnostic, that would simplify it - and testing - alot. See JDK-8267141229// for details.230shift = 0;231232// This must be true since at dumptime cds+ccs is 4G, at runtime it can233// only be smaller, see comment above.234assert(len <= 4 * G, "Encoding range cannot be larger than 4G");235range = 4 * G;236237} else {238239// Otherwise we attempt to use a zero base if the range fits in lower 32G.240if (end <= (address)KlassEncodingMetaspaceMax) {241base = 0;242} else {243base = addr;244}245246// Highest offset a Klass* can ever have in relation to base.247range = end - base;248249// We may not even need a shift if the range fits into 32bit:250const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);251if (range < UnscaledClassSpaceMax) {252shift = 0;253} else {254shift = LogKlassAlignmentInBytes;255}256257}258259set_base(base);260set_shift(shift);261set_range(range);262#else263fatal("64bit only.");264#endif265}266267// Given an address p, return true if p can be used as an encoding base.268// (Some platforms have restrictions of what constitutes a valid base address).269bool CompressedKlassPointers::is_valid_base(address p) {270#ifdef AARCH64271// Below 32G, base must be aligned to 4G.272// Above that point, base must be aligned to 32G273if (p < (address)(32 * G)) {274return is_aligned(p, 4 * G);275}276return is_aligned(p, (4 << LogKlassAlignmentInBytes) * G);277#else278return true;279#endif280}281282void CompressedKlassPointers::print_mode(outputStream* st) {283st->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: %d, "284"Narrow klass range: " SIZE_FORMAT_HEX, p2i(base()), shift(),285range());286}287288void CompressedKlassPointers::set_base(address base) {289assert(UseCompressedClassPointers, "no compressed klass ptrs?");290_narrow_klass._base = base;291}292293void CompressedKlassPointers::set_shift(int shift) {294assert(shift == 0 || shift == LogKlassAlignmentInBytes, "invalid shift for klass ptrs");295_narrow_klass._shift = shift;296}297298void CompressedKlassPointers::set_range(size_t range) {299assert(UseCompressedClassPointers, "no compressed klass ptrs?");300_range = range;301}302303304