Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/shenandoah/heuristics/shenandoahAdaptiveHeuristics.cpp
38922 views
/*1* Copyright (c) 2018, 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_implementation/shenandoah/heuristics/shenandoahAdaptiveHeuristics.hpp"26#include "gc_implementation/shenandoah/shenandoahCollectionSet.hpp"27#include "gc_implementation/shenandoah/shenandoahFreeSet.hpp"28#include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp"29#include "gc_implementation/shenandoah/shenandoahHeapRegion.inline.hpp"30#include "gc_implementation/shenandoah/shenandoahLogging.hpp"31#include "utilities/quickSort.hpp"3233ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics() :34ShenandoahHeuristics() {}3536ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {}3738void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,39RegionData* data, size_t size,40size_t actual_free) {41size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;4243// The logic for cset selection in adaptive is as follows:44//45// 1. We cannot get cset larger than available free space. Otherwise we guarantee OOME46// during evacuation, and thus guarantee full GC. In practice, we also want to let47// application to allocate something. This is why we limit CSet to some fraction of48// available space. In non-overloaded heap, max_cset would contain all plausible candidates49// over garbage threshold.50//51// 2. We should not get cset too low so that free threshold would not be met right52// after the cycle. Otherwise we get back-to-back cycles for no reason if heap is53// too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.54//55// Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates56// before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before57// we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,58// ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.5960size_t capacity = ShenandoahHeap::heap()->soft_max_capacity();61size_t max_cset = (size_t)((1.0 * capacity / 100 * ShenandoahEvacReserve) / ShenandoahEvacWaste);62size_t free_target = (capacity / 100 * ShenandoahMinFreeThreshold) + max_cset;63size_t min_garbage = (free_target > actual_free ? (free_target - actual_free) : 0);6465log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "%s, Actual Free: "66SIZE_FORMAT "%s, Max CSet: " SIZE_FORMAT "%s, Min Garbage: " SIZE_FORMAT "%s",67byte_size_in_proper_unit(free_target), proper_unit_for_byte_size(free_target),68byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free),69byte_size_in_proper_unit(max_cset), proper_unit_for_byte_size(max_cset),70byte_size_in_proper_unit(min_garbage), proper_unit_for_byte_size(min_garbage));7172// Better select garbage-first regions73QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false);7475size_t cur_cset = 0;76size_t cur_garbage = 0;7778for (size_t idx = 0; idx < size; idx++) {79ShenandoahHeapRegion* r = data[idx]._region;8081size_t new_cset = cur_cset + r->get_live_data_bytes();82size_t new_garbage = cur_garbage + r->garbage();8384if (new_cset > max_cset) {85break;86}8788if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) {89cset->add_region(r);90cur_cset = new_cset;91cur_garbage = new_garbage;92}93}94}9596void ShenandoahAdaptiveHeuristics::record_cycle_start() {97ShenandoahHeuristics::record_cycle_start();98}99100bool ShenandoahAdaptiveHeuristics::should_start_gc() const {101ShenandoahHeap* heap = ShenandoahHeap::heap();102size_t max_capacity = heap->max_capacity();103size_t capacity = heap->soft_max_capacity();104size_t available = heap->free_set()->available();105106// Make sure the code below treats available without the soft tail.107size_t soft_tail = max_capacity - capacity;108available = (available > soft_tail) ? (available - soft_tail) : 0;109110// Check if we are falling below the worst limit, time to trigger the GC, regardless of111// anything else.112size_t min_threshold = capacity / 100 * ShenandoahMinFreeThreshold;113if (available < min_threshold) {114log_info(gc)("Trigger: Free (" SIZE_FORMAT "%s) is below minimum threshold (" SIZE_FORMAT "%s)",115byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),116byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold));117return true;118}119120// Check if are need to learn a bit about the application121const size_t max_learn = ShenandoahLearningSteps;122if (_gc_times_learned < max_learn) {123size_t init_threshold = capacity / 100 * ShenandoahInitFreeThreshold;124if (available < init_threshold) {125log_info(gc)("Trigger: Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "%s) is below initial threshold (" SIZE_FORMAT "%s)",126_gc_times_learned + 1, max_learn,127byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),128byte_size_in_proper_unit(init_threshold), proper_unit_for_byte_size(init_threshold));129return true;130}131}132133// Check if allocation headroom is still okay. This also factors in:134// 1. Some space to absorb allocation spikes135// 2. Accumulated penalties from Degenerated and Full GC136137size_t allocation_headroom = available;138139size_t spike_headroom = capacity / 100 * ShenandoahAllocSpikeFactor;140size_t penalties = capacity / 100 * _gc_time_penalties;141142allocation_headroom -= MIN2(allocation_headroom, spike_headroom);143allocation_headroom -= MIN2(allocation_headroom, penalties);144145// TODO: Allocation rate is way too averaged to be useful during state changes146147double average_gc = _gc_time_history->avg();148double time_since_last = time_since_last_gc();149double allocation_rate = heap->bytes_allocated_since_gc_start() / time_since_last;150151if (average_gc > allocation_headroom / allocation_rate) {152log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s)",153average_gc * 1000,154byte_size_in_proper_unit(allocation_rate), proper_unit_for_byte_size(allocation_rate),155byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom));156log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "%s (free) - " SIZE_FORMAT "%s (spike) - " SIZE_FORMAT "%s (penalties) = " SIZE_FORMAT "%s",157byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),158byte_size_in_proper_unit(spike_headroom), proper_unit_for_byte_size(spike_headroom),159byte_size_in_proper_unit(penalties), proper_unit_for_byte_size(penalties),160byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom));161return true;162}163164return ShenandoahHeuristics::should_start_gc();165}166167168