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

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 2043
2dfd013a7465
child 2717
371bbc844bf1
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

ysr@777 1 /*
stefank@2314 2 * Copyright (c) 2001, 2010, 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"
stefank@2314 29 #include "memory/space.inline.hpp"
ysr@777 30
ysr@777 31 CSetChooserCache::CSetChooserCache() {
ysr@777 32 for (int i = 0; i < CacheLength; ++i)
ysr@777 33 _cache[i] = NULL;
ysr@777 34 clear();
ysr@777 35 }
ysr@777 36
ysr@777 37 void CSetChooserCache::clear() {
ysr@777 38 _occupancy = 0;
ysr@777 39 _first = 0;
ysr@777 40 for (int i = 0; i < CacheLength; ++i) {
ysr@777 41 HeapRegion *hr = _cache[i];
ysr@777 42 if (hr != NULL)
ysr@777 43 hr->set_sort_index(-1);
ysr@777 44 _cache[i] = NULL;
ysr@777 45 }
ysr@777 46 }
ysr@777 47
ysr@777 48 #ifndef PRODUCT
ysr@777 49 bool CSetChooserCache::verify() {
ysr@777 50 int index = _first;
ysr@777 51 HeapRegion *prev = NULL;
ysr@777 52 for (int i = 0; i < _occupancy; ++i) {
ysr@777 53 guarantee(_cache[index] != NULL, "cache entry should not be empty");
ysr@777 54 HeapRegion *hr = _cache[index];
ysr@777 55 guarantee(!hr->is_young(), "should not be young!");
ysr@777 56 if (prev != NULL) {
ysr@777 57 guarantee(prev->gc_efficiency() >= hr->gc_efficiency(),
ysr@777 58 "cache should be correctly ordered");
ysr@777 59 }
ysr@777 60 guarantee(hr->sort_index() == get_sort_index(index),
ysr@777 61 "sort index should be correct");
ysr@777 62 index = trim_index(index + 1);
ysr@777 63 prev = hr;
ysr@777 64 }
ysr@777 65
ysr@777 66 for (int i = 0; i < (CacheLength - _occupancy); ++i) {
ysr@777 67 guarantee(_cache[index] == NULL, "cache entry should be empty");
ysr@777 68 index = trim_index(index + 1);
ysr@777 69 }
ysr@777 70
ysr@777 71 guarantee(index == _first, "we should have reached where we started from");
ysr@777 72 return true;
ysr@777 73 }
ysr@777 74 #endif // PRODUCT
ysr@777 75
ysr@777 76 void CSetChooserCache::insert(HeapRegion *hr) {
ysr@777 77 assert(!is_full(), "cache should not be empty");
ysr@777 78 hr->calc_gc_efficiency();
ysr@777 79
ysr@777 80 int empty_index;
ysr@777 81 if (_occupancy == 0) {
ysr@777 82 empty_index = _first;
ysr@777 83 } else {
ysr@777 84 empty_index = trim_index(_first + _occupancy);
ysr@777 85 assert(_cache[empty_index] == NULL, "last slot should be empty");
ysr@777 86 int last_index = trim_index(empty_index - 1);
ysr@777 87 HeapRegion *last = _cache[last_index];
ysr@777 88 assert(last != NULL,"as the cache is not empty, last should not be empty");
ysr@777 89 while (empty_index != _first &&
ysr@777 90 last->gc_efficiency() < hr->gc_efficiency()) {
ysr@777 91 _cache[empty_index] = last;
ysr@777 92 last->set_sort_index(get_sort_index(empty_index));
ysr@777 93 empty_index = last_index;
ysr@777 94 last_index = trim_index(last_index - 1);
ysr@777 95 last = _cache[last_index];
ysr@777 96 }
ysr@777 97 }
ysr@777 98 _cache[empty_index] = hr;
ysr@777 99 hr->set_sort_index(get_sort_index(empty_index));
ysr@777 100
ysr@777 101 ++_occupancy;
ysr@777 102 assert(verify(), "cache should be consistent");
ysr@777 103 }
ysr@777 104
ysr@777 105 HeapRegion *CSetChooserCache::remove_first() {
ysr@777 106 if (_occupancy > 0) {
ysr@777 107 assert(_cache[_first] != NULL, "cache should have at least one region");
ysr@777 108 HeapRegion *ret = _cache[_first];
ysr@777 109 _cache[_first] = NULL;
ysr@777 110 ret->set_sort_index(-1);
ysr@777 111 --_occupancy;
ysr@777 112 _first = trim_index(_first + 1);
ysr@777 113 assert(verify(), "cache should be consistent");
ysr@777 114 return ret;
ysr@777 115 } else {
ysr@777 116 return NULL;
ysr@777 117 }
ysr@777 118 }
ysr@777 119
ysr@777 120 // this is a bit expensive... but we expect that it should not be called
ysr@777 121 // to often.
ysr@777 122 void CSetChooserCache::remove(HeapRegion *hr) {
ysr@777 123 assert(_occupancy > 0, "cache should not be empty");
ysr@777 124 assert(hr->sort_index() < -1, "should already be in the cache");
ysr@777 125 int index = get_index(hr->sort_index());
ysr@777 126 assert(_cache[index] == hr, "index should be correct");
ysr@777 127 int next_index = trim_index(index + 1);
ysr@777 128 int last_index = trim_index(_first + _occupancy - 1);
ysr@777 129 while (index != last_index) {
ysr@777 130 assert(_cache[next_index] != NULL, "should not be null");
ysr@777 131 _cache[index] = _cache[next_index];
ysr@777 132 _cache[index]->set_sort_index(get_sort_index(index));
ysr@777 133
ysr@777 134 index = next_index;
ysr@777 135 next_index = trim_index(next_index+1);
ysr@777 136 }
ysr@777 137 assert(index == last_index, "should have reached the last one");
ysr@777 138 _cache[index] = NULL;
ysr@777 139 hr->set_sort_index(-1);
ysr@777 140 --_occupancy;
ysr@777 141 assert(verify(), "cache should be consistent");
ysr@777 142 }
ysr@777 143
ysr@777 144 static inline int orderRegions(HeapRegion* hr1, HeapRegion* hr2) {
ysr@777 145 if (hr1 == NULL) {
ysr@777 146 if (hr2 == NULL) return 0;
ysr@777 147 else return 1;
ysr@777 148 } else if (hr2 == NULL) {
ysr@777 149 return -1;
ysr@777 150 }
ysr@777 151 if (hr2->gc_efficiency() < hr1->gc_efficiency()) return -1;
ysr@777 152 else if (hr1->gc_efficiency() < hr2->gc_efficiency()) return 1;
ysr@777 153 else return 0;
ysr@777 154 }
ysr@777 155
ysr@777 156 static int orderRegions(HeapRegion** hr1p, HeapRegion** hr2p) {
ysr@777 157 return orderRegions(*hr1p, *hr2p);
ysr@777 158 }
ysr@777 159
ysr@777 160 CollectionSetChooser::CollectionSetChooser() :
ysr@777 161 // The line below is the worst bit of C++ hackery I've ever written
ysr@777 162 // (Detlefs, 11/23). You should think of it as equivalent to
ysr@777 163 // "_regions(100, true)": initialize the growable array and inform it
kvn@2043 164 // that it should allocate its elem array(s) on the C heap.
kvn@2043 165 //
kvn@2043 166 // The first argument, however, is actually a comma expression
kvn@2043 167 // (set_allocation_type(this, C_HEAP), 100). The purpose of the
kvn@2043 168 // set_allocation_type() call is to replace the default allocation
kvn@2043 169 // type for embedded objects STACK_OR_EMBEDDED with C_HEAP. It will
kvn@2043 170 // allow to pass the assert in GenericGrowableArray() which checks
kvn@2043 171 // that a growable array object must be on C heap if elements are.
kvn@2043 172 //
kvn@2043 173 // Note: containing object is allocated on C heap since it is CHeapObj.
kvn@2043 174 //
kvn@2043 175 _markedRegions((ResourceObj::set_allocation_type((address)&_markedRegions,
ysr@777 176 ResourceObj::C_HEAP),
ysr@777 177 100),
ysr@777 178 true),
ysr@777 179 _curMarkedIndex(0),
ysr@777 180 _numMarkedRegions(0),
ysr@777 181 _unmarked_age_1_returned_as_new(false),
ysr@777 182 _first_par_unreserved_idx(0)
ysr@777 183 {}
ysr@777 184
ysr@777 185
ysr@777 186
ysr@777 187 #ifndef PRODUCT
ysr@777 188 bool CollectionSetChooser::verify() {
ysr@777 189 int index = 0;
ysr@777 190 guarantee(_curMarkedIndex <= _numMarkedRegions,
ysr@777 191 "_curMarkedIndex should be within bounds");
ysr@777 192 while (index < _curMarkedIndex) {
ysr@777 193 guarantee(_markedRegions.at(index++) == NULL,
ysr@777 194 "all entries before _curMarkedIndex should be NULL");
ysr@777 195 }
ysr@777 196 HeapRegion *prev = NULL;
ysr@777 197 while (index < _numMarkedRegions) {
ysr@777 198 HeapRegion *curr = _markedRegions.at(index++);
ysr@777 199 if (curr != NULL) {
ysr@777 200 int si = curr->sort_index();
ysr@777 201 guarantee(!curr->is_young(), "should not be young!");
ysr@777 202 guarantee(si > -1 && si == (index-1), "sort index invariant");
ysr@777 203 if (prev != NULL) {
ysr@777 204 guarantee(orderRegions(prev, curr) != 1, "regions should be sorted");
ysr@777 205 }
ysr@777 206 prev = curr;
ysr@777 207 }
ysr@777 208 }
ysr@777 209 return _cache.verify();
ysr@777 210 }
ysr@777 211 #endif
ysr@777 212
ysr@777 213 bool
ysr@777 214 CollectionSetChooser::addRegionToCache() {
ysr@777 215 assert(!_cache.is_full(), "cache should not be full");
ysr@777 216
ysr@777 217 HeapRegion *hr = NULL;
ysr@777 218 while (hr == NULL && _curMarkedIndex < _numMarkedRegions) {
ysr@777 219 hr = _markedRegions.at(_curMarkedIndex++);
ysr@777 220 }
ysr@777 221 if (hr == NULL)
ysr@777 222 return false;
ysr@777 223 assert(!hr->is_young(), "should not be young!");
ysr@777 224 assert(hr->sort_index() == _curMarkedIndex-1, "sort_index invariant");
ysr@777 225 _markedRegions.at_put(hr->sort_index(), NULL);
ysr@777 226 _cache.insert(hr);
ysr@777 227 assert(!_cache.is_empty(), "cache should not be empty");
ysr@777 228 assert(verify(), "cache should be consistent");
ysr@777 229 return false;
ysr@777 230 }
ysr@777 231
ysr@777 232 void
ysr@777 233 CollectionSetChooser::fillCache() {
ysr@777 234 while (!_cache.is_full() && addRegionToCache()) {
ysr@777 235 }
ysr@777 236 }
ysr@777 237
ysr@777 238 void
ysr@777 239 CollectionSetChooser::sortMarkedHeapRegions() {
ysr@777 240 guarantee(_cache.is_empty(), "cache should be empty");
ysr@777 241 // First trim any unused portion of the top in the parallel case.
ysr@777 242 if (_first_par_unreserved_idx > 0) {
ysr@777 243 if (G1PrintParCleanupStats) {
ysr@777 244 gclog_or_tty->print(" Truncating _markedRegions from %d to %d.\n",
ysr@777 245 _markedRegions.length(), _first_par_unreserved_idx);
ysr@777 246 }
ysr@777 247 assert(_first_par_unreserved_idx <= _markedRegions.length(),
ysr@777 248 "Or we didn't reserved enough length");
ysr@777 249 _markedRegions.trunc_to(_first_par_unreserved_idx);
ysr@777 250 }
ysr@777 251 _markedRegions.sort(orderRegions);
ysr@777 252 assert(_numMarkedRegions <= _markedRegions.length(), "Requirement");
ysr@777 253 assert(_numMarkedRegions == 0
ysr@777 254 || _markedRegions.at(_numMarkedRegions-1) != NULL,
ysr@777 255 "Testing _numMarkedRegions");
ysr@777 256 assert(_numMarkedRegions == _markedRegions.length()
ysr@777 257 || _markedRegions.at(_numMarkedRegions) == NULL,
ysr@777 258 "Testing _numMarkedRegions");
ysr@777 259 if (G1PrintParCleanupStats) {
ysr@777 260 gclog_or_tty->print_cr(" Sorted %d marked regions.", _numMarkedRegions);
ysr@777 261 }
ysr@777 262 for (int i = 0; i < _numMarkedRegions; i++) {
ysr@777 263 assert(_markedRegions.at(i) != NULL, "Should be true by sorting!");
ysr@777 264 _markedRegions.at(i)->set_sort_index(i);
ysr@777 265 if (G1PrintRegionLivenessInfo > 0) {
ysr@777 266 if (i == 0) gclog_or_tty->print_cr("Sorted marked regions:");
ysr@777 267 if (i < G1PrintRegionLivenessInfo ||
ysr@777 268 (_numMarkedRegions-i) < G1PrintRegionLivenessInfo) {
ysr@777 269 HeapRegion* hr = _markedRegions.at(i);
ysr@777 270 size_t u = hr->used();
ysr@777 271 gclog_or_tty->print_cr(" Region %d: %d used, %d max live, %5.2f%%.",
ysr@777 272 i, u, hr->max_live_bytes(),
ysr@777 273 100.0*(float)hr->max_live_bytes()/(float)u);
ysr@777 274 }
ysr@777 275 }
ysr@777 276 }
ysr@777 277 if (G1PolicyVerbose > 1)
ysr@777 278 printSortedHeapRegions();
ysr@777 279 assert(verify(), "should now be sorted");
ysr@777 280 }
ysr@777 281
ysr@777 282 void
ysr@777 283 printHeapRegion(HeapRegion *hr) {
ysr@777 284 if (hr->isHumongous())
ysr@777 285 gclog_or_tty->print("H: ");
ysr@777 286 if (hr->in_collection_set())
ysr@777 287 gclog_or_tty->print("CS: ");
ysr@777 288 gclog_or_tty->print_cr("Region " PTR_FORMAT " (%s%s) "
ysr@777 289 "[" PTR_FORMAT ", " PTR_FORMAT"] "
ysr@777 290 "Used: " SIZE_FORMAT "K, garbage: " SIZE_FORMAT "K.",
ysr@777 291 hr, hr->is_young() ? "Y " : " ",
ysr@777 292 hr->is_marked()? "M1" : "M0",
ysr@777 293 hr->bottom(), hr->end(),
ysr@777 294 hr->used()/K, hr->garbage_bytes()/K);
ysr@777 295 }
ysr@777 296
ysr@777 297 void
ysr@777 298 CollectionSetChooser::addMarkedHeapRegion(HeapRegion* hr) {
ysr@777 299 assert(!hr->isHumongous(),
ysr@777 300 "Humongous regions shouldn't be added to the collection set");
ysr@777 301 assert(!hr->is_young(), "should not be young!");
ysr@777 302 _markedRegions.append(hr);
ysr@777 303 _numMarkedRegions++;
ysr@777 304 hr->calc_gc_efficiency();
ysr@777 305 }
ysr@777 306
ysr@777 307 void
ysr@777 308 CollectionSetChooser::
ysr@777 309 prepareForAddMarkedHeapRegionsPar(size_t n_regions, size_t chunkSize) {
ysr@777 310 _first_par_unreserved_idx = 0;
ysr@777 311 size_t max_waste = ParallelGCThreads * chunkSize;
ysr@777 312 // it should be aligned with respect to chunkSize
ysr@777 313 size_t aligned_n_regions =
ysr@777 314 (n_regions + (chunkSize - 1)) / chunkSize * chunkSize;
ysr@777 315 assert( aligned_n_regions % chunkSize == 0, "should be aligned" );
ysr@777 316 _markedRegions.at_put_grow((int)(aligned_n_regions + max_waste - 1), NULL);
ysr@777 317 }
ysr@777 318
ysr@777 319 jint
ysr@777 320 CollectionSetChooser::getParMarkedHeapRegionChunk(jint n_regions) {
ysr@777 321 jint res = Atomic::add(n_regions, &_first_par_unreserved_idx);
ysr@777 322 assert(_markedRegions.length() > res + n_regions - 1,
ysr@777 323 "Should already have been expanded");
ysr@777 324 return res - n_regions;
ysr@777 325 }
ysr@777 326
ysr@777 327 void
ysr@777 328 CollectionSetChooser::setMarkedHeapRegion(jint index, HeapRegion* hr) {
ysr@777 329 assert(_markedRegions.at(index) == NULL, "precondition");
ysr@777 330 assert(!hr->is_young(), "should not be young!");
ysr@777 331 _markedRegions.at_put(index, hr);
ysr@777 332 hr->calc_gc_efficiency();
ysr@777 333 }
ysr@777 334
ysr@777 335 void
ysr@777 336 CollectionSetChooser::incNumMarkedHeapRegions(jint inc_by) {
ysr@777 337 (void)Atomic::add(inc_by, &_numMarkedRegions);
ysr@777 338 }
ysr@777 339
ysr@777 340 void
ysr@777 341 CollectionSetChooser::clearMarkedHeapRegions(){
ysr@777 342 for (int i = 0; i < _markedRegions.length(); i++) {
ysr@777 343 HeapRegion* r = _markedRegions.at(i);
ysr@777 344 if (r != NULL) r->set_sort_index(-1);
ysr@777 345 }
ysr@777 346 _markedRegions.clear();
ysr@777 347 _curMarkedIndex = 0;
ysr@777 348 _numMarkedRegions = 0;
ysr@777 349 _cache.clear();
ysr@777 350 };
ysr@777 351
ysr@777 352 void
ysr@777 353 CollectionSetChooser::updateAfterFullCollection() {
ysr@777 354 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 355 clearMarkedHeapRegions();
ysr@777 356 }
ysr@777 357
ysr@777 358 void
ysr@777 359 CollectionSetChooser::printSortedHeapRegions() {
ysr@777 360 gclog_or_tty->print_cr("Printing %d Heap Regions sorted by amount of known garbage",
ysr@777 361 _numMarkedRegions);
johnc@1538 362
johnc@1538 363 DEBUG_ONLY(int marked_count = 0;)
ysr@777 364 for (int i = 0; i < _markedRegions.length(); i++) {
johnc@1538 365 HeapRegion* r = _markedRegions.at(i);
johnc@1538 366 if (r != NULL) {
johnc@1538 367 printHeapRegion(r);
johnc@1538 368 DEBUG_ONLY(marked_count++;)
johnc@1538 369 }
ysr@777 370 }
johnc@1538 371 assert(marked_count == _numMarkedRegions, "must be");
ysr@777 372 gclog_or_tty->print_cr("Done sorted heap region print");
ysr@777 373 }
ysr@777 374
ysr@777 375 void CollectionSetChooser::removeRegion(HeapRegion *hr) {
ysr@777 376 int si = hr->sort_index();
ysr@777 377 assert(si == -1 || hr->is_marked(), "Sort index not valid.");
ysr@777 378 if (si > -1) {
ysr@777 379 assert(_markedRegions.at(si) == hr, "Sort index not valid." );
ysr@777 380 _markedRegions.at_put(si, NULL);
ysr@777 381 } else if (si < -1) {
ysr@777 382 assert(_cache.region_in_cache(hr), "should be in the cache");
ysr@777 383 _cache.remove(hr);
ysr@777 384 assert(hr->sort_index() == -1, "sort index invariant");
ysr@777 385 }
ysr@777 386 hr->set_sort_index(-1);
ysr@777 387 }
ysr@777 388
ysr@777 389 // if time_remaining < 0.0, then this method should try to return
ysr@777 390 // a region, whether it fits within the remaining time or not
ysr@777 391 HeapRegion*
ysr@777 392 CollectionSetChooser::getNextMarkedRegion(double time_remaining,
ysr@777 393 double avg_prediction) {
ysr@777 394 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 395 G1CollectorPolicy* g1p = g1h->g1_policy();
ysr@777 396 fillCache();
ysr@777 397 if (_cache.is_empty()) {
ysr@777 398 assert(_curMarkedIndex == _numMarkedRegions,
ysr@777 399 "if cache is empty, list should also be empty");
ysr@777 400 return NULL;
ysr@777 401 }
ysr@777 402
ysr@777 403 HeapRegion *hr = _cache.get_first();
ysr@777 404 assert(hr != NULL, "if cache not empty, first entry should be non-null");
ysr@777 405 double predicted_time = g1h->predict_region_elapsed_time_ms(hr, false);
ysr@777 406
ysr@777 407 if (g1p->adaptive_young_list_length()) {
ysr@777 408 if (time_remaining - predicted_time < 0.0) {
ysr@777 409 g1h->check_if_region_is_too_expensive(predicted_time);
ysr@777 410 return NULL;
ysr@777 411 }
ysr@777 412 } else {
ysr@777 413 if (predicted_time > 2.0 * avg_prediction) {
ysr@777 414 return NULL;
ysr@777 415 }
ysr@777 416 }
ysr@777 417
ysr@777 418 HeapRegion *hr2 = _cache.remove_first();
ysr@777 419 assert(hr == hr2, "cache contents should not have changed");
ysr@777 420
ysr@777 421 return hr;
ysr@777 422 }

mercurial