/* * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
class MasterFreeRegionListChecker : public HeapRegionSetChecker { public: void check_mt_safety() { // Master Free List MT safety protocol: // (a) If we're at a safepoint, operations on the master free list // should be invoked by either the VM thread (which will serialize // them) or by the GC workers while holding the // FreeList_lock. // (b) If we're not at a safepoint, operations on the master free // list should be invoked while holding the Heap_lock.
if (SafepointSynchronize::is_at_safepoint()) {
guarantee(Thread::current()->is_VM_thread() ||
FreeList_lock->owned_by_self(), "master free list MT safety protocol at a safepoint");
} else {
guarantee(Heap_lock->owned_by_self(), "master free list MT safety protocol outside a safepoint");
}
} bool is_correct_type(HeapRegion* hr) { return hr->is_free(); } constchar* get_description() { return"Free Regions"; }
};
if (requested_node_index != G1NUMA::AnyNodeIndex && numa->is_enabled()) { // Try to allocate with requested node index.
hr = _free_list.remove_region_with_node_index(from_head, requested_node_index);
}
if (hr == NULL) { // If there's a single active node or we did not get a region from our requested node, // try without requested node index.
hr = _free_list.remove_region(from_head);
}
if (hr != NULL) {
assert(hr->next() == NULL, "Single region should not have next");
assert(is_available(hr->hrm_index()), "Must be committed");
HeapRegion* HeapRegionManager::allocate_humongous(uint num_regions) { // Special case a single region to avoid expensive search. if (num_regions == 1) { return allocate_free_region(HeapRegionType::Humongous, G1NUMA::AnyNodeIndex);
} return allocate_humongous_from_free_list(num_regions);
}
void HeapRegionManager::expand(uint start, uint num_regions, WorkerThreads* pretouch_workers) {
commit_regions(start, num_regions, pretouch_workers); for (uint i = start; i < start + num_regions; i++) {
HeapRegion* hr = _regions.get_by_index(i); if (hr == NULL) {
hr = new_heap_region(i);
OrderAccess::storestore();
_regions.set_by_index(i, hr);
_allocated_heapregions_length = MAX2(_allocated_heapregions_length, i + 1);
}
G1CollectedHeap::heap()->hr_printer()->commit(hr);
}
activate_regions(start, num_regions);
}
void HeapRegionManager::commit_regions(uint index, size_t num_regions, WorkerThreads* pretouch_workers) {
guarantee(num_regions > 0, "Must commit more than zero regions");
guarantee(num_regions <= available(), "Cannot commit more than the maximum amount of regions");
void HeapRegionManager::uncommit_regions(uint start, uint num_regions) {
guarantee(num_regions > 0, "No point in calling this for zero regions");
uint end = start + num_regions;
G1HRPrinter* printer = G1CollectedHeap::heap()->hr_printer(); if (printer->is_active()) { for (uint i = start; i < end; i++) { // Can't use at() here since region is no longer marked available.
HeapRegion* hr = _regions.get_by_index(i);
assert(hr != NULL, "Region should still be present");
printer->uncommit(hr);
}
}
void HeapRegionManager::initialize_regions(uint start, uint num_regions) { for (uint i = start; i < start + num_regions; i++) {
assert(is_available(i), "Just made region %u available but is apparently not.", i);
HeapRegion* hr = at(i);
void HeapRegionManager::deactivate_regions(uint start, uint num_regions) {
assert(num_regions > 0, "Need to specify at least one region to uncommit, tried to uncommit zero regions at %u", start);
assert(length() >= num_regions, "pre-condition");
// Reset NUMA index to and print state change.
uint end = start + num_regions; for (uint i = start; i < end; i++) {
HeapRegion* hr = at(i);
hr->set_node_index(G1NUMA::UnknownNodeIndex);
G1CollectedHeap::heap()->hr_printer()->inactive(hr);
}
_committed_map.deactivate(start, end);
}
void HeapRegionManager::clear_auxiliary_data_structures(uint start, uint num_regions) { // Signal marking bitmaps to clear the given regions.
_bitmap_mapper->signal_mapping_changed(start, num_regions); // Signal G1BlockOffsetTable to clear the given regions.
_bot_mapper->signal_mapping_changed(start, num_regions); // Signal G1CardTable to clear the given regions.
_cardtable_mapper->signal_mapping_changed(start, num_regions); // Signal G1CardCounts to clear the given regions.
_card_counts_mapper->signal_mapping_changed(start, num_regions);
}
uint HeapRegionManager::uncommit_inactive_regions(uint limit) {
assert(limit > 0, "Need to specify at least one region to uncommit");
uint uncommitted = 0;
uint offset = 0; do {
MutexLocker uc(Uncommit_lock, Mutex::_no_safepoint_check_flag);
HeapRegionRange range = _committed_map.next_inactive_range(offset); // No more regions available for uncommit. Return the number of regions // already uncommitted or 0 if there were no longer any inactive regions. if (range.length() == 0) { return uncommitted;
}
uint HeapRegionManager::expand_by(uint num_regions, WorkerThreads* pretouch_workers) {
assert(num_regions > 0, "Must expand at least 1 region");
// First "undo" any requests to uncommit memory concurrently by // reverting such regions to being available.
uint expanded = expand_inactive(num_regions);
// Commit more regions if needed. if (expanded < num_regions) {
expanded += expand_any(num_regions - expanded, pretouch_workers);
}
verify_optional(); return expanded;
}
void HeapRegionManager::expand_exact(uint start, uint num_regions, WorkerThreads* pretouch_workers) {
assert(num_regions != 0, "Need to request at least one region");
uint end = start + num_regions;
for (uint i = start; i < end; i++) { // First check inactive. If the regions is inactive, try to reactivate it // before it get uncommitted by the G1SeriveThread. if (_committed_map.inactive(i)) { // Need to grab the lock since this can be called by a java thread // doing humongous allocations.
MutexLocker uc(Uncommit_lock, Mutex::_no_safepoint_check_flag); // State might change while getting the lock. if (_committed_map.inactive(i)) {
reactivate_regions(i, 1);
}
} // Not else-if to catch the case where the inactive region was uncommitted // while waiting to get the lock. if (!_committed_map.active(i)) {
expand(i, 1, pretouch_workers);
}
assert(at(i)->is_free(), "Region must be free at this point");
}
if (available() >= 1) { for (uint i = 0; i < reserved_length(); i++) { if (is_available(i)) { // Already in use continue continue;
} // Always save the candidate so we can expand later on.
expand_candidate = i; if (is_on_preferred_index(expand_candidate, preferred_index)) { // We have found a candidate on the preferred node, break. break;
}
}
}
if (expand_candidate == UINT_MAX) { // No regions left, expand failed. return 0;
}
#ifdef ASSERT void HeapRegionManager::assert_contiguous_range(uint start, uint num_regions) { // General sanity check, regions found should either be available and empty // or not available so that we can make them available and use them. for (uint i = start; i < (start + num_regions); i++) {
HeapRegion* hr = _regions.get_by_index(i);
assert(!is_available(i) || hr->is_free(), "Found region sequence starting at " UINT32_FORMAT ", length " UINT32_FORMAT " that is not free at " UINT32_FORMAT ". Hr is " PTR_FORMAT ", type is %s",
start, num_regions, i, p2i(hr), hr->get_type_str());
}
} #endif
uint HeapRegionManager::find_contiguous_in_range(uint start, uint end, uint num_regions) {
assert(start <= end, "precondition");
assert(num_regions >= 1, "precondition");
uint candidate = start; // First region in candidate sequence.
uint unchecked = candidate; // First unchecked region in candidate. // While the candidate sequence fits in the range... while (num_regions <= (end - candidate)) { // Walk backward over the regions for the current candidate. for (uint i = candidate + num_regions - 1; true; --i) { if (is_available(i) && !at(i)->is_free()) { // Region i can't be used, so restart with i+1 as the start // of a new candidate sequence, and with the region after the // old candidate sequence being the first unchecked region.
unchecked = candidate + num_regions;
candidate = i + 1; break;
} elseif (i == unchecked) { // All regions of candidate sequence have passed check.
assert_contiguous_range(candidate, num_regions); return candidate;
}
}
} return G1_NO_HRM_INDEX;
}
do {
range = _committed_map.next_active_range(range.end());
candidate = find_contiguous_in_range(range.start(), range.end(), num_regions);
} while (candidate == G1_NO_HRM_INDEX && range.end() < reserved_length());
return candidate;
}
uint HeapRegionManager::find_contiguous_allow_expand(uint num_regions) { // Check if we can actually satisfy the allocation. if (num_regions > available()) { return G1_NO_HRM_INDEX;
} // Find any candidate. return find_contiguous_in_range(0, reserved_length(), num_regions);
}
HeapRegion* HeapRegionManager::next_region_in_heap(const HeapRegion* r) const {
guarantee(r != NULL, "Start region must be a valid region");
guarantee(is_available(r->hrm_index()), "Trying to iterate starting from region %u which is not in the heap", r->hrm_index()); for (uint i = r->hrm_index() + 1; i < _allocated_heapregions_length; i++) {
HeapRegion* hr = _regions.get_by_index(i); if (is_available(i)) { return hr;
}
} return NULL;
}
void HeapRegionManager::iterate(HeapRegionClosure* blk) const {
uint len = reserved_length();
for (uint i = 0; i < len; i++) { if (!is_available(i)) { continue;
}
guarantee(at(i) != NULL, "Tried to access region %u that has a NULL HeapRegion*", i); bool res = blk->do_heap_region(at(i)); if (res) {
blk->set_incomplete(); return;
}
}
}
void HeapRegionManager::iterate(HeapRegionIndexClosure* blk) const {
uint len = reserved_length();
for (uint i = 0; i < len; i++) { if (!is_available(i)) { continue;
} bool res = blk->do_heap_region_index(i); if (res) {
blk->set_incomplete(); return;
}
}
}
uint HeapRegionManager::find_highest_free(bool* expanded) { // Loop downwards from the highest region index, looking for an // entry which is either free or not yet committed. If not yet // committed, expand at that index. for (uint curr = reserved_length(); curr-- > 0;) {
HeapRegion *hr = _regions.get_by_index(curr); if (hr == NULL || !is_available(curr)) { // Found uncommitted and free region, expand to make it available for use.
expand_exact(curr, 1, NULL);
assert(at(curr)->is_free(), "Region (%u) must be available and free after expand", curr);
// Ensure that each G1 region in the range is free, returning false if not. // Commit those that are not yet available, and keep count. for (uint curr_index = start_index; curr_index <= last_index; curr_index++) { if (!is_available(curr_index)) {
commits++;
expand_exact(curr_index, 1, pretouch_workers);
}
HeapRegion* curr_region = _regions.get_by_index(curr_index); if (!curr_region->is_free()) { returnfalse;
}
}
void HeapRegionManager::par_iterate(HeapRegionClosure* blk, HeapRegionClaimer* hrclaimer, const uint start_index) const { // Every worker will actually look at all regions, skipping over regions that // are currently not committed. // This also (potentially) iterates over regions newly allocated during GC. This // is no problem except for some extra work. const uint n_regions = hrclaimer->n_regions(); for (uint count = 0; count < n_regions; count++) { const uint index = (start_index + count) % n_regions;
assert(index < n_regions, "sanity"); // Skip over unavailable regions if (!is_available(index)) { continue;
}
HeapRegion* r = _regions.get_by_index(index); // We'll ignore regions already claimed. if (hrclaimer->is_region_claimed(index)) { continue;
} // OK, try to claim it if (!hrclaimer->claim_region(index)) { continue;
} bool res = blk->do_heap_region(r); if (res) { return;
}
}
}
uint HeapRegionManager::shrink_by(uint num_regions_to_remove) {
assert(length() > 0, "the region sequence should not be empty");
assert(length() <= _allocated_heapregions_length, "invariant");
assert(_allocated_heapregions_length > 0, "we should have at least one region committed");
assert(num_regions_to_remove < length(), "We should never remove all regions");
void HeapRegionManager::shrink_at(uint index, size_t num_regions) { #ifdef ASSERT for (uint i = index; i < (index + num_regions); i++) {
assert(is_available(i), "Expected available region at index %u", i);
assert(at(i)->is_empty(), "Expected empty region at index %u", i);
assert(at(i)->is_free(), "Expected free region at index %u", i);
} #endif // Mark regions as inactive making them ready for uncommit.
deactivate_regions(index, (uint) num_regions);
}
bool prev_committed = true;
uint num_committed = 0;
HeapWord* prev_end = heap_bottom(); for (uint i = 0; i < _allocated_heapregions_length; i++) { if (!is_available(i)) {
prev_committed = false; continue;
}
num_committed++;
HeapRegion* hr = _regions.get_by_index(i);
guarantee(hr != NULL, "invariant: i: %u", i);
guarantee(!prev_committed || hr->bottom() == prev_end, "invariant i: %u " HR_FORMAT " prev_end: " PTR_FORMAT,
i, HR_FORMAT_PARAMS(hr), p2i(prev_end));
guarantee(hr->hrm_index() == i, "invariant: i: %u hrm_index(): %u", i, hr->hrm_index()); // Asserts will fire if i is >= _length
HeapWord* addr = hr->bottom();
guarantee(addr_to_region(addr) == hr, "sanity"); // We cannot check whether the region is part of a particular set: at the time // this method may be called, we have only completed allocation of the regions, // but not put into a region set.
prev_committed = true;
prev_end = hr->end();
} for (uint i = _allocated_heapregions_length; i < reserved_length(); i++) {
guarantee(_regions.get_by_index(i) == NULL, "invariant i: %u", i);
}
guarantee(num_committed == length(), "Found %u committed regions, but should be %u", num_committed, length());
_free_list.verify();
}
// Each worker creates a free list for a chunk of the heap. The chunks won't // be overlapping so we don't need to do any claiming. void work(uint worker_id) {
Ticks start_time = Ticks::now();
EventGCPhaseParallel event;
// If start is outside the heap, this worker has nothing to do. if (start > end) { return;
}
FreeRegionList *free_list = worker_freelist(worker_id); for (uint i = start; i < end; i++) {
HeapRegion *region = _hrm->at_or_null(i); if (region != NULL && region->is_free()) { // Need to clear old links to allow to be added to new freelist.
region->unlink_from_list();
free_list->add_to_tail(region);
}
}
log_debug(gc, ergo)("Running %s using %u workers for rebuilding free list of regions",
task.name(), num_workers);
workers->run_task(&task, num_workers);
// Link the partial free lists together.
Ticks serial_time = Ticks::now(); for (uint worker = 0; worker < num_workers; worker++) {
_free_list.append_ordered(task.worker_freelist(worker));
}
G1CollectedHeap::heap()->phase_times()->record_serial_rebuild_freelist_time_ms((Ticks::now() - serial_time).seconds() * 1000.0);
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.