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