Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/shenandoah/heuristics/shenandoahHeuristics.cpp
38922 views
/*1* Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223#include "precompiled.hpp"2425#include "gc_interface/gcCause.hpp"26#include "gc_implementation/shenandoah/shenandoahCollectorPolicy.hpp"27#include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp"28#include "gc_implementation/shenandoah/shenandoahHeapRegion.inline.hpp"29#include "gc_implementation/shenandoah/shenandoahMarkingContext.inline.hpp"30#include "gc_implementation/shenandoah/shenandoahUtils.hpp"31#include "gc_implementation/shenandoah/heuristics/shenandoahHeuristics.hpp"3233int ShenandoahHeuristics::compare_by_garbage(RegionData a, RegionData b) {34if (a._garbage > b._garbage)35return -1;36else if (a._garbage < b._garbage)37return 1;38else return 0;39}4041ShenandoahHeuristics::ShenandoahHeuristics() :42_region_data(NULL),43_degenerated_cycles_in_a_row(0),44_successful_cycles_in_a_row(0),45_cycle_start(os::elapsedTime()),46_last_cycle_end(0),47_gc_times_learned(0),48_gc_time_penalties(0),49_gc_time_history(new TruncatedSeq(5)),50_metaspace_oom()51{52// No unloading during concurrent mark? Communicate that to heuristics53if (!ClassUnloadingWithConcurrentMark) {54FLAG_SET_DEFAULT(ShenandoahUnloadClassesFrequency, 0);55}5657size_t num_regions = ShenandoahHeap::heap()->num_regions();58assert(num_regions > 0, "Sanity");5960_region_data = NEW_C_HEAP_ARRAY(RegionData, num_regions, mtGC);61}6263ShenandoahHeuristics::~ShenandoahHeuristics() {64FREE_C_HEAP_ARRAY(RegionGarbage, _region_data, mtGC);65}6667void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {68assert(collection_set->count() == 0, "Must be empty");69start_choose_collection_set();7071ShenandoahHeap* heap = ShenandoahHeap::heap();7273// Check all pinned regions have updated status before choosing the collection set.74heap->assert_pinned_region_status();7576// Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.7778size_t num_regions = heap->num_regions();7980RegionData* candidates = _region_data;8182size_t cand_idx = 0;8384size_t total_garbage = 0;8586size_t immediate_garbage = 0;87size_t immediate_regions = 0;8889size_t free = 0;90size_t free_regions = 0;9192ShenandoahMarkingContext* const ctx = heap->complete_marking_context();9394for (size_t i = 0; i < num_regions; i++) {95ShenandoahHeapRegion* region = heap->get_region(i);9697size_t garbage = region->garbage();98total_garbage += garbage;99100if (region->is_empty()) {101free_regions++;102free += ShenandoahHeapRegion::region_size_bytes();103} else if (region->is_regular()) {104if (!region->has_live()) {105// We can recycle it right away and put it in the free set.106immediate_regions++;107immediate_garbage += garbage;108region->make_trash_immediate();109} else {110// This is our candidate for later consideration.111candidates[cand_idx]._region = region;112candidates[cand_idx]._garbage = garbage;113cand_idx++;114}115} else if (region->is_humongous_start()) {116// Reclaim humongous regions here, and count them as the immediate garbage117#ifdef ASSERT118bool reg_live = region->has_live();119bool bm_live = ctx->is_marked(oop(region->bottom()));120assert(reg_live == bm_live,121err_msg("Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,122BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words()));123#endif124if (!region->has_live()) {125heap->trash_humongous_region_at(region);126127// Count only the start. Continuations would be counted on "trash" path128immediate_regions++;129immediate_garbage += garbage;130}131} else if (region->is_trash()) {132// Count in just trashed collection set, during coalesced CM-with-UR133immediate_regions++;134immediate_garbage += garbage;135}136}137138// Step 2. Look back at garbage statistics, and decide if we want to collect anything,139// given the amount of immediately reclaimable garbage. If we do, figure out the collection set.140141assert (immediate_garbage <= total_garbage,142err_msg("Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",143byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),144byte_size_in_proper_unit(total_garbage), proper_unit_for_byte_size(total_garbage)));145146size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);147148if (immediate_percent <= ShenandoahImmediateThreshold) {149choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);150}151152size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);153154size_t collectable_garbage = collection_set->garbage() + immediate_garbage;155size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage);156157log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "158"Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "159"CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%)",160161byte_size_in_proper_unit(collectable_garbage),162proper_unit_for_byte_size(collectable_garbage),163collectable_garbage_percent,164165byte_size_in_proper_unit(immediate_garbage),166proper_unit_for_byte_size(immediate_garbage),167immediate_percent,168169byte_size_in_proper_unit(collection_set->garbage()),170proper_unit_for_byte_size(collection_set->garbage()),171cset_percent);172}173174void ShenandoahHeuristics::record_cycle_start() {175_cycle_start = os::elapsedTime();176}177178void ShenandoahHeuristics::record_cycle_end() {179_last_cycle_end = os::elapsedTime();180}181182bool ShenandoahHeuristics::should_degenerate_cycle() {183return _degenerated_cycles_in_a_row <= ShenandoahFullGCThreshold;184}185186void ShenandoahHeuristics::adjust_penalty(intx step) {187assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,188err_msg("In range before adjustment: " INTX_FORMAT, _gc_time_penalties));189190intx new_val = _gc_time_penalties + step;191if (new_val < 0) {192new_val = 0;193}194if (new_val > 100) {195new_val = 100;196}197_gc_time_penalties = new_val;198199assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,200err_msg("In range after adjustment: " INTX_FORMAT, _gc_time_penalties));201}202203void ShenandoahHeuristics::record_success_concurrent() {204_degenerated_cycles_in_a_row = 0;205_successful_cycles_in_a_row++;206207_gc_time_history->add(time_since_last_gc());208_gc_times_learned++;209210adjust_penalty(Concurrent_Adjust);211}212213void ShenandoahHeuristics::record_success_degenerated() {214_degenerated_cycles_in_a_row++;215_successful_cycles_in_a_row = 0;216217adjust_penalty(Degenerated_Penalty);218}219220void ShenandoahHeuristics::record_success_full() {221_degenerated_cycles_in_a_row = 0;222_successful_cycles_in_a_row++;223224adjust_penalty(Full_Penalty);225}226227void ShenandoahHeuristics::record_allocation_failure_gc() {228// Do nothing.229}230231void ShenandoahHeuristics::record_requested_gc() {232// Assume users call System.gc() when external state changes significantly,233// which forces us to re-learn the GC timings and allocation rates.234_gc_times_learned = 0;235}236237bool ShenandoahHeuristics::can_process_references() {238if (ShenandoahRefProcFrequency == 0) return false;239return true;240}241242bool ShenandoahHeuristics::should_process_references() {243if (!can_process_references()) return false;244size_t cycle = ShenandoahHeap::heap()->shenandoah_policy()->cycle_counter();245// Process references every Nth GC cycle.246return cycle % ShenandoahRefProcFrequency == 0;247}248249bool ShenandoahHeuristics::can_unload_classes() {250if (!ClassUnloading) return false;251return true;252}253254bool ShenandoahHeuristics::can_unload_classes_normal() {255if (!can_unload_classes()) return false;256if (has_metaspace_oom()) return true;257if (!ClassUnloadingWithConcurrentMark) return false;258if (ShenandoahUnloadClassesFrequency == 0) return false;259return true;260}261262bool ShenandoahHeuristics::should_unload_classes() {263if (!can_unload_classes_normal()) return false;264if (has_metaspace_oom()) return true;265size_t cycle = ShenandoahHeap::heap()->shenandoah_policy()->cycle_counter();266// Unload classes every Nth GC cycle.267// This should not happen in the same cycle as process_references to amortize costs.268// Offsetting by one is enough to break the rendezvous when periods are equal.269// When periods are not equal, offsetting by one is just as good as any other guess.270return (cycle + 1) % ShenandoahUnloadClassesFrequency == 0;271}272273void ShenandoahHeuristics::initialize() {274// Nothing to do by default.275}276277double ShenandoahHeuristics::time_since_last_gc() const {278return os::elapsedTime() - _cycle_start;279}280281bool ShenandoahHeuristics::should_start_gc() const {282// Perform GC to cleanup metaspace283if (has_metaspace_oom()) {284// Some of vmTestbase/metaspace tests depend on following line to count GC cycles285log_info(gc)("Trigger: %s", GCCause::to_string(GCCause::_metadata_GC_threshold));286return true;287}288289if (ShenandoahGuaranteedGCInterval > 0) {290double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;291if (last_time_ms > ShenandoahGuaranteedGCInterval) {292log_info(gc)("Trigger: Time since last GC (%.0f ms) is larger than guaranteed interval (" UINTX_FORMAT " ms)",293last_time_ms, ShenandoahGuaranteedGCInterval);294return true;295}296}297298return false;299}300301302