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