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