ysr@777: /* mikael@6198: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP stefank@2314: #define SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP stefank@2314: stefank@2314: #include "gc_implementation/g1/heapRegion.hpp" stefank@2314: #include "utilities/growableArray.hpp" stefank@2314: zgu@3900: class CollectionSetChooser: public CHeapObj { ysr@777: tonyp@3714: GrowableArray _regions; tonyp@3714: tonyp@3714: // Unfortunately, GrowableArray uses ints for length and indexes. To tonyp@3714: // avoid excessive casting in the rest of the class the following tonyp@3714: // wrapper methods are provided that use uints. tonyp@3714: tonyp@3714: uint regions_length() { return (uint) _regions.length(); } tonyp@3714: HeapRegion* regions_at(uint i) { return _regions.at((int) i); } tonyp@3714: void regions_at_put(uint i, HeapRegion* hr) { tonyp@3714: _regions.at_put((int) i, hr); tonyp@3714: } tonyp@3714: void regions_at_put_grow(uint i, HeapRegion* hr) { tonyp@3714: _regions.at_put_grow((int) i, hr); tonyp@3714: } tonyp@3714: void regions_trunc_to(uint i) { _regions.trunc_to((uint) i); } tonyp@3539: tonyp@3539: // The index of the next candidate old region to be considered for tonyp@3539: // addition to the CSet. tonyp@3714: uint _curr_index; tonyp@3539: tonyp@3539: // The number of candidate old regions added to the CSet chooser. johnc@4681: // Note: this is not updated when removing a region using johnc@4681: // remove_and_move_to_next() below. tonyp@3714: uint _length; tonyp@3539: tonyp@3714: // Keeps track of the start of the next array chunk to be claimed by tonyp@3714: // parallel GC workers. tonyp@3714: uint _first_par_unreserved_idx; ysr@777: tonyp@3539: // If a region has more live bytes than this threshold, it will not tonyp@3539: // be added to the CSet chooser and will not be a candidate for tonyp@3539: // collection. tonyp@3714: size_t _region_live_threshold_bytes; ysr@777: tonyp@3539: // The sum of reclaimable bytes over all the regions in the CSet chooser. tonyp@3714: size_t _remaining_reclaimable_bytes; ysr@777: ysr@777: public: ysr@777: tonyp@3539: // Return the current candidate region to be considered for tonyp@3539: // collection without removing it from the CSet chooser. tonyp@3539: HeapRegion* peek() { tonyp@3539: HeapRegion* res = NULL; tonyp@3539: if (_curr_index < _length) { tonyp@3714: res = regions_at(_curr_index); tonyp@3539: assert(res != NULL, tonyp@3714: err_msg("Unexpected NULL hr in _regions at index %u", tonyp@3539: _curr_index)); tonyp@3539: } tonyp@3539: return res; tonyp@3539: } tonyp@3539: tonyp@3539: // Remove the given region from the CSet chooser and move to the tonyp@3539: // next one. The given region should be the current candidate region tonyp@3539: // in the CSet chooser. tonyp@3539: void remove_and_move_to_next(HeapRegion* hr) { tonyp@3539: assert(hr != NULL, "pre-condition"); tonyp@3539: assert(_curr_index < _length, "pre-condition"); tonyp@3714: assert(regions_at(_curr_index) == hr, "pre-condition"); tonyp@3714: regions_at_put(_curr_index, NULL); tonyp@3714: assert(hr->reclaimable_bytes() <= _remaining_reclaimable_bytes, tonyp@3539: err_msg("remaining reclaimable bytes inconsistent " kevinw@9327: "from region: " SIZE_FORMAT " remaining: " SIZE_FORMAT, tonyp@3714: hr->reclaimable_bytes(), _remaining_reclaimable_bytes)); tonyp@3714: _remaining_reclaimable_bytes -= hr->reclaimable_bytes(); tonyp@3539: _curr_index += 1; tonyp@3539: } ysr@777: ysr@777: CollectionSetChooser(); ysr@777: tonyp@3714: void sort_regions(); tonyp@3539: tonyp@3539: // Determine whether to add the given region to the CSet chooser or tonyp@3539: // not. Currently, we skip humongous regions (we never add them to tonyp@3539: // the CSet, we only reclaim them during cleanup) and regions whose tonyp@3539: // live bytes are over the threshold. tonyp@3714: bool should_add(HeapRegion* hr) { tonyp@3539: assert(hr->is_marked(), "pre-condition"); tonyp@3539: assert(!hr->is_young(), "should never consider young regions"); tonyp@3539: return !hr->isHumongous() && tonyp@3714: hr->live_bytes() < _region_live_threshold_bytes; tonyp@3539: } tonyp@3539: johnc@4681: // Returns the number candidate old regions added johnc@4681: uint length() { return _length; } tonyp@3539: tonyp@3539: // Serial version. tonyp@3714: void add_region(HeapRegion *hr); ysr@777: tonyp@3714: // Must be called before calls to claim_array_chunk(). tonyp@3714: // n_regions is the number of regions, chunk_size the chunk size. tonyp@3714: void prepare_for_par_region_addition(uint n_regions, uint chunk_size); tonyp@3714: // Returns the first index in a contiguous chunk of chunk_size indexes ysr@777: // that the calling thread has reserved. These must be set by the tonyp@3714: // calling thread using set_region() (to NULL if necessary). tonyp@3714: uint claim_array_chunk(uint chunk_size); ysr@777: // Set the marked array entry at index to hr. Careful to claim the index ysr@777: // first if in parallel. tonyp@3714: void set_region(uint index, HeapRegion* hr); tonyp@3539: // Atomically increment the number of added regions by region_num tonyp@3539: // and the amount of reclaimable bytes by reclaimable_bytes. tonyp@3714: void update_totals(uint region_num, size_t reclaimable_bytes); ysr@777: tonyp@3714: void clear(); ysr@777: tonyp@3539: // Return the number of candidate regions that remain to be collected. tonyp@3714: uint remaining_regions() { return _length - _curr_index; } ysr@777: tonyp@3539: // Determine whether the CSet chooser has more candidate regions or not. tonyp@3714: bool is_empty() { return remaining_regions() == 0; } tonyp@3539: tonyp@3539: // Return the reclaimable bytes that remain to be collected on tonyp@3539: // all the candidate regions in the CSet chooser. tonyp@3714: size_t remaining_reclaimable_bytes() { return _remaining_reclaimable_bytes; } ysr@777: tonyp@3714: // Returns true if the used portion of "_regions" is properly ysr@777: // sorted, otherwise asserts false. tonyp@3714: void verify() PRODUCT_RETURN; ysr@777: }; stefank@2314: tonyp@3957: class CSetChooserParUpdater : public StackObj { tonyp@3957: private: tonyp@3957: CollectionSetChooser* _chooser; tonyp@3957: bool _parallel; tonyp@3957: uint _chunk_size; tonyp@3957: uint _cur_chunk_idx; tonyp@3957: uint _cur_chunk_end; tonyp@3957: uint _regions_added; tonyp@3957: size_t _reclaimable_bytes_added; tonyp@3957: tonyp@3957: public: tonyp@3957: CSetChooserParUpdater(CollectionSetChooser* chooser, tonyp@3957: bool parallel, uint chunk_size) : tonyp@3957: _chooser(chooser), _parallel(parallel), _chunk_size(chunk_size), tonyp@3957: _cur_chunk_idx(0), _cur_chunk_end(0), tonyp@3957: _regions_added(0), _reclaimable_bytes_added(0) { } tonyp@3957: tonyp@3957: ~CSetChooserParUpdater() { tonyp@3957: if (_parallel && _regions_added > 0) { tonyp@3957: _chooser->update_totals(_regions_added, _reclaimable_bytes_added); tonyp@3957: } tonyp@3957: } tonyp@3957: tonyp@3957: void add_region(HeapRegion* hr) { tonyp@3957: if (_parallel) { tonyp@3957: if (_cur_chunk_idx == _cur_chunk_end) { tonyp@3957: _cur_chunk_idx = _chooser->claim_array_chunk(_chunk_size); tonyp@3957: _cur_chunk_end = _cur_chunk_idx + _chunk_size; tonyp@3957: } tonyp@3957: assert(_cur_chunk_idx < _cur_chunk_end, "invariant"); tonyp@3957: _chooser->set_region(_cur_chunk_idx, hr); tonyp@3957: _cur_chunk_idx += 1; tonyp@3957: } else { tonyp@3957: _chooser->add_region(hr); tonyp@3957: } tonyp@3957: _regions_added += 1; tonyp@3957: _reclaimable_bytes_added += hr->reclaimable_bytes(); tonyp@3957: } tonyp@3957: tonyp@3957: bool should_add(HeapRegion* hr) { return _chooser->should_add(hr); } tonyp@3957: }; tonyp@3957: stefank@2314: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP tonyp@3957: