1 /*
   2  * Copyright (c) 2012, 2016, 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 "utilities/exceptions.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 #define DEFAULT_VTBL_LIST_SIZE          (17)  // number of entries in the shared space vtable list.
  36 #define DEFAULT_VTBL_VIRTUALS_COUNT     (200) // maximum number of virtual functions
  37 // If virtual functions are added to Metadata,
  38 // this number needs to be increased.  Also,
  39 // SharedMiscCodeSize will need to be increased.
  40 // The following 2 sizes were based on
  41 // MetaspaceShared::generate_vtable_methods()
  42 #define DEFAULT_VTBL_METHOD_SIZE        (16)  // conservative size of the mov1 and jmp instructions
  43 // for the x64 platform
  44 #define DEFAULT_VTBL_COMMON_CODE_SIZE   (1*K) // conservative size of the "common_code" for the x64 platform
  45 
  46 #define DEFAULT_SHARED_READ_WRITE_SIZE  (NOT_LP64(6*M) LP64_ONLY(10*M))
  47 #define MIN_SHARED_READ_WRITE_SIZE      (NOT_LP64(6*M) LP64_ONLY(10*M))
  48 
  49 #define DEFAULT_SHARED_READ_ONLY_SIZE   (NOT_LP64(6*M) LP64_ONLY(10*M))
  50 #define MIN_SHARED_READ_ONLY_SIZE       (NOT_LP64(6*M) LP64_ONLY(10*M))
  51 
  52 // the MIN_SHARED_MISC_DATA_SIZE and MIN_SHARED_MISC_CODE_SIZE estimates are based on
  53 // the sizes required for dumping the archive using the default classlist. The sizes
  54 // are multiplied by 1.5 for a safety margin.
  55 
  56 #define DEFAULT_SHARED_MISC_DATA_SIZE   (NOT_LP64(2*M) LP64_ONLY(4*M))
  57 #define MIN_SHARED_MISC_DATA_SIZE       (NOT_LP64(1*M) LP64_ONLY(1200*K))
  58 
  59 #define DEFAULT_SHARED_MISC_CODE_SIZE   (120*K)
  60 #define MIN_SHARED_MISC_CODE_SIZE       (NOT_LP64(63*K) LP64_ONLY(69*K))
  61 #define DEFAULT_COMBINED_SIZE           (DEFAULT_SHARED_READ_WRITE_SIZE+DEFAULT_SHARED_READ_ONLY_SIZE+DEFAULT_SHARED_MISC_DATA_SIZE+DEFAULT_SHARED_MISC_CODE_SIZE)
  62 
  63 // the max size is the MAX size (ie. 0x7FFFFFFF) - the total size of
  64 // the other 3 sections - page size (to avoid overflow in case the final
  65 // size will get aligned up on page size)
  66 #define SHARED_PAGE                     ((size_t)os::vm_page_size())
  67 #define MAX_SHARED_DELTA                (0x7FFFFFFF)
  68 #define MAX_SHARED_READ_WRITE_SIZE      (MAX_SHARED_DELTA-(MIN_SHARED_READ_ONLY_SIZE+MIN_SHARED_MISC_DATA_SIZE+MIN_SHARED_MISC_CODE_SIZE)-SHARED_PAGE)
  69 #define MAX_SHARED_READ_ONLY_SIZE       (MAX_SHARED_DELTA-(MIN_SHARED_READ_WRITE_SIZE+MIN_SHARED_MISC_DATA_SIZE+MIN_SHARED_MISC_CODE_SIZE)-SHARED_PAGE)
  70 #define MAX_SHARED_MISC_DATA_SIZE       (MAX_SHARED_DELTA-(MIN_SHARED_READ_WRITE_SIZE+MIN_SHARED_READ_ONLY_SIZE+MIN_SHARED_MISC_CODE_SIZE)-SHARED_PAGE)
  71 #define MAX_SHARED_MISC_CODE_SIZE       (MAX_SHARED_DELTA-(MIN_SHARED_READ_WRITE_SIZE+MIN_SHARED_READ_ONLY_SIZE+MIN_SHARED_MISC_DATA_SIZE)-SHARED_PAGE)
  72 
  73 #define LargeSharedArchiveSize          (300*M)
  74 #define HugeSharedArchiveSize           (800*M)
  75 #define ReadOnlyRegionPercentage        0.4
  76 #define ReadWriteRegionPercentage       0.55
  77 #define MiscDataRegionPercentage        0.03
  78 #define MiscCodeRegionPercentage        0.02
  79 #define LargeThresholdClassCount        5000
  80 #define HugeThresholdClassCount         40000
  81 
  82 #define SET_ESTIMATED_SIZE(type, region)                              \
  83   Shared ##region## Size  = FLAG_IS_DEFAULT(Shared ##region## Size) ? \
  84     (uintx)(type ## SharedArchiveSize *  region ## RegionPercentage) : Shared ## region ## Size
  85 
  86 class FileMapInfo;
  87 
  88 class MetaspaceSharedStats VALUE_OBJ_CLASS_SPEC {
  89 public:
  90   MetaspaceSharedStats() {
  91     memset(this, 0, sizeof(*this));
  92   }
  93   CompactHashtableStats symbol;
  94   CompactHashtableStats string;
  95 };
  96 
  97 class SharedMiscRegion VALUE_OBJ_CLASS_SPEC {
  98 private:
  99   VirtualSpace _vs;
 100   char* _alloc_top;
 101   SharedSpaceType _space_type;
 102 
 103 public:
 104   void initialize(ReservedSpace rs, size_t committed_byte_size,  SharedSpaceType space_type);
 105   VirtualSpace* virtual_space() {
 106     return &_vs;
 107   }
 108   char* low() const {
 109     return _vs.low();
 110   }
 111   char* alloc_top() const {
 112     return _alloc_top;
 113   }
 114   char* alloc(size_t num_bytes) NOT_CDS_RETURN_(NULL);
 115 };
 116 
 117 // Class Data Sharing Support
 118 class MetaspaceShared : AllStatic {
 119 
 120   // CDS support
 121   static ReservedSpace* _shared_rs;
 122   static int _max_alignment;
 123   static MetaspaceSharedStats _stats;
 124   static bool _link_classes_made_progress;
 125   static bool _check_classes_made_progress;
 126   static bool _has_error_classes;
 127   static bool _archive_loading_failed;
 128   static bool _remapped_readwrite;
 129   static address _cds_i2i_entry_code_buffers;
 130   static size_t  _cds_i2i_entry_code_buffers_size;
 131 
 132   // Used only during dumping.
 133   static SharedMiscRegion _md;
 134   static SharedMiscRegion _mc;
 135  public:
 136   enum {
 137     vtbl_list_size         = DEFAULT_VTBL_LIST_SIZE,
 138     num_virtuals           = DEFAULT_VTBL_VIRTUALS_COUNT,
 139     vtbl_method_size       = DEFAULT_VTBL_METHOD_SIZE,
 140     vtbl_common_code_size  = DEFAULT_VTBL_COMMON_CODE_SIZE
 141   };
 142 
 143   enum {
 144     ro = 0,  // read-only shared space in the heap
 145     rw = 1,  // read-write shared space in the heap
 146     md = 2,  // miscellaneous data for initializing tables, etc.
 147     mc = 3,  // miscellaneous code - vtable replacement.
 148     max_strings = 2, // max number of string regions in string space
 149     num_non_strings = 4, // number of non-string regions
 150     first_string = num_non_strings, // index of first string region
 151     n_regions = max_strings + num_non_strings // total number of regions
 152   };
 153 
 154   // Accessor functions to save shared space created for metadata, which has
 155   // extra space allocated at the end for miscellaneous data and code.
 156   static void set_max_alignment(int alignment) {
 157     CDS_ONLY(_max_alignment = alignment);
 158   }
 159 
 160   static int max_alignment() {
 161     CDS_ONLY(return _max_alignment);
 162     NOT_CDS(return 0);
 163   }
 164 
 165   static void prepare_for_dumping() NOT_CDS_RETURN;
 166   static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
 167   static int preload_and_dump(const char * class_list_path,
 168                               GrowableArray<Klass*>* class_promote_order,
 169                               TRAPS) NOT_CDS_RETURN_(0);
 170 
 171   static ReservedSpace* shared_rs() {
 172     CDS_ONLY(return _shared_rs);
 173     NOT_CDS(return NULL);
 174   }
 175 
 176   static void initialize_shared_rs(ReservedSpace* rs) NOT_CDS_RETURN;
 177 
 178   static void set_archive_loading_failed() {
 179     _archive_loading_failed = true;
 180   }
 181   static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false);
 182   static void initialize_shared_spaces() NOT_CDS_RETURN;
 183   static void fixup_shared_string_regions() NOT_CDS_RETURN;
 184 
 185   // Return true if given address is in the mapped shared space.
 186   static bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false);
 187 
 188   // Return true if given address is in the shared region corresponding to the idx
 189   static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 190 
 191   static bool is_string_region(int idx) NOT_CDS_RETURN_(false);
 192 
 193   static void generate_vtable_methods(void** vtbl_list,
 194                                       void** vtable,
 195                                       char** md_top, char* md_end,
 196                                       char** mc_top, char* mc_end);
 197   static void serialize(SerializeClosure* sc, GrowableArray<MemRegion> *string_space,
 198                         size_t* space_size);
 199 
 200   static MetaspaceSharedStats* stats() {
 201     return &_stats;
 202   }
 203 
 204   // JVM/TI RedefineClasses() support:
 205   // Remap the shared readonly space to shared readwrite, private if
 206   // sharing is enabled. Simply returns true if sharing is not enabled
 207   // or if the remapping has already been done by a prior call.
 208   static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
 209   static bool remapped_readwrite() {
 210     CDS_ONLY(return _remapped_readwrite);
 211     NOT_CDS(return false);
 212   }
 213 
 214   static void print_shared_spaces();
 215 
 216   static bool try_link_class(InstanceKlass* ik, TRAPS);
 217   static void link_one_shared_class(Klass* obj, TRAPS);
 218   static void check_one_shared_class(Klass* obj);
 219   static void check_shared_class_loader_type(Klass* obj);
 220   static void link_and_cleanup_shared_classes(TRAPS);
 221 
 222   static int count_class(const char* classlist_file);
 223   static void estimate_regions_size() NOT_CDS_RETURN;
 224 
 225   // Allocate a block of memory from the "mc" or "md" regions.
 226   static char* misc_code_space_alloc(size_t num_bytes) {  return _mc.alloc(num_bytes); }
 227   static char* misc_data_space_alloc(size_t num_bytes) {  return _md.alloc(num_bytes); }
 228 
 229   static address cds_i2i_entry_code_buffers(size_t total_size);
 230 
 231   static address cds_i2i_entry_code_buffers() {
 232     return _cds_i2i_entry_code_buffers;
 233   }
 234   static size_t cds_i2i_entry_code_buffers_size() {
 235     return _cds_i2i_entry_code_buffers_size;
 236   }
 237 
 238   static SharedMiscRegion* misc_code_region() {
 239     assert(DumpSharedSpaces, "used during dumping only");
 240     return &_mc;
 241   }
 242   static SharedMiscRegion* misc_data_region() {
 243     assert(DumpSharedSpaces, "used during dumping only");
 244     return &_md;
 245   }
 246 };
 247 #endif // SHARE_VM_MEMORY_METASPACESHARED_HPP