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