< prev index next >

src/hotspot/share/memory/metaspace.cpp

Print this page
rev 47755 : 8189864: Provide an ascii map to visualize metaspace fragmentation
Reviewed-by: goetz, coleenp


 475   // to Virtualspace
 476   bool expand_by(size_t min_words, size_t preferred_words);
 477 
 478   // In preparation for deleting this node, remove all the chunks
 479   // in the node from any freelist.
 480   void purge(ChunkManager* chunk_manager);
 481 
 482   // If an allocation doesn't fit in the current node a new node is created.
 483   // Allocate chunks out of the remaining committed space in this node
 484   // to avoid wasting that memory.
 485   // This always adds up because all the chunk sizes are multiples of
 486   // the smallest chunk size.
 487   void retire(ChunkManager* chunk_manager);
 488 
 489 #ifdef ASSERT
 490   // Debug support
 491   void mangle();
 492 #endif
 493 
 494   void print_on(outputStream* st) const;

 495 };
 496 
 497 #define assert_is_aligned(value, alignment)                  \
 498   assert(is_aligned((value), (alignment)),                   \
 499          SIZE_FORMAT_HEX " is not aligned to "               \
 500          SIZE_FORMAT, (size_t)(uintptr_t)value, (alignment))
 501 
 502 // Decide if large pages should be committed when the memory is reserved.
 503 static bool should_commit_large_pages_when_reserving(size_t bytes) {
 504   if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) {
 505     size_t words = bytes / BytesPerWord;
 506     bool is_class = false; // We never reserve large pages for the class space.
 507     if (MetaspaceGC::can_expand(words, is_class) &&
 508         MetaspaceGC::allowed_expansion() >= words) {
 509       return true;
 510     }
 511   }
 512 
 513   return false;
 514 }


 526     assert_is_aligned(_rs.size(), Metaspace::reserve_alignment());
 527 
 528     MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
 529   }
 530 }
 531 
 532 void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
 533   Metachunk* chunk = first_chunk();
 534   Metachunk* invalid_chunk = (Metachunk*) top();
 535   while (chunk < invalid_chunk ) {
 536     assert(chunk->is_tagged_free(), "Should be tagged free");
 537     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 538     chunk_manager->remove_chunk(chunk);
 539     assert(chunk->next() == NULL &&
 540            chunk->prev() == NULL,
 541            "Was not removed from its list");
 542     chunk = (Metachunk*) next;
 543   }
 544 }
 545 
























































































 546 #ifdef ASSERT
 547 uintx VirtualSpaceNode::container_count_slow() {
 548   uintx count = 0;
 549   Metachunk* chunk = first_chunk();
 550   Metachunk* invalid_chunk = (Metachunk*) top();
 551   while (chunk < invalid_chunk ) {
 552     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 553     // Don't count the chunks on the free lists.  Those are
 554     // still part of the VirtualSpaceNode but not currently
 555     // counted.
 556     if (!chunk->is_tagged_free()) {
 557       count++;
 558     }
 559     chunk = (Metachunk*) next;
 560   }
 561   return count;
 562 }
 563 #endif
 564 
 565 // List of VirtualSpaces for metadata allocation.


 632   bool initialization_succeeded() { return _virtual_space_list != NULL; }
 633 
 634   size_t reserved_words()  { return _reserved_words; }
 635   size_t reserved_bytes()  { return reserved_words() * BytesPerWord; }
 636   size_t committed_words() { return _committed_words; }
 637   size_t committed_bytes() { return committed_words() * BytesPerWord; }
 638 
 639   void inc_reserved_words(size_t v);
 640   void dec_reserved_words(size_t v);
 641   void inc_committed_words(size_t v);
 642   void dec_committed_words(size_t v);
 643   void inc_virtual_space_count();
 644   void dec_virtual_space_count();
 645 
 646   bool contains(const void* ptr);
 647 
 648   // Unlink empty VirtualSpaceNodes and free it.
 649   void purge(ChunkManager* chunk_manager);
 650 
 651   void print_on(outputStream* st) const;

 652 
 653   class VirtualSpaceListIterator : public StackObj {
 654     VirtualSpaceNode* _virtual_spaces;
 655    public:
 656     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
 657       _virtual_spaces(virtual_spaces) {}
 658 
 659     bool repeat() {
 660       return _virtual_spaces != NULL;
 661     }
 662 
 663     VirtualSpaceNode* get_next() {
 664       VirtualSpaceNode* result = _virtual_spaces;
 665       if (_virtual_spaces != NULL) {
 666         _virtual_spaces = _virtual_spaces->next();
 667       }
 668       return result;
 669     }
 670   };
 671 };


1444     preferred_word_size = min_word_size;
1445   }
1446 
1447   bool expanded = expand_by(min_word_size, preferred_word_size);
1448   if (expanded) {
1449     next = current_virtual_space()->get_chunk_vs(chunk_word_size);
1450     assert(next != NULL, "The allocation was expected to succeed after the expansion");
1451   }
1452 
1453    return next;
1454 }
1455 
1456 void VirtualSpaceList::print_on(outputStream* st) const {
1457   VirtualSpaceListIterator iter(virtual_space_list());
1458   while (iter.repeat()) {
1459     VirtualSpaceNode* node = iter.get_next();
1460     node->print_on(st);
1461   }
1462 }
1463 












1464 // MetaspaceGC methods
1465 
1466 // VM_CollectForMetadataAllocation is the vm operation used to GC.
1467 // Within the VM operation after the GC the attempt to allocate the metadata
1468 // should succeed.  If the GC did not free enough space for the metaspace
1469 // allocation, the HWM is increased so that another virtualspace will be
1470 // allocated for the metadata.  With perm gen the increase in the perm
1471 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion.  The
1472 // metaspace policy uses those as the small and large steps for the HWM.
1473 //
1474 // After the GC the compute_new_size() for MetaspaceGC is called to
1475 // resize the capacity of the metaspaces.  The current implementation
1476 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
1477 // to resize the Java heap by some GC's.  New flags can be implemented
1478 // if really needed.  MinMetaspaceFreeRatio is used to calculate how much
1479 // free space is desirable in the metaspace capacity to decide how much
1480 // to increase the HWM.  MaxMetaspaceFreeRatio is used to decide how much
1481 // free space is desirable in the metaspace capacity before decreasing
1482 // the HWM.
1483 


1917 
1918     log_trace(gc, metaspace, freelist)("ChunkManager::free_chunks_get: free_list " PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
1919                                        p2i(free_list), p2i(chunk), chunk->word_size());
1920   } else {
1921     chunk = humongous_dictionary()->get_chunk(word_size);
1922 
1923     if (chunk == NULL) {
1924       return NULL;
1925     }
1926 
1927     log_debug(gc, metaspace, alloc)("Free list allocate humongous chunk size " SIZE_FORMAT " for requested size " SIZE_FORMAT " waste " SIZE_FORMAT,
1928                                     chunk->word_size(), word_size, chunk->word_size() - word_size);
1929   }
1930 
1931   // Chunk has been removed from the chunk manager; update counters.
1932   account_for_removed_chunk(chunk);
1933 
1934   // Remove it from the links to this freelist
1935   chunk->set_next(NULL);
1936   chunk->set_prev(NULL);
1937 #ifdef ASSERT
1938   // Chunk is no longer on any freelist. Setting to false make container_count_slow()
1939   // work.
1940   chunk->set_is_tagged_free(false);
1941 #endif
1942   chunk->container()->inc_container_count();
1943 
1944   slow_locked_verify();
1945   return chunk;
1946 }
1947 
1948 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
1949   assert_lock_strong(SpaceManager::expand_lock());
1950   slow_locked_verify();
1951 
1952   // Take from the beginning of the list
1953   Metachunk* chunk = free_chunks_get(word_size);
1954   if (chunk == NULL) {
1955     return NULL;
1956   }
1957 
1958   assert((word_size <= chunk->word_size()) ||
1959          (list_index(chunk->word_size()) == HumongousIndex),
1960          "Non-humongous variable sized chunk");
1961   LogTarget(Debug, gc, metaspace, freelist) lt;


1989   // keeps tree node pointers in the chunk payload area which mangle will overwrite.
1990   NOT_PRODUCT(chunk->mangle(badMetaWordVal);)
1991 
1992   if (index != HumongousIndex) {
1993     // Return non-humongous chunk to freelist.
1994     ChunkList* list = free_chunks(index);
1995     assert(list->size() == chunk->word_size(), "Wrong chunk type.");
1996     list->return_chunk_at_head(chunk);
1997     log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " to freelist.",
1998         chunk_size_name(index), p2i(chunk));
1999   } else {
2000     // Return humongous chunk to dictionary.
2001     assert(chunk->word_size() > free_chunks(MediumIndex)->size(), "Wrong chunk type.");
2002     assert(chunk->word_size() % free_chunks(SpecializedIndex)->size() == 0,
2003            "Humongous chunk has wrong alignment.");
2004     _humongous_dictionary.return_chunk(chunk);
2005     log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " (word size " SIZE_FORMAT ") to freelist.",
2006         chunk_size_name(index), p2i(chunk), chunk->word_size());
2007   }
2008   chunk->container()->dec_container_count();
2009   DEBUG_ONLY(chunk->set_is_tagged_free(true);)
2010 
2011   // Chunk has been added; update counters.
2012   account_for_added_chunk(chunk);
2013 
2014 }
2015 
2016 void ChunkManager::return_chunk_list(ChunkIndex index, Metachunk* chunks) {
2017   index_bounds_check(index);
2018   if (chunks == NULL) {
2019     return;
2020   }
2021   LogTarget(Trace, gc, metaspace, freelist) log;
2022   if (log.is_enabled()) { // tracing
2023     log.print("returning list of %s chunks...", chunk_size_name(index));
2024   }
2025   unsigned num_chunks_returned = 0;
2026   size_t size_chunks_returned = 0;
2027   Metachunk* cur = chunks;
2028   while (cur != NULL) {
2029     // Capture the next link before it is changed


2928                   "used "      SIZE_FORMAT "K, "
2929                   "capacity "  SIZE_FORMAT "K, "
2930                   "committed " SIZE_FORMAT "K, "
2931                   "reserved "  SIZE_FORMAT "K",
2932                   used_bytes(ct)/K,
2933                   capacity_bytes(ct)/K,
2934                   committed_bytes(ct)/K,
2935                   reserved_bytes(ct)/K);
2936   }
2937 }
2938 
2939 // Print information for class space and data space separately.
2940 // This is almost the same as above.
2941 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
2942   size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype);
2943   size_t capacity_bytes = capacity_bytes_slow(mdtype);
2944   size_t used_bytes = used_bytes_slow(mdtype);
2945   size_t free_bytes = free_bytes_slow(mdtype);
2946   size_t used_and_free = used_bytes + free_bytes +
2947                            free_chunks_capacity_bytes;
2948   out->print_cr("  Chunk accounting: used in chunks " SIZE_FORMAT
2949              "K + unused in chunks " SIZE_FORMAT "K  + "
2950              " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT
2951              "K  capacity in allocated chunks " SIZE_FORMAT "K",
2952              used_bytes / K,
2953              free_bytes / K,
2954              free_chunks_capacity_bytes / K,
2955              used_and_free / K,
2956              capacity_bytes / K);
2957   // Accounting can only be correct if we got the values during a safepoint
2958   assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
2959 }
2960 
2961 // Print total fragmentation for class metaspaces
2962 void MetaspaceAux::print_class_waste(outputStream* out) {
2963   assert(Metaspace::using_class_space(), "class metaspace not used");
2964   size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
2965   size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
2966   ClassLoaderDataGraphMetaspaceIterator iter;
2967   while (iter.repeat()) {
2968     Metaspace* msp = iter.get_next();
2969     if (msp != NULL) {
2970       cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);


3194   out->cr();
3195   ChunkManager::print_all_chunkmanagers(out, scale);
3196 
3197   out->cr();
3198   out->print_cr("Per-classloader metadata:");
3199   out->cr();
3200 
3201   PrintCLDMetaspaceInfoClosure cl(out, scale);
3202   ClassLoaderDataGraph::cld_do(&cl);
3203 }
3204 
3205 
3206 // Dump global metaspace things from the end of ClassLoaderDataGraph
3207 void MetaspaceAux::dump(outputStream* out) {
3208   out->print_cr("All Metaspace:");
3209   out->print("data space: "); print_on(out, Metaspace::NonClassType);
3210   out->print("class space: "); print_on(out, Metaspace::ClassType);
3211   print_waste(out);
3212 }
3213 

























3214 void MetaspaceAux::verify_free_chunks() {
3215   Metaspace::chunk_manager_metadata()->verify();
3216   if (Metaspace::using_class_space()) {
3217     Metaspace::chunk_manager_class()->verify();
3218   }
3219 }
3220 
3221 void MetaspaceAux::verify_capacity() {
3222 #ifdef ASSERT
3223   size_t running_sum_capacity_bytes = capacity_bytes();
3224   // For purposes of the running sum of capacity, verify against capacity
3225   size_t capacity_in_use_bytes = capacity_bytes_slow();
3226   assert(running_sum_capacity_bytes == capacity_in_use_bytes,
3227          "capacity_words() * BytesPerWord " SIZE_FORMAT
3228          " capacity_bytes_slow()" SIZE_FORMAT,
3229          running_sum_capacity_bytes, capacity_in_use_bytes);
3230   for (Metaspace::MetadataType i = Metaspace::ClassType;
3231        i < Metaspace:: MetadataTypeCount;
3232        i = (Metaspace::MetadataType)(i + 1)) {
3233     size_t capacity_in_use_bytes = capacity_bytes_slow(i);


3832   return class_vsm()->calc_chunk_size(word_size);
3833 }
3834 
3835 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) {
3836   tracer()->report_metadata_oom(loader_data, word_size, type, mdtype);
3837 
3838   // If result is still null, we are out of memory.
3839   Log(gc, metaspace, freelist) log;
3840   if (log.is_info()) {
3841     log.info("Metaspace (%s) allocation failed for size " SIZE_FORMAT,
3842              is_class_space_allocation(mdtype) ? "class" : "data", word_size);
3843     ResourceMark rm;
3844     if (log.is_debug()) {
3845       if (loader_data->metaspace_or_null() != NULL) {
3846         LogStream ls(log.debug());
3847         loader_data->dump(&ls);
3848       }
3849     }
3850     LogStream ls(log.info());
3851     MetaspaceAux::dump(&ls);

3852     ChunkManager::print_all_chunkmanagers(&ls);
3853   }
3854 
3855   bool out_of_compressed_class_space = false;
3856   if (is_class_space_allocation(mdtype)) {
3857     Metaspace* metaspace = loader_data->metaspace_non_null();
3858     out_of_compressed_class_space =
3859       MetaspaceAux::committed_bytes(Metaspace::ClassType) +
3860       (metaspace->class_chunk_size(word_size) * BytesPerWord) >
3861       CompressedClassSpaceSize;
3862   }
3863 
3864   // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
3865   const char* space_string = out_of_compressed_class_space ?
3866     "Compressed class space" : "Metaspace";
3867 
3868   report_java_out_of_memory(space_string);
3869 
3870   if (JvmtiExport::should_post_resource_exhausted()) {
3871     JvmtiExport::post_resource_exhausted(




 475   // to Virtualspace
 476   bool expand_by(size_t min_words, size_t preferred_words);
 477 
 478   // In preparation for deleting this node, remove all the chunks
 479   // in the node from any freelist.
 480   void purge(ChunkManager* chunk_manager);
 481 
 482   // If an allocation doesn't fit in the current node a new node is created.
 483   // Allocate chunks out of the remaining committed space in this node
 484   // to avoid wasting that memory.
 485   // This always adds up because all the chunk sizes are multiples of
 486   // the smallest chunk size.
 487   void retire(ChunkManager* chunk_manager);
 488 
 489 #ifdef ASSERT
 490   // Debug support
 491   void mangle();
 492 #endif
 493 
 494   void print_on(outputStream* st) const;
 495   void print_map(outputStream* st, bool is_class) const;
 496 };
 497 
 498 #define assert_is_aligned(value, alignment)                  \
 499   assert(is_aligned((value), (alignment)),                   \
 500          SIZE_FORMAT_HEX " is not aligned to "               \
 501          SIZE_FORMAT, (size_t)(uintptr_t)value, (alignment))
 502 
 503 // Decide if large pages should be committed when the memory is reserved.
 504 static bool should_commit_large_pages_when_reserving(size_t bytes) {
 505   if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) {
 506     size_t words = bytes / BytesPerWord;
 507     bool is_class = false; // We never reserve large pages for the class space.
 508     if (MetaspaceGC::can_expand(words, is_class) &&
 509         MetaspaceGC::allowed_expansion() >= words) {
 510       return true;
 511     }
 512   }
 513 
 514   return false;
 515 }


 527     assert_is_aligned(_rs.size(), Metaspace::reserve_alignment());
 528 
 529     MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
 530   }
 531 }
 532 
 533 void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
 534   Metachunk* chunk = first_chunk();
 535   Metachunk* invalid_chunk = (Metachunk*) top();
 536   while (chunk < invalid_chunk ) {
 537     assert(chunk->is_tagged_free(), "Should be tagged free");
 538     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 539     chunk_manager->remove_chunk(chunk);
 540     assert(chunk->next() == NULL &&
 541            chunk->prev() == NULL,
 542            "Was not removed from its list");
 543     chunk = (Metachunk*) next;
 544   }
 545 }
 546 
 547 void VirtualSpaceNode::print_map(outputStream* st, bool is_class) const {
 548 
 549   // Format:
 550   // <ptr>
 551   // <ptr>  . .. .               .  ..
 552   //        SSxSSMMMMMMMMMMMMMMMMsssXX
 553   //        112114444444444444444
 554   // <ptr>  . .. .               .  ..
 555   //        SSxSSMMMMMMMMMMMMMMMMsssXX
 556   //        112114444444444444444
 557 
 558   if (bottom() == top()) {
 559     return;
 560   }
 561 
 562   // First line: dividers for every med-chunk-sized interval
 563   // Second line: a dot for the start of a chunk
 564   // Third line: a letter per chunk type (x,s,m,h), uppercase if in use.
 565 
 566   const size_t spec_chunk_size = is_class ? ClassSpecializedChunk : SpecializedChunk;
 567   const size_t small_chunk_size = is_class ? ClassSmallChunk : SmallChunk;
 568   const size_t med_chunk_size = is_class ? ClassMediumChunk : MediumChunk;
 569 
 570   int line_len = 100;
 571   const size_t section_len = align_up(spec_chunk_size * line_len, med_chunk_size);
 572   line_len = (int)(section_len / spec_chunk_size);
 573 
 574   char* line1 = (char*)os::malloc(line_len, mtInternal);
 575   char* line2 = (char*)os::malloc(line_len, mtInternal);
 576   char* line3 = (char*)os::malloc(line_len, mtInternal);
 577   int pos = 0;
 578   const MetaWord* p = bottom();
 579   const Metachunk* chunk = (const Metachunk*)p;
 580   const MetaWord* chunk_end = p + chunk->word_size();
 581   while (p < top()) {
 582     if (pos == line_len) {
 583       pos = 0;
 584       st->fill_to(22);
 585       st->print_raw(line1, line_len);
 586       st->cr();
 587       st->fill_to(22);
 588       st->print_raw(line2, line_len);
 589       st->cr();
 590     }
 591     if (pos == 0) {
 592       st->print(PTR_FORMAT ":", p2i(p));
 593     }
 594     if (p == chunk_end) {
 595       chunk = (Metachunk*)p;
 596       chunk_end = p + chunk->word_size();
 597     }
 598     if (p == (const MetaWord*)chunk) {
 599       // chunk starts.
 600       line1[pos] = '.';
 601     } else {
 602       line1[pos] = ' ';
 603     }
 604     // Line 2: chunk type (x=spec, s=small, m=medium, h=humongous), uppercase if
 605     // chunk is in use.
 606     const bool chunk_is_free = ((Metachunk*)chunk)->is_tagged_free();
 607     if (chunk->word_size() == spec_chunk_size) {
 608       line2[pos] = chunk_is_free ? 'x' : 'X';
 609     } else if (chunk->word_size() == small_chunk_size) {
 610       line2[pos] = chunk_is_free ? 's' : 'S';
 611     } else if (chunk->word_size() == med_chunk_size) {
 612       line2[pos] = chunk_is_free ? 'm' : 'M';
 613    } else if (chunk->word_size() > med_chunk_size) {
 614       line2[pos] = chunk_is_free ? 'h' : 'H';
 615     } else {
 616       ShouldNotReachHere();
 617     }
 618     p += spec_chunk_size;
 619     pos ++;
 620   }
 621   if (pos > 0) {
 622     st->fill_to(22);
 623     st->print_raw(line1, pos);
 624     st->cr();
 625     st->fill_to(22);
 626     st->print_raw(line2, pos);
 627     st->cr();
 628   }
 629   os::free(line1);
 630   os::free(line2);
 631   os::free(line3);
 632 }
 633 
 634 
 635 #ifdef ASSERT
 636 uintx VirtualSpaceNode::container_count_slow() {
 637   uintx count = 0;
 638   Metachunk* chunk = first_chunk();
 639   Metachunk* invalid_chunk = (Metachunk*) top();
 640   while (chunk < invalid_chunk ) {
 641     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 642     // Don't count the chunks on the free lists.  Those are
 643     // still part of the VirtualSpaceNode but not currently
 644     // counted.
 645     if (!chunk->is_tagged_free()) {
 646       count++;
 647     }
 648     chunk = (Metachunk*) next;
 649   }
 650   return count;
 651 }
 652 #endif
 653 
 654 // List of VirtualSpaces for metadata allocation.


 721   bool initialization_succeeded() { return _virtual_space_list != NULL; }
 722 
 723   size_t reserved_words()  { return _reserved_words; }
 724   size_t reserved_bytes()  { return reserved_words() * BytesPerWord; }
 725   size_t committed_words() { return _committed_words; }
 726   size_t committed_bytes() { return committed_words() * BytesPerWord; }
 727 
 728   void inc_reserved_words(size_t v);
 729   void dec_reserved_words(size_t v);
 730   void inc_committed_words(size_t v);
 731   void dec_committed_words(size_t v);
 732   void inc_virtual_space_count();
 733   void dec_virtual_space_count();
 734 
 735   bool contains(const void* ptr);
 736 
 737   // Unlink empty VirtualSpaceNodes and free it.
 738   void purge(ChunkManager* chunk_manager);
 739 
 740   void print_on(outputStream* st) const;
 741   void print_map(outputStream* st) const;
 742 
 743   class VirtualSpaceListIterator : public StackObj {
 744     VirtualSpaceNode* _virtual_spaces;
 745    public:
 746     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
 747       _virtual_spaces(virtual_spaces) {}
 748 
 749     bool repeat() {
 750       return _virtual_spaces != NULL;
 751     }
 752 
 753     VirtualSpaceNode* get_next() {
 754       VirtualSpaceNode* result = _virtual_spaces;
 755       if (_virtual_spaces != NULL) {
 756         _virtual_spaces = _virtual_spaces->next();
 757       }
 758       return result;
 759     }
 760   };
 761 };


1534     preferred_word_size = min_word_size;
1535   }
1536 
1537   bool expanded = expand_by(min_word_size, preferred_word_size);
1538   if (expanded) {
1539     next = current_virtual_space()->get_chunk_vs(chunk_word_size);
1540     assert(next != NULL, "The allocation was expected to succeed after the expansion");
1541   }
1542 
1543    return next;
1544 }
1545 
1546 void VirtualSpaceList::print_on(outputStream* st) const {
1547   VirtualSpaceListIterator iter(virtual_space_list());
1548   while (iter.repeat()) {
1549     VirtualSpaceNode* node = iter.get_next();
1550     node->print_on(st);
1551   }
1552 }
1553 
1554 void VirtualSpaceList::print_map(outputStream* st) const {
1555   VirtualSpaceNode* list = virtual_space_list();
1556   VirtualSpaceListIterator iter(list);
1557   unsigned i = 0;
1558   while (iter.repeat()) {
1559     st->print_cr("Node %u:", i);
1560     VirtualSpaceNode* node = iter.get_next();
1561     node->print_map(st, this->is_class());
1562     i ++;
1563   }
1564 }
1565 
1566 // MetaspaceGC methods
1567 
1568 // VM_CollectForMetadataAllocation is the vm operation used to GC.
1569 // Within the VM operation after the GC the attempt to allocate the metadata
1570 // should succeed.  If the GC did not free enough space for the metaspace
1571 // allocation, the HWM is increased so that another virtualspace will be
1572 // allocated for the metadata.  With perm gen the increase in the perm
1573 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion.  The
1574 // metaspace policy uses those as the small and large steps for the HWM.
1575 //
1576 // After the GC the compute_new_size() for MetaspaceGC is called to
1577 // resize the capacity of the metaspaces.  The current implementation
1578 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
1579 // to resize the Java heap by some GC's.  New flags can be implemented
1580 // if really needed.  MinMetaspaceFreeRatio is used to calculate how much
1581 // free space is desirable in the metaspace capacity to decide how much
1582 // to increase the HWM.  MaxMetaspaceFreeRatio is used to decide how much
1583 // free space is desirable in the metaspace capacity before decreasing
1584 // the HWM.
1585 


2019 
2020     log_trace(gc, metaspace, freelist)("ChunkManager::free_chunks_get: free_list " PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
2021                                        p2i(free_list), p2i(chunk), chunk->word_size());
2022   } else {
2023     chunk = humongous_dictionary()->get_chunk(word_size);
2024 
2025     if (chunk == NULL) {
2026       return NULL;
2027     }
2028 
2029     log_debug(gc, metaspace, alloc)("Free list allocate humongous chunk size " SIZE_FORMAT " for requested size " SIZE_FORMAT " waste " SIZE_FORMAT,
2030                                     chunk->word_size(), word_size, chunk->word_size() - word_size);
2031   }
2032 
2033   // Chunk has been removed from the chunk manager; update counters.
2034   account_for_removed_chunk(chunk);
2035 
2036   // Remove it from the links to this freelist
2037   chunk->set_next(NULL);
2038   chunk->set_prev(NULL);
2039 
2040   // Chunk is no longer on any freelist. Setting to false make container_count_slow()
2041   // work.
2042   chunk->set_is_tagged_free(false);

2043   chunk->container()->inc_container_count();
2044 
2045   slow_locked_verify();
2046   return chunk;
2047 }
2048 
2049 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
2050   assert_lock_strong(SpaceManager::expand_lock());
2051   slow_locked_verify();
2052 
2053   // Take from the beginning of the list
2054   Metachunk* chunk = free_chunks_get(word_size);
2055   if (chunk == NULL) {
2056     return NULL;
2057   }
2058 
2059   assert((word_size <= chunk->word_size()) ||
2060          (list_index(chunk->word_size()) == HumongousIndex),
2061          "Non-humongous variable sized chunk");
2062   LogTarget(Debug, gc, metaspace, freelist) lt;


2090   // keeps tree node pointers in the chunk payload area which mangle will overwrite.
2091   NOT_PRODUCT(chunk->mangle(badMetaWordVal);)
2092 
2093   if (index != HumongousIndex) {
2094     // Return non-humongous chunk to freelist.
2095     ChunkList* list = free_chunks(index);
2096     assert(list->size() == chunk->word_size(), "Wrong chunk type.");
2097     list->return_chunk_at_head(chunk);
2098     log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " to freelist.",
2099         chunk_size_name(index), p2i(chunk));
2100   } else {
2101     // Return humongous chunk to dictionary.
2102     assert(chunk->word_size() > free_chunks(MediumIndex)->size(), "Wrong chunk type.");
2103     assert(chunk->word_size() % free_chunks(SpecializedIndex)->size() == 0,
2104            "Humongous chunk has wrong alignment.");
2105     _humongous_dictionary.return_chunk(chunk);
2106     log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " (word size " SIZE_FORMAT ") to freelist.",
2107         chunk_size_name(index), p2i(chunk), chunk->word_size());
2108   }
2109   chunk->container()->dec_container_count();
2110   chunk->set_is_tagged_free(true);
2111 
2112   // Chunk has been added; update counters.
2113   account_for_added_chunk(chunk);
2114 
2115 }
2116 
2117 void ChunkManager::return_chunk_list(ChunkIndex index, Metachunk* chunks) {
2118   index_bounds_check(index);
2119   if (chunks == NULL) {
2120     return;
2121   }
2122   LogTarget(Trace, gc, metaspace, freelist) log;
2123   if (log.is_enabled()) { // tracing
2124     log.print("returning list of %s chunks...", chunk_size_name(index));
2125   }
2126   unsigned num_chunks_returned = 0;
2127   size_t size_chunks_returned = 0;
2128   Metachunk* cur = chunks;
2129   while (cur != NULL) {
2130     // Capture the next link before it is changed


3029                   "used "      SIZE_FORMAT "K, "
3030                   "capacity "  SIZE_FORMAT "K, "
3031                   "committed " SIZE_FORMAT "K, "
3032                   "reserved "  SIZE_FORMAT "K",
3033                   used_bytes(ct)/K,
3034                   capacity_bytes(ct)/K,
3035                   committed_bytes(ct)/K,
3036                   reserved_bytes(ct)/K);
3037   }
3038 }
3039 
3040 // Print information for class space and data space separately.
3041 // This is almost the same as above.
3042 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
3043   size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype);
3044   size_t capacity_bytes = capacity_bytes_slow(mdtype);
3045   size_t used_bytes = used_bytes_slow(mdtype);
3046   size_t free_bytes = free_bytes_slow(mdtype);
3047   size_t used_and_free = used_bytes + free_bytes +
3048                            free_chunks_capacity_bytes;
3049   out->print_cr("  Chunk accounting: (used in chunks " SIZE_FORMAT
3050              "K + unused in chunks " SIZE_FORMAT "K  + "
3051              " capacity in free chunks " SIZE_FORMAT "K) = " SIZE_FORMAT
3052              "K  capacity in allocated chunks " SIZE_FORMAT "K",
3053              used_bytes / K,
3054              free_bytes / K,
3055              free_chunks_capacity_bytes / K,
3056              used_and_free / K,
3057              capacity_bytes / K);
3058   // Accounting can only be correct if we got the values during a safepoint
3059   assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
3060 }
3061 
3062 // Print total fragmentation for class metaspaces
3063 void MetaspaceAux::print_class_waste(outputStream* out) {
3064   assert(Metaspace::using_class_space(), "class metaspace not used");
3065   size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
3066   size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
3067   ClassLoaderDataGraphMetaspaceIterator iter;
3068   while (iter.repeat()) {
3069     Metaspace* msp = iter.get_next();
3070     if (msp != NULL) {
3071       cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);


3295   out->cr();
3296   ChunkManager::print_all_chunkmanagers(out, scale);
3297 
3298   out->cr();
3299   out->print_cr("Per-classloader metadata:");
3300   out->cr();
3301 
3302   PrintCLDMetaspaceInfoClosure cl(out, scale);
3303   ClassLoaderDataGraph::cld_do(&cl);
3304 }
3305 
3306 
3307 // Dump global metaspace things from the end of ClassLoaderDataGraph
3308 void MetaspaceAux::dump(outputStream* out) {
3309   out->print_cr("All Metaspace:");
3310   out->print("data space: "); print_on(out, Metaspace::NonClassType);
3311   out->print("class space: "); print_on(out, Metaspace::ClassType);
3312   print_waste(out);
3313 }
3314 
3315 // Prints an ASCII representation of the given space.
3316 void MetaspaceAux::print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype) {
3317   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
3318   const bool for_class = mdtype == Metaspace::ClassType ? true : false;
3319   VirtualSpaceList* const vsl = for_class ? Metaspace::class_space_list() : Metaspace::space_list();
3320   if (vsl != NULL) {
3321     if (for_class) {
3322       if (!Metaspace::using_class_space()) {
3323         out->print_cr("No Class Space.");
3324         return;
3325       }
3326       out->print_raw("---- Metaspace Map (Class Space) ----");
3327     } else {
3328       out->print_raw("---- Metaspace Map (Non-Class Space) ----");
3329     }
3330     // Print legend:
3331     out->cr();
3332     out->print_cr("Chunk Types (uppercase chunks are in use): x-specialized, s-small, m-medium, h-humongous.");
3333     out->cr();
3334     VirtualSpaceList* const vsl = for_class ? Metaspace::class_space_list() : Metaspace::space_list();
3335     vsl->print_map(out);
3336     out->cr();
3337   }
3338 }
3339 
3340 void MetaspaceAux::verify_free_chunks() {
3341   Metaspace::chunk_manager_metadata()->verify();
3342   if (Metaspace::using_class_space()) {
3343     Metaspace::chunk_manager_class()->verify();
3344   }
3345 }
3346 
3347 void MetaspaceAux::verify_capacity() {
3348 #ifdef ASSERT
3349   size_t running_sum_capacity_bytes = capacity_bytes();
3350   // For purposes of the running sum of capacity, verify against capacity
3351   size_t capacity_in_use_bytes = capacity_bytes_slow();
3352   assert(running_sum_capacity_bytes == capacity_in_use_bytes,
3353          "capacity_words() * BytesPerWord " SIZE_FORMAT
3354          " capacity_bytes_slow()" SIZE_FORMAT,
3355          running_sum_capacity_bytes, capacity_in_use_bytes);
3356   for (Metaspace::MetadataType i = Metaspace::ClassType;
3357        i < Metaspace:: MetadataTypeCount;
3358        i = (Metaspace::MetadataType)(i + 1)) {
3359     size_t capacity_in_use_bytes = capacity_bytes_slow(i);


3958   return class_vsm()->calc_chunk_size(word_size);
3959 }
3960 
3961 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) {
3962   tracer()->report_metadata_oom(loader_data, word_size, type, mdtype);
3963 
3964   // If result is still null, we are out of memory.
3965   Log(gc, metaspace, freelist) log;
3966   if (log.is_info()) {
3967     log.info("Metaspace (%s) allocation failed for size " SIZE_FORMAT,
3968              is_class_space_allocation(mdtype) ? "class" : "data", word_size);
3969     ResourceMark rm;
3970     if (log.is_debug()) {
3971       if (loader_data->metaspace_or_null() != NULL) {
3972         LogStream ls(log.debug());
3973         loader_data->dump(&ls);
3974       }
3975     }
3976     LogStream ls(log.info());
3977     MetaspaceAux::dump(&ls);
3978     MetaspaceAux::print_metaspace_map(&ls, mdtype);
3979     ChunkManager::print_all_chunkmanagers(&ls);
3980   }
3981 
3982   bool out_of_compressed_class_space = false;
3983   if (is_class_space_allocation(mdtype)) {
3984     Metaspace* metaspace = loader_data->metaspace_non_null();
3985     out_of_compressed_class_space =
3986       MetaspaceAux::committed_bytes(Metaspace::ClassType) +
3987       (metaspace->class_chunk_size(word_size) * BytesPerWord) >
3988       CompressedClassSpaceSize;
3989   }
3990 
3991   // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
3992   const char* space_string = out_of_compressed_class_space ?
3993     "Compressed class space" : "Metaspace";
3994 
3995   report_java_out_of_memory(space_string);
3996 
3997   if (JvmtiExport::should_post_resource_exhausted()) {
3998     JvmtiExport::post_resource_exhausted(


< prev index next >