Path: blob/master/src/hotspot/share/gc/z/zDirector.cpp
40957 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. 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*/2223#include "precompiled.hpp"24#include "gc/shared/gc_globals.hpp"25#include "gc/z/zCollectedHeap.hpp"26#include "gc/z/zDirector.hpp"27#include "gc/z/zHeap.inline.hpp"28#include "gc/z/zHeuristics.hpp"29#include "gc/z/zStat.hpp"30#include "logging/log.hpp"3132const double ZDirector::one_in_1000 = 3.290527;3334ZDirector::ZDirector() :35_relocation_headroom(ZHeuristics::relocation_headroom()),36_metronome(ZStatAllocRate::sample_hz) {37set_name("ZDirector");38create_and_start();39}4041void ZDirector::sample_allocation_rate() const {42// Sample allocation rate. This is needed by rule_allocation_rate()43// below to estimate the time we have until we run out of memory.44const double bytes_per_second = ZStatAllocRate::sample_and_reset();4546log_debug(gc, alloc)("Allocation Rate: %.3fMB/s, Avg: %.3f(+/-%.3f)MB/s",47bytes_per_second / M,48ZStatAllocRate::avg() / M,49ZStatAllocRate::avg_sd() / M);50}5152bool ZDirector::rule_timer() const {53if (ZCollectionInterval <= 0) {54// Rule disabled55return false;56}5758// Perform GC if timer has expired.59const double time_since_last_gc = ZStatCycle::time_since_last();60const double time_until_gc = ZCollectionInterval - time_since_last_gc;6162log_debug(gc, director)("Rule: Timer, Interval: %.3fs, TimeUntilGC: %.3fs",63ZCollectionInterval, time_until_gc);6465return time_until_gc <= 0;66}6768bool ZDirector::rule_warmup() const {69if (ZStatCycle::is_warm()) {70// Rule disabled71return false;72}7374// Perform GC if heap usage passes 10/20/30% and no other GC has been75// performed yet. This allows us to get some early samples of the GC76// duration, which is needed by the other rules.77const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();78const size_t used = ZHeap::heap()->used();79const double used_threshold_percent = (ZStatCycle::nwarmup_cycles() + 1) * 0.1;80const size_t used_threshold = soft_max_capacity * used_threshold_percent;8182log_debug(gc, director)("Rule: Warmup %.0f%%, Used: " SIZE_FORMAT "MB, UsedThreshold: " SIZE_FORMAT "MB",83used_threshold_percent * 100, used / M, used_threshold / M);8485return used >= used_threshold;86}8788bool ZDirector::rule_allocation_rate() const {89if (!ZStatCycle::is_normalized_duration_trustable()) {90// Rule disabled91return false;92}9394// Perform GC if the estimated max allocation rate indicates that we95// will run out of memory. The estimated max allocation rate is based96// on the moving average of the sampled allocation rate plus a safety97// margin based on variations in the allocation rate and unforeseen98// allocation spikes.99100// Calculate amount of free memory available. Note that we take the101// relocation headroom into account to avoid in-place relocation.102const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();103const size_t used = ZHeap::heap()->used();104const size_t free_including_headroom = soft_max_capacity - MIN2(soft_max_capacity, used);105const size_t free = free_including_headroom - MIN2(free_including_headroom, _relocation_headroom);106107// Calculate time until OOM given the max allocation rate and the amount108// of free memory. The allocation rate is a moving average and we multiply109// that with an allocation spike tolerance factor to guard against unforeseen110// phase changes in the allocate rate. We then add ~3.3 sigma to account for111// the allocation rate variance, which means the probability is 1 in 1000112// that a sample is outside of the confidence interval.113const double max_alloc_rate = (ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::avg_sd() * one_in_1000);114const double time_until_oom = free / (max_alloc_rate + 1.0); // Plus 1.0B/s to avoid division by zero115116// Calculate max duration of a GC cycle. The duration of GC is a moving117// average, we add ~3.3 sigma to account for the GC duration variance.118const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration();119const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000);120121// Calculate time until GC given the time until OOM and max duration of GC.122// We also deduct the sample interval, so that we don't overshoot the target123// time and end up starting the GC too late in the next interval.124const double sample_interval = 1.0 / ZStatAllocRate::sample_hz;125const double time_until_gc = time_until_oom - max_duration_of_gc - sample_interval;126127log_debug(gc, director)("Rule: Allocation Rate, MaxAllocRate: %.3fMB/s, Free: " SIZE_FORMAT "MB, MaxDurationOfGC: %.3fs, TimeUntilGC: %.3fs",128max_alloc_rate / M, free / M, max_duration_of_gc, time_until_gc);129130return time_until_gc <= 0;131}132133bool ZDirector::rule_proactive() const {134if (!ZProactive || !ZStatCycle::is_warm()) {135// Rule disabled136return false;137}138139// Perform GC if the impact of doing so, in terms of application throughput140// reduction, is considered acceptable. This rule allows us to keep the heap141// size down and allow reference processing to happen even when we have a lot142// of free space on the heap.143144// Only consider doing a proactive GC if the heap usage has grown by at least145// 10% of the max capacity since the previous GC, or more than 5 minutes has146// passed since the previous GC. This helps avoid superfluous GCs when running147// applications with very low allocation rate.148const size_t used_after_last_gc = ZStatHeap::used_at_relocate_end();149const size_t used_increase_threshold = ZHeap::heap()->soft_max_capacity() * 0.10; // 10%150const size_t used_threshold = used_after_last_gc + used_increase_threshold;151const size_t used = ZHeap::heap()->used();152const double time_since_last_gc = ZStatCycle::time_since_last();153const double time_since_last_gc_threshold = 5 * 60; // 5 minutes154if (used < used_threshold && time_since_last_gc < time_since_last_gc_threshold) {155// Don't even consider doing a proactive GC156log_debug(gc, director)("Rule: Proactive, UsedUntilEnabled: " SIZE_FORMAT "MB, TimeUntilEnabled: %.3fs",157(used_threshold - used) / M,158time_since_last_gc_threshold - time_since_last_gc);159return false;160}161162const double assumed_throughput_drop_during_gc = 0.50; // 50%163const double acceptable_throughput_drop = 0.01; // 1%164const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration();165const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000);166const double acceptable_gc_interval = max_duration_of_gc * ((assumed_throughput_drop_during_gc / acceptable_throughput_drop) - 1.0);167const double time_until_gc = acceptable_gc_interval - time_since_last_gc;168169log_debug(gc, director)("Rule: Proactive, AcceptableGCInterval: %.3fs, TimeSinceLastGC: %.3fs, TimeUntilGC: %.3fs",170acceptable_gc_interval, time_since_last_gc, time_until_gc);171172return time_until_gc <= 0;173}174175bool ZDirector::rule_high_usage() const {176// Perform GC if the amount of free memory is 5% or less. This is a preventive177// meassure in the case where the application has a very low allocation rate,178// such that the allocation rate rule doesn't trigger, but the amount of free179// memory is still slowly but surely heading towards zero. In this situation,180// we start a GC cycle to avoid a potential allocation stall later.181182// Calculate amount of free memory available. Note that we take the183// relocation headroom into account to avoid in-place relocation.184const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();185const size_t used = ZHeap::heap()->used();186const size_t free_including_headroom = soft_max_capacity - MIN2(soft_max_capacity, used);187const size_t free = free_including_headroom - MIN2(free_including_headroom, _relocation_headroom);188const double free_percent = percent_of(free, soft_max_capacity);189190log_debug(gc, director)("Rule: High Usage, Free: " SIZE_FORMAT "MB(%.1f%%)",191free / M, free_percent);192193return free_percent <= 5.0;194}195196GCCause::Cause ZDirector::make_gc_decision() const {197// Rule 0: Timer198if (rule_timer()) {199return GCCause::_z_timer;200}201202// Rule 1: Warmup203if (rule_warmup()) {204return GCCause::_z_warmup;205}206207// Rule 2: Allocation rate208if (rule_allocation_rate()) {209return GCCause::_z_allocation_rate;210}211212// Rule 3: Proactive213if (rule_proactive()) {214return GCCause::_z_proactive;215}216217// Rule 4: High usage218if (rule_high_usage()) {219return GCCause::_z_high_usage;220}221222// No GC223return GCCause::_no_gc;224}225226void ZDirector::run_service() {227// Main loop228while (_metronome.wait_for_tick()) {229sample_allocation_rate();230const GCCause::Cause cause = make_gc_decision();231if (cause != GCCause::_no_gc) {232ZCollectedHeap::heap()->collect(cause);233}234}235}236237void ZDirector::stop_service() {238_metronome.stop();239}240241242