1 /*
   2  * Copyright (c) 2001, 2016, 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_GC_PARALLEL_PSYOUNGGEN_HPP
  26 #define SHARE_VM_GC_PARALLEL_PSYOUNGGEN_HPP
  27 
  28 #include "gc/parallel/mutableSpace.hpp"
  29 #include "gc/parallel/objectStartArray.hpp"
  30 #include "gc/parallel/psGenerationCounters.hpp"
  31 #include "gc/parallel/psVirtualspace.hpp"
  32 #include "gc/parallel/spaceCounters.hpp"
  33 
  34 class PSMarkSweepDecorator;
  35 class MarkSweep;
  36 
  37 class PSYoungGen : public CHeapObj<mtGC> {
  38   friend class VMStructs;
  39   friend class ParallelScavengeHeap;
  40   friend class AdjoiningGenerations;
  41 
  42  protected:
  43   MemRegion       _reserved;
  44   PSVirtualSpace* _virtual_space;
  45 
  46   // Spaces
  47   MutableSpace* _eden_space;
  48   MutableSpace* _from_space;
  49   MutableSpace* _to_space;
  50 
  51 
  52   // MarkSweep Decorators
  53   PSMarkSweepDecorator* _eden_mark_sweep;
  54   PSMarkSweepDecorator* _from_mark_sweep;
  55   PSMarkSweepDecorator* _to_mark_sweep;
  56 
  57   // Sizing information, in bytes, set in constructor
  58   const size_t _init_gen_size;
  59   const size_t _min_gen_size;
  60   const size_t _max_gen_size;
  61 
  62   // Performance counters
  63   PSGenerationCounters*     _gen_counters;
  64   SpaceCounters*            _eden_counters;
  65   SpaceCounters*            _from_counters;
  66   SpaceCounters*            _to_counters;
  67 
  68   // Initialize the space boundaries
  69   void compute_initial_space_boundaries();
  70 
  71   // Space boundary helper
  72   void set_space_boundaries(size_t eden_size, size_t survivor_size);
  73 
  74   virtual bool resize_generation(size_t eden_size, size_t survivor_size);
  75   virtual void resize_spaces(size_t eden_size, size_t survivor_size);
  76 
  77   // Adjust the spaces to be consistent with the virtual space.
  78   void post_resize();
  79 
  80   // Return number of bytes that the generation can change.
  81   // These should not be used by PSYoungGen
  82   virtual size_t available_for_expansion();
  83   virtual size_t available_for_contraction();
  84 
  85   // Given a desired shrinkage in the size of the young generation,
  86   // return the actual size available for shrinkage.
  87   virtual size_t limit_gen_shrink(size_t desired_change);
  88   // returns the number of bytes available from the current size
  89   // down to the minimum generation size.
  90   size_t available_to_min_gen();
  91   // Return the number of bytes available for shrinkage considering
  92   // the location the live data in the generation.
  93   virtual size_t available_to_live();
  94 
  95  public:
  96   // Initialize the generation.
  97   PSYoungGen(size_t        initial_byte_size,
  98              size_t        minimum_byte_size,
  99              size_t        maximum_byte_size);
 100   void initialize_work();
 101   virtual void initialize(ReservedSpace rs, size_t alignment);
 102   virtual void initialize_virtual_space(ReservedSpace rs, size_t alignment);
 103 
 104   MemRegion reserved() const            { return _reserved; }
 105 
 106   bool is_in(const void* p) const   {
 107       return _virtual_space->contains((void *)p);
 108   }
 109 
 110   bool is_in_reserved(const void* p) const   {
 111       return reserved().contains((void *)p);
 112   }
 113 
 114   MutableSpace*   eden_space() const    { return _eden_space; }
 115   MutableSpace*   from_space() const    { return _from_space; }
 116   MutableSpace*   to_space() const      { return _to_space; }
 117   PSVirtualSpace* virtual_space() const { return _virtual_space; }
 118 
 119   // For Adaptive size policy
 120   size_t min_gen_size() { return _min_gen_size; }
 121 
 122   // MarkSweep support
 123   PSMarkSweepDecorator* eden_mark_sweep() const    { return _eden_mark_sweep; }
 124   PSMarkSweepDecorator* from_mark_sweep() const    { return _from_mark_sweep; }
 125   PSMarkSweepDecorator* to_mark_sweep() const      { return _to_mark_sweep;   }
 126 
 127   void precompact();
 128   void adjust_pointers(MarkSweep* ms);
 129   void compact();
 130 
 131   // Called during/after GC
 132   void swap_spaces();
 133 
 134   // Resize generation using suggested free space size and survivor size
 135   // NOTE:  "eden_size" and "survivor_size" are suggestions only. Current
 136   //        heap layout (particularly, live objects in from space) might
 137   //        not allow us to use these values.
 138   void resize(size_t eden_size, size_t survivor_size);
 139 
 140   // Size info
 141   size_t capacity_in_bytes() const;
 142   size_t used_in_bytes() const;
 143   size_t free_in_bytes() const;
 144 
 145   size_t capacity_in_words() const;
 146   size_t used_in_words() const;
 147   size_t free_in_words() const;
 148 
 149   // The max this generation can grow to
 150   size_t max_size() const { return _reserved.byte_size(); }
 151 
 152   // The max this generation can grow to if the boundary between
 153   // the generations are allowed to move.
 154   size_t gen_size_limit() const { return _max_gen_size; }
 155 
 156   bool is_maximal_no_gc() const {
 157     return true;  // Never expands except at a GC
 158   }
 159 
 160   // Allocation
 161   HeapWord* allocate(size_t word_size) {
 162     HeapWord* result = eden_space()->cas_allocate(word_size);
 163     return result;
 164   }
 165 
 166   HeapWord* volatile* top_addr() const   { return eden_space()->top_addr(); }
 167   HeapWord** end_addr() const   { return eden_space()->end_addr(); }
 168 
 169   // Iteration.
 170   void oop_iterate(ExtendedOopClosure* cl);
 171   void object_iterate(ObjectClosure* cl);
 172 
 173   virtual void reset_after_change();
 174   virtual void reset_survivors_after_shrink();
 175 
 176   // Performance Counter support
 177   void update_counters();
 178 
 179   // Debugging - do not use for time critical operations
 180   void print() const;
 181   void print_on(outputStream* st) const;
 182   void print_used_change(size_t prev_used) const;
 183   virtual const char* name() const { return "PSYoungGen"; }
 184 
 185   void verify();
 186 
 187   // Space boundary invariant checker
 188   void space_invariants() PRODUCT_RETURN;
 189 
 190   // Helper for mangling survivor spaces.
 191   void mangle_survivors(MutableSpace* s1,
 192                         MemRegion s1MR,
 193                         MutableSpace* s2,
 194                         MemRegion s2MR) PRODUCT_RETURN;
 195 
 196   void record_spaces_top() PRODUCT_RETURN;
 197 };
 198 
 199 #endif // SHARE_VM_GC_PARALLEL_PSYOUNGGEN_HPP