1 /*
   2  * Copyright (c) 2011, 2017, 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 ChunkManager;
  59 class ClassLoaderData;
  60 class ClassLoaderMetaspaceStatistics;
  61 class Metablock;
  62 class Metachunk;
  63 class MetaspaceTracer;
  64 class MetaWord;
  65 class Mutex;
  66 class outputStream;
  67 class PrintCLDMetaspaceInfoClosure;
  68 class SpaceManager;
  69 class VirtualSpaceList;
  70 class CollectedHeap;
  71 
  72 // Metaspaces each have a  SpaceManager and allocations
  73 // are done by the SpaceManager.  Allocations are done
  74 // out of the current Metachunk.  When the current Metachunk
  75 // is exhausted, the SpaceManager gets a new one from
  76 // the current VirtualSpace.  When the VirtualSpace is exhausted
  77 // the SpaceManager gets a new one.  The SpaceManager
  78 // also manages freelists of available Chunks.
  79 //
  80 // Currently the space manager maintains the list of
  81 // virtual spaces and the list of chunks in use.  Its
  82 // allocate() method returns a block for use as a
  83 // quantum of metadata.
  84 
  85 // Namespace for important central static functions
  86 // (auxiliary stuff goes into MetaspaceUtils)
  87 class Metaspace : public AllStatic {
  88 
  89   friend class MetaspaceShared;
  90 
  91  public:
  92   enum MetadataType {
  93     ClassType,
  94     NonClassType,
  95     MetadataTypeCount
  96   };
  97   enum MetaspaceType {
  98     ZeroMetaspaceType = 0,
  99     //...
 100     StandardMetaspaceType = ZeroMetaspaceType,
 101     BootMetaspaceType = StandardMetaspaceType + 1,
 102     AnonymousMetaspaceType = BootMetaspaceType + 1,
 103     ReflectionMetaspaceType = AnonymousMetaspaceType + 1,
 104     //..
 105     MetaspaceTypeCount
 106   };
 107 
 108  private:
 109 
 110   // Align up the word size to the allocation word size
 111   static size_t align_word_size_up(size_t);
 112 
 113   // Aligned size of the metaspace.
 114   static size_t _compressed_class_space_size;
 115 
 116   static size_t compressed_class_space_size() {
 117     return _compressed_class_space_size;
 118   }
 119 
 120   static void set_compressed_class_space_size(size_t size) {
 121     _compressed_class_space_size = size;
 122   }
 123 
 124   static size_t _first_chunk_word_size;
 125   static size_t _first_class_chunk_word_size;
 126 
 127   static size_t _commit_alignment;
 128   static size_t _reserve_alignment;
 129   DEBUG_ONLY(static bool   _frozen;)
 130 
 131   // Virtual Space lists for both classes and other metadata
 132   static VirtualSpaceList* _space_list;
 133   static VirtualSpaceList* _class_space_list;
 134 
 135   static ChunkManager* _chunk_manager_metadata;
 136   static ChunkManager* _chunk_manager_class;
 137 
 138   static const MetaspaceTracer* _tracer;
 139 
 140  public:
 141   static VirtualSpaceList* space_list()       { return _space_list; }
 142   static VirtualSpaceList* class_space_list() { return _class_space_list; }
 143   static VirtualSpaceList* get_space_list(MetadataType mdtype) {
 144     assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 145     return mdtype == ClassType ? class_space_list() : space_list();
 146   }
 147 
 148   static ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; }
 149   static ChunkManager* chunk_manager_class()    { return _chunk_manager_class; }
 150   static ChunkManager* get_chunk_manager(MetadataType mdtype) {
 151     assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 152     return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata();
 153   }
 154 
 155   // convenience function
 156   static ChunkManager* get_chunk_manager(bool is_class) {
 157     return is_class ? chunk_manager_class() : chunk_manager_metadata();
 158   }
 159 
 160   static const MetaspaceTracer* tracer() { return _tracer; }
 161   static void freeze() {
 162     assert(DumpSharedSpaces, "sanity");
 163     DEBUG_ONLY(_frozen = true;)
 164   }
 165   static void assert_not_frozen() {
 166     assert(!_frozen, "sanity");
 167   }
 168 #ifdef _LP64
 169   static void allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base);
 170 #endif
 171 
 172  private:
 173 
 174 #ifdef _LP64
 175   static void set_narrow_klass_base_and_shift(address metaspace_base, address cds_base);
 176 
 177   // Returns true if can use CDS with metaspace allocated as specified address.
 178   static bool can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base);
 179 
 180   static void initialize_class_space(ReservedSpace rs);
 181 #endif
 182 
 183  public:
 184 
 185   static void ergo_initialize();
 186   static void global_initialize();
 187   static void post_initialize();
 188 
 189   static void verify_global_initialization();
 190 
 191   static size_t first_chunk_word_size() { return _first_chunk_word_size; }
 192   static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; }
 193 
 194   static size_t reserve_alignment()       { return _reserve_alignment; }
 195   static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; }
 196   static size_t commit_alignment()        { return _commit_alignment; }
 197   static size_t commit_alignment_words()  { return _commit_alignment / BytesPerWord; }
 198 
 199   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
 200                             MetaspaceObj::Type type, TRAPS);
 201   void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
 202 
 203   static bool contains(const void* ptr);
 204   static bool contains_non_shared(const void* ptr);
 205 
 206   // Free empty virtualspaces
 207   static void purge(MetadataType mdtype);
 208   static void purge();
 209 
 210   static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
 211                                    MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
 212 
 213   static const char* metadata_type_name(Metaspace::MetadataType mdtype);
 214 
 215   static void print_compressed_class_space(outputStream* st, const char* requested_addr = 0) NOT_LP64({});
 216 
 217   // Return TRUE only if UseCompressedClassPointers is True.
 218   static bool using_class_space() {
 219     return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
 220   }
 221 
 222   static bool is_class_space_allocation(MetadataType mdType) {
 223     return mdType == ClassType && using_class_space();
 224   }
 225 
 226 };
 227 
 228 // Manages the metaspace portion belonging to a class loader
 229 class ClassLoaderMetaspace : public CHeapObj<mtClass> {
 230   friend class CollectedHeap; // For expand_and_allocate()
 231   friend class Metaspace;
 232   friend class MetaspaceUtils;
 233   friend class PrintCLDMetaspaceInfoClosure;
 234   friend class VM_CollectForMetadataAllocation; // For expand_and_allocate()
 235 
 236  private:
 237 
 238   void initialize(Mutex* lock, Metaspace::MetaspaceType type);
 239 
 240   // Initialize the first chunk for a Metaspace.  Used for
 241   // special cases such as the boot class loader, reflection
 242   // class loader and anonymous class loader.
 243   void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
 244   Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
 245 
 246   const Metaspace::MetaspaceType _space_type;
 247   Mutex* const  _lock;
 248   SpaceManager* _vsm;
 249   SpaceManager* _class_vsm;
 250 
 251   SpaceManager* vsm() const { return _vsm; }
 252   SpaceManager* class_vsm() const { return _class_vsm; }
 253   SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) {
 254     assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
 255     return mdtype == Metaspace::ClassType ? class_vsm() : vsm();
 256   }
 257 
 258   Mutex* lock() const { return _lock; }
 259 
 260   MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype);
 261 
 262   size_t class_chunk_size(size_t word_size);
 263 
 264   // Adds to the given statistic object. Must be locked with CLD metaspace lock.
 265   void add_to_statistics_locked(ClassLoaderMetaspaceStatistics* out) const;
 266 
 267  public:
 268 
 269   ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type);
 270   ~ClassLoaderMetaspace();
 271 
 272   Metaspace::MetaspaceType space_type() const { return _space_type; }
 273 
 274   // Allocate space for metadata of type mdtype. This is space
 275   // within a Metachunk and is used by
 276   //   allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
 277   MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype);
 278 
 279   size_t allocated_blocks_bytes() const;
 280   size_t allocated_chunks_bytes() const;
 281 
 282   void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
 283 
 284   void print_on(outputStream* st) const;
 285   // Debugging support
 286   void verify();
 287 
 288   // Adds to the given statistic object. Will lock with CLD metaspace lock.
 289   void add_to_statistics(ClassLoaderMetaspaceStatistics* out) const;
 290 
 291 }; // ClassLoaderMetaspace
 292 
 293 class MetaspaceUtils : AllStatic {
 294   static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
 295 
 296   // Running counters for statistics concerning in-use chunks.
 297   enum StatType {
 298     stat_overhead = 0,
 299     stat_capacity,
 300     stat_used,
 301     stat_free,
 302     stat_waste,
 303     stat_num_values
 304   };
 305   volatile size_t _in_use_stats[stat_num_values][Metaspace::MetadataTypeCount];
 306 
 307   template <StatType e> static void inc_stat_value(Metaspace::MetadataType mdtype, size_t words);
 308   template <StatType e> static void dec_stat_value(Metaspace::MetadataType mdtype, size_t words);
 309   template <StatType e> static size_t get_stat_value(Metaspace::MetadataType mdtype) const;
 310 
 311 public:
 312 
 313   // Collect used metaspace statistics. This involves walking the CLDG. The resulting
 314   // output will be the accumulated values for all live metaspaces.
 315   // Note: method does not do any locking.
 316   static void collect_statistics(ClassLoaderMetaspaceStatistics* out);
 317 
 318   // Atomically decrement or increment in-use statistic counters.
 319   static void dec_capacity(Metaspace::MetadataType mdtype, size_t words)    { dec_stat_value<stat_capacity>(mdtype, words); }
 320   static void inc_capacity(Metaspace::MetadataType mdtype, size_t words)    { inc_stat_value<stat_capacity>(mdtype, words); }
 321   static size_t capacity_words(Metaspace::MetadataType mdtype) const        { return get_stat_value<stat_capacity>(mdtype); }
 322 
 323   static void dec_used(Metaspace::MetadataType mdtype, size_t words)        { dec_stat_value<stat_used>(mdtype, words); }
 324   static void inc_used(Metaspace::MetadataType mdtype, size_t words)        { inc_stat_value<stat_used>(mdtype, words); }
 325   static size_t used_words(Metaspace::MetadataType mdtype) const            { return get_stat_value<stat_used>(mdtype); }
 326 
 327   static void dec_overhead(Metaspace::MetadataType mdtype, size_t words)    { dec_stat_value<stat_overhead>(mdtype, words); }
 328   static void inc_overhead(Metaspace::MetadataType mdtype, size_t words)    { inc_stat_value<stat_overhead>(mdtype, words); }
 329   static size_t overhead_words(Metaspace::MetadataType mdtype) const        { return get_stat_value<stat_overhead>(mdtype); }
 330 
 331   static void dec_free(Metaspace::MetadataType mdtype, size_t words)        { dec_stat_value<stat_free>(mdtype, words); }
 332   static void inc_free(Metaspace::MetadataType mdtype, size_t words)        { inc_stat_value<stat_free>(mdtype, words); }
 333   static size_t free_words(Metaspace::MetadataType mdtype) const            { return get_stat_value<stat_free>(mdtype); }
 334 
 335   static void dec_waste(Metaspace::MetadataType mdtype, size_t words)       { dec_stat_value<stat_waste>(mdtype, words); }
 336   static void inc_waste(Metaspace::MetadataType mdtype, size_t words)       { inc_stat_value<stat_waste>(mdtype, words); }
 337   static size_t waste_words(Metaspace::MetadataType mdtype) const           { return get_stat_value<stat_waste>(mdtype); }
 338 
 339   // Used by MetaspaceCounters
 340   static size_t free_chunks_total_words();
 341   static size_t free_chunks_total_bytes();
 342   static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
 343 
 344   static size_t capacity_words() {
 345     return capacity_words(Metaspace::NonClassType) +
 346            capacity_words(Metaspace::ClassType);
 347   }
 348   static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
 349     return capacity_words(mdtype) * BytesPerWord;
 350   }
 351   static size_t capacity_bytes() {
 352     return capacity_words() * BytesPerWord;
 353   }
 354 
 355   static size_t used_words() {
 356     return used_words(Metaspace::NonClassType) +
 357            used_words(Metaspace::ClassType);
 358   }
 359   static size_t used_bytes(Metaspace::MetadataType mdtype) {
 360     return used_words(mdtype) * BytesPerWord;
 361   }
 362   static size_t used_bytes() {
 363     return used_words() * BytesPerWord;
 364   }
 365 
 366   static size_t free_bytes();
 367   static size_t free_bytes(Metaspace::MetadataType mdtype);
 368 
 369   static size_t reserved_bytes(Metaspace::MetadataType mdtype);
 370   static size_t reserved_bytes() {
 371     return reserved_bytes(Metaspace::ClassType) +
 372            reserved_bytes(Metaspace::NonClassType);
 373   }
 374 
 375   static size_t committed_bytes(Metaspace::MetadataType mdtype);
 376   static size_t committed_bytes() {
 377     return committed_bytes(Metaspace::ClassType) +
 378            committed_bytes(Metaspace::NonClassType);
 379   }
 380 
 381   static size_t min_chunk_size_words();
 382   static size_t min_chunk_size_bytes() {
 383     return min_chunk_size_words() * BytesPerWord;
 384   }
 385 
 386   // Flags for print_report().
 387   enum ReportFlag {
 388     // Breaks report down by class loader.
 389     rf_show_loaders                 = (1 << 0),
 390     // Breaks report down by chunk type (small, medium, ...).
 391     rf_break_down_by_chunktype      = (1 << 1),
 392     // Breaks report down by space type (anonymous, reflection, ...).
 393     rf_break_down_by_spacetype      = (1 << 2),
 394     // Print details about the underlying virtual spaces.
 395     rf_show_vslist                  = (1 << 3),
 396     // Print metaspace map.
 397     rf_show_vsmap                   = (1 << 4),
 398     // Print details about the free chunk list.
 399     rf_show_chunk_freelist          = (1 << 5),
 400   };
 401 
 402   // Prints a report about the current metaspace state. Form of report is defined
 403   // by flags (see ReportFlags). In its most basic form (flags == 0), a short
 404   // overview is printed.
 405   // Scale is one of 1, K, M, G - in which case numbers are printed as bytes,
 406   // KB, MB, GB, respectively - or 0, in which case the most suitable scale
 407   // is chosen dynamically.
 408   static void print_report(outputStream* out, size_t scale = 0, int flags = 0);
 409 
 410   static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
 411   static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
 412 
 413   // Print change in used metadata.
 414   static void print_metaspace_change(size_t prev_metadata_used);
 415   static void print_on(outputStream * out);
 416 
 417   // Prints an ASCII representation of the given space.
 418   static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype);
 419 
 420   static void dump(outputStream* out);
 421   static void verify_free_chunks();
 422   // Check internal counters (capacity, used).
 423   static void verify_metrics();
 424 };
 425 
 426 // Metaspace are deallocated when their class loader are GC'ed.
 427 // This class implements a policy for inducing GC's to recover
 428 // Metaspaces.
 429 
 430 class MetaspaceGC : AllStatic {
 431 
 432   // The current high-water-mark for inducing a GC.
 433   // When committed memory of all metaspaces reaches this value,
 434   // a GC is induced and the value is increased. Size is in bytes.
 435   static volatile intptr_t _capacity_until_GC;
 436 
 437   // For a CMS collection, signal that a concurrent collection should
 438   // be started.
 439   static bool _should_concurrent_collect;
 440 
 441   static uint _shrink_factor;
 442 
 443   static size_t shrink_factor() { return _shrink_factor; }
 444   void set_shrink_factor(uint v) { _shrink_factor = v; }
 445 
 446  public:
 447 
 448   static void initialize();
 449   static void post_initialize();
 450 
 451   static size_t capacity_until_GC();
 452   static bool inc_capacity_until_GC(size_t v,
 453                                     size_t* new_cap_until_GC = NULL,
 454                                     size_t* old_cap_until_GC = NULL);
 455   static size_t dec_capacity_until_GC(size_t v);
 456 
 457   static bool should_concurrent_collect() { return _should_concurrent_collect; }
 458   static void set_should_concurrent_collect(bool v) {
 459     _should_concurrent_collect = v;
 460   }
 461 
 462   // The amount to increase the high-water-mark (_capacity_until_GC)
 463   static size_t delta_capacity_until_GC(size_t bytes);
 464 
 465   // Tells if we have can expand metaspace without hitting set limits.
 466   static bool can_expand(size_t words, bool is_class);
 467 
 468   // Returns amount that we can expand without hitting a GC,
 469   // measured in words.
 470   static size_t allowed_expansion();
 471 
 472   // Calculate the new high-water mark at which to induce
 473   // a GC.
 474   static void compute_new_size();
 475 };
 476 
 477 #endif // SHARE_VM_MEMORY_METASPACE_HPP