Path: blob/master/src/hotspot/share/gc/shenandoah/heuristics/shenandoahHeuristics.cpp
40975 views
/*1* Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/shared/gcCause.hpp"26#include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"27#include "gc/shenandoah/shenandoahCollectorPolicy.hpp"28#include "gc/shenandoah/shenandoahHeap.inline.hpp"29#include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"30#include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"31#include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"32#include "logging/log.hpp"33#include "logging/logTag.hpp"34#include "runtime/globals_extension.hpp"3536int ShenandoahHeuristics::compare_by_garbage(RegionData a, RegionData b) {37if (a._garbage > b._garbage)38return -1;39else if (a._garbage < b._garbage)40return 1;41else return 0;42}4344ShenandoahHeuristics::ShenandoahHeuristics() :45_region_data(NULL),46_degenerated_cycles_in_a_row(0),47_successful_cycles_in_a_row(0),48_cycle_start(os::elapsedTime()),49_last_cycle_end(0),50_gc_times_learned(0),51_gc_time_penalties(0),52_gc_time_history(new TruncatedSeq(10, ShenandoahAdaptiveDecayFactor)),53_metaspace_oom()54{55// No unloading during concurrent mark? Communicate that to heuristics56if (!ClassUnloadingWithConcurrentMark) {57FLAG_SET_DEFAULT(ShenandoahUnloadClassesFrequency, 0);58}5960size_t num_regions = ShenandoahHeap::heap()->num_regions();61assert(num_regions > 0, "Sanity");6263_region_data = NEW_C_HEAP_ARRAY(RegionData, num_regions, mtGC);64}6566ShenandoahHeuristics::~ShenandoahHeuristics() {67FREE_C_HEAP_ARRAY(RegionGarbage, _region_data);68}6970void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {71assert(collection_set->count() == 0, "Must be empty");7273ShenandoahHeap* heap = ShenandoahHeap::heap();7475// Check all pinned regions have updated status before choosing the collection set.76heap->assert_pinned_region_status();7778// Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.7980size_t num_regions = heap->num_regions();8182RegionData* candidates = _region_data;8384size_t cand_idx = 0;8586size_t total_garbage = 0;8788size_t immediate_garbage = 0;89size_t immediate_regions = 0;9091size_t free = 0;92size_t free_regions = 0;9394ShenandoahMarkingContext* const ctx = heap->complete_marking_context();9596for (size_t i = 0; i < num_regions; i++) {97ShenandoahHeapRegion* region = heap->get_region(i);9899size_t garbage = region->garbage();100total_garbage += garbage;101102if (region->is_empty()) {103free_regions++;104free += ShenandoahHeapRegion::region_size_bytes();105} else if (region->is_regular()) {106if (!region->has_live()) {107// We can recycle it right away and put it in the free set.108immediate_regions++;109immediate_garbage += garbage;110region->make_trash_immediate();111} else {112// This is our candidate for later consideration.113candidates[cand_idx]._region = region;114candidates[cand_idx]._garbage = garbage;115cand_idx++;116}117} else if (region->is_humongous_start()) {118// Reclaim humongous regions here, and count them as the immediate garbage119#ifdef ASSERT120bool reg_live = region->has_live();121bool bm_live = ctx->is_marked(cast_to_oop(region->bottom()));122assert(reg_live == bm_live,123"Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,124BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words());125#endif126if (!region->has_live()) {127heap->trash_humongous_region_at(region);128129// Count only the start. Continuations would be counted on "trash" path130immediate_regions++;131immediate_garbage += garbage;132}133} else if (region->is_trash()) {134// Count in just trashed collection set, during coalesced CM-with-UR135immediate_regions++;136immediate_garbage += garbage;137}138}139140// Step 2. Look back at garbage statistics, and decide if we want to collect anything,141// given the amount of immediately reclaimable garbage. If we do, figure out the collection set.142143assert (immediate_garbage <= total_garbage,144"Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",145byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),146byte_size_in_proper_unit(total_garbage), proper_unit_for_byte_size(total_garbage));147148size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);149150if (immediate_percent <= ShenandoahImmediateThreshold) {151choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);152}153154size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);155156size_t collectable_garbage = collection_set->garbage() + immediate_garbage;157size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage);158159log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "160"Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "161"CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%)",162163byte_size_in_proper_unit(collectable_garbage),164proper_unit_for_byte_size(collectable_garbage),165collectable_garbage_percent,166167byte_size_in_proper_unit(immediate_garbage),168proper_unit_for_byte_size(immediate_garbage),169immediate_percent,170171byte_size_in_proper_unit(collection_set->garbage()),172proper_unit_for_byte_size(collection_set->garbage()),173cset_percent);174}175176void ShenandoahHeuristics::record_cycle_start() {177_cycle_start = os::elapsedTime();178}179180void ShenandoahHeuristics::record_cycle_end() {181_last_cycle_end = os::elapsedTime();182}183184bool ShenandoahHeuristics::should_start_gc() {185// Perform GC to cleanup metaspace186if (has_metaspace_oom()) {187// Some of vmTestbase/metaspace tests depend on following line to count GC cycles188log_info(gc)("Trigger: %s", GCCause::to_string(GCCause::_metadata_GC_threshold));189return true;190}191192if (ShenandoahGuaranteedGCInterval > 0) {193double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;194if (last_time_ms > ShenandoahGuaranteedGCInterval) {195log_info(gc)("Trigger: Time since last GC (%.0f ms) is larger than guaranteed interval (" UINTX_FORMAT " ms)",196last_time_ms, ShenandoahGuaranteedGCInterval);197return true;198}199}200201return false;202}203204bool ShenandoahHeuristics::should_degenerate_cycle() {205return _degenerated_cycles_in_a_row <= ShenandoahFullGCThreshold;206}207208void ShenandoahHeuristics::adjust_penalty(intx step) {209assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,210"In range before adjustment: " INTX_FORMAT, _gc_time_penalties);211212intx new_val = _gc_time_penalties + step;213if (new_val < 0) {214new_val = 0;215}216if (new_val > 100) {217new_val = 100;218}219_gc_time_penalties = new_val;220221assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,222"In range after adjustment: " INTX_FORMAT, _gc_time_penalties);223}224225void ShenandoahHeuristics::record_success_concurrent() {226_degenerated_cycles_in_a_row = 0;227_successful_cycles_in_a_row++;228229_gc_time_history->add(time_since_last_gc());230_gc_times_learned++;231232adjust_penalty(Concurrent_Adjust);233}234235void ShenandoahHeuristics::record_success_degenerated() {236_degenerated_cycles_in_a_row++;237_successful_cycles_in_a_row = 0;238239adjust_penalty(Degenerated_Penalty);240}241242void ShenandoahHeuristics::record_success_full() {243_degenerated_cycles_in_a_row = 0;244_successful_cycles_in_a_row++;245246adjust_penalty(Full_Penalty);247}248249void ShenandoahHeuristics::record_allocation_failure_gc() {250// Do nothing.251}252253void ShenandoahHeuristics::record_requested_gc() {254// Assume users call System.gc() when external state changes significantly,255// which forces us to re-learn the GC timings and allocation rates.256_gc_times_learned = 0;257}258259bool ShenandoahHeuristics::can_unload_classes() {260if (!ClassUnloading) return false;261return true;262}263264bool ShenandoahHeuristics::can_unload_classes_normal() {265if (!can_unload_classes()) return false;266if (has_metaspace_oom()) return true;267if (!ClassUnloadingWithConcurrentMark) return false;268if (ShenandoahUnloadClassesFrequency == 0) return false;269return true;270}271272bool ShenandoahHeuristics::should_unload_classes() {273if (!can_unload_classes_normal()) return false;274if (has_metaspace_oom()) return true;275size_t cycle = ShenandoahHeap::heap()->shenandoah_policy()->cycle_counter();276// Unload classes every Nth GC cycle.277// This should not happen in the same cycle as process_references to amortize costs.278// Offsetting by one is enough to break the rendezvous when periods are equal.279// When periods are not equal, offsetting by one is just as good as any other guess.280return (cycle + 1) % ShenandoahUnloadClassesFrequency == 0;281}282283void ShenandoahHeuristics::initialize() {284// Nothing to do by default.285}286287double ShenandoahHeuristics::time_since_last_gc() const {288return os::elapsedTime() - _cycle_start;289}290291292