1 /*
   2  * Copyright (c) 2012, 2018, 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_METASPACESHARED_HPP
  26 #define SHARE_VM_MEMORY_METASPACESHARED_HPP
  27 
  28 #include "classfile/compactHashtable.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/memRegion.hpp"
  31 #include "memory/virtualspace.hpp"
  32 #include "oops/oop.hpp"
  33 #include "utilities/exceptions.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "utilities/resourceHash.hpp"
  36 
  37 #define MAX_SHARED_DELTA                (0x7FFFFFFF)
  38 
  39 class FileMapInfo;
  40 
  41 class MetaspaceSharedStats {
  42 public:
  43   MetaspaceSharedStats() {
  44     memset(this, 0, sizeof(*this));
  45   }
  46   CompactHashtableStats symbol;
  47   CompactHashtableStats string;
  48 };
  49 
  50 // Class Data Sharing Support
  51 class MetaspaceShared : AllStatic {
  52 
  53   // CDS support
  54   static ReservedSpace _shared_rs;
  55   static VirtualSpace _shared_vs;
  56   static int _max_alignment;
  57   static MetaspaceSharedStats _stats;
  58   static bool _has_error_classes;
  59   static bool _archive_loading_failed;
  60   static bool _remapped_readwrite;
  61   static bool _open_archive_heap_region_mapped;
  62   static address _cds_i2i_entry_code_buffers;
  63   static size_t  _cds_i2i_entry_code_buffers_size;
  64   static size_t  _core_spaces_size;
  65  public:
  66   enum {
  67     // core archive spaces
  68     mc = 0,  // miscellaneous code for method trampolines
  69     rw = 1,  // read-write shared space in the heap
  70     ro = 2,  // read-only shared space in the heap
  71     md = 3,  // miscellaneous data for initializing tables, etc.
  72     num_core_spaces = 4, // number of non-string regions
  73 
  74     // optional mapped spaces
  75     // Currently it only contains class file data.
  76     od = num_core_spaces,
  77     num_non_heap_spaces = od + 1,
  78 
  79     // mapped java heap regions
  80     first_string = od + 1, // index of first string region
  81     max_strings = 2, // max number of string regions in string space
  82     first_open_archive_heap_region = first_string + max_strings,
  83     max_open_archive_heap_region = 2,
  84 
  85     last_valid_region = first_open_archive_heap_region + max_open_archive_heap_region - 1,
  86     n_regions =  last_valid_region + 1 // total number of regions
  87   };
  88 
  89   static void prepare_for_dumping() NOT_CDS_RETURN;
  90   static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
  91   static int preload_classes(const char * class_list_path,
  92                              TRAPS) NOT_CDS_RETURN_(0);
  93 
  94 #if INCLUDE_CDS_JAVA_HEAP
  95  private:
  96   static bool obj_equals(oop const& p1, oop const& p2) {
  97     return p1 == p2;
  98   }
  99   static unsigned obj_hash(oop const& p);
 100 
 101   typedef ResourceHashtable<oop, oop,
 102       MetaspaceShared::obj_hash,
 103       MetaspaceShared::obj_equals,
 104       15889, // prime number
 105       ResourceObj::C_HEAP> ArchivedObjectCache;
 106   static ArchivedObjectCache* _archive_object_cache;
 107 
 108  public:
 109   static ArchivedObjectCache* archive_object_cache() {
 110     return _archive_object_cache;
 111   }
 112   static oop find_archived_heap_object(oop obj);
 113   static oop archive_heap_object(oop obj, Thread* THREAD);
 114   static void archive_klass_objects(Thread* THREAD);
 115 #endif
 116   static bool is_heap_object_archiving_allowed() {
 117     CDS_JAVA_HEAP_ONLY(return (UseG1GC && UseCompressedOops && UseCompressedClassPointers);)
 118     NOT_CDS_JAVA_HEAP(return false;)
 119   }
 120   static void create_archive_object_cache() {
 121     CDS_JAVA_HEAP_ONLY(_archive_object_cache = new (ResourceObj::C_HEAP, mtClass)ArchivedObjectCache(););
 122   }
 123   static void destroy_archive_object_cache() {
 124     CDS_JAVA_HEAP_ONLY(delete _archive_object_cache; _archive_object_cache = NULL;);
 125   }
 126   static void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
 127 
 128   static void dump_closed_archive_heap_objects(GrowableArray<MemRegion> * closed_archive) NOT_CDS_JAVA_HEAP_RETURN;
 129 
 130   static void dump_open_archive_heap_objects(GrowableArray<MemRegion> * open_archive) NOT_CDS_JAVA_HEAP_RETURN;
 131   static void set_open_archive_heap_region_mapped() {
 132     CDS_JAVA_HEAP_ONLY(_open_archive_heap_region_mapped = true);
 133     NOT_CDS_JAVA_HEAP_RETURN;
 134   }
 135   static bool open_archive_heap_region_mapped() {
 136     CDS_JAVA_HEAP_ONLY(return _open_archive_heap_region_mapped);
 137     NOT_CDS_JAVA_HEAP_RETURN_(false);
 138   }
 139 
 140   static ReservedSpace* shared_rs() {
 141     CDS_ONLY(return &_shared_rs);
 142     NOT_CDS(return NULL);
 143   }
 144   static void commit_shared_space_to(char* newtop) NOT_CDS_RETURN;
 145   static size_t core_spaces_size() {
 146     return _core_spaces_size;
 147   }
 148   static void initialize_dumptime_shared_and_meta_spaces() NOT_CDS_RETURN;
 149   static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN;
 150   static void post_initialize(TRAPS) NOT_CDS_RETURN;
 151 
 152   // Delta of this object from the bottom of the archive.
 153   static uintx object_delta(void* obj) {
 154     assert(DumpSharedSpaces, "supported only for dumping");
 155     assert(shared_rs()->contains(obj), "must be");
 156     address base_address = address(shared_rs()->base());
 157     uintx delta = address(obj) - base_address;
 158     return delta;
 159   }
 160 
 161   static void set_archive_loading_failed() {
 162     _archive_loading_failed = true;
 163   }
 164   static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false);
 165   static void initialize_shared_spaces() NOT_CDS_RETURN;
 166 
 167   // Return true if given address is in the shared metaspace regions (i.e., excluding any
 168   // mapped shared heap regions.)
 169   static bool is_in_shared_metaspace(const void* p) {
 170     // If no shared metaspace regions are mapped, MetaspceObj::_shared_metaspace_{base,top} will
 171     // both be NULL and all values of p will be rejected quickly.
 172     return (p < MetaspaceObj::_shared_metaspace_top && p >= MetaspaceObj::_shared_metaspace_base);
 173   }
 174 
 175   // Return true if given address is in the shared region corresponding to the idx
 176   static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 177 
 178   static bool is_heap_region(int idx) {
 179     CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_string &&
 180                                idx < MetaspaceShared::first_open_archive_heap_region +
 181                                      MetaspaceShared::max_open_archive_heap_region));
 182     NOT_CDS_JAVA_HEAP_RETURN_(false);
 183   }
 184   static bool is_string_region(int idx) {
 185     CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_string &&
 186                                idx < MetaspaceShared::first_string + MetaspaceShared::max_strings));
 187     NOT_CDS_JAVA_HEAP_RETURN_(false);
 188   }
 189   static bool is_open_archive_heap_region(int idx) {
 190     CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_open_archive_heap_region &&
 191                                idx < MetaspaceShared::first_open_archive_heap_region +
 192                                      MetaspaceShared::max_open_archive_heap_region));
 193     NOT_CDS_JAVA_HEAP_RETURN_(false);
 194   }
 195   static bool is_in_trampoline_frame(address addr) NOT_CDS_RETURN_(false);
 196 
 197   static void allocate_cpp_vtable_clones();
 198   static intptr_t* clone_cpp_vtables(intptr_t* p);
 199   static void zero_cpp_vtable_clones_for_writing();
 200   static void patch_cpp_vtable_pointers();
 201   static bool is_valid_shared_method(const Method* m) NOT_CDS_RETURN_(false);
 202   static void serialize(SerializeClosure* sc) NOT_CDS_RETURN;
 203   static void serialize_well_known_classes(SerializeClosure* soc) NOT_CDS_RETURN;
 204 
 205   static MetaspaceSharedStats* stats() {
 206     return &_stats;
 207   }
 208 
 209   static void report_out_of_space(const char* name, size_t needed_bytes);
 210 
 211   // JVM/TI RedefineClasses() support:
 212   // Remap the shared readonly space to shared readwrite, private if
 213   // sharing is enabled. Simply returns true if sharing is not enabled
 214   // or if the remapping has already been done by a prior call.
 215   static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
 216   static bool remapped_readwrite() {
 217     CDS_ONLY(return _remapped_readwrite);
 218     NOT_CDS(return false);
 219   }
 220 
 221   static void print_shared_spaces();
 222 
 223   static bool try_link_class(InstanceKlass* ik, TRAPS);
 224   static void link_and_cleanup_shared_classes(TRAPS);
 225   static void check_shared_class_loader_type(Klass* obj);
 226 
 227   // Allocate a block of memory from the "mc", "ro", or "rw" regions.
 228   static char* misc_code_space_alloc(size_t num_bytes);
 229   static char* read_only_space_alloc(size_t num_bytes);
 230 
 231   template <typename T>
 232   static Array<T>* new_ro_array(int length) {
 233 #if INCLUDE_CDS
 234     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
 235     Array<T>* array = (Array<T>*)read_only_space_alloc(byte_size);
 236     array->initialize(length);
 237     return array;
 238 #else
 239     return NULL;
 240 #endif
 241   }
 242 
 243   static address cds_i2i_entry_code_buffers(size_t total_size);
 244 
 245   static address cds_i2i_entry_code_buffers() {
 246     return _cds_i2i_entry_code_buffers;
 247   }
 248   static size_t cds_i2i_entry_code_buffers_size() {
 249     return _cds_i2i_entry_code_buffers_size;
 250   }
 251   static void relocate_klass_ptr(oop o);
 252 
 253   static Klass* get_relocated_klass(Klass *k);
 254 
 255 private:
 256   static void read_extra_data(const char* filename, TRAPS) NOT_CDS_RETURN;
 257 };
 258 #endif // SHARE_VM_MEMORY_METASPACESHARED_HPP