1 /*
   2  * Copyright (c) 2011, 2013, 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 #include "precompiled.hpp"
  25 #include "gc_interface/collectedHeap.hpp"
  26 #include "memory/binaryTreeDictionary.hpp"
  27 #include "memory/freeList.hpp"
  28 #include "memory/collectorPolicy.hpp"
  29 #include "memory/filemap.hpp"
  30 #include "memory/freeList.hpp"
  31 #include "memory/metablock.hpp"
  32 #include "memory/metachunk.hpp"
  33 #include "memory/metaspace.hpp"
  34 #include "memory/metaspaceShared.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "memory/universe.hpp"
  37 #include "runtime/globals.hpp"
  38 #include "runtime/mutex.hpp"
  39 #include "runtime/orderAccess.hpp"
  40 #include "services/memTracker.hpp"
  41 #include "utilities/copy.hpp"
  42 #include "utilities/debug.hpp"
  43 
  44 typedef BinaryTreeDictionary<Metablock, FreeList> BlockTreeDictionary;
  45 typedef BinaryTreeDictionary<Metachunk, FreeList> ChunkTreeDictionary;
  46 // Define this macro to enable slow integrity checking of
  47 // the free chunk lists
  48 const bool metaspace_slow_verify = false;
  49 
  50 // Parameters for stress mode testing
  51 const uint metadata_deallocate_a_lot_block = 10;
  52 const uint metadata_deallocate_a_lock_chunk = 3;
  53 size_t const allocation_from_dictionary_limit = 64 * K;
  54 
  55 MetaWord* last_allocated = 0;
  56 
  57 // Used in declarations in SpaceManager and ChunkManager
  58 enum ChunkIndex {
  59   ZeroIndex = 0,
  60   SpecializedIndex = ZeroIndex,
  61   SmallIndex = SpecializedIndex + 1,
  62   MediumIndex = SmallIndex + 1,
  63   HumongousIndex = MediumIndex + 1,
  64   NumberOfFreeLists = 3,
  65   NumberOfInUseLists = 4
  66 };
  67 
  68 enum ChunkSizes {    // in words.
  69   ClassSpecializedChunk = 128,
  70   SpecializedChunk = 128,
  71   ClassSmallChunk = 256,
  72   SmallChunk = 512,
  73   ClassMediumChunk = 1 * K,
  74   MediumChunk = 8 * K,
  75   HumongousChunkGranularity = 8
  76 };
  77 
  78 static ChunkIndex next_chunk_index(ChunkIndex i) {
  79   assert(i < NumberOfInUseLists, "Out of bound");
  80   return (ChunkIndex) (i+1);
  81 }
  82 
  83 // Originally _capacity_until_GC was set to MetaspaceSize here but
  84 // the default MetaspaceSize before argument processing was being
  85 // used which was not the desired value.  See the code
  86 // in should_expand() to see how the initialization is handled
  87 // now.
  88 size_t MetaspaceGC::_capacity_until_GC = 0;
  89 bool MetaspaceGC::_expand_after_GC = false;
  90 uint MetaspaceGC::_shrink_factor = 0;
  91 bool MetaspaceGC::_should_concurrent_collect = false;
  92 
  93 // Blocks of space for metadata are allocated out of Metachunks.
  94 //
  95 // Metachunk are allocated out of MetadataVirtualspaces and once
  96 // allocated there is no explicit link between a Metachunk and
  97 // the MetadataVirtualspaces from which it was allocated.
  98 //
  99 // Each SpaceManager maintains a
 100 // list of the chunks it is using and the current chunk.  The current
 101 // chunk is the chunk from which allocations are done.  Space freed in
 102 // a chunk is placed on the free list of blocks (BlockFreelist) and
 103 // reused from there.
 104 
 105 typedef class FreeList<Metachunk> ChunkList;
 106 
 107 // Manages the global free lists of chunks.
 108 // Has three lists of free chunks, and a total size and
 109 // count that includes all three
 110 
 111 class ChunkManager VALUE_OBJ_CLASS_SPEC {
 112 
 113   // Free list of chunks of different sizes.
 114   //   SpecializedChunk
 115   //   SmallChunk
 116   //   MediumChunk
 117   //   HumongousChunk
 118   ChunkList _free_chunks[NumberOfFreeLists];
 119 
 120 
 121   //   HumongousChunk
 122   ChunkTreeDictionary _humongous_dictionary;
 123 
 124   // ChunkManager in all lists of this type
 125   size_t _free_chunks_total;
 126   size_t _free_chunks_count;
 127 
 128   void dec_free_chunks_total(size_t v) {
 129     assert(_free_chunks_count > 0 &&
 130              _free_chunks_total > 0,
 131              "About to go negative");
 132     Atomic::add_ptr(-1, &_free_chunks_count);
 133     jlong minus_v = (jlong) - (jlong) v;
 134     Atomic::add_ptr(minus_v, &_free_chunks_total);
 135   }
 136 
 137   // Debug support
 138 
 139   size_t sum_free_chunks();
 140   size_t sum_free_chunks_count();
 141 
 142   void locked_verify_free_chunks_total();
 143   void slow_locked_verify_free_chunks_total() {
 144     if (metaspace_slow_verify) {
 145       locked_verify_free_chunks_total();
 146     }
 147   }
 148   void locked_verify_free_chunks_count();
 149   void slow_locked_verify_free_chunks_count() {
 150     if (metaspace_slow_verify) {
 151       locked_verify_free_chunks_count();
 152     }
 153   }
 154   void verify_free_chunks_count();
 155 
 156  public:
 157 
 158   ChunkManager() : _free_chunks_total(0), _free_chunks_count(0) {}
 159 
 160   // add or delete (return) a chunk to the global freelist.
 161   Metachunk* chunk_freelist_allocate(size_t word_size);
 162   void chunk_freelist_deallocate(Metachunk* chunk);
 163 
 164   // Map a size to a list index assuming that there are lists
 165   // for special, small, medium, and humongous chunks.
 166   static ChunkIndex list_index(size_t size);
 167 
 168   // Remove the chunk from its freelist.  It is
 169   // expected to be on one of the _free_chunks[] lists.
 170   void remove_chunk(Metachunk* chunk);
 171 
 172   // Add the simple linked list of chunks to the freelist of chunks
 173   // of type index.
 174   void return_chunks(ChunkIndex index, Metachunk* chunks);
 175 
 176   // Total of the space in the free chunks list
 177   size_t free_chunks_total();
 178   size_t free_chunks_total_in_bytes();
 179 
 180   // Number of chunks in the free chunks list
 181   size_t free_chunks_count();
 182 
 183   void inc_free_chunks_total(size_t v, size_t count = 1) {
 184     Atomic::add_ptr(count, &_free_chunks_count);
 185     Atomic::add_ptr(v, &_free_chunks_total);
 186   }
 187   ChunkTreeDictionary* humongous_dictionary() {
 188     return &_humongous_dictionary;
 189   }
 190 
 191   ChunkList* free_chunks(ChunkIndex index);
 192 
 193   // Returns the list for the given chunk word size.
 194   ChunkList* find_free_chunks_list(size_t word_size);
 195 
 196   // Add and remove from a list by size.  Selects
 197   // list based on size of chunk.
 198   void free_chunks_put(Metachunk* chuck);
 199   Metachunk* free_chunks_get(size_t chunk_word_size);
 200 
 201   // Debug support
 202   void verify();
 203   void slow_verify() {
 204     if (metaspace_slow_verify) {
 205       verify();
 206     }
 207   }
 208   void locked_verify();
 209   void slow_locked_verify() {
 210     if (metaspace_slow_verify) {
 211       locked_verify();
 212     }
 213   }
 214   void verify_free_chunks_total();
 215 
 216   void locked_print_free_chunks(outputStream* st);
 217   void locked_print_sum_free_chunks(outputStream* st);
 218 
 219   void print_on(outputStream* st);
 220 };
 221 
 222 // Used to manage the free list of Metablocks (a block corresponds
 223 // to the allocation of a quantum of metadata).
 224 class BlockFreelist VALUE_OBJ_CLASS_SPEC {
 225   BlockTreeDictionary* _dictionary;
 226   static Metablock* initialize_free_chunk(MetaWord* p, size_t word_size);
 227 
 228   // Accessors
 229   BlockTreeDictionary* dictionary() const { return _dictionary; }
 230 
 231  public:
 232   BlockFreelist();
 233   ~BlockFreelist();
 234 
 235   // Get and return a block to the free list
 236   MetaWord* get_block(size_t word_size);
 237   void return_block(MetaWord* p, size_t word_size);
 238 
 239   size_t total_size() {
 240   if (dictionary() == NULL) {
 241     return 0;
 242   } else {
 243     return dictionary()->total_size();
 244   }
 245 }
 246 
 247   void print_on(outputStream* st) const;
 248 };
 249 
 250 class VirtualSpaceNode : public CHeapObj<mtClass> {
 251   friend class VirtualSpaceList;
 252 
 253   // Link to next VirtualSpaceNode
 254   VirtualSpaceNode* _next;
 255 
 256   // total in the VirtualSpace
 257   MemRegion _reserved;
 258   ReservedSpace _rs;
 259   VirtualSpace _virtual_space;
 260   MetaWord* _top;
 261   // count of chunks contained in this VirtualSpace
 262   uintx _container_count;
 263 
 264   // Convenience functions for logical bottom and end
 265   MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
 266   MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
 267 
 268   // Convenience functions to access the _virtual_space
 269   char* low()  const { return virtual_space()->low(); }
 270   char* high() const { return virtual_space()->high(); }
 271 
 272   // The first Metachunk will be allocated at the bottom of the
 273   // VirtualSpace
 274   Metachunk* first_chunk() { return (Metachunk*) bottom(); }
 275 
 276   void inc_container_count();
 277 #ifdef ASSERT
 278   uint container_count_slow();
 279 #endif
 280 
 281  public:
 282 
 283   VirtualSpaceNode(size_t byte_size);
 284   VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
 285   ~VirtualSpaceNode();
 286 
 287   // address of next available space in _virtual_space;
 288   // Accessors
 289   VirtualSpaceNode* next() { return _next; }
 290   void set_next(VirtualSpaceNode* v) { _next = v; }
 291 
 292   void set_reserved(MemRegion const v) { _reserved = v; }
 293   void set_top(MetaWord* v) { _top = v; }
 294 
 295   // Accessors
 296   MemRegion* reserved() { return &_reserved; }
 297   VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
 298 
 299   // Returns true if "word_size" is available in the VirtualSpace
 300   bool is_available(size_t word_size) { return _top + word_size <= end(); }
 301 
 302   MetaWord* top() const { return _top; }
 303   void inc_top(size_t word_size) { _top += word_size; }
 304 
 305   uintx container_count() { return _container_count; }
 306   void dec_container_count();
 307 #ifdef ASSERT
 308   void verify_container_count();
 309 #endif
 310 
 311   // used and capacity in this single entry in the list
 312   size_t used_words_in_vs() const;
 313   size_t capacity_words_in_vs() const;
 314   size_t free_words_in_vs() const;
 315 
 316   bool initialize();
 317 
 318   // get space from the virtual space
 319   Metachunk* take_from_committed(size_t chunk_word_size);
 320 
 321   // Allocate a chunk from the virtual space and return it.
 322   Metachunk* get_chunk_vs(size_t chunk_word_size);
 323   Metachunk* get_chunk_vs_with_expand(size_t chunk_word_size);
 324 
 325   // Expands/shrinks the committed space in a virtual space.  Delegates
 326   // to Virtualspace
 327   bool expand_by(size_t words, bool pre_touch = false);
 328   bool shrink_by(size_t words);
 329 
 330   // In preparation for deleting this node, remove all the chunks
 331   // in the node from any freelist.
 332   void purge(ChunkManager* chunk_manager);
 333 
 334 #ifdef ASSERT
 335   // Debug support
 336   static void verify_virtual_space_total();
 337   static void verify_virtual_space_count();
 338   void mangle();
 339 #endif
 340 
 341   void print_on(outputStream* st) const;
 342 };
 343 
 344   // byte_size is the size of the associated virtualspace.
 345 VirtualSpaceNode::VirtualSpaceNode(size_t byte_size) : _top(NULL), _next(NULL), _rs(0), _container_count(0) {
 346   // align up to vm allocation granularity
 347   byte_size = align_size_up(byte_size, os::vm_allocation_granularity());
 348 
 349   // This allocates memory with mmap.  For DumpSharedspaces, try to reserve
 350   // configurable address, generally at the top of the Java heap so other
 351   // memory addresses don't conflict.
 352   if (DumpSharedSpaces) {
 353     char* shared_base = (char*)SharedBaseAddress;
 354     _rs = ReservedSpace(byte_size, 0, false, shared_base, 0);
 355     if (_rs.is_reserved()) {
 356       assert(shared_base == 0 || _rs.base() == shared_base, "should match");
 357     } else {
 358       // Get a mmap region anywhere if the SharedBaseAddress fails.
 359       _rs = ReservedSpace(byte_size);
 360     }
 361     MetaspaceShared::set_shared_rs(&_rs);
 362   } else {
 363     _rs = ReservedSpace(byte_size);
 364   }
 365 
 366   MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
 367 }
 368 
 369 void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
 370   Metachunk* chunk = first_chunk();
 371   Metachunk* invalid_chunk = (Metachunk*) top();
 372   while (chunk < invalid_chunk ) {
 373     assert(chunk->is_free(), "Should be marked free");
 374       MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 375       chunk_manager->remove_chunk(chunk);
 376       assert(chunk->next() == NULL &&
 377              chunk->prev() == NULL,
 378              "Was not removed from its list");
 379       chunk = (Metachunk*) next;
 380   }
 381 }
 382 
 383 #ifdef ASSERT
 384 uint VirtualSpaceNode::container_count_slow() {
 385   uint count = 0;
 386   Metachunk* chunk = first_chunk();
 387   Metachunk* invalid_chunk = (Metachunk*) top();
 388   while (chunk < invalid_chunk ) {
 389     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 390     // Don't count the chunks on the free lists.  Those are
 391     // still part of the VirtualSpaceNode but not currently
 392     // counted.
 393     if (!chunk->is_free()) {
 394       count++;
 395     }
 396     chunk = (Metachunk*) next;
 397   }
 398   return count;
 399 }
 400 #endif
 401 
 402 // List of VirtualSpaces for metadata allocation.
 403 // It has a  _next link for singly linked list and a MemRegion
 404 // for total space in the VirtualSpace.
 405 class VirtualSpaceList : public CHeapObj<mtClass> {
 406   friend class VirtualSpaceNode;
 407 
 408   enum VirtualSpaceSizes {
 409     VirtualSpaceSize = 256 * K
 410   };
 411 
 412   // Global list of virtual spaces
 413   // Head of the list
 414   VirtualSpaceNode* _virtual_space_list;
 415   // virtual space currently being used for allocations
 416   VirtualSpaceNode* _current_virtual_space;
 417   // Free chunk list for all other metadata
 418   ChunkManager      _chunk_manager;
 419 
 420   // Can this virtual list allocate >1 spaces?  Also, used to determine
 421   // whether to allocate unlimited small chunks in this virtual space
 422   bool _is_class;
 423   bool can_grow() const { return !is_class() || !UseCompressedKlassPointers; }
 424 
 425   // Sum of space in all virtual spaces and number of virtual spaces
 426   size_t _virtual_space_total;
 427   size_t _virtual_space_count;
 428 
 429   ~VirtualSpaceList();
 430 
 431   VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
 432 
 433   void set_virtual_space_list(VirtualSpaceNode* v) {
 434     _virtual_space_list = v;
 435   }
 436   void set_current_virtual_space(VirtualSpaceNode* v) {
 437     _current_virtual_space = v;
 438   }
 439 
 440   void link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size);
 441 
 442   // Get another virtual space and add it to the list.  This
 443   // is typically prompted by a failed attempt to allocate a chunk
 444   // and is typically followed by the allocation of a chunk.
 445   bool grow_vs(size_t vs_word_size);
 446 
 447  public:
 448   VirtualSpaceList(size_t word_size);
 449   VirtualSpaceList(ReservedSpace rs);
 450 
 451   size_t free_bytes();
 452 
 453   Metachunk* get_new_chunk(size_t word_size,
 454                            size_t grow_chunks_by_words,
 455                            size_t medium_chunk_bunch);
 456 
 457   // Get the first chunk for a Metaspace.  Used for
 458   // special cases such as the boot class loader, reflection
 459   // class loader and anonymous class loader.
 460   Metachunk* get_initialization_chunk(size_t word_size, size_t chunk_bunch);
 461 
 462   VirtualSpaceNode* current_virtual_space() {
 463     return _current_virtual_space;
 464   }
 465 
 466   ChunkManager* chunk_manager() { return &_chunk_manager; }
 467   bool is_class() const { return _is_class; }
 468 
 469   // Allocate the first virtualspace.
 470   void initialize(size_t word_size);
 471 
 472   size_t virtual_space_total() { return _virtual_space_total; }
 473 
 474   void inc_virtual_space_total(size_t v);
 475   void dec_virtual_space_total(size_t v);
 476   void inc_virtual_space_count();
 477   void dec_virtual_space_count();
 478 
 479   // Unlink empty VirtualSpaceNodes and free it.
 480   void purge();
 481 
 482   // Used and capacity in the entire list of virtual spaces.
 483   // These are global values shared by all Metaspaces
 484   size_t capacity_words_sum();
 485   size_t capacity_bytes_sum() { return capacity_words_sum() * BytesPerWord; }
 486   size_t used_words_sum();
 487   size_t used_bytes_sum() { return used_words_sum() * BytesPerWord; }
 488 
 489   bool contains(const void *ptr);
 490 
 491   void print_on(outputStream* st) const;
 492 
 493   class VirtualSpaceListIterator : public StackObj {
 494     VirtualSpaceNode* _virtual_spaces;
 495    public:
 496     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
 497       _virtual_spaces(virtual_spaces) {}
 498 
 499     bool repeat() {
 500       return _virtual_spaces != NULL;
 501     }
 502 
 503     VirtualSpaceNode* get_next() {
 504       VirtualSpaceNode* result = _virtual_spaces;
 505       if (_virtual_spaces != NULL) {
 506         _virtual_spaces = _virtual_spaces->next();
 507       }
 508       return result;
 509     }
 510   };
 511 };
 512 
 513 class Metadebug : AllStatic {
 514   // Debugging support for Metaspaces
 515   static int _deallocate_block_a_lot_count;
 516   static int _deallocate_chunk_a_lot_count;
 517   static int _allocation_fail_alot_count;
 518 
 519  public:
 520   static int deallocate_block_a_lot_count() {
 521     return _deallocate_block_a_lot_count;
 522   }
 523   static void set_deallocate_block_a_lot_count(int v) {
 524     _deallocate_block_a_lot_count = v;
 525   }
 526   static void inc_deallocate_block_a_lot_count() {
 527     _deallocate_block_a_lot_count++;
 528   }
 529   static int deallocate_chunk_a_lot_count() {
 530     return _deallocate_chunk_a_lot_count;
 531   }
 532   static void reset_deallocate_chunk_a_lot_count() {
 533     _deallocate_chunk_a_lot_count = 1;
 534   }
 535   static void inc_deallocate_chunk_a_lot_count() {
 536     _deallocate_chunk_a_lot_count++;
 537   }
 538 
 539   static void init_allocation_fail_alot_count();
 540 #ifdef ASSERT
 541   static bool test_metadata_failure();
 542 #endif
 543 
 544   static void deallocate_chunk_a_lot(SpaceManager* sm,
 545                                      size_t chunk_word_size);
 546   static void deallocate_block_a_lot(SpaceManager* sm,
 547                                      size_t chunk_word_size);
 548 
 549 };
 550 
 551 int Metadebug::_deallocate_block_a_lot_count = 0;
 552 int Metadebug::_deallocate_chunk_a_lot_count = 0;
 553 int Metadebug::_allocation_fail_alot_count = 0;
 554 
 555 //  SpaceManager - used by Metaspace to handle allocations
 556 class SpaceManager : public CHeapObj<mtClass> {
 557   friend class Metaspace;
 558   friend class Metadebug;
 559 
 560  private:
 561 
 562   // protects allocations and contains.
 563   Mutex* const _lock;
 564 
 565   // Type of metadata allocated.
 566   Metaspace::MetadataType _mdtype;
 567 
 568   // Chunk related size
 569   size_t _medium_chunk_bunch;
 570 
 571   // List of chunks in use by this SpaceManager.  Allocations
 572   // are done from the current chunk.  The list is used for deallocating
 573   // chunks when the SpaceManager is freed.
 574   Metachunk* _chunks_in_use[NumberOfInUseLists];
 575   Metachunk* _current_chunk;
 576 
 577   // Virtual space where allocation comes from.
 578   VirtualSpaceList* _vs_list;
 579 
 580   // Number of small chunks to allocate to a manager
 581   // If class space manager, small chunks are unlimited
 582   static uint const _small_chunk_limit;
 583   bool has_small_chunk_limit() { return !vs_list()->is_class(); }
 584 
 585   // Sum of all space in allocated chunks
 586   size_t _allocated_blocks_words;
 587 
 588   // Sum of all allocated chunks
 589   size_t _allocated_chunks_words;
 590   size_t _allocated_chunks_count;
 591 
 592   // Free lists of blocks are per SpaceManager since they
 593   // are assumed to be in chunks in use by the SpaceManager
 594   // and all chunks in use by a SpaceManager are freed when
 595   // the class loader using the SpaceManager is collected.
 596   BlockFreelist _block_freelists;
 597 
 598   // protects virtualspace and chunk expansions
 599   static const char*  _expand_lock_name;
 600   static const int    _expand_lock_rank;
 601   static Mutex* const _expand_lock;
 602 
 603  private:
 604   // Accessors
 605   Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
 606   void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; }
 607 
 608   BlockFreelist* block_freelists() const {
 609     return (BlockFreelist*) &_block_freelists;
 610   }
 611 
 612   Metaspace::MetadataType mdtype() { return _mdtype; }
 613   VirtualSpaceList* vs_list() const    { return _vs_list; }
 614 
 615   Metachunk* current_chunk() const { return _current_chunk; }
 616   void set_current_chunk(Metachunk* v) {
 617     _current_chunk = v;
 618   }
 619 
 620   Metachunk* find_current_chunk(size_t word_size);
 621 
 622   // Add chunk to the list of chunks in use
 623   void add_chunk(Metachunk* v, bool make_current);
 624 
 625   Mutex* lock() const { return _lock; }
 626 
 627   const char* chunk_size_name(ChunkIndex index) const;
 628 
 629  protected:
 630   void initialize();
 631 
 632  public:
 633   SpaceManager(Metaspace::MetadataType mdtype,
 634                Mutex* lock,
 635                VirtualSpaceList* vs_list);
 636   ~SpaceManager();
 637 
 638   enum ChunkMultiples {
 639     MediumChunkMultiple = 4
 640   };
 641 
 642   // Accessors
 643   size_t specialized_chunk_size() { return SpecializedChunk; }
 644   size_t small_chunk_size() { return (size_t) vs_list()->is_class() ? ClassSmallChunk : SmallChunk; }
 645   size_t medium_chunk_size() { return (size_t) vs_list()->is_class() ? ClassMediumChunk : MediumChunk; }
 646   size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; }
 647 
 648   size_t allocated_blocks_words() const { return _allocated_blocks_words; }
 649   size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
 650   size_t allocated_chunks_words() const { return _allocated_chunks_words; }
 651   size_t allocated_chunks_count() const { return _allocated_chunks_count; }
 652 
 653   bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
 654 
 655   static Mutex* expand_lock() { return _expand_lock; }
 656 
 657   // Increment the per Metaspace and global running sums for Metachunks
 658   // by the given size.  This is used when a Metachunk to added to
 659   // the in-use list.
 660   void inc_size_metrics(size_t words);
 661   // Increment the per Metaspace and global running sums Metablocks by the given
 662   // size.  This is used when a Metablock is allocated.
 663   void inc_used_metrics(size_t words);
 664   // Delete the portion of the running sums for this SpaceManager. That is,
 665   // the globals running sums for the Metachunks and Metablocks are
 666   // decremented for all the Metachunks in-use by this SpaceManager.
 667   void dec_total_from_size_metrics();
 668 
 669   // Set the sizes for the initial chunks.
 670   void get_initial_chunk_sizes(Metaspace::MetaspaceType type,
 671                                size_t* chunk_word_size,
 672                                size_t* class_chunk_word_size);
 673 
 674   size_t sum_capacity_in_chunks_in_use() const;
 675   size_t sum_used_in_chunks_in_use() const;
 676   size_t sum_free_in_chunks_in_use() const;
 677   size_t sum_waste_in_chunks_in_use() const;
 678   size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
 679 
 680   size_t sum_count_in_chunks_in_use();
 681   size_t sum_count_in_chunks_in_use(ChunkIndex i);
 682 
 683   Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words);
 684 
 685   // Block allocation and deallocation.
 686   // Allocates a block from the current chunk
 687   MetaWord* allocate(size_t word_size);
 688 
 689   // Helper for allocations
 690   MetaWord* allocate_work(size_t word_size);
 691 
 692   // Returns a block to the per manager freelist
 693   void deallocate(MetaWord* p, size_t word_size);
 694 
 695   // Based on the allocation size and a minimum chunk size,
 696   // returned chunk size (for expanding space for chunk allocation).
 697   size_t calc_chunk_size(size_t allocation_word_size);
 698 
 699   // Called when an allocation from the current chunk fails.
 700   // Gets a new chunk (may require getting a new virtual space),
 701   // and allocates from that chunk.
 702   MetaWord* grow_and_allocate(size_t word_size);
 703 
 704   // debugging support.
 705 
 706   void dump(outputStream* const out) const;
 707   void print_on(outputStream* st) const;
 708   void locked_print_chunks_in_use_on(outputStream* st) const;
 709 
 710   void verify();
 711   void verify_chunk_size(Metachunk* chunk);
 712   NOT_PRODUCT(void mangle_freed_chunks();)
 713 #ifdef ASSERT
 714   void verify_allocated_blocks_words();
 715 #endif
 716 };
 717 
 718 uint const SpaceManager::_small_chunk_limit = 4;
 719 
 720 const char* SpaceManager::_expand_lock_name =
 721   "SpaceManager chunk allocation lock";
 722 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
 723 Mutex* const SpaceManager::_expand_lock =
 724   new Mutex(SpaceManager::_expand_lock_rank,
 725             SpaceManager::_expand_lock_name,
 726             Mutex::_allow_vm_block_flag);
 727 
 728 void VirtualSpaceNode::inc_container_count() {
 729   assert_lock_strong(SpaceManager::expand_lock());
 730   _container_count++;
 731   assert(_container_count == container_count_slow(),
 732          err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
 733                  "container_count_slow() " SIZE_FORMAT,
 734                  _container_count, container_count_slow()));
 735 }
 736 
 737 void VirtualSpaceNode::dec_container_count() {
 738   assert_lock_strong(SpaceManager::expand_lock());
 739   _container_count--;
 740 }
 741 
 742 #ifdef ASSERT
 743 void VirtualSpaceNode::verify_container_count() {
 744   assert(_container_count == container_count_slow(),
 745     err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
 746             "container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow()));
 747 }
 748 #endif
 749 
 750 // BlockFreelist methods
 751 
 752 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
 753 
 754 BlockFreelist::~BlockFreelist() {
 755   if (_dictionary != NULL) {
 756     if (Verbose && TraceMetadataChunkAllocation) {
 757       _dictionary->print_free_lists(gclog_or_tty);
 758     }
 759     delete _dictionary;
 760   }
 761 }
 762 
 763 Metablock* BlockFreelist::initialize_free_chunk(MetaWord* p, size_t word_size) {
 764   Metablock* block = (Metablock*) p;
 765   block->set_word_size(word_size);
 766   block->set_prev(NULL);
 767   block->set_next(NULL);
 768 
 769   return block;
 770 }
 771 
 772 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
 773   Metablock* free_chunk = initialize_free_chunk(p, word_size);
 774   if (dictionary() == NULL) {
 775    _dictionary = new BlockTreeDictionary();
 776   }
 777   dictionary()->return_chunk(free_chunk);
 778 }
 779 
 780 MetaWord* BlockFreelist::get_block(size_t word_size) {
 781   if (dictionary() == NULL) {
 782     return NULL;
 783   }
 784 
 785   if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
 786     // Dark matter.  Too small for dictionary.
 787     return NULL;
 788   }
 789 
 790   Metablock* free_block =
 791     dictionary()->get_chunk(word_size, FreeBlockDictionary<Metablock>::exactly);
 792   if (free_block == NULL) {
 793     return NULL;
 794   }
 795 
 796   return (MetaWord*) free_block;
 797 }
 798 
 799 void BlockFreelist::print_on(outputStream* st) const {
 800   if (dictionary() == NULL) {
 801     return;
 802   }
 803   dictionary()->print_free_lists(st);
 804 }
 805 
 806 // VirtualSpaceNode methods
 807 
 808 VirtualSpaceNode::~VirtualSpaceNode() {
 809   _rs.release();
 810 #ifdef ASSERT
 811   size_t word_size = sizeof(*this) / BytesPerWord;
 812   Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1);
 813 #endif
 814 }
 815 
 816 size_t VirtualSpaceNode::used_words_in_vs() const {
 817   return pointer_delta(top(), bottom(), sizeof(MetaWord));
 818 }
 819 
 820 // Space committed in the VirtualSpace
 821 size_t VirtualSpaceNode::capacity_words_in_vs() const {
 822   return pointer_delta(end(), bottom(), sizeof(MetaWord));
 823 }
 824 
 825 size_t VirtualSpaceNode::free_words_in_vs() const {
 826   return pointer_delta(end(), top(), sizeof(MetaWord));
 827 }
 828 
 829 // Allocates the chunk from the virtual space only.
 830 // This interface is also used internally for debugging.  Not all
 831 // chunks removed here are necessarily used for allocation.
 832 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
 833   // Bottom of the new chunk
 834   MetaWord* chunk_limit = top();
 835   assert(chunk_limit != NULL, "Not safe to call this method");
 836 
 837   if (!is_available(chunk_word_size)) {
 838     if (TraceMetadataChunkAllocation) {
 839       tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size);
 840       // Dump some information about the virtual space that is nearly full
 841       print_on(tty);
 842     }
 843     return NULL;
 844   }
 845 
 846   // Take the space  (bump top on the current virtual space).
 847   inc_top(chunk_word_size);
 848 
 849   // Initialize the chunk
 850   Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this);
 851   return result;
 852 }
 853 
 854 
 855 // Expand the virtual space (commit more of the reserved space)
 856 bool VirtualSpaceNode::expand_by(size_t words, bool pre_touch) {
 857   size_t bytes = words * BytesPerWord;
 858   bool result =  virtual_space()->expand_by(bytes, pre_touch);
 859   if (TraceMetavirtualspaceAllocation && !result) {
 860     gclog_or_tty->print_cr("VirtualSpaceNode::expand_by() failed "
 861                            "for byte size " SIZE_FORMAT, bytes);
 862     virtual_space()->print();
 863   }
 864   return result;
 865 }
 866 
 867 // Shrink the virtual space (commit more of the reserved space)
 868 bool VirtualSpaceNode::shrink_by(size_t words) {
 869   size_t bytes = words * BytesPerWord;
 870   virtual_space()->shrink_by(bytes);
 871   return true;
 872 }
 873 
 874 // Add another chunk to the chunk list.
 875 
 876 Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
 877   assert_lock_strong(SpaceManager::expand_lock());
 878   Metachunk* result = take_from_committed(chunk_word_size);
 879   if (result != NULL) {
 880     inc_container_count();
 881   }
 882   return result;
 883 }
 884 
 885 Metachunk* VirtualSpaceNode::get_chunk_vs_with_expand(size_t chunk_word_size) {
 886   assert_lock_strong(SpaceManager::expand_lock());
 887 
 888   Metachunk* new_chunk = get_chunk_vs(chunk_word_size);
 889 
 890   if (new_chunk == NULL) {
 891     // Only a small part of the virtualspace is committed when first
 892     // allocated so committing more here can be expected.
 893     size_t page_size_words = os::vm_page_size() / BytesPerWord;
 894     size_t aligned_expand_vs_by_words = align_size_up(chunk_word_size,
 895                                                     page_size_words);
 896     expand_by(aligned_expand_vs_by_words, false);
 897     new_chunk = get_chunk_vs(chunk_word_size);
 898   }
 899   return new_chunk;
 900 }
 901 
 902 bool VirtualSpaceNode::initialize() {
 903 
 904   if (!_rs.is_reserved()) {
 905     return false;
 906   }
 907 
 908   // An allocation out of this Virtualspace that is larger
 909   // than an initial commit size can waste that initial committed
 910   // space.
 911   size_t committed_byte_size = 0;
 912   bool result = virtual_space()->initialize(_rs, committed_byte_size);
 913   if (result) {
 914     set_top((MetaWord*)virtual_space()->low());
 915     set_reserved(MemRegion((HeapWord*)_rs.base(),
 916                  (HeapWord*)(_rs.base() + _rs.size())));
 917 
 918     assert(reserved()->start() == (HeapWord*) _rs.base(),
 919       err_msg("Reserved start was not set properly " PTR_FORMAT
 920         " != " PTR_FORMAT, reserved()->start(), _rs.base()));
 921     assert(reserved()->word_size() == _rs.size() / BytesPerWord,
 922       err_msg("Reserved size was not set properly " SIZE_FORMAT
 923         " != " SIZE_FORMAT, reserved()->word_size(),
 924         _rs.size() / BytesPerWord));
 925   }
 926 
 927   return result;
 928 }
 929 
 930 void VirtualSpaceNode::print_on(outputStream* st) const {
 931   size_t used = used_words_in_vs();
 932   size_t capacity = capacity_words_in_vs();
 933   VirtualSpace* vs = virtual_space();
 934   st->print_cr("   space @ " PTR_FORMAT " " SIZE_FORMAT "K, %3d%% used "
 935            "[" PTR_FORMAT ", " PTR_FORMAT ", "
 936            PTR_FORMAT ", " PTR_FORMAT ")",
 937            vs, capacity / K,
 938            capacity == 0 ? 0 : used * 100 / capacity,
 939            bottom(), top(), end(),
 940            vs->high_boundary());
 941 }
 942 
 943 #ifdef ASSERT
 944 void VirtualSpaceNode::mangle() {
 945   size_t word_size = capacity_words_in_vs();
 946   Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1);
 947 }
 948 #endif // ASSERT
 949 
 950 // VirtualSpaceList methods
 951 // Space allocated from the VirtualSpace
 952 
 953 VirtualSpaceList::~VirtualSpaceList() {
 954   VirtualSpaceListIterator iter(virtual_space_list());
 955   while (iter.repeat()) {
 956     VirtualSpaceNode* vsl = iter.get_next();
 957     delete vsl;
 958   }
 959 }
 960 
 961 void VirtualSpaceList::inc_virtual_space_total(size_t v) {
 962   assert_lock_strong(SpaceManager::expand_lock());
 963   _virtual_space_total = _virtual_space_total + v;
 964 }
 965 void VirtualSpaceList::dec_virtual_space_total(size_t v) {
 966   assert_lock_strong(SpaceManager::expand_lock());
 967   _virtual_space_total = _virtual_space_total - v;
 968 }
 969 
 970 void VirtualSpaceList::inc_virtual_space_count() {
 971   assert_lock_strong(SpaceManager::expand_lock());
 972   _virtual_space_count++;
 973 }
 974 void VirtualSpaceList::dec_virtual_space_count() {
 975   assert_lock_strong(SpaceManager::expand_lock());
 976   _virtual_space_count--;
 977 }
 978 
 979 void ChunkManager::remove_chunk(Metachunk* chunk) {
 980   size_t word_size = chunk->word_size();
 981   ChunkIndex index = list_index(word_size);
 982   if (index != HumongousIndex) {
 983     free_chunks(index)->remove_chunk(chunk);
 984   } else {
 985     humongous_dictionary()->remove_chunk(chunk);
 986   }
 987 
 988   // Chunk is being removed from the chunks free list.
 989   dec_free_chunks_total(chunk->capacity_word_size());
 990 }
 991 
 992 // Walk the list of VirtualSpaceNodes and delete
 993 // nodes with a 0 container_count.  Remove Metachunks in
 994 // the node from their respective freelists.
 995 void VirtualSpaceList::purge() {
 996   assert_lock_strong(SpaceManager::expand_lock());
 997   // Don't use a VirtualSpaceListIterator because this
 998   // list is being changed and a straightforward use of an iterator is not safe.
 999   VirtualSpaceNode* purged_vsl = NULL;
1000   VirtualSpaceNode* prev_vsl = virtual_space_list();
1001   VirtualSpaceNode* next_vsl = prev_vsl;
1002   while (next_vsl != NULL) {
1003     VirtualSpaceNode* vsl = next_vsl;
1004     next_vsl = vsl->next();
1005     // Don't free the current virtual space since it will likely
1006     // be needed soon.
1007     if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
1008       // Unlink it from the list
1009       if (prev_vsl == vsl) {
1010         // This is the case of the current note being the first note.
1011         assert(vsl == virtual_space_list(), "Expected to be the first note");
1012         set_virtual_space_list(vsl->next());
1013       } else {
1014         prev_vsl->set_next(vsl->next());
1015       }
1016 
1017       vsl->purge(chunk_manager());
1018       dec_virtual_space_total(vsl->reserved()->word_size());
1019       dec_virtual_space_count();
1020       purged_vsl = vsl;
1021       delete vsl;
1022     } else {
1023       prev_vsl = vsl;
1024     }
1025   }
1026 #ifdef ASSERT
1027   if (purged_vsl != NULL) {
1028   // List should be stable enough to use an iterator here.
1029   VirtualSpaceListIterator iter(virtual_space_list());
1030     while (iter.repeat()) {
1031       VirtualSpaceNode* vsl = iter.get_next();
1032       assert(vsl != purged_vsl, "Purge of vsl failed");
1033     }
1034   }
1035 #endif
1036 }
1037 
1038 size_t VirtualSpaceList::used_words_sum() {
1039   size_t allocated_by_vs = 0;
1040   VirtualSpaceListIterator iter(virtual_space_list());
1041   while (iter.repeat()) {
1042     VirtualSpaceNode* vsl = iter.get_next();
1043     // Sum used region [bottom, top) in each virtualspace
1044     allocated_by_vs += vsl->used_words_in_vs();
1045   }
1046   assert(allocated_by_vs >= chunk_manager()->free_chunks_total(),
1047     err_msg("Total in free chunks " SIZE_FORMAT
1048             " greater than total from virtual_spaces " SIZE_FORMAT,
1049             allocated_by_vs, chunk_manager()->free_chunks_total()));
1050   size_t used =
1051     allocated_by_vs - chunk_manager()->free_chunks_total();
1052   return used;
1053 }
1054 
1055 // Space available in all MetadataVirtualspaces allocated
1056 // for metadata.  This is the upper limit on the capacity
1057 // of chunks allocated out of all the MetadataVirtualspaces.
1058 size_t VirtualSpaceList::capacity_words_sum() {
1059   size_t capacity = 0;
1060   VirtualSpaceListIterator iter(virtual_space_list());
1061   while (iter.repeat()) {
1062     VirtualSpaceNode* vsl = iter.get_next();
1063     capacity += vsl->capacity_words_in_vs();
1064   }
1065   return capacity;
1066 }
1067 
1068 VirtualSpaceList::VirtualSpaceList(size_t word_size ) :
1069                                    _is_class(false),
1070                                    _virtual_space_list(NULL),
1071                                    _current_virtual_space(NULL),
1072                                    _virtual_space_total(0),
1073                                    _virtual_space_count(0) {
1074   MutexLockerEx cl(SpaceManager::expand_lock(),
1075                    Mutex::_no_safepoint_check_flag);
1076   bool initialization_succeeded = grow_vs(word_size);
1077 
1078   _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk);
1079   _chunk_manager.free_chunks(SmallIndex)->set_size(SmallChunk);
1080   _chunk_manager.free_chunks(MediumIndex)->set_size(MediumChunk);
1081   assert(initialization_succeeded,
1082     " VirtualSpaceList initialization should not fail");
1083 }
1084 
1085 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
1086                                    _is_class(true),
1087                                    _virtual_space_list(NULL),
1088                                    _current_virtual_space(NULL),
1089                                    _virtual_space_total(0),
1090                                    _virtual_space_count(0) {
1091   MutexLockerEx cl(SpaceManager::expand_lock(),
1092                    Mutex::_no_safepoint_check_flag);
1093   VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
1094   bool succeeded = class_entry->initialize();
1095   _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk);
1096   _chunk_manager.free_chunks(SmallIndex)->set_size(ClassSmallChunk);
1097   _chunk_manager.free_chunks(MediumIndex)->set_size(ClassMediumChunk);
1098   assert(succeeded, " VirtualSpaceList initialization should not fail");
1099   link_vs(class_entry, rs.size()/BytesPerWord);
1100 }
1101 
1102 size_t VirtualSpaceList::free_bytes() {
1103   return virtual_space_list()->free_words_in_vs() * BytesPerWord;
1104 }
1105 
1106 // Allocate another meta virtual space and add it to the list.
1107 bool VirtualSpaceList::grow_vs(size_t vs_word_size) {
1108   assert_lock_strong(SpaceManager::expand_lock());
1109   if (vs_word_size == 0) {
1110     return false;
1111   }
1112   // Reserve the space
1113   size_t vs_byte_size = vs_word_size * BytesPerWord;
1114   assert(vs_byte_size % os::vm_page_size() == 0, "Not aligned");
1115 
1116   // Allocate the meta virtual space and initialize it.
1117   VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
1118   if (!new_entry->initialize()) {
1119     delete new_entry;
1120     return false;
1121   } else {
1122     // ensure lock-free iteration sees fully initialized node
1123     OrderAccess::storestore();
1124     link_vs(new_entry, vs_word_size);
1125     return true;
1126   }
1127 }
1128 
1129 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size) {
1130   if (virtual_space_list() == NULL) {
1131       set_virtual_space_list(new_entry);
1132   } else {
1133     current_virtual_space()->set_next(new_entry);
1134   }
1135   set_current_virtual_space(new_entry);
1136   inc_virtual_space_total(vs_word_size);
1137   inc_virtual_space_count();
1138 #ifdef ASSERT
1139   new_entry->mangle();
1140 #endif
1141   if (TraceMetavirtualspaceAllocation && Verbose) {
1142     VirtualSpaceNode* vsl = current_virtual_space();
1143     vsl->print_on(tty);
1144   }
1145 }
1146 
1147 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
1148                                            size_t grow_chunks_by_words,
1149                                            size_t medium_chunk_bunch) {
1150 
1151   // Get a chunk from the chunk freelist
1152   Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
1153 
1154   if (next != NULL) {
1155     next->container()->inc_container_count();
1156   } else {
1157     // Allocate a chunk out of the current virtual space.
1158     next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
1159   }
1160 
1161   if (next == NULL) {
1162     // Not enough room in current virtual space.  Try to commit
1163     // more space.
1164     size_t expand_vs_by_words = MAX2(medium_chunk_bunch,
1165                                      grow_chunks_by_words);
1166     size_t page_size_words = os::vm_page_size() / BytesPerWord;
1167     size_t aligned_expand_vs_by_words = align_size_up(expand_vs_by_words,
1168                                                         page_size_words);
1169     bool vs_expanded =
1170       current_virtual_space()->expand_by(aligned_expand_vs_by_words, false);
1171     if (!vs_expanded) {
1172       // Should the capacity of the metaspaces be expanded for
1173       // this allocation?  If it's the virtual space for classes and is
1174       // being used for CompressedHeaders, don't allocate a new virtualspace.
1175       if (can_grow() && MetaspaceGC::should_expand(this, word_size)) {
1176         // Get another virtual space.
1177           size_t grow_vs_words =
1178             MAX2((size_t)VirtualSpaceSize, aligned_expand_vs_by_words);
1179         if (grow_vs(grow_vs_words)) {
1180           // Got it.  It's on the list now.  Get a chunk from it.
1181           next = current_virtual_space()->get_chunk_vs_with_expand(grow_chunks_by_words);
1182         }
1183       } else {
1184         // Allocation will fail and induce a GC
1185         if (TraceMetadataChunkAllocation && Verbose) {
1186           gclog_or_tty->print_cr("VirtualSpaceList::get_new_chunk():"
1187             " Fail instead of expand the metaspace");
1188         }
1189       }
1190     } else {
1191       // The virtual space expanded, get a new chunk
1192       next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
1193       assert(next != NULL, "Just expanded, should succeed");
1194     }
1195   }
1196 
1197   assert(next == NULL || (next->next() == NULL && next->prev() == NULL),
1198          "New chunk is still on some list");
1199   return next;
1200 }
1201 
1202 Metachunk* VirtualSpaceList::get_initialization_chunk(size_t chunk_word_size,
1203                                                       size_t chunk_bunch) {
1204   // Get a chunk from the chunk freelist
1205   Metachunk* new_chunk = get_new_chunk(chunk_word_size,
1206                                        chunk_word_size,
1207                                        chunk_bunch);
1208   return new_chunk;
1209 }
1210 
1211 void VirtualSpaceList::print_on(outputStream* st) const {
1212   if (TraceMetadataChunkAllocation && Verbose) {
1213     VirtualSpaceListIterator iter(virtual_space_list());
1214     while (iter.repeat()) {
1215       VirtualSpaceNode* node = iter.get_next();
1216       node->print_on(st);
1217     }
1218   }
1219 }
1220 
1221 bool VirtualSpaceList::contains(const void *ptr) {
1222   VirtualSpaceNode* list = virtual_space_list();
1223   VirtualSpaceListIterator iter(list);
1224   while (iter.repeat()) {
1225     VirtualSpaceNode* node = iter.get_next();
1226     if (node->reserved()->contains(ptr)) {
1227       return true;
1228     }
1229   }
1230   return false;
1231 }
1232 
1233 
1234 // MetaspaceGC methods
1235 
1236 // VM_CollectForMetadataAllocation is the vm operation used to GC.
1237 // Within the VM operation after the GC the attempt to allocate the metadata
1238 // should succeed.  If the GC did not free enough space for the metaspace
1239 // allocation, the HWM is increased so that another virtualspace will be
1240 // allocated for the metadata.  With perm gen the increase in the perm
1241 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion.  The
1242 // metaspace policy uses those as the small and large steps for the HWM.
1243 //
1244 // After the GC the compute_new_size() for MetaspaceGC is called to
1245 // resize the capacity of the metaspaces.  The current implementation
1246 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
1247 // to resize the Java heap by some GC's.  New flags can be implemented
1248 // if really needed.  MinMetaspaceFreeRatio is used to calculate how much
1249 // free space is desirable in the metaspace capacity to decide how much
1250 // to increase the HWM.  MaxMetaspaceFreeRatio is used to decide how much
1251 // free space is desirable in the metaspace capacity before decreasing
1252 // the HWM.
1253 
1254 // Calculate the amount to increase the high water mark (HWM).
1255 // Increase by a minimum amount (MinMetaspaceExpansion) so that
1256 // another expansion is not requested too soon.  If that is not
1257 // enough to satisfy the allocation (i.e. big enough for a word_size
1258 // allocation), increase by MaxMetaspaceExpansion.  If that is still
1259 // not enough, expand by the size of the allocation (word_size) plus
1260 // some.
1261 size_t MetaspaceGC::delta_capacity_until_GC(size_t word_size) {
1262   size_t before_inc = MetaspaceGC::capacity_until_GC();
1263   size_t min_delta_words = MinMetaspaceExpansion / BytesPerWord;
1264   size_t max_delta_words = MaxMetaspaceExpansion / BytesPerWord;
1265   size_t page_size_words = os::vm_page_size() / BytesPerWord;
1266   size_t size_delta_words = align_size_up(word_size, page_size_words);
1267   size_t delta_words = MAX2(size_delta_words, min_delta_words);
1268   if (delta_words > min_delta_words) {
1269     // Don't want to hit the high water mark on the next
1270     // allocation so make the delta greater than just enough
1271     // for this allocation.
1272     delta_words = MAX2(delta_words, max_delta_words);
1273     if (delta_words > max_delta_words) {
1274       // This allocation is large but the next ones are probably not
1275       // so increase by the minimum.
1276       delta_words = delta_words + min_delta_words;
1277     }
1278   }
1279   return delta_words;
1280 }
1281 
1282 bool MetaspaceGC::should_expand(VirtualSpaceList* vsl, size_t word_size) {
1283 
1284   size_t committed_capacity_bytes = MetaspaceAux::allocated_capacity_bytes();
1285   // If the user wants a limit, impose one.
1286   size_t max_metaspace_size_bytes = MaxMetaspaceSize;
1287   size_t metaspace_size_bytes = MetaspaceSize;
1288   if (!FLAG_IS_DEFAULT(MaxMetaspaceSize) &&
1289       MetaspaceAux::reserved_in_bytes() >= MaxMetaspaceSize) {
1290     return false;
1291   }
1292 
1293   // Class virtual space should always be expanded.  Call GC for the other
1294   // metadata virtual space.
1295   if (vsl == Metaspace::class_space_list()) return true;
1296 
1297   // If this is part of an allocation after a GC, expand
1298   // unconditionally.
1299   if (MetaspaceGC::expand_after_GC()) {
1300     return true;
1301   }
1302 
1303 
1304 
1305   // If the capacity is below the minimum capacity, allow the
1306   // expansion.  Also set the high-water-mark (capacity_until_GC)
1307   // to that minimum capacity so that a GC will not be induced
1308   // until that minimum capacity is exceeded.
1309   if (committed_capacity_bytes < metaspace_size_bytes ||
1310       capacity_until_GC() == 0) {
1311     set_capacity_until_GC(metaspace_size_bytes);
1312     return true;
1313   } else {
1314     if (committed_capacity_bytes < capacity_until_GC()) {
1315       return true;
1316     } else {
1317       if (TraceMetadataChunkAllocation && Verbose) {
1318         gclog_or_tty->print_cr("  allocation request size " SIZE_FORMAT
1319                         "  capacity_until_GC " SIZE_FORMAT
1320                         "  allocated_capacity_bytes " SIZE_FORMAT,
1321                         word_size,
1322                         capacity_until_GC(),
1323                         MetaspaceAux::allocated_capacity_bytes());
1324       }
1325       return false;
1326     }
1327   }
1328 }
1329 
1330 
1331 
1332 void MetaspaceGC::compute_new_size() {
1333   assert(_shrink_factor <= 100, "invalid shrink factor");
1334   uint current_shrink_factor = _shrink_factor;
1335   _shrink_factor = 0;
1336 
1337   // Until a faster way of calculating the "used" quantity is implemented,
1338   // use "capacity".
1339   const size_t used_after_gc = MetaspaceAux::allocated_capacity_bytes();
1340   const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC();
1341 
1342   const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
1343   const double maximum_used_percentage = 1.0 - minimum_free_percentage;
1344 
1345   const double min_tmp = used_after_gc / maximum_used_percentage;
1346   size_t minimum_desired_capacity =
1347     (size_t)MIN2(min_tmp, double(max_uintx));
1348   // Don't shrink less than the initial generation size
1349   minimum_desired_capacity = MAX2(minimum_desired_capacity,
1350                                   MetaspaceSize);
1351 
1352   if (PrintGCDetails && Verbose) {
1353     gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: ");
1354     gclog_or_tty->print_cr("  "
1355                   "  minimum_free_percentage: %6.2f"
1356                   "  maximum_used_percentage: %6.2f",
1357                   minimum_free_percentage,
1358                   maximum_used_percentage);
1359     gclog_or_tty->print_cr("  "
1360                   "   used_after_gc       : %6.1fKB",
1361                   used_after_gc / (double) K);
1362   }
1363 
1364 
1365   size_t shrink_bytes = 0;
1366   if (capacity_until_GC < minimum_desired_capacity) {
1367     // If we have less capacity below the metaspace HWM, then
1368     // increment the HWM.
1369     size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
1370     // Don't expand unless it's significant
1371     if (expand_bytes >= MinMetaspaceExpansion) {
1372       MetaspaceGC::set_capacity_until_GC(capacity_until_GC + expand_bytes);
1373     }
1374     if (PrintGCDetails && Verbose) {
1375       size_t new_capacity_until_GC = capacity_until_GC;
1376       gclog_or_tty->print_cr("    expanding:"
1377                     "  minimum_desired_capacity: %6.1fKB"
1378                     "  expand_bytes: %6.1fKB"
1379                     "  MinMetaspaceExpansion: %6.1fKB"
1380                     "  new metaspace HWM:  %6.1fKB",
1381                     minimum_desired_capacity / (double) K,
1382                     expand_bytes / (double) K,
1383                     MinMetaspaceExpansion / (double) K,
1384                     new_capacity_until_GC / (double) K);
1385     }
1386     return;
1387   }
1388 
1389   // No expansion, now see if we want to shrink
1390   // We would never want to shrink more than this
1391   size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity;
1392   assert(max_shrink_bytes >= 0, err_msg("max_shrink_bytes " SIZE_FORMAT,
1393     max_shrink_bytes));
1394 
1395   // Should shrinking be considered?
1396   if (MaxMetaspaceFreeRatio < 100) {
1397     const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0;
1398     const double minimum_used_percentage = 1.0 - maximum_free_percentage;
1399     const double max_tmp = used_after_gc / minimum_used_percentage;
1400     size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
1401     maximum_desired_capacity = MAX2(maximum_desired_capacity,
1402                                     MetaspaceSize);
1403     if (PrintGCDetails && Verbose) {
1404       gclog_or_tty->print_cr("  "
1405                              "  maximum_free_percentage: %6.2f"
1406                              "  minimum_used_percentage: %6.2f",
1407                              maximum_free_percentage,
1408                              minimum_used_percentage);
1409       gclog_or_tty->print_cr("  "
1410                              "  minimum_desired_capacity: %6.1fKB"
1411                              "  maximum_desired_capacity: %6.1fKB",
1412                              minimum_desired_capacity / (double) K,
1413                              maximum_desired_capacity / (double) K);
1414     }
1415 
1416     assert(minimum_desired_capacity <= maximum_desired_capacity,
1417            "sanity check");
1418 
1419     if (capacity_until_GC > maximum_desired_capacity) {
1420       // Capacity too large, compute shrinking size
1421       shrink_bytes = capacity_until_GC - maximum_desired_capacity;
1422       // We don't want shrink all the way back to initSize if people call
1423       // System.gc(), because some programs do that between "phases" and then
1424       // we'd just have to grow the heap up again for the next phase.  So we
1425       // damp the shrinking: 0% on the first call, 10% on the second call, 40%
1426       // on the third call, and 100% by the fourth call.  But if we recompute
1427       // size without shrinking, it goes back to 0%.
1428       shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
1429       assert(shrink_bytes <= max_shrink_bytes,
1430         err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
1431           shrink_bytes, max_shrink_bytes));
1432       if (current_shrink_factor == 0) {
1433         _shrink_factor = 10;
1434       } else {
1435         _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
1436       }
1437       if (PrintGCDetails && Verbose) {
1438         gclog_or_tty->print_cr("  "
1439                       "  shrinking:"
1440                       "  initSize: %.1fK"
1441                       "  maximum_desired_capacity: %.1fK",
1442                       MetaspaceSize / (double) K,
1443                       maximum_desired_capacity / (double) K);
1444         gclog_or_tty->print_cr("  "
1445                       "  shrink_bytes: %.1fK"
1446                       "  current_shrink_factor: %d"
1447                       "  new shrink factor: %d"
1448                       "  MinMetaspaceExpansion: %.1fK",
1449                       shrink_bytes / (double) K,
1450                       current_shrink_factor,
1451                       _shrink_factor,
1452                       MinMetaspaceExpansion / (double) K);
1453       }
1454     }
1455   }
1456 
1457   // Don't shrink unless it's significant
1458   if (shrink_bytes >= MinMetaspaceExpansion &&
1459       ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
1460     MetaspaceGC::set_capacity_until_GC(capacity_until_GC - shrink_bytes);
1461   }
1462 }
1463 
1464 // Metadebug methods
1465 
1466 void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm,
1467                                        size_t chunk_word_size){
1468 #ifdef ASSERT
1469   VirtualSpaceList* vsl = sm->vs_list();
1470   if (MetaDataDeallocateALot &&
1471       Metadebug::deallocate_chunk_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
1472     Metadebug::reset_deallocate_chunk_a_lot_count();
1473     for (uint i = 0; i < metadata_deallocate_a_lock_chunk; i++) {
1474       Metachunk* dummy_chunk = vsl->current_virtual_space()->take_from_committed(chunk_word_size);
1475       if (dummy_chunk == NULL) {
1476         break;
1477       }
1478       vsl->chunk_manager()->chunk_freelist_deallocate(dummy_chunk);
1479 
1480       if (TraceMetadataChunkAllocation && Verbose) {
1481         gclog_or_tty->print("Metadebug::deallocate_chunk_a_lot: %d) ",
1482                                sm->sum_count_in_chunks_in_use());
1483         dummy_chunk->print_on(gclog_or_tty);
1484         gclog_or_tty->print_cr("  Free chunks total %d  count %d",
1485                                vsl->chunk_manager()->free_chunks_total(),
1486                                vsl->chunk_manager()->free_chunks_count());
1487       }
1488     }
1489   } else {
1490     Metadebug::inc_deallocate_chunk_a_lot_count();
1491   }
1492 #endif
1493 }
1494 
1495 void Metadebug::deallocate_block_a_lot(SpaceManager* sm,
1496                                        size_t raw_word_size){
1497 #ifdef ASSERT
1498   if (MetaDataDeallocateALot &&
1499         Metadebug::deallocate_block_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
1500     Metadebug::set_deallocate_block_a_lot_count(0);
1501     for (uint i = 0; i < metadata_deallocate_a_lot_block; i++) {
1502       MetaWord* dummy_block = sm->allocate_work(raw_word_size);
1503       if (dummy_block == 0) {
1504         break;
1505       }
1506       sm->deallocate(dummy_block, raw_word_size);
1507     }
1508   } else {
1509     Metadebug::inc_deallocate_block_a_lot_count();
1510   }
1511 #endif
1512 }
1513 
1514 void Metadebug::init_allocation_fail_alot_count() {
1515   if (MetadataAllocationFailALot) {
1516     _allocation_fail_alot_count =
1517       1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0));
1518   }
1519 }
1520 
1521 #ifdef ASSERT
1522 bool Metadebug::test_metadata_failure() {
1523   if (MetadataAllocationFailALot &&
1524       Threads::is_vm_complete()) {
1525     if (_allocation_fail_alot_count > 0) {
1526       _allocation_fail_alot_count--;
1527     } else {
1528       if (TraceMetadataChunkAllocation && Verbose) {
1529         gclog_or_tty->print_cr("Metadata allocation failing for "
1530                                "MetadataAllocationFailALot");
1531       }
1532       init_allocation_fail_alot_count();
1533       return true;
1534     }
1535   }
1536   return false;
1537 }
1538 #endif
1539 
1540 // ChunkManager methods
1541 
1542 // Verification of _free_chunks_total and _free_chunks_count does not
1543 // work with the CMS collector because its use of additional locks
1544 // complicate the mutex deadlock detection but it can still be useful
1545 // for detecting errors in the chunk accounting with other collectors.
1546 
1547 size_t ChunkManager::free_chunks_total() {
1548 #ifdef ASSERT
1549   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
1550     MutexLockerEx cl(SpaceManager::expand_lock(),
1551                      Mutex::_no_safepoint_check_flag);
1552     slow_locked_verify_free_chunks_total();
1553   }
1554 #endif
1555   return _free_chunks_total;
1556 }
1557 
1558 size_t ChunkManager::free_chunks_total_in_bytes() {
1559   return free_chunks_total() * BytesPerWord;
1560 }
1561 
1562 size_t ChunkManager::free_chunks_count() {
1563 #ifdef ASSERT
1564   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
1565     MutexLockerEx cl(SpaceManager::expand_lock(),
1566                      Mutex::_no_safepoint_check_flag);
1567     // This lock is only needed in debug because the verification
1568     // of the _free_chunks_totals walks the list of free chunks
1569     slow_locked_verify_free_chunks_count();
1570   }
1571 #endif
1572   return _free_chunks_count;
1573 }
1574 
1575 void ChunkManager::locked_verify_free_chunks_total() {
1576   assert_lock_strong(SpaceManager::expand_lock());
1577   assert(sum_free_chunks() == _free_chunks_total,
1578     err_msg("_free_chunks_total " SIZE_FORMAT " is not the"
1579            " same as sum " SIZE_FORMAT, _free_chunks_total,
1580            sum_free_chunks()));
1581 }
1582 
1583 void ChunkManager::verify_free_chunks_total() {
1584   MutexLockerEx cl(SpaceManager::expand_lock(),
1585                      Mutex::_no_safepoint_check_flag);
1586   locked_verify_free_chunks_total();
1587 }
1588 
1589 void ChunkManager::locked_verify_free_chunks_count() {
1590   assert_lock_strong(SpaceManager::expand_lock());
1591   assert(sum_free_chunks_count() == _free_chunks_count,
1592     err_msg("_free_chunks_count " SIZE_FORMAT " is not the"
1593            " same as sum " SIZE_FORMAT, _free_chunks_count,
1594            sum_free_chunks_count()));
1595 }
1596 
1597 void ChunkManager::verify_free_chunks_count() {
1598 #ifdef ASSERT
1599   MutexLockerEx cl(SpaceManager::expand_lock(),
1600                      Mutex::_no_safepoint_check_flag);
1601   locked_verify_free_chunks_count();
1602 #endif
1603 }
1604 
1605 void ChunkManager::verify() {
1606   MutexLockerEx cl(SpaceManager::expand_lock(),
1607                      Mutex::_no_safepoint_check_flag);
1608   locked_verify();
1609 }
1610 
1611 void ChunkManager::locked_verify() {
1612   locked_verify_free_chunks_count();
1613   locked_verify_free_chunks_total();
1614 }
1615 
1616 void ChunkManager::locked_print_free_chunks(outputStream* st) {
1617   assert_lock_strong(SpaceManager::expand_lock());
1618   st->print_cr("Free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
1619                 _free_chunks_total, _free_chunks_count);
1620 }
1621 
1622 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
1623   assert_lock_strong(SpaceManager::expand_lock());
1624   st->print_cr("Sum free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
1625                 sum_free_chunks(), sum_free_chunks_count());
1626 }
1627 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
1628   return &_free_chunks[index];
1629 }
1630 
1631 // These methods that sum the free chunk lists are used in printing
1632 // methods that are used in product builds.
1633 size_t ChunkManager::sum_free_chunks() {
1634   assert_lock_strong(SpaceManager::expand_lock());
1635   size_t result = 0;
1636   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
1637     ChunkList* list = free_chunks(i);
1638 
1639     if (list == NULL) {
1640       continue;
1641     }
1642 
1643     result = result + list->count() * list->size();
1644   }
1645   result = result + humongous_dictionary()->total_size();
1646   return result;
1647 }
1648 
1649 size_t ChunkManager::sum_free_chunks_count() {
1650   assert_lock_strong(SpaceManager::expand_lock());
1651   size_t count = 0;
1652   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
1653     ChunkList* list = free_chunks(i);
1654     if (list == NULL) {
1655       continue;
1656     }
1657     count = count + list->count();
1658   }
1659   count = count + humongous_dictionary()->total_free_blocks();
1660   return count;
1661 }
1662 
1663 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
1664   ChunkIndex index = list_index(word_size);
1665   assert(index < HumongousIndex, "No humongous list");
1666   return free_chunks(index);
1667 }
1668 
1669 void ChunkManager::free_chunks_put(Metachunk* chunk) {
1670   assert_lock_strong(SpaceManager::expand_lock());
1671   ChunkList* free_list = find_free_chunks_list(chunk->word_size());
1672   chunk->set_next(free_list->head());
1673   free_list->set_head(chunk);
1674   // chunk is being returned to the chunk free list
1675   inc_free_chunks_total(chunk->capacity_word_size());
1676   slow_locked_verify();
1677 }
1678 
1679 void ChunkManager::chunk_freelist_deallocate(Metachunk* chunk) {
1680   // The deallocation of a chunk originates in the freelist
1681   // manangement code for a Metaspace and does not hold the
1682   // lock.
1683   assert(chunk != NULL, "Deallocating NULL");
1684   assert_lock_strong(SpaceManager::expand_lock());
1685   slow_locked_verify();
1686   if (TraceMetadataChunkAllocation) {
1687     tty->print_cr("ChunkManager::chunk_freelist_deallocate: chunk "
1688                   PTR_FORMAT "  size " SIZE_FORMAT,
1689                   chunk, chunk->word_size());
1690   }
1691   free_chunks_put(chunk);
1692 }
1693 
1694 Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
1695   assert_lock_strong(SpaceManager::expand_lock());
1696 
1697   slow_locked_verify();
1698 
1699   Metachunk* chunk = NULL;
1700   if (list_index(word_size) != HumongousIndex) {
1701     ChunkList* free_list = find_free_chunks_list(word_size);
1702     assert(free_list != NULL, "Sanity check");
1703 
1704     chunk = free_list->head();
1705     debug_only(Metachunk* debug_head = chunk;)
1706 
1707     if (chunk == NULL) {
1708       return NULL;
1709     }
1710 
1711     // Remove the chunk as the head of the list.
1712     free_list->remove_chunk(chunk);
1713 
1714     // Chunk is being removed from the chunks free list.
1715     dec_free_chunks_total(chunk->capacity_word_size());
1716 
1717     if (TraceMetadataChunkAllocation && Verbose) {
1718       tty->print_cr("ChunkManager::free_chunks_get: free_list "
1719                     PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
1720                     free_list, chunk, chunk->word_size());
1721     }
1722   } else {
1723     chunk = humongous_dictionary()->get_chunk(
1724       word_size,
1725       FreeBlockDictionary<Metachunk>::atLeast);
1726 
1727     if (chunk != NULL) {
1728       if (TraceMetadataHumongousAllocation) {
1729         size_t waste = chunk->word_size() - word_size;
1730         tty->print_cr("Free list allocate humongous chunk size " SIZE_FORMAT
1731                       " for requested size " SIZE_FORMAT
1732                       " waste " SIZE_FORMAT,
1733                       chunk->word_size(), word_size, waste);
1734       }
1735       // Chunk is being removed from the chunks free list.
1736       dec_free_chunks_total(chunk->capacity_word_size());
1737     } else {
1738       return NULL;
1739     }
1740   }
1741 
1742   // Remove it from the links to this freelist
1743   chunk->set_next(NULL);
1744   chunk->set_prev(NULL);
1745 #ifdef ASSERT
1746   // Chunk is no longer on any freelist. Setting to false make container_count_slow()
1747   // work.
1748   chunk->set_is_free(false);
1749 #endif
1750   slow_locked_verify();
1751   return chunk;
1752 }
1753 
1754 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
1755   assert_lock_strong(SpaceManager::expand_lock());
1756   slow_locked_verify();
1757 
1758   // Take from the beginning of the list
1759   Metachunk* chunk = free_chunks_get(word_size);
1760   if (chunk == NULL) {
1761     return NULL;
1762   }
1763 
1764   assert((word_size <= chunk->word_size()) ||
1765          list_index(chunk->word_size() == HumongousIndex),
1766          "Non-humongous variable sized chunk");
1767   if (TraceMetadataChunkAllocation) {
1768     size_t list_count;
1769     if (list_index(word_size) < HumongousIndex) {
1770       ChunkList* list = find_free_chunks_list(word_size);
1771       list_count = list->count();
1772     } else {
1773       list_count = humongous_dictionary()->total_count();
1774     }
1775     tty->print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk "
1776                PTR_FORMAT "  size " SIZE_FORMAT " count " SIZE_FORMAT " ",
1777                this, chunk, chunk->word_size(), list_count);
1778     locked_print_free_chunks(tty);
1779   }
1780 
1781   return chunk;
1782 }
1783 
1784 void ChunkManager::print_on(outputStream* out) {
1785   if (PrintFLSStatistics != 0) {
1786     humongous_dictionary()->report_statistics();
1787   }
1788 }
1789 
1790 // SpaceManager methods
1791 
1792 void SpaceManager::get_initial_chunk_sizes(Metaspace::MetaspaceType type,
1793                                            size_t* chunk_word_size,
1794                                            size_t* class_chunk_word_size) {
1795   switch (type) {
1796   case Metaspace::BootMetaspaceType:
1797     *chunk_word_size = Metaspace::first_chunk_word_size();
1798     *class_chunk_word_size = Metaspace::first_class_chunk_word_size();
1799     break;
1800   case Metaspace::ROMetaspaceType:
1801     *chunk_word_size = SharedReadOnlySize / wordSize;
1802     *class_chunk_word_size = ClassSpecializedChunk;
1803     break;
1804   case Metaspace::ReadWriteMetaspaceType:
1805     *chunk_word_size = SharedReadWriteSize / wordSize;
1806     *class_chunk_word_size = ClassSpecializedChunk;
1807     break;
1808   case Metaspace::AnonymousMetaspaceType:
1809   case Metaspace::ReflectionMetaspaceType:
1810     *chunk_word_size = SpecializedChunk;
1811     *class_chunk_word_size = ClassSpecializedChunk;
1812     break;
1813   default:
1814     *chunk_word_size = SmallChunk;
1815     *class_chunk_word_size = ClassSmallChunk;
1816     break;
1817   }
1818   assert(*chunk_word_size != 0 && *class_chunk_word_size != 0,
1819     err_msg("Initial chunks sizes bad: data  " SIZE_FORMAT
1820             " class " SIZE_FORMAT,
1821             *chunk_word_size, *class_chunk_word_size));
1822 }
1823 
1824 size_t SpaceManager::sum_free_in_chunks_in_use() const {
1825   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
1826   size_t free = 0;
1827   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1828     Metachunk* chunk = chunks_in_use(i);
1829     while (chunk != NULL) {
1830       free += chunk->free_word_size();
1831       chunk = chunk->next();
1832     }
1833   }
1834   return free;
1835 }
1836 
1837 size_t SpaceManager::sum_waste_in_chunks_in_use() const {
1838   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
1839   size_t result = 0;
1840   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1841    result += sum_waste_in_chunks_in_use(i);
1842   }
1843 
1844   return result;
1845 }
1846 
1847 size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const {
1848   size_t result = 0;
1849   Metachunk* chunk = chunks_in_use(index);
1850   // Count the free space in all the chunk but not the
1851   // current chunk from which allocations are still being done.
1852   if (chunk != NULL) {
1853     Metachunk* prev = chunk;
1854     while (chunk != NULL && chunk != current_chunk()) {
1855       result += chunk->free_word_size();
1856       prev = chunk;
1857       chunk = chunk->next();
1858     }
1859   }
1860   return result;
1861 }
1862 
1863 size_t SpaceManager::sum_capacity_in_chunks_in_use() const {
1864   // For CMS use "allocated_chunks_words()" which does not need the
1865   // Metaspace lock.  For the other collectors sum over the
1866   // lists.  Use both methods as a check that "allocated_chunks_words()"
1867   // is correct.  That is, sum_capacity_in_chunks() is too expensive
1868   // to use in the product and allocated_chunks_words() should be used
1869   // but allow for  checking that allocated_chunks_words() returns the same
1870   // value as sum_capacity_in_chunks_in_use() which is the definitive
1871   // answer.
1872   if (UseConcMarkSweepGC) {
1873     return allocated_chunks_words();
1874   } else {
1875     MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
1876     size_t sum = 0;
1877     for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1878       Metachunk* chunk = chunks_in_use(i);
1879       while (chunk != NULL) {
1880         sum += chunk->capacity_word_size();
1881         chunk = chunk->next();
1882       }
1883     }
1884   return sum;
1885   }
1886 }
1887 
1888 size_t SpaceManager::sum_count_in_chunks_in_use() {
1889   size_t count = 0;
1890   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1891     count = count + sum_count_in_chunks_in_use(i);
1892   }
1893 
1894   return count;
1895 }
1896 
1897 size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) {
1898   size_t count = 0;
1899   Metachunk* chunk = chunks_in_use(i);
1900   while (chunk != NULL) {
1901     count++;
1902     chunk = chunk->next();
1903   }
1904   return count;
1905 }
1906 
1907 
1908 size_t SpaceManager::sum_used_in_chunks_in_use() const {
1909   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
1910   size_t used = 0;
1911   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1912     Metachunk* chunk = chunks_in_use(i);
1913     while (chunk != NULL) {
1914       used += chunk->used_word_size();
1915       chunk = chunk->next();
1916     }
1917   }
1918   return used;
1919 }
1920 
1921 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
1922 
1923   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1924     Metachunk* chunk = chunks_in_use(i);
1925     st->print("SpaceManager: %s " PTR_FORMAT,
1926                  chunk_size_name(i), chunk);
1927     if (chunk != NULL) {
1928       st->print_cr(" free " SIZE_FORMAT,
1929                    chunk->free_word_size());
1930     } else {
1931       st->print_cr("");
1932     }
1933   }
1934 
1935   vs_list()->chunk_manager()->locked_print_free_chunks(st);
1936   vs_list()->chunk_manager()->locked_print_sum_free_chunks(st);
1937 }
1938 
1939 size_t SpaceManager::calc_chunk_size(size_t word_size) {
1940 
1941   // Decide between a small chunk and a medium chunk.  Up to
1942   // _small_chunk_limit small chunks can be allocated but
1943   // once a medium chunk has been allocated, no more small
1944   // chunks will be allocated.
1945   size_t chunk_word_size;
1946   if (chunks_in_use(MediumIndex) == NULL &&
1947       (!has_small_chunk_limit() ||
1948        sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit)) {
1949     chunk_word_size = (size_t) small_chunk_size();
1950     if (word_size + Metachunk::overhead() > small_chunk_size()) {
1951       chunk_word_size = medium_chunk_size();
1952     }
1953   } else {
1954     chunk_word_size = medium_chunk_size();
1955   }
1956 
1957   // Might still need a humongous chunk.  Enforce an
1958   // eight word granularity to facilitate reuse (some
1959   // wastage but better chance of reuse).
1960   size_t if_humongous_sized_chunk =
1961     align_size_up(word_size + Metachunk::overhead(),
1962                   HumongousChunkGranularity);
1963   chunk_word_size =
1964     MAX2((size_t) chunk_word_size, if_humongous_sized_chunk);
1965 
1966   assert(!SpaceManager::is_humongous(word_size) ||
1967          chunk_word_size == if_humongous_sized_chunk,
1968          err_msg("Size calculation is wrong, word_size " SIZE_FORMAT
1969                  " chunk_word_size " SIZE_FORMAT,
1970                  word_size, chunk_word_size));
1971   if (TraceMetadataHumongousAllocation &&
1972       SpaceManager::is_humongous(word_size)) {
1973     gclog_or_tty->print_cr("Metadata humongous allocation:");
1974     gclog_or_tty->print_cr("  word_size " PTR_FORMAT, word_size);
1975     gclog_or_tty->print_cr("  chunk_word_size " PTR_FORMAT,
1976                            chunk_word_size);
1977     gclog_or_tty->print_cr("    chunk overhead " PTR_FORMAT,
1978                            Metachunk::overhead());
1979   }
1980   return chunk_word_size;
1981 }
1982 
1983 MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
1984   assert(vs_list()->current_virtual_space() != NULL,
1985          "Should have been set");
1986   assert(current_chunk() == NULL ||
1987          current_chunk()->allocate(word_size) == NULL,
1988          "Don't need to expand");
1989   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
1990 
1991   if (TraceMetadataChunkAllocation && Verbose) {
1992     size_t words_left = 0;
1993     size_t words_used = 0;
1994     if (current_chunk() != NULL) {
1995       words_left = current_chunk()->free_word_size();
1996       words_used = current_chunk()->used_word_size();
1997     }
1998     gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT
1999                            " words " SIZE_FORMAT " words used " SIZE_FORMAT
2000                            " words left",
2001                             word_size, words_used, words_left);
2002   }
2003 
2004   // Get another chunk out of the virtual space
2005   size_t grow_chunks_by_words = calc_chunk_size(word_size);
2006   Metachunk* next = get_new_chunk(word_size, grow_chunks_by_words);
2007 
2008   // If a chunk was available, add it to the in-use chunk list
2009   // and do an allocation from it.
2010   if (next != NULL) {
2011     Metadebug::deallocate_chunk_a_lot(this, grow_chunks_by_words);
2012     // Add to this manager's list of chunks in use.
2013     add_chunk(next, false);
2014     return next->allocate(word_size);
2015   }
2016   return NULL;
2017 }
2018 
2019 void SpaceManager::print_on(outputStream* st) const {
2020 
2021   for (ChunkIndex i = ZeroIndex;
2022        i < NumberOfInUseLists ;
2023        i = next_chunk_index(i) ) {
2024     st->print_cr("  chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
2025                  chunks_in_use(i),
2026                  chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
2027   }
2028   st->print_cr("    waste:  Small " SIZE_FORMAT " Medium " SIZE_FORMAT
2029                " Humongous " SIZE_FORMAT,
2030                sum_waste_in_chunks_in_use(SmallIndex),
2031                sum_waste_in_chunks_in_use(MediumIndex),
2032                sum_waste_in_chunks_in_use(HumongousIndex));
2033   // block free lists
2034   if (block_freelists() != NULL) {
2035     st->print_cr("total in block free lists " SIZE_FORMAT,
2036       block_freelists()->total_size());
2037   }
2038 }
2039 
2040 SpaceManager::SpaceManager(Metaspace::MetadataType mdtype,
2041                            Mutex* lock,
2042                            VirtualSpaceList* vs_list) :
2043   _vs_list(vs_list),
2044   _mdtype(mdtype),
2045   _allocated_blocks_words(0),
2046   _allocated_chunks_words(0),
2047   _allocated_chunks_count(0),
2048   _lock(lock)
2049 {
2050   initialize();
2051 }
2052 
2053 void SpaceManager::inc_size_metrics(size_t words) {
2054   assert_lock_strong(SpaceManager::expand_lock());
2055   // Total of allocated Metachunks and allocated Metachunks count
2056   // for each SpaceManager
2057   _allocated_chunks_words = _allocated_chunks_words + words;
2058   _allocated_chunks_count++;
2059   // Global total of capacity in allocated Metachunks
2060   MetaspaceAux::inc_capacity(mdtype(), words);
2061   // Global total of allocated Metablocks.
2062   // used_words_slow() includes the overhead in each
2063   // Metachunk so include it in the used when the
2064   // Metachunk is first added (so only added once per
2065   // Metachunk).
2066   MetaspaceAux::inc_used(mdtype(), Metachunk::overhead());
2067 }
2068 
2069 void SpaceManager::inc_used_metrics(size_t words) {
2070   // Add to the per SpaceManager total
2071   Atomic::add_ptr(words, &_allocated_blocks_words);
2072   // Add to the global total
2073   MetaspaceAux::inc_used(mdtype(), words);
2074 }
2075 
2076 void SpaceManager::dec_total_from_size_metrics() {
2077   MetaspaceAux::dec_capacity(mdtype(), allocated_chunks_words());
2078   MetaspaceAux::dec_used(mdtype(), allocated_blocks_words());
2079   // Also deduct the overhead per Metachunk
2080   MetaspaceAux::dec_used(mdtype(), allocated_chunks_count() * Metachunk::overhead());
2081 }
2082 
2083 void SpaceManager::initialize() {
2084   Metadebug::init_allocation_fail_alot_count();
2085   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2086     _chunks_in_use[i] = NULL;
2087   }
2088   _current_chunk = NULL;
2089   if (TraceMetadataChunkAllocation && Verbose) {
2090     gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this);
2091   }
2092 }
2093 
2094 void ChunkManager::return_chunks(ChunkIndex index, Metachunk* chunks) {
2095   if (chunks == NULL) {
2096     return;
2097   }
2098   ChunkList* list = free_chunks(index);
2099   assert(list->size() == chunks->word_size(), "Mismatch in chunk sizes");
2100   assert_lock_strong(SpaceManager::expand_lock());
2101   Metachunk* cur = chunks;
2102 
2103   // This returns chunks one at a time.  If a new
2104   // class List can be created that is a base class
2105   // of FreeList then something like FreeList::prepend()
2106   // can be used in place of this loop
2107   while (cur != NULL) {
2108     assert(cur->container() != NULL, "Container should have been set");
2109     cur->container()->dec_container_count();
2110     // Capture the next link before it is changed
2111     // by the call to return_chunk_at_head();
2112     Metachunk* next = cur->next();
2113     cur->set_is_free(true);
2114     list->return_chunk_at_head(cur);
2115     cur = next;
2116   }
2117 }
2118 
2119 SpaceManager::~SpaceManager() {
2120   // This call this->_lock which can't be done while holding expand_lock()
2121   assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(),
2122     err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT
2123             " allocated_chunks_words() " SIZE_FORMAT,
2124             sum_capacity_in_chunks_in_use(), allocated_chunks_words()));
2125 
2126   MutexLockerEx fcl(SpaceManager::expand_lock(),
2127                     Mutex::_no_safepoint_check_flag);
2128 
2129   ChunkManager* chunk_manager = vs_list()->chunk_manager();
2130 
2131   chunk_manager->slow_locked_verify();
2132 
2133   dec_total_from_size_metrics();
2134 
2135   if (TraceMetadataChunkAllocation && Verbose) {
2136     gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
2137     locked_print_chunks_in_use_on(gclog_or_tty);
2138   }
2139 
2140   // Do not mangle freed Metachunks.  The chunk size inside Metachunks
2141   // is during the freeing of a VirtualSpaceNodes.
2142 
2143   // Have to update before the chunks_in_use lists are emptied
2144   // below.
2145   chunk_manager->inc_free_chunks_total(allocated_chunks_words(),
2146                                        sum_count_in_chunks_in_use());
2147 
2148   // Add all the chunks in use by this space manager
2149   // to the global list of free chunks.
2150 
2151   // Follow each list of chunks-in-use and add them to the
2152   // free lists.  Each list is NULL terminated.
2153 
2154   for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) {
2155     if (TraceMetadataChunkAllocation && Verbose) {
2156       gclog_or_tty->print_cr("returned %d %s chunks to freelist",
2157                              sum_count_in_chunks_in_use(i),
2158                              chunk_size_name(i));
2159     }
2160     Metachunk* chunks = chunks_in_use(i);
2161     chunk_manager->return_chunks(i, chunks);
2162     set_chunks_in_use(i, NULL);
2163     if (TraceMetadataChunkAllocation && Verbose) {
2164       gclog_or_tty->print_cr("updated freelist count %d %s",
2165                              chunk_manager->free_chunks(i)->count(),
2166                              chunk_size_name(i));
2167     }
2168     assert(i != HumongousIndex, "Humongous chunks are handled explicitly later");
2169   }
2170 
2171   // The medium chunk case may be optimized by passing the head and
2172   // tail of the medium chunk list to add_at_head().  The tail is often
2173   // the current chunk but there are probably exceptions.
2174 
2175   // Humongous chunks
2176   if (TraceMetadataChunkAllocation && Verbose) {
2177     gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary",
2178                             sum_count_in_chunks_in_use(HumongousIndex),
2179                             chunk_size_name(HumongousIndex));
2180     gclog_or_tty->print("Humongous chunk dictionary: ");
2181   }
2182   // Humongous chunks are never the current chunk.
2183   Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
2184 
2185   while (humongous_chunks != NULL) {
2186 #ifdef ASSERT
2187     humongous_chunks->set_is_free(true);
2188 #endif
2189     if (TraceMetadataChunkAllocation && Verbose) {
2190       gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ",
2191                           humongous_chunks,
2192                           humongous_chunks->word_size());
2193     }
2194     assert(humongous_chunks->word_size() == (size_t)
2195            align_size_up(humongous_chunks->word_size(),
2196                              HumongousChunkGranularity),
2197            err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT
2198                    " granularity %d",
2199                    humongous_chunks->word_size(), HumongousChunkGranularity));
2200     Metachunk* next_humongous_chunks = humongous_chunks->next();
2201     humongous_chunks->container()->dec_container_count();
2202     chunk_manager->humongous_dictionary()->return_chunk(humongous_chunks);
2203     humongous_chunks = next_humongous_chunks;
2204   }
2205   if (TraceMetadataChunkAllocation && Verbose) {
2206     gclog_or_tty->print_cr("");
2207     gclog_or_tty->print_cr("updated dictionary count %d %s",
2208                      chunk_manager->humongous_dictionary()->total_count(),
2209                      chunk_size_name(HumongousIndex));
2210   }
2211   chunk_manager->slow_locked_verify();
2212 }
2213 
2214 const char* SpaceManager::chunk_size_name(ChunkIndex index) const {
2215   switch (index) {
2216     case SpecializedIndex:
2217       return "Specialized";
2218     case SmallIndex:
2219       return "Small";
2220     case MediumIndex:
2221       return "Medium";
2222     case HumongousIndex:
2223       return "Humongous";
2224     default:
2225       return NULL;
2226   }
2227 }
2228 
2229 ChunkIndex ChunkManager::list_index(size_t size) {
2230   switch (size) {
2231     case SpecializedChunk:
2232       assert(SpecializedChunk == ClassSpecializedChunk,
2233              "Need branch for ClassSpecializedChunk");
2234       return SpecializedIndex;
2235     case SmallChunk:
2236     case ClassSmallChunk:
2237       return SmallIndex;
2238     case MediumChunk:
2239     case ClassMediumChunk:
2240       return MediumIndex;
2241     default:
2242       assert(size > MediumChunk || size > ClassMediumChunk,
2243              "Not a humongous chunk");
2244       return HumongousIndex;
2245   }
2246 }
2247 
2248 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
2249   assert_lock_strong(_lock);
2250   size_t min_size = TreeChunk<Metablock, FreeList>::min_size();
2251   assert(word_size >= min_size,
2252     err_msg("Should not deallocate dark matter " SIZE_FORMAT, word_size));
2253   block_freelists()->return_block(p, word_size);
2254 }
2255 
2256 // Adds a chunk to the list of chunks in use.
2257 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
2258 
2259   assert(new_chunk != NULL, "Should not be NULL");
2260   assert(new_chunk->next() == NULL, "Should not be on a list");
2261 
2262   new_chunk->reset_empty();
2263 
2264   // Find the correct list and and set the current
2265   // chunk for that list.
2266   ChunkIndex index = ChunkManager::list_index(new_chunk->word_size());
2267 
2268   if (index != HumongousIndex) {
2269     set_current_chunk(new_chunk);
2270     new_chunk->set_next(chunks_in_use(index));
2271     set_chunks_in_use(index, new_chunk);
2272   } else {
2273     // For null class loader data and DumpSharedSpaces, the first chunk isn't
2274     // small, so small will be null.  Link this first chunk as the current
2275     // chunk.
2276     if (make_current) {
2277       // Set as the current chunk but otherwise treat as a humongous chunk.
2278       set_current_chunk(new_chunk);
2279     }
2280     // Link at head.  The _current_chunk only points to a humongous chunk for
2281     // the null class loader metaspace (class and data virtual space managers)
2282     // any humongous chunks so will not point to the tail
2283     // of the humongous chunks list.
2284     new_chunk->set_next(chunks_in_use(HumongousIndex));
2285     set_chunks_in_use(HumongousIndex, new_chunk);
2286 
2287     assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
2288   }
2289 
2290   // Add to the running sum of capacity
2291   inc_size_metrics(new_chunk->word_size());
2292 
2293   assert(new_chunk->is_empty(), "Not ready for reuse");
2294   if (TraceMetadataChunkAllocation && Verbose) {
2295     gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
2296                         sum_count_in_chunks_in_use());
2297     new_chunk->print_on(gclog_or_tty);
2298     if (vs_list() != NULL) {
2299       vs_list()->chunk_manager()->locked_print_free_chunks(tty);
2300     }
2301   }
2302 }
2303 
2304 Metachunk* SpaceManager::get_new_chunk(size_t word_size,
2305                                        size_t grow_chunks_by_words) {
2306 
2307   Metachunk* next = vs_list()->get_new_chunk(word_size,
2308                                              grow_chunks_by_words,
2309                                              medium_chunk_bunch());
2310 
2311   if (TraceMetadataHumongousAllocation &&
2312       SpaceManager::is_humongous(next->word_size())) {
2313     gclog_or_tty->print_cr("  new humongous chunk word size " PTR_FORMAT,
2314                            next->word_size());
2315   }
2316 
2317   return next;
2318 }
2319 
2320 MetaWord* SpaceManager::allocate(size_t word_size) {
2321   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2322 
2323   // If only the dictionary is going to be used (i.e., no
2324   // indexed free list), then there is a minimum size requirement.
2325   // MinChunkSize is a placeholder for the real minimum size JJJ
2326   size_t byte_size = word_size * BytesPerWord;
2327 
2328   size_t byte_size_with_overhead = byte_size + Metablock::overhead();
2329 
2330   size_t raw_bytes_size = MAX2(byte_size_with_overhead,
2331                                Metablock::min_block_byte_size());
2332   raw_bytes_size = ARENA_ALIGN(raw_bytes_size);
2333   size_t raw_word_size = raw_bytes_size / BytesPerWord;
2334   assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
2335 
2336   BlockFreelist* fl =  block_freelists();
2337   MetaWord* p = NULL;
2338   // Allocation from the dictionary is expensive in the sense that
2339   // the dictionary has to be searched for a size.  Don't allocate
2340   // from the dictionary until it starts to get fat.  Is this
2341   // a reasonable policy?  Maybe an skinny dictionary is fast enough
2342   // for allocations.  Do some profiling.  JJJ
2343   if (fl->total_size() > allocation_from_dictionary_limit) {
2344     p = fl->get_block(raw_word_size);
2345   }
2346   if (p == NULL) {
2347     p = allocate_work(raw_word_size);
2348   }
2349   Metadebug::deallocate_block_a_lot(this, raw_word_size);
2350 
2351   return p;
2352 }
2353 
2354 // Returns the address of spaced allocated for "word_size".
2355 // This methods does not know about blocks (Metablocks)
2356 MetaWord* SpaceManager::allocate_work(size_t word_size) {
2357   assert_lock_strong(_lock);
2358 #ifdef ASSERT
2359   if (Metadebug::test_metadata_failure()) {
2360     return NULL;
2361   }
2362 #endif
2363   // Is there space in the current chunk?
2364   MetaWord* result = NULL;
2365 
2366   // For DumpSharedSpaces, only allocate out of the current chunk which is
2367   // never null because we gave it the size we wanted.   Caller reports out
2368   // of memory if this returns null.
2369   if (DumpSharedSpaces) {
2370     assert(current_chunk() != NULL, "should never happen");
2371     inc_used_metrics(word_size);
2372     return current_chunk()->allocate(word_size); // caller handles null result
2373   }
2374   if (current_chunk() != NULL) {
2375     result = current_chunk()->allocate(word_size);
2376   }
2377 
2378   if (result == NULL) {
2379     result = grow_and_allocate(word_size);
2380   }
2381   if (result > 0) {
2382     inc_used_metrics(word_size);
2383     assert(result != (MetaWord*) chunks_in_use(MediumIndex),
2384            "Head of the list is being allocated");
2385   }
2386 
2387   return result;
2388 }
2389 
2390 void SpaceManager::verify() {
2391   // If there are blocks in the dictionary, then
2392   // verfication of chunks does not work since
2393   // being in the dictionary alters a chunk.
2394   if (block_freelists()->total_size() == 0) {
2395     for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2396       Metachunk* curr = chunks_in_use(i);
2397       while (curr != NULL) {
2398         curr->verify();
2399         verify_chunk_size(curr);
2400         curr = curr->next();
2401       }
2402     }
2403   }
2404 }
2405 
2406 void SpaceManager::verify_chunk_size(Metachunk* chunk) {
2407   assert(is_humongous(chunk->word_size()) ||
2408          chunk->word_size() == medium_chunk_size() ||
2409          chunk->word_size() == small_chunk_size() ||
2410          chunk->word_size() == specialized_chunk_size(),
2411          "Chunk size is wrong");
2412   return;
2413 }
2414 
2415 #ifdef ASSERT
2416 void SpaceManager::verify_allocated_blocks_words() {
2417   // Verification is only guaranteed at a safepoint.
2418   assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(),
2419     "Verification can fail if the applications is running");
2420   assert(allocated_blocks_words() == sum_used_in_chunks_in_use(),
2421     err_msg("allocation total is not consistent " SIZE_FORMAT
2422             " vs " SIZE_FORMAT,
2423             allocated_blocks_words(), sum_used_in_chunks_in_use()));
2424 }
2425 
2426 #endif
2427 
2428 void SpaceManager::dump(outputStream* const out) const {
2429   size_t curr_total = 0;
2430   size_t waste = 0;
2431   uint i = 0;
2432   size_t used = 0;
2433   size_t capacity = 0;
2434 
2435   // Add up statistics for all chunks in this SpaceManager.
2436   for (ChunkIndex index = ZeroIndex;
2437        index < NumberOfInUseLists;
2438        index = next_chunk_index(index)) {
2439     for (Metachunk* curr = chunks_in_use(index);
2440          curr != NULL;
2441          curr = curr->next()) {
2442       out->print("%d) ", i++);
2443       curr->print_on(out);
2444       if (TraceMetadataChunkAllocation && Verbose) {
2445         block_freelists()->print_on(out);
2446       }
2447       curr_total += curr->word_size();
2448       used += curr->used_word_size();
2449       capacity += curr->capacity_word_size();
2450       waste += curr->free_word_size() + curr->overhead();;
2451     }
2452   }
2453 
2454   size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size();
2455   // Free space isn't wasted.
2456   waste -= free;
2457 
2458   out->print_cr("total of all chunks "  SIZE_FORMAT " used " SIZE_FORMAT
2459                 " free " SIZE_FORMAT " capacity " SIZE_FORMAT
2460                 " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste);
2461 }
2462 
2463 #ifndef PRODUCT
2464 void SpaceManager::mangle_freed_chunks() {
2465   for (ChunkIndex index = ZeroIndex;
2466        index < NumberOfInUseLists;
2467        index = next_chunk_index(index)) {
2468     for (Metachunk* curr = chunks_in_use(index);
2469          curr != NULL;
2470          curr = curr->next()) {
2471       curr->mangle();
2472     }
2473   }
2474 }
2475 #endif // PRODUCT
2476 
2477 // MetaspaceAux
2478 
2479 
2480 size_t MetaspaceAux::_allocated_capacity_words[] = {0, 0};
2481 size_t MetaspaceAux::_allocated_used_words[] = {0, 0};
2482 
2483 size_t MetaspaceAux::free_bytes() {
2484   size_t result = 0;
2485   if (Metaspace::class_space_list() != NULL) {
2486     result = result + Metaspace::class_space_list()->free_bytes();
2487   }
2488   if (Metaspace::space_list() != NULL) {
2489     result = result + Metaspace::space_list()->free_bytes();
2490   }
2491   return result;
2492 }
2493 
2494 void MetaspaceAux::dec_capacity(Metaspace::MetadataType mdtype, size_t words) {
2495   assert_lock_strong(SpaceManager::expand_lock());
2496   assert(words <= allocated_capacity_words(mdtype),
2497     err_msg("About to decrement below 0: words " SIZE_FORMAT
2498             " is greater than _allocated_capacity_words[%u] " SIZE_FORMAT,
2499             words, mdtype, allocated_capacity_words(mdtype)));
2500   _allocated_capacity_words[mdtype] -= words;
2501 }
2502 
2503 void MetaspaceAux::inc_capacity(Metaspace::MetadataType mdtype, size_t words) {
2504   assert_lock_strong(SpaceManager::expand_lock());
2505   // Needs to be atomic
2506   _allocated_capacity_words[mdtype] += words;
2507 }
2508 
2509 void MetaspaceAux::dec_used(Metaspace::MetadataType mdtype, size_t words) {
2510   assert(words <= allocated_used_words(mdtype),
2511     err_msg("About to decrement below 0: words " SIZE_FORMAT
2512             " is greater than _allocated_used_words[%u] " SIZE_FORMAT,
2513             words, mdtype, allocated_used_words(mdtype)));
2514   // For CMS deallocation of the Metaspaces occurs during the
2515   // sweep which is a concurrent phase.  Protection by the expand_lock()
2516   // is not enough since allocation is on a per Metaspace basis
2517   // and protected by the Metaspace lock.
2518   jlong minus_words = (jlong) - (jlong) words;
2519   Atomic::add_ptr(minus_words, &_allocated_used_words[mdtype]);
2520 }
2521 
2522 void MetaspaceAux::inc_used(Metaspace::MetadataType mdtype, size_t words) {
2523   // _allocated_used_words tracks allocations for
2524   // each piece of metadata.  Those allocations are
2525   // generally done concurrently by different application
2526   // threads so must be done atomically.
2527   Atomic::add_ptr(words, &_allocated_used_words[mdtype]);
2528 }
2529 
2530 size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) {
2531   size_t used = 0;
2532   ClassLoaderDataGraphMetaspaceIterator iter;
2533   while (iter.repeat()) {
2534     Metaspace* msp = iter.get_next();
2535     // Sum allocated_blocks_words for each metaspace
2536     if (msp != NULL) {
2537       used += msp->used_words_slow(mdtype);
2538     }
2539   }
2540   return used * BytesPerWord;
2541 }
2542 
2543 size_t MetaspaceAux::free_in_bytes(Metaspace::MetadataType mdtype) {
2544   size_t free = 0;
2545   ClassLoaderDataGraphMetaspaceIterator iter;
2546   while (iter.repeat()) {
2547     Metaspace* msp = iter.get_next();
2548     if (msp != NULL) {
2549       free += msp->free_words(mdtype);
2550     }
2551   }
2552   return free * BytesPerWord;
2553 }
2554 
2555 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) {
2556   // Don't count the space in the freelists.  That space will be
2557   // added to the capacity calculation as needed.
2558   size_t capacity = 0;
2559   ClassLoaderDataGraphMetaspaceIterator iter;
2560   while (iter.repeat()) {
2561     Metaspace* msp = iter.get_next();
2562     if (msp != NULL) {
2563       capacity += msp->capacity_words_slow(mdtype);
2564     }
2565   }
2566   return capacity * BytesPerWord;
2567 }
2568 
2569 size_t MetaspaceAux::reserved_in_bytes(Metaspace::MetadataType mdtype) {
2570   size_t reserved = (mdtype == Metaspace::ClassType) ?
2571                        Metaspace::class_space_list()->virtual_space_total() :
2572                        Metaspace::space_list()->virtual_space_total();
2573   return reserved * BytesPerWord;
2574 }
2575 
2576 size_t MetaspaceAux::min_chunk_size() { return Metaspace::first_chunk_word_size(); }
2577 
2578 size_t MetaspaceAux::free_chunks_total(Metaspace::MetadataType mdtype) {
2579   ChunkManager* chunk = (mdtype == Metaspace::ClassType) ?
2580                             Metaspace::class_space_list()->chunk_manager() :
2581                             Metaspace::space_list()->chunk_manager();
2582   chunk->slow_verify();
2583   return chunk->free_chunks_total();
2584 }
2585 
2586 size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) {
2587   return free_chunks_total(mdtype) * BytesPerWord;
2588 }
2589 
2590 size_t MetaspaceAux::free_chunks_total() {
2591   return free_chunks_total(Metaspace::ClassType) +
2592          free_chunks_total(Metaspace::NonClassType);
2593 }
2594 
2595 size_t MetaspaceAux::free_chunks_total_in_bytes() {
2596   return free_chunks_total() * BytesPerWord;
2597 }
2598 
2599 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
2600   gclog_or_tty->print(", [Metaspace:");
2601   if (PrintGCDetails && Verbose) {
2602     gclog_or_tty->print(" "  SIZE_FORMAT
2603                         "->" SIZE_FORMAT
2604                         "("  SIZE_FORMAT ")",
2605                         prev_metadata_used,
2606                         allocated_capacity_bytes(),
2607                         reserved_in_bytes());
2608   } else {
2609     gclog_or_tty->print(" "  SIZE_FORMAT "K"
2610                         "->" SIZE_FORMAT "K"
2611                         "("  SIZE_FORMAT "K)",
2612                         prev_metadata_used / K,
2613                         allocated_capacity_bytes() / K,
2614                         reserved_in_bytes()/ K);
2615   }
2616 
2617   gclog_or_tty->print("]");
2618 }
2619 
2620 // This is printed when PrintGCDetails
2621 void MetaspaceAux::print_on(outputStream* out) {
2622   Metaspace::MetadataType ct = Metaspace::ClassType;
2623   Metaspace::MetadataType nct = Metaspace::NonClassType;
2624 
2625   out->print_cr(" Metaspace total "
2626                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
2627                 " reserved " SIZE_FORMAT "K",
2628                 allocated_capacity_bytes()/K, allocated_used_bytes()/K, reserved_in_bytes()/K);
2629 
2630   out->print_cr("  data space     "
2631                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
2632                 " reserved " SIZE_FORMAT "K",
2633                 allocated_capacity_bytes(nct)/K,
2634                 allocated_used_bytes(nct)/K,
2635                 reserved_in_bytes(nct)/K);
2636   out->print_cr("  class space    "
2637                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
2638                 " reserved " SIZE_FORMAT "K",
2639                 allocated_capacity_bytes(ct)/K,
2640                 allocated_used_bytes(ct)/K,
2641                 reserved_in_bytes(ct)/K);
2642 }
2643 
2644 // Print information for class space and data space separately.
2645 // This is almost the same as above.
2646 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
2647   size_t free_chunks_capacity_bytes = free_chunks_total_in_bytes(mdtype);
2648   size_t capacity_bytes = capacity_bytes_slow(mdtype);
2649   size_t used_bytes = used_bytes_slow(mdtype);
2650   size_t free_bytes = free_in_bytes(mdtype);
2651   size_t used_and_free = used_bytes + free_bytes +
2652                            free_chunks_capacity_bytes;
2653   out->print_cr("  Chunk accounting: used in chunks " SIZE_FORMAT
2654              "K + unused in chunks " SIZE_FORMAT "K  + "
2655              " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT
2656              "K  capacity in allocated chunks " SIZE_FORMAT "K",
2657              used_bytes / K,
2658              free_bytes / K,
2659              free_chunks_capacity_bytes / K,
2660              used_and_free / K,
2661              capacity_bytes / K);
2662   // Accounting can only be correct if we got the values during a safepoint
2663   assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
2664 }
2665 
2666 // Print total fragmentation for class and data metaspaces separately
2667 void MetaspaceAux::print_waste(outputStream* out) {
2668 
2669   size_t specialized_waste = 0, small_waste = 0, medium_waste = 0, large_waste = 0;
2670   size_t specialized_count = 0, small_count = 0, medium_count = 0, large_count = 0;
2671   size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0, cls_large_waste = 0;
2672   size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_large_count = 0;
2673 
2674   ClassLoaderDataGraphMetaspaceIterator iter;
2675   while (iter.repeat()) {
2676     Metaspace* msp = iter.get_next();
2677     if (msp != NULL) {
2678       specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
2679       specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
2680       small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
2681       small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex);
2682       medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
2683       medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex);
2684       large_waste += msp->vsm()->sum_waste_in_chunks_in_use(HumongousIndex);
2685       large_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex);
2686 
2687       cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
2688       cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
2689       cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
2690       cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
2691       cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
2692       cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
2693       cls_large_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(HumongousIndex);
2694       cls_large_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
2695     }
2696   }
2697   out->print_cr("Total fragmentation waste (words) doesn't count free space");
2698   out->print_cr("  data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
2699                         SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
2700                         SIZE_FORMAT " medium(s) " SIZE_FORMAT,
2701              specialized_count, specialized_waste, small_count,
2702              small_waste, medium_count, medium_waste);
2703   out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
2704                            SIZE_FORMAT " small(s) " SIZE_FORMAT,
2705              cls_specialized_count, cls_specialized_waste,
2706              cls_small_count, cls_small_waste);
2707 }
2708 
2709 // Dump global metaspace things from the end of ClassLoaderDataGraph
2710 void MetaspaceAux::dump(outputStream* out) {
2711   out->print_cr("All Metaspace:");
2712   out->print("data space: "); print_on(out, Metaspace::NonClassType);
2713   out->print("class space: "); print_on(out, Metaspace::ClassType);
2714   print_waste(out);
2715 }
2716 
2717 void MetaspaceAux::verify_free_chunks() {
2718   Metaspace::space_list()->chunk_manager()->verify();
2719   Metaspace::class_space_list()->chunk_manager()->verify();
2720 }
2721 
2722 void MetaspaceAux::verify_capacity() {
2723 #ifdef ASSERT
2724   size_t running_sum_capacity_bytes = allocated_capacity_bytes();
2725   // For purposes of the running sum of capacity, verify against capacity
2726   size_t capacity_in_use_bytes = capacity_bytes_slow();
2727   assert(running_sum_capacity_bytes == capacity_in_use_bytes,
2728     err_msg("allocated_capacity_words() * BytesPerWord " SIZE_FORMAT
2729             " capacity_bytes_slow()" SIZE_FORMAT,
2730             running_sum_capacity_bytes, capacity_in_use_bytes));
2731   for (Metaspace::MetadataType i = Metaspace::ClassType;
2732        i < Metaspace:: MetadataTypeCount;
2733        i = (Metaspace::MetadataType)(i + 1)) {
2734     size_t capacity_in_use_bytes = capacity_bytes_slow(i);
2735     assert(allocated_capacity_bytes(i) == capacity_in_use_bytes,
2736       err_msg("allocated_capacity_bytes(%u) " SIZE_FORMAT
2737               " capacity_bytes_slow(%u)" SIZE_FORMAT,
2738               i, allocated_capacity_bytes(i), i, capacity_in_use_bytes));
2739   }
2740 #endif
2741 }
2742 
2743 void MetaspaceAux::verify_used() {
2744 #ifdef ASSERT
2745   size_t running_sum_used_bytes = allocated_used_bytes();
2746   // For purposes of the running sum of used, verify against used
2747   size_t used_in_use_bytes = used_bytes_slow();
2748   assert(allocated_used_bytes() == used_in_use_bytes,
2749     err_msg("allocated_used_bytes() " SIZE_FORMAT
2750             " used_bytes_slow()" SIZE_FORMAT,
2751             allocated_used_bytes(), used_in_use_bytes));
2752   for (Metaspace::MetadataType i = Metaspace::ClassType;
2753        i < Metaspace:: MetadataTypeCount;
2754        i = (Metaspace::MetadataType)(i + 1)) {
2755     size_t used_in_use_bytes = used_bytes_slow(i);
2756     assert(allocated_used_bytes(i) == used_in_use_bytes,
2757       err_msg("allocated_used_bytes(%u) " SIZE_FORMAT
2758               " used_bytes_slow(%u)" SIZE_FORMAT,
2759               i, allocated_used_bytes(i), i, used_in_use_bytes));
2760   }
2761 #endif
2762 }
2763 
2764 void MetaspaceAux::verify_metrics() {
2765   verify_capacity();
2766   verify_used();
2767 }
2768 
2769 
2770 // Metaspace methods
2771 
2772 size_t Metaspace::_first_chunk_word_size = 0;
2773 size_t Metaspace::_first_class_chunk_word_size = 0;
2774 
2775 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
2776   initialize(lock, type);
2777 }
2778 
2779 Metaspace::~Metaspace() {
2780   delete _vsm;
2781   delete _class_vsm;
2782 }
2783 
2784 VirtualSpaceList* Metaspace::_space_list = NULL;
2785 VirtualSpaceList* Metaspace::_class_space_list = NULL;
2786 
2787 #define VIRTUALSPACEMULTIPLIER 2
2788 
2789 void Metaspace::global_initialize() {
2790   // Initialize the alignment for shared spaces.
2791   int max_alignment = os::vm_page_size();
2792   MetaspaceShared::set_max_alignment(max_alignment);
2793 
2794   if (DumpSharedSpaces) {
2795     SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
2796     SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
2797     SharedMiscDataSize  = align_size_up(SharedMiscDataSize, max_alignment);
2798     SharedMiscCodeSize  = align_size_up(SharedMiscCodeSize, max_alignment);
2799 
2800     // Initialize with the sum of the shared space sizes.  The read-only
2801     // and read write metaspace chunks will be allocated out of this and the
2802     // remainder is the misc code and data chunks.
2803     size_t total = align_size_up(SharedReadOnlySize + SharedReadWriteSize +
2804                                  SharedMiscDataSize + SharedMiscCodeSize,
2805                                  os::vm_allocation_granularity());
2806     size_t word_size = total/wordSize;
2807     _space_list = new VirtualSpaceList(word_size);
2808   } else {
2809     // If using shared space, open the file that contains the shared space
2810     // and map in the memory before initializing the rest of metaspace (so
2811     // the addresses don't conflict)
2812     if (UseSharedSpaces) {
2813       FileMapInfo* mapinfo = new FileMapInfo();
2814       memset(mapinfo, 0, sizeof(FileMapInfo));
2815 
2816       // Open the shared archive file, read and validate the header. If
2817       // initialization fails, shared spaces [UseSharedSpaces] are
2818       // disabled and the file is closed.
2819       // Map in spaces now also
2820       if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) {
2821         FileMapInfo::set_current_info(mapinfo);
2822       } else {
2823         assert(!mapinfo->is_open() && !UseSharedSpaces,
2824                "archive file not closed or shared spaces not disabled.");
2825       }
2826     }
2827 
2828     // Initialize these before initializing the VirtualSpaceList
2829     _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
2830     _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
2831     // Make the first class chunk bigger than a medium chunk so it's not put
2832     // on the medium chunk list.   The next chunk will be small and progress
2833     // from there.  This size calculated by -version.
2834     _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
2835                                        (ClassMetaspaceSize/BytesPerWord)*2);
2836     _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
2837     // Arbitrarily set the initial virtual space to a multiple
2838     // of the boot class loader size.
2839     size_t word_size = VIRTUALSPACEMULTIPLIER * first_chunk_word_size();
2840     // Initialize the list of virtual spaces.
2841     _space_list = new VirtualSpaceList(word_size);
2842   }
2843 }
2844 
2845 // For UseCompressedKlassPointers the class space is reserved as a piece of the
2846 // Java heap because the compression algorithm is the same for each.  The
2847 // argument passed in is at the top of the compressed space
2848 void Metaspace::initialize_class_space(ReservedSpace rs) {
2849   // The reserved space size may be bigger because of alignment, esp with UseLargePages
2850   assert(rs.size() >= ClassMetaspaceSize,
2851          err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), ClassMetaspaceSize));
2852   _class_space_list = new VirtualSpaceList(rs);
2853 }
2854 
2855 void Metaspace::initialize(Mutex* lock,
2856                            MetaspaceType type) {
2857 
2858   assert(space_list() != NULL,
2859     "Metadata VirtualSpaceList has not been initialized");
2860 
2861   _vsm = new SpaceManager(Metaspace::NonClassType, lock, space_list());
2862   if (_vsm == NULL) {
2863     return;
2864   }
2865   size_t word_size;
2866   size_t class_word_size;
2867   vsm()->get_initial_chunk_sizes(type,
2868                                  &word_size,
2869                                  &class_word_size);
2870 
2871   assert(class_space_list() != NULL,
2872     "Class VirtualSpaceList has not been initialized");
2873 
2874   // Allocate SpaceManager for classes.
2875   _class_vsm = new SpaceManager(Metaspace::ClassType, lock, class_space_list());
2876   if (_class_vsm == NULL) {
2877     return;
2878   }
2879 
2880   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
2881 
2882   // Allocate chunk for metadata objects
2883   Metachunk* new_chunk =
2884      space_list()->get_initialization_chunk(word_size,
2885                                             vsm()->medium_chunk_bunch());
2886   assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks");
2887   if (new_chunk != NULL) {
2888     // Add to this manager's list of chunks in use and current_chunk().
2889     vsm()->add_chunk(new_chunk, true);
2890   }
2891 
2892   // Allocate chunk for class metadata objects
2893   Metachunk* class_chunk =
2894      class_space_list()->get_initialization_chunk(class_word_size,
2895                                                   class_vsm()->medium_chunk_bunch());
2896   if (class_chunk != NULL) {
2897     class_vsm()->add_chunk(class_chunk, true);
2898   }
2899 }
2900 
2901 size_t Metaspace::align_word_size_up(size_t word_size) {
2902   size_t byte_size = word_size * wordSize;
2903   return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
2904 }
2905 
2906 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
2907   // DumpSharedSpaces doesn't use class metadata area (yet)
2908   if (mdtype == ClassType && !DumpSharedSpaces) {
2909     return  class_vsm()->allocate(word_size);
2910   } else {
2911     return  vsm()->allocate(word_size);
2912   }
2913 }
2914 
2915 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) {
2916   MetaWord* result;
2917   MetaspaceGC::set_expand_after_GC(true);
2918   size_t before_inc = MetaspaceGC::capacity_until_GC();
2919   size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size) * BytesPerWord;
2920   MetaspaceGC::inc_capacity_until_GC(delta_bytes);
2921   if (PrintGCDetails && Verbose) {
2922     gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT
2923       " to " SIZE_FORMAT, before_inc, MetaspaceGC::capacity_until_GC());
2924   }
2925 
2926   result = allocate(word_size, mdtype);
2927 
2928   return result;
2929 }
2930 
2931 // Space allocated in the Metaspace.  This may
2932 // be across several metadata virtual spaces.
2933 char* Metaspace::bottom() const {
2934   assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces");
2935   return (char*)vsm()->current_chunk()->bottom();
2936 }
2937 
2938 size_t Metaspace::used_words_slow(MetadataType mdtype) const {
2939   // return vsm()->allocated_used_words();
2940   return mdtype == ClassType ? class_vsm()->sum_used_in_chunks_in_use() :
2941                                vsm()->sum_used_in_chunks_in_use();  // includes overhead!
2942 }
2943 
2944 size_t Metaspace::free_words(MetadataType mdtype) const {
2945   return mdtype == ClassType ? class_vsm()->sum_free_in_chunks_in_use() :
2946                                vsm()->sum_free_in_chunks_in_use();
2947 }
2948 
2949 // Space capacity in the Metaspace.  It includes
2950 // space in the list of chunks from which allocations
2951 // have been made. Don't include space in the global freelist and
2952 // in the space available in the dictionary which
2953 // is already counted in some chunk.
2954 size_t Metaspace::capacity_words_slow(MetadataType mdtype) const {
2955   return mdtype == ClassType ? class_vsm()->sum_capacity_in_chunks_in_use() :
2956                                vsm()->sum_capacity_in_chunks_in_use();
2957 }
2958 
2959 size_t Metaspace::used_bytes_slow(MetadataType mdtype) const {
2960   return used_words_slow(mdtype) * BytesPerWord;
2961 }
2962 
2963 size_t Metaspace::capacity_bytes_slow(MetadataType mdtype) const {
2964   return capacity_words_slow(mdtype) * BytesPerWord;
2965 }
2966 
2967 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
2968   if (SafepointSynchronize::is_at_safepoint()) {
2969     assert(Thread::current()->is_VM_thread(), "should be the VM thread");
2970     // Don't take Heap_lock
2971     MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
2972     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
2973       // Dark matter.  Too small for dictionary.
2974 #ifdef ASSERT
2975       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
2976 #endif
2977       return;
2978     }
2979     if (is_class) {
2980        class_vsm()->deallocate(ptr, word_size);
2981     } else {
2982       vsm()->deallocate(ptr, word_size);
2983     }
2984   } else {
2985     MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
2986 
2987     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
2988       // Dark matter.  Too small for dictionary.
2989 #ifdef ASSERT
2990       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
2991 #endif
2992       return;
2993     }
2994     if (is_class) {
2995       class_vsm()->deallocate(ptr, word_size);
2996     } else {
2997       vsm()->deallocate(ptr, word_size);
2998     }
2999   }
3000 }
3001 
3002 Metablock* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size,
3003                               bool read_only, MetadataType mdtype, TRAPS) {
3004   if (HAS_PENDING_EXCEPTION) {
3005     assert(false, "Should not allocate with exception pending");
3006     return NULL;  // caller does a CHECK_NULL too
3007   }
3008 
3009   // SSS: Should we align the allocations and make sure the sizes are aligned.
3010   MetaWord* result = NULL;
3011 
3012   assert(loader_data != NULL, "Should never pass around a NULL loader_data. "
3013         "ClassLoaderData::the_null_class_loader_data() should have been used.");
3014   // Allocate in metaspaces without taking out a lock, because it deadlocks
3015   // with the SymbolTable_lock.  Dumping is single threaded for now.  We'll have
3016   // to revisit this for application class data sharing.
3017   if (DumpSharedSpaces) {
3018     if (read_only) {
3019       result = loader_data->ro_metaspace()->allocate(word_size, NonClassType);
3020     } else {
3021       result = loader_data->rw_metaspace()->allocate(word_size, NonClassType);
3022     }
3023     if (result == NULL) {
3024       report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite);
3025     }
3026     return Metablock::initialize(result, word_size);
3027   }
3028 
3029   result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
3030 
3031   if (result == NULL) {
3032     // Try to clean out some memory and retry.
3033     result =
3034       Universe::heap()->collector_policy()->satisfy_failed_metadata_allocation(
3035         loader_data, word_size, mdtype);
3036 
3037     // If result is still null, we are out of memory.
3038     if (result == NULL) {
3039       if (Verbose && TraceMetadataChunkAllocation) {
3040         gclog_or_tty->print_cr("Metaspace allocation failed for size "
3041           SIZE_FORMAT, word_size);
3042         if (loader_data->metaspace_or_null() != NULL) loader_data->metaspace_or_null()->dump(gclog_or_tty);
3043         MetaspaceAux::dump(gclog_or_tty);
3044       }
3045       // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
3046       report_java_out_of_memory("Metadata space");
3047 
3048       if (JvmtiExport::should_post_resource_exhausted()) {
3049         JvmtiExport::post_resource_exhausted(
3050             JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
3051             "Metadata space");
3052       }
3053       THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
3054     }
3055   }
3056   return Metablock::initialize(result, word_size);
3057 }
3058 
3059 void Metaspace::purge() {
3060   MutexLockerEx cl(SpaceManager::expand_lock(),
3061                    Mutex::_no_safepoint_check_flag);
3062   space_list()->purge();
3063   class_space_list()->purge();
3064 }
3065 
3066 void Metaspace::print_on(outputStream* out) const {
3067   // Print both class virtual space counts and metaspace.
3068   if (Verbose) {
3069       vsm()->print_on(out);
3070       class_vsm()->print_on(out);
3071   }
3072 }
3073 
3074 bool Metaspace::contains(const void * ptr) {
3075   if (MetaspaceShared::is_in_shared_space(ptr)) {
3076     return true;
3077   }
3078   // This is checked while unlocked.  As long as the virtualspaces are added
3079   // at the end, the pointer will be in one of them.  The virtual spaces
3080   // aren't deleted presently.  When they are, some sort of locking might
3081   // be needed.  Note, locking this can cause inversion problems with the
3082   // caller in MetaspaceObj::is_metadata() function.
3083   return space_list()->contains(ptr) ||
3084          class_space_list()->contains(ptr);
3085 }
3086 
3087 void Metaspace::verify() {
3088   vsm()->verify();
3089   class_vsm()->verify();
3090 }
3091 
3092 void Metaspace::dump(outputStream* const out) const {
3093   out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm());
3094   vsm()->dump(out);
3095   out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm());
3096   class_vsm()->dump(out);
3097 }