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