1 /*
   2  * Copyright (c) 2001, 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_GC_PARALLEL_PSOLDGEN_HPP
  26 #define SHARE_VM_GC_PARALLEL_PSOLDGEN_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/psFileBackedVirtualspace.hpp"
  33 #include "gc/parallel/spaceCounters.hpp"
  34 #include "runtime/safepoint.hpp"
  35 
  36 class PSMarkSweepDecorator;
  37 
  38 class PSOldGen : public CHeapObj<mtGC> {
  39   friend class VMStructs;
  40   friend class PSPromotionManager; // Uses the cas_allocate methods
  41   friend class ParallelScavengeHeap;
  42   friend class AdjoiningGenerations;
  43 
  44  protected:
  45   MemRegion                _reserved;          // Used for simple containment tests
  46   PSVirtualSpace*          _virtual_space;     // Controls mapping and unmapping of virtual mem
  47   ObjectStartArray         _start_array;       // Keeps track of where objects start in a 512b block
  48   MutableSpace*            _object_space;      // Where all the objects live
  49 #if INCLUDE_SERIALGC
  50   PSMarkSweepDecorator*    _object_mark_sweep; // The mark sweep view of _object_space
  51 #endif
  52   const char* const        _name;              // Name of this generation.
  53 
  54   // Performance Counters
  55   PSGenerationCounters*    _gen_counters;
  56   SpaceCounters*           _space_counters;
  57 
  58   // Sizing information, in bytes, set in constructor
  59   const size_t _init_gen_size;
  60   const size_t _min_gen_size;
  61   const size_t _max_gen_size;
  62 
  63   // Used when initializing the _name field.
  64   static inline const char* select_name();
  65 
  66 #ifdef ASSERT
  67   void assert_block_in_covered_region(MemRegion new_memregion) {
  68     // Explictly capture current covered_region in a local
  69     MemRegion covered_region = this->start_array()->covered_region();
  70     assert(covered_region.contains(new_memregion),
  71            "new region is not in covered_region [ " PTR_FORMAT ", " PTR_FORMAT " ], "
  72            "new region [ " PTR_FORMAT ", " PTR_FORMAT " ], "
  73            "object space [ " PTR_FORMAT ", " PTR_FORMAT " ]",
  74            p2i(covered_region.start()),
  75            p2i(covered_region.end()),
  76            p2i(new_memregion.start()),
  77            p2i(new_memregion.end()),
  78            p2i(this->object_space()->used_region().start()),
  79            p2i(this->object_space()->used_region().end()));
  80   }
  81 #endif
  82 
  83   HeapWord* allocate_noexpand(size_t word_size) {
  84     // We assume the heap lock is held here.
  85     assert_locked_or_safepoint(Heap_lock);
  86     HeapWord* res = object_space()->allocate(word_size);
  87     if (res != NULL) {
  88       DEBUG_ONLY(assert_block_in_covered_region(MemRegion(res, word_size)));
  89       _start_array.allocate_block(res);
  90     }
  91     return res;
  92   }
  93 
  94   // Support for MT garbage collection. CAS allocation is lower overhead than grabbing
  95   // and releasing the heap lock, which is held during gc's anyway. This method is not
  96   // safe for use at the same time as allocate_noexpand()!
  97   HeapWord* cas_allocate_noexpand(size_t word_size) {
  98     assert(SafepointSynchronize::is_at_safepoint(), "Must only be called at safepoint");
  99     HeapWord* res = object_space()->cas_allocate(word_size);
 100     if (res != NULL) {
 101       DEBUG_ONLY(assert_block_in_covered_region(MemRegion(res, word_size)));
 102       _start_array.allocate_block(res);
 103     }
 104     return res;
 105   }
 106 
 107   // Support for MT garbage collection. See above comment.
 108   HeapWord* cas_allocate(size_t word_size) {
 109     HeapWord* res = cas_allocate_noexpand(word_size);
 110     return (res == NULL) ? expand_and_cas_allocate(word_size) : res;
 111   }
 112 
 113   HeapWord* expand_and_allocate(size_t word_size);
 114   HeapWord* expand_and_cas_allocate(size_t word_size);
 115   void expand(size_t bytes);
 116   bool expand_by(size_t bytes);
 117   bool expand_to_reserved();
 118 
 119   void shrink(size_t bytes);
 120 
 121   void post_resize();
 122 
 123  public:
 124   // Initialize the generation.
 125   PSOldGen(ReservedSpace rs, size_t alignment,
 126            size_t initial_size, size_t min_size, size_t max_size,
 127            const char* perf_data_name, int level);
 128 
 129   PSOldGen(size_t initial_size, size_t min_size, size_t max_size,
 130            const char* perf_data_name, int level);
 131 
 132   virtual void initialize(ReservedSpace rs, size_t alignment,
 133                   const char* perf_data_name, int level);
 134   void initialize_virtual_space(ReservedSpace rs, size_t alignment);
 135   virtual void initialize_work(const char* perf_data_name, int level);
 136   virtual void initialize_performance_counters(const char* perf_data_name, int level);
 137 
 138   MemRegion reserved() const                { return _reserved; }
 139   virtual size_t max_gen_size()             { return _max_gen_size; }
 140   size_t min_gen_size()                     { return _min_gen_size; }
 141 
 142   // Returns limit on the maximum size of the generation.  This
 143   // is the same as _max_gen_size for PSOldGen but need not be
 144   // for a derived class.
 145   virtual size_t gen_size_limit();
 146 
 147   bool is_in(const void* p) const           {
 148     return _virtual_space->contains((void *)p);
 149   }
 150 
 151   bool is_in_reserved(const void* p) const {
 152     return reserved().contains(p);
 153   }
 154 
 155   MutableSpace*         object_space() const      { return _object_space; }
 156 #if INCLUDE_SERIALGC
 157   PSMarkSweepDecorator* object_mark_sweep() const { return _object_mark_sweep; }
 158 #endif
 159   ObjectStartArray*     start_array()             { return &_start_array; }
 160   PSVirtualSpace*       virtual_space() const     { return _virtual_space;}
 161 
 162   // Has the generation been successfully allocated?
 163   bool is_allocated();
 164 
 165 #if INCLUDE_SERIALGC
 166   // MarkSweep methods
 167   virtual void precompact();
 168   void adjust_pointers();
 169   void compact();
 170 #endif
 171 
 172   // Size info
 173   size_t capacity_in_bytes() const        { return object_space()->capacity_in_bytes(); }
 174   size_t used_in_bytes() const            { return object_space()->used_in_bytes(); }
 175   size_t free_in_bytes() const            { return object_space()->free_in_bytes(); }
 176 
 177   size_t capacity_in_words() const        { return object_space()->capacity_in_words(); }
 178   size_t used_in_words() const            { return object_space()->used_in_words(); }
 179   size_t free_in_words() const            { return object_space()->free_in_words(); }
 180 
 181   // Includes uncommitted memory
 182   size_t contiguous_available() const;
 183 
 184   bool is_maximal_no_gc() const {
 185     return virtual_space()->uncommitted_size() == 0;
 186   }
 187 
 188   // Calculating new sizes
 189   void resize(size_t desired_free_space);
 190 
 191   // Allocation. We report all successful allocations to the size policy
 192   // Note that the perm gen does not use this method, and should not!
 193   HeapWord* allocate(size_t word_size);
 194 
 195   // Iteration.
 196   void oop_iterate(OopIterateClosure* cl) { object_space()->oop_iterate(cl); }
 197   void object_iterate(ObjectClosure* cl) { object_space()->object_iterate(cl); }
 198 
 199   // Debugging - do not use for time critical operations
 200   virtual void print() const;
 201   virtual void print_on(outputStream* st) const;
 202   void print_used_change(size_t prev_used) const;
 203 
 204   void verify();
 205   void verify_object_start_array();
 206 
 207   // These should not used
 208   virtual void reset_after_change();
 209 
 210   // These should not used
 211   virtual size_t available_for_expansion();
 212   virtual size_t available_for_contraction();
 213 
 214   void space_invariants() PRODUCT_RETURN;
 215 
 216   // Performance Counter support
 217   void update_counters();
 218 
 219   // Printing support
 220   virtual const char* name() const { return _name; }
 221 
 222   // Debugging support
 223   // Save the tops of all spaces for later use during mangling.
 224   void record_spaces_top() PRODUCT_RETURN;
 225 };
 226 
 227 #endif // SHARE_VM_GC_PARALLEL_PSOLDGEN_HPP