1 /* 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP 26 #define SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP 27 28 #include "gc_implementation/shared/adaptiveSizePolicy.hpp" 29 #include "memory/collectorPolicy.hpp" 30 #include "memory/generation.hpp" 31 #include "memory/sharedHeap.hpp" 32 33 class SubTasksDone; 34 35 // A "GenCollectedHeap" is a SharedHeap that uses generational 36 // collection. It is represented with a sequence of Generation's. 37 class GenCollectedHeap : public SharedHeap { 38 friend class GenCollectorPolicy; 39 friend class Generation; 40 friend class DefNewGeneration; 41 friend class TenuredGeneration; 42 friend class ConcurrentMarkSweepGeneration; 43 friend class CMSCollector; 44 friend class GenMarkSweep; 45 friend class VM_GenCollectForAllocation; 46 friend class VM_GenCollectForPermanentAllocation; 47 friend class VM_GenCollectFull; 48 friend class VM_GenCollectFullConcurrent; 49 friend class VM_GC_HeapInspection; 50 friend class VM_HeapDumper; 51 friend class HeapInspection; 52 friend class GCCauseSetter; 53 friend class VMStructs; 54 public: 55 enum SomeConstants { 56 max_gens = 10 57 }; 58 59 friend class VM_PopulateDumpSharedSpace; 60 61 protected: 62 // Fields: 63 static GenCollectedHeap* _gch; 64 65 private: 66 int _n_gens; 67 Generation* _gens[max_gens]; 68 GenerationSpec** _gen_specs; 69 70 // The generational collector policy. 71 GenCollectorPolicy* _gen_policy; 72 73 // If a generation would bail out of an incremental collection, 74 // it sets this flag. If the flag is set, satisfy_failed_allocation 75 // will attempt allocating in all generations before doing a full GC. 76 bool _incremental_collection_will_fail; 77 bool _last_incremental_collection_failed; 78 79 // In support of ExplicitGCInvokesConcurrent functionality 80 unsigned int _full_collections_completed; 81 82 // Data structure for claiming the (potentially) parallel tasks in 83 // (gen-specific) strong roots processing. 84 SubTasksDone* _gen_process_strong_tasks; 85 SubTasksDone* gen_process_strong_tasks() { return _gen_process_strong_tasks; } 86 87 // In block contents verification, the number of header words to skip 88 NOT_PRODUCT(static size_t _skip_header_HeapWords;) 89 90 // GC is not allowed during the dump of the shared classes. Keep track 91 // of this in order to provide an reasonable error message when terminating. 92 bool _preloading_shared_classes; 93 94 protected: 95 // Directs each generation up to and including "collectedGen" to recompute 96 // its desired size. 97 void compute_new_generation_sizes(int collectedGen); 98 99 // Helper functions for allocation 100 HeapWord* attempt_allocation(size_t size, 101 bool is_tlab, 102 bool first_only); 103 104 // Helper function for two callbacks below. 105 // Considers collection of the first max_level+1 generations. 106 void do_collection(bool full, 107 bool clear_all_soft_refs, 108 size_t size, 109 bool is_tlab, 110 int max_level); 111 112 // Callback from VM_GenCollectForAllocation operation. 113 // This function does everything necessary/possible to satisfy an 114 // allocation request that failed in the youngest generation that should 115 // have handled it (including collection, expansion, etc.) 116 HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab); 117 118 // Callback from VM_GenCollectFull operation. 119 // Perform a full collection of the first max_level+1 generations. 120 void do_full_collection(bool clear_all_soft_refs, int max_level); 121 122 // Does the "cause" of GC indicate that 123 // we absolutely __must__ clear soft refs? 124 bool must_clear_all_soft_refs(); 125 126 public: 127 GenCollectedHeap(GenCollectorPolicy *policy); 128 129 GCStats* gc_stats(int level) const; 130 131 // Returns JNI_OK on success 132 virtual jint initialize(); 133 char* allocate(size_t alignment, PermanentGenerationSpec* perm_gen_spec, 134 size_t* _total_reserved, int* _n_covered_regions, 135 ReservedSpace* heap_rs); 136 137 // Does operations required after initialization has been done. 138 void post_initialize(); 139 140 // Initialize ("weak") refs processing support 141 virtual void ref_processing_init(); 142 143 virtual CollectedHeap::Name kind() const { 144 return CollectedHeap::GenCollectedHeap; 145 } 146 147 // The generational collector policy. 148 GenCollectorPolicy* gen_policy() const { return _gen_policy; } 149 150 // Adaptive size policy 151 virtual AdaptiveSizePolicy* size_policy() { 152 return gen_policy()->size_policy(); 153 } 154 155 size_t capacity() const; 156 size_t used() const; 157 158 // Save the "used_region" for generations level and lower, 159 // and, if perm is true, for perm gen. 160 void save_used_regions(int level, bool perm); 161 162 size_t max_capacity() const; 163 164 HeapWord* mem_allocate(size_t size, 165 bool is_large_noref, 166 bool is_tlab, 167 bool* gc_overhead_limit_was_exceeded); 168 169 // We may support a shared contiguous allocation area, if the youngest 170 // generation does. 171 bool supports_inline_contig_alloc() const; 172 HeapWord** top_addr() const; 173 HeapWord** end_addr() const; 174 175 // Return an estimate of the maximum allocation that could be performed 176 // without triggering any collection activity. In a generational 177 // collector, for example, this is probably the largest allocation that 178 // could be supported in the youngest generation. It is "unsafe" because 179 // no locks are taken; the result should be treated as an approximation, 180 // not a guarantee. 181 size_t unsafe_max_alloc(); 182 183 // Does this heap support heap inspection? (+PrintClassHistogram) 184 virtual bool supports_heap_inspection() const { return true; } 185 186 // Perform a full collection of the heap; intended for use in implementing 187 // "System.gc". This implies as full a collection as the CollectedHeap 188 // supports. Caller does not hold the Heap_lock on entry. 189 void collect(GCCause::Cause cause); 190 191 // This interface assumes that it's being called by the 192 // vm thread. It collects the heap assuming that the 193 // heap lock is already held and that we are executing in 194 // the context of the vm thread. 195 void collect_as_vm_thread(GCCause::Cause cause); 196 197 // The same as above but assume that the caller holds the Heap_lock. 198 void collect_locked(GCCause::Cause cause); 199 200 // Perform a full collection of the first max_level+1 generations. 201 // Mostly used for testing purposes. Caller does not hold the Heap_lock on entry. 202 void collect(GCCause::Cause cause, int max_level); 203 204 // Returns "TRUE" iff "p" points into the allocated area of the heap. 205 // The methods is_in(), is_in_closed_subset() and is_in_youngest() may 206 // be expensive to compute in general, so, to prevent 207 // their inadvertent use in product jvm's, we restrict their use to 208 // assertion checking or verification only. 209 bool is_in(const void* p) const; 210 211 // override 212 bool is_in_closed_subset(const void* p) const { 213 if (UseConcMarkSweepGC) { 214 return is_in_reserved(p); 215 } else { 216 return is_in(p); 217 } 218 } 219 220 // Returns "TRUE" iff "p" points into the youngest generation. 221 bool is_in_youngest(void* p); 222 223 // Iteration functions. 224 void oop_iterate(OopClosure* cl); 225 void oop_iterate(MemRegion mr, OopClosure* cl); 226 void object_iterate(ObjectClosure* cl); 227 void safe_object_iterate(ObjectClosure* cl); 228 void object_iterate_since_last_GC(ObjectClosure* cl); 229 Space* space_containing(const void* addr) const; 230 231 // A CollectedHeap is divided into a dense sequence of "blocks"; that is, 232 // each address in the (reserved) heap is a member of exactly 233 // one block. The defining characteristic of a block is that it is 234 // possible to find its size, and thus to progress forward to the next 235 // block. (Blocks may be of different sizes.) Thus, blocks may 236 // represent Java objects, or they might be free blocks in a 237 // free-list-based heap (or subheap), as long as the two kinds are 238 // distinguishable and the size of each is determinable. 239 240 // Returns the address of the start of the "block" that contains the 241 // address "addr". We say "blocks" instead of "object" since some heaps 242 // may not pack objects densely; a chunk may either be an object or a 243 // non-object. 244 virtual HeapWord* block_start(const void* addr) const; 245 246 // Requires "addr" to be the start of a chunk, and returns its size. 247 // "addr + size" is required to be the start of a new chunk, or the end 248 // of the active area of the heap. Assumes (and verifies in non-product 249 // builds) that addr is in the allocated part of the heap and is 250 // the start of a chunk. 251 virtual size_t block_size(const HeapWord* addr) const; 252 253 // Requires "addr" to be the start of a block, and returns "TRUE" iff 254 // the block is an object. Assumes (and verifies in non-product 255 // builds) that addr is in the allocated part of the heap and is 256 // the start of a chunk. 257 virtual bool block_is_obj(const HeapWord* addr) const; 258 259 // Section on TLAB's. 260 virtual bool supports_tlab_allocation() const; 261 virtual size_t tlab_capacity(Thread* thr) const; 262 virtual size_t unsafe_max_tlab_alloc(Thread* thr) const; 263 virtual HeapWord* allocate_new_tlab(size_t size); 264 265 // Can a compiler initialize a new object without store barriers? 266 // This permission only extends from the creation of a new object 267 // via a TLAB up to the first subsequent safepoint. 268 virtual bool can_elide_tlab_store_barriers() const { 269 return true; 270 } 271 272 virtual bool card_mark_must_follow_store() const { 273 return UseConcMarkSweepGC; 274 } 275 276 // We don't need barriers for stores to objects in the 277 // young gen and, a fortiori, for initializing stores to 278 // objects therein. This applies to {DefNew,ParNew}+{Tenured,CMS} 279 // only and may need to be re-examined in case other 280 // kinds of collectors are implemented in the future. 281 virtual bool can_elide_initializing_store_barrier(oop new_obj) { 282 // We wanted to assert that:- 283 // assert(UseParNewGC || UseSerialGC || UseConcMarkSweepGC, 284 // "Check can_elide_initializing_store_barrier() for this collector"); 285 // but unfortunately the flag UseSerialGC need not necessarily always 286 // be set when DefNew+Tenured are being used. 287 return is_in_youngest((void*)new_obj); 288 } 289 290 // Can a compiler elide a store barrier when it writes 291 // a permanent oop into the heap? Applies when the compiler 292 // is storing x to the heap, where x->is_perm() is true. 293 virtual bool can_elide_permanent_oop_store_barriers() const { 294 // CMS needs to see all, even intra-generational, ref updates. 295 return !UseConcMarkSweepGC; 296 } 297 298 // The "requestor" generation is performing some garbage collection 299 // action for which it would be useful to have scratch space. The 300 // requestor promises to allocate no more than "max_alloc_words" in any 301 // older generation (via promotion say.) Any blocks of space that can 302 // be provided are returned as a list of ScratchBlocks, sorted by 303 // decreasing size. 304 ScratchBlock* gather_scratch(Generation* requestor, size_t max_alloc_words); 305 // Allow each generation to reset any scratch space that it has 306 // contributed as it needs. 307 void release_scratch(); 308 309 size_t large_typearray_limit(); 310 311 // Ensure parsability: override 312 virtual void ensure_parsability(bool retire_tlabs); 313 314 // Time in ms since the longest time a collector ran in 315 // in any generation. 316 virtual jlong millis_since_last_gc(); 317 318 // Total number of full collections completed. 319 unsigned int total_full_collections_completed() { 320 assert(_full_collections_completed <= _total_full_collections, 321 "Can't complete more collections than were started"); 322 return _full_collections_completed; 323 } 324 325 // Update above counter, as appropriate, at the end of a stop-world GC cycle 326 unsigned int update_full_collections_completed(); 327 // Update above counter, as appropriate, at the end of a concurrent GC cycle 328 unsigned int update_full_collections_completed(unsigned int count); 329 330 // Update "time of last gc" for all constituent generations 331 // to "now". 332 void update_time_of_last_gc(jlong now) { 333 for (int i = 0; i < _n_gens; i++) { 334 _gens[i]->update_time_of_last_gc(now); 335 } 336 perm_gen()->update_time_of_last_gc(now); 337 } 338 339 // Update the gc statistics for each generation. 340 // "level" is the level of the lastest collection 341 void update_gc_stats(int current_level, bool full) { 342 for (int i = 0; i < _n_gens; i++) { 343 _gens[i]->update_gc_stats(current_level, full); 344 } 345 perm_gen()->update_gc_stats(current_level, full); 346 } 347 348 // Override. 349 bool no_gc_in_progress() { return !is_gc_active(); } 350 351 // Override. 352 void prepare_for_verify(); 353 354 // Override. 355 void verify(bool allow_dirty, bool silent, bool /* option */); 356 357 // Override. 358 void print() const; 359 void print_on(outputStream* st) const; 360 virtual void print_gc_threads_on(outputStream* st) const; 361 virtual void gc_threads_do(ThreadClosure* tc) const; 362 virtual void print_tracing_info() const; 363 364 // PrintGC, PrintGCDetails support 365 void print_heap_change(size_t prev_used) const; 366 void print_perm_heap_change(size_t perm_prev_used) const; 367 368 // The functions below are helper functions that a subclass of 369 // "CollectedHeap" can use in the implementation of its virtual 370 // functions. 371 372 class GenClosure : public StackObj { 373 public: 374 virtual void do_generation(Generation* gen) = 0; 375 }; 376 377 // Apply "cl.do_generation" to all generations in the heap (not including 378 // the permanent generation). If "old_to_young" determines the order. 379 void generation_iterate(GenClosure* cl, bool old_to_young); 380 381 void space_iterate(SpaceClosure* cl); 382 383 // Return "true" if all generations (but perm) have reached the 384 // maximal committed limit that they can reach, without a garbage 385 // collection. 386 virtual bool is_maximal_no_gc() const; 387 388 // Return the generation before "gen", or else NULL. 389 Generation* prev_gen(Generation* gen) const { 390 int l = gen->level(); 391 if (l == 0) return NULL; 392 else return _gens[l-1]; 393 } 394 395 // Return the generation after "gen", or else NULL. 396 Generation* next_gen(Generation* gen) const { 397 int l = gen->level() + 1; 398 if (l == _n_gens) return NULL; 399 else return _gens[l]; 400 } 401 402 Generation* get_gen(int i) const { 403 if (i >= 0 && i < _n_gens) 404 return _gens[i]; 405 else 406 return NULL; 407 } 408 409 int n_gens() const { 410 assert(_n_gens == gen_policy()->number_of_generations(), "Sanity"); 411 return _n_gens; 412 } 413 414 // Convenience function to be used in situations where the heap type can be 415 // asserted to be this type. 416 static GenCollectedHeap* heap(); 417 418 void set_par_threads(int t); 419 420 421 // Invoke the "do_oop" method of one of the closures "not_older_gens" 422 // or "older_gens" on root locations for the generation at 423 // "level". (The "older_gens" closure is used for scanning references 424 // from older generations; "not_older_gens" is used everywhere else.) 425 // If "younger_gens_as_roots" is false, younger generations are 426 // not scanned as roots; in this case, the caller must be arranging to 427 // scan the younger generations itself. (For example, a generation might 428 // explicitly mark reachable objects in younger generations, to avoid 429 // excess storage retention.) If "collecting_perm_gen" is false, then 430 // roots that may only contain references to permGen objects are not 431 // scanned. The "so" argument determines which of the roots 432 // the closure is applied to: 433 // "SO_None" does none; 434 // "SO_AllClasses" applies the closure to all entries in the SystemDictionary; 435 // "SO_SystemClasses" to all the "system" classes and loaders; 436 // "SO_Symbols_and_Strings" applies the closure to all entries in 437 // SymbolsTable and StringTable. 438 void gen_process_strong_roots(int level, 439 bool younger_gens_as_roots, 440 // The remaining arguments are in an order 441 // consistent with SharedHeap::process_strong_roots: 442 bool activate_scope, 443 bool collecting_perm_gen, 444 SharedHeap::ScanningOption so, 445 OopsInGenClosure* not_older_gens, 446 bool do_code_roots, 447 OopsInGenClosure* older_gens); 448 449 // Apply "blk" to all the weak roots of the system. These include 450 // JNI weak roots, the code cache, system dictionary, symbol table, 451 // string table, and referents of reachable weak refs. 452 void gen_process_weak_roots(OopClosure* root_closure, 453 CodeBlobClosure* code_roots, 454 OopClosure* non_root_closure); 455 456 // Set the saved marks of generations, if that makes sense. 457 // In particular, if any generation might iterate over the oops 458 // in other generations, it should call this method. 459 void save_marks(); 460 461 // Apply "cur->do_oop" or "older->do_oop" to all the oops in objects 462 // allocated since the last call to save_marks in generations at or above 463 // "level" (including the permanent generation.) The "cur" closure is 464 // applied to references in the generation at "level", and the "older" 465 // closure to older (and permanent) generations. 466 #define GCH_SINCE_SAVE_MARKS_ITERATE_DECL(OopClosureType, nv_suffix) \ 467 void oop_since_save_marks_iterate(int level, \ 468 OopClosureType* cur, \ 469 OopClosureType* older); 470 471 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DECL) 472 473 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DECL 474 475 // Returns "true" iff no allocations have occurred in any generation at 476 // "level" or above (including the permanent generation) since the last 477 // call to "save_marks". 478 bool no_allocs_since_save_marks(int level); 479 480 // If a generation bails out of an incremental collection, 481 // it sets this flag. 482 bool incremental_collection_will_fail() { 483 return _incremental_collection_will_fail; 484 } 485 void set_incremental_collection_will_fail() { 486 _incremental_collection_will_fail = true; 487 } 488 void clear_incremental_collection_will_fail() { 489 _incremental_collection_will_fail = false; 490 } 491 492 bool last_incremental_collection_failed() const { 493 return _last_incremental_collection_failed; 494 } 495 void set_last_incremental_collection_failed() { 496 _last_incremental_collection_failed = true; 497 } 498 void clear_last_incremental_collection_failed() { 499 _last_incremental_collection_failed = false; 500 } 501 502 // Promotion of obj into gen failed. Try to promote obj to higher non-perm 503 // gens in ascending order; return the new location of obj if successful. 504 // Otherwise, try expand-and-allocate for obj in each generation starting at 505 // gen; return the new location of obj if successful. Otherwise, return NULL. 506 oop handle_failed_promotion(Generation* gen, 507 oop obj, 508 size_t obj_size); 509 510 private: 511 // Accessor for memory state verification support 512 NOT_PRODUCT( 513 static size_t skip_header_HeapWords() { return _skip_header_HeapWords; } 514 ) 515 516 // Override 517 void check_for_non_bad_heap_word_value(HeapWord* addr, 518 size_t size) PRODUCT_RETURN; 519 520 // For use by mark-sweep. As implemented, mark-sweep-compact is global 521 // in an essential way: compaction is performed across generations, by 522 // iterating over spaces. 523 void prepare_for_compaction(); 524 525 // Perform a full collection of the first max_level+1 generations. 526 // This is the low level interface used by the public versions of 527 // collect() and collect_locked(). Caller holds the Heap_lock on entry. 528 void collect_locked(GCCause::Cause cause, int max_level); 529 530 // Returns success or failure. 531 bool create_cms_collector(); 532 533 // In support of ExplicitGCInvokesConcurrent functionality 534 bool should_do_concurrent_full_gc(GCCause::Cause cause); 535 void collect_mostly_concurrent(GCCause::Cause cause); 536 537 // Save the tops of the spaces in all generations 538 void record_gen_tops_before_GC() PRODUCT_RETURN; 539 540 protected: 541 virtual void gc_prologue(bool full); 542 virtual void gc_epilogue(bool full); 543 544 public: 545 virtual void preload_and_dump(TRAPS) KERNEL_RETURN; 546 }; 547 548 #endif // SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP