1 /*
   2  * Copyright (c) 2003, 2010, 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_COMPACTINGPERMGENGEN_HPP
  26 #define SHARE_VM_MEMORY_COMPACTINGPERMGENGEN_HPP
  27 
  28 #include "gc_implementation/shared/generationCounters.hpp"
  29 #include "memory/space.hpp"
  30 
  31 // All heaps contains a "permanent generation," containing permanent
  32 // (reflective) objects.  This is like a regular generation in some ways,
  33 // but unlike one in others, and so is split apart.
  34 
  35 class PermanentGenerationSpec;
  36 
  37 // This is the "generation" view of a CompactingPermGen.
  38 // NOTE: the shared spaces used for CDS are here handled in
  39 // a somewhat awkward and potentially buggy fashion, see CR 6801625.
  40 // This infelicity should be fixed, see CR 6897789.
  41 class CompactingPermGenGen: public OneContigSpaceCardGeneration {
  42   friend class VMStructs;
  43   // Abstractly, this is a subtype that gets access to protected fields.
  44   friend class CompactingPermGen;
  45 
  46 private:
  47   // Shared spaces
  48   PermanentGenerationSpec* _spec;
  49   size_t _shared_space_size;
  50   VirtualSpace _ro_vs;
  51   VirtualSpace _rw_vs;
  52   VirtualSpace _md_vs;
  53   VirtualSpace _mc_vs;
  54   BlockOffsetSharedArray* _ro_bts;
  55   BlockOffsetSharedArray* _rw_bts;
  56   OffsetTableContigSpace* _ro_space;
  57   OffsetTableContigSpace* _rw_space;
  58 
  59   // With shared spaces there is a dichotomy in the use of the
  60   // _virtual_space of the generation.  There is a portion of the
  61   // _virtual_space that is used for the unshared part of the
  62   // permanent generation and a portion that is reserved for the shared part.
  63   // The _reserved field in the generation represents both the
  64   // unshared and shared parts of the generation.  The _reserved
  65   // variable is initialized for only the unshared part but is
  66   // later extended to include the shared part during initialization
  67   // if shared spaces are being used.
  68   // The reserved size for the _virtual_space for CompactingPermGenGen
  69   // is the size of the space for the permanent generation including the
  70   // the shared spaces.  This can be seen by the use of MaxPermSize
  71   // in the allocation of PermanentGenerationSpec.  The space for the
  72   // shared spaces is committed separately (???).
  73   // In general at initialization only a part of the
  74   // space for the unshared part of the permanent generation is
  75   // committed and more is committed as the permanent generation is
  76   // grown.  In growing the permanent generation the capacity() and
  77   // max_capacity() of the generation are used.  For the permanent
  78   // generation (implemented with a CompactingPermGenGen) the capacity()
  79   // is taken from the capacity of the space (_the_space variable used for the
  80   // unshared part of the generation) and the max_capacity() is based
  81   // on the size of the _reserved variable (which includes the size of the
  82   // shared spaces) minus the size of the shared spaces.
  83 
  84   // These values are redundant, but are called out separately to avoid
  85   // going through heap/space/gen pointers for performance.
  86   static HeapWord* unshared_bottom;
  87   static HeapWord* unshared_end;
  88   static HeapWord* shared_bottom;
  89   static HeapWord* readonly_bottom;
  90   static HeapWord* readonly_end;
  91   static HeapWord* readwrite_bottom;
  92   static HeapWord* readwrite_end;
  93   static HeapWord* miscdata_bottom;
  94   static HeapWord* miscdata_end;
  95   static HeapWord* misccode_bottom;
  96   static HeapWord* misccode_end;
  97   static HeapWord* shared_end;
  98 
  99   // Performance Counters
 100   GenerationCounters*  _gen_counters;
 101   CSpaceCounters*      _space_counters;
 102 
 103   void initialize_performance_counters();
 104 
 105 public:
 106 
 107   enum {
 108     vtbl_list_size = 16, // number of entries in the shared space vtable list.
 109     num_virtuals = 200   // number of virtual methods in Klass (or
 110                          // subclass) objects, or greater.
 111   };
 112 
 113   enum {
 114     ro = 0,  // read-only shared space in the heap
 115     rw = 1,  // read-write shared space in the heap
 116     md = 2,  // miscellaneous data for initializing tables, etc.
 117     mc = 3,  // miscellaneous code - vtable replacement.
 118     n_regions = 4
 119   };
 120 
 121   CompactingPermGenGen(ReservedSpace rs, ReservedSpace shared_rs,
 122                        size_t initial_byte_size, int level, GenRemSet* remset,
 123                        ContiguousSpace* space,
 124                        PermanentGenerationSpec* perm_spec);
 125 
 126   const char* name() const {
 127     return "compacting perm gen";
 128   }
 129 
 130   const char* short_name() const {
 131     return "Perm";
 132   }
 133 
 134   // Return the maximum capacity for the object space.  This
 135   // explicitly does not include the shared spaces.
 136   size_t max_capacity() const;
 137 
 138   void update_counters();
 139 
 140   void compute_new_size() {
 141     assert(false, "Should not call this -- handled at PermGen level.");
 142   }
 143 
 144   bool must_be_youngest() const { return false; }
 145   bool must_be_oldest() const { return false; }
 146 
 147   OffsetTableContigSpace* ro_space() const { return _ro_space; }
 148   OffsetTableContigSpace* rw_space() const { return _rw_space; }
 149   VirtualSpace*           md_space()       { return &_md_vs; }
 150   VirtualSpace*           mc_space()       { return &_mc_vs; }
 151   ContiguousSpace* unshared_space() const { return _the_space; }
 152 
 153   static bool inline is_shared(const void* p) {
 154     return p >= shared_bottom && p < shared_end;
 155   }
 156   // RedefineClasses note: this tester is used to check residence of
 157   // the specified oop in the shared readonly space and not whether
 158   // the oop is readonly.
 159   static bool inline is_shared_readonly(const void* p) {
 160     return p >= readonly_bottom && p < readonly_end;
 161   }
 162   // RedefineClasses note: this tester is used to check residence of
 163   // the specified oop in the shared readwrite space and not whether
 164   // the oop is readwrite.
 165   static bool inline is_shared_readwrite(const void* p) {
 166     return p >= readwrite_bottom && p < readwrite_end;
 167   }
 168 
 169   // Checks if the pointer is either in unshared space or in shared space
 170   inline bool is_in(const void* p) const {
 171     return OneContigSpaceCardGeneration::is_in(p) || is_shared(p);
 172   }
 173 
 174   inline PermanentGenerationSpec* spec() const { return _spec; }
 175   inline void set_spec(PermanentGenerationSpec* spec) { _spec = spec; }
 176 
 177   void pre_adjust_pointers();
 178   void adjust_pointers();
 179   void space_iterate(SpaceClosure* blk, bool usedOnly = false);
 180   void print_on(outputStream* st) const;
 181   void younger_refs_iterate(OopsInGenClosure* blk);
 182   void compact();
 183   void post_compact();
 184   size_t contiguous_available() const;
 185 
 186   void clear_remembered_set();
 187   void invalidate_remembered_set();
 188 
 189   inline bool block_is_obj(const HeapWord* addr) const {
 190     if      (addr < the_space()->top()) return true;
 191     else if (addr < the_space()->end()) return false;
 192     else if (addr < ro_space()->top())  return true;
 193     else if (addr < ro_space()->end())  return false;
 194     else if (addr < rw_space()->top())  return true;
 195     else                                return false;
 196   }
 197 
 198 
 199   inline size_t block_size(const HeapWord* addr) const {
 200     if (addr < the_space()->top()) {
 201       return oop(addr)->size();
 202     }
 203     else if (addr < the_space()->end()) {
 204       assert(addr == the_space()->top(), "non-block head arg to block_size");
 205       return the_space()->end() - the_space()->top();
 206     }
 207 
 208     else if (addr < ro_space()->top()) {
 209       return oop(addr)->size();
 210     }
 211     else if (addr < ro_space()->end()) {
 212       assert(addr == ro_space()->top(), "non-block head arg to block_size");
 213       return ro_space()->end() - ro_space()->top();
 214     }
 215 
 216     else if (addr < rw_space()->top()) {
 217       return oop(addr)->size();
 218     }
 219     else {
 220       assert(addr == rw_space()->top(), "non-block head arg to block_size");
 221       return rw_space()->end() - rw_space()->top();
 222     }
 223   }
 224 
 225   static void generate_vtable_methods(void** vtbl_list,
 226                                       void** vtable,
 227                                       char** md_top, char* md_end,
 228                                       char** mc_top, char* mc_end);
 229   static void* find_matching_vtbl_ptr(void** vtbl_list,
 230                                       void* new_vtable_start,
 231                                       void* obj);
 232 
 233   void verify(bool allow_dirty);
 234 
 235   // Serialization
 236   static void initialize_oops() KERNEL_RETURN;
 237   static void serialize_oops(SerializeOopClosure* soc);
 238   void serialize_bts(SerializeOopClosure* soc);
 239 
 240   // Initiate dumping of shared file.
 241   static jint dump_shared(GrowableArray<oop>* class_promote_order, TRAPS);
 242 
 243   // JVM/TI RedefineClasses() support:
 244   // Remap the shared readonly space to shared readwrite, private if
 245   // sharing is enabled. Simply returns true if sharing is not enabled
 246   // or if the remapping has already been done by a prior call.
 247   static bool remap_shared_readonly_as_readwrite();
 248 };
 249 
 250 #endif // SHARE_VM_MEMORY_COMPACTINGPERMGENGEN_HPP