Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/collectionSetChooser.hpp
38920 views
/*1* Copyright (c) 2001, 2013, 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#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP25#define SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP2627#include "gc_implementation/g1/heapRegion.hpp"28#include "utilities/growableArray.hpp"2930class CollectionSetChooser: public CHeapObj<mtGC> {3132GrowableArray<HeapRegion*> _regions;3334// Unfortunately, GrowableArray uses ints for length and indexes. To35// avoid excessive casting in the rest of the class the following36// wrapper methods are provided that use uints.3738uint regions_length() { return (uint) _regions.length(); }39HeapRegion* regions_at(uint i) { return _regions.at((int) i); }40void regions_at_put(uint i, HeapRegion* hr) {41_regions.at_put((int) i, hr);42}43void regions_at_put_grow(uint i, HeapRegion* hr) {44_regions.at_put_grow((int) i, hr);45}46void regions_trunc_to(uint i) { _regions.trunc_to((uint) i); }4748// The index of the next candidate old region to be considered for49// addition to the CSet.50uint _curr_index;5152// The number of candidate old regions added to the CSet chooser.53// Note: this is not updated when removing a region using54// remove_and_move_to_next() below.55uint _length;5657// Keeps track of the start of the next array chunk to be claimed by58// parallel GC workers.59uint _first_par_unreserved_idx;6061// If a region has more live bytes than this threshold, it will not62// be added to the CSet chooser and will not be a candidate for63// collection.64size_t _region_live_threshold_bytes;6566// The sum of reclaimable bytes over all the regions in the CSet chooser.67size_t _remaining_reclaimable_bytes;6869public:7071// Return the current candidate region to be considered for72// collection without removing it from the CSet chooser.73HeapRegion* peek() {74HeapRegion* res = NULL;75if (_curr_index < _length) {76res = regions_at(_curr_index);77assert(res != NULL,78err_msg("Unexpected NULL hr in _regions at index %u",79_curr_index));80}81return res;82}8384// Remove the given region from the CSet chooser and move to the85// next one. The given region should be the current candidate region86// in the CSet chooser.87void remove_and_move_to_next(HeapRegion* hr) {88assert(hr != NULL, "pre-condition");89assert(_curr_index < _length, "pre-condition");90assert(regions_at(_curr_index) == hr, "pre-condition");91regions_at_put(_curr_index, NULL);92assert(hr->reclaimable_bytes() <= _remaining_reclaimable_bytes,93err_msg("remaining reclaimable bytes inconsistent "94"from region: " SIZE_FORMAT " remaining: " SIZE_FORMAT,95hr->reclaimable_bytes(), _remaining_reclaimable_bytes));96_remaining_reclaimable_bytes -= hr->reclaimable_bytes();97_curr_index += 1;98}99100CollectionSetChooser();101102void sort_regions();103104// Determine whether to add the given region to the CSet chooser or105// not. Currently, we skip humongous regions (we never add them to106// the CSet, we only reclaim them during cleanup) and regions whose107// live bytes are over the threshold.108bool should_add(HeapRegion* hr) {109assert(hr->is_marked(), "pre-condition");110assert(!hr->is_young(), "should never consider young regions");111return !hr->isHumongous() &&112hr->live_bytes() < _region_live_threshold_bytes;113}114115// Returns the number candidate old regions added116uint length() { return _length; }117118// Serial version.119void add_region(HeapRegion *hr);120121// Must be called before calls to claim_array_chunk().122// n_regions is the number of regions, chunk_size the chunk size.123void prepare_for_par_region_addition(uint n_regions, uint chunk_size);124// Returns the first index in a contiguous chunk of chunk_size indexes125// that the calling thread has reserved. These must be set by the126// calling thread using set_region() (to NULL if necessary).127uint claim_array_chunk(uint chunk_size);128// Set the marked array entry at index to hr. Careful to claim the index129// first if in parallel.130void set_region(uint index, HeapRegion* hr);131// Atomically increment the number of added regions by region_num132// and the amount of reclaimable bytes by reclaimable_bytes.133void update_totals(uint region_num, size_t reclaimable_bytes);134135void clear();136137// Return the number of candidate regions that remain to be collected.138uint remaining_regions() { return _length - _curr_index; }139140// Determine whether the CSet chooser has more candidate regions or not.141bool is_empty() { return remaining_regions() == 0; }142143// Return the reclaimable bytes that remain to be collected on144// all the candidate regions in the CSet chooser.145size_t remaining_reclaimable_bytes() { return _remaining_reclaimable_bytes; }146147// Returns true if the used portion of "_regions" is properly148// sorted, otherwise asserts false.149void verify() PRODUCT_RETURN;150};151152class CSetChooserParUpdater : public StackObj {153private:154CollectionSetChooser* _chooser;155bool _parallel;156uint _chunk_size;157uint _cur_chunk_idx;158uint _cur_chunk_end;159uint _regions_added;160size_t _reclaimable_bytes_added;161162public:163CSetChooserParUpdater(CollectionSetChooser* chooser,164bool parallel, uint chunk_size) :165_chooser(chooser), _parallel(parallel), _chunk_size(chunk_size),166_cur_chunk_idx(0), _cur_chunk_end(0),167_regions_added(0), _reclaimable_bytes_added(0) { }168169~CSetChooserParUpdater() {170if (_parallel && _regions_added > 0) {171_chooser->update_totals(_regions_added, _reclaimable_bytes_added);172}173}174175void add_region(HeapRegion* hr) {176if (_parallel) {177if (_cur_chunk_idx == _cur_chunk_end) {178_cur_chunk_idx = _chooser->claim_array_chunk(_chunk_size);179_cur_chunk_end = _cur_chunk_idx + _chunk_size;180}181assert(_cur_chunk_idx < _cur_chunk_end, "invariant");182_chooser->set_region(_cur_chunk_idx, hr);183_cur_chunk_idx += 1;184} else {185_chooser->add_region(hr);186}187_regions_added += 1;188_reclaimable_bytes_added += hr->reclaimable_bytes();189}190191bool should_add(HeapRegion* hr) { return _chooser->should_add(hr); }192};193194#endif // SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP195196197198