Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/shenandoah/shenandoahCollectionSet.cpp
38921 views
/*1* Copyright (c) 2016, 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223#include "precompiled.hpp"2425#include "gc_implementation/shenandoah/shenandoahCollectionSet.hpp"26#include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp"27#include "gc_implementation/shenandoah/shenandoahHeapRegion.inline.hpp"28#include "gc_implementation/shenandoah/shenandoahHeapRegionSet.hpp"29#include "gc_implementation/shenandoah/shenandoahUtils.hpp"30#include "runtime/atomic.hpp"31#include "services/memTracker.hpp"32#include "utilities/copy.hpp"3334ShenandoahCollectionSet::ShenandoahCollectionSet(ShenandoahHeap* heap, ReservedSpace space, char* heap_base) :35_map_size(heap->num_regions()),36_region_size_bytes_shift(ShenandoahHeapRegion::region_size_bytes_shift()),37_map_space(space),38_cset_map(_map_space.base() + ((uintx)heap_base >> _region_size_bytes_shift)),39_biased_cset_map(_map_space.base()),40_heap(heap),41_garbage(0),42_used(0),43_region_count(0),44_current_index(0) {4546// The collection set map is reserved to cover the entire heap *and* zero addresses.47// This is needed to accept in-cset checks for both heap oops and NULLs, freeing48// high-performance code from checking for NULL first.49//50// Since heap_base can be far away, committing the entire map would waste memory.51// Therefore, we only commit the parts that are needed to operate: the heap view,52// and the zero page.53//54// Note: we could instead commit the entire map, and piggyback on OS virtual memory55// subsystem for mapping not-yet-written-to pages to a single physical backing page,56// but this is not guaranteed, and would confuse NMT and other memory accounting tools.5758MemTracker::record_virtual_memory_type(_map_space.base(), mtGC);5960size_t page_size = (size_t)os::vm_page_size();6162if (!_map_space.special()) {63// Commit entire pages that cover the heap cset map.64char* bot_addr = (char*)align_ptr_down(_cset_map, page_size);65char* top_addr = (char*)align_ptr_up(_cset_map + _map_size, page_size);66os::commit_memory_or_exit(bot_addr, pointer_delta(top_addr, bot_addr, 1), false,67"Unable to commit collection set bitmap: heap");6869// Commit the zero page, if not yet covered by heap cset map.70if (bot_addr != _biased_cset_map) {71os::commit_memory_or_exit(_biased_cset_map, page_size, false,72"Unable to commit collection set bitmap: zero page");73}74}7576Copy::zero_to_bytes(_cset_map, _map_size);77Copy::zero_to_bytes(_biased_cset_map, page_size);78}7980void ShenandoahCollectionSet::add_region(ShenandoahHeapRegion* r) {81assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");82assert(Thread::current()->is_VM_thread(), "Must be VMThread");83assert(!is_in(r), "Already in collection set");84_cset_map[r->index()] = 1;85_region_count++;86_garbage += r->garbage();87_used += r->used();8889// Update the region status too. State transition would be checked internally.90r->make_cset();91}9293void ShenandoahCollectionSet::clear() {94assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");95Copy::zero_to_bytes(_cset_map, _map_size);9697#ifdef ASSERT98for (size_t index = 0; index < _heap->num_regions(); index ++) {99assert (!_heap->get_region(index)->is_cset(), "should have been cleared before");100}101#endif102103_garbage = 0;104_used = 0;105106_region_count = 0;107_current_index = 0;108}109110ShenandoahHeapRegion* ShenandoahCollectionSet::claim_next() {111size_t num_regions = _heap->num_regions();112if (_current_index >= (jint)num_regions) {113return NULL;114}115116jint saved_current = _current_index;117size_t index = (size_t)saved_current;118119while(index < num_regions) {120if (is_in(index)) {121jint cur = Atomic::cmpxchg((jint)(index + 1), &_current_index, saved_current);122assert(cur >= (jint)saved_current, "Must move forward");123if (cur == saved_current) {124assert(is_in(index), "Invariant");125return _heap->get_region(index);126} else {127index = (size_t)cur;128saved_current = cur;129}130} else {131index ++;132}133}134return NULL;135}136137ShenandoahHeapRegion* ShenandoahCollectionSet::next() {138assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");139assert(Thread::current()->is_VM_thread(), "Must be VMThread");140size_t num_regions = _heap->num_regions();141for (size_t index = (size_t)_current_index; index < num_regions; index ++) {142if (is_in(index)) {143_current_index = (jint)(index + 1);144return _heap->get_region(index);145}146}147148return NULL;149}150151void ShenandoahCollectionSet::print_on(outputStream* out) const {152out->print_cr("Collection Set : " SIZE_FORMAT "", count());153154debug_only(size_t regions = 0;)155for (size_t index = 0; index < _heap->num_regions(); index ++) {156if (is_in(index)) {157_heap->get_region(index)->print_on(out);158debug_only(regions ++;)159}160}161assert(regions == count(), "Must match");162}163164165