/* * 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. *
*/
// INVARIANTS/NOTES // // All allocation activity covered by the G1CollectedHeap interface is // serialized by acquiring the HeapLock. This happens in mem_allocate // and allocate_new_tlab, which are the "entry" points to the // allocation code from the rest of the JVM. (Note that this does not // apply to TLAB allocation, which is not part of this interface: it // is done by clients of this interface.)
void G1RegionMappingChangedListener::on_commit(uint start_idx, size_t num_regions, bool zero_filled) { // The from card cache is not the memory that is actually committed. So we cannot // take advantage of the zero_filled parameter.
reset_from_card_cache(start_idx, num_regions);
}
uint G1CollectedHeap::get_chunks_per_region() {
uint log_region_size = HeapRegion::LogOfHRGrainBytes; // Limit the expected input values to current known possible values of the // (log) region size. Adjust as necessary after testing if changing the permissible // values for region size.
assert(log_region_size >= 20 && log_region_size <= 29, "expected value in [20,29], but got %u", log_region_size); return 1u << (log_region_size / 2 - 4);
}
HeapRegion* G1CollectedHeap::new_region(size_t word_size,
HeapRegionType type, bool do_expand,
uint node_index) {
assert(!is_humongous(word_size) || word_size <= HeapRegion::GrainWords, "the only time we use this to allocate a humongous region is " "when we are allocating a single humongous region");
HeapRegion* res = _hrm.allocate_free_region(type, node_index);
if (res == NULL && do_expand) { // Currently, only attempts to allocate GC alloc regions set // do_expand to true. So, we should only reach here during a // safepoint.
assert(SafepointSynchronize::is_at_safepoint(), "invariant");
assert(word_size * HeapWordSize < HeapRegion::GrainBytes, "This kind of expansion should never be more than one region. Size: " SIZE_FORMAT,
word_size * HeapWordSize); if (expand_single_region(node_index)) { // Given that expand_single_region() succeeded in expanding the heap, and we // always expand the heap by an amount aligned to the heap // region size, the free list should in theory not be empty. // In either case allocate_free_region() will check for NULL.
res = _hrm.allocate_free_region(type, node_index);
}
} return res;
}
// Index of last region in the series.
uint first = first_hr->hrm_index();
uint last = first + num_regions - 1;
// We need to initialize the region(s) we just discovered. This is // a bit tricky given that it can happen concurrently with // refinement threads refining cards on these regions and // potentially wanting to refine the BOT as they are scanning // those cards (this can happen shortly after a cleanup; see CR // 6991377). So we have to set up the region(s) carefully and in // a specific order.
// The word size sum of all the regions we will allocate.
size_t word_size_sum = (size_t) num_regions * HeapRegion::GrainWords;
assert(word_size <= word_size_sum, "sanity");
// The passed in hr will be the "starts humongous" region. The header // of the new object will be placed at the bottom of this region.
HeapWord* new_obj = first_hr->bottom(); // This will be the new top of the new object.
HeapWord* obj_top = new_obj + word_size;
// First, we need to zero the header of the space that we will be // allocating. When we update top further down, some refinement // threads might try to scan the region. By zeroing the header we // ensure that any thread that will try to scan the region will // come across the zero klass word and bail out. // // NOTE: It would not have been correct to have used // CollectedHeap::fill_with_object() and make the space look like // an int array. The thread that is doing the allocation will // later update the object header to a potentially different array // type and, for a very short period of time, the klass and length // fields will be inconsistent. This could cause a refinement // thread to calculate the object size incorrectly.
Copy::fill_to_words(new_obj, oopDesc::header_size(), 0);
// Next, pad out the unused tail of the last region with filler // objects, for improved usage accounting. // How many words we use for filler objects.
size_t word_fill_size = word_size_sum - word_size;
// How many words memory we "waste" which cannot hold a filler object.
size_t words_not_fillable = 0;
if (word_fill_size >= min_fill_size()) {
fill_with_objects(obj_top, word_fill_size);
} elseif (word_fill_size > 0) { // We have space to fill, but we cannot fit an object there.
words_not_fillable = word_fill_size;
word_fill_size = 0;
}
// We will set up the first region as "starts humongous". This // will also update the BOT covering all the regions to reflect // that there is a single object that starts at the bottom of the // first region.
first_hr->set_starts_humongous(obj_top, word_fill_size);
_policy->remset_tracker()->update_at_allocate(first_hr); // Then, if there are any, we will set up the "continues // humongous" regions.
HeapRegion* hr = NULL; for (uint i = first + 1; i <= last; ++i) {
hr = region_at(i);
hr->set_continues_humongous(first_hr);
_policy->remset_tracker()->update_at_allocate(hr);
}
// Up to this point no concurrent thread would have been able to // do any scanning on any region in this series. All the top // fields still point to bottom, so the intersection between // [bottom,top] and [card_start,card_end] will be empty. Before we // update the top fields, we'll do a storestore to make sure that // no thread sees the update to top before the zeroing of the // object header and the BOT initialization.
OrderAccess::storestore();
// Now, we will update the top fields of the "continues humongous" // regions except the last one. for (uint i = first; i < last; ++i) {
hr = region_at(i);
hr->set_top(hr->end());
}
hr = region_at(last); // If we cannot fit a filler object, we must set top to the end // of the humongous object, otherwise we cannot iterate the heap // and the BOT will not be complete.
hr->set_top(hr->end() - words_not_fillable);
assert(hr->bottom() < obj_top && obj_top <= hr->end(), "obj_top should be in last region");
for (uint i = first; i <= last; ++i) {
hr = region_at(i);
_humongous_set.add(hr);
_hr_printer.alloc(hr);
}
return new_obj;
}
size_t G1CollectedHeap::humongous_obj_size_in_regions(size_t word_size) {
assert(is_humongous(word_size), "Object of size " SIZE_FORMAT " must be humongous here", word_size); return align_up(word_size, HeapRegion::GrainWords) / HeapRegion::GrainWords;
}
// If could fit into free regions w/o expansion, try. // Otherwise, if can expand, do so. // Otherwise, if using ex regions might help, try with ex given back.
HeapWord* G1CollectedHeap::humongous_obj_allocate(size_t word_size) {
assert_heap_locked_or_at_safepoint(true/* should_be_vm_thread */);
// Policy: First try to allocate a humongous object in the free list.
HeapRegion* humongous_start = _hrm.allocate_humongous(obj_regions); if (humongous_start == NULL) { // Policy: We could not find enough regions for the humongous object in the // free list. Look through the heap to find a mix of free and uncommitted regions. // If so, expand the heap and allocate the humongous object.
humongous_start = _hrm.expand_and_allocate_humongous(obj_regions); if (humongous_start != NULL) { // We managed to find a region by expanding the heap.
log_debug(gc, ergo, heap)("Heap expansion (humongous allocation request). Allocation request: " SIZE_FORMAT "B",
word_size * HeapWordSize);
policy()->record_new_heap_size(num_regions());
} else { // Policy: Potentially trigger a defragmentation GC.
}
}
HeapWord* result = NULL; if (humongous_start != NULL) {
result = humongous_obj_allocate_initialize_regions(humongous_start, obj_regions, word_size);
assert(result != NULL, "it should always return a valid result");
// A successful humongous object allocation changes the used space // information of the old generation so we need to recalculate the // sizes and update the jstat counters here.
monitoring_support()->update_sizes();
}
_verifier->verify_region_sets_optional();
return result;
}
HeapWord* G1CollectedHeap::allocate_new_tlab(size_t min_size,
size_t requested_size,
size_t* actual_size) {
assert_heap_not_locked_and_not_at_safepoint();
assert(!is_humongous(requested_size), "we do not allow humongous TLABs");
HeapWord* G1CollectedHeap::attempt_allocation_slow(size_t word_size) {
ResourceMark rm; // For retrieving the thread names in log messages.
// Make sure you read the note in attempt_allocation_humongous().
assert_heap_not_locked_and_not_at_safepoint();
assert(!is_humongous(word_size), "attempt_allocation_slow() should not " "be called for humongous allocation requests");
// We should only get here after the first-level allocation attempt // (attempt_allocation()) failed to allocate.
// We will loop until a) we manage to successfully perform the // allocation or b) we successfully schedule a collection which // fails to perform the allocation. b) is the only case when we'll // return NULL.
HeapWord* result = NULL; for (uint try_count = 1, gclocker_retry_count = 0; /* we'll return */; try_count += 1) { bool should_try_gc; bool preventive_collection_required = false;
uint gc_count_before;
{
MutexLocker x(Heap_lock);
// Now that we have the lock, we first retry the allocation in case another // thread changed the region while we were waiting to acquire the lock.
size_t actual_size;
result = _allocator->attempt_allocation(word_size, word_size, &actual_size); if (result != NULL) { return result;
}
preventive_collection_required = policy()->preventive_collection_required(1); if (!preventive_collection_required) { // We've already attempted a lock-free allocation above, so we don't want to // do it again. Let's jump straight to replacing the active region.
result = _allocator->attempt_allocation_using_new_region(word_size); if (result != NULL) { return result;
}
// If the GCLocker is active and we are bound for a GC, try expanding young gen. // This is different to when only GCLocker::needs_gc() is set: try to avoid // waiting because the GCLocker is active to not wait too long. if (GCLocker::is_active_and_needs_gc() && policy()->can_expand_young_list()) { // No need for an ergo message here, can_expand_young_list() does this when // it returns true.
result = _allocator->attempt_allocation_force(word_size); if (result != NULL) { return result;
}
}
}
// Only try a GC if the GCLocker does not signal the need for a GC. Wait until // the GCLocker initiated GC has been performed and then retry. This includes // the case when the GC Locker is not active but has not been performed.
should_try_gc = !GCLocker::needs_gc(); // Read the GC count while still holding the Heap_lock.
gc_count_before = total_collections();
}
if (should_try_gc) {
GCCause::Cause gc_cause = preventive_collection_required ? GCCause::_g1_preventive_collection
: GCCause::_g1_inc_collection_pause; bool succeeded;
result = do_collection_pause(word_size, gc_count_before, &succeeded, gc_cause); if (result != NULL) {
assert(succeeded, "only way to get back a non-NULL result");
log_trace(gc, alloc)("%s: Successfully scheduled collection returning " PTR_FORMAT,
Thread::current()->name(), p2i(result)); return result;
}
if (succeeded) { // We successfully scheduled a collection which failed to allocate. No // point in trying to allocate further. We'll just return NULL.
log_trace(gc, alloc)("%s: Successfully scheduled collection failing to allocate "
SIZE_FORMAT " words", Thread::current()->name(), word_size); return NULL;
}
log_trace(gc, alloc)("%s: Unsuccessfully scheduled collection allocating " SIZE_FORMAT " words",
Thread::current()->name(), word_size);
} else { // Failed to schedule a collection. if (gclocker_retry_count > GCLockerRetryAllocationCount) {
log_warning(gc, alloc)("%s: Retried waiting for GCLocker too often allocating "
SIZE_FORMAT " words", Thread::current()->name(), word_size); return NULL;
}
log_trace(gc, alloc)("%s: Stall until clear", Thread::current()->name()); // The GCLocker is either active or the GCLocker initiated // GC has not yet been performed. Stall until it is and // then retry the allocation.
GCLocker::stall_until_clear();
gclocker_retry_count += 1;
}
// We can reach here if we were unsuccessful in scheduling a // collection (because another thread beat us to it) or if we were // stalled due to the GC locker. In either can we should retry the // allocation attempt in case another thread successfully // performed a collection and reclaimed enough space. We do the // first attempt (without holding the Heap_lock) here and the // follow-on attempt will be at the start of the next loop // iteration (after taking the Heap_lock).
size_t dummy = 0;
result = _allocator->attempt_allocation(word_size, word_size, &dummy); if (result != NULL) { return result;
}
// Give a warning if we seem to be looping forever. if ((QueuedAllocationWarningCount > 0) &&
(try_count % QueuedAllocationWarningCount == 0)) {
log_warning(gc, alloc)("%s: Retried allocation %u times for " SIZE_FORMAT " words",
Thread::current()->name(), try_count, word_size);
}
}
ShouldNotReachHere(); return NULL;
}
void G1CollectedHeap::begin_archive_alloc_range(bool open) {
assert_at_safepoint_on_vm_thread();
assert(_archive_allocator == nullptr, "should not be initialized");
_archive_allocator = G1ArchiveAllocator::create_allocator(this, open);
}
bool G1CollectedHeap::is_archive_alloc_too_large(size_t word_size) { // Allocations in archive regions cannot be of a size that would be considered // humongous even for a minimum-sized region, because G1 region sizes/boundaries // may be different at archive-restore time. return word_size >= humongous_threshold_for(HeapRegion::min_region_size_in_words());
}
HeapWord* G1CollectedHeap::archive_mem_allocate(size_t word_size) {
assert_at_safepoint_on_vm_thread();
assert(_archive_allocator != nullptr, "_archive_allocator not initialized"); if (is_archive_alloc_too_large(word_size)) { return nullptr;
} return _archive_allocator->archive_mem_allocate(word_size);
}
// Call complete_archive to do the real work, filling in the MemRegion // array with the archive regions.
_archive_allocator->complete_archive(ranges, end_alignment_in_bytes); delete _archive_allocator;
_archive_allocator = nullptr;
}
bool G1CollectedHeap::check_archive_addresses(MemRegion* ranges, size_t count) {
assert(ranges != NULL, "MemRegion array NULL");
assert(count != 0, "No MemRegions provided");
MemRegion reserved = _hrm.reserved(); for (size_t i = 0; i < count; i++) { if (!reserved.contains(ranges[i].start()) || !reserved.contains(ranges[i].last())) { returnfalse;
}
} returntrue;
}
bool G1CollectedHeap::alloc_archive_regions(MemRegion* ranges,
size_t count, bool open) {
assert(!is_init_completed(), "Expect to be called at JVM init time");
assert(ranges != NULL, "MemRegion array NULL");
assert(count != 0, "No MemRegions provided");
MutexLocker x(Heap_lock);
// Temporarily disable pretouching of heap pages. This interface is used // when mmap'ing archived heap data in, so pre-touching is wasted.
FlagSetting fs(AlwaysPreTouch, false);
// For each specified MemRegion range, allocate the corresponding G1 // regions and mark them as archive regions. We expect the ranges // in ascending starting address order, without overlap. for (size_t i = 0; i < count; i++) {
MemRegion curr_range = ranges[i];
HeapWord* start_address = curr_range.start();
size_t word_size = curr_range.word_size();
HeapWord* last_address = curr_range.last();
size_t commits = 0;
// Check for ranges that start in the same G1 region in which the previous // range ended, and adjust the start address so we don't try to allocate // the same region again. If the current range is entirely within that // region, skip it, just adjusting the recorded top.
HeapRegion* start_region = _hrm.addr_to_region(start_address); if ((prev_last_region != NULL) && (start_region == prev_last_region)) {
start_address = start_region->end(); if (start_address > last_address) {
increase_used(word_size * HeapWordSize);
start_region->set_top(last_address + 1); continue;
}
start_region->set_top(start_address);
curr_range = MemRegion(start_address, last_address + 1);
start_region = _hrm.addr_to_region(start_address);
}
// Perform the actual region allocation, exiting if it fails. // Then note how much new space we have allocated. if (!_hrm.allocate_containing_regions(curr_range, &commits, workers())) { returnfalse;
}
increase_used(word_size * HeapWordSize); if (commits != 0) {
log_debug(gc, ergo, heap)("Attempt heap expansion (allocate archive regions). Total size: " SIZE_FORMAT "B",
HeapRegion::GrainWords * HeapWordSize * commits);
}
// Mark each G1 region touched by the range as archive, add it to // the old set, and set top.
HeapRegion* curr_region = _hrm.addr_to_region(start_address);
HeapRegion* last_region = _hrm.addr_to_region(last_address);
prev_last_region = last_region;
while (curr_region != NULL) {
assert(curr_region->is_empty() && !curr_region->is_pinned(), "Region already in use (index %u)", curr_region->hrm_index()); if (open) {
curr_region->set_open_archive();
} else {
curr_region->set_closed_archive();
}
_hr_printer.alloc(curr_region);
_archive_set.add(curr_region);
HeapWord* top;
HeapRegion* next_region; if (curr_region != last_region) {
top = curr_region->end();
next_region = _hrm.next_region_in_heap(curr_region);
} else {
top = last_address + 1;
next_region = NULL;
}
curr_region->set_top(top);
curr_region = next_region;
}
} returntrue;
}
void G1CollectedHeap::fill_archive_regions(MemRegion* ranges, size_t count) {
assert(!is_init_completed(), "Expect to be called at JVM init time");
assert(ranges != NULL, "MemRegion array NULL");
assert(count != 0, "No MemRegions provided");
MemRegion reserved = _hrm.reserved();
HeapWord *prev_last_addr = NULL;
HeapRegion* prev_last_region = NULL;
// For each MemRegion, create filler objects, if needed, in the G1 regions // that contain the address range. The address range actually within the // MemRegion will not be modified. That is assumed to have been initialized // elsewhere, probably via an mmap of archived heap data.
MutexLocker x(Heap_lock); for (size_t i = 0; i < count; i++) {
HeapWord* start_address = ranges[i].start();
HeapWord* last_address = ranges[i].last();
// Check for a range beginning in the same region in which the // previous one ended. if (start_region == prev_last_region) {
bottom_address = prev_last_addr + 1;
}
// Verify that the regions were all marked as archive regions by // alloc_archive_regions.
HeapRegion* curr_region = start_region; while (curr_region != NULL) {
guarantee(curr_region->is_archive(), "Expected archive region at index %u", curr_region->hrm_index()); if (curr_region != last_region) {
curr_region = _hrm.next_region_in_heap(curr_region);
} else {
curr_region = NULL;
}
}
// Fill the memory below the allocated range with dummy object(s), // if the region bottom does not match the range start, or if the previous // range ended within the same G1 region, and there is a gap.
assert(start_address >= bottom_address, "bottom address should not be greater than start address"); if (start_address > bottom_address) {
size_t fill_size = pointer_delta(start_address, bottom_address);
G1CollectedHeap::fill_with_objects(bottom_address, fill_size);
increase_used(fill_size * HeapWordSize);
}
}
}
inline HeapWord* G1CollectedHeap::attempt_allocation(size_t min_word_size,
size_t desired_word_size,
size_t* actual_word_size) {
assert_heap_not_locked_and_not_at_safepoint();
assert(!is_humongous(desired_word_size), "attempt_allocation() should not " "be called for humongous allocation requests");
HeapWord* result = _allocator->attempt_allocation(min_word_size, desired_word_size, actual_word_size);
if (result == NULL) {
*actual_word_size = desired_word_size;
result = attempt_allocation_slow(desired_word_size);
}
assert_heap_not_locked(); if (result != NULL) {
assert(*actual_word_size != 0, "Actual size must have been set here");
dirty_young_block(result, *actual_word_size);
} else {
*actual_word_size = 0;
}
return result;
}
void G1CollectedHeap::populate_archive_regions_bot_part(MemRegion* ranges, size_t count) {
assert(!is_init_completed(), "Expect to be called at JVM init time");
assert(ranges != NULL, "MemRegion array NULL");
assert(count != 0, "No MemRegions provided");
HeapWord* st = ranges[0].start();
HeapWord* last = ranges[count-1].last();
HeapRegion* hr_st = _hrm.addr_to_region(st);
HeapRegion* hr_last = _hrm.addr_to_region(last);
void G1CollectedHeap::dealloc_archive_regions(MemRegion* ranges, size_t count) {
assert(!is_init_completed(), "Expect to be called at JVM init time");
assert(ranges != NULL, "MemRegion array NULL");
assert(count != 0, "No MemRegions provided");
MemRegion reserved = _hrm.reserved();
HeapWord* prev_last_addr = NULL;
HeapRegion* prev_last_region = NULL;
size_t size_used = 0;
uint shrink_count = 0;
// For each Memregion, free the G1 regions that constitute it, and // notify mark-sweep that the range is no longer to be considered 'archive.'
MutexLocker x(Heap_lock); for (size_t i = 0; i < count; i++) {
HeapWord* start_address = ranges[i].start();
HeapWord* last_address = ranges[i].last();
// Check for ranges that start in the same G1 region in which the previous // range ended, and adjust the start address so we don't try to free // the same region again. If the current range is entirely within that // region, skip it. if (start_region == prev_last_region) {
start_address = start_region->end(); if (start_address > last_address) { continue;
}
start_region = _hrm.addr_to_region(start_address);
}
prev_last_region = last_region;
// After verifying that each region was marked as an archive region by // alloc_archive_regions, set it free and empty and uncommit it.
HeapRegion* curr_region = start_region; while (curr_region != NULL) {
guarantee(curr_region->is_archive(), "Expected archive region at index %u", curr_region->hrm_index());
uint curr_index = curr_region->hrm_index();
_archive_set.remove(curr_region);
curr_region->set_free();
curr_region->set_top(curr_region->bottom()); if (curr_region != last_region) {
curr_region = _hrm.next_region_in_heap(curr_region);
} else {
curr_region = NULL;
}
HeapWord* G1CollectedHeap::attempt_allocation_humongous(size_t word_size) {
ResourceMark rm; // For retrieving the thread names in log messages.
// The structure of this method has a lot of similarities to // attempt_allocation_slow(). The reason these two were not merged // into a single one is that such a method would require several "if // allocation is not humongous do this, otherwise do that" // conditional paths which would obscure its flow. In fact, an early // version of this code did use a unified method which was harder to // follow and, as a result, it had subtle bugs that were hard to // track down. So keeping these two methods separate allows each to // be more readable. It will be good to keep these two in sync as // much as possible.
assert_heap_not_locked_and_not_at_safepoint();
assert(is_humongous(word_size), "attempt_allocation_humongous() " "should only be called for humongous allocations");
// Humongous objects can exhaust the heap quickly, so we should check if we // need to start a marking cycle at each humongous object allocation. We do // the check before we do the actual allocation. The reason for doing it // before the allocation is that we avoid having to keep track of the newly // allocated memory while we do a GC. if (policy()->need_to_start_conc_mark("concurrent humongous allocation",
word_size)) {
collect(GCCause::_g1_humongous_allocation);
}
// We will loop until a) we manage to successfully perform the // allocation or b) we successfully schedule a collection which // fails to perform the allocation. b) is the only case when we'll // return NULL.
HeapWord* result = NULL; for (uint try_count = 1, gclocker_retry_count = 0; /* we'll return */; try_count += 1) { bool should_try_gc; bool preventive_collection_required = false;
uint gc_count_before;
{
MutexLocker x(Heap_lock);
size_t size_in_regions = humongous_obj_size_in_regions(word_size);
preventive_collection_required = policy()->preventive_collection_required((uint)size_in_regions); if (!preventive_collection_required) { // Given that humongous objects are not allocated in young // regions, we'll first try to do the allocation without doing a // collection hoping that there's enough space in the heap.
result = humongous_obj_allocate(word_size); if (result != NULL) {
policy()->old_gen_alloc_tracker()->
add_allocated_humongous_bytes_since_last_gc(size_in_regions * HeapRegion::GrainBytes); return result;
}
}
// Only try a GC if the GCLocker does not signal the need for a GC. Wait until // the GCLocker initiated GC has been performed and then retry. This includes // the case when the GC Locker is not active but has not been performed.
should_try_gc = !GCLocker::needs_gc(); // Read the GC count while still holding the Heap_lock.
gc_count_before = total_collections();
}
if (should_try_gc) {
GCCause::Cause gc_cause = preventive_collection_required ? GCCause::_g1_preventive_collection
: GCCause::_g1_humongous_allocation; bool succeeded;
result = do_collection_pause(word_size, gc_count_before, &succeeded, gc_cause); if (result != NULL) {
assert(succeeded, "only way to get back a non-NULL result");
log_trace(gc, alloc)("%s: Successfully scheduled collection returning " PTR_FORMAT,
Thread::current()->name(), p2i(result));
size_t size_in_regions = humongous_obj_size_in_regions(word_size);
policy()->old_gen_alloc_tracker()->
record_collection_pause_humongous_allocation(size_in_regions * HeapRegion::GrainBytes); return result;
}
if (succeeded) { // We successfully scheduled a collection which failed to allocate. No // point in trying to allocate further. We'll just return NULL.
log_trace(gc, alloc)("%s: Successfully scheduled collection failing to allocate "
SIZE_FORMAT " words", Thread::current()->name(), word_size); return NULL;
}
log_trace(gc, alloc)("%s: Unsuccessfully scheduled collection allocating " SIZE_FORMAT "",
Thread::current()->name(), word_size);
} else { // Failed to schedule a collection. if (gclocker_retry_count > GCLockerRetryAllocationCount) {
log_warning(gc, alloc)("%s: Retried waiting for GCLocker too often allocating "
SIZE_FORMAT " words", Thread::current()->name(), word_size); return NULL;
}
log_trace(gc, alloc)("%s: Stall until clear", Thread::current()->name()); // The GCLocker is either active or the GCLocker initiated // GC has not yet been performed. Stall until it is and // then retry the allocation.
GCLocker::stall_until_clear();
gclocker_retry_count += 1;
}
// We can reach here if we were unsuccessful in scheduling a // collection (because another thread beat us to it) or if we were // stalled due to the GC locker. In either can we should retry the // allocation attempt in case another thread successfully // performed a collection and reclaimed enough space. // Humongous object allocation always needs a lock, so we wait for the retry // in the next iteration of the loop, unlike for the regular iteration case. // Give a warning if we seem to be looping forever.
if ((QueuedAllocationWarningCount > 0) &&
(try_count % QueuedAllocationWarningCount == 0)) {
log_warning(gc, alloc)("%s: Retried allocation %u times for " SIZE_FORMAT " words",
Thread::current()->name(), try_count, word_size);
}
}
ShouldNotReachHere(); return NULL;
}
HeapWord* G1CollectedHeap::attempt_allocation_at_safepoint(size_t word_size, bool expect_null_mutator_alloc_region) {
assert_at_safepoint_on_vm_thread();
assert(!_allocator->has_mutator_alloc_region() || !expect_null_mutator_alloc_region, "the current alloc region was unexpectedly found to be non-NULL");
if (!is_humongous(word_size)) { return _allocator->attempt_allocation_locked(word_size);
} else {
HeapWord* result = humongous_obj_allocate(word_size); if (result != NULL && policy()->need_to_start_conc_mark("STW humongous allocation")) {
collector_state()->set_initiate_conc_mark_if_possible(true);
} return result;
}
ShouldNotReachHere();
}
class PostCompactionPrinterClosure: public HeapRegionClosure { private:
G1HRPrinter* _hr_printer; public: bool do_heap_region(HeapRegion* hr) {
assert(!hr->is_young(), "not expecting to find young regions");
_hr_printer->post_compaction(hr); returnfalse;
}
void G1CollectedHeap::print_heap_after_full_collection() { // Post collection region logging. // We should do this after we potentially resize the heap so // that all the COMMIT / UNCOMMIT events are generated before // the compaction events. if (_hr_printer.is_active()) {
PostCompactionPrinterClosure cl(hr_printer());
heap_region_iterate(&cl);
}
}
bool G1CollectedHeap::abort_concurrent_cycle() { // Disable discovery and empty the discovered lists // for the CM ref processor.
_ref_processor_cm->disable_discovery();
_ref_processor_cm->abandon_partial_discovery();
_ref_processor_cm->verify_no_references_recorded();
// Abandon current iterations of concurrent marking and concurrent // refinement, if any are in progress. return concurrent_mark()->concurrent_cycle_abort();
}
void G1CollectedHeap::prepare_heap_for_full_collection() { // Make sure we'll choose a new allocation region afterwards.
_allocator->release_mutator_alloc_regions();
_allocator->abandon_gc_alloc_regions();
// We may have added regions to the current incremental collection // set between the last GC or pause and now. We need to clear the // incremental collection set and then start rebuilding it afresh // after this full GC.
abandon_collection_set(collection_set());
void G1CollectedHeap::prepare_heap_for_mutators() { // Delete metaspaces for unloaded class loaders and clean up loader_data graph
ClassLoaderDataGraph::purge(/*at_safepoint*/true);
DEBUG_ONLY(MetaspaceUtils::verify();)
// Prepare heap for normal collections.
assert(num_free_regions() == 0, "we should not have added any free regions");
rebuild_region_sets(false/* free_list_only */);
abort_refinement();
resize_heap_if_necessary();
uncommit_regions_if_necessary();
// Rebuild the code root lists for each region
rebuild_code_roots();
// Start a new incremental collection set for the next pause
start_new_collection_set();
_allocator->init_mutator_alloc_regions();
// Post collection state updates.
MetaspaceGC::compute_new_size();
}
void G1CollectedHeap::abort_refinement() { if (G1HotCardCache::use_cache()) {
_hot_card_cache->reset_hot_cache();
}
// Discard all remembered set updates and reset refinement statistics.
G1BarrierSet::dirty_card_queue_set().abandon_logs_and_stats();
assert(G1BarrierSet::dirty_card_queue_set().num_cards() == 0, "DCQS should be empty");
concurrent_refine()->get_and_reset_refinement_stats();
}
// At this point there should be no regions in the // entire heap tagged as young.
assert(check_young_list_empty(), "young list should be empty at this point");
// Note: since we've just done a full GC, concurrent // marking is no longer active. Therefore we need not // re-enable reference discovery for the CM ref processor. // That will be done at the start of the next marking cycle. // We also know that the STW processor should no longer // discover any new references.
assert(!_ref_processor_stw->discovery_enabled(), "Postcondition");
assert(!_ref_processor_cm->discovery_enabled(), "Postcondition");
_ref_processor_stw->verify_no_references_recorded();
_ref_processor_cm->verify_no_references_recorded();
}
// Full collection was successfully completed. returntrue;
}
void G1CollectedHeap::do_full_collection(bool clear_all_soft_refs) { // Currently, there is no facility in the do_full_collection(bool) API to notify // the caller that the collection did not succeed (e.g., because it was locked // out by the GC locker). So, right now, we'll ignore the return value. // When clear_all_soft_refs is set we want to do a maximal compaction // not leaving any dead wood. bool do_maximal_compaction = clear_all_soft_refs; bool dummy = do_full_collection(true, /* explicit_gc */
clear_all_soft_refs,
do_maximal_compaction);
}
bool G1CollectedHeap::upgrade_to_full_collection() {
GCCauseSetter compaction(this, GCCause::_g1_compaction_pause);
log_info(gc, ergo)("Attempting full compaction clearing soft references"); bool success = do_full_collection(false/* explicit gc */, true/* clear_all_soft_refs */, false/* do_maximal_compaction */); // do_full_collection only fails if blocked by GC locker and that can't // be the case here since we only call this when already completed one gc.
assert(success, "invariant"); return success;
}
// In a G1 heap, we're supposed to keep allocation from failing by // incremental pauses. Therefore, at least for now, we'll favor // expansion over collection. (This might change in the future if we can // do something smarter than full collection to satisfy a failed alloc.)
result = expand_and_allocate(word_size); if (result != NULL) { return result;
}
if (do_gc) {
GCCauseSetter compaction(this, GCCause::_g1_compaction_pause); // Expansion didn't work, we'll try to do a Full GC. // If maximal_compaction is set we clear all soft references and don't // allow any dead wood to be left on the heap. if (maximal_compaction) {
log_info(gc, ergo)("Attempting maximal full compaction clearing soft references");
} else {
log_info(gc, ergo)("Attempting full compaction");
}
*gc_succeeded = do_full_collection(false, /* explicit_gc */
maximal_compaction /* clear_all_soft_refs */ ,
maximal_compaction /* do_maximal_compaction */);
}
// Attempts to allocate followed by Full GC.
HeapWord* result =
satisfy_failed_allocation_helper(word_size, true, /* do_gc */ false, /* maximum_collection */ false, /* expect_null_mutator_alloc_region */
succeeded);
if (result != NULL || !*succeeded) { return result;
}
// Attempts to allocate followed by Full GC that will collect all soft references.
result = satisfy_failed_allocation_helper(word_size, true, /* do_gc */ true, /* maximum_collection */ true, /* expect_null_mutator_alloc_region */
succeeded);
if (result != NULL || !*succeeded) { return result;
}
// Attempts to allocate, no GC
result = satisfy_failed_allocation_helper(word_size, false, /* do_gc */ false, /* maximum_collection */ true, /* expect_null_mutator_alloc_region */
succeeded);
if (result != NULL) { return result;
}
assert(!soft_ref_policy()->should_clear_all_soft_refs(), "Flag should have been handled and cleared prior to this point");
// What else? We might try synchronous finalization later. If the total // space available is large enough for the allocation, then a more // complete compaction phase than we've tried so far might be // appropriate. return NULL;
}
// Attempting to expand the heap sufficiently // to support an allocation of the given "word_size". If // successful, perform the allocation and return the address of the // allocated block, or else "NULL".
if (is_maximal_no_gc()) {
log_debug(gc, ergo, heap)("Did not expand the heap (heap already fully expanded)"); returnfalse;
}
double expand_heap_start_time_sec = os::elapsedTime();
uint regions_to_expand = (uint)(aligned_expand_bytes / HeapRegion::GrainBytes);
assert(regions_to_expand > 0, "Must expand by at least one region");
if (expanded_by > 0) {
size_t actual_expand_bytes = expanded_by * HeapRegion::GrainBytes;
assert(actual_expand_bytes <= aligned_expand_bytes, "post-condition");
policy()->record_new_heap_size(num_regions());
} else {
log_debug(gc, ergo, heap)("Did not expand the heap (heap expansion operation failed)");
// The expansion of the virtual storage space was unsuccessful. // Let's see if it was because we ran out of swap. if (G1ExitOnExpansionFailure &&
_hrm.available() >= regions_to_expand) { // We had head room...
vm_exit_out_of_memory(aligned_expand_bytes, OOM_MMAP_ERROR, "G1 heap expansion");
}
} return expanded_by > 0;
}
if (expanded_by == 0) {
assert(is_maximal_no_gc(), "Should be no regions left, available: %u", _hrm.available());
log_debug(gc, ergo, heap)("Did not expand the heap (heap already fully expanded)"); returnfalse;
}
// We should only reach here at the end of a Full GC or during Remark which // means we should not not be holding to any GC alloc regions. The method // below will make sure of that and do any remaining clean up.
_allocator->abandon_gc_alloc_regions();
// Instead of tearing down / rebuilding the free lists here, we // could instead use the remove_all_pending() method on free_list to // remove only the ones that we need to remove.
_hrm.remove_all_free_regions();
shrink_helper(shrink_bytes);
rebuild_region_sets(true/* free_list_only */);
class OldRegionSetChecker : public HeapRegionSetChecker { public: void check_mt_safety() { // Master Old Set MT safety protocol: // (a) If we're at a safepoint, operations on the master old set // should be invoked: // - by the VM thread (which will serialize them), or // - by the GC workers while holding the FreeList_lock, if we're // at a safepoint for an evacuation pause (this lock is taken // anyway when an GC alloc region is retired so that a new one // is allocated from the free list), or // - by the GC workers while holding the OldSets_lock, if we're at a // safepoint for a cleanup pause. // (b) If we're not at a safepoint, operations on the master old set // should be invoked while holding the Heap_lock.
if (SafepointSynchronize::is_at_safepoint()) {
guarantee(Thread::current()->is_VM_thread() ||
FreeList_lock->owned_by_self() || OldSets_lock->owned_by_self(), "master old set MT safety protocol at a safepoint");
} else {
guarantee(Heap_lock->owned_by_self(), "master old set MT safety protocol outside a safepoint");
}
} bool is_correct_type(HeapRegion* hr) { return hr->is_old(); } constchar* get_description() { return"Old Regions"; }
};
class ArchiveRegionSetChecker : public HeapRegionSetChecker { public: void check_mt_safety() {
guarantee(!Universe::is_fully_initialized() || SafepointSynchronize::is_at_safepoint(), "May only change archive regions during initialization or safepoint.");
} bool is_correct_type(HeapRegion* hr) { return hr->is_archive(); } constchar* get_description() { return"Archive Regions"; }
};
class HumongousRegionSetChecker : public HeapRegionSetChecker { public: void check_mt_safety() { // Humongous Set MT safety protocol: // (a) If we're at a safepoint, operations on the master humongous // set should be invoked by either the VM thread (which will // serialize them) or by the GC workers while holding the // OldSets_lock. // (b) If we're not at a safepoint, operations on the master // humongous set should be invoked while holding the Heap_lock.
if (SafepointSynchronize::is_at_safepoint()) {
guarantee(Thread::current()->is_VM_thread() ||
OldSets_lock->owned_by_self(), "master humongous set MT safety protocol at a safepoint");
} else {
guarantee(Heap_lock->owned_by_self(), "master humongous set MT safety protocol outside a safepoint");
}
} bool is_correct_type(HeapRegion* hr) { return hr->is_humongous(); } constchar* get_description() { return"Humongous Regions"; }
};
// Override the default _filler_array_max_size so that no humongous filler // objects are created.
_filler_array_max_size = _humongous_object_threshold_in_words;
// Override the default _stack_chunk_max_size so that no humongous stack chunks are created
_stack_chunk_max_size = _humongous_object_threshold_in_words;
uint n_queues = ParallelGCThreads;
_task_queues = new G1ScannerTasksQueueSet(n_queues);
for (uint i = 0; i < n_queues; i++) {
G1ScannerTasksQueue* q = new G1ScannerTasksQueue();
_task_queues->register_queue(i, q);
}
jint G1CollectedHeap::initialize_service_thread() {
_service_thread = new G1ServiceThread(); if (_service_thread->osthread() == NULL) {
vm_shutdown_during_initialization("Could not create G1ServiceThread"); return JNI_ENOMEM;
} return JNI_OK;
}
jint G1CollectedHeap::initialize() {
// Necessary to satisfy locking discipline assertions.
MutexLocker x(Heap_lock);
// While there are no constraints in the GC code that HeapWordSize // be any particular value, there are multiple other areas in the // system which believe this to be true (e.g. oop->object_size in some // cases incorrectly returns the size in wordSize units rather than // HeapWordSize).
guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
// Ensure that the sizes are properly aligned.
Universe::check_alignment(init_byte_size, HeapRegion::GrainBytes, "g1 heap");
Universe::check_alignment(reserved_byte_size, HeapRegion::GrainBytes, "g1 heap");
Universe::check_alignment(reserved_byte_size, HeapAlignment, "g1 heap");
// Reserve the maximum.
// When compressed oops are enabled, the preferred heap base // is calculated by subtracting the requested size from the // 32Gb boundary and using the result as the base address for // heap reservation. If the requested size is not aligned to // HeapRegion::GrainBytes (i.e. the alignment that is passed // into the ReservedHeapSpace constructor) then the actual // base of the reserved heap may end up differing from the // address that was requested (i.e. the preferred heap base). // If this happens then we could end up using a non-optimal // compressed oops mode.
// Create the barrier set for the entire reserved region.
G1CardTable* ct = new G1CardTable(heap_rs.region());
ct->initialize();
G1BarrierSet* bs = new G1BarrierSet(ct);
bs->initialize();
assert(bs->is_a(BarrierSet::G1BarrierSet), "sanity");
BarrierSet::set_barrier_set(bs);
_card_table = ct;
// Do later initialization work for concurrent refinement.
_hot_card_cache->initialize(card_counts_storage);
// 6843694 - ensure that the maximum region index can fit // in the remembered set structures. const uint max_region_idx = (1U << (sizeof(RegionIdx_t)*BitsPerByte-1)) - 1;
guarantee((max_reserved_regions() - 1) <= max_region_idx, "too many regions");
// The G1FromCardCache reserves card with value 0 as "invalid", so the heap must not // start within the first card.
guarantee((uintptr_t)(heap_rs.base()) >= G1CardTable::card_size(), "Java heap must not start within the first card.");
G1FromCardCache::initialize(max_reserved_regions()); // Also create a G1 rem set.
_rem_set = new G1RemSet(this, _card_table, _hot_card_cache);
_rem_set->initialize(max_reserved_regions());
// Create the G1ConcurrentMark data structure and thread. // (Must do this late, so that "max_[reserved_]regions" is defined.)
_cm = new G1ConcurrentMark(this, bitmap_storage);
_cm_thread = _cm->cm_thread();
// Now expand into the initial heap size. if (!expand(init_byte_size, _workers)) {
vm_shutdown_during_initialization("Failed to allocate initial heap."); return JNI_ENOMEM;
}
// Perform any initialization actions delegated to the policy.
policy()->init(this, &_collection_set);
// Create and schedule the periodic gc task on the service thread.
_periodic_gc_task = new G1PeriodicGCTask("Periodic GC Task");
_service_thread->register_task(_periodic_gc_task);
_free_arena_memory_task = new G1MonotonicArenaFreeMemoryTask("Card Set Free Memory Task");
_service_thread->register_task(_free_arena_memory_task);
// Here we allocate the dummy HeapRegion that is required by the // G1AllocRegion class.
HeapRegion* dummy_region = _hrm.get_dummy_region();
// We'll re-use the same region whether the alloc region will // require BOT updates or not and, if it doesn't, then a non-young // region will complain that it cannot support allocations without // BOT updates. So we'll tag the dummy region as eden to avoid that.
dummy_region->set_eden(); // Make sure it's full.
dummy_region->set_top(dummy_region->end());
G1AllocRegion::setup(this, dummy_region);
_allocator->init_mutator_alloc_regions();
// Do create of the monitoring and management support so that // values in the heap have been properly initialized.
_monitoring_support = new G1MonitoringSupport(this);
void G1CollectedHeap::stop() { // Stop all concurrent threads. We do this to make sure these threads // do not continue to execute and access resources (e.g. logging) // that are destroyed during shutdown.
_cr->stop();
_service_thread->stop();
_cm_thread->stop();
}
void G1CollectedHeap::ref_processing_init() { // Reference processing in G1 currently works as follows: // // * There are two reference processor instances. One is // used to record and process discovered references // during concurrent marking; the other is used to // record and process references during STW pauses // (both full and incremental). // * Both ref processors need to 'span' the entire heap as // the regions in the collection set may be dotted around. // // * For the concurrent marking ref processor: // * Reference discovery is enabled at concurrent start. // * Reference discovery is disabled and the discovered // references processed etc during remarking. // * Reference discovery is MT (see below). // * Reference discovery requires a barrier (see below). // * Reference processing may or may not be MT // (depending on the value of ParallelRefProcEnabled // and ParallelGCThreads). // * A full GC disables reference discovery by the CM // ref processor and abandons any entries on it's // discovered lists. // // * For the STW processor: // * Non MT discovery is enabled at the start of a full GC. // * Processing and enqueueing during a full GC is non-MT. // * During a full GC, references are processed after marking. // // * Discovery (may or may not be MT) is enabled at the start // of an incremental evacuation pause. // * References are processed near the end of a STW evacuation pause. // * For both types of GC: // * Discovery is atomic - i.e. not concurrent. // * Reference discovery will not need a barrier.
// Concurrent Mark ref processor
_ref_processor_cm = new ReferenceProcessor(&_is_subject_to_discovery_cm,
ParallelGCThreads, // degree of mt processing // We discover with the gc worker threads during Remark, so both // thread counts must be considered for discovery.
MAX2(ParallelGCThreads, ConcGCThreads), // degree of mt discovery true, // Reference discovery is concurrent
&_is_alive_closure_cm); // is alive closure
// STW ref processor
_ref_processor_stw = new ReferenceProcessor(&_is_subject_to_discovery_stw,
ParallelGCThreads, // degree of mt processing
ParallelGCThreads, // degree of mt discovery false, // Reference discovery is not concurrent
&_is_alive_closure_stw); // is alive closure
}
// Computes the sum of the storage used by the various regions.
size_t G1CollectedHeap::used() const {
size_t result = _summary_bytes_used + _allocator->used_in_alloc_regions();
assert(_archive_allocator == nullptr, "must be, should not contribute to used"); return result;
}
bool G1CollectedHeap::is_user_requested_concurrent_full_gc(GCCause::Cause cause) { switch (cause) { case GCCause::_java_lang_system_gc: return ExplicitGCInvokesConcurrent; case GCCause::_dcmd_gc_run: return ExplicitGCInvokesConcurrent; case GCCause::_wb_conc_mark: returntrue; default : returnfalse;
}
}
bool G1CollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) { switch (cause) { case GCCause::_g1_humongous_allocation: returntrue; case GCCause::_g1_periodic_collection: return G1PeriodicGCInvokesConcurrent; case GCCause::_wb_breakpoint: returntrue; case GCCause::_codecache_GC_aggressive: returntrue; case GCCause::_codecache_GC_threshold: returntrue; default: return is_user_requested_concurrent_full_gc(cause);
}
}
#ifndef PRODUCT void G1CollectedHeap::allocate_dummy_regions() { // Let's fill up most of the region
size_t word_size = HeapRegion::GrainWords - 1024; // And as a result the region we'll allocate will be humongous.
guarantee(is_humongous(word_size), "sanity");
// _filler_array_max_size is set to humongous object threshold // but temporarily change it to use CollectedHeap::fill_with_object().
AutoModifyRestore<size_t> temporarily(_filler_array_max_size, word_size);
for (uintx i = 0; i < G1DummyRegionsPerGC; ++i) { // Let's use the existing mechanism for the allocation
HeapWord* dummy_obj = humongous_obj_allocate(word_size); if (dummy_obj != NULL) {
MemRegion mr(dummy_obj, word_size);
CollectedHeap::fill_with_object(mr);
} else { // If we can't allocate once, we probably cannot allocate // again. Let's get out of the loop. break;
}
}
} #endif// !PRODUCT
// We assume that if concurrent == true, then the caller is a // concurrent thread that was joined the Suspendible Thread // Set. If there's ever a cheap way to check this, we should add an // assert here.
// Given that this method is called at the end of a Full GC or of a // concurrent cycle, and those can be nested (i.e., a Full GC can // interrupt a concurrent cycle), the number of full collections // completed should be either one (in the case where there was no // nesting) or two (when a Full GC interrupted a concurrent cycle) // behind the number of full collections started.
// This is the case for the inner caller, i.e. a Full GC.
assert(concurrent ||
(_old_marking_cycles_started == _old_marking_cycles_completed + 1) ||
(_old_marking_cycles_started == _old_marking_cycles_completed + 2), "for inner caller (Full GC): _old_marking_cycles_started = %u " "is inconsistent with _old_marking_cycles_completed = %u",
_old_marking_cycles_started, _old_marking_cycles_completed);
// This is the case for the outer caller, i.e. the concurrent cycle.
assert(!concurrent ||
(_old_marking_cycles_started == _old_marking_cycles_completed + 1), "for outer caller (concurrent cycle): " "_old_marking_cycles_started = %u " "is inconsistent with _old_marking_cycles_completed = %u",
_old_marking_cycles_started, _old_marking_cycles_completed);
_old_marking_cycles_completed += 1; if (whole_heap_examined) { // Signal that we have completed a visit to all live objects.
record_whole_heap_examined_timestamp();
}
// We need to clear the "in_progress" flag in the CM thread before // we wake up any waiters (especially when ExplicitInvokesConcurrent // is set) so that if a waiter requests another System.gc() it doesn't // incorrectly see that a marking cycle is still in progress. if (concurrent) {
_cm_thread->set_idle();
}
// Notify threads waiting in System.gc() (with ExplicitGCInvokesConcurrent) // for a full GC to finish that their wait is over.
ml.notify_all();
}
for (uint i = 1; true; ++i) { // Try to schedule concurrent start evacuation pause that will // start a concurrent cycle.
LOG_COLLECT_CONCURRENTLY(cause, "attempt %u", i);
VM_G1TryInitiateConcMark op(gc_counter, cause);
VMThread::execute(&op);
// Request is trivially finished. if (cause == GCCause::_g1_periodic_collection) {
LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, op.gc_succeeded()); return op.gc_succeeded();
}
// If VMOp skipped initiating concurrent marking cycle because // we're terminating, then we're done. if (op.terminating()) {
LOG_COLLECT_CONCURRENTLY(cause, "skipped: terminating"); returnfalse;
}
// Lock to get consistent set of values.
uint old_marking_started_after;
uint old_marking_completed_after;
{
MutexLocker ml(Heap_lock); // Update gc_counter for retrying VMOp if needed. Captured here to be // consistent with the values we use below for termination tests. If // a retry is needed after a possible wait, and another collection // occurs in the meantime, it will cause our retry to be skipped and // we'll recheck for termination with updated conditions from that // more recent collection. That's what we want, rather than having // our retry possibly perform an unnecessary collection.
gc_counter = total_collections();
old_marking_started_after = _old_marking_cycles_started;
old_marking_completed_after = _old_marking_cycles_completed;
}
if (cause == GCCause::_wb_breakpoint) { if (op.gc_succeeded()) {
LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, true); returntrue;
} // When _wb_breakpoint there can't be another cycle or deferred.
assert(!op.cycle_already_in_progress(), "invariant");
assert(!op.whitebox_attached(), "invariant"); // Concurrent cycle attempt might have been cancelled by some other // collection, so retry. Unlike other cases below, we want to retry // even if cancelled by a STW full collection, because we really want // to start a concurrent cycle. if (old_marking_started_before != old_marking_started_after) {
LOG_COLLECT_CONCURRENTLY(cause, "ignoring STW full GC");
old_marking_started_before = old_marking_started_after;
}
} elseif (!GCCause::is_user_requested_gc(cause)) { // For an "automatic" (not user-requested) collection, we just need to // ensure that progress is made. // // Request is finished if any of // (1) the VMOp successfully performed a GC, // (2) a concurrent cycle was already in progress, // (3) whitebox is controlling concurrent cycles, // (4) a new cycle was started (by this thread or some other), or // (5) a Full GC was performed. // Cases (4) and (5) are detected together by a change to // _old_marking_cycles_started. // // Note that (1) does not imply (4). If we're still in the mixed // phase of an earlier concurrent collection, the request to make the // collection a concurrent start won't be honored. If we don't check for // both conditions we'll spin doing back-to-back collections. if (op.gc_succeeded() ||
op.cycle_already_in_progress() ||
op.whitebox_attached() ||
(old_marking_started_before != old_marking_started_after)) {
LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, true); returntrue;
}
} else { // User-requested GC. // For a user-requested collection, we want to ensure that a complete // full collection has been performed before returning, but without // waiting for more than needed.
// For user-requested GCs (unlike non-UR), a successful VMOp implies a // new cycle was started. That's good, because it's not clear what we // should do otherwise. Trying again just does back to back GCs. // Can't wait for someone else to start a cycle. And returning fails // to meet the goal of ensuring a full collection was performed.
assert(!op.gc_succeeded() ||
(old_marking_started_before != old_marking_started_after), "invariant: succeeded %s, started before %u, started after %u",
BOOL_TO_STR(op.gc_succeeded()),
old_marking_started_before, old_marking_started_after);
// Request is finished if a full collection (concurrent or stw) // was started after this request and has completed, e.g. // started_before < completed_after. if (gc_counter_less_than(old_marking_started_before,
old_marking_completed_after)) {
LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, true); returntrue;
}
if (old_marking_started_after != old_marking_completed_after) { // If there is an in-progress cycle (possibly started by us), then // wait for that cycle to complete, e.g. // while completed_now < started_after.
LOG_COLLECT_CONCURRENTLY(cause, "wait");
MonitorLocker ml(G1OldGCCount_lock); while (gc_counter_less_than(_old_marking_cycles_completed,
old_marking_started_after)) {
ml.wait();
} // Request is finished if the collection we just waited for was // started after this request. if (old_marking_started_before != old_marking_started_after) {
LOG_COLLECT_CONCURRENTLY(cause, "complete after wait"); returntrue;
}
}
// If VMOp was successful then it started a new cycle that the above // wait &etc should have recognized as finishing this request. This // differs from a non-user-request, where gc_succeeded does not imply // a new cycle was started.
assert(!op.gc_succeeded(), "invariant");
if (op.cycle_already_in_progress()) { // If VMOp failed because a cycle was already in progress, it // is now complete. But it didn't finish this user-requested // GC, so try again.
LOG_COLLECT_CONCURRENTLY(cause, "retry after in-progress"); continue;
} elseif (op.whitebox_attached()) { // If WhiteBox wants control, wait for notification of a state // change in the controller, then try again. Don't wait for // release of control, since collections may complete while in // control. Note: This won't recognize a STW full collection // while waiting; we can't wait on multiple monitors.
LOG_COLLECT_CONCURRENTLY(cause, "whitebox control stall");
MonitorLocker ml(ConcurrentGCBreakpoints::monitor()); if (ConcurrentGCBreakpoints::is_controlled()) {
ml.wait();
} continue;
}
}
// Collection failed and should be retried.
assert(op.transient_failure(), "invariant");
if (GCLocker::is_active_and_needs_gc()) { // If GCLocker is active, wait until clear before retrying.
LOG_COLLECT_CONCURRENTLY(cause, "gc-locker stall");
GCLocker::stall_until_clear();
}
LOG_COLLECT_CONCURRENTLY(cause, "retry");
}
}
bool G1CollectedHeap::try_collect(GCCause::Cause cause, const G1GCCounters& counters_before) { if (should_do_concurrent_full_gc(cause)) { return try_collect_concurrently(cause,
counters_before.total_collections(),
counters_before.old_marking_cycles_started());
} elseif (GCLocker::should_discard(cause, counters_before.total_collections())) { // Indicate failure to be consistent with VMOp failure due to // another collection slipping in after our gc_count but before // our request is processed. returnfalse;
} elseif (cause == GCCause::_gc_locker || cause == GCCause::_wb_young_gc
DEBUG_ONLY(|| cause == GCCause::_scavenge_alot)) {
// Schedule a standard evacuation pause. We're setting word_size // to 0 which means that we are not requesting a post-GC allocation.
VM_G1CollectForAllocation op(0, /* word_size */
counters_before.total_collections(),
cause);
VMThread::execute(&op); return op.gc_succeeded();
} else { // Schedule a Full GC.
VM_G1CollectFull op(counters_before.total_collections(),
counters_before.total_full_collections(),
cause);
VMThread::execute(&op); return op.gc_succeeded();
}
}
// At this point we are supposed to start a concurrent cycle. We // will do so if one is not already in progress. bool should_start = policy()->force_concurrent_start_if_outside_cycle(gc_cause); if (should_start) {
do_collection_pause_at_safepoint();
}
}
do {
uint region_idx = regions[cur_pos]; if (hr_claimer == NULL || hr_claimer->claim_region(region_idx)) {
HeapRegion* r = region_at(region_idx); bool result = cl->do_heap_region(r);
guarantee(!result, "Must not cancel iteration");
}
cur_pos++; if (cur_pos == length) {
cur_pos = 0;
}
} while (cur_pos != start_pos);
}
HeapWord* G1CollectedHeap::block_start(constvoid* addr) const {
HeapRegion* hr = heap_region_containing(addr); // The CollectedHeap API requires us to not fail for any given address within // the heap. HeapRegion::block_start() has been optimized to not accept addresses // outside of the allocated area. if (addr >= hr->top()) { return nullptr;
} return hr->block_start(addr);
}
// For G1 TLABs should not contain humongous objects, so the maximum TLAB size // must be equal to the humongous object limit.
size_t G1CollectedHeap::max_tlab_size() const { return align_down(_humongous_object_threshold_in_words, MinObjAlignment);
}
void G1CollectedHeap::gc_prologue(bool full) {
assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
// Update common counters.
increment_total_collections(full /* full gc */); if (full || collector_state()->in_concurrent_start_gc()) {
increment_old_marking_cycles_started();
}
}
void G1CollectedHeap::gc_epilogue(bool full) { // Update common counters. if (full) { // Update the number of full collections that have been completed.
increment_old_marking_cycles_completed(false/* concurrent */, true /* liveness_completed */);
}
if (lt.is_enabled()) {
LogStream ls(lt); // Iterate all heap regions to print matching between preferred numa id and actual numa id.
G1NodeIndexCheckClosure cl(desc, _numa, &ls);
heap_region_iterate(&cl);
}
}
HeapWord* result = op.result(); bool ret_succeeded = op.prologue_succeeded() && op.gc_succeeded();
assert(result == NULL || ret_succeeded, "the result should be NULL if the VM did not succeed");
*succeeded = ret_succeeded;
assert_heap_not_locked(); return result;
}
void G1CollectedHeap::start_concurrent_cycle(bool concurrent_operation_is_full_mark) {
assert(!_cm_thread->in_progress(), "Can not start concurrent operation while in progress");
bool G1CollectedHeap::is_potential_eager_reclaim_candidate(HeapRegion* r) const { // We don't nominate objects with many remembered set entries, on // the assumption that such objects are likely still live.
HeapRegionRemSet* rem_set = r->rem_set();
guarantee(_eden.length() == 0, "eden should have been cleared");
policy()->transfer_survivors_to_cset(survivor());
// We redo the verification but now wrt to the new CSet which // has just got initialized after the previous CSet was freed.
_cm->verify_no_collection_set_oops();
void G1CollectedHeap::expand_heap_after_young_collection(){
size_t expand_bytes = _heap_sizing_policy->young_collection_expansion_amount(); if (expand_bytes > 0) { // No need for an ergo logging here, // expansion_amount() does this when it returns a value > 0. double expand_ms = 0.0; if (!expand(expand_bytes, _workers, &expand_ms)) { // We failed to expand the heap. Cannot do anything about it.
}
phase_times()->record_expand_heap_time(expand_ms);
}
}
bool G1CollectedHeap::do_collection_pause_at_safepoint() {
assert_at_safepoint_on_vm_thread();
guarantee(!is_gc_active(), "collection is not reentrant");
if (GCLocker::check_active_before_gc()) { returnfalse;
}
G1HeapPrinterMark::G1HeapPrinterMark(G1CollectedHeap* g1h) : _g1h(g1h), _heap_transition(g1h) { // This summary needs to be printed before incrementing total collections.
_g1h->rem_set()->print_periodic_summary_info("Before GC RS summary",
_g1h->total_collections(), true/* show_thread_times */);
_g1h->print_heap_before_gc();
_g1h->print_heap_regions();
}
G1HeapPrinterMark::~G1HeapPrinterMark() {
_g1h->policy()->print_age_table();
_g1h->rem_set()->print_coarsen_stats(); // We are at the end of the GC. Total collections has already been increased.
_g1h->rem_set()->print_periodic_summary_info("After GC RS summary",
_g1h->total_collections() - 1, false/* show_thread_times */);
_heap_transition.print();
_g1h->print_heap_regions();
_g1h->print_heap_after_gc(); // Print NUMA statistics.
_g1h->numa()->print_statistics();
}
policy()->decide_on_concurrent_start_pause(); // Record whether this pause may need to trigger a concurrent operation. Later, // when we signal the G1ConcurrentMarkThread, the collector state has already // been reset for the next pause. bool should_start_concurrent_mark_operation = collector_state()->in_concurrent_start_gc();
// Perform the collection.
G1YoungCollector collector(gc_cause());
collector.collect();
// It should now be safe to tell the concurrent mark thread to start // without its logging output interfering with the logging output // that came from the pause. if (should_start_concurrent_mark_operation) {
verifier()->verify_bitmap_clear(true/* above_tams_only */); // CAUTION: after the start_concurrent_cycle() call below, the concurrent marking // thread(s) could be running concurrently with us. Make sure that anything // after this point does not assume that we are the only GC thread running. // Note: of course, the actual marking work will not start until the safepoint // itself is released in SuspendibleThreadSet::desynchronize().
start_concurrent_cycle(collector.concurrent_operation_is_full_mark());
ConcurrentGCBreakpoints::notify_idle_to_active();
}
}
bool G1STWSubjectToDiscoveryClosure::do_object_b(oop obj) {
assert(obj != NULL, "must not be NULL");
assert(_g1h->is_in_reserved(obj), "Trying to discover obj " PTR_FORMAT " not in heap", p2i(obj)); // The areas the CM and STW ref processor manage must be disjoint. The is_in_cset() below // may falsely indicate that this is not the case here: however the collection set only // contains old regions when concurrent mark is not running. return _g1h->is_in_cset(obj) || _g1h->heap_region_containing(obj)->is_survivor();
}
void G1CollectedHeap::make_pending_list_reachable() { if (collector_state()->in_concurrent_start_gc()) {
oop pll_head = Universe::reference_pending_list(); if (pll_head != NULL) { // Any valid worker id is fine here as we are in the VM thread and single-threaded.
_cm->mark_in_bitmap(0 /* worker_id */, pll_head);
}
}
}
void G1CollectedHeap::free_region(HeapRegion* hr, FreeRegionList* free_list) {
assert(!hr->is_free(), "the region should not be free");
assert(!hr->is_empty(), "the region should not be empty");
assert(_hrm.is_available(hr->hrm_index()), "region should be committed");
// Clear the card counts for this region. // Note: we only need to do this if the region is not young // (since we don't refine cards in young regions). if (!hr->is_young()) {
_hot_card_cache->reset_card_counts(hr);
}
// Reset region metadata to allow reuse.
hr->hr_clear(true/* clear_space */);
_policy->remset_tracker()->update_at_free(hr);
if (free_list != NULL) {
free_list->add_ordered(hr);
}
}
void G1CollectedHeap::free_humongous_region(HeapRegion* hr,
FreeRegionList* free_list) {
assert(hr->is_humongous(), "this is only for humongous regions");
hr->clear_humongous();
free_region(hr, free_list);
}
class G1AbandonCollectionSetClosure : public HeapRegionClosure { public: virtualbool do_heap_region(HeapRegion* r) {
assert(r->in_collection_set(), "Region %u must have been in collection set", r->hrm_index());
G1CollectedHeap::heap()->clear_region_attr(r);
r->clear_young_index_in_cset(); returnfalse;
}
};
bool G1CollectedHeap::check_young_list_empty() { bool ret = (young_regions_count() == 0);
NoYoungRegionsClosure closure;
heap_region_iterate(&closure);
ret = ret && closure.success();
return ret;
}
#endif// ASSERT
// Remove the given HeapRegion from the appropriate region set. void G1CollectedHeap::prepare_region_for_full_compaction(HeapRegion* hr) { if (hr->is_archive()) {
_archive_set.remove(hr);
} elseif (hr->is_humongous()) {
_humongous_set.remove(hr);
} elseif (hr->is_old()) {
_old_set.remove(hr);
} elseif (hr->is_young()) { // Note that emptying the eden and survivor lists is postponed and instead // done as the first step when rebuilding the regions sets again. The reason // for this is that during a full GC string deduplication needs to know if // a collected region was young or old when the full GC was initiated.
hr->uninstall_surv_rate_group();
} else { // We ignore free regions, we'll empty the free list afterwards.
assert(hr->is_free(), "it cannot be another type");
}
}
bool do_heap_region(HeapRegion* r) { if (r->is_empty()) {
assert(r->rem_set()->is_empty(), "Empty regions should have empty remembered sets."); // Add free regions to the free list
r->set_free();
_hrm->insert_into_free_list(r);
} elseif (!_free_list_only) {
assert(r->rem_set()->is_empty(), "At this point remembered sets must have been cleared.");
if (r->is_humongous()) {
_humongous_set->add(r);
} elseif (r->is_archive()) {
_archive_set->add(r);
} else {
assert(r->is_young() || r->is_free() || r->is_old(), "invariant"); // We now move all (non-humongous, non-old, non-archive) regions to old gen, // and register them as such.
r->move_to_old();
_old_set->add(r);
}
_total_used += r->used();
}
if (!free_list_only) {
set_used(cl.total_used());
assert(_archive_allocator == nullptr, "must be, should not contribute to used");
}
assert_used_and_recalculate_used_equal(this);
}
void G1CollectedHeap::retire_mutator_alloc_region(HeapRegion* alloc_region,
size_t allocated_bytes) {
assert_heap_locked_or_at_safepoint(true/* should_be_vm_thread */);
assert(alloc_region->is_eden(), "all mutator alloc regions should be eden");
// We update the eden sizes here, when the region is retired, // instead of when it's allocated, since this is the point that its // used space has been recorded in _summary_bytes_used.
monitoring_support()->update_eden_size();
}
void G1CollectedHeap::update_used_after_gc(bool evacuation_failed) { if (evacuation_failed) { // Reset the G1EvacuationFailureALot counters and flags
evac_failure_injector()->reset();
set_used(recalculate_used());
assert(_archive_allocator == nullptr, "must be, should not contribute to used");
} else { // The "used" of the collection set have already been subtracted // when they were freed. Add in the bytes used.
increase_used(_bytes_used_during_gc);
}
}
void G1CollectedHeap::start_codecache_marking_cycle_if_inactive() { if (!CodeCache::is_gc_marking_cycle_active()) { // This is the normal case when we do not call collect when a // concurrent mark is ongoing. We then start a new code marking // cycle. If, on the other hand, a concurrent mark is ongoing, we // will be conservative and use the last code marking cycle. Code // caches marked between the two concurrent marks will live a bit // longer than needed.
CodeCache::on_gc_marking_cycle_start();
CodeCache::arm_all_nmethods();
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.48 Sekunden
(vorverarbeitet am 2026-05-02)
¤
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.