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