ysr@777: /* mikael@6198: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/g1/collectionSetChooser.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectorPolicy.hpp" tonyp@3114: #include "gc_implementation/g1/g1ErgoVerbose.hpp" stefank@2314: #include "memory/space.inline.hpp" ysr@777: tonyp@3539: // Even though we don't use the GC efficiency in our heuristics as tonyp@3539: // much as we used to, we still order according to GC efficiency. This tonyp@3539: // will cause regions with a lot of live objects and large RSets to tonyp@3539: // end up at the end of the array. Given that we might skip collecting tonyp@3539: // the last few old regions, if after a few mixed GCs the remaining tonyp@3539: // have reclaimable bytes under a certain threshold, the hope is that tonyp@3539: // the ones we'll skip are ones with both large RSets and a lot of tonyp@3539: // live objects, not the ones with just a lot of live objects if we tonyp@3539: // ordered according to the amount of reclaimable bytes per region. tonyp@3714: static int order_regions(HeapRegion* hr1, HeapRegion* hr2) { ysr@777: if (hr1 == NULL) { tonyp@3539: if (hr2 == NULL) { tonyp@3539: return 0; tonyp@3539: } else { tonyp@3539: return 1; tonyp@3539: } ysr@777: } else if (hr2 == NULL) { ysr@777: return -1; ysr@777: } tonyp@3539: tonyp@3539: double gc_eff1 = hr1->gc_efficiency(); tonyp@3539: double gc_eff2 = hr2->gc_efficiency(); tonyp@3539: if (gc_eff1 > gc_eff2) { tonyp@3539: return -1; tonyp@3539: } if (gc_eff1 < gc_eff2) { tonyp@3539: return 1; tonyp@3539: } else { tonyp@3539: return 0; tonyp@3539: } ysr@777: } ysr@777: tonyp@3714: static int order_regions(HeapRegion** hr1p, HeapRegion** hr2p) { tonyp@3714: return order_regions(*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 kvn@2043: // that it should allocate its elem array(s) on the C heap. kvn@2043: // kvn@2043: // The first argument, however, is actually a comma expression kvn@2043: // (set_allocation_type(this, C_HEAP), 100). The purpose of the kvn@2043: // set_allocation_type() call is to replace the default allocation kvn@2043: // type for embedded objects STACK_OR_EMBEDDED with C_HEAP. It will kvn@2043: // allow to pass the assert in GenericGrowableArray() which checks kvn@2043: // that a growable array object must be on C heap if elements are. kvn@2043: // kvn@2043: // Note: containing object is allocated on C heap since it is CHeapObj. kvn@2043: // tonyp@3714: _regions((ResourceObj::set_allocation_type((address) &_regions, ysr@777: ResourceObj::C_HEAP), tonyp@3539: 100), true /* C_Heap */), tonyp@3714: _curr_index(0), _length(0), _first_par_unreserved_idx(0), tonyp@3714: _region_live_threshold_bytes(0), _remaining_reclaimable_bytes(0) { tonyp@3714: _region_live_threshold_bytes = johnc@4385: HeapRegion::GrainBytes * (size_t) G1MixedGCLiveThresholdPercent / 100; tonyp@3539: } ysr@777: ysr@777: #ifndef PRODUCT tonyp@3714: void CollectionSetChooser::verify() { tonyp@3714: guarantee(_length <= regions_length(), tonyp@3714: err_msg("_length: %u regions length: %u", _length, regions_length())); tonyp@3714: guarantee(_curr_index <= _length, tonyp@3714: err_msg("_curr_index: %u _length: %u", _curr_index, _length)); tonyp@3714: uint index = 0; tonyp@3539: size_t sum_of_reclaimable_bytes = 0; tonyp@3539: while (index < _curr_index) { tonyp@3714: guarantee(regions_at(index) == NULL, tonyp@3539: "all entries before _curr_index should be NULL"); tonyp@3539: index += 1; ysr@777: } ysr@777: HeapRegion *prev = NULL; tonyp@3539: while (index < _length) { tonyp@3714: HeapRegion *curr = regions_at(index++); tonyp@3714: guarantee(curr != NULL, "Regions in _regions array cannot be NULL"); ysr@3185: guarantee(!curr->is_young(), "should not be young!"); tonyp@3539: guarantee(!curr->isHumongous(), "should not be humongous!"); ysr@3185: if (prev != NULL) { tonyp@3714: guarantee(order_regions(prev, curr) != 1, tonyp@3539: err_msg("GC eff prev: %1.4f GC eff curr: %1.4f", tonyp@3539: prev->gc_efficiency(), curr->gc_efficiency())); ysr@777: } tonyp@3539: sum_of_reclaimable_bytes += curr->reclaimable_bytes(); ysr@3185: prev = curr; ysr@777: } tonyp@3714: guarantee(sum_of_reclaimable_bytes == _remaining_reclaimable_bytes, tonyp@3539: err_msg("reclaimable bytes inconsistent, " kevinw@9327: "remaining: " SIZE_FORMAT " sum: " SIZE_FORMAT, tonyp@3714: _remaining_reclaimable_bytes, sum_of_reclaimable_bytes)); ysr@777: } tonyp@3714: #endif // !PRODUCT ysr@777: tonyp@3714: void CollectionSetChooser::sort_regions() { ysr@777: // First trim any unused portion of the top in the parallel case. ysr@777: if (_first_par_unreserved_idx > 0) { tonyp@3714: assert(_first_par_unreserved_idx <= regions_length(), ysr@777: "Or we didn't reserved enough length"); tonyp@3714: regions_trunc_to(_first_par_unreserved_idx); ysr@777: } tonyp@3714: _regions.sort(order_regions); tonyp@3714: assert(_length <= regions_length(), "Requirement"); tonyp@3714: #ifdef ASSERT tonyp@3714: for (uint i = 0; i < _length; i++) { tonyp@3714: assert(regions_at(i) != NULL, "Should be true by sorting!"); ysr@777: } tonyp@3714: #endif // ASSERT tonyp@2717: if (G1PrintRegionLivenessInfo) { tonyp@2717: G1PrintRegionLivenessInfoClosure cl(gclog_or_tty, "Post-Sorting"); tonyp@3714: for (uint i = 0; i < _length; ++i) { tonyp@3714: HeapRegion* r = regions_at(i); tonyp@2717: cl.doHeapRegion(r); ysr@777: } ysr@777: } tonyp@3714: verify(); ysr@777: } ysr@777: tonyp@3539: tonyp@3714: void CollectionSetChooser::add_region(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!"); tonyp@3714: _regions.append(hr); tonyp@3539: _length++; tonyp@3714: _remaining_reclaimable_bytes += hr->reclaimable_bytes(); ysr@777: hr->calc_gc_efficiency(); ysr@777: } ysr@777: tonyp@3714: void CollectionSetChooser::prepare_for_par_region_addition(uint n_regions, tonyp@3714: uint chunk_size) { ysr@777: _first_par_unreserved_idx = 0; tonyp@3713: uint n_threads = (uint) ParallelGCThreads; jmasa@3294: if (UseDynamicNumberOfGCThreads) { jmasa@3294: assert(G1CollectedHeap::heap()->workers()->active_workers() > 0, jmasa@3294: "Should have been set earlier"); jmasa@3294: // This is defensive code. As the assertion above says, the number jmasa@3294: // of active threads should be > 0, but in case there is some path jmasa@3294: // or some improperly initialized variable with leads to no jmasa@3294: // active threads, protect against that in a product build. jmasa@3294: n_threads = MAX2(G1CollectedHeap::heap()->workers()->active_workers(), jmasa@3357: 1U); jmasa@3294: } tonyp@3714: uint max_waste = n_threads * chunk_size; tonyp@3714: // it should be aligned with respect to chunk_size tonyp@3714: uint aligned_n_regions = (n_regions + chunk_size - 1) / chunk_size * chunk_size; tonyp@3714: assert(aligned_n_regions % chunk_size == 0, "should be aligned"); tonyp@3714: regions_at_put_grow(aligned_n_regions + max_waste - 1, NULL); ysr@777: } ysr@777: tonyp@3714: uint CollectionSetChooser::claim_array_chunk(uint chunk_size) { tonyp@3714: uint res = (uint) Atomic::add((jint) chunk_size, tonyp@3714: (volatile jint*) &_first_par_unreserved_idx); tonyp@3714: assert(regions_length() > res + chunk_size - 1, ysr@777: "Should already have been expanded"); tonyp@3714: return res - chunk_size; ysr@777: } ysr@777: tonyp@3714: void CollectionSetChooser::set_region(uint index, HeapRegion* hr) { tonyp@3714: assert(regions_at(index) == NULL, "precondition"); ysr@777: assert(!hr->is_young(), "should not be young!"); tonyp@3714: regions_at_put(index, hr); ysr@777: hr->calc_gc_efficiency(); ysr@777: } ysr@777: tonyp@3714: void CollectionSetChooser::update_totals(uint region_num, tonyp@3714: size_t reclaimable_bytes) { tonyp@3539: // Only take the lock if we actually need to update the totals. tonyp@3539: if (region_num > 0) { tonyp@3539: assert(reclaimable_bytes > 0, "invariant"); tonyp@3539: // We could have just used atomics instead of taking the tonyp@3539: // lock. However, we currently don't have an atomic add for size_t. tonyp@3539: MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag); tonyp@3714: _length += region_num; tonyp@3714: _remaining_reclaimable_bytes += reclaimable_bytes; tonyp@3539: } else { tonyp@3539: assert(reclaimable_bytes == 0, "invariant"); tonyp@3539: } ysr@777: } ysr@777: tonyp@3714: void CollectionSetChooser::clear() { tonyp@3714: _regions.clear(); tonyp@3539: _curr_index = 0; tonyp@3539: _length = 0; tonyp@3714: _remaining_reclaimable_bytes = 0; ysr@777: };