Path: blob/master/src/hotspot/share/gc/shenandoah/shenandoahCollectionSet.cpp
40957 views
/*1* Copyright (c) 2016, 2019, Red Hat, Inc. 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"2526#include "gc/shenandoah/shenandoahCollectionSet.hpp"27#include "gc/shenandoah/shenandoahHeap.inline.hpp"28#include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"29#include "gc/shenandoah/shenandoahHeapRegionSet.hpp"30#include "gc/shenandoah/shenandoahUtils.hpp"31#include "runtime/atomic.hpp"32#include "services/memTracker.hpp"33#include "utilities/copy.hpp"3435ShenandoahCollectionSet::ShenandoahCollectionSet(ShenandoahHeap* heap, ReservedSpace space, char* heap_base) :36_map_size(heap->num_regions()),37_region_size_bytes_shift(ShenandoahHeapRegion::region_size_bytes_shift()),38_map_space(space),39_cset_map(_map_space.base() + ((uintx)heap_base >> _region_size_bytes_shift)),40_biased_cset_map(_map_space.base()),41_heap(heap),42_garbage(0),43_used(0),44_region_count(0),45_current_index(0) {4647// The collection set map is reserved to cover the entire heap *and* zero addresses.48// This is needed to accept in-cset checks for both heap oops and NULLs, freeing49// high-performance code from checking for NULL first.50//51// Since heap_base can be far away, committing the entire map would waste memory.52// Therefore, we only commit the parts that are needed to operate: the heap view,53// and the zero page.54//55// Note: we could instead commit the entire map, and piggyback on OS virtual memory56// subsystem for mapping not-yet-written-to pages to a single physical backing page,57// but this is not guaranteed, and would confuse NMT and other memory accounting tools.5859MemTracker::record_virtual_memory_type(_map_space.base(), mtGC);6061size_t page_size = (size_t)os::vm_page_size();6263if (!_map_space.special()) {64// Commit entire pages that cover the heap cset map.65char* bot_addr = align_down(_cset_map, page_size);66char* top_addr = align_up(_cset_map + _map_size, page_size);67os::commit_memory_or_exit(bot_addr, pointer_delta(top_addr, bot_addr, 1), false,68"Unable to commit collection set bitmap: heap");6970// Commit the zero page, if not yet covered by heap cset map.71if (bot_addr != _biased_cset_map) {72os::commit_memory_or_exit(_biased_cset_map, page_size, false,73"Unable to commit collection set bitmap: zero page");74}75}7677Copy::zero_to_bytes(_cset_map, _map_size);78Copy::zero_to_bytes(_biased_cset_map, page_size);79}8081void ShenandoahCollectionSet::add_region(ShenandoahHeapRegion* r) {82assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");83assert(Thread::current()->is_VM_thread(), "Must be VMThread");84assert(!is_in(r), "Already in collection set");85_cset_map[r->index()] = 1;86_region_count++;87_garbage += r->garbage();88_used += r->used();8990// Update the region status too. State transition would be checked internally.91r->make_cset();92}9394void ShenandoahCollectionSet::clear() {95assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");96Copy::zero_to_bytes(_cset_map, _map_size);9798#ifdef ASSERT99for (size_t index = 0; index < _heap->num_regions(); index ++) {100assert (!_heap->get_region(index)->is_cset(), "should have been cleared before");101}102#endif103104_garbage = 0;105_used = 0;106107_region_count = 0;108_current_index = 0;109}110111ShenandoahHeapRegion* ShenandoahCollectionSet::claim_next() {112// This code is optimized for the case when collection set contains only113// a few regions. In this case, it is more constructive to check for is_in114// before hitting the (potentially contended) atomic index.115116size_t max = _heap->num_regions();117size_t old = Atomic::load(&_current_index);118119for (size_t index = old; index < max; index++) {120if (is_in(index)) {121size_t cur = Atomic::cmpxchg(&_current_index, old, index + 1, memory_order_relaxed);122assert(cur >= old, "Always move forward");123if (cur == old) {124// Successfully moved the claim index, this is our region.125return _heap->get_region(index);126} else {127// Somebody else moved the claim index, restart from there.128index = cur - 1; // adjust for loop post-increment129old = cur;130}131}132}133return NULL;134}135136ShenandoahHeapRegion* ShenandoahCollectionSet::next() {137assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");138assert(Thread::current()->is_VM_thread(), "Must be VMThread");139140size_t max = _heap->num_regions();141for (size_t index = _current_index; index < max; index++) {142if (is_in(index)) {143_current_index = 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