Path: blob/master/src/hotspot/share/gc/z/zCollectedHeap.cpp
40957 views
/*1* Copyright (c) 2015, 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*/2223#include "precompiled.hpp"24#include "classfile/classLoaderData.hpp"25#include "gc/shared/gcHeapSummary.hpp"26#include "gc/shared/suspendibleThreadSet.hpp"27#include "gc/z/zCollectedHeap.hpp"28#include "gc/z/zDirector.hpp"29#include "gc/z/zDriver.hpp"30#include "gc/z/zGlobals.hpp"31#include "gc/z/zHeap.inline.hpp"32#include "gc/z/zNMethod.hpp"33#include "gc/z/zObjArrayAllocator.hpp"34#include "gc/z/zOop.inline.hpp"35#include "gc/z/zServiceability.hpp"36#include "gc/z/zStat.hpp"37#include "gc/z/zUtils.inline.hpp"38#include "memory/classLoaderMetaspace.hpp"39#include "memory/iterator.hpp"40#include "memory/universe.hpp"41#include "utilities/align.hpp"4243ZCollectedHeap* ZCollectedHeap::heap() {44return named_heap<ZCollectedHeap>(CollectedHeap::Z);45}4647ZCollectedHeap::ZCollectedHeap() :48_soft_ref_policy(),49_barrier_set(),50_initialize(&_barrier_set),51_heap(),52_director(new ZDirector()),53_driver(new ZDriver()),54_stat(new ZStat()),55_runtime_workers() {}5657CollectedHeap::Name ZCollectedHeap::kind() const {58return CollectedHeap::Z;59}6061const char* ZCollectedHeap::name() const {62return ZName;63}6465jint ZCollectedHeap::initialize() {66if (!_heap.is_initialized()) {67return JNI_ENOMEM;68}6970Universe::calculate_verify_data((HeapWord*)0, (HeapWord*)UINTPTR_MAX);7172return JNI_OK;73}7475void ZCollectedHeap::initialize_serviceability() {76_heap.serviceability_initialize();77}7879class ZStopConcurrentGCThreadClosure : public ThreadClosure {80public:81virtual void do_thread(Thread* thread) {82if (thread->is_ConcurrentGC_thread() &&83!thread->is_GC_task_thread()) {84static_cast<ConcurrentGCThread*>(thread)->stop();85}86}87};8889void ZCollectedHeap::stop() {90ZStopConcurrentGCThreadClosure cl;91gc_threads_do(&cl);92}9394SoftRefPolicy* ZCollectedHeap::soft_ref_policy() {95return &_soft_ref_policy;96}9798size_t ZCollectedHeap::max_capacity() const {99return _heap.max_capacity();100}101102size_t ZCollectedHeap::capacity() const {103return _heap.capacity();104}105106size_t ZCollectedHeap::used() const {107return _heap.used();108}109110size_t ZCollectedHeap::unused() const {111return _heap.unused();112}113114bool ZCollectedHeap::is_maximal_no_gc() const {115// Not supported116ShouldNotReachHere();117return false;118}119120bool ZCollectedHeap::is_in(const void* p) const {121return _heap.is_in((uintptr_t)p);122}123124uint32_t ZCollectedHeap::hash_oop(oop obj) const {125return _heap.hash_oop(ZOop::to_address(obj));126}127128HeapWord* ZCollectedHeap::allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) {129const size_t size_in_bytes = ZUtils::words_to_bytes(align_object_size(requested_size));130const uintptr_t addr = _heap.alloc_tlab(size_in_bytes);131132if (addr != 0) {133*actual_size = requested_size;134}135136return (HeapWord*)addr;137}138139oop ZCollectedHeap::array_allocate(Klass* klass, int size, int length, bool do_zero, TRAPS) {140if (!do_zero) {141return CollectedHeap::array_allocate(klass, size, length, false /* do_zero */, THREAD);142}143144ZObjArrayAllocator allocator(klass, size, length, THREAD);145return allocator.allocate();146}147148HeapWord* ZCollectedHeap::mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded) {149const size_t size_in_bytes = ZUtils::words_to_bytes(align_object_size(size));150return (HeapWord*)_heap.alloc_object(size_in_bytes);151}152153MetaWord* ZCollectedHeap::satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,154size_t size,155Metaspace::MetadataType mdtype) {156MetaWord* result;157158// Start asynchronous GC159collect(GCCause::_metadata_GC_threshold);160161// Expand and retry allocation162result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);163if (result != NULL) {164return result;165}166167// Start synchronous GC168collect(GCCause::_metadata_GC_clear_soft_refs);169170// Retry allocation171result = loader_data->metaspace_non_null()->allocate(size, mdtype);172if (result != NULL) {173return result;174}175176// Expand and retry allocation177result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);178if (result != NULL) {179return result;180}181182// Out of memory183return NULL;184}185186void ZCollectedHeap::collect(GCCause::Cause cause) {187_driver->collect(cause);188}189190void ZCollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {191// These collection requests are ignored since ZGC can't run a synchronous192// GC cycle from within the VM thread. This is considered benign, since the193// only GC causes coming in here should be heap dumper and heap inspector.194// However, neither the heap dumper nor the heap inspector really need a GC195// to happen, but the result of their heap iterations might in that case be196// less accurate since they might include objects that would otherwise have197// been collected by a GC.198assert(Thread::current()->is_VM_thread(), "Should be the VM thread");199guarantee(cause == GCCause::_heap_dump ||200cause == GCCause::_heap_inspection, "Invalid cause");201}202203void ZCollectedHeap::do_full_collection(bool clear_all_soft_refs) {204// Not supported205ShouldNotReachHere();206}207208size_t ZCollectedHeap::tlab_capacity(Thread* ignored) const {209return _heap.tlab_capacity();210}211212size_t ZCollectedHeap::tlab_used(Thread* ignored) const {213return _heap.tlab_used();214}215216size_t ZCollectedHeap::max_tlab_size() const {217return _heap.max_tlab_size();218}219220size_t ZCollectedHeap::unsafe_max_tlab_alloc(Thread* ignored) const {221return _heap.unsafe_max_tlab_alloc();222}223224bool ZCollectedHeap::uses_stack_watermark_barrier() const {225return true;226}227228GrowableArray<GCMemoryManager*> ZCollectedHeap::memory_managers() {229GrowableArray<GCMemoryManager*> memory_managers(2);230memory_managers.append(_heap.serviceability_cycle_memory_manager());231memory_managers.append(_heap.serviceability_pause_memory_manager());232return memory_managers;233}234235GrowableArray<MemoryPool*> ZCollectedHeap::memory_pools() {236GrowableArray<MemoryPool*> memory_pools(1);237memory_pools.append(_heap.serviceability_memory_pool());238return memory_pools;239}240241void ZCollectedHeap::object_iterate(ObjectClosure* cl) {242_heap.object_iterate(cl, true /* visit_weaks */);243}244245ParallelObjectIterator* ZCollectedHeap::parallel_object_iterator(uint nworkers) {246return _heap.parallel_object_iterator(nworkers, true /* visit_weaks */);247}248249void ZCollectedHeap::keep_alive(oop obj) {250_heap.keep_alive(obj);251}252253void ZCollectedHeap::register_nmethod(nmethod* nm) {254ZNMethod::register_nmethod(nm);255}256257void ZCollectedHeap::unregister_nmethod(nmethod* nm) {258ZNMethod::unregister_nmethod(nm);259}260261void ZCollectedHeap::flush_nmethod(nmethod* nm) {262ZNMethod::flush_nmethod(nm);263}264265void ZCollectedHeap::verify_nmethod(nmethod* nm) {266// Does nothing267}268269WorkGang* ZCollectedHeap::safepoint_workers() {270return _runtime_workers.workers();271}272273void ZCollectedHeap::gc_threads_do(ThreadClosure* tc) const {274tc->do_thread(_director);275tc->do_thread(_driver);276tc->do_thread(_stat);277_heap.threads_do(tc);278_runtime_workers.threads_do(tc);279}280281VirtualSpaceSummary ZCollectedHeap::create_heap_space_summary() {282return VirtualSpaceSummary((HeapWord*)0, (HeapWord*)capacity(), (HeapWord*)max_capacity());283}284285void ZCollectedHeap::safepoint_synchronize_begin() {286SuspendibleThreadSet::synchronize();287}288289void ZCollectedHeap::safepoint_synchronize_end() {290SuspendibleThreadSet::desynchronize();291}292293void ZCollectedHeap::prepare_for_verify() {294// Does nothing295}296297void ZCollectedHeap::print_on(outputStream* st) const {298_heap.print_on(st);299}300301void ZCollectedHeap::print_on_error(outputStream* st) const {302st->print_cr("ZGC Globals:");303st->print_cr(" GlobalPhase: %u (%s)", ZGlobalPhase, ZGlobalPhaseToString());304st->print_cr(" GlobalSeqNum: %u", ZGlobalSeqNum);305st->print_cr(" Offset Max: " SIZE_FORMAT "%s (" PTR_FORMAT ")",306byte_size_in_exact_unit(ZAddressOffsetMax),307exact_unit_for_byte_size(ZAddressOffsetMax),308ZAddressOffsetMax);309st->print_cr(" Page Size Small: " SIZE_FORMAT "M", ZPageSizeSmall / M);310st->print_cr(" Page Size Medium: " SIZE_FORMAT "M", ZPageSizeMedium / M);311st->cr();312st->print_cr("ZGC Metadata Bits:");313st->print_cr(" Good: " PTR_FORMAT, ZAddressGoodMask);314st->print_cr(" Bad: " PTR_FORMAT, ZAddressBadMask);315st->print_cr(" WeakBad: " PTR_FORMAT, ZAddressWeakBadMask);316st->print_cr(" Marked: " PTR_FORMAT, ZAddressMetadataMarked);317st->print_cr(" Remapped: " PTR_FORMAT, ZAddressMetadataRemapped);318st->cr();319CollectedHeap::print_on_error(st);320}321322void ZCollectedHeap::print_extended_on(outputStream* st) const {323_heap.print_extended_on(st);324}325326void ZCollectedHeap::print_tracing_info() const {327// Does nothing328}329330bool ZCollectedHeap::print_location(outputStream* st, void* addr) const {331return _heap.print_location(st, (uintptr_t)addr);332}333334void ZCollectedHeap::verify(VerifyOption option /* ignored */) {335_heap.verify();336}337338bool ZCollectedHeap::is_oop(oop object) const {339return _heap.is_oop(ZOop::to_address(object));340}341342bool ZCollectedHeap::supports_concurrent_gc_breakpoints() const {343return true;344}345346347