1 /*
   2  * Copyright (c) 2020, 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_MEMORY_ARCHIVEBUILDER_HPP
  26 #define SHARE_MEMORY_ARCHIVEBUILDER_HPP
  27 
  28 #include "memory/metaspaceClosure.hpp"
  29 #include "utilities/bitMap.inline.hpp"
  30 #include "utilities/growableArray.hpp"
  31 #include "utilities/hashtable.inline.hpp"
  32 #include "utilities/resourceHash.hpp"
  33 
  34 class CHeapBitMap;
  35 class Klass;
  36 class DumpRegion;
  37 class Symbol;
  38 class DumpAllocStats;
  39 
  40 class ArchiveBuilder : public StackObj {
  41 public:
  42   enum FollowMode {
  43     make_a_copy, point_to_it, set_to_null
  44   };
  45 
  46 private:
  47   class SpecialRefInfo {
  48     // We have a "special pointer" of the given _type at _field_offset of _src_obj.
  49     // See MetaspaceClosure::push_special().
  50     MetaspaceClosure::SpecialRef _type;
  51     address _src_obj;
  52     size_t _field_offset;
  53 
  54   public:
  55     SpecialRefInfo() {}
  56     SpecialRefInfo(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset)
  57       : _type(type), _src_obj(src_obj), _field_offset(field_offset) {}
  58 
  59     MetaspaceClosure::SpecialRef type() const { return _type;         }
  60     address src_obj()                   const { return _src_obj;      }
  61     size_t field_offset()               const { return _field_offset; }
  62   };
  63 
  64   class SourceObjInfo {
  65     MetaspaceClosure::Ref* _ref;
  66     uintx _ptrmap_start;     // The bit-offset of the start of this object (inclusive)
  67     uintx _ptrmap_end;       // The bit-offset of the end   of this object (exclusive)
  68     bool _read_only;
  69     FollowMode _follow_mode;
  70     address _dumped_addr;    // Address this->obj(), as used by the dumped archive.
  71 
  72   public:
  73     SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) :
  74       _ref(ref), _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode) {
  75       if (follow_mode == point_to_it) {
  76         _dumped_addr = ref->obj();
  77       } else {
  78         _dumped_addr = NULL;
  79       }
  80     }
  81 
  82     bool should_copy() const { return _follow_mode == make_a_copy; }
  83     MetaspaceClosure::Ref* ref() const { return  _ref; }
  84     void set_dumped_addr(address dumped_addr)  {
  85       assert(should_copy(), "must be");
  86       assert(_dumped_addr == NULL, "cannot be copied twice");
  87       assert(dumped_addr != NULL, "must be a valid copy");
  88       _dumped_addr = dumped_addr;
  89     }
  90     void set_ptrmap_start(uintx v) { _ptrmap_start = v;    }
  91     void set_ptrmap_end(uintx v)   { _ptrmap_end = v;      }
  92     uintx ptrmap_start()  const    { return _ptrmap_start; } // inclusive
  93     uintx ptrmap_end()    const    { return _ptrmap_end;   } // exclusive
  94     bool read_only()      const    { return _read_only;    }
  95     int size_in_bytes()   const    { return _ref->size() * BytesPerWord; }
  96     address dumped_addr() const    { return _dumped_addr; }
  97 
  98     // convenience accessor
  99     address obj() const { return ref()->obj(); }
 100   };
 101 
 102   class SourceObjList {
 103     uintx _total_bytes;
 104     GrowableArray<SourceObjInfo*>* _objs;     // Source objects to be archived
 105     CHeapBitMap _ptrmap;                      // Marks the addresses of the pointer fields
 106                                               // in the source objects
 107   public:
 108     SourceObjList();
 109     ~SourceObjList();
 110 
 111     GrowableArray<SourceObjInfo*>* objs() const { return _objs; }
 112 
 113     void append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info);
 114     void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref);
 115     void relocate(int i, ArchiveBuilder* builder);
 116 
 117     // convenience accessor
 118     SourceObjInfo* at(int i) const { return objs()->at(i); }
 119   };
 120 
 121   class SrcObjTableCleaner {
 122   public:
 123     bool do_entry(address key, const SourceObjInfo* value) {
 124       delete value->ref();
 125       return true;
 126     }
 127   };
 128 
 129   static const int INITIAL_TABLE_SIZE = 15889;
 130   static const int MAX_TABLE_SIZE     = 1000000;
 131 
 132   DumpRegion* _rw_region;
 133   DumpRegion* _ro_region;
 134 
 135   SourceObjList _rw_src_objs;                 // objs to put in rw region
 136   SourceObjList _ro_src_objs;                 // objs to put in ro region
 137   KVHashtable<address, SourceObjInfo, mtClassShared> _src_obj_table;
 138   GrowableArray<Klass*>* _klasses;
 139   GrowableArray<Symbol*>* _symbols;
 140   GrowableArray<SpecialRefInfo>* _special_refs;
 141 
 142   // statistics
 143   int _num_instance_klasses;
 144   int _num_obj_array_klasses;
 145   int _num_type_array_klasses;
 146   DumpAllocStats* _alloc_stats;
 147 
 148   // For global access.
 149   static ArchiveBuilder* _singleton;
 150 
 151 public:
 152   // Use this when you allocate space with MetaspaceShare::read_only_space_alloc()
 153   // outside of ArchiveCompactor::allocate(). These are usually for misc tables
 154   // that are allocated in the RO space.
 155   class OtherROAllocMark {
 156     char* _oldtop;
 157   public:
 158     OtherROAllocMark() {
 159       _oldtop = _singleton->_ro_region->top();
 160     }
 161     ~OtherROAllocMark();
 162   };
 163 
 164 private:
 165   FollowMode get_follow_mode(MetaspaceClosure::Ref *ref);
 166 
 167   void iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers);
 168   void sort_symbols_and_fix_hash();
 169   void sort_klasses();
 170   static int compare_symbols_by_address(Symbol** a, Symbol** b);
 171   static int compare_klass_by_name(Klass** a, Klass** b);
 172 
 173   void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs);
 174   void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info);
 175 
 176   void update_special_refs();
 177   void relocate_embedded_pointers(SourceObjList* src_objs);
 178   void relocate_roots();
 179 
 180   bool is_excluded(Klass* k);
 181   void clean_up_src_obj_table();
 182 
 183 protected:
 184   virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) = 0;
 185 
 186   // Conservative estimate for number of bytes needed for:
 187   size_t _estimated_metsapceobj_bytes;   // all archived MetsapceObj's.
 188 
 189   void set_dump_regions(DumpRegion* rw_region, DumpRegion* ro_region) {
 190     assert(_rw_region == NULL && _ro_region == NULL, "do not change");
 191     _rw_region = rw_region;
 192     _ro_region = ro_region;
 193   }
 194 
 195 public:
 196   ArchiveBuilder(DumpRegion* rw_region, DumpRegion* ro_region);
 197   ~ArchiveBuilder();
 198 
 199   void gather_klasses_and_symbols();
 200   void gather_source_objs();
 201   bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only);
 202   bool gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref, bool read_only);
 203   void add_special_ref(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset);
 204   void remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref);
 205 
 206   void dump_rw_region();
 207   void dump_ro_region();
 208   void relocate_pointers();
 209   void relocate_well_known_klasses();
 210 
 211   address get_dumped_addr(address src_obj) const;
 212 
 213   // All klasses and symbols that will be copied into the archive
 214   GrowableArray<Klass*>*  klasses() const { return _klasses; }
 215   GrowableArray<Symbol*>* symbols() const { return _symbols; }
 216 
 217   static ArchiveBuilder* singleton() {
 218     assert(_singleton != NULL, "ArchiveBuilder must be active");
 219     return _singleton;
 220   }
 221 
 222   static DumpAllocStats* alloc_stats() {
 223     return singleton()->_alloc_stats;
 224   }
 225 
 226   static Klass* get_relocated_klass(Klass* orig_klass) {
 227     Klass* klass = (Klass*)singleton()->get_dumped_addr((address)orig_klass);
 228     assert(klass != NULL && klass->is_klass(), "must be");
 229     return klass;
 230   }
 231 
 232   void print_stats(int ro_all, int rw_all, int mc_all);
 233 };
 234 
 235 #endif // SHARE_MEMORY_ARCHIVEBUILDER_HPP