1 /*
   2  * Copyright (c) 2011, 2019, 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_MEMORY_METASPACE_HPP
  25 #define SHARE_MEMORY_METASPACE_HPP
  26 
  27 #include "memory/allocation.hpp"
  28 #include "memory/memRegion.hpp"
  29 #include "memory/metaspaceChunkFreeListSummary.hpp"
  30 #include "memory/virtualspace.hpp"
  31 #include "memory/metaspace/metaspaceSizesSnapshot.hpp"
  32 #include "utilities/exceptions.hpp"
  33 
  34 // Metaspace
  35 //
  36 // Metaspaces are Arenas for the VM's metadata.
  37 // They are allocated one per class loader object, and one for the null
  38 // bootstrap class loader
  39 //
  40 //    block X ---+       +-------------------+
  41 //               |       |  Virtualspace     |
  42 //               |       |                   |
  43 //               |       |                   |
  44 //               |       |-------------------|
  45 //               |       || Chunk            |
  46 //               |       ||                  |
  47 //               |       ||----------        |
  48 //               +------>||| block 0 |       |
  49 //                       ||----------        |
  50 //                       ||| block 1 |       |
  51 //                       ||----------        |
  52 //                       ||                  |
  53 //                       |-------------------|
  54 //                       |                   |
  55 //                       |                   |
  56 //                       +-------------------+
  57 //
  58 
  59 class ClassLoaderData;
  60 class MetaspaceTracer;
  61 class Mutex;
  62 class outputStream;
  63 
  64 class CollectedHeap;
  65 
  66 namespace metaspace {
  67   class ChunkManager;
  68   class ClassLoaderMetaspaceStatistics;
  69   class Metablock;
  70   class Metachunk;
  71   class PrintCLDMetaspaceInfoClosure;
  72   class SpaceManager;
  73   class VirtualSpaceList;
  74   class VirtualSpaceNode;
  75 }
  76 
  77 // Metaspaces each have a  SpaceManager and allocations
  78 // are done by the SpaceManager.  Allocations are done
  79 // out of the current Metachunk.  When the current Metachunk
  80 // is exhausted, the SpaceManager gets a new one from
  81 // the current VirtualSpace.  When the VirtualSpace is exhausted
  82 // the SpaceManager gets a new one.  The SpaceManager
  83 // also manages freelists of available Chunks.
  84 //
  85 // Currently the space manager maintains the list of
  86 // virtual spaces and the list of chunks in use.  Its
  87 // allocate() method returns a block for use as a
  88 // quantum of metadata.
  89 
  90 // Namespace for important central static functions
  91 // (auxiliary stuff goes into MetaspaceUtils)
  92 class Metaspace : public AllStatic {
  93 
  94   friend class MetaspaceShared;
  95 
  96  public:
  97   enum MetadataType {
  98     ClassType,
  99     NonClassType,
 100     MetadataTypeCount
 101   };
 102   enum MetaspaceType {
 103     ZeroMetaspaceType = 0,
 104     StandardMetaspaceType = ZeroMetaspaceType,
 105     BootMetaspaceType = StandardMetaspaceType + 1,
 106     UnsafeAnonymousMetaspaceType = BootMetaspaceType + 1,
 107     ReflectionMetaspaceType = UnsafeAnonymousMetaspaceType + 1,
 108     MetaspaceTypeCount
 109   };
 110 
 111  private:
 112 
 113   // Aligned size of the metaspace.
 114   static size_t _compressed_class_space_size;
 115 
 116   static size_t compressed_class_space_size()                 { return _compressed_class_space_size; }
 117   static void   set_compressed_class_space_size(size_t size)  { _compressed_class_space_size = size; }
 118 
 119   static size_t _commit_alignment;
 120   static size_t _reserve_alignment;
 121   DEBUG_ONLY(static bool   _frozen;)
 122 
 123   // Virtual Space lists for both classes and other metadata
 124   static metaspace::VirtualSpaceList* _space_list;
 125   static metaspace::VirtualSpaceList* _class_space_list;
 126 
 127   static metaspace::ChunkManager* _chunk_manager_metadata;
 128   static metaspace::ChunkManager* _chunk_manager_class;
 129 
 130   static const MetaspaceTracer* _tracer;
 131 
 132   static bool _initialized;
 133 
 134   static metaspace::VirtualSpaceList* space_list()       { return _space_list; }
 135   static metaspace::VirtualSpaceList* class_space_list() { return _class_space_list; }
 136 
 137   static metaspace::VirtualSpaceList* get_space_list(MetadataType mdtype) {
 138     assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 139     return mdtype == ClassType ? class_space_list() : space_list();
 140   }
 141 
 142 public:
 143 
 144 
 145   static metaspace::ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; }
 146   static metaspace::ChunkManager* chunk_manager_class()    { return _chunk_manager_class; }
 147   static metaspace::ChunkManager* get_chunk_manager(MetadataType mdtype) {
 148     assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 149     return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata();
 150   }
 151 
 152   // convenience function
 153   static metaspace::ChunkManager* get_chunk_manager(bool is_class) {
 154     return is_class ? chunk_manager_class() : chunk_manager_metadata();
 155   }
 156 
 157   static const MetaspaceTracer* tracer() { return _tracer; }
 158   static void freeze() {
 159     assert(DumpSharedSpaces, "sanity");
 160     DEBUG_ONLY(_frozen = true;)
 161   }
 162   static void assert_not_frozen() {
 163     assert(!_frozen, "sanity");
 164   }
 165 #ifdef _LP64
 166   static void allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base);
 167 #endif
 168 
 169  private:
 170 
 171 #ifdef _LP64
 172   static void set_narrow_klass_base_and_shift(address metaspace_base, address cds_base);
 173 
 174   // Returns true if can use CDS with metaspace allocated as specified address.
 175   static bool can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base);
 176 
 177   static void initialize_class_space(ReservedSpace rs);
 178 #endif
 179 
 180  public:
 181 
 182   static void ergo_initialize();
 183   static void global_initialize();
 184   static void post_initialize();
 185 
 186   static void verify_global_initialization();
 187 
 188   // The alignment at which Metaspace mappings are reserved.
 189   static size_t reserve_alignment()       { return _reserve_alignment; }
 190   static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; }
 191 
 192   // The granularity at which Metaspace is committed and uncommitted.
 193   static size_t commit_alignment()        { return _commit_alignment; }
 194   static size_t commit_words()            { return _commit_alignment / BytesPerWord; }
 195 
 196   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
 197                             MetaspaceObj::Type type, TRAPS);
 198 
 199   static bool contains(const void* ptr);
 200   static bool contains_non_shared(const void* ptr);
 201 
 202   // Free empty virtualspaces
 203   static void purge(MetadataType mdtype);
 204 
 205   static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
 206                                    MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
 207 
 208   static const char* metadata_type_name(Metaspace::MetadataType mdtype);
 209 
 210   static void print_compressed_class_space(outputStream* st, const char* requested_addr = 0) NOT_LP64({});
 211 
 212   // Return TRUE only if UseCompressedClassPointers is True.
 213   static bool using_class_space() {
 214     return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
 215   }
 216 
 217   static bool is_class_space_allocation(MetadataType mdType) {
 218     return mdType == ClassType && using_class_space();
 219   }
 220 
 221   static bool initialized() { return _initialized; }
 222 
 223 };
 224 
 225 class MetaspaceUtils : AllStatic {
 226 
 227   // Spacemanager updates running counters.
 228   friend class metaspace::SpaceManager;
 229 
 230   // Special access for error reporting (checks without locks).
 231   friend class oopDesc;
 232   friend class Klass;
 233 
 234   // Running counters for statistics concerning in-use chunks.
 235   // Note: capacity = used + free + waste + overhead. Note that we do not
 236   // count free and waste. Their sum can be deduces from the three other values.
 237   // For more details, one should call print_report() from within a safe point.
 238   static size_t _capacity_words [Metaspace:: MetadataTypeCount];
 239   static size_t _overhead_words [Metaspace:: MetadataTypeCount];
 240   static volatile size_t _used_words [Metaspace:: MetadataTypeCount];
 241 
 242   // Atomically decrement or increment in-use statistic counters
 243   static void dec_capacity(Metaspace::MetadataType mdtype, size_t words);
 244   static void inc_capacity(Metaspace::MetadataType mdtype, size_t words);
 245   static void dec_used(Metaspace::MetadataType mdtype, size_t words);
 246   static void inc_used(Metaspace::MetadataType mdtype, size_t words);
 247   static void dec_overhead(Metaspace::MetadataType mdtype, size_t words);
 248   static void inc_overhead(Metaspace::MetadataType mdtype, size_t words);
 249 
 250 
 251   // Getters for the in-use counters.
 252   static size_t capacity_words(Metaspace::MetadataType mdtype)        { return _capacity_words[mdtype]; }
 253   static size_t used_words(Metaspace::MetadataType mdtype)            { return _used_words[mdtype]; }
 254   static size_t overhead_words(Metaspace::MetadataType mdtype)        { return _overhead_words[mdtype]; }
 255 
 256   static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
 257 
 258 
 259 public:
 260 
 261   // Collect used metaspace statistics. This involves walking the CLDG. The resulting
 262   // output will be the accumulated values for all live metaspaces.
 263   // Note: method does not do any locking.
 264   static void collect_statistics(metaspace::ClassLoaderMetaspaceStatistics* out);
 265 
 266   // Used by MetaspaceCounters
 267   static size_t free_chunks_total_words();
 268   static size_t free_chunks_total_bytes();
 269   static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
 270 
 271   static size_t capacity_words() {
 272     return capacity_words(Metaspace::NonClassType) +
 273            capacity_words(Metaspace::ClassType);
 274   }
 275   static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
 276     return capacity_words(mdtype) * BytesPerWord;
 277   }
 278   static size_t capacity_bytes() {
 279     return capacity_words() * BytesPerWord;
 280   }
 281 
 282   static size_t used_words() {
 283     return used_words(Metaspace::NonClassType) +
 284            used_words(Metaspace::ClassType);
 285   }
 286   static size_t used_bytes(Metaspace::MetadataType mdtype) {
 287     return used_words(mdtype) * BytesPerWord;
 288   }
 289   static size_t used_bytes() {
 290     return used_words() * BytesPerWord;
 291   }
 292 
 293   // Space committed but yet unclaimed by any class loader.
 294   static size_t free_in_vs_bytes();
 295   static size_t free_in_vs_bytes(Metaspace::MetadataType mdtype);
 296 
 297   static size_t reserved_bytes(Metaspace::MetadataType mdtype);
 298   static size_t reserved_bytes() {
 299     return reserved_bytes(Metaspace::ClassType) +
 300            reserved_bytes(Metaspace::NonClassType);
 301   }
 302 
 303   static size_t committed_bytes(Metaspace::MetadataType mdtype);
 304   static size_t committed_bytes() {
 305     return committed_bytes(Metaspace::ClassType) +
 306            committed_bytes(Metaspace::NonClassType);
 307   }
 308 
 309   static size_t min_chunk_size_words();
 310 
 311   static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
 312   static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
 313 
 314   // Log change in used metadata.
 315   static void print_metaspace_change(const metaspace::MetaspaceSizesSnapshot& pre_meta_values);
 316   static void print_on(outputStream * out);
 317 
 318   // Prints an ASCII representation of the given space.
 319   static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype);
 320 
 321   static void dump(outputStream* out);
 322   static void verify_free_chunks();
 323   // Check internal counters (capacity, used).
 324   static void verify_metrics();
 325 };
 326 
 327 #endif // SHARE_MEMORY_METASPACE_HPP