1 /*
   2  * Copyright (c) 2011, 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 #ifndef SHARE_VM_MEMORY_METASPACE_HPP
  25 #define SHARE_VM_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 "utilities/exceptions.hpp"
  32 
  33 // Metaspace
  34 //
  35 // Metaspaces are Arenas for the VM's metadata.
  36 // They are allocated one per class loader object, and one for the null
  37 // bootstrap class loader
  38 //
  39 //    block X ---+       +-------------------+
  40 //               |       |  Virtualspace     |
  41 //               |       |                   |
  42 //               |       |                   |
  43 //               |       |-------------------|
  44 //               |       || Chunk            |
  45 //               |       ||                  |
  46 //               |       ||----------        |
  47 //               +------>||| block 0 |       |
  48 //                       ||----------        |
  49 //                       ||| block 1 |       |
  50 //                       ||----------        |
  51 //                       ||                  |
  52 //                       |-------------------|
  53 //                       |                   |
  54 //                       |                   |
  55 //                       +-------------------+
  56 //
  57 
  58 class ClassLoaderData;
  59 class MetaspaceTracer;
  60 class MetaWord;
  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     AnonymousMetaspaceType = BootMetaspaceType + 1,
 107     ReflectionMetaspaceType = AnonymousMetaspaceType + 1,
 108     MetaspaceTypeCount
 109   };
 110 
 111  private:
 112 
 113   // Align up the word size to the allocation word size
 114   static size_t align_word_size_up(size_t);
 115 
 116   // Aligned size of the metaspace.
 117   static size_t _compressed_class_space_size;
 118 
 119   static size_t compressed_class_space_size() {
 120     return _compressed_class_space_size;
 121   }
 122 
 123   static void set_compressed_class_space_size(size_t size) {
 124     _compressed_class_space_size = size;
 125   }
 126 
 127   static size_t _first_chunk_word_size;
 128   static size_t _first_class_chunk_word_size;
 129 
 130   static size_t _commit_alignment;
 131   static size_t _reserve_alignment;
 132   DEBUG_ONLY(static bool   _frozen;)
 133 
 134   // Virtual Space lists for both classes and other metadata
 135   static metaspace::VirtualSpaceList* _space_list;
 136   static metaspace::VirtualSpaceList* _class_space_list;
 137 
 138   static metaspace::ChunkManager* _chunk_manager_metadata;
 139   static metaspace::ChunkManager* _chunk_manager_class;
 140 
 141   static const MetaspaceTracer* _tracer;
 142 
 143  public:
 144   static metaspace::VirtualSpaceList* space_list()       { return _space_list; }
 145   static metaspace::VirtualSpaceList* class_space_list() { return _class_space_list; }
 146   static metaspace::VirtualSpaceList* get_space_list(MetadataType mdtype) {
 147     assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 148     return mdtype == ClassType ? class_space_list() : space_list();
 149   }
 150 
 151   static metaspace::ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; }
 152   static metaspace::ChunkManager* chunk_manager_class()    { return _chunk_manager_class; }
 153   static metaspace::ChunkManager* get_chunk_manager(MetadataType mdtype) {
 154     assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 155     return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata();
 156   }
 157 
 158   // convenience function
 159   static metaspace::ChunkManager* get_chunk_manager(bool is_class) {
 160     return is_class ? chunk_manager_class() : chunk_manager_metadata();
 161   }
 162 
 163   static const MetaspaceTracer* tracer() { return _tracer; }
 164   static void freeze() {
 165     assert(DumpSharedSpaces, "sanity");
 166     DEBUG_ONLY(_frozen = true;)
 167   }
 168   static void assert_not_frozen() {
 169     assert(!_frozen, "sanity");
 170   }
 171 #ifdef _LP64
 172   static void allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base);
 173 #endif
 174 
 175  private:
 176 
 177 #ifdef _LP64
 178   static void set_narrow_klass_base_and_shift(address metaspace_base, address cds_base);
 179 
 180   // Returns true if can use CDS with metaspace allocated as specified address.
 181   static bool can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base);
 182 
 183   static void initialize_class_space(ReservedSpace rs);
 184 #endif
 185 
 186  public:
 187 
 188   static void ergo_initialize();
 189   static void global_initialize();
 190   static void post_initialize();
 191 
 192   static void verify_global_initialization();
 193 
 194   static size_t first_chunk_word_size() { return _first_chunk_word_size; }
 195   static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; }
 196 
 197   static size_t reserve_alignment()       { return _reserve_alignment; }
 198   static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; }
 199   static size_t commit_alignment()        { return _commit_alignment; }
 200   static size_t commit_alignment_words()  { return _commit_alignment / BytesPerWord; }
 201 
 202   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
 203                             MetaspaceObj::Type type, TRAPS);
 204 
 205   static bool contains(const void* ptr);
 206   static bool contains_non_shared(const void* ptr);
 207 
 208   // Free empty virtualspaces
 209   static void purge(MetadataType mdtype);
 210   static void purge();
 211 
 212   static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
 213                                    MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
 214 
 215   static const char* metadata_type_name(Metaspace::MetadataType mdtype);
 216 
 217   static void print_compressed_class_space(outputStream* st, const char* requested_addr = 0) NOT_LP64({});
 218 
 219   // Return TRUE only if UseCompressedClassPointers is True.
 220   static bool using_class_space() {
 221     return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
 222   }
 223 
 224   static bool is_class_space_allocation(MetadataType mdType) {
 225     return mdType == ClassType && using_class_space();
 226   }
 227 
 228 };
 229 
 230 // Manages the metaspace portion belonging to a class loader
 231 class ClassLoaderMetaspace : public CHeapObj<mtClass> {
 232   friend class CollectedHeap; // For expand_and_allocate()
 233   friend class ZCollectedHeap; // For expand_and_allocate()
 234   friend class ShenandoahHeap; // For expand_and_allocate()
 235   friend class Metaspace;
 236   friend class MetaspaceUtils;
 237   friend class metaspace::PrintCLDMetaspaceInfoClosure;
 238   friend class VM_CollectForMetadataAllocation; // For expand_and_allocate()
 239 
 240  private:
 241 
 242   void initialize(Mutex* lock, Metaspace::MetaspaceType type);
 243 
 244   // Initialize the first chunk for a Metaspace.  Used for
 245   // special cases such as the boot class loader, reflection
 246   // class loader and anonymous class loader.
 247   void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
 248   metaspace::Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
 249 
 250   const Metaspace::MetaspaceType _space_type;
 251   Mutex* const  _lock;
 252   metaspace::SpaceManager* _vsm;
 253   metaspace::SpaceManager* _class_vsm;
 254 
 255   metaspace::SpaceManager* vsm() const { return _vsm; }
 256   metaspace::SpaceManager* class_vsm() const { return _class_vsm; }
 257   metaspace::SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) {
 258     assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 259     return mdtype == Metaspace::ClassType ? class_vsm() : vsm();
 260   }
 261 
 262   Mutex* lock() const { return _lock; }
 263 
 264   MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype);
 265 
 266   size_t class_chunk_size(size_t word_size);
 267 
 268   // Adds to the given statistic object. Must be locked with CLD metaspace lock.
 269   void add_to_statistics_locked(metaspace::ClassLoaderMetaspaceStatistics* out) const;
 270 
 271   Metaspace::MetaspaceType space_type() const { return _space_type; }
 272 
 273  public:
 274 
 275   ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type);
 276   ~ClassLoaderMetaspace();
 277 
 278   // Allocate space for metadata of type mdtype. This is space
 279   // within a Metachunk and is used by
 280   //   allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
 281   MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype);
 282 
 283   size_t allocated_blocks_bytes() const;
 284   size_t allocated_chunks_bytes() const;
 285 
 286   void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
 287 
 288   void print_on(outputStream* st) const;
 289   // Debugging support
 290   void verify();
 291 
 292   // Adds to the given statistic object. Will lock with CLD metaspace lock.
 293   void add_to_statistics(metaspace::ClassLoaderMetaspaceStatistics* out) const;
 294 
 295 }; // ClassLoaderMetaspace
 296 
 297 class MetaspaceUtils : AllStatic {
 298 
 299   // Spacemanager updates running counters.
 300   friend class metaspace::SpaceManager;
 301 
 302   // Special access for error reporting (checks without locks).
 303   friend class oopDesc;
 304   friend class Klass;
 305 
 306   // Running counters for statistics concerning in-use chunks.
 307   // Note: capacity = used + free + waste + overhead. Note that we do not
 308   // count free and waste. Their sum can be deduces from the three other values.
 309   // For more details, one should call print_report() from within a safe point.
 310   static size_t _capacity_words [Metaspace:: MetadataTypeCount];
 311   static size_t _overhead_words [Metaspace:: MetadataTypeCount];
 312   static volatile size_t _used_words [Metaspace:: MetadataTypeCount];
 313 
 314   // Atomically decrement or increment in-use statistic counters
 315   static void dec_capacity(Metaspace::MetadataType mdtype, size_t words);
 316   static void inc_capacity(Metaspace::MetadataType mdtype, size_t words);
 317   static void dec_used(Metaspace::MetadataType mdtype, size_t words);
 318   static void inc_used(Metaspace::MetadataType mdtype, size_t words);
 319   static void dec_overhead(Metaspace::MetadataType mdtype, size_t words);
 320   static void inc_overhead(Metaspace::MetadataType mdtype, size_t words);
 321 
 322 
 323   // Getters for the in-use counters.
 324   static size_t capacity_words(Metaspace::MetadataType mdtype)        { return _capacity_words[mdtype]; }
 325   static size_t used_words(Metaspace::MetadataType mdtype)            { return _used_words[mdtype]; }
 326   static size_t overhead_words(Metaspace::MetadataType mdtype)        { return _overhead_words[mdtype]; }
 327 
 328   static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
 329 
 330   // Helper for print_xx_report.
 331   static void print_vs(outputStream* out, size_t scale);
 332 
 333   // Utils to check if a pointer or range is part of a committed metaspace region
 334   // without acquiring any locks.
 335   static metaspace::VirtualSpaceNode* find_enclosing_virtual_space(const void* p);
 336   static bool is_in_committed(const void* p);
 337   static bool is_range_in_committed(const void* from, const void* to);
 338 
 339 public:
 340 
 341   // Collect used metaspace statistics. This involves walking the CLDG. The resulting
 342   // output will be the accumulated values for all live metaspaces.
 343   // Note: method does not do any locking.
 344   static void collect_statistics(metaspace::ClassLoaderMetaspaceStatistics* out);
 345 
 346   // Used by MetaspaceCounters
 347   static size_t free_chunks_total_words();
 348   static size_t free_chunks_total_bytes();
 349   static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
 350 
 351   static size_t capacity_words() {
 352     return capacity_words(Metaspace::NonClassType) +
 353            capacity_words(Metaspace::ClassType);
 354   }
 355   static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
 356     return capacity_words(mdtype) * BytesPerWord;
 357   }
 358   static size_t capacity_bytes() {
 359     return capacity_words() * BytesPerWord;
 360   }
 361 
 362   static size_t used_words() {
 363     return used_words(Metaspace::NonClassType) +
 364            used_words(Metaspace::ClassType);
 365   }
 366   static size_t used_bytes(Metaspace::MetadataType mdtype) {
 367     return used_words(mdtype) * BytesPerWord;
 368   }
 369   static size_t used_bytes() {
 370     return used_words() * BytesPerWord;
 371   }
 372 
 373   // Space committed but yet unclaimed by any class loader.
 374   static size_t free_in_vs_bytes();
 375   static size_t free_in_vs_bytes(Metaspace::MetadataType mdtype);
 376 
 377   static size_t reserved_bytes(Metaspace::MetadataType mdtype);
 378   static size_t reserved_bytes() {
 379     return reserved_bytes(Metaspace::ClassType) +
 380            reserved_bytes(Metaspace::NonClassType);
 381   }
 382 
 383   static size_t committed_bytes(Metaspace::MetadataType mdtype);
 384   static size_t committed_bytes() {
 385     return committed_bytes(Metaspace::ClassType) +
 386            committed_bytes(Metaspace::NonClassType);
 387   }
 388 
 389   static size_t min_chunk_size_words();
 390   static size_t min_chunk_size_bytes() {
 391     return min_chunk_size_words() * BytesPerWord;
 392   }
 393 
 394   // Flags for print_report().
 395   enum ReportFlag {
 396     // Show usage by class loader.
 397     rf_show_loaders                 = (1 << 0),
 398     // Breaks report down by chunk type (small, medium, ...).
 399     rf_break_down_by_chunktype      = (1 << 1),
 400     // Breaks report down by space type (anonymous, reflection, ...).
 401     rf_break_down_by_spacetype      = (1 << 2),
 402     // Print details about the underlying virtual spaces.
 403     rf_show_vslist                  = (1 << 3),
 404     // Print metaspace map.
 405     rf_show_vsmap                   = (1 << 4),
 406     // If show_loaders: show loaded classes for each loader.
 407     rf_show_classes                 = (1 << 5)
 408   };
 409 
 410   // This will print out a basic metaspace usage report but
 411   // unlike print_report() is guaranteed not to lock or to walk the CLDG.
 412   static void print_basic_report(outputStream* st, size_t scale);
 413 
 414   // Prints a report about the current metaspace state.
 415   // Optional parts can be enabled via flags.
 416   // Function will walk the CLDG and will lock the expand lock; if that is not
 417   // convenient, use print_basic_report() instead.
 418   static void print_report(outputStream* out, size_t scale = 0, int flags = 0);
 419 
 420   static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
 421   static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
 422 
 423   // Print change in used metadata.
 424   static void print_metaspace_change(size_t prev_metadata_used);
 425   static void print_on(outputStream * out);
 426 
 427   // Prints an ASCII representation of the given space.
 428   static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype);
 429 
 430   static void dump(outputStream* out);
 431   static void verify_free_chunks();
 432   // Check internal counters (capacity, used).
 433   static void verify_metrics();
 434 };
 435 
 436 // Metaspace are deallocated when their class loader are GC'ed.
 437 // This class implements a policy for inducing GC's to recover
 438 // Metaspaces.
 439 
 440 class MetaspaceGC : AllStatic {
 441 
 442   // The current high-water-mark for inducing a GC.
 443   // When committed memory of all metaspaces reaches this value,
 444   // a GC is induced and the value is increased. Size is in bytes.
 445   static volatile size_t _capacity_until_GC;
 446 
 447   // For a CMS collection, signal that a concurrent collection should
 448   // be started.
 449   static bool _should_concurrent_collect;
 450 
 451   static uint _shrink_factor;
 452 
 453   static size_t shrink_factor() { return _shrink_factor; }
 454   void set_shrink_factor(uint v) { _shrink_factor = v; }
 455 
 456  public:
 457 
 458   static void initialize();
 459   static void post_initialize();
 460 
 461   static size_t capacity_until_GC();
 462   static bool inc_capacity_until_GC(size_t v,
 463                                     size_t* new_cap_until_GC = NULL,
 464                                     size_t* old_cap_until_GC = NULL,
 465                                     bool* can_retry = NULL);
 466   static size_t dec_capacity_until_GC(size_t v);
 467 
 468   static bool should_concurrent_collect() { return _should_concurrent_collect; }
 469   static void set_should_concurrent_collect(bool v) {
 470     _should_concurrent_collect = v;
 471   }
 472 
 473   // The amount to increase the high-water-mark (_capacity_until_GC)
 474   static size_t delta_capacity_until_GC(size_t bytes);
 475 
 476   // Tells if we have can expand metaspace without hitting set limits.
 477   static bool can_expand(size_t words, bool is_class);
 478 
 479   // Returns amount that we can expand without hitting a GC,
 480   // measured in words.
 481   static size_t allowed_expansion();
 482 
 483   // Calculate the new high-water mark at which to induce
 484   // a GC.
 485   static void compute_new_size();
 486 };
 487 
 488 #endif // SHARE_VM_MEMORY_METASPACE_HPP