aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_MEMORY_COLLECTORPOLICY_HPP aoqi@0: #define SHARE_VM_MEMORY_COLLECTORPOLICY_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "memory/barrierSet.hpp" aoqi@0: #include "memory/generationSpec.hpp" aoqi@0: #include "memory/genRemSet.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: aoqi@0: // This class (or more correctly, subtypes of this class) aoqi@0: // are used to define global garbage collector attributes. aoqi@0: // This includes initialization of generations and any other aoqi@0: // shared resources they may need. aoqi@0: // aoqi@0: // In general, all flag adjustment and validation should be aoqi@0: // done in initialize_flags(), which is called prior to aoqi@0: // initialize_size_info(). aoqi@0: // aoqi@0: // This class is not fully developed yet. As more collector(s) aoqi@0: // are added, it is expected that we will come across further aoqi@0: // behavior that requires global attention. The correct place aoqi@0: // to deal with those issues is this class. aoqi@0: aoqi@0: // Forward declarations. aoqi@0: class GenCollectorPolicy; aoqi@0: class TwoGenerationCollectorPolicy; aoqi@0: class AdaptiveSizePolicy; aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: class ConcurrentMarkSweepPolicy; aoqi@0: class G1CollectorPolicy; aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: class GCPolicyCounters; aoqi@0: class MarkSweepPolicy; aoqi@0: aoqi@0: class CollectorPolicy : public CHeapObj { aoqi@0: protected: aoqi@0: GCPolicyCounters* _gc_policy_counters; aoqi@0: aoqi@0: virtual void initialize_alignments() = 0; aoqi@0: virtual void initialize_flags(); aoqi@0: virtual void initialize_size_info(); aoqi@0: aoqi@0: DEBUG_ONLY(virtual void assert_flags();) aoqi@0: DEBUG_ONLY(virtual void assert_size_info();) aoqi@0: aoqi@0: size_t _initial_heap_byte_size; aoqi@0: size_t _max_heap_byte_size; aoqi@0: size_t _min_heap_byte_size; aoqi@0: aoqi@0: size_t _space_alignment; aoqi@0: size_t _heap_alignment; aoqi@0: aoqi@0: // Needed to keep information if MaxHeapSize was set on the command line aoqi@0: // when the flag value is aligned etc by ergonomics aoqi@0: bool _max_heap_size_cmdline; aoqi@0: aoqi@0: // The sizing of the heap are controlled by a sizing policy. aoqi@0: AdaptiveSizePolicy* _size_policy; aoqi@0: aoqi@0: // Set to true when policy wants soft refs cleared. aoqi@0: // Reset to false by gc after it clears all soft refs. aoqi@0: bool _should_clear_all_soft_refs; aoqi@0: aoqi@0: // Set to true by the GC if the just-completed gc cleared all aoqi@0: // softrefs. This is set to true whenever a gc clears all softrefs, and aoqi@0: // set to false each time gc returns to the mutator. For example, in the aoqi@0: // ParallelScavengeHeap case the latter would be done toward the end of aoqi@0: // mem_allocate() where it returns op.result() aoqi@0: bool _all_soft_refs_clear; aoqi@0: aoqi@0: CollectorPolicy(); aoqi@0: aoqi@0: public: aoqi@0: virtual void initialize_all() { aoqi@0: initialize_alignments(); aoqi@0: initialize_flags(); aoqi@0: initialize_size_info(); aoqi@0: } aoqi@0: aoqi@0: // Return maximum heap alignment that may be imposed by the policy aoqi@0: static size_t compute_heap_alignment(); aoqi@0: aoqi@0: size_t space_alignment() { return _space_alignment; } aoqi@0: size_t heap_alignment() { return _heap_alignment; } aoqi@0: aoqi@0: size_t initial_heap_byte_size() { return _initial_heap_byte_size; } aoqi@0: size_t max_heap_byte_size() { return _max_heap_byte_size; } aoqi@0: size_t min_heap_byte_size() { return _min_heap_byte_size; } aoqi@0: aoqi@0: enum Name { aoqi@0: CollectorPolicyKind, aoqi@0: TwoGenerationCollectorPolicyKind, aoqi@0: ConcurrentMarkSweepPolicyKind, aoqi@0: ASConcurrentMarkSweepPolicyKind, aoqi@0: G1CollectorPolicyKind aoqi@0: }; aoqi@0: aoqi@0: AdaptiveSizePolicy* size_policy() { return _size_policy; } aoqi@0: bool should_clear_all_soft_refs() { return _should_clear_all_soft_refs; } aoqi@0: void set_should_clear_all_soft_refs(bool v) { _should_clear_all_soft_refs = v; } aoqi@0: // Returns the current value of _should_clear_all_soft_refs. aoqi@0: // _should_clear_all_soft_refs is set to false as a side effect. aoqi@0: bool use_should_clear_all_soft_refs(bool v); aoqi@0: bool all_soft_refs_clear() { return _all_soft_refs_clear; } aoqi@0: void set_all_soft_refs_clear(bool v) { _all_soft_refs_clear = v; } aoqi@0: aoqi@0: // Called by the GC after Soft Refs have been cleared to indicate aoqi@0: // that the request in _should_clear_all_soft_refs has been fulfilled. aoqi@0: void cleared_all_soft_refs(); aoqi@0: aoqi@0: // Identification methods. aoqi@0: virtual GenCollectorPolicy* as_generation_policy() { return NULL; } aoqi@0: virtual TwoGenerationCollectorPolicy* as_two_generation_policy() { return NULL; } aoqi@0: virtual MarkSweepPolicy* as_mark_sweep_policy() { return NULL; } aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: virtual ConcurrentMarkSweepPolicy* as_concurrent_mark_sweep_policy() { return NULL; } aoqi@0: virtual G1CollectorPolicy* as_g1_policy() { return NULL; } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: // Note that these are not virtual. aoqi@0: bool is_generation_policy() { return as_generation_policy() != NULL; } aoqi@0: bool is_two_generation_policy() { return as_two_generation_policy() != NULL; } aoqi@0: bool is_mark_sweep_policy() { return as_mark_sweep_policy() != NULL; } aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: bool is_concurrent_mark_sweep_policy() { return as_concurrent_mark_sweep_policy() != NULL; } aoqi@0: bool is_g1_policy() { return as_g1_policy() != NULL; } aoqi@0: #else // INCLUDE_ALL_GCS aoqi@0: bool is_concurrent_mark_sweep_policy() { return false; } aoqi@0: bool is_g1_policy() { return false; } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: aoqi@0: virtual BarrierSet::Name barrier_set_name() = 0; aoqi@0: aoqi@0: // Create the remembered set (to cover the given reserved region, aoqi@0: // allowing breaking up into at most "max_covered_regions"). aoqi@0: virtual GenRemSet* create_rem_set(MemRegion reserved, aoqi@0: int max_covered_regions); aoqi@0: aoqi@0: // This method controls how a collector satisfies a request aoqi@0: // for a block of memory. "gc_time_limit_was_exceeded" will aoqi@0: // be set to true if the adaptive size policy determine that aoqi@0: // an excessive amount of time is being spent doing collections aoqi@0: // and caused a NULL to be returned. If a NULL is not returned, aoqi@0: // "gc_time_limit_was_exceeded" has an undefined meaning. aoqi@0: virtual HeapWord* mem_allocate_work(size_t size, aoqi@0: bool is_tlab, aoqi@0: bool* gc_overhead_limit_was_exceeded) = 0; aoqi@0: aoqi@0: // This method controls how a collector handles one or more aoqi@0: // of its generations being fully allocated. aoqi@0: virtual HeapWord *satisfy_failed_allocation(size_t size, bool is_tlab) = 0; aoqi@0: // This method controls how a collector handles a metadata allocation aoqi@0: // failure. aoqi@0: virtual MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data, aoqi@0: size_t size, aoqi@0: Metaspace::MetadataType mdtype); aoqi@0: aoqi@0: // Performace Counter support aoqi@0: GCPolicyCounters* counters() { return _gc_policy_counters; } aoqi@0: aoqi@0: // Create the jstat counters for the GC policy. By default, policy's aoqi@0: // don't have associated counters, and we complain if this is invoked. aoqi@0: virtual void initialize_gc_policy_counters() { aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: virtual CollectorPolicy::Name kind() { aoqi@0: return CollectorPolicy::CollectorPolicyKind; aoqi@0: } aoqi@0: aoqi@0: // Returns true if a collector has eden space with soft end. aoqi@0: virtual bool has_soft_ended_eden() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Do any updates required to global flags that are due to heap initialization aoqi@0: // changes aoqi@0: virtual void post_heap_initialize() = 0; aoqi@0: }; aoqi@0: aoqi@0: class ClearedAllSoftRefs : public StackObj { aoqi@0: bool _clear_all_soft_refs; aoqi@0: CollectorPolicy* _collector_policy; aoqi@0: public: aoqi@0: ClearedAllSoftRefs(bool clear_all_soft_refs, aoqi@0: CollectorPolicy* collector_policy) : aoqi@0: _clear_all_soft_refs(clear_all_soft_refs), aoqi@0: _collector_policy(collector_policy) {} aoqi@0: aoqi@0: ~ClearedAllSoftRefs() { aoqi@0: if (_clear_all_soft_refs) { aoqi@0: _collector_policy->cleared_all_soft_refs(); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class GenCollectorPolicy : public CollectorPolicy { aoqi@0: friend class TestGenCollectorPolicy; aoqi@0: protected: aoqi@0: size_t _min_gen0_size; aoqi@0: size_t _initial_gen0_size; aoqi@0: size_t _max_gen0_size; aoqi@0: aoqi@0: // _gen_alignment and _space_alignment will have the same value most of the aoqi@0: // time. When using large pages they can differ. aoqi@0: size_t _gen_alignment; aoqi@0: aoqi@0: GenerationSpec **_generations; aoqi@0: aoqi@0: // Return true if an allocation should be attempted in the older aoqi@0: // generation if it fails in the younger generation. Return aoqi@0: // false, otherwise. aoqi@0: virtual bool should_try_older_generation_allocation(size_t word_size) const; aoqi@0: aoqi@0: void initialize_flags(); aoqi@0: void initialize_size_info(); aoqi@0: aoqi@0: DEBUG_ONLY(void assert_flags();) aoqi@0: DEBUG_ONLY(void assert_size_info();) aoqi@0: aoqi@0: // Try to allocate space by expanding the heap. aoqi@0: virtual HeapWord* expand_heap_and_allocate(size_t size, bool is_tlab); aoqi@0: aoqi@0: // Compute max heap alignment aoqi@0: size_t compute_max_alignment(); aoqi@0: aoqi@0: // Scale the base_size by NewRatio according to aoqi@0: // result = base_size / (NewRatio + 1) aoqi@0: // and align by min_alignment() aoqi@0: size_t scale_by_NewRatio_aligned(size_t base_size); aoqi@0: aoqi@0: // Bound the value by the given maximum minus the min_alignment aoqi@0: size_t bound_minus_alignment(size_t desired_size, size_t maximum_size); aoqi@0: aoqi@0: public: aoqi@0: GenCollectorPolicy(); aoqi@0: aoqi@0: // Accessors aoqi@0: size_t min_gen0_size() { return _min_gen0_size; } aoqi@0: size_t initial_gen0_size() { return _initial_gen0_size; } aoqi@0: size_t max_gen0_size() { return _max_gen0_size; } aoqi@0: size_t gen_alignment() { return _gen_alignment; } aoqi@0: aoqi@0: virtual int number_of_generations() = 0; aoqi@0: aoqi@0: virtual GenerationSpec **generations() { aoqi@0: assert(_generations != NULL, "Sanity check"); aoqi@0: return _generations; aoqi@0: } aoqi@0: aoqi@0: virtual GenCollectorPolicy* as_generation_policy() { return this; } aoqi@0: aoqi@0: virtual void initialize_generations() { }; aoqi@0: aoqi@0: virtual void initialize_all() { aoqi@0: CollectorPolicy::initialize_all(); aoqi@0: initialize_generations(); aoqi@0: } aoqi@0: aoqi@0: size_t young_gen_size_lower_bound(); aoqi@0: aoqi@0: HeapWord* mem_allocate_work(size_t size, aoqi@0: bool is_tlab, aoqi@0: bool* gc_overhead_limit_was_exceeded); aoqi@0: aoqi@0: HeapWord *satisfy_failed_allocation(size_t size, bool is_tlab); aoqi@0: aoqi@0: // Adaptive size policy aoqi@0: virtual void initialize_size_policy(size_t init_eden_size, aoqi@0: size_t init_promo_size, aoqi@0: size_t init_survivor_size); aoqi@0: aoqi@0: virtual void post_heap_initialize() { aoqi@0: assert(_max_gen0_size == MaxNewSize, "Should be taken care of by initialize_size_info"); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // All of hotspot's current collectors are subtypes of this aoqi@0: // class. Currently, these collectors all use the same gen[0], aoqi@0: // but have different gen[1] types. If we add another subtype aoqi@0: // of CollectorPolicy, this class should be broken out into aoqi@0: // its own file. aoqi@0: aoqi@0: class TwoGenerationCollectorPolicy : public GenCollectorPolicy { aoqi@0: protected: aoqi@0: size_t _min_gen1_size; aoqi@0: size_t _initial_gen1_size; aoqi@0: size_t _max_gen1_size; aoqi@0: aoqi@0: void initialize_flags(); aoqi@0: void initialize_size_info(); aoqi@0: aoqi@0: DEBUG_ONLY(void assert_flags();) aoqi@0: DEBUG_ONLY(void assert_size_info();) aoqi@0: aoqi@0: public: aoqi@0: TwoGenerationCollectorPolicy() : GenCollectorPolicy(), _min_gen1_size(0), aoqi@0: _initial_gen1_size(0), _max_gen1_size(0) {} aoqi@0: aoqi@0: // Accessors aoqi@0: size_t min_gen1_size() { return _min_gen1_size; } aoqi@0: size_t initial_gen1_size() { return _initial_gen1_size; } aoqi@0: size_t max_gen1_size() { return _max_gen1_size; } aoqi@0: aoqi@0: // Inherited methods aoqi@0: TwoGenerationCollectorPolicy* as_two_generation_policy() { return this; } aoqi@0: aoqi@0: int number_of_generations() { return 2; } aoqi@0: BarrierSet::Name barrier_set_name() { return BarrierSet::CardTableModRef; } aoqi@0: aoqi@0: virtual CollectorPolicy::Name kind() { aoqi@0: return CollectorPolicy::TwoGenerationCollectorPolicyKind; aoqi@0: } aoqi@0: aoqi@0: // Returns true if gen0 sizes were adjusted aoqi@0: bool adjust_gen0_sizes(size_t* gen0_size_ptr, size_t* gen1_size_ptr, aoqi@0: const size_t heap_size); aoqi@0: }; aoqi@0: aoqi@0: class MarkSweepPolicy : public TwoGenerationCollectorPolicy { aoqi@0: protected: aoqi@0: void initialize_alignments(); aoqi@0: void initialize_generations(); aoqi@0: aoqi@0: public: aoqi@0: MarkSweepPolicy() {} aoqi@0: aoqi@0: MarkSweepPolicy* as_mark_sweep_policy() { return this; } aoqi@0: aoqi@0: void initialize_gc_policy_counters(); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_MEMORY_COLLECTORPOLICY_HPP