src/share/vm/memory/collectorPolicy.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_COLLECTORPOLICY_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_COLLECTORPOLICY_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "memory/barrierSet.hpp"
aoqi@0 30 #include "memory/generationSpec.hpp"
aoqi@0 31 #include "memory/genRemSet.hpp"
aoqi@0 32 #include "utilities/macros.hpp"
aoqi@0 33
aoqi@0 34 // This class (or more correctly, subtypes of this class)
aoqi@0 35 // are used to define global garbage collector attributes.
aoqi@0 36 // This includes initialization of generations and any other
aoqi@0 37 // shared resources they may need.
aoqi@0 38 //
aoqi@0 39 // In general, all flag adjustment and validation should be
aoqi@0 40 // done in initialize_flags(), which is called prior to
aoqi@0 41 // initialize_size_info().
aoqi@0 42 //
aoqi@0 43 // This class is not fully developed yet. As more collector(s)
aoqi@0 44 // are added, it is expected that we will come across further
aoqi@0 45 // behavior that requires global attention. The correct place
aoqi@0 46 // to deal with those issues is this class.
aoqi@0 47
aoqi@0 48 // Forward declarations.
aoqi@0 49 class GenCollectorPolicy;
aoqi@0 50 class TwoGenerationCollectorPolicy;
aoqi@0 51 class AdaptiveSizePolicy;
aoqi@0 52 #if INCLUDE_ALL_GCS
aoqi@0 53 class ConcurrentMarkSweepPolicy;
aoqi@0 54 class G1CollectorPolicy;
aoqi@0 55 #endif // INCLUDE_ALL_GCS
aoqi@0 56
aoqi@0 57 class GCPolicyCounters;
aoqi@0 58 class MarkSweepPolicy;
aoqi@0 59
aoqi@0 60 class CollectorPolicy : public CHeapObj<mtGC> {
aoqi@0 61 protected:
aoqi@0 62 GCPolicyCounters* _gc_policy_counters;
aoqi@0 63
aoqi@0 64 virtual void initialize_alignments() = 0;
aoqi@0 65 virtual void initialize_flags();
aoqi@0 66 virtual void initialize_size_info();
aoqi@0 67
aoqi@0 68 DEBUG_ONLY(virtual void assert_flags();)
aoqi@0 69 DEBUG_ONLY(virtual void assert_size_info();)
aoqi@0 70
aoqi@0 71 size_t _initial_heap_byte_size;
aoqi@0 72 size_t _max_heap_byte_size;
aoqi@0 73 size_t _min_heap_byte_size;
aoqi@0 74
aoqi@0 75 size_t _space_alignment;
aoqi@0 76 size_t _heap_alignment;
aoqi@0 77
aoqi@0 78 // Needed to keep information if MaxHeapSize was set on the command line
aoqi@0 79 // when the flag value is aligned etc by ergonomics
aoqi@0 80 bool _max_heap_size_cmdline;
aoqi@0 81
aoqi@0 82 // The sizing of the heap are controlled by a sizing policy.
aoqi@0 83 AdaptiveSizePolicy* _size_policy;
aoqi@0 84
aoqi@0 85 // Set to true when policy wants soft refs cleared.
aoqi@0 86 // Reset to false by gc after it clears all soft refs.
aoqi@0 87 bool _should_clear_all_soft_refs;
aoqi@0 88
aoqi@0 89 // Set to true by the GC if the just-completed gc cleared all
aoqi@0 90 // softrefs. This is set to true whenever a gc clears all softrefs, and
aoqi@0 91 // set to false each time gc returns to the mutator. For example, in the
aoqi@0 92 // ParallelScavengeHeap case the latter would be done toward the end of
aoqi@0 93 // mem_allocate() where it returns op.result()
aoqi@0 94 bool _all_soft_refs_clear;
aoqi@0 95
aoqi@0 96 CollectorPolicy();
aoqi@0 97
aoqi@0 98 public:
aoqi@0 99 virtual void initialize_all() {
aoqi@0 100 initialize_alignments();
aoqi@0 101 initialize_flags();
aoqi@0 102 initialize_size_info();
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 // Return maximum heap alignment that may be imposed by the policy
aoqi@0 106 static size_t compute_heap_alignment();
aoqi@0 107
aoqi@0 108 size_t space_alignment() { return _space_alignment; }
aoqi@0 109 size_t heap_alignment() { return _heap_alignment; }
aoqi@0 110
aoqi@0 111 size_t initial_heap_byte_size() { return _initial_heap_byte_size; }
aoqi@0 112 size_t max_heap_byte_size() { return _max_heap_byte_size; }
aoqi@0 113 size_t min_heap_byte_size() { return _min_heap_byte_size; }
aoqi@0 114
aoqi@0 115 enum Name {
aoqi@0 116 CollectorPolicyKind,
aoqi@0 117 TwoGenerationCollectorPolicyKind,
aoqi@0 118 ConcurrentMarkSweepPolicyKind,
aoqi@0 119 ASConcurrentMarkSweepPolicyKind,
aoqi@0 120 G1CollectorPolicyKind
aoqi@0 121 };
aoqi@0 122
aoqi@0 123 AdaptiveSizePolicy* size_policy() { return _size_policy; }
aoqi@0 124 bool should_clear_all_soft_refs() { return _should_clear_all_soft_refs; }
aoqi@0 125 void set_should_clear_all_soft_refs(bool v) { _should_clear_all_soft_refs = v; }
aoqi@0 126 // Returns the current value of _should_clear_all_soft_refs.
aoqi@0 127 // _should_clear_all_soft_refs is set to false as a side effect.
aoqi@0 128 bool use_should_clear_all_soft_refs(bool v);
aoqi@0 129 bool all_soft_refs_clear() { return _all_soft_refs_clear; }
aoqi@0 130 void set_all_soft_refs_clear(bool v) { _all_soft_refs_clear = v; }
aoqi@0 131
aoqi@0 132 // Called by the GC after Soft Refs have been cleared to indicate
aoqi@0 133 // that the request in _should_clear_all_soft_refs has been fulfilled.
aoqi@0 134 void cleared_all_soft_refs();
aoqi@0 135
aoqi@0 136 // Identification methods.
aoqi@0 137 virtual GenCollectorPolicy* as_generation_policy() { return NULL; }
aoqi@0 138 virtual TwoGenerationCollectorPolicy* as_two_generation_policy() { return NULL; }
aoqi@0 139 virtual MarkSweepPolicy* as_mark_sweep_policy() { return NULL; }
aoqi@0 140 #if INCLUDE_ALL_GCS
aoqi@0 141 virtual ConcurrentMarkSweepPolicy* as_concurrent_mark_sweep_policy() { return NULL; }
aoqi@0 142 virtual G1CollectorPolicy* as_g1_policy() { return NULL; }
aoqi@0 143 #endif // INCLUDE_ALL_GCS
aoqi@0 144 // Note that these are not virtual.
aoqi@0 145 bool is_generation_policy() { return as_generation_policy() != NULL; }
aoqi@0 146 bool is_two_generation_policy() { return as_two_generation_policy() != NULL; }
aoqi@0 147 bool is_mark_sweep_policy() { return as_mark_sweep_policy() != NULL; }
aoqi@0 148 #if INCLUDE_ALL_GCS
aoqi@0 149 bool is_concurrent_mark_sweep_policy() { return as_concurrent_mark_sweep_policy() != NULL; }
aoqi@0 150 bool is_g1_policy() { return as_g1_policy() != NULL; }
aoqi@0 151 #else // INCLUDE_ALL_GCS
aoqi@0 152 bool is_concurrent_mark_sweep_policy() { return false; }
aoqi@0 153 bool is_g1_policy() { return false; }
aoqi@0 154 #endif // INCLUDE_ALL_GCS
aoqi@0 155
aoqi@0 156
aoqi@0 157 virtual BarrierSet::Name barrier_set_name() = 0;
aoqi@0 158
aoqi@0 159 // Create the remembered set (to cover the given reserved region,
aoqi@0 160 // allowing breaking up into at most "max_covered_regions").
aoqi@0 161 virtual GenRemSet* create_rem_set(MemRegion reserved,
aoqi@0 162 int max_covered_regions);
aoqi@0 163
aoqi@0 164 // This method controls how a collector satisfies a request
aoqi@0 165 // for a block of memory. "gc_time_limit_was_exceeded" will
aoqi@0 166 // be set to true if the adaptive size policy determine that
aoqi@0 167 // an excessive amount of time is being spent doing collections
aoqi@0 168 // and caused a NULL to be returned. If a NULL is not returned,
aoqi@0 169 // "gc_time_limit_was_exceeded" has an undefined meaning.
aoqi@0 170 virtual HeapWord* mem_allocate_work(size_t size,
aoqi@0 171 bool is_tlab,
aoqi@0 172 bool* gc_overhead_limit_was_exceeded) = 0;
aoqi@0 173
aoqi@0 174 // This method controls how a collector handles one or more
aoqi@0 175 // of its generations being fully allocated.
aoqi@0 176 virtual HeapWord *satisfy_failed_allocation(size_t size, bool is_tlab) = 0;
aoqi@0 177 // This method controls how a collector handles a metadata allocation
aoqi@0 178 // failure.
aoqi@0 179 virtual MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
aoqi@0 180 size_t size,
aoqi@0 181 Metaspace::MetadataType mdtype);
aoqi@0 182
aoqi@0 183 // Performace Counter support
aoqi@0 184 GCPolicyCounters* counters() { return _gc_policy_counters; }
aoqi@0 185
aoqi@0 186 // Create the jstat counters for the GC policy. By default, policy's
aoqi@0 187 // don't have associated counters, and we complain if this is invoked.
aoqi@0 188 virtual void initialize_gc_policy_counters() {
aoqi@0 189 ShouldNotReachHere();
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 virtual CollectorPolicy::Name kind() {
aoqi@0 193 return CollectorPolicy::CollectorPolicyKind;
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 // Returns true if a collector has eden space with soft end.
aoqi@0 197 virtual bool has_soft_ended_eden() {
aoqi@0 198 return false;
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 // Do any updates required to global flags that are due to heap initialization
aoqi@0 202 // changes
aoqi@0 203 virtual void post_heap_initialize() = 0;
aoqi@0 204 };
aoqi@0 205
aoqi@0 206 class ClearedAllSoftRefs : public StackObj {
aoqi@0 207 bool _clear_all_soft_refs;
aoqi@0 208 CollectorPolicy* _collector_policy;
aoqi@0 209 public:
aoqi@0 210 ClearedAllSoftRefs(bool clear_all_soft_refs,
aoqi@0 211 CollectorPolicy* collector_policy) :
aoqi@0 212 _clear_all_soft_refs(clear_all_soft_refs),
aoqi@0 213 _collector_policy(collector_policy) {}
aoqi@0 214
aoqi@0 215 ~ClearedAllSoftRefs() {
aoqi@0 216 if (_clear_all_soft_refs) {
aoqi@0 217 _collector_policy->cleared_all_soft_refs();
aoqi@0 218 }
aoqi@0 219 }
aoqi@0 220 };
aoqi@0 221
aoqi@0 222 class GenCollectorPolicy : public CollectorPolicy {
aoqi@0 223 friend class TestGenCollectorPolicy;
aoqi@0 224 protected:
aoqi@0 225 size_t _min_gen0_size;
aoqi@0 226 size_t _initial_gen0_size;
aoqi@0 227 size_t _max_gen0_size;
aoqi@0 228
aoqi@0 229 // _gen_alignment and _space_alignment will have the same value most of the
aoqi@0 230 // time. When using large pages they can differ.
aoqi@0 231 size_t _gen_alignment;
aoqi@0 232
aoqi@0 233 GenerationSpec **_generations;
aoqi@0 234
aoqi@0 235 // Return true if an allocation should be attempted in the older
aoqi@0 236 // generation if it fails in the younger generation. Return
aoqi@0 237 // false, otherwise.
aoqi@0 238 virtual bool should_try_older_generation_allocation(size_t word_size) const;
aoqi@0 239
aoqi@0 240 void initialize_flags();
aoqi@0 241 void initialize_size_info();
aoqi@0 242
aoqi@0 243 DEBUG_ONLY(void assert_flags();)
aoqi@0 244 DEBUG_ONLY(void assert_size_info();)
aoqi@0 245
aoqi@0 246 // Try to allocate space by expanding the heap.
aoqi@0 247 virtual HeapWord* expand_heap_and_allocate(size_t size, bool is_tlab);
aoqi@0 248
aoqi@0 249 // Compute max heap alignment
aoqi@0 250 size_t compute_max_alignment();
aoqi@0 251
aoqi@0 252 // Scale the base_size by NewRatio according to
aoqi@0 253 // result = base_size / (NewRatio + 1)
aoqi@0 254 // and align by min_alignment()
aoqi@0 255 size_t scale_by_NewRatio_aligned(size_t base_size);
aoqi@0 256
aoqi@0 257 // Bound the value by the given maximum minus the min_alignment
aoqi@0 258 size_t bound_minus_alignment(size_t desired_size, size_t maximum_size);
aoqi@0 259
aoqi@0 260 public:
aoqi@0 261 GenCollectorPolicy();
aoqi@0 262
aoqi@0 263 // Accessors
aoqi@0 264 size_t min_gen0_size() { return _min_gen0_size; }
aoqi@0 265 size_t initial_gen0_size() { return _initial_gen0_size; }
aoqi@0 266 size_t max_gen0_size() { return _max_gen0_size; }
aoqi@0 267 size_t gen_alignment() { return _gen_alignment; }
aoqi@0 268
aoqi@0 269 virtual int number_of_generations() = 0;
aoqi@0 270
aoqi@0 271 virtual GenerationSpec **generations() {
aoqi@0 272 assert(_generations != NULL, "Sanity check");
aoqi@0 273 return _generations;
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 virtual GenCollectorPolicy* as_generation_policy() { return this; }
aoqi@0 277
aoqi@0 278 virtual void initialize_generations() { };
aoqi@0 279
aoqi@0 280 virtual void initialize_all() {
aoqi@0 281 CollectorPolicy::initialize_all();
aoqi@0 282 initialize_generations();
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 size_t young_gen_size_lower_bound();
aoqi@0 286
aoqi@0 287 HeapWord* mem_allocate_work(size_t size,
aoqi@0 288 bool is_tlab,
aoqi@0 289 bool* gc_overhead_limit_was_exceeded);
aoqi@0 290
aoqi@0 291 HeapWord *satisfy_failed_allocation(size_t size, bool is_tlab);
aoqi@0 292
aoqi@0 293 // Adaptive size policy
aoqi@0 294 virtual void initialize_size_policy(size_t init_eden_size,
aoqi@0 295 size_t init_promo_size,
aoqi@0 296 size_t init_survivor_size);
aoqi@0 297
aoqi@0 298 virtual void post_heap_initialize() {
aoqi@0 299 assert(_max_gen0_size == MaxNewSize, "Should be taken care of by initialize_size_info");
aoqi@0 300 }
aoqi@0 301 };
aoqi@0 302
aoqi@0 303 // All of hotspot's current collectors are subtypes of this
aoqi@0 304 // class. Currently, these collectors all use the same gen[0],
aoqi@0 305 // but have different gen[1] types. If we add another subtype
aoqi@0 306 // of CollectorPolicy, this class should be broken out into
aoqi@0 307 // its own file.
aoqi@0 308
aoqi@0 309 class TwoGenerationCollectorPolicy : public GenCollectorPolicy {
aoqi@0 310 protected:
aoqi@0 311 size_t _min_gen1_size;
aoqi@0 312 size_t _initial_gen1_size;
aoqi@0 313 size_t _max_gen1_size;
aoqi@0 314
aoqi@0 315 void initialize_flags();
aoqi@0 316 void initialize_size_info();
aoqi@0 317
aoqi@0 318 DEBUG_ONLY(void assert_flags();)
aoqi@0 319 DEBUG_ONLY(void assert_size_info();)
aoqi@0 320
aoqi@0 321 public:
aoqi@0 322 TwoGenerationCollectorPolicy() : GenCollectorPolicy(), _min_gen1_size(0),
aoqi@0 323 _initial_gen1_size(0), _max_gen1_size(0) {}
aoqi@0 324
aoqi@0 325 // Accessors
aoqi@0 326 size_t min_gen1_size() { return _min_gen1_size; }
aoqi@0 327 size_t initial_gen1_size() { return _initial_gen1_size; }
aoqi@0 328 size_t max_gen1_size() { return _max_gen1_size; }
aoqi@0 329
aoqi@0 330 // Inherited methods
aoqi@0 331 TwoGenerationCollectorPolicy* as_two_generation_policy() { return this; }
aoqi@0 332
aoqi@0 333 int number_of_generations() { return 2; }
aoqi@0 334 BarrierSet::Name barrier_set_name() { return BarrierSet::CardTableModRef; }
aoqi@0 335
aoqi@0 336 virtual CollectorPolicy::Name kind() {
aoqi@0 337 return CollectorPolicy::TwoGenerationCollectorPolicyKind;
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 // Returns true if gen0 sizes were adjusted
aoqi@0 341 bool adjust_gen0_sizes(size_t* gen0_size_ptr, size_t* gen1_size_ptr,
aoqi@0 342 const size_t heap_size);
aoqi@0 343 };
aoqi@0 344
aoqi@0 345 class MarkSweepPolicy : public TwoGenerationCollectorPolicy {
aoqi@0 346 protected:
aoqi@0 347 void initialize_alignments();
aoqi@0 348 void initialize_generations();
aoqi@0 349
aoqi@0 350 public:
aoqi@0 351 MarkSweepPolicy() {}
aoqi@0 352
aoqi@0 353 MarkSweepPolicy* as_mark_sweep_policy() { return this; }
aoqi@0 354
aoqi@0 355 void initialize_gc_policy_counters();
aoqi@0 356 };
aoqi@0 357
aoqi@0 358 #endif // SHARE_VM_MEMORY_COLLECTORPOLICY_HPP

mercurial