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