/* * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved. * * 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 "classfile/classLoaderDataGraph.hpp" #include "classfile/stringTable.hpp" #include "classfile/systemDictionary.hpp" #include "code/codeCache.hpp" #include "gc/epsilon/epsilonHeap.hpp" #include "gc/epsilon/epsilonMemoryPool.hpp" #include "gc/epsilon/epsilonThreadLocalData.hpp" #include "gc/shared/barrierSet.inline.hpp" #include "gc/shared/gcTraceTime.inline.hpp" #include "gc/shared/markBitMap.inline.hpp" #include "gc/shared/strongRootsScope.hpp" #include "gc/shared/preservedMarks.inline.hpp" #include "gc/shared/weakProcessor.hpp" #include "memory/allocation.inline.hpp" #include "memory/iterator.inline.hpp" #include "memory/resourceArea.hpp" #include "oops/compressedOops.inline.hpp" #include "oops/markOop.inline.hpp" #include "runtime/biasedLocking.hpp" #include "runtime/objectMonitor.inline.hpp" #include "runtime/thread.hpp" #include "runtime/vmOperations.hpp" #include "runtime/vmThread.hpp" #include "utilities/stack.inline.hpp" #include "services/management.hpp" jint EpsilonHeap::initialize() { size_t align = _policy->heap_alignment(); size_t init_byte_size = align_up(_policy->initial_heap_byte_size(), align); size_t max_byte_size = align_up(_policy->max_heap_byte_size(), align); // Initialize backing storage ReservedSpace heap_rs = Universe::reserve_heap(max_byte_size, align); _virtual_space.initialize(heap_rs, init_byte_size); MemRegion committed_region((HeapWord*)_virtual_space.low(), (HeapWord*)_virtual_space.high()); MemRegion reserved_region((HeapWord*)_virtual_space.low_boundary(), (HeapWord*)_virtual_space.high_boundary()); initialize_reserved_region(reserved_region.start(), reserved_region.end()); _space = new ContiguousSpace(); _space->initialize(committed_region, /* clear_space = */ true, /* mangle_space = */ true); // Precompute hot fields _max_tlab_size = MIN2(CollectedHeap::max_tlab_size(), align_object_size(EpsilonMaxTLABSize / HeapWordSize)); _step_counter_update = MIN2(max_byte_size / 16, EpsilonUpdateCountersStep); _step_heap_print = (EpsilonPrintHeapSteps == 0) ? SIZE_MAX : (max_byte_size / EpsilonPrintHeapSteps); _decay_time_ns = (int64_t) EpsilonTLABDecayTime * NANOSECS_PER_MILLISEC; // Enable monitoring _monitoring_support = new EpsilonMonitoringSupport(this); _last_counter_update = 0; _last_heap_print = 0; // Install barrier set BarrierSet::set_barrier_set(new EpsilonBarrierSet()); size_t bitmap_page_size = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size(); size_t _bitmap_size = MarkBitMap::compute_size(heap_rs.size()); _bitmap_size = align_up(_bitmap_size, bitmap_page_size); // Initialize marking bitmap if (EpsilonWhyNotGCAnyway) { ReservedSpace bitmap(_bitmap_size, bitmap_page_size); os::commit_memory_or_exit(bitmap.base(), bitmap.size(), false, "couldn't allocate marking bitmap"); MemTracker::record_virtual_memory_type(bitmap.base(), mtGC); MemRegion bitmap_region = MemRegion((HeapWord *) bitmap.base(), bitmap.size() / HeapWordSize); MemRegion heap_region = MemRegion((HeapWord *) heap_rs.base(), heap_rs.size() / HeapWordSize); _bitmap.initialize(heap_region, bitmap_region); } // All done, print out the configuration if (init_byte_size != max_byte_size) { log_info(gc)("Resizeable heap; starting at " SIZE_FORMAT "M, max: " SIZE_FORMAT "M, step: " SIZE_FORMAT "M", init_byte_size / M, max_byte_size / M, EpsilonMinHeapExpand / M); } else { log_info(gc)("Non-resizeable heap; start/max: " SIZE_FORMAT "M", init_byte_size / M); } if (UseTLAB) { log_info(gc)("Using TLAB allocation; max: " SIZE_FORMAT "K", _max_tlab_size * HeapWordSize / K); if (EpsilonElasticTLAB) { log_info(gc)("Elastic TLABs enabled; elasticity: %.2fx", EpsilonTLABElasticity); } if (EpsilonElasticTLABDecay) { log_info(gc)("Elastic TLABs decay enabled; decay time: " SIZE_FORMAT "ms", EpsilonTLABDecayTime); } } else { log_info(gc)("Not using TLAB allocation"); } return JNI_OK; } void EpsilonHeap::post_initialize() { CollectedHeap::post_initialize(); } void EpsilonHeap::initialize_serviceability() { _pool = new EpsilonMemoryPool(this); _memory_manager.add_pool(_pool); } GrowableArray EpsilonHeap::memory_managers() { GrowableArray memory_managers(1); memory_managers.append(&_memory_manager); return memory_managers; } GrowableArray EpsilonHeap::memory_pools() { GrowableArray memory_pools(1); memory_pools.append(_pool); return memory_pools; } size_t EpsilonHeap::unsafe_max_tlab_alloc(Thread* thr) const { // Return max allocatable TLAB size, and let allocation path figure out // the actual TLAB allocation size. return _max_tlab_size; } EpsilonHeap* EpsilonHeap::heap() { CollectedHeap* heap = Universe::heap(); assert(heap != NULL, "Uninitialized access to EpsilonHeap::heap()"); assert(heap->kind() == CollectedHeap::Epsilon, "Not an Epsilon heap"); return (EpsilonHeap*)heap; } HeapWord* EpsilonHeap::allocate_work(size_t size) { assert(is_object_aligned(size), "Allocation size should be aligned: " SIZE_FORMAT, size); HeapWord* res = _space->par_allocate(size); while (res == NULL) { // Allocation failed, attempt expansion, and retry: MutexLockerEx ml(Heap_lock); size_t space_left = max_capacity() - capacity(); size_t want_space = MAX2(size, EpsilonMinHeapExpand); if (want_space < space_left) { // Enough space to expand in bulk: bool expand = _virtual_space.expand_by(want_space); assert(expand, "Should be able to expand"); } else if (size < space_left) { // No space to expand in bulk, and this allocation is still possible, // take all the remaining space: bool expand = _virtual_space.expand_by(space_left); assert(expand, "Should be able to expand"); } else { // No space left: return NULL; } _space->set_end((HeapWord *) _virtual_space.high()); res = _space->par_allocate(size); } size_t used = _space->used(); // Allocation successful, update counters { size_t last = _last_counter_update; if ((used - last >= _step_counter_update) && Atomic::cmpxchg(used, &_last_counter_update, last) == last) { _monitoring_support->update_counters(); } } // ...and print the occupancy line, if needed { size_t last = _last_heap_print; if ((used - last >= _step_heap_print) && Atomic::cmpxchg(used, &_last_heap_print, last) == last) { print_heap_info(used); print_metaspace_info(); } } assert(is_object_aligned(res), "Object should be aligned: " PTR_FORMAT, p2i(res)); return res; } HeapWord* EpsilonHeap::allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) { Thread* thread = Thread::current(); // Defaults in case elastic paths are not taken bool fits = true; size_t size = requested_size; size_t ergo_tlab = requested_size; int64_t time = 0; if (EpsilonElasticTLAB) { ergo_tlab = EpsilonThreadLocalData::ergo_tlab_size(thread); if (EpsilonElasticTLABDecay) { int64_t last_time = EpsilonThreadLocalData::last_tlab_time(thread); time = (int64_t) os::javaTimeNanos(); assert(last_time <= time, "time should be monotonic"); // If the thread had not allocated recently, retract the ergonomic size. // This conserves memory when the thread had initial burst of allocations, // and then started allocating only sporadically. if (last_time != 0 && (time - last_time > _decay_time_ns)) { ergo_tlab = 0; EpsilonThreadLocalData::set_ergo_tlab_size(thread, 0); } } // If we can fit the allocation under current TLAB size, do so. // Otherwise, we want to elastically increase the TLAB size. fits = (requested_size <= ergo_tlab); if (!fits) { size = (size_t) (ergo_tlab * EpsilonTLABElasticity); } } // Always honor boundaries size = MAX2(min_size, MIN2(_max_tlab_size, size)); // Always honor alignment size = align_up(size, MinObjAlignment); // Check that adjustments did not break local and global invariants assert(is_object_aligned(size), "Size honors object alignment: " SIZE_FORMAT, size); assert(min_size <= size, "Size honors min size: " SIZE_FORMAT " <= " SIZE_FORMAT, min_size, size); assert(size <= _max_tlab_size, "Size honors max size: " SIZE_FORMAT " <= " SIZE_FORMAT, size, _max_tlab_size); assert(size <= CollectedHeap::max_tlab_size(), "Size honors global max size: " SIZE_FORMAT " <= " SIZE_FORMAT, size, CollectedHeap::max_tlab_size()); if (log_is_enabled(Trace, gc)) { ResourceMark rm; log_trace(gc)("TLAB size for \"%s\" (Requested: " SIZE_FORMAT "K, Min: " SIZE_FORMAT "K, Max: " SIZE_FORMAT "K, Ergo: " SIZE_FORMAT "K) -> " SIZE_FORMAT "K", thread->name(), requested_size * HeapWordSize / K, min_size * HeapWordSize / K, _max_tlab_size * HeapWordSize / K, ergo_tlab * HeapWordSize / K, size * HeapWordSize / K); } // All prepared, let's do it! HeapWord* res = allocate_or_collect_work(size); if (res != NULL) { // Allocation successful *actual_size = size; if (EpsilonElasticTLABDecay) { EpsilonThreadLocalData::set_last_tlab_time(thread, time); } if (EpsilonElasticTLAB && !fits) { // If we requested expansion, this is our new ergonomic TLAB size EpsilonThreadLocalData::set_ergo_tlab_size(thread, size); } } else { // Allocation failed, reset ergonomics to try and fit smaller TLABs if (EpsilonElasticTLAB) { EpsilonThreadLocalData::set_ergo_tlab_size(thread, 0); } } return res; } HeapWord* EpsilonHeap::mem_allocate(size_t size, bool *gc_overhead_limit_was_exceeded) { *gc_overhead_limit_was_exceeded = false; return allocate_or_collect_work(size); } void EpsilonHeap::collect(GCCause::Cause cause) { switch (cause) { case GCCause::_metadata_GC_threshold: case GCCause::_metadata_GC_clear_soft_refs: // Receiving these causes means the VM itself entered the safepoint for metadata collection. // While Epsilon does not do GC, it has to perform sizing adjustments, otherwise we would // re-enter the safepoint again very soon. assert(SafepointSynchronize::is_at_safepoint(), "Expected at safepoint"); log_info(gc)("GC request for \"%s\" is handled", GCCause::to_string(cause)); MetaspaceGC::compute_new_size(); print_metaspace_info(); break; default: if (EpsilonWhyNotGCAnyway) { if (SafepointSynchronize::is_at_safepoint()) { entry_collect(cause); } else { vmentry_collect(cause); } } else { log_info(gc)("GC request for \"%s\" is ignored", GCCause::to_string(cause)); } } _monitoring_support->update_counters(); } void EpsilonHeap::do_full_collection(bool clear_all_soft_refs) { collect(gc_cause()); } void EpsilonHeap::safe_object_iterate(ObjectClosure *cl) { _space->safe_object_iterate(cl); } void EpsilonHeap::print_on(outputStream *st) const { st->print_cr("Epsilon Heap"); // Cast away constness: ((VirtualSpace)_virtual_space).print_on(st); st->print_cr("Allocation space:"); _space->print_on(st); MetaspaceUtils::print_on(st); } void EpsilonHeap::print_tracing_info() const { print_heap_info(used()); print_metaspace_info(); } void EpsilonHeap::print_heap_info(size_t used) const { size_t reserved = max_capacity(); size_t committed = capacity(); if (reserved != 0) { log_info(gc)("Heap: " SIZE_FORMAT "%s reserved, " SIZE_FORMAT "%s (%.2f%%) committed, " SIZE_FORMAT "%s (%.2f%%) used", byte_size_in_proper_unit(reserved), proper_unit_for_byte_size(reserved), byte_size_in_proper_unit(committed), proper_unit_for_byte_size(committed), committed * 100.0 / reserved, byte_size_in_proper_unit(used), proper_unit_for_byte_size(used), used * 100.0 / reserved); } else { log_info(gc)("Heap: no reliable data"); } } void EpsilonHeap::print_metaspace_info() const { size_t reserved = MetaspaceUtils::reserved_bytes(); size_t committed = MetaspaceUtils::committed_bytes(); size_t used = MetaspaceUtils::used_bytes(); if (reserved != 0) { log_info(gc, metaspace)("Metaspace: " SIZE_FORMAT "%s reserved, " SIZE_FORMAT "%s (%.2f%%) committed, " SIZE_FORMAT "%s (%.2f%%) used", byte_size_in_proper_unit(reserved), proper_unit_for_byte_size(reserved), byte_size_in_proper_unit(committed), proper_unit_for_byte_size(committed), committed * 100.0 / reserved, byte_size_in_proper_unit(used), proper_unit_for_byte_size(used), used * 100.0 / reserved); } else { log_info(gc, metaspace)("Metaspace: no reliable data"); } } // ------------------------------- EXPERIMENTAL MARK-COMPACT -------------------------------------------- // // This implements a trivial Lisp2-style sliding collector: // https://en.wikipedia.org/wiki/Mark-compact_algorithm#LISP2_algorithm // // The goal for this implementation is to be as trivial as possible, ignoring even the // basic and obvious performance optimizations. // // VM operation that executes collection cycle under safepoint class VM_EpsilonCollect: public VM_Operation { private: GCCause::Cause _cause; public: VM_EpsilonCollect(GCCause::Cause cause) : VM_Operation(), _cause(cause) {}; VM_Operation::VMOp_Type type() const { return VMOp_EpsilonCollect; } const char* name() const { return "Epsilon Collection"; } virtual void doit() { EpsilonHeap* heap = EpsilonHeap::heap(); heap->entry_collect(_cause); if (EpsilonWhyNotGCAnywayAgain) { heap->entry_collect(_cause); } } }; // Utility to enter the safepoint for GC void EpsilonHeap::vmentry_collect(GCCause::Cause cause) { VM_EpsilonCollect vmop(cause); VMThread::execute(&vmop); } HeapWord* EpsilonHeap::allocate_or_collect_work(size_t size) { HeapWord* res = allocate_work(size); if (res == NULL && EpsilonWhyNotGCAnyway) { vmentry_collect(GCCause::_allocation_failure); res = allocate_work(size); } return res; } typedef Stack EpsilonMarkStack; void EpsilonHeap::process_all_roots(OopClosure* oops) { // Need to adapt passed closure for some root types CLDToOopClosure clds(oops, ClassLoaderData::_claim_none); MarkingCodeBlobClosure blobs(oops, CodeBlobToOopClosure::FixRelocations); // Need to tell runtime we are about to walk the roots with 1 thread StrongRootsScope scope(1); // Need locks to walk some roots MutexLockerEx lock_cc(CodeCache_lock, Mutex::_no_safepoint_check_flag); MutexLockerEx lock_cldg(ClassLoaderDataGraph_lock); // Walk all these different parts of runtime roots CodeCache::blobs_do(&blobs); ClassLoaderDataGraph::cld_do(&clds); Universe::oops_do(oops); Management::oops_do(oops); JvmtiExport::oops_do(oops); JNIHandles::oops_do(oops); WeakProcessor::oops_do(oops); ObjectSynchronizer::oops_do(oops); SystemDictionary::oops_do(oops); StringTable::oops_do(oops); Threads::possibly_parallel_oops_do(false, oops, &blobs); } // Walk the marking bitmap and call object closure on every marked object. void EpsilonHeap::walk_bitmap(ObjectClosure* cl) { HeapWord* limit = _space->top(); HeapWord* addr = _bitmap.get_next_marked_addr(_space->bottom(), limit); while (addr < limit) { oop obj = oop(addr); assert(_bitmap.is_marked(obj), "sanity"); cl->do_object(obj); addr += 1; if (addr < limit) { addr = _bitmap.get_next_marked_addr(addr, limit); } } } class EpsilonScanOopClosure : public BasicOopIterateClosure { private: EpsilonMarkStack* _stack; MarkBitMap* _bitmap; template void do_oop_work(T* p) { T o = RawAccess<>::oop_load(p); if (!CompressedOops::is_null(o)) { oop obj = CompressedOops::decode_not_null(o); if (_bitmap->par_mark(obj)) { _stack->push(obj); } } } public: EpsilonScanOopClosure(EpsilonMarkStack* stack, MarkBitMap* bitmap) : _stack(stack), _bitmap(bitmap) {} virtual void do_oop(oop* p) { do_oop_work(p); } virtual void do_oop(narrowOop* p) { do_oop_work(p); } }; class EpsilonCalcNewLocationObjectClosure : public ObjectClosure { private: PreservedMarks* _preserved; HeapWord* _compact_point; public: EpsilonCalcNewLocationObjectClosure(PreservedMarks* preserved, HeapWord *bottom) : _preserved(preserved), _compact_point(bottom) {} void do_object(oop obj) { if ((HeapWord*)obj != _compact_point) { markOop mark = obj->mark_raw(); if (mark->must_be_preserved(obj)) { _preserved->push(obj, mark); } obj->forward_to(oop(_compact_point)); } _compact_point += obj->size(); } HeapWord* compact_point() { return _compact_point; } }; class EpsilonAdjustPointersOopClosure : public BasicOopIterateClosure { private: template void do_oop_work(T* p) { T o = RawAccess<>::oop_load(p); if (!CompressedOops::is_null(o)) { oop obj = CompressedOops::decode_not_null(o); if (obj->is_forwarded()) { oop fwd = obj->forwardee(); RawAccess<>::oop_store(p, fwd); } } } public: EpsilonAdjustPointersOopClosure() {} virtual void do_oop(oop* p) { do_oop_work(p); } virtual void do_oop(narrowOop* p) { do_oop_work(p); } }; class EpsilonAdjustPointersObjectClosure : public ObjectClosure { public: void do_object(oop obj) { EpsilonAdjustPointersOopClosure cl; obj->oop_iterate(&cl); } }; class EpsilonMoveObjects : public ObjectClosure { public: void do_object(oop obj) { if (obj->is_forwarded()) { oop fwd = obj->forwardee(); Copy::aligned_conjoint_words((HeapWord*) obj, (HeapWord*) fwd, obj->size()); oop(fwd)->init_mark_raw(); } } }; void EpsilonHeap::entry_collect(GCCause::Cause cause) { GCIdMark mark; GCTraceTime(Info, gc) time("Lisp2-style Mark-Compact", NULL, cause, true); { GCTraceTime(Info, gc) time("Step 0: Prologue", NULL); // Strictly speaking, we do not need parsable heap for this algorithm, // but we want threads to give up their TLABs. ensure_parsability(true); // Tell various parts of runtime we are doing GC. CodeCache::gc_prologue(); BiasedLocking::preserve_marks(); DerivedPointerTable::clear(); DerivedPointerTable::set_active(false); } { GCTraceTime(Info, gc) time("Step 1: Clear bitmap", NULL); // Clear bitmap in preparation for marking. Do this in a separate step // to show the heap-size-dependent cost of bitmap manipulations. _bitmap.clear_range_large(_space->used_region()); } { GCTraceTime(Info, gc) time("Step 2: Mark", NULL); // Marking stack and the closure that does most of the work. // The closure would scan the outgoing references, mark them in bitmap, // and push newly-marked objects to stack for further processing. EpsilonMarkStack stack; EpsilonScanOopClosure cl(&stack, &_bitmap); // Seed the marking with roots. process_all_roots(&cl); // Scan the rest of the heap until we run out of objects. // Termination is guaranteed, because all reachable threads would // be marked eventually. while (!stack.is_empty()) { oop obj = stack.pop(); obj->oop_iterate(&cl); } } // We are going to store forwarding information (where the new copy resides) // in mark words. Some of those mark words need to be carefully preserved. // This is a utility that maintains the list of those special mark words. PreservedMarks preserved_marks; // New top of the allocated space. HeapWord* new_top; { GCTraceTime(Info, gc) time("Step 3: Calculate new locations", NULL); // Walk all alive objects, compute their new addresses and store those addresses // in mark words. Optionally preserve some marks. EpsilonCalcNewLocationObjectClosure cl(&preserved_marks, _space->bottom()); walk_bitmap(&cl); // After addresses are calculated, we know the new top for the allocated space. // We cannot set it just yet, because some asserts check that objects are "in heap" // based on current "top". new_top = cl.compact_point(); } { GCTraceTime(Info, gc) time("Step 4: Adjust pointers", NULL); // Walk all alive objects _and their reference fields_, and put "new addresses" // there. We know the new addresses from the forwarding data in mark words. // Take care of the heap objects first. EpsilonAdjustPointersObjectClosure cl; walk_bitmap(&cl); // Now do the same, but for all VM roots, which reference the objects on // their own: their references should also be updated. EpsilonAdjustPointersOopClosure cli; process_all_roots(&cli); // Finally, make sure preserved marks know the objects are about to move. preserved_marks.adjust_during_full_gc(); } { GCTraceTime(Info, gc) time("Step 5: Move objects", NULL); // Move all alive objects to their new locations. All the references are already // adjusted at previous step. EpsilonMoveObjects cl; walk_bitmap(&cl); // Now we moved all objects to their relevant locations, we can retract the "top" // of the allocation space to the end of the compacted prefix. _space->set_top(new_top); } { GCTraceTime(Info, gc) time("Step 6: Epilogue", NULL); // Restore all special mark words. preserved_marks.restore(); // Tell the rest of runtime we have finished the GC. DerivedPointerTable::update_pointers(); BiasedLocking::restore_marks(); CodeCache::gc_epilogue(); JvmtiExport::gc_epilogue(); } }