1 /*
   2  * Copyright (c) 2001, 2012, 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_DEFNEWGENERATION_HPP
  26 #define SHARE_VM_MEMORY_DEFNEWGENERATION_HPP
  27 
  28 #include "gc_implementation/shared/ageTable.hpp"
  29 #include "gc_implementation/shared/cSpaceCounters.hpp"
  30 #include "gc_implementation/shared/generationCounters.hpp"
  31 #include "memory/generation.inline.hpp"
  32 #include "utilities/stack.hpp"
  33 
  34 class EdenSpace;
  35 class ContiguousSpace;
  36 class ScanClosure;
  37 
  38 // DefNewGeneration is a young generation containing eden, from- and
  39 // to-space.
  40 
  41 class DefNewGeneration: public Generation {
  42   friend class VMStructs;
  43 
  44 protected:
  45   Generation* _next_gen;
  46   uint        _tenuring_threshold;   // Tenuring threshold for next collection.
  47   ageTable    _age_table;
  48   // Size of object to pretenure in words; command line provides bytes
  49   size_t        _pretenure_size_threshold_words;
  50 
  51   ageTable*   age_table() { return &_age_table; }
  52   // Initialize state to optimistically assume no promotion failure will
  53   // happen.
  54   void   init_assuming_no_promotion_failure();
  55   // True iff a promotion has failed in the current collection.
  56   bool   _promotion_failed;
  57   bool   promotion_failed() { return _promotion_failed; }
  58 
  59   // Handling promotion failure.  A young generation collection
  60   // can fail if a live object cannot be copied out of its
  61   // location in eden or from-space during the collection.  If
  62   // a collection fails, the young generation is left in a
  63   // consistent state such that it can be collected by a
  64   // full collection.
  65   //   Before the collection
  66   //     Objects are in eden or from-space
  67   //     All roots into the young generation point into eden or from-space.
  68   //
  69   //   After a failed collection
  70   //     Objects may be in eden, from-space, or to-space
  71   //     An object A in eden or from-space may have a copy B
  72   //       in to-space.  If B exists, all roots that once pointed
  73   //       to A must now point to B.
  74   //     All objects in the young generation are unmarked.
  75   //     Eden, from-space, and to-space will all be collected by
  76   //       the full collection.
  77   void handle_promotion_failure(oop);
  78 
  79   // In the absence of promotion failure, we wouldn't look at "from-space"
  80   // objects after a young-gen collection.  When promotion fails, however,
  81   // the subsequent full collection will look at from-space objects:
  82   // therefore we must remove their forwarding pointers.
  83   void remove_forwarding_pointers();
  84 
  85   // Preserve the mark of "obj", if necessary, in preparation for its mark
  86   // word being overwritten with a self-forwarding-pointer.
  87   void   preserve_mark_if_necessary(oop obj, markOop m);
  88   void   preserve_mark(oop obj, markOop m);    // work routine used by the above
  89 
  90   // Together, these keep <object with a preserved mark, mark value> pairs.
  91   // They should always contain the same number of elements.
  92   Stack<oop, mtGC>     _objs_with_preserved_marks;
  93   Stack<markOop, mtGC> _preserved_marks_of_objs;
  94 
  95   // Promotion failure handling
  96   ExtendedOopClosure *_promo_failure_scan_stack_closure;
  97   void set_promo_failure_scan_stack_closure(ExtendedOopClosure *scan_stack_closure) {
  98     _promo_failure_scan_stack_closure = scan_stack_closure;
  99   }
 100 
 101   Stack<oop, mtGC> _promo_failure_scan_stack;
 102   void drain_promo_failure_scan_stack(void);
 103   bool _promo_failure_drain_in_progress;
 104 
 105   // Performance Counters
 106   GenerationCounters*  _gen_counters;
 107   CSpaceCounters*      _eden_counters;
 108   CSpaceCounters*      _from_counters;
 109   CSpaceCounters*      _to_counters;
 110 
 111   // sizing information
 112   size_t               _max_eden_size;
 113   size_t               _max_survivor_size;
 114 
 115   // Allocation support
 116   bool _should_allocate_from_space;
 117   bool should_allocate_from_space() const {
 118     return _should_allocate_from_space;
 119   }
 120   void clear_should_allocate_from_space() {
 121     _should_allocate_from_space = false;
 122   }
 123   void set_should_allocate_from_space() {
 124     _should_allocate_from_space = true;
 125   }
 126 
 127  protected:
 128   // Spaces
 129   EdenSpace*       _eden_space;
 130   ContiguousSpace* _from_space;
 131   ContiguousSpace* _to_space;
 132 
 133   enum SomeProtectedConstants {
 134     // Generations are GenGrain-aligned and have size that are multiples of
 135     // GenGrain.
 136     MinFreeScratchWords = 100
 137   };
 138 
 139   // Return the size of a survivor space if this generation were of size
 140   // gen_size.
 141   size_t compute_survivor_size(size_t gen_size, size_t alignment) const {
 142     size_t n = gen_size / (SurvivorRatio + 2);
 143     return n > alignment ? align_size_down(n, alignment) : alignment;
 144   }
 145 
 146  public:  // was "protected" but caused compile error on win32
 147   class IsAliveClosure: public BoolObjectClosure {
 148     Generation* _g;
 149   public:
 150     IsAliveClosure(Generation* g);
 151     void do_object(oop p);
 152     bool do_object_b(oop p);
 153   };
 154 
 155   class KeepAliveClosure: public OopClosure {
 156   protected:
 157     ScanWeakRefClosure* _cl;
 158     CardTableRS* _rs;
 159     template <class T> void do_oop_work(T* p);
 160   public:
 161     KeepAliveClosure(ScanWeakRefClosure* cl);
 162     virtual void do_oop(oop* p);
 163     virtual void do_oop(narrowOop* p);
 164   };
 165 
 166   class FastKeepAliveClosure: public KeepAliveClosure {
 167   protected:
 168     HeapWord* _boundary;
 169     template <class T> void do_oop_work(T* p);
 170   public:
 171     FastKeepAliveClosure(DefNewGeneration* g, ScanWeakRefClosure* cl);
 172     virtual void do_oop(oop* p);
 173     virtual void do_oop(narrowOop* p);
 174   };
 175 
 176   class EvacuateFollowersClosure: public VoidClosure {
 177     GenCollectedHeap* _gch;
 178     int _level;
 179     ScanClosure* _scan_cur_or_nonheap;
 180     ScanClosure* _scan_older;
 181   public:
 182     EvacuateFollowersClosure(GenCollectedHeap* gch, int level,
 183                              ScanClosure* cur, ScanClosure* older);
 184     void do_void();
 185   };
 186 
 187   class FastEvacuateFollowersClosure: public VoidClosure {
 188     GenCollectedHeap* _gch;
 189     int _level;
 190     DefNewGeneration* _gen;
 191     FastScanClosure* _scan_cur_or_nonheap;
 192     FastScanClosure* _scan_older;
 193   public:
 194     FastEvacuateFollowersClosure(GenCollectedHeap* gch, int level,
 195                                  DefNewGeneration* gen,
 196                                  FastScanClosure* cur,
 197                                  FastScanClosure* older);
 198     void do_void();
 199   };
 200 
 201  public:
 202   DefNewGeneration(ReservedSpace rs, size_t initial_byte_size, int level,
 203                    const char* policy="Copy");
 204 
 205   virtual Generation::Name kind() { return Generation::DefNew; }
 206 
 207   // Accessing spaces
 208   EdenSpace*       eden() const           { return _eden_space; }
 209   ContiguousSpace* from() const           { return _from_space;  }
 210   ContiguousSpace* to()   const           { return _to_space;    }
 211 
 212   virtual CompactibleSpace* first_compaction_space() const;
 213 
 214   // Space enquiries
 215   size_t capacity() const;
 216   size_t used() const;
 217   size_t free() const;
 218   size_t max_capacity() const;
 219   size_t capacity_before_gc() const;
 220   size_t unsafe_max_alloc_nogc() const;
 221   size_t contiguous_available() const;
 222 
 223   size_t max_eden_size() const              { return _max_eden_size; }
 224   size_t max_survivor_size() const          { return _max_survivor_size; }
 225 
 226   bool supports_inline_contig_alloc() const { return true; }
 227   HeapWord** top_addr() const;
 228   HeapWord** end_addr() const;
 229 
 230   // Thread-local allocation buffers
 231   bool supports_tlab_allocation() const { return true; }
 232   size_t tlab_capacity() const;
 233   size_t unsafe_max_tlab_alloc() const;
 234 
 235   // Grow the generation by the specified number of bytes.
 236   // The size of bytes is assumed to be properly aligned.
 237   // Return true if the expansion was successful.
 238   bool expand(size_t bytes);
 239 
 240   // DefNewGeneration cannot currently expand except at
 241   // a GC.
 242   virtual bool is_maximal_no_gc() const { return true; }
 243 
 244   // Iteration
 245   void object_iterate(ObjectClosure* blk);
 246   void object_iterate_since_last_GC(ObjectClosure* cl);
 247 
 248   void younger_refs_iterate(OopsInGenClosure* cl);
 249 
 250   void space_iterate(SpaceClosure* blk, bool usedOnly = false);
 251 
 252   // Allocation support
 253   virtual bool should_allocate(size_t word_size, bool is_tlab) {
 254     assert(UseTLAB || !is_tlab, "Should not allocate tlab");
 255 
 256     size_t overflow_limit    = (size_t)1 << (BitsPerSize_t - LogHeapWordSize);
 257 
 258     const bool non_zero      = word_size > 0;
 259     const bool overflows     = word_size >= overflow_limit;
 260     const bool check_too_big = _pretenure_size_threshold_words > 0;
 261     const bool not_too_big   = word_size < _pretenure_size_threshold_words;
 262     const bool size_ok       = is_tlab || !check_too_big || not_too_big;
 263 
 264     bool result = !overflows &&
 265                   non_zero   &&
 266                   size_ok;
 267 
 268     return result;
 269   }
 270 
 271   HeapWord* allocate(size_t word_size, bool is_tlab);
 272   HeapWord* allocate_from_space(size_t word_size);
 273 
 274   HeapWord* par_allocate(size_t word_size, bool is_tlab);
 275 
 276   // Prologue & Epilogue
 277   virtual void gc_prologue(bool full);
 278   virtual void gc_epilogue(bool full);
 279 
 280   // Save the tops for eden, from, and to
 281   virtual void record_spaces_top();
 282 
 283   // Doesn't require additional work during GC prologue and epilogue
 284   virtual bool performs_in_place_marking() const { return false; }
 285 
 286   // Accessing marks
 287   void save_marks();
 288   void reset_saved_marks();
 289   bool no_allocs_since_save_marks();
 290 
 291   // Need to declare the full complement of closures, whether we'll
 292   // override them or not, or get message from the compiler:
 293   //   oop_since_save_marks_iterate_nv hides virtual function...
 294 #define DefNew_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix) \
 295   void oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl);
 296 
 297   ALL_SINCE_SAVE_MARKS_CLOSURES(DefNew_SINCE_SAVE_MARKS_DECL)
 298 
 299 #undef DefNew_SINCE_SAVE_MARKS_DECL
 300 
 301   // For non-youngest collection, the DefNewGeneration can contribute
 302   // "to-space".
 303   virtual void contribute_scratch(ScratchBlock*& list, Generation* requestor,
 304                           size_t max_alloc_words);
 305 
 306   // Reset for contribution of "to-space".
 307   virtual void reset_scratch();
 308 
 309   // GC support
 310   virtual void compute_new_size();
 311 
 312   // Returns true if the collection is likely to be safely
 313   // completed. Even if this method returns true, a collection
 314   // may not be guaranteed to succeed, and the system should be
 315   // able to safely unwind and recover from that failure, albeit
 316   // at some additional cost. Override superclass's implementation.
 317   virtual bool collection_attempt_is_safe();
 318 
 319   virtual void collect(bool   full,
 320                        bool   clear_all_soft_refs,
 321                        size_t size,
 322                        bool   is_tlab);
 323   HeapWord* expand_and_allocate(size_t size,
 324                                 bool is_tlab,
 325                                 bool parallel = false);
 326 
 327   oop copy_to_survivor_space(oop old);
 328   uint tenuring_threshold() { return _tenuring_threshold; }
 329 
 330   // Performance Counter support
 331   void update_counters();
 332 
 333   // Printing
 334   virtual const char* name() const;
 335   virtual const char* short_name() const { return "DefNew"; }
 336 
 337   bool must_be_youngest() const { return true; }
 338   bool must_be_oldest() const { return false; }
 339 
 340   // PrintHeapAtGC support.
 341   void print_on(outputStream* st) const;
 342 
 343   void verify();
 344 
 345   bool promo_failure_scan_is_complete() const {
 346     return _promo_failure_scan_stack.is_empty();
 347   }
 348 
 349  protected:
 350   // If clear_space is true, clear the survivor spaces.  Eden is
 351   // cleared if the minimum size of eden is 0.  If mangle_space
 352   // is true, also mangle the space in debug mode.
 353   void compute_space_boundaries(uintx minimum_eden_size,
 354                                 bool clear_space,
 355                                 bool mangle_space);
 356   // Scavenge support
 357   void swap_spaces();
 358 };
 359 
 360 #endif // SHARE_VM_MEMORY_DEFNEWGENERATION_HPP