1 /* 2 * Copyright (c) 2000, 2015, 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 has two generations, young and old. 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_GenCollectFull; 47 friend class VM_GenCollectFullConcurrent; 48 friend class VM_GC_HeapInspection; 49 friend class VM_HeapDumper; 50 friend class HeapInspection; 51 friend class GCCauseSetter; 52 friend class VMStructs; 53 public: 54 enum SomeConstants { 55 max_gens = 10 56 }; 57 58 friend class VM_PopulateDumpSharedSpace; 59 60 protected: 61 // Fields: 62 static GenCollectedHeap* _gch; 63 64 private: 65 int _n_gens; 66 67 Generation* _young_gen; 68 Generation* _old_gen; 69 70 GenerationSpec** _gen_specs; 71 72 // The singleton Gen Remembered Set. 73 GenRemSet* _rem_set; 74 75 // The generational collector policy. 76 GenCollectorPolicy* _gen_policy; 77 78 // Indicates that the most recent previous incremental collection failed. 79 // The flag is cleared when an action is taken that might clear the 80 // condition that caused that incremental collection to fail. 81 bool _incremental_collection_failed; 82 83 // In support of ExplicitGCInvokesConcurrent functionality 84 unsigned int _full_collections_completed; 85 86 // Data structure for claiming the (potentially) parallel tasks in 87 // (gen-specific) roots processing. 88 SubTasksDone* _gen_process_roots_tasks; 89 SubTasksDone* gen_process_roots_tasks() { return _gen_process_roots_tasks; } 90 91 // Collects the given generation. 92 void collect_generation(Generation* gen, bool full, size_t size, bool is_tlab, 93 bool run_verification, bool clear_soft_refs, 94 bool restore_marks_for_biased_locking); 95 96 // In block contents verification, the number of header words to skip 97 NOT_PRODUCT(static size_t _skip_header_HeapWords;) 98 99 protected: 100 // Helper functions for allocation 101 HeapWord* attempt_allocation(size_t size, 102 bool is_tlab, 103 bool first_only); 104 105 // Helper function for two callbacks below. 106 // Considers collection of the first max_level+1 generations. 107 void do_collection(bool full, 108 bool clear_all_soft_refs, 109 size_t size, 110 bool is_tlab, 111 int max_level); 112 113 // Callback from VM_GenCollectForAllocation operation. 114 // This function does everything necessary/possible to satisfy an 115 // allocation request that failed in the youngest generation that should 116 // have handled it (including collection, expansion, etc.) 117 HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab); 118 119 // Callback from VM_GenCollectFull operation. 120 // Perform a full collection of the first max_level+1 generations. 121 virtual void do_full_collection(bool clear_all_soft_refs); 122 void do_full_collection(bool clear_all_soft_refs, int max_level); 123 124 // Does the "cause" of GC indicate that 125 // we absolutely __must__ clear soft refs? 126 bool must_clear_all_soft_refs(); 127 128 public: 129 GenCollectedHeap(GenCollectorPolicy *policy); 130 131 GCStats* gc_stats(int level) const; 132 133 // Returns JNI_OK on success 134 virtual jint initialize(); 135 136 char* allocate(size_t alignment, size_t* _total_reserved, ReservedSpace* heap_rs); 137 138 // Does operations required after initialization has been done. 139 void post_initialize(); 140 141 // Initialize ("weak") refs processing support 142 virtual void ref_processing_init(); 143 144 virtual CollectedHeap::Name kind() const { 145 return CollectedHeap::GenCollectedHeap; 146 } 147 148 Generation* young_gen() { return _young_gen; } 149 Generation* old_gen() { return _old_gen; } 150 151 // The generational collector policy. 152 GenCollectorPolicy* gen_policy() const { return _gen_policy; } 153 154 virtual CollectorPolicy* collector_policy() const { return (CollectorPolicy*) gen_policy(); } 155 156 // Adaptive size policy 157 virtual AdaptiveSizePolicy* size_policy() { 158 return gen_policy()->size_policy(); 159 } 160 161 // Return the (conservative) maximum heap alignment 162 static size_t conservative_max_heap_alignment() { 163 return Generation::GenGrain; 164 } 165 166 size_t capacity() const; 167 size_t used() const; 168 169 // Save the "used_region" for generations level and lower. 170 void save_used_regions(int level); 171 172 size_t max_capacity() const; 173 174 HeapWord* mem_allocate(size_t size, 175 bool* gc_overhead_limit_was_exceeded); 176 177 // We may support a shared contiguous allocation area, if the youngest 178 // generation does. 179 bool supports_inline_contig_alloc() const; 180 HeapWord** top_addr() const; 181 HeapWord** end_addr() const; 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 // The same as above but assume that the caller holds the Heap_lock. 192 void collect_locked(GCCause::Cause cause); 193 194 // Perform a full collection of the first max_level+1 generations. 195 // Mostly used for testing purposes. Caller does not hold the Heap_lock on entry. 196 void collect(GCCause::Cause cause, int max_level); 197 198 // Returns "TRUE" iff "p" points into the committed areas of the heap. 199 // The methods is_in(), is_in_closed_subset() and is_in_youngest() may 200 // be expensive to compute in general, so, to prevent 201 // their inadvertent use in product jvm's, we restrict their use to 202 // assertion checking or verification only. 203 bool is_in(const void* p) const; 204 205 // override 206 bool is_in_closed_subset(const void* p) const { 207 if (UseConcMarkSweepGC) { 208 return is_in_reserved(p); 209 } else { 210 return is_in(p); 211 } 212 } 213 214 // Returns true if the reference is to an object in the reserved space 215 // for the young generation. 216 // Assumes the the young gen address range is less than that of the old gen. 217 bool is_in_young(oop p); 218 219 #ifdef ASSERT 220 virtual bool is_in_partial_collection(const void* p); 221 #endif 222 223 virtual bool is_scavengable(const void* addr) { 224 return is_in_young((oop)addr); 225 } 226 227 // Iteration functions. 228 void oop_iterate(ExtendedOopClosure* cl); 229 void object_iterate(ObjectClosure* cl); 230 void safe_object_iterate(ObjectClosure* cl); 231 Space* space_containing(const void* addr) const; 232 233 // A CollectedHeap is divided into a dense sequence of "blocks"; that is, 234 // each address in the (reserved) heap is a member of exactly 235 // one block. The defining characteristic of a block is that it is 236 // possible to find its size, and thus to progress forward to the next 237 // block. (Blocks may be of different sizes.) Thus, blocks may 238 // represent Java objects, or they might be free blocks in a 239 // free-list-based heap (or subheap), as long as the two kinds are 240 // distinguishable and the size of each is determinable. 241 242 // Returns the address of the start of the "block" that contains the 243 // address "addr". We say "blocks" instead of "object" since some heaps 244 // may not pack objects densely; a chunk may either be an object or a 245 // non-object. 246 virtual HeapWord* block_start(const void* addr) const; 247 248 // Requires "addr" to be the start of a chunk, and returns its size. 249 // "addr + size" is required to be the start of a new chunk, or the end 250 // of the active area of the heap. Assumes (and verifies in non-product 251 // builds) that addr is in the allocated part of the heap and is 252 // the start of a chunk. 253 virtual size_t block_size(const HeapWord* addr) const; 254 255 // Requires "addr" to be the start of a block, and returns "TRUE" iff 256 // the block is an object. Assumes (and verifies in non-product 257 // builds) that addr is in the allocated part of the heap and is 258 // the start of a chunk. 259 virtual bool block_is_obj(const HeapWord* addr) const; 260 261 // Section on TLAB's. 262 virtual bool supports_tlab_allocation() const; 263 virtual size_t tlab_capacity(Thread* thr) const; 264 virtual size_t tlab_used(Thread* thr) const; 265 virtual size_t unsafe_max_tlab_alloc(Thread* thr) const; 266 virtual HeapWord* allocate_new_tlab(size_t size); 267 268 // Can a compiler initialize a new object without store barriers? 269 // This permission only extends from the creation of a new object 270 // via a TLAB up to the first subsequent safepoint. 271 virtual bool can_elide_tlab_store_barriers() const { 272 return true; 273 } 274 275 virtual bool card_mark_must_follow_store() const { 276 return UseConcMarkSweepGC; 277 } 278 279 // We don't need barriers for stores to objects in the 280 // young gen and, a fortiori, for initializing stores to 281 // objects therein. This applies to DefNew+Tenured and ParNew+CMS 282 // only and may need to be re-examined in case other 283 // kinds of collectors are implemented in the future. 284 virtual bool can_elide_initializing_store_barrier(oop new_obj) { 285 // We wanted to assert that:- 286 // assert(UseSerialGC || UseConcMarkSweepGC, 287 // "Check can_elide_initializing_store_barrier() for this collector"); 288 // but unfortunately the flag UseSerialGC need not necessarily always 289 // be set when DefNew+Tenured are being used. 290 return is_in_young(new_obj); 291 } 292 293 // The "requestor" generation is performing some garbage collection 294 // action for which it would be useful to have scratch space. The 295 // requestor promises to allocate no more than "max_alloc_words" in any 296 // older generation (via promotion say.) Any blocks of space that can 297 // be provided are returned as a list of ScratchBlocks, sorted by 298 // decreasing size. 299 ScratchBlock* gather_scratch(Generation* requestor, size_t max_alloc_words); 300 // Allow each generation to reset any scratch space that it has 301 // contributed as it needs. 302 void release_scratch(); 303 304 // Ensure parsability: override 305 virtual void ensure_parsability(bool retire_tlabs); 306 307 // Time in ms since the longest time a collector ran in 308 // in any generation. 309 virtual jlong millis_since_last_gc(); 310 311 // Total number of full collections completed. 312 unsigned int total_full_collections_completed() { 313 assert(_full_collections_completed <= _total_full_collections, 314 "Can't complete more collections than were started"); 315 return _full_collections_completed; 316 } 317 318 // Update above counter, as appropriate, at the end of a stop-world GC cycle 319 unsigned int update_full_collections_completed(); 320 // Update above counter, as appropriate, at the end of a concurrent GC cycle 321 unsigned int update_full_collections_completed(unsigned int count); 322 323 // Update "time of last gc" for all generations to "now". 324 void update_time_of_last_gc(jlong now) { 325 _young_gen->update_time_of_last_gc(now); 326 _old_gen->update_time_of_last_gc(now); 327 } 328 329 // Update the gc statistics for each generation. 330 // "level" is the level of the latest collection. 331 void update_gc_stats(int current_level, bool full) { 332 _young_gen->update_gc_stats(current_level, full); 333 _old_gen->update_gc_stats(current_level, full); 334 } 335 336 // Override. 337 bool no_gc_in_progress() { return !is_gc_active(); } 338 339 // Override. 340 void prepare_for_verify(); 341 342 // Override. 343 void verify(bool silent, VerifyOption option); 344 345 // Override. 346 virtual void print_on(outputStream* st) const; 347 virtual void print_gc_threads_on(outputStream* st) const; 348 virtual void gc_threads_do(ThreadClosure* tc) const; 349 virtual void print_tracing_info() const; 350 virtual void print_on_error(outputStream* st) const; 351 352 // PrintGC, PrintGCDetails support 353 void print_heap_change(size_t prev_used) const; 354 355 // The functions below are helper functions that a subclass of 356 // "CollectedHeap" can use in the implementation of its virtual 357 // functions. 358 359 class GenClosure : public StackObj { 360 public: 361 virtual void do_generation(Generation* gen) = 0; 362 }; 363 364 // Apply "cl.do_generation" to all generations in the heap 365 // If "old_to_young" determines the order. 366 void generation_iterate(GenClosure* cl, bool old_to_young); 367 368 void space_iterate(SpaceClosure* cl); 369 370 // Return "true" if all generations have reached the 371 // maximal committed limit that they can reach, without a garbage 372 // collection. 373 virtual bool is_maximal_no_gc() const; 374 375 // Return the generation before "gen". 376 Generation* prev_gen(Generation* gen) const { 377 guarantee(gen->level() == 1, "Out of bounds"); 378 return _young_gen; 379 } 380 381 // Return the generation after "gen". 382 Generation* next_gen(Generation* gen) const { 383 guarantee(gen->level() == 0, "Out of bounds"); 384 return _old_gen; 385 } 386 387 Generation* get_gen(int i) const { 388 guarantee(i == 0 || i == 1, "Out of bounds"); 389 if (i == 0) { 390 return _young_gen; 391 } else { 392 return _old_gen; 393 } 394 } 395 396 int n_gens() const { 397 assert(_n_gens == gen_policy()->number_of_generations(), "Sanity"); 398 return _n_gens; 399 } 400 401 // This function returns the "GenRemSet" object that allows us to scan 402 // generations in a fully generational heap. 403 GenRemSet* rem_set() { return _rem_set; } 404 405 // Convenience function to be used in situations where the heap type can be 406 // asserted to be this type. 407 static GenCollectedHeap* heap(); 408 409 void set_par_threads(uint t); 410 411 // Invoke the "do_oop" method of one of the closures "not_older_gens" 412 // or "older_gens" on root locations for the generation at 413 // "level". (The "older_gens" closure is used for scanning references 414 // from older generations; "not_older_gens" is used everywhere else.) 415 // If "younger_gens_as_roots" is false, younger generations are 416 // not scanned as roots; in this case, the caller must be arranging to 417 // scan the younger generations itself. (For example, a generation might 418 // explicitly mark reachable objects in younger generations, to avoid 419 // excess storage retention.) 420 // The "so" argument determines which of the roots 421 // the closure is applied to: 422 // "SO_None" does none; 423 private: 424 void gen_process_roots(int level, 425 bool younger_gens_as_roots, 426 bool activate_scope, 427 SharedHeap::ScanningOption so, 428 OopsInGenClosure* not_older_gens, 429 OopsInGenClosure* weak_roots, 430 OopsInGenClosure* older_gens, 431 CLDClosure* cld_closure, 432 CLDClosure* weak_cld_closure, 433 CodeBlobClosure* code_closure); 434 435 public: 436 static const bool StrongAndWeakRoots = false; 437 static const bool StrongRootsOnly = true; 438 439 void gen_process_roots(int level, 440 bool younger_gens_as_roots, 441 bool activate_scope, 442 SharedHeap::ScanningOption so, 443 bool only_strong_roots, 444 OopsInGenClosure* not_older_gens, 445 OopsInGenClosure* older_gens, 446 CLDClosure* cld_closure); 447 448 // Apply "root_closure" to all the weak roots of the system. 449 // These include JNI weak roots, string table, 450 // and referents of reachable weak refs. 451 void gen_process_weak_roots(OopClosure* root_closure); 452 453 // Set the saved marks of generations, if that makes sense. 454 // In particular, if any generation might iterate over the oops 455 // in other generations, it should call this method. 456 void save_marks(); 457 458 // Apply "cur->do_oop" or "older->do_oop" to all the oops in objects 459 // allocated since the last call to save_marks in generations at or above 460 // "level". The "cur" closure is 461 // applied to references in the generation at "level", and the "older" 462 // closure to older generations. 463 #define GCH_SINCE_SAVE_MARKS_ITERATE_DECL(OopClosureType, nv_suffix) \ 464 void oop_since_save_marks_iterate(int level, \ 465 OopClosureType* cur, \ 466 OopClosureType* older); 467 468 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DECL) 469 470 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DECL 471 472 // Returns "true" iff no allocations have occurred in any generation at 473 // "level" or above since the last 474 // call to "save_marks". 475 bool no_allocs_since_save_marks(int level); 476 477 // Returns true if an incremental collection is likely to fail. 478 // We optionally consult the young gen, if asked to do so; 479 // otherwise we base our answer on whether the previous incremental 480 // collection attempt failed with no corrective action as of yet. 481 bool incremental_collection_will_fail(bool consult_young) { 482 // Assumes a 2-generation system; the first disjunct remembers if an 483 // incremental collection failed, even when we thought (second disjunct) 484 // that it would not. 485 assert(heap()->collector_policy()->is_generation_policy(), 486 "the following definition may not be suitable for an n(>2)-generation system"); 487 return incremental_collection_failed() || 488 (consult_young && !get_gen(0)->collection_attempt_is_safe()); 489 } 490 491 // If a generation bails out of an incremental collection, 492 // it sets this flag. 493 bool incremental_collection_failed() const { 494 return _incremental_collection_failed; 495 } 496 void set_incremental_collection_failed() { 497 _incremental_collection_failed = true; 498 } 499 void clear_incremental_collection_failed() { 500 _incremental_collection_failed = false; 501 } 502 503 // Promotion of obj into gen failed. Try to promote obj to higher 504 // gens in ascending order; return the new location of obj if successful. 505 // Otherwise, try expand-and-allocate for obj in both the young and old 506 // generation; return the new location of obj if successful. Otherwise, return NULL. 507 oop handle_failed_promotion(Generation* old_gen, 508 oop obj, 509 size_t obj_size); 510 511 private: 512 // Accessor for memory state verification support 513 NOT_PRODUCT( 514 static size_t skip_header_HeapWords() { return _skip_header_HeapWords; } 515 ) 516 517 // Override 518 void check_for_non_bad_heap_word_value(HeapWord* addr, 519 size_t size) PRODUCT_RETURN; 520 521 // For use by mark-sweep. As implemented, mark-sweep-compact is global 522 // in an essential way: compaction is performed across generations, by 523 // iterating over spaces. 524 void prepare_for_compaction(); 525 526 // Perform a full collection of the first max_level+1 generations. 527 // This is the low level interface used by the public versions of 528 // collect() and collect_locked(). Caller holds the Heap_lock on entry. 529 void collect_locked(GCCause::Cause cause, int max_level); 530 531 // Returns success or failure. 532 bool create_cms_collector(); 533 534 // In support of ExplicitGCInvokesConcurrent functionality 535 bool should_do_concurrent_full_gc(GCCause::Cause cause); 536 void collect_mostly_concurrent(GCCause::Cause cause); 537 538 // Save the tops of the spaces in all generations 539 void record_gen_tops_before_GC() PRODUCT_RETURN; 540 541 protected: 542 virtual void gc_prologue(bool full); 543 virtual void gc_epilogue(bool full); 544 }; 545 546 #endif // SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP