annotate src/share/vm/memory/collectorPolicy.hpp @ 1387:0bfd3fb24150

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