src/share/vm/gc_implementation/g1/collectionSetChooser.hpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 6876
710a3c8b516e
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP
aoqi@0 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP
aoqi@0 27
aoqi@0 28 #include "gc_implementation/g1/heapRegion.hpp"
aoqi@0 29 #include "utilities/growableArray.hpp"
aoqi@0 30
aoqi@0 31 class CollectionSetChooser: public CHeapObj<mtGC> {
aoqi@0 32
aoqi@0 33 GrowableArray<HeapRegion*> _regions;
aoqi@0 34
aoqi@0 35 // Unfortunately, GrowableArray uses ints for length and indexes. To
aoqi@0 36 // avoid excessive casting in the rest of the class the following
aoqi@0 37 // wrapper methods are provided that use uints.
aoqi@0 38
aoqi@0 39 uint regions_length() { return (uint) _regions.length(); }
aoqi@0 40 HeapRegion* regions_at(uint i) { return _regions.at((int) i); }
aoqi@0 41 void regions_at_put(uint i, HeapRegion* hr) {
aoqi@0 42 _regions.at_put((int) i, hr);
aoqi@0 43 }
aoqi@0 44 void regions_at_put_grow(uint i, HeapRegion* hr) {
aoqi@0 45 _regions.at_put_grow((int) i, hr);
aoqi@0 46 }
aoqi@0 47 void regions_trunc_to(uint i) { _regions.trunc_to((uint) i); }
aoqi@0 48
aoqi@0 49 // The index of the next candidate old region to be considered for
aoqi@0 50 // addition to the CSet.
aoqi@0 51 uint _curr_index;
aoqi@0 52
aoqi@0 53 // The number of candidate old regions added to the CSet chooser.
aoqi@0 54 // Note: this is not updated when removing a region using
aoqi@0 55 // remove_and_move_to_next() below.
aoqi@0 56 uint _length;
aoqi@0 57
aoqi@0 58 // Keeps track of the start of the next array chunk to be claimed by
aoqi@0 59 // parallel GC workers.
aoqi@0 60 uint _first_par_unreserved_idx;
aoqi@0 61
aoqi@0 62 // If a region has more live bytes than this threshold, it will not
aoqi@0 63 // be added to the CSet chooser and will not be a candidate for
aoqi@0 64 // collection.
aoqi@0 65 size_t _region_live_threshold_bytes;
aoqi@0 66
aoqi@0 67 // The sum of reclaimable bytes over all the regions in the CSet chooser.
aoqi@0 68 size_t _remaining_reclaimable_bytes;
aoqi@0 69
aoqi@0 70 public:
aoqi@0 71
aoqi@0 72 // Return the current candidate region to be considered for
aoqi@0 73 // collection without removing it from the CSet chooser.
aoqi@0 74 HeapRegion* peek() {
aoqi@0 75 HeapRegion* res = NULL;
aoqi@0 76 if (_curr_index < _length) {
aoqi@0 77 res = regions_at(_curr_index);
aoqi@0 78 assert(res != NULL,
aoqi@0 79 err_msg("Unexpected NULL hr in _regions at index %u",
aoqi@0 80 _curr_index));
aoqi@0 81 }
aoqi@0 82 return res;
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 // Remove the given region from the CSet chooser and move to the
aoqi@0 86 // next one. The given region should be the current candidate region
aoqi@0 87 // in the CSet chooser.
aoqi@0 88 void remove_and_move_to_next(HeapRegion* hr) {
aoqi@0 89 assert(hr != NULL, "pre-condition");
aoqi@0 90 assert(_curr_index < _length, "pre-condition");
aoqi@0 91 assert(regions_at(_curr_index) == hr, "pre-condition");
aoqi@0 92 regions_at_put(_curr_index, NULL);
aoqi@0 93 assert(hr->reclaimable_bytes() <= _remaining_reclaimable_bytes,
aoqi@0 94 err_msg("remaining reclaimable bytes inconsistent "
aoqi@0 95 "from region: "SIZE_FORMAT" remaining: "SIZE_FORMAT,
aoqi@0 96 hr->reclaimable_bytes(), _remaining_reclaimable_bytes));
aoqi@0 97 _remaining_reclaimable_bytes -= hr->reclaimable_bytes();
aoqi@0 98 _curr_index += 1;
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 CollectionSetChooser();
aoqi@0 102
aoqi@0 103 void sort_regions();
aoqi@0 104
aoqi@0 105 // Determine whether to add the given region to the CSet chooser or
aoqi@0 106 // not. Currently, we skip humongous regions (we never add them to
aoqi@0 107 // the CSet, we only reclaim them during cleanup) and regions whose
aoqi@0 108 // live bytes are over the threshold.
aoqi@0 109 bool should_add(HeapRegion* hr) {
aoqi@0 110 assert(hr->is_marked(), "pre-condition");
aoqi@0 111 assert(!hr->is_young(), "should never consider young regions");
aoqi@0 112 return !hr->isHumongous() &&
aoqi@0 113 hr->live_bytes() < _region_live_threshold_bytes;
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 // Returns the number candidate old regions added
aoqi@0 117 uint length() { return _length; }
aoqi@0 118
aoqi@0 119 // Serial version.
aoqi@0 120 void add_region(HeapRegion *hr);
aoqi@0 121
aoqi@0 122 // Must be called before calls to claim_array_chunk().
aoqi@0 123 // n_regions is the number of regions, chunk_size the chunk size.
aoqi@0 124 void prepare_for_par_region_addition(uint n_regions, uint chunk_size);
aoqi@0 125 // Returns the first index in a contiguous chunk of chunk_size indexes
aoqi@0 126 // that the calling thread has reserved. These must be set by the
aoqi@0 127 // calling thread using set_region() (to NULL if necessary).
aoqi@0 128 uint claim_array_chunk(uint chunk_size);
aoqi@0 129 // Set the marked array entry at index to hr. Careful to claim the index
aoqi@0 130 // first if in parallel.
aoqi@0 131 void set_region(uint index, HeapRegion* hr);
aoqi@0 132 // Atomically increment the number of added regions by region_num
aoqi@0 133 // and the amount of reclaimable bytes by reclaimable_bytes.
aoqi@0 134 void update_totals(uint region_num, size_t reclaimable_bytes);
aoqi@0 135
aoqi@0 136 void clear();
aoqi@0 137
aoqi@0 138 // Return the number of candidate regions that remain to be collected.
aoqi@0 139 uint remaining_regions() { return _length - _curr_index; }
aoqi@0 140
aoqi@0 141 // Determine whether the CSet chooser has more candidate regions or not.
aoqi@0 142 bool is_empty() { return remaining_regions() == 0; }
aoqi@0 143
aoqi@0 144 // Return the reclaimable bytes that remain to be collected on
aoqi@0 145 // all the candidate regions in the CSet chooser.
aoqi@0 146 size_t remaining_reclaimable_bytes() { return _remaining_reclaimable_bytes; }
aoqi@0 147
aoqi@0 148 // Returns true if the used portion of "_regions" is properly
aoqi@0 149 // sorted, otherwise asserts false.
aoqi@0 150 void verify() PRODUCT_RETURN;
aoqi@0 151 };
aoqi@0 152
aoqi@0 153 class CSetChooserParUpdater : public StackObj {
aoqi@0 154 private:
aoqi@0 155 CollectionSetChooser* _chooser;
aoqi@0 156 bool _parallel;
aoqi@0 157 uint _chunk_size;
aoqi@0 158 uint _cur_chunk_idx;
aoqi@0 159 uint _cur_chunk_end;
aoqi@0 160 uint _regions_added;
aoqi@0 161 size_t _reclaimable_bytes_added;
aoqi@0 162
aoqi@0 163 public:
aoqi@0 164 CSetChooserParUpdater(CollectionSetChooser* chooser,
aoqi@0 165 bool parallel, uint chunk_size) :
aoqi@0 166 _chooser(chooser), _parallel(parallel), _chunk_size(chunk_size),
aoqi@0 167 _cur_chunk_idx(0), _cur_chunk_end(0),
aoqi@0 168 _regions_added(0), _reclaimable_bytes_added(0) { }
aoqi@0 169
aoqi@0 170 ~CSetChooserParUpdater() {
aoqi@0 171 if (_parallel && _regions_added > 0) {
aoqi@0 172 _chooser->update_totals(_regions_added, _reclaimable_bytes_added);
aoqi@0 173 }
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 void add_region(HeapRegion* hr) {
aoqi@0 177 if (_parallel) {
aoqi@0 178 if (_cur_chunk_idx == _cur_chunk_end) {
aoqi@0 179 _cur_chunk_idx = _chooser->claim_array_chunk(_chunk_size);
aoqi@0 180 _cur_chunk_end = _cur_chunk_idx + _chunk_size;
aoqi@0 181 }
aoqi@0 182 assert(_cur_chunk_idx < _cur_chunk_end, "invariant");
aoqi@0 183 _chooser->set_region(_cur_chunk_idx, hr);
aoqi@0 184 _cur_chunk_idx += 1;
aoqi@0 185 } else {
aoqi@0 186 _chooser->add_region(hr);
aoqi@0 187 }
aoqi@0 188 _regions_added += 1;
aoqi@0 189 _reclaimable_bytes_added += hr->reclaimable_bytes();
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 bool should_add(HeapRegion* hr) { return _chooser->should_add(hr); }
aoqi@0 193 };
aoqi@0 194
aoqi@0 195 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP
aoqi@0 196

mercurial