Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/genCollectedHeap.hpp
32285 views
/*1* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP25#define SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP2627#include "gc_implementation/shared/adaptiveSizePolicy.hpp"28#include "memory/collectorPolicy.hpp"29#include "memory/generation.hpp"30#include "memory/sharedHeap.hpp"3132class SubTasksDone;3334// A "GenCollectedHeap" is a SharedHeap that uses generational35// collection. It is represented with a sequence of Generation's.36class GenCollectedHeap : public SharedHeap {37friend class GenCollectorPolicy;38friend class Generation;39friend class DefNewGeneration;40friend class TenuredGeneration;41friend class ConcurrentMarkSweepGeneration;42friend class CMSCollector;43friend class GenMarkSweep;44friend class VM_GenCollectForAllocation;45friend class VM_GenCollectFull;46friend class VM_GenCollectFullConcurrent;47friend class VM_GC_HeapInspection;48friend class VM_HeapDumper;49friend class HeapInspection;50friend class GCCauseSetter;51friend class VMStructs;52public:53enum SomeConstants {54max_gens = 1055};5657friend class VM_PopulateDumpSharedSpace;5859protected:60// Fields:61static GenCollectedHeap* _gch;6263private:64int _n_gens;65Generation* _gens[max_gens];66GenerationSpec** _gen_specs;6768// The generational collector policy.69GenCollectorPolicy* _gen_policy;7071// Indicates that the most recent previous incremental collection failed.72// The flag is cleared when an action is taken that might clear the73// condition that caused that incremental collection to fail.74bool _incremental_collection_failed;7576// In support of ExplicitGCInvokesConcurrent functionality77unsigned int _full_collections_completed;7879// Data structure for claiming the (potentially) parallel tasks in80// (gen-specific) roots processing.81SubTasksDone* _process_strong_tasks;8283// In block contents verification, the number of header words to skip84NOT_PRODUCT(static size_t _skip_header_HeapWords;)8586protected:87// Helper functions for allocation88HeapWord* attempt_allocation(size_t size,89bool is_tlab,90bool first_only);9192// Helper function for two callbacks below.93// Considers collection of the first max_level+1 generations.94void do_collection(bool full,95bool clear_all_soft_refs,96size_t size,97bool is_tlab,98int max_level);99100// Callback from VM_GenCollectForAllocation operation.101// This function does everything necessary/possible to satisfy an102// allocation request that failed in the youngest generation that should103// have handled it (including collection, expansion, etc.)104HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab);105106// Callback from VM_GenCollectFull operation.107// Perform a full collection of the first max_level+1 generations.108virtual void do_full_collection(bool clear_all_soft_refs);109void do_full_collection(bool clear_all_soft_refs, int max_level);110111// Does the "cause" of GC indicate that112// we absolutely __must__ clear soft refs?113bool must_clear_all_soft_refs();114115public:116GenCollectedHeap(GenCollectorPolicy *policy);117118GCStats* gc_stats(int level) const;119120// Returns JNI_OK on success121virtual jint initialize();122char* allocate(size_t alignment,123size_t* _total_reserved, int* _n_covered_regions,124ReservedSpace* heap_rs);125126// Does operations required after initialization has been done.127void post_initialize();128129// Initialize ("weak") refs processing support130virtual void ref_processing_init();131132virtual CollectedHeap::Name kind() const {133return CollectedHeap::GenCollectedHeap;134}135136// The generational collector policy.137GenCollectorPolicy* gen_policy() const { return _gen_policy; }138virtual CollectorPolicy* collector_policy() const { return (CollectorPolicy*) gen_policy(); }139140// Adaptive size policy141virtual AdaptiveSizePolicy* size_policy() {142return gen_policy()->size_policy();143}144145// Return the (conservative) maximum heap alignment146static size_t conservative_max_heap_alignment() {147return Generation::GenGrain;148}149150size_t capacity() const;151size_t used() const;152153// Save the "used_region" for generations level and lower.154void save_used_regions(int level);155156size_t max_capacity() const;157158HeapWord* mem_allocate(size_t size,159bool* gc_overhead_limit_was_exceeded);160161// We may support a shared contiguous allocation area, if the youngest162// generation does.163bool supports_inline_contig_alloc() const;164HeapWord** top_addr() const;165HeapWord** end_addr() const;166167// Does this heap support heap inspection? (+PrintClassHistogram)168virtual bool supports_heap_inspection() const { return true; }169170// Perform a full collection of the heap; intended for use in implementing171// "System.gc". This implies as full a collection as the CollectedHeap172// supports. Caller does not hold the Heap_lock on entry.173void collect(GCCause::Cause cause);174175// The same as above but assume that the caller holds the Heap_lock.176void collect_locked(GCCause::Cause cause);177178// Perform a full collection of the first max_level+1 generations.179// Mostly used for testing purposes. Caller does not hold the Heap_lock on entry.180void collect(GCCause::Cause cause, int max_level);181182// Returns "TRUE" iff "p" points into the committed areas of the heap.183// The methods is_in(), is_in_closed_subset() and is_in_youngest() may184// be expensive to compute in general, so, to prevent185// their inadvertent use in product jvm's, we restrict their use to186// assertion checking or verification only.187bool is_in(const void* p) const;188189// override190bool is_in_closed_subset(const void* p) const {191if (UseConcMarkSweepGC) {192return is_in_reserved(p);193} else {194return is_in(p);195}196}197198// Returns true if the reference is to an object in the reserved space199// for the young generation.200// Assumes the the young gen address range is less than that of the old gen.201bool is_in_young(oop p);202203#ifdef ASSERT204virtual bool is_in_partial_collection(const void* p);205#endif206207virtual bool is_scavengable(const void* addr) {208return is_in_young((oop)addr);209}210211// Iteration functions.212void oop_iterate(ExtendedOopClosure* cl);213void object_iterate(ObjectClosure* cl);214void safe_object_iterate(ObjectClosure* cl);215Space* space_containing(const void* addr) const;216217// A CollectedHeap is divided into a dense sequence of "blocks"; that is,218// each address in the (reserved) heap is a member of exactly219// one block. The defining characteristic of a block is that it is220// possible to find its size, and thus to progress forward to the next221// block. (Blocks may be of different sizes.) Thus, blocks may222// represent Java objects, or they might be free blocks in a223// free-list-based heap (or subheap), as long as the two kinds are224// distinguishable and the size of each is determinable.225226// Returns the address of the start of the "block" that contains the227// address "addr". We say "blocks" instead of "object" since some heaps228// may not pack objects densely; a chunk may either be an object or a229// non-object.230virtual HeapWord* block_start(const void* addr) const;231232// Requires "addr" to be the start of a chunk, and returns its size.233// "addr + size" is required to be the start of a new chunk, or the end234// of the active area of the heap. Assumes (and verifies in non-product235// builds) that addr is in the allocated part of the heap and is236// the start of a chunk.237virtual size_t block_size(const HeapWord* addr) const;238239// Requires "addr" to be the start of a block, and returns "TRUE" iff240// the block is an object. Assumes (and verifies in non-product241// builds) that addr is in the allocated part of the heap and is242// the start of a chunk.243virtual bool block_is_obj(const HeapWord* addr) const;244245// Section on TLAB's.246virtual bool supports_tlab_allocation() const;247virtual size_t tlab_capacity(Thread* thr) const;248virtual size_t tlab_used(Thread* thr) const;249virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;250virtual HeapWord* allocate_new_tlab(size_t size);251252// Can a compiler initialize a new object without store barriers?253// This permission only extends from the creation of a new object254// via a TLAB up to the first subsequent safepoint.255virtual bool can_elide_tlab_store_barriers() const {256return true;257}258259virtual bool card_mark_must_follow_store() const {260return UseConcMarkSweepGC;261}262263// We don't need barriers for stores to objects in the264// young gen and, a fortiori, for initializing stores to265// objects therein. This applies to {DefNew,ParNew}+{Tenured,CMS}266// only and may need to be re-examined in case other267// kinds of collectors are implemented in the future.268virtual bool can_elide_initializing_store_barrier(oop new_obj) {269// We wanted to assert that:-270// assert(UseParNewGC || UseSerialGC || UseConcMarkSweepGC,271// "Check can_elide_initializing_store_barrier() for this collector");272// but unfortunately the flag UseSerialGC need not necessarily always273// be set when DefNew+Tenured are being used.274return is_in_young(new_obj);275}276277// The "requestor" generation is performing some garbage collection278// action for which it would be useful to have scratch space. The279// requestor promises to allocate no more than "max_alloc_words" in any280// older generation (via promotion say.) Any blocks of space that can281// be provided are returned as a list of ScratchBlocks, sorted by282// decreasing size.283ScratchBlock* gather_scratch(Generation* requestor, size_t max_alloc_words);284// Allow each generation to reset any scratch space that it has285// contributed as it needs.286void release_scratch();287288// Ensure parsability: override289virtual void ensure_parsability(bool retire_tlabs);290291// Time in ms since the longest time a collector ran in292// in any generation.293virtual jlong millis_since_last_gc();294295// Total number of full collections completed.296unsigned int total_full_collections_completed() {297assert(_full_collections_completed <= _total_full_collections,298"Can't complete more collections than were started");299return _full_collections_completed;300}301302// Update above counter, as appropriate, at the end of a stop-world GC cycle303unsigned int update_full_collections_completed();304// Update above counter, as appropriate, at the end of a concurrent GC cycle305unsigned int update_full_collections_completed(unsigned int count);306307// Update "time of last gc" for all constituent generations308// to "now".309void update_time_of_last_gc(jlong now) {310for (int i = 0; i < _n_gens; i++) {311_gens[i]->update_time_of_last_gc(now);312}313}314315// Update the gc statistics for each generation.316// "level" is the level of the lastest collection317void update_gc_stats(int current_level, bool full) {318for (int i = 0; i < _n_gens; i++) {319_gens[i]->update_gc_stats(current_level, full);320}321}322323// Override.324bool no_gc_in_progress() { return !is_gc_active(); }325326// Override.327void prepare_for_verify();328329// Override.330void verify(bool silent, VerifyOption option);331332// Override.333virtual void print_on(outputStream* st) const;334virtual void print_gc_threads_on(outputStream* st) const;335virtual void gc_threads_do(ThreadClosure* tc) const;336virtual void print_tracing_info() const;337virtual void print_on_error(outputStream* st) const;338339// PrintGC, PrintGCDetails support340void print_heap_change(size_t prev_used) const;341342// The functions below are helper functions that a subclass of343// "CollectedHeap" can use in the implementation of its virtual344// functions.345346class GenClosure : public StackObj {347public:348virtual void do_generation(Generation* gen) = 0;349};350351// Apply "cl.do_generation" to all generations in the heap352// If "old_to_young" determines the order.353void generation_iterate(GenClosure* cl, bool old_to_young);354355void space_iterate(SpaceClosure* cl);356357// Return "true" if all generations have reached the358// maximal committed limit that they can reach, without a garbage359// collection.360virtual bool is_maximal_no_gc() const;361362// Return the generation before "gen".363Generation* prev_gen(Generation* gen) const {364int l = gen->level();365guarantee(l > 0, "Out of bounds");366return _gens[l-1];367}368369// Return the generation after "gen".370Generation* next_gen(Generation* gen) const {371int l = gen->level() + 1;372guarantee(l < _n_gens, "Out of bounds");373return _gens[l];374}375376Generation* get_gen(int i) const {377guarantee(i >= 0 && i < _n_gens, "Out of bounds");378return _gens[i];379}380381int n_gens() const {382assert(_n_gens == gen_policy()->number_of_generations(), "Sanity");383return _n_gens;384}385386// Convenience function to be used in situations where the heap type can be387// asserted to be this type.388static GenCollectedHeap* heap();389390void set_par_threads(uint t);391void set_n_termination(uint t);392393// Invoke the "do_oop" method of one of the closures "not_older_gens"394// or "older_gens" on root locations for the generation at395// "level". (The "older_gens" closure is used for scanning references396// from older generations; "not_older_gens" is used everywhere else.)397// If "younger_gens_as_roots" is false, younger generations are398// not scanned as roots; in this case, the caller must be arranging to399// scan the younger generations itself. (For example, a generation might400// explicitly mark reachable objects in younger generations, to avoid401// excess storage retention.)402// The "so" argument determines which of the roots403// the closure is applied to:404// "SO_None" does none;405enum ScanningOption {406SO_None = 0x0,407SO_AllCodeCache = 0x8,408SO_ScavengeCodeCache = 0x10409};410411private:412void process_roots(bool activate_scope,413ScanningOption so,414OopClosure* strong_roots,415OopClosure* weak_roots,416CLDClosure* strong_cld_closure,417CLDClosure* weak_cld_closure,418CodeBlobToOopClosure* code_roots);419420void gen_process_roots(int level,421bool younger_gens_as_roots,422bool activate_scope,423ScanningOption so,424OopsInGenClosure* not_older_gens,425OopsInGenClosure* weak_roots,426OopsInGenClosure* older_gens,427CLDClosure* cld_closure,428CLDClosure* weak_cld_closure,429CodeBlobClosure* code_closure);430431public:432static const bool StrongAndWeakRoots = false;433static const bool StrongRootsOnly = true;434435void gen_process_roots(int level,436bool younger_gens_as_roots,437bool activate_scope,438ScanningOption so,439bool only_strong_roots,440OopsInGenClosure* not_older_gens,441OopsInGenClosure* older_gens,442CLDClosure* cld_closure);443444// Apply "root_closure" to all the weak roots of the system.445// These include JNI weak roots, string table,446// and referents of reachable weak refs.447void gen_process_weak_roots(OopClosure* root_closure);448449// Set the saved marks of generations, if that makes sense.450// In particular, if any generation might iterate over the oops451// in other generations, it should call this method.452void save_marks();453454// Apply "cur->do_oop" or "older->do_oop" to all the oops in objects455// allocated since the last call to save_marks in generations at or above456// "level". The "cur" closure is457// applied to references in the generation at "level", and the "older"458// closure to older generations.459#define GCH_SINCE_SAVE_MARKS_ITERATE_DECL(OopClosureType, nv_suffix) \460void oop_since_save_marks_iterate(int level, \461OopClosureType* cur, \462OopClosureType* older);463464ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DECL)465466#undef GCH_SINCE_SAVE_MARKS_ITERATE_DECL467468// Returns "true" iff no allocations have occurred in any generation at469// "level" or above since the last470// call to "save_marks".471bool no_allocs_since_save_marks(int level);472473// Returns true if an incremental collection is likely to fail.474// We optionally consult the young gen, if asked to do so;475// otherwise we base our answer on whether the previous incremental476// collection attempt failed with no corrective action as of yet.477bool incremental_collection_will_fail(bool consult_young) {478// Assumes a 2-generation system; the first disjunct remembers if an479// incremental collection failed, even when we thought (second disjunct)480// that it would not.481assert(heap()->collector_policy()->is_two_generation_policy(),482"the following definition may not be suitable for an n(>2)-generation system");483return incremental_collection_failed() ||484(consult_young && !get_gen(0)->collection_attempt_is_safe());485}486487// If a generation bails out of an incremental collection,488// it sets this flag.489bool incremental_collection_failed() const {490return _incremental_collection_failed;491}492void set_incremental_collection_failed() {493_incremental_collection_failed = true;494}495void clear_incremental_collection_failed() {496_incremental_collection_failed = false;497}498499// Promotion of obj into gen failed. Try to promote obj to higher500// gens in ascending order; return the new location of obj if successful.501// Otherwise, try expand-and-allocate for obj in both the young and old502// generation; return the new location of obj if successful. Otherwise, return NULL.503oop handle_failed_promotion(Generation* old_gen,504oop obj,505size_t obj_size);506507private:508// Accessor for memory state verification support509NOT_PRODUCT(510static size_t skip_header_HeapWords() { return _skip_header_HeapWords; }511)512513// Override514void check_for_non_bad_heap_word_value(HeapWord* addr,515size_t size) PRODUCT_RETURN;516517// For use by mark-sweep. As implemented, mark-sweep-compact is global518// in an essential way: compaction is performed across generations, by519// iterating over spaces.520void prepare_for_compaction();521522// Perform a full collection of the first max_level+1 generations.523// This is the low level interface used by the public versions of524// collect() and collect_locked(). Caller holds the Heap_lock on entry.525void collect_locked(GCCause::Cause cause, int max_level);526527// Returns success or failure.528bool create_cms_collector();529530// In support of ExplicitGCInvokesConcurrent functionality531bool should_do_concurrent_full_gc(GCCause::Cause cause);532void collect_mostly_concurrent(GCCause::Cause cause);533534// Save the tops of the spaces in all generations535void record_gen_tops_before_GC() PRODUCT_RETURN;536537protected:538virtual void gc_prologue(bool full);539virtual void gc_epilogue(bool full);540};541542#endif // SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP543544545