Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/collectorPolicy.hpp
32285 views
/*1* Copyright (c) 2001, 2013, 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*22*/2324#ifndef SHARE_VM_MEMORY_COLLECTORPOLICY_HPP25#define SHARE_VM_MEMORY_COLLECTORPOLICY_HPP2627#include "memory/allocation.hpp"28#include "memory/barrierSet.hpp"29#include "memory/generationSpec.hpp"30#include "memory/genRemSet.hpp"31#include "utilities/macros.hpp"3233// This class (or more correctly, subtypes of this class)34// are used to define global garbage collector attributes.35// This includes initialization of generations and any other36// shared resources they may need.37//38// In general, all flag adjustment and validation should be39// done in initialize_flags(), which is called prior to40// initialize_size_info().41//42// This class is not fully developed yet. As more collector(s)43// are added, it is expected that we will come across further44// behavior that requires global attention. The correct place45// to deal with those issues is this class.4647// Forward declarations.48class GenCollectorPolicy;49class TwoGenerationCollectorPolicy;50class AdaptiveSizePolicy;51#if INCLUDE_ALL_GCS52class ConcurrentMarkSweepPolicy;53class G1CollectorPolicy;54#endif // INCLUDE_ALL_GCS5556class GCPolicyCounters;57class MarkSweepPolicy;5859class CollectorPolicy : public CHeapObj<mtGC> {60protected:61GCPolicyCounters* _gc_policy_counters;6263virtual void initialize_alignments() = 0;64virtual void initialize_flags();65virtual void initialize_size_info();6667DEBUG_ONLY(virtual void assert_flags();)68DEBUG_ONLY(virtual void assert_size_info();)6970size_t _initial_heap_byte_size;71size_t _max_heap_byte_size;72size_t _min_heap_byte_size;7374size_t _space_alignment;75size_t _heap_alignment;7677// Needed to keep information if MaxHeapSize was set on the command line78// when the flag value is aligned etc by ergonomics79bool _max_heap_size_cmdline;8081// The sizing of the heap are controlled by a sizing policy.82AdaptiveSizePolicy* _size_policy;8384// Set to true when policy wants soft refs cleared.85// Reset to false by gc after it clears all soft refs.86bool _should_clear_all_soft_refs;8788// Set to true by the GC if the just-completed gc cleared all89// softrefs. This is set to true whenever a gc clears all softrefs, and90// set to false each time gc returns to the mutator. For example, in the91// ParallelScavengeHeap case the latter would be done toward the end of92// mem_allocate() where it returns op.result()93bool _all_soft_refs_clear;9495CollectorPolicy();9697public:98virtual void initialize_all() {99initialize_alignments();100initialize_flags();101initialize_size_info();102}103104// Return maximum heap alignment that may be imposed by the policy105static size_t compute_heap_alignment();106107size_t space_alignment() { return _space_alignment; }108size_t heap_alignment() { return _heap_alignment; }109110size_t initial_heap_byte_size() { return _initial_heap_byte_size; }111size_t max_heap_byte_size() { return _max_heap_byte_size; }112size_t min_heap_byte_size() { return _min_heap_byte_size; }113114enum Name {115CollectorPolicyKind,116TwoGenerationCollectorPolicyKind,117ConcurrentMarkSweepPolicyKind,118ASConcurrentMarkSweepPolicyKind,119G1CollectorPolicyKind120};121122AdaptiveSizePolicy* size_policy() { return _size_policy; }123bool should_clear_all_soft_refs() { return _should_clear_all_soft_refs; }124void set_should_clear_all_soft_refs(bool v) { _should_clear_all_soft_refs = v; }125// Returns the current value of _should_clear_all_soft_refs.126// _should_clear_all_soft_refs is set to false as a side effect.127bool use_should_clear_all_soft_refs(bool v);128bool all_soft_refs_clear() { return _all_soft_refs_clear; }129void set_all_soft_refs_clear(bool v) { _all_soft_refs_clear = v; }130131// Called by the GC after Soft Refs have been cleared to indicate132// that the request in _should_clear_all_soft_refs has been fulfilled.133void cleared_all_soft_refs();134135// Identification methods.136virtual GenCollectorPolicy* as_generation_policy() { return NULL; }137virtual TwoGenerationCollectorPolicy* as_two_generation_policy() { return NULL; }138virtual MarkSweepPolicy* as_mark_sweep_policy() { return NULL; }139#if INCLUDE_ALL_GCS140virtual ConcurrentMarkSweepPolicy* as_concurrent_mark_sweep_policy() { return NULL; }141virtual G1CollectorPolicy* as_g1_policy() { return NULL; }142#endif // INCLUDE_ALL_GCS143// Note that these are not virtual.144bool is_generation_policy() { return as_generation_policy() != NULL; }145bool is_two_generation_policy() { return as_two_generation_policy() != NULL; }146bool is_mark_sweep_policy() { return as_mark_sweep_policy() != NULL; }147#if INCLUDE_ALL_GCS148bool is_concurrent_mark_sweep_policy() { return as_concurrent_mark_sweep_policy() != NULL; }149bool is_g1_policy() { return as_g1_policy() != NULL; }150#else // INCLUDE_ALL_GCS151bool is_concurrent_mark_sweep_policy() { return false; }152bool is_g1_policy() { return false; }153#endif // INCLUDE_ALL_GCS154155156virtual BarrierSet::Name barrier_set_name() = 0;157158// Create the remembered set (to cover the given reserved region,159// allowing breaking up into at most "max_covered_regions").160virtual GenRemSet* create_rem_set(MemRegion reserved,161int max_covered_regions);162163// This method controls how a collector satisfies a request164// for a block of memory. "gc_time_limit_was_exceeded" will165// be set to true if the adaptive size policy determine that166// an excessive amount of time is being spent doing collections167// and caused a NULL to be returned. If a NULL is not returned,168// "gc_time_limit_was_exceeded" has an undefined meaning.169virtual HeapWord* mem_allocate_work(size_t size,170bool is_tlab,171bool* gc_overhead_limit_was_exceeded) = 0;172173// This method controls how a collector handles one or more174// of its generations being fully allocated.175virtual HeapWord *satisfy_failed_allocation(size_t size, bool is_tlab) = 0;176// This method controls how a collector handles a metadata allocation177// failure.178virtual MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,179size_t size,180Metaspace::MetadataType mdtype);181182// Performace Counter support183GCPolicyCounters* counters() { return _gc_policy_counters; }184185// Create the jstat counters for the GC policy. By default, policy's186// don't have associated counters, and we complain if this is invoked.187virtual void initialize_gc_policy_counters() {188ShouldNotReachHere();189}190191virtual CollectorPolicy::Name kind() {192return CollectorPolicy::CollectorPolicyKind;193}194195// Returns true if a collector has eden space with soft end.196virtual bool has_soft_ended_eden() {197return false;198}199200// Do any updates required to global flags that are due to heap initialization201// changes202virtual void post_heap_initialize() = 0;203};204205class ClearedAllSoftRefs : public StackObj {206bool _clear_all_soft_refs;207CollectorPolicy* _collector_policy;208public:209ClearedAllSoftRefs(bool clear_all_soft_refs,210CollectorPolicy* collector_policy) :211_clear_all_soft_refs(clear_all_soft_refs),212_collector_policy(collector_policy) {}213214~ClearedAllSoftRefs() {215if (_clear_all_soft_refs) {216_collector_policy->cleared_all_soft_refs();217}218}219};220221class GenCollectorPolicy : public CollectorPolicy {222friend class TestGenCollectorPolicy;223protected:224size_t _min_gen0_size;225size_t _initial_gen0_size;226size_t _max_gen0_size;227228// _gen_alignment and _space_alignment will have the same value most of the229// time. When using large pages they can differ.230size_t _gen_alignment;231232GenerationSpec **_generations;233234// Return true if an allocation should be attempted in the older235// generation if it fails in the younger generation. Return236// false, otherwise.237virtual bool should_try_older_generation_allocation(size_t word_size) const;238239void initialize_flags();240void initialize_size_info();241242DEBUG_ONLY(void assert_flags();)243DEBUG_ONLY(void assert_size_info();)244245// Try to allocate space by expanding the heap.246virtual HeapWord* expand_heap_and_allocate(size_t size, bool is_tlab);247248// Compute max heap alignment249size_t compute_max_alignment();250251// Scale the base_size by NewRatio according to252// result = base_size / (NewRatio + 1)253// and align by min_alignment()254size_t scale_by_NewRatio_aligned(size_t base_size);255256// Bound the value by the given maximum minus the min_alignment257size_t bound_minus_alignment(size_t desired_size, size_t maximum_size);258259public:260GenCollectorPolicy();261262// Accessors263size_t min_gen0_size() { return _min_gen0_size; }264size_t initial_gen0_size() { return _initial_gen0_size; }265size_t max_gen0_size() { return _max_gen0_size; }266size_t gen_alignment() { return _gen_alignment; }267268virtual int number_of_generations() = 0;269270virtual GenerationSpec **generations() {271assert(_generations != NULL, "Sanity check");272return _generations;273}274275virtual GenCollectorPolicy* as_generation_policy() { return this; }276277virtual void initialize_generations() { };278279virtual void initialize_all() {280CollectorPolicy::initialize_all();281initialize_generations();282}283284size_t young_gen_size_lower_bound();285286HeapWord* mem_allocate_work(size_t size,287bool is_tlab,288bool* gc_overhead_limit_was_exceeded);289290HeapWord *satisfy_failed_allocation(size_t size, bool is_tlab);291292// Adaptive size policy293virtual void initialize_size_policy(size_t init_eden_size,294size_t init_promo_size,295size_t init_survivor_size);296297virtual void post_heap_initialize() {298assert(_max_gen0_size == MaxNewSize, "Should be taken care of by initialize_size_info");299}300};301302// All of hotspot's current collectors are subtypes of this303// class. Currently, these collectors all use the same gen[0],304// but have different gen[1] types. If we add another subtype305// of CollectorPolicy, this class should be broken out into306// its own file.307308class TwoGenerationCollectorPolicy : public GenCollectorPolicy {309protected:310size_t _min_gen1_size;311size_t _initial_gen1_size;312size_t _max_gen1_size;313314void initialize_flags();315void initialize_size_info();316317DEBUG_ONLY(void assert_flags();)318DEBUG_ONLY(void assert_size_info();)319320public:321TwoGenerationCollectorPolicy() : GenCollectorPolicy(), _min_gen1_size(0),322_initial_gen1_size(0), _max_gen1_size(0) {}323324// Accessors325size_t min_gen1_size() { return _min_gen1_size; }326size_t initial_gen1_size() { return _initial_gen1_size; }327size_t max_gen1_size() { return _max_gen1_size; }328329// Inherited methods330TwoGenerationCollectorPolicy* as_two_generation_policy() { return this; }331332int number_of_generations() { return 2; }333BarrierSet::Name barrier_set_name() { return BarrierSet::CardTableModRef; }334335virtual CollectorPolicy::Name kind() {336return CollectorPolicy::TwoGenerationCollectorPolicyKind;337}338339// Returns true if gen0 sizes were adjusted340bool adjust_gen0_sizes(size_t* gen0_size_ptr, size_t* gen1_size_ptr,341const size_t heap_size);342};343344class MarkSweepPolicy : public TwoGenerationCollectorPolicy {345protected:346void initialize_alignments();347void initialize_generations();348349public:350MarkSweepPolicy() {}351352MarkSweepPolicy* as_mark_sweep_policy() { return this; }353354void initialize_gc_policy_counters();355};356357#endif // SHARE_VM_MEMORY_COLLECTORPOLICY_HPP358359360