1 /*
   2  * Copyright (c) 2012, 2015, 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 LargeSharedArchiveSize    (300*M)
  36 #define HugeSharedArchiveSize     (800*M)
  37 #define ReadOnlyRegionPercentage  0.4
  38 #define ReadWriteRegionPercentage 0.55
  39 #define MiscDataRegionPercentage  0.03
  40 #define MiscCodeRegionPercentage  0.02
  41 #define LargeThresholdClassCount  5000
  42 #define HugeThresholdClassCount   40000
  43 
  44 #define SET_ESTIMATED_SIZE(type, region)                              \
  45   Shared ##region## Size  = FLAG_IS_DEFAULT(Shared ##region## Size) ? \
  46     (uintx)(type ## SharedArchiveSize *  region ## RegionPercentage) : Shared ## region ## Size
  47 
  48 class FileMapInfo;
  49 
  50 class MetaspaceSharedStats VALUE_OBJ_CLASS_SPEC {
  51 public:
  52   MetaspaceSharedStats() {
  53     memset(this, 0, sizeof(*this));
  54   }
  55   CompactHashtableStats symbol;
  56   CompactHashtableStats string;
  57 };
  58 
  59 // Class Data Sharing Support
  60 class MetaspaceShared : AllStatic {
  61 
  62   // CDS support
  63   static ReservedSpace* _shared_rs;
  64   static int _max_alignment;
  65   static MetaspaceSharedStats _stats;
  66   static bool _link_classes_made_progress;
  67   static bool _check_classes_made_progress;
  68   static bool _has_error_classes;
  69   static bool _archive_loading_failed;
  70  public:
  71   enum {
  72     vtbl_list_size         = DEFAULT_VTBL_LIST_SIZE,
  73     num_virtuals           = DEFAULT_VTBL_VIRTUALS_COUNT,
  74     vtbl_method_size       = DEFAULT_VTBL_METHOD_SIZE,
  75     vtbl_common_code_size  = DEFAULT_VTBL_COMMON_CODE_SIZE
  76   };
  77 
  78   enum {
  79     ro = 0,  // read-only shared space in the heap
  80     rw = 1,  // read-write shared space in the heap
  81     md = 2,  // miscellaneous data for initializing tables, etc.
  82     mc = 3,  // miscellaneous code - vtable replacement.
  83     max_strings = 2, // max number of string regions in string space
  84     num_non_strings = 4, // number of non-string regions
  85     first_string = num_non_strings, // index of first string region
  86     n_regions = max_strings + num_non_strings // total number of regions
  87   };
  88 
  89   // Accessor functions to save shared space created for metadata, which has
  90   // extra space allocated at the end for miscellaneous data and code.
  91   static void set_max_alignment(int alignment) {
  92     CDS_ONLY(_max_alignment = alignment);
  93   }
  94 
  95   static int max_alignment() {
  96     CDS_ONLY(return _max_alignment);
  97     NOT_CDS(return 0);
  98   }
  99 
 100   static void prepare_for_dumping() NOT_CDS_RETURN;
 101   static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
 102   static int preload_and_dump(const char * class_list_path,
 103                               GrowableArray<Klass*>* class_promote_order,
 104                               TRAPS) NOT_CDS_RETURN_(0);
 105 
 106   static ReservedSpace* shared_rs() {
 107     CDS_ONLY(return _shared_rs);
 108     NOT_CDS(return NULL);
 109   }
 110 
 111   static void set_shared_rs(ReservedSpace* rs) {
 112     CDS_ONLY(_shared_rs = rs;)
 113   }
 114 
 115   static void set_archive_loading_failed() {
 116     _archive_loading_failed = true;
 117   }
 118   static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false);
 119   static void initialize_shared_spaces() NOT_CDS_RETURN;
 120   static void fixup_shared_string_regions() NOT_CDS_RETURN;
 121 
 122   // Return true if given address is in the mapped shared space.
 123   static bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false);
 124 
 125   static bool is_string_region(int idx) NOT_CDS_RETURN_(false);
 126 
 127   static void generate_vtable_methods(void** vtbl_list,
 128                                       void** vtable,
 129                                       char** md_top, char* md_end,
 130                                       char** mc_top, char* mc_end);
 131   static void serialize(SerializeClosure* sc);
 132 
 133   static MetaspaceSharedStats* stats() {
 134     return &_stats;
 135   }
 136 
 137   // JVM/TI RedefineClasses() support:
 138   // Remap the shared readonly space to shared readwrite, private if
 139   // sharing is enabled. Simply returns true if sharing is not enabled
 140   // or if the remapping has already been done by a prior call.
 141   static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
 142 
 143   static void print_shared_spaces();
 144 
 145   static bool try_link_class(InstanceKlass* ik, TRAPS);
 146   static void link_one_shared_class(Klass* obj, TRAPS);
 147   static void check_one_shared_class(Klass* obj);
 148   static void link_and_cleanup_shared_classes(TRAPS);
 149 
 150   static int count_class(const char* classlist_file);
 151   static void estimate_regions_size() NOT_CDS_RETURN;
 152 };
 153 #endif // SHARE_VM_MEMORY_METASPACESHARED_HPP