1 /*
   2  * Copyright (c) 2012, 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 #ifndef SHARE_VM_MEMORY_METASPACE_SHARED_HPP
  25 #define SHARE_VM_MEMORY_METASPACE_SHARED_HPP
  26 
  27 #include "classfile/compactHashtable.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "runtime/virtualspace.hpp"
  31 #include "utilities/exceptions.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 #define LargeSharedArchiveSize    (300*M)
  35 #define HugeSharedArchiveSize     (800*M)
  36 #define ReadOnlyRegionPercentage  0.4
  37 #define ReadWriteRegionPercentage 0.55
  38 #define MiscDataRegionPercentage  0.03
  39 #define MiscCodeRegionPercentage  0.02
  40 #define LargeThresholdClassCount  5000
  41 #define HugeThresholdClassCount   40000
  42 
  43 #define SET_ESTIMATED_SIZE(type, region)                              \
  44   Shared ##region## Size  = FLAG_IS_DEFAULT(Shared ##region## Size) ? \
  45     (uintx)(type ## SharedArchiveSize *  region ## RegionPercentage) : Shared ## region ## Size
  46 
  47 class FileMapInfo;
  48 
  49 class MetaspaceSharedStats VALUE_OBJ_CLASS_SPEC {
  50 public:
  51   MetaspaceSharedStats() {
  52     memset(this, 0, sizeof(*this));
  53   }
  54   CompactHashtableStats symbol;
  55 };
  56 
  57 // Class Data Sharing Support
  58 class MetaspaceShared : AllStatic {
  59 
  60   // CDS support
  61   static ReservedSpace* _shared_rs;
  62   static int _max_alignment;
  63   static MetaspaceSharedStats _stats;
  64   static bool _link_classes_made_progress;
  65   static bool _check_classes_made_progress;
  66   static bool _has_error_classes;
  67   static bool _archive_loading_failed;
  68  public:
  69   enum {
  70     vtbl_list_size         = 17,   // number of entries in the shared space vtable list.
  71     num_virtuals           = 200,  // maximum number of virtual functions
  72                                    // If virtual functions are added to Metadata,
  73                                    // this number needs to be increased.  Also,
  74                                    // SharedMiscCodeSize will need to be increased.
  75                                    // The following 2 sizes were based on
  76                                    // MetaspaceShared::generate_vtable_methods()
  77     vtbl_method_size       = 16,   // conservative size of the mov1 and jmp instructions
  78                                    // for the x64 platform
  79     vtbl_common_code_size  = (1*K) // conservative size of the "common_code" for the x64 platform
  80   };
  81 
  82   enum {
  83     min_ro_size = NOT_LP64(8*M) LP64_ONLY(9*M), // minimum ro and rw regions sizes based on dumping
  84     min_rw_size = NOT_LP64(7*M) LP64_ONLY(12*M) // of a shared archive using the default classlist
  85   };
  86 
  87   enum {
  88     ro = 0,  // read-only shared space in the heap
  89     rw = 1,  // read-write shared space in the heap
  90     md = 2,  // miscellaneous data for initializing tables, etc.
  91     mc = 3,  // miscellaneous code - vtable replacement.
  92     n_regions = 4
  93   };
  94 
  95   // Accessor functions to save shared space created for metadata, which has
  96   // extra space allocated at the end for miscellaneous data and code.
  97   static void set_max_alignment(int alignment) {
  98     CDS_ONLY(_max_alignment = alignment);
  99   }
 100 
 101   static int max_alignment() {
 102     CDS_ONLY(return _max_alignment);
 103     NOT_CDS(return 0);
 104   }
 105 
 106   static void prepare_for_dumping() NOT_CDS_RETURN;
 107   static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
 108   static int preload_and_dump(const char * class_list_path,
 109                               GrowableArray<Klass*>* class_promote_order,
 110                               TRAPS) NOT_CDS_RETURN_(0);
 111 
 112   static ReservedSpace* shared_rs() {
 113     CDS_ONLY(return _shared_rs);
 114     NOT_CDS(return NULL);
 115   }
 116 
 117   static void set_shared_rs(ReservedSpace* rs) {
 118     CDS_ONLY(_shared_rs = rs;)
 119   }
 120 
 121   static void set_archive_loading_failed() {
 122     _archive_loading_failed = true;
 123   }
 124   static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false);
 125   static void initialize_shared_spaces() NOT_CDS_RETURN;
 126 
 127   // Return true if given address is in the mapped shared space.
 128   static bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false);
 129 
 130   static void generate_vtable_methods(void** vtbl_list,
 131                                       void** vtable,
 132                                       char** md_top, char* md_end,
 133                                       char** mc_top, char* mc_end);
 134   static void serialize(SerializeClosure* sc);
 135 
 136   static MetaspaceSharedStats* stats() {
 137     return &_stats;
 138   }
 139 
 140   // JVM/TI RedefineClasses() support:
 141   // Remap the shared readonly space to shared readwrite, private if
 142   // sharing is enabled. Simply returns true if sharing is not enabled
 143   // or if the remapping has already been done by a prior call.
 144   static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
 145 
 146   static void print_shared_spaces();
 147 
 148   static bool try_link_class(InstanceKlass* ik, TRAPS);
 149   static void link_one_shared_class(Klass* obj, TRAPS);
 150   static void check_one_shared_class(Klass* obj);
 151   static void link_and_cleanup_shared_classes(TRAPS);
 152 
 153   static int count_class(const char* classlist_file);
 154   static void estimate_regions_size() NOT_CDS_RETURN;
 155 };
 156 #endif // SHARE_VM_MEMORY_METASPACE_SHARED_HPP