ysr@777: /* ysr@777: * Copyright 2001-2007 Sun Microsystems, Inc. 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: * ysr@777: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, ysr@777: * CA 95054 USA or visit www.sun.com if you need additional information or ysr@777: * have any questions. ysr@777: * ysr@777: */ ysr@777: ysr@777: # include "incls/_precompiled.incl" ysr@777: # include "incls/_collectionSetChooser.cpp.incl" ysr@777: ysr@777: CSetChooserCache::CSetChooserCache() { ysr@777: for (int i = 0; i < CacheLength; ++i) ysr@777: _cache[i] = NULL; ysr@777: clear(); ysr@777: } ysr@777: ysr@777: void CSetChooserCache::clear() { ysr@777: _occupancy = 0; ysr@777: _first = 0; ysr@777: for (int i = 0; i < CacheLength; ++i) { ysr@777: HeapRegion *hr = _cache[i]; ysr@777: if (hr != NULL) ysr@777: hr->set_sort_index(-1); ysr@777: _cache[i] = NULL; ysr@777: } ysr@777: } ysr@777: ysr@777: #ifndef PRODUCT ysr@777: bool CSetChooserCache::verify() { ysr@777: int index = _first; ysr@777: HeapRegion *prev = NULL; ysr@777: for (int i = 0; i < _occupancy; ++i) { ysr@777: guarantee(_cache[index] != NULL, "cache entry should not be empty"); ysr@777: HeapRegion *hr = _cache[index]; ysr@777: guarantee(!hr->is_young(), "should not be young!"); ysr@777: if (prev != NULL) { ysr@777: guarantee(prev->gc_efficiency() >= hr->gc_efficiency(), ysr@777: "cache should be correctly ordered"); ysr@777: } ysr@777: guarantee(hr->sort_index() == get_sort_index(index), ysr@777: "sort index should be correct"); ysr@777: index = trim_index(index + 1); ysr@777: prev = hr; ysr@777: } ysr@777: ysr@777: for (int i = 0; i < (CacheLength - _occupancy); ++i) { ysr@777: guarantee(_cache[index] == NULL, "cache entry should be empty"); ysr@777: index = trim_index(index + 1); ysr@777: } ysr@777: ysr@777: guarantee(index == _first, "we should have reached where we started from"); ysr@777: return true; ysr@777: } ysr@777: #endif // PRODUCT ysr@777: ysr@777: void CSetChooserCache::insert(HeapRegion *hr) { ysr@777: assert(!is_full(), "cache should not be empty"); ysr@777: hr->calc_gc_efficiency(); ysr@777: ysr@777: int empty_index; ysr@777: if (_occupancy == 0) { ysr@777: empty_index = _first; ysr@777: } else { ysr@777: empty_index = trim_index(_first + _occupancy); ysr@777: assert(_cache[empty_index] == NULL, "last slot should be empty"); ysr@777: int last_index = trim_index(empty_index - 1); ysr@777: HeapRegion *last = _cache[last_index]; ysr@777: assert(last != NULL,"as the cache is not empty, last should not be empty"); ysr@777: while (empty_index != _first && ysr@777: last->gc_efficiency() < hr->gc_efficiency()) { ysr@777: _cache[empty_index] = last; ysr@777: last->set_sort_index(get_sort_index(empty_index)); ysr@777: empty_index = last_index; ysr@777: last_index = trim_index(last_index - 1); ysr@777: last = _cache[last_index]; ysr@777: } ysr@777: } ysr@777: _cache[empty_index] = hr; ysr@777: hr->set_sort_index(get_sort_index(empty_index)); ysr@777: ysr@777: ++_occupancy; ysr@777: assert(verify(), "cache should be consistent"); ysr@777: } ysr@777: ysr@777: HeapRegion *CSetChooserCache::remove_first() { ysr@777: if (_occupancy > 0) { ysr@777: assert(_cache[_first] != NULL, "cache should have at least one region"); ysr@777: HeapRegion *ret = _cache[_first]; ysr@777: _cache[_first] = NULL; ysr@777: ret->set_sort_index(-1); ysr@777: --_occupancy; ysr@777: _first = trim_index(_first + 1); ysr@777: assert(verify(), "cache should be consistent"); ysr@777: return ret; ysr@777: } else { ysr@777: return NULL; ysr@777: } ysr@777: } ysr@777: ysr@777: // this is a bit expensive... but we expect that it should not be called ysr@777: // to often. ysr@777: void CSetChooserCache::remove(HeapRegion *hr) { ysr@777: assert(_occupancy > 0, "cache should not be empty"); ysr@777: assert(hr->sort_index() < -1, "should already be in the cache"); ysr@777: int index = get_index(hr->sort_index()); ysr@777: assert(_cache[index] == hr, "index should be correct"); ysr@777: int next_index = trim_index(index + 1); ysr@777: int last_index = trim_index(_first + _occupancy - 1); ysr@777: while (index != last_index) { ysr@777: assert(_cache[next_index] != NULL, "should not be null"); ysr@777: _cache[index] = _cache[next_index]; ysr@777: _cache[index]->set_sort_index(get_sort_index(index)); ysr@777: ysr@777: index = next_index; ysr@777: next_index = trim_index(next_index+1); ysr@777: } ysr@777: assert(index == last_index, "should have reached the last one"); ysr@777: _cache[index] = NULL; ysr@777: hr->set_sort_index(-1); ysr@777: --_occupancy; ysr@777: assert(verify(), "cache should be consistent"); ysr@777: } ysr@777: ysr@777: static inline int orderRegions(HeapRegion* hr1, HeapRegion* hr2) { ysr@777: if (hr1 == NULL) { ysr@777: if (hr2 == NULL) return 0; ysr@777: else return 1; ysr@777: } else if (hr2 == NULL) { ysr@777: return -1; ysr@777: } ysr@777: if (hr2->gc_efficiency() < hr1->gc_efficiency()) return -1; ysr@777: else if (hr1->gc_efficiency() < hr2->gc_efficiency()) return 1; ysr@777: else return 0; ysr@777: } ysr@777: ysr@777: static int orderRegions(HeapRegion** hr1p, HeapRegion** hr2p) { ysr@777: return orderRegions(*hr1p, *hr2p); ysr@777: } ysr@777: ysr@777: CollectionSetChooser::CollectionSetChooser() : ysr@777: // The line below is the worst bit of C++ hackery I've ever written ysr@777: // (Detlefs, 11/23). You should think of it as equivalent to ysr@777: // "_regions(100, true)": initialize the growable array and inform it ysr@777: // that it should allocate its elem array(s) on the C heap. The first ysr@777: // argument, however, is actually a comma expression (new-expr, 100). ysr@777: // The purpose of the new_expr is to inform the growable array that it ysr@777: // is *already* allocated on the C heap: it uses the placement syntax to ysr@777: // keep it from actually doing any allocation. ysr@777: _markedRegions((ResourceObj::operator new (sizeof(GrowableArray), ysr@777: (void*)&_markedRegions, ysr@777: ResourceObj::C_HEAP), ysr@777: 100), ysr@777: true), ysr@777: _curMarkedIndex(0), ysr@777: _numMarkedRegions(0), ysr@777: _unmarked_age_1_returned_as_new(false), ysr@777: _first_par_unreserved_idx(0) ysr@777: {} ysr@777: ysr@777: ysr@777: ysr@777: #ifndef PRODUCT ysr@777: bool CollectionSetChooser::verify() { ysr@777: int index = 0; ysr@777: guarantee(_curMarkedIndex <= _numMarkedRegions, ysr@777: "_curMarkedIndex should be within bounds"); ysr@777: while (index < _curMarkedIndex) { ysr@777: guarantee(_markedRegions.at(index++) == NULL, ysr@777: "all entries before _curMarkedIndex should be NULL"); ysr@777: } ysr@777: HeapRegion *prev = NULL; ysr@777: while (index < _numMarkedRegions) { ysr@777: HeapRegion *curr = _markedRegions.at(index++); ysr@777: if (curr != NULL) { ysr@777: int si = curr->sort_index(); ysr@777: guarantee(!curr->is_young(), "should not be young!"); ysr@777: guarantee(si > -1 && si == (index-1), "sort index invariant"); ysr@777: if (prev != NULL) { ysr@777: guarantee(orderRegions(prev, curr) != 1, "regions should be sorted"); ysr@777: } ysr@777: prev = curr; ysr@777: } ysr@777: } ysr@777: return _cache.verify(); ysr@777: } ysr@777: #endif ysr@777: ysr@777: bool ysr@777: CollectionSetChooser::addRegionToCache() { ysr@777: assert(!_cache.is_full(), "cache should not be full"); ysr@777: ysr@777: HeapRegion *hr = NULL; ysr@777: while (hr == NULL && _curMarkedIndex < _numMarkedRegions) { ysr@777: hr = _markedRegions.at(_curMarkedIndex++); ysr@777: } ysr@777: if (hr == NULL) ysr@777: return false; ysr@777: assert(!hr->is_young(), "should not be young!"); ysr@777: assert(hr->sort_index() == _curMarkedIndex-1, "sort_index invariant"); ysr@777: _markedRegions.at_put(hr->sort_index(), NULL); ysr@777: _cache.insert(hr); ysr@777: assert(!_cache.is_empty(), "cache should not be empty"); ysr@777: assert(verify(), "cache should be consistent"); ysr@777: return false; ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser::fillCache() { ysr@777: while (!_cache.is_full() && addRegionToCache()) { ysr@777: } ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser::sortMarkedHeapRegions() { ysr@777: guarantee(_cache.is_empty(), "cache should be empty"); ysr@777: // First trim any unused portion of the top in the parallel case. ysr@777: if (_first_par_unreserved_idx > 0) { ysr@777: if (G1PrintParCleanupStats) { ysr@777: gclog_or_tty->print(" Truncating _markedRegions from %d to %d.\n", ysr@777: _markedRegions.length(), _first_par_unreserved_idx); ysr@777: } ysr@777: assert(_first_par_unreserved_idx <= _markedRegions.length(), ysr@777: "Or we didn't reserved enough length"); ysr@777: _markedRegions.trunc_to(_first_par_unreserved_idx); ysr@777: } ysr@777: _markedRegions.sort(orderRegions); ysr@777: assert(_numMarkedRegions <= _markedRegions.length(), "Requirement"); ysr@777: assert(_numMarkedRegions == 0 ysr@777: || _markedRegions.at(_numMarkedRegions-1) != NULL, ysr@777: "Testing _numMarkedRegions"); ysr@777: assert(_numMarkedRegions == _markedRegions.length() ysr@777: || _markedRegions.at(_numMarkedRegions) == NULL, ysr@777: "Testing _numMarkedRegions"); ysr@777: if (G1PrintParCleanupStats) { ysr@777: gclog_or_tty->print_cr(" Sorted %d marked regions.", _numMarkedRegions); ysr@777: } ysr@777: for (int i = 0; i < _numMarkedRegions; i++) { ysr@777: assert(_markedRegions.at(i) != NULL, "Should be true by sorting!"); ysr@777: _markedRegions.at(i)->set_sort_index(i); ysr@777: if (G1PrintRegionLivenessInfo > 0) { ysr@777: if (i == 0) gclog_or_tty->print_cr("Sorted marked regions:"); ysr@777: if (i < G1PrintRegionLivenessInfo || ysr@777: (_numMarkedRegions-i) < G1PrintRegionLivenessInfo) { ysr@777: HeapRegion* hr = _markedRegions.at(i); ysr@777: size_t u = hr->used(); ysr@777: gclog_or_tty->print_cr(" Region %d: %d used, %d max live, %5.2f%%.", ysr@777: i, u, hr->max_live_bytes(), ysr@777: 100.0*(float)hr->max_live_bytes()/(float)u); ysr@777: } ysr@777: } ysr@777: } ysr@777: if (G1PolicyVerbose > 1) ysr@777: printSortedHeapRegions(); ysr@777: assert(verify(), "should now be sorted"); ysr@777: } ysr@777: ysr@777: void ysr@777: printHeapRegion(HeapRegion *hr) { ysr@777: if (hr->isHumongous()) ysr@777: gclog_or_tty->print("H: "); ysr@777: if (hr->in_collection_set()) ysr@777: gclog_or_tty->print("CS: "); ysr@777: gclog_or_tty->print_cr("Region " PTR_FORMAT " (%s%s) " ysr@777: "[" PTR_FORMAT ", " PTR_FORMAT"] " ysr@777: "Used: " SIZE_FORMAT "K, garbage: " SIZE_FORMAT "K.", ysr@777: hr, hr->is_young() ? "Y " : " ", ysr@777: hr->is_marked()? "M1" : "M0", ysr@777: hr->bottom(), hr->end(), ysr@777: hr->used()/K, hr->garbage_bytes()/K); ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser::addMarkedHeapRegion(HeapRegion* hr) { ysr@777: assert(!hr->isHumongous(), ysr@777: "Humongous regions shouldn't be added to the collection set"); ysr@777: assert(!hr->is_young(), "should not be young!"); ysr@777: _markedRegions.append(hr); ysr@777: _numMarkedRegions++; ysr@777: hr->calc_gc_efficiency(); ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser:: ysr@777: prepareForAddMarkedHeapRegionsPar(size_t n_regions, size_t chunkSize) { ysr@777: _first_par_unreserved_idx = 0; ysr@777: size_t max_waste = ParallelGCThreads * chunkSize; ysr@777: // it should be aligned with respect to chunkSize ysr@777: size_t aligned_n_regions = ysr@777: (n_regions + (chunkSize - 1)) / chunkSize * chunkSize; ysr@777: assert( aligned_n_regions % chunkSize == 0, "should be aligned" ); ysr@777: _markedRegions.at_put_grow((int)(aligned_n_regions + max_waste - 1), NULL); ysr@777: } ysr@777: ysr@777: jint ysr@777: CollectionSetChooser::getParMarkedHeapRegionChunk(jint n_regions) { ysr@777: jint res = Atomic::add(n_regions, &_first_par_unreserved_idx); ysr@777: assert(_markedRegions.length() > res + n_regions - 1, ysr@777: "Should already have been expanded"); ysr@777: return res - n_regions; ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser::setMarkedHeapRegion(jint index, HeapRegion* hr) { ysr@777: assert(_markedRegions.at(index) == NULL, "precondition"); ysr@777: assert(!hr->is_young(), "should not be young!"); ysr@777: _markedRegions.at_put(index, hr); ysr@777: hr->calc_gc_efficiency(); ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser::incNumMarkedHeapRegions(jint inc_by) { ysr@777: (void)Atomic::add(inc_by, &_numMarkedRegions); ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser::clearMarkedHeapRegions(){ ysr@777: for (int i = 0; i < _markedRegions.length(); i++) { ysr@777: HeapRegion* r = _markedRegions.at(i); ysr@777: if (r != NULL) r->set_sort_index(-1); ysr@777: } ysr@777: _markedRegions.clear(); ysr@777: _curMarkedIndex = 0; ysr@777: _numMarkedRegions = 0; ysr@777: _cache.clear(); ysr@777: }; ysr@777: ysr@777: void ysr@777: CollectionSetChooser::updateAfterFullCollection() { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: clearMarkedHeapRegions(); ysr@777: } ysr@777: ysr@777: void ysr@777: CollectionSetChooser::printSortedHeapRegions() { ysr@777: gclog_or_tty->print_cr("Printing %d Heap Regions sorted by amount of known garbage", ysr@777: _numMarkedRegions); ysr@777: for (int i = 0; i < _markedRegions.length(); i++) { ysr@777: printHeapRegion(_markedRegions.at(i)); ysr@777: } ysr@777: gclog_or_tty->print_cr("Done sorted heap region print"); ysr@777: } ysr@777: ysr@777: void CollectionSetChooser::removeRegion(HeapRegion *hr) { ysr@777: int si = hr->sort_index(); ysr@777: assert(si == -1 || hr->is_marked(), "Sort index not valid."); ysr@777: if (si > -1) { ysr@777: assert(_markedRegions.at(si) == hr, "Sort index not valid." ); ysr@777: _markedRegions.at_put(si, NULL); ysr@777: } else if (si < -1) { ysr@777: assert(_cache.region_in_cache(hr), "should be in the cache"); ysr@777: _cache.remove(hr); ysr@777: assert(hr->sort_index() == -1, "sort index invariant"); ysr@777: } ysr@777: hr->set_sort_index(-1); ysr@777: } ysr@777: ysr@777: // if time_remaining < 0.0, then this method should try to return ysr@777: // a region, whether it fits within the remaining time or not ysr@777: HeapRegion* ysr@777: CollectionSetChooser::getNextMarkedRegion(double time_remaining, ysr@777: double avg_prediction) { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: G1CollectorPolicy* g1p = g1h->g1_policy(); ysr@777: fillCache(); ysr@777: if (_cache.is_empty()) { ysr@777: assert(_curMarkedIndex == _numMarkedRegions, ysr@777: "if cache is empty, list should also be empty"); ysr@777: return NULL; ysr@777: } ysr@777: ysr@777: HeapRegion *hr = _cache.get_first(); ysr@777: assert(hr != NULL, "if cache not empty, first entry should be non-null"); ysr@777: double predicted_time = g1h->predict_region_elapsed_time_ms(hr, false); ysr@777: ysr@777: if (g1p->adaptive_young_list_length()) { ysr@777: if (time_remaining - predicted_time < 0.0) { ysr@777: g1h->check_if_region_is_too_expensive(predicted_time); ysr@777: return NULL; ysr@777: } ysr@777: } else { ysr@777: if (predicted_time > 2.0 * avg_prediction) { ysr@777: return NULL; ysr@777: } ysr@777: } ysr@777: ysr@777: HeapRegion *hr2 = _cache.remove_first(); ysr@777: assert(hr == hr2, "cache contents should not have changed"); ysr@777: ysr@777: return hr; ysr@777: }