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

Tue, 25 Sep 2012 07:05:55 -0700

author
jmasa
date
Tue, 25 Sep 2012 07:05:55 -0700
changeset 4097
5baec2e69518
parent 3714
f7a8920427a6
child 4385
37f7535e5f18
permissions
-rw-r--r--

7200615: NPG: optimized VM build is broken
Reviewed-by: kvn

ysr@777 1 /*
tonyp@3539 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
ysr@777 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ysr@777 4 *
ysr@777 5 * This code is free software; you can redistribute it and/or modify it
ysr@777 6 * under the terms of the GNU General Public License version 2 only, as
ysr@777 7 * published by the Free Software Foundation.
ysr@777 8 *
ysr@777 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ysr@777 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ysr@777 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ysr@777 12 * version 2 for more details (a copy is included in the LICENSE file that
ysr@777 13 * accompanied this code).
ysr@777 14 *
ysr@777 15 * You should have received a copy of the GNU General Public License version
ysr@777 16 * 2 along with this work; if not, write to the Free Software Foundation,
ysr@777 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ysr@777 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
ysr@777 22 *
ysr@777 23 */
ysr@777 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "gc_implementation/g1/collectionSetChooser.hpp"
stefank@2314 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
stefank@2314 28 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
tonyp@3114 29 #include "gc_implementation/g1/g1ErgoVerbose.hpp"
stefank@2314 30 #include "memory/space.inline.hpp"
ysr@777 31
tonyp@3539 32 // Even though we don't use the GC efficiency in our heuristics as
tonyp@3539 33 // much as we used to, we still order according to GC efficiency. This
tonyp@3539 34 // will cause regions with a lot of live objects and large RSets to
tonyp@3539 35 // end up at the end of the array. Given that we might skip collecting
tonyp@3539 36 // the last few old regions, if after a few mixed GCs the remaining
tonyp@3539 37 // have reclaimable bytes under a certain threshold, the hope is that
tonyp@3539 38 // the ones we'll skip are ones with both large RSets and a lot of
tonyp@3539 39 // live objects, not the ones with just a lot of live objects if we
tonyp@3539 40 // ordered according to the amount of reclaimable bytes per region.
tonyp@3714 41 static int order_regions(HeapRegion* hr1, HeapRegion* hr2) {
ysr@777 42 if (hr1 == NULL) {
tonyp@3539 43 if (hr2 == NULL) {
tonyp@3539 44 return 0;
tonyp@3539 45 } else {
tonyp@3539 46 return 1;
tonyp@3539 47 }
ysr@777 48 } else if (hr2 == NULL) {
ysr@777 49 return -1;
ysr@777 50 }
tonyp@3539 51
tonyp@3539 52 double gc_eff1 = hr1->gc_efficiency();
tonyp@3539 53 double gc_eff2 = hr2->gc_efficiency();
tonyp@3539 54 if (gc_eff1 > gc_eff2) {
tonyp@3539 55 return -1;
tonyp@3539 56 } if (gc_eff1 < gc_eff2) {
tonyp@3539 57 return 1;
tonyp@3539 58 } else {
tonyp@3539 59 return 0;
tonyp@3539 60 }
ysr@777 61 }
ysr@777 62
tonyp@3714 63 static int order_regions(HeapRegion** hr1p, HeapRegion** hr2p) {
tonyp@3714 64 return order_regions(*hr1p, *hr2p);
ysr@777 65 }
ysr@777 66
ysr@777 67 CollectionSetChooser::CollectionSetChooser() :
ysr@777 68 // The line below is the worst bit of C++ hackery I've ever written
ysr@777 69 // (Detlefs, 11/23). You should think of it as equivalent to
ysr@777 70 // "_regions(100, true)": initialize the growable array and inform it
kvn@2043 71 // that it should allocate its elem array(s) on the C heap.
kvn@2043 72 //
kvn@2043 73 // The first argument, however, is actually a comma expression
kvn@2043 74 // (set_allocation_type(this, C_HEAP), 100). The purpose of the
kvn@2043 75 // set_allocation_type() call is to replace the default allocation
kvn@2043 76 // type for embedded objects STACK_OR_EMBEDDED with C_HEAP. It will
kvn@2043 77 // allow to pass the assert in GenericGrowableArray() which checks
kvn@2043 78 // that a growable array object must be on C heap if elements are.
kvn@2043 79 //
kvn@2043 80 // Note: containing object is allocated on C heap since it is CHeapObj.
kvn@2043 81 //
tonyp@3714 82 _regions((ResourceObj::set_allocation_type((address) &_regions,
ysr@777 83 ResourceObj::C_HEAP),
tonyp@3539 84 100), true /* C_Heap */),
tonyp@3714 85 _curr_index(0), _length(0), _first_par_unreserved_idx(0),
tonyp@3714 86 _region_live_threshold_bytes(0), _remaining_reclaimable_bytes(0) {
tonyp@3714 87 _region_live_threshold_bytes =
tonyp@3539 88 HeapRegion::GrainBytes * (size_t) G1OldCSetRegionLiveThresholdPercent / 100;
tonyp@3539 89 }
ysr@777 90
ysr@777 91 #ifndef PRODUCT
tonyp@3714 92 void CollectionSetChooser::verify() {
tonyp@3714 93 guarantee(_length <= regions_length(),
tonyp@3714 94 err_msg("_length: %u regions length: %u", _length, regions_length()));
tonyp@3714 95 guarantee(_curr_index <= _length,
tonyp@3714 96 err_msg("_curr_index: %u _length: %u", _curr_index, _length));
tonyp@3714 97 uint index = 0;
tonyp@3539 98 size_t sum_of_reclaimable_bytes = 0;
tonyp@3539 99 while (index < _curr_index) {
tonyp@3714 100 guarantee(regions_at(index) == NULL,
tonyp@3539 101 "all entries before _curr_index should be NULL");
tonyp@3539 102 index += 1;
ysr@777 103 }
ysr@777 104 HeapRegion *prev = NULL;
tonyp@3539 105 while (index < _length) {
tonyp@3714 106 HeapRegion *curr = regions_at(index++);
tonyp@3714 107 guarantee(curr != NULL, "Regions in _regions array cannot be NULL");
ysr@3185 108 guarantee(!curr->is_young(), "should not be young!");
tonyp@3539 109 guarantee(!curr->isHumongous(), "should not be humongous!");
ysr@3185 110 if (prev != NULL) {
tonyp@3714 111 guarantee(order_regions(prev, curr) != 1,
tonyp@3539 112 err_msg("GC eff prev: %1.4f GC eff curr: %1.4f",
tonyp@3539 113 prev->gc_efficiency(), curr->gc_efficiency()));
ysr@777 114 }
tonyp@3539 115 sum_of_reclaimable_bytes += curr->reclaimable_bytes();
ysr@3185 116 prev = curr;
ysr@777 117 }
tonyp@3714 118 guarantee(sum_of_reclaimable_bytes == _remaining_reclaimable_bytes,
tonyp@3539 119 err_msg("reclaimable bytes inconsistent, "
tonyp@3539 120 "remaining: "SIZE_FORMAT" sum: "SIZE_FORMAT,
tonyp@3714 121 _remaining_reclaimable_bytes, sum_of_reclaimable_bytes));
ysr@777 122 }
tonyp@3714 123 #endif // !PRODUCT
ysr@777 124
tonyp@3714 125 void CollectionSetChooser::sort_regions() {
ysr@777 126 // First trim any unused portion of the top in the parallel case.
ysr@777 127 if (_first_par_unreserved_idx > 0) {
tonyp@3714 128 assert(_first_par_unreserved_idx <= regions_length(),
ysr@777 129 "Or we didn't reserved enough length");
tonyp@3714 130 regions_trunc_to(_first_par_unreserved_idx);
ysr@777 131 }
tonyp@3714 132 _regions.sort(order_regions);
tonyp@3714 133 assert(_length <= regions_length(), "Requirement");
tonyp@3714 134 #ifdef ASSERT
tonyp@3714 135 for (uint i = 0; i < _length; i++) {
tonyp@3714 136 assert(regions_at(i) != NULL, "Should be true by sorting!");
ysr@777 137 }
tonyp@3714 138 #endif // ASSERT
tonyp@2717 139 if (G1PrintRegionLivenessInfo) {
tonyp@2717 140 G1PrintRegionLivenessInfoClosure cl(gclog_or_tty, "Post-Sorting");
tonyp@3714 141 for (uint i = 0; i < _length; ++i) {
tonyp@3714 142 HeapRegion* r = regions_at(i);
tonyp@2717 143 cl.doHeapRegion(r);
ysr@777 144 }
ysr@777 145 }
tonyp@3714 146 verify();
ysr@777 147 }
ysr@777 148
tonyp@3714 149 uint CollectionSetChooser::calc_min_old_cset_length() {
tonyp@3539 150 // The min old CSet region bound is based on the maximum desired
tonyp@3539 151 // number of mixed GCs after a cycle. I.e., even if some old regions
tonyp@3539 152 // look expensive, we should add them to the CSet anyway to make
tonyp@3539 153 // sure we go through the available old regions in no more than the
tonyp@3539 154 // maximum desired number of mixed GCs.
tonyp@3539 155 //
tonyp@3539 156 // The calculation is based on the number of marked regions we added
tonyp@3539 157 // to the CSet chooser in the first place, not how many remain, so
tonyp@3539 158 // that the result is the same during all mixed GCs that follow a cycle.
tonyp@3539 159
tonyp@3539 160 const size_t region_num = (size_t) _length;
tonyp@3667 161 const size_t gc_num = (size_t) G1MixedGCCountTarget;
tonyp@3539 162 size_t result = region_num / gc_num;
tonyp@3539 163 // emulate ceiling
tonyp@3539 164 if (result * gc_num < region_num) {
tonyp@3539 165 result += 1;
tonyp@3539 166 }
tonyp@3713 167 return (uint) result;
tonyp@3539 168 }
tonyp@3539 169
tonyp@3714 170 uint CollectionSetChooser::calc_max_old_cset_length() {
tonyp@3539 171 // The max old CSet region bound is based on the threshold expressed
tonyp@3539 172 // as a percentage of the heap size. I.e., it should bound the
tonyp@3539 173 // number of old regions added to the CSet irrespective of how many
tonyp@3539 174 // of them are available.
tonyp@3539 175
tonyp@3539 176 G1CollectedHeap* g1h = G1CollectedHeap::heap();
tonyp@3539 177 const size_t region_num = g1h->n_regions();
tonyp@3539 178 const size_t perc = (size_t) G1OldCSetRegionThresholdPercent;
tonyp@3539 179 size_t result = region_num * perc / 100;
tonyp@3539 180 // emulate ceiling
tonyp@3539 181 if (100 * result < region_num * perc) {
tonyp@3539 182 result += 1;
tonyp@3539 183 }
tonyp@3713 184 return (uint) result;
tonyp@3539 185 }
tonyp@3539 186
tonyp@3714 187 void CollectionSetChooser::add_region(HeapRegion* hr) {
ysr@777 188 assert(!hr->isHumongous(),
ysr@777 189 "Humongous regions shouldn't be added to the collection set");
ysr@777 190 assert(!hr->is_young(), "should not be young!");
tonyp@3714 191 _regions.append(hr);
tonyp@3539 192 _length++;
tonyp@3714 193 _remaining_reclaimable_bytes += hr->reclaimable_bytes();
ysr@777 194 hr->calc_gc_efficiency();
ysr@777 195 }
ysr@777 196
tonyp@3714 197 void CollectionSetChooser::prepare_for_par_region_addition(uint n_regions,
tonyp@3714 198 uint chunk_size) {
ysr@777 199 _first_par_unreserved_idx = 0;
tonyp@3713 200 uint n_threads = (uint) ParallelGCThreads;
jmasa@3294 201 if (UseDynamicNumberOfGCThreads) {
jmasa@3294 202 assert(G1CollectedHeap::heap()->workers()->active_workers() > 0,
jmasa@3294 203 "Should have been set earlier");
jmasa@3294 204 // This is defensive code. As the assertion above says, the number
jmasa@3294 205 // of active threads should be > 0, but in case there is some path
jmasa@3294 206 // or some improperly initialized variable with leads to no
jmasa@3294 207 // active threads, protect against that in a product build.
jmasa@3294 208 n_threads = MAX2(G1CollectedHeap::heap()->workers()->active_workers(),
jmasa@3357 209 1U);
jmasa@3294 210 }
tonyp@3714 211 uint max_waste = n_threads * chunk_size;
tonyp@3714 212 // it should be aligned with respect to chunk_size
tonyp@3714 213 uint aligned_n_regions = (n_regions + chunk_size - 1) / chunk_size * chunk_size;
tonyp@3714 214 assert(aligned_n_regions % chunk_size == 0, "should be aligned");
tonyp@3714 215 regions_at_put_grow(aligned_n_regions + max_waste - 1, NULL);
ysr@777 216 }
ysr@777 217
tonyp@3714 218 uint CollectionSetChooser::claim_array_chunk(uint chunk_size) {
tonyp@3714 219 uint res = (uint) Atomic::add((jint) chunk_size,
tonyp@3714 220 (volatile jint*) &_first_par_unreserved_idx);
tonyp@3714 221 assert(regions_length() > res + chunk_size - 1,
ysr@777 222 "Should already have been expanded");
tonyp@3714 223 return res - chunk_size;
ysr@777 224 }
ysr@777 225
tonyp@3714 226 void CollectionSetChooser::set_region(uint index, HeapRegion* hr) {
tonyp@3714 227 assert(regions_at(index) == NULL, "precondition");
ysr@777 228 assert(!hr->is_young(), "should not be young!");
tonyp@3714 229 regions_at_put(index, hr);
ysr@777 230 hr->calc_gc_efficiency();
ysr@777 231 }
ysr@777 232
tonyp@3714 233 void CollectionSetChooser::update_totals(uint region_num,
tonyp@3714 234 size_t reclaimable_bytes) {
tonyp@3539 235 // Only take the lock if we actually need to update the totals.
tonyp@3539 236 if (region_num > 0) {
tonyp@3539 237 assert(reclaimable_bytes > 0, "invariant");
tonyp@3539 238 // We could have just used atomics instead of taking the
tonyp@3539 239 // lock. However, we currently don't have an atomic add for size_t.
tonyp@3539 240 MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
tonyp@3714 241 _length += region_num;
tonyp@3714 242 _remaining_reclaimable_bytes += reclaimable_bytes;
tonyp@3539 243 } else {
tonyp@3539 244 assert(reclaimable_bytes == 0, "invariant");
tonyp@3539 245 }
ysr@777 246 }
ysr@777 247
tonyp@3714 248 void CollectionSetChooser::clear() {
tonyp@3714 249 _regions.clear();
tonyp@3539 250 _curr_index = 0;
tonyp@3539 251 _length = 0;
tonyp@3714 252 _remaining_reclaimable_bytes = 0;
ysr@777 253 };

mercurial