< prev index next >

src/hotspot/share/memory/heap.cpp

Print this page




   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 
  25 #include "precompiled.hpp"

  26 #include "memory/heap.hpp"
  27 #include "oops/oop.inline.hpp"
  28 #include "runtime/os.hpp"

  29 #include "services/memTracker.hpp"
  30 #include "utilities/align.hpp"
  31 
  32 size_t CodeHeap::header_size() {
  33   return sizeof(HeapBlock);
  34 }
  35 
  36 
  37 // Implementation of Heap
  38 
  39 CodeHeap::CodeHeap(const char* name, const int code_blob_type)
  40   : _code_blob_type(code_blob_type) {
  41   _name                         = name;
  42   _number_of_committed_segments = 0;
  43   _number_of_reserved_segments  = 0;
  44   _segment_size                 = 0;
  45   _log2_segment_size            = 0;
  46   _next_segment                 = 0;
  47   _freelist                     = NULL;
  48   _freelist_segments            = 0;


 543     assert(len == _freelist_segments, "wrong freelist");
 544 
 545     for(HeapBlock* h = first_block(); h != NULL; h = next_block(h)) {
 546       if (h->free()) count--;
 547     }
 548     // Verify that the freelist contains the same number of blocks
 549     // than free blocks found on the full list.
 550     assert(count == 0, "missing free blocks");
 551 
 552     // Verify that the number of free blocks is not out of hand.
 553     static int free_block_threshold = 10000;
 554     if (count > free_block_threshold) {
 555       warning("CodeHeap: # of free blocks > %d", free_block_threshold);
 556       // Double the warning limit
 557       free_block_threshold *= 2;
 558     }
 559   }
 560 }
 561 
 562 #endif























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































   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 
  25 #include "precompiled.hpp"
  26 #include "compiler/compileBroker.hpp"
  27 #include "memory/heap.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/os.hpp"
  30 #include "runtime/sweeper.hpp"
  31 #include "services/memTracker.hpp"
  32 #include "utilities/align.hpp"
  33 
  34 size_t CodeHeap::header_size() {
  35   return sizeof(HeapBlock);
  36 }
  37 
  38 
  39 // Implementation of Heap
  40 
  41 CodeHeap::CodeHeap(const char* name, const int code_blob_type)
  42   : _code_blob_type(code_blob_type) {
  43   _name                         = name;
  44   _number_of_committed_segments = 0;
  45   _number_of_reserved_segments  = 0;
  46   _segment_size                 = 0;
  47   _log2_segment_size            = 0;
  48   _next_segment                 = 0;
  49   _freelist                     = NULL;
  50   _freelist_segments            = 0;


 545     assert(len == _freelist_segments, "wrong freelist");
 546 
 547     for(HeapBlock* h = first_block(); h != NULL; h = next_block(h)) {
 548       if (h->free()) count--;
 549     }
 550     // Verify that the freelist contains the same number of blocks
 551     // than free blocks found on the full list.
 552     assert(count == 0, "missing free blocks");
 553 
 554     // Verify that the number of free blocks is not out of hand.
 555     static int free_block_threshold = 10000;
 556     if (count > free_block_threshold) {
 557       warning("CodeHeap: # of free blocks > %d", free_block_threshold);
 558       // Double the warning limit
 559       free_block_threshold *= 2;
 560     }
 561   }
 562 }
 563 
 564 #endif
 565 
 566 
 567 //---<  BEGIN  >--- 8198691: CodeHeap State Analytics.
 568 //
 569 // With this declaration macro, it is possible to switch between
 570 //  - direct output into an argument-passed outputStream and
 571 //  - buffered output into a bufferedStream with subsequent flush
 572 //    of the filled buffer to the outputStream.
 573 #define USE_STRINGSTREAM
 574 #define HEX32_FORMAT  "0x%x"  // just a helper format string used below multiple times
 575 //
 576 // Writing to a bufferedStream buffer first has a significant advantage:
 577 // It uses noticeably less cpu cycles and reduces (when wirting to a
 578 // network file) the required bandwidth by at least a factor of ten.
 579 // That clearly makes up for the increased code complexity.
 580 #if defined(USE_STRINGSTREAM)
 581 #define STRINGSTREAM_DECL(_anyst, _outst)                 \
 582     /* _anyst  name of the stream as used in the code */  \
 583     /* _outst  stream where final output will go to   */  \
 584     ResourceMark rm;                                      \
 585     bufferedStream   _sstobj = bufferedStream(2*K);       \
 586     bufferedStream*  _sstbuf = &_sstobj;                  \
 587     outputStream*    _outbuf = _outst;                    \
 588     bufferedStream*  _anyst  = &_sstobj; /* any stream. Use this to just print - no buffer flush.  */
 589 
 590 #define STRINGSTREAM_FLUSH(termString)                    \
 591     _sstbuf->print("%s", termString);                     \
 592     _outbuf->print("%s", _sstbuf->as_string());           \
 593     _sstbuf->reset();
 594 #else
 595 #define STRINGSTREAM_DECL(_anyst, _outst)                 \
 596     outputStream*  _outbuf = _outst;                      \
 597     outputStream*  _anyst  = _outst;   /* any stream. Use this to just print - no buffer flush.  */
 598 
 599 #define STRINGSTREAM_FLUSH(termString)                    \
 600     _outbuf->print("%s", termString);
 601 #endif
 602 
 603 const char  blobTypeChar[] = {' ', 'N', 'I', 'X', 'Z', 'U', 'R', '?', 'D', 'T', 'E', 'S', 'A', 'M', 'B', 'L' };
 604 const char* blobTypeName[] = {"noType"
 605                              ,     "nMethod (active)"
 606                              ,          "nMethod (inactive)"
 607                              ,               "nMethod (deopt)"
 608                              ,                    "nMethod (zombie)"
 609                              ,                         "nMethod (unloaded)"
 610                              ,                              "runtime stub"
 611                              ,                                   "ricochet stub"
 612                              ,                                        "deopt stub"
 613                              ,                                             "uncommon trap stub"
 614                              ,                                                  "exception stub"
 615                              ,                                                       "safepoint stub"
 616                              ,                                                            "adapter blob"
 617                              ,                                                                 "MH adapter blob"
 618                              ,                                                                      "buffer blob"
 619                              ,                                                                           "lastType"
 620                              };
 621 const char* compTypeName[] = { "none", "c1", "c2", "jvmci" };
 622 
 623 //----------------
 624 //  StatElement
 625 //----------------
 626 //  Each analysis granule is represented by an instance of
 627 //  this StatElement struct. It collects and aggregates all
 628 //  information describing the allocated contents of the granule.
 629 //  Free (unallocated) contents is not considered (see FreeBlk for that).
 630 //  All StatElements of a heap segment are stored in the related StatArray.
 631 //  Current size: 40 bytes + 8 bytes class header.
 632 class StatElement : public CHeapObj<mtCode> {
 633   public:
 634     // A note on ages: The compilation_id easily overflows unsigned short in large systems
 635     unsigned int       t1_age;      // oldest compilation_id of tier1 nMethods.
 636     unsigned int       t2_age;      // oldest compilation_id of tier2 nMethods.
 637     unsigned int       tx_age;      // oldest compilation_id of inactive/not entrant nMethods.
 638     unsigned short     t1_space;    // in units of _segment_size to "prevent" overflow
 639     unsigned short     t2_space;    // in units of _segment_size to "prevent" overflow
 640     unsigned short     tx_space;    // in units of _segment_size to "prevent" overflow
 641     unsigned short     dead_space;  // in units of _segment_size to "prevent" overflow
 642     unsigned short     stub_space;  // in units of _segment_size to "prevent" overflow
 643     unsigned short     t1_count;
 644     unsigned short     t2_count;
 645     unsigned short     tx_count;
 646     unsigned short     dead_count;
 647     unsigned short     stub_count;
 648     CompLevel          level;       // optimization level (see globalDefinitions.hpp)
 649     //---<  replaced the correct enum typing with u2 to save space.
 650     u2                 compiler;    // compiler which generated this blob
 651     u2                 type;        // used only if granularity == segment_size
 652 //    CodeHeap::compType compiler;    // compiler which generated this blob
 653 //    CodeHeap::blobType type;        // used only if granularity == segment_size
 654 };
 655 
 656 //-----------
 657 //  FreeBlk
 658 //-----------
 659 //  Each free block in the code heap is represented by an instance
 660 //  of this FreeBlk struct. It collects all information we need to
 661 //  know about each free block.
 662 //  All FreeBlks of a heap segment are stored in the related FreeArray.
 663 struct FreeBlk : public CHeapObj<mtCode> {
 664   HeapBlock      *start;       // address of free block
 665   unsigned int   len;          // length of free block
 666 
 667   unsigned int   gap;          // gap to next free block
 668   unsigned int   index;        // sequential number of free block
 669   unsigned short n_gapBlocks;  // # used blocks in gap
 670   bool           stubs_in_gap; // The occupied space between this and the next free block contains (unmovable) stubs or blobs.
 671 };
 672 
 673 //--------------
 674 //  TopSizeBlk
 675 //--------------
 676 //  The n largest blocks in the code heap are represented in an instance
 677 //  of this TopSizeBlk struct. It collects all information we need to
 678 //  know about those largest blocks.
 679 //  All TopSizeBlks of a heap segment are stored in the related TopSizeArray.
 680 struct TopSizeBlk : public CHeapObj<mtCode> {
 681   HeapBlock      *start;       // address of block
 682   unsigned int   len;          // length of block, in _segment_size units. Will never overflow int.
 683 
 684   unsigned int   index;        // ordering index, 0 is largest block
 685                                // contains array index of next smaller block
 686                                // -1 indicates end of list
 687   CompLevel      level;        // optimization level (see globalDefinitions.hpp)
 688   u2             compiler;     // compiler which generated this blob
 689   u2             type;         // blob type
 690 };
 691 
 692 //---------------------------
 693 //  SizeDistributionElement
 694 //---------------------------
 695 //  During CodeHeap analysis, each allocated code block is associated with a
 696 //  SizeDistributionElement according to its size. Later on, the array of
 697 //  SizeDistributionElements is used to print a size distribution bar graph.
 698 //  All SizeDistributionElements of a heap segment are stored in the related SizeDistributionArray.
 699 struct SizeDistributionElement : public CHeapObj<mtCode> {
 700                                // Range is [rangeStart..rangeEnd).
 701   unsigned int   rangeStart;   // start of length range, in _segment_size units.
 702   unsigned int   rangeEnd;     // end   of length range, in _segment_size units.
 703   unsigned int   lenSum;       // length of block, in _segment_size units. Will never overflow int.
 704 
 705   unsigned int   count;        // number of blocks assigned to this range.
 706 };
 707 
 708 //----------------
 709 //  CodeHeapStat
 710 //----------------
 711 //  Because we have to deal with multiple CodeHeaps, we need to
 712 //  collect "global" information in a segment-specific way as well.
 713 //  Thats what the CodeHeapStat and CodeHeapStatArray are used for.
 714 //  Before a heap segment is processed, the contents of the CodeHeapStat
 715 //  element is copied to the global variables (get_HeapStatGlobals).
 716 //  When processing is done, the possibly modified global variables are
 717 //  copied back (set_HeapStatGlobals) to the CodeHeapStat element.
 718 struct CodeHeapStat {
 719 //    struct StatElement              *StatArray;
 720     StatElement                     *StatArray;
 721     struct FreeBlk                  *FreeArray;
 722     struct TopSizeBlk               *TopSizeArray;
 723     struct SizeDistributionElement  *SizeDistributionArray;
 724     const char                      *heapName;
 725     // StatElement data
 726     size_t        alloc_granules;
 727     size_t        granule_size;
 728     bool          segment_granules;
 729     unsigned int  nBlocks_t1;
 730     unsigned int  nBlocks_t2;
 731     unsigned int  nBlocks_alive;
 732     unsigned int  nBlocks_dead;
 733     unsigned int  nBlocks_unloaded;
 734     unsigned int  nBlocks_stub;
 735     // FreeBlk data
 736     unsigned int  alloc_freeBlocks;
 737     // UsedBlk data
 738     unsigned int  alloc_topSizeBlocks;
 739     unsigned int  used_topSizeBlocks;
 740     // method hotness data. Temperature range is [-reset_val..+reset_val]
 741     int           avgTemp;
 742     int           maxTemp;
 743     int           minTemp;
 744 };
 745 
 746 // Be prepared for ten different Code Heaps. Should be enough for a few years.
 747 const unsigned int nSizeDistElements = 31;  // logarithmic range growth, max size: 2**32
 748 const unsigned int  maxTopSizeBlocks = 50;
 749 const unsigned int        tsbStopper = 2*maxTopSizeBlocks;
 750 const unsigned int          maxHeaps = 10;
 751 static unsigned int         nHeaps   =  0;
 752 static struct CodeHeapStat  CodeHeapStatArray[maxHeaps];
 753 
 754 // static struct StatElement *StatArray      = NULL;
 755 static StatElement *StatArray             = NULL;
 756 static size_t       alloc_granules        = 0;
 757 static size_t       granule_size          = 0;
 758 static bool         segment_granules      = false;
 759 static unsigned int nBlocks_t1            = 0;  // counting "in_use" nmethods only.
 760 static unsigned int nBlocks_t2            = 0;  // counting "in_use" nmethods only.
 761 static unsigned int nBlocks_alive         = 0;  // counting "not_used" and "not_entrant" nmethods only.
 762 static unsigned int nBlocks_dead          = 0;  // counting "zombie" and "unloaded" methods only.
 763 static unsigned int nBlocks_unloaded      = 0;  // counting "unloaded" nmethods only. This is a transien state.
 764 static unsigned int nBlocks_stub          = 0;
 765 
 766 static struct FreeBlk          *FreeArray = NULL;
 767 static unsigned int      alloc_freeBlocks = 0;
 768 
 769 static struct TopSizeBlk    *TopSizeArray = NULL;
 770 static unsigned int   alloc_topSizeBlocks = 0;
 771 static unsigned int    used_topSizeBlocks = 0;
 772 
 773 static struct SizeDistributionElement  *SizeDistributionArray = NULL;
 774 
 775 // nMethod temperature (hotness) indicators.
 776 static int                     avgTemp    = 0;
 777 static int                     maxTemp    = 0;
 778 static int                     minTemp    = 0;
 779 
 780 static unsigned int  latest_compilation_id   = 0;
 781 static volatile bool initialization_complete = false;
 782 
 783 const char* CodeHeap::get_heapName() {
 784   if (SegmentedCodeCache) return name();
 785   else                    return "CodeHeap";
 786 }
 787 
 788 // returns the index to be used for the heap being processed.
 789 //   <  nHeaps: entry found. Use this index.
 790 //   == nHeaps: entry not found. Use this index for new entry.
 791 //   == maxHeaps: entry not found. No space for a new entry.
 792 unsigned int CodeHeap::findHeapIndex(outputStream *out, const char *heapName) {
 793   if (SegmentedCodeCache) {
 794     unsigned int ix = 0;
 795     while ( (ix < nHeaps) && ((CodeHeapStatArray[ix].heapName == NULL) || strcmp(heapName, CodeHeapStatArray[ix].heapName)) ) {
 796       ix++;
 797     }
 798     if (ix <  nHeaps) { // existing entry found
 799       return ix;
 800     }
 801     if ((ix == nHeaps) && (nHeaps < maxHeaps)) { // new entry allocated
 802       nHeaps++;
 803       CodeHeapStatArray[ix].heapName = heapName;
 804       return ix;
 805     }
 806     out->print_cr("Too many heap segments, please adapt maxHeaps in heap.cpp");
 807     return maxHeaps;
 808   } else {
 809     nHeaps = 1;
 810     CodeHeapStatArray[0].heapName = heapName;
 811     return 0; // This is the default index if CodeCache is not segmented.
 812   }
 813 }
 814 
 815 void CodeHeap::get_HeapStatGlobals(outputStream *out, const char *heapName) {
 816   unsigned int ix = (heapName == NULL) ? maxHeaps : findHeapIndex(out, heapName);
 817   // Coverity CID 696443 - also check for index being smaller than maxHeaps.
 818   //          This coverity finding is bullshit. Looking at the implementation
 819   //          of findHeapIndex(), we learn that nHeaps will never grow beyond maxHeaps,
 820   //          making (ix < nHeaps) a safe check.
 821   //          But anyway, making coverity happy is more important than correct code.
 822   if ((ix < nHeaps) && (ix < maxHeaps)) {
 823     StatArray             = CodeHeapStatArray[ix].StatArray;
 824     alloc_granules        = CodeHeapStatArray[ix].alloc_granules;
 825     granule_size          = CodeHeapStatArray[ix].granule_size;
 826     segment_granules      = CodeHeapStatArray[ix].segment_granules;
 827     nBlocks_t1            = CodeHeapStatArray[ix].nBlocks_t1;
 828     nBlocks_t2            = CodeHeapStatArray[ix].nBlocks_t2;
 829     nBlocks_alive         = CodeHeapStatArray[ix].nBlocks_alive;
 830     nBlocks_dead          = CodeHeapStatArray[ix].nBlocks_dead;
 831     nBlocks_unloaded      = CodeHeapStatArray[ix].nBlocks_unloaded;
 832     nBlocks_stub          = CodeHeapStatArray[ix].nBlocks_stub;
 833     FreeArray             = CodeHeapStatArray[ix].FreeArray;
 834     alloc_freeBlocks      = CodeHeapStatArray[ix].alloc_freeBlocks;
 835     TopSizeArray          = CodeHeapStatArray[ix].TopSizeArray;
 836     alloc_topSizeBlocks   = CodeHeapStatArray[ix].alloc_topSizeBlocks;
 837     used_topSizeBlocks    = CodeHeapStatArray[ix].used_topSizeBlocks;
 838     SizeDistributionArray = CodeHeapStatArray[ix].SizeDistributionArray;
 839     avgTemp               = CodeHeapStatArray[ix].avgTemp;
 840     maxTemp               = CodeHeapStatArray[ix].maxTemp;
 841     minTemp               = CodeHeapStatArray[ix].minTemp;
 842   } else {
 843     StatArray             = NULL;
 844     alloc_granules        = 0;
 845     granule_size          = 0;
 846     segment_granules      = false;
 847     nBlocks_t1            = 0;
 848     nBlocks_t2            = 0;
 849     nBlocks_alive         = 0;
 850     nBlocks_dead          = 0;
 851     nBlocks_unloaded      = 0;
 852     nBlocks_stub          = 0;
 853     FreeArray             = NULL;
 854     alloc_freeBlocks      = 0;
 855     TopSizeArray          = NULL;
 856     alloc_topSizeBlocks   = 0;
 857     used_topSizeBlocks    = 0;
 858     SizeDistributionArray = NULL;
 859     avgTemp               = 0;
 860     maxTemp               = 0;
 861     minTemp               = 0;
 862   }
 863 }
 864 
 865 void CodeHeap::set_HeapStatGlobals(outputStream *out, const char *heapName) {
 866   unsigned int ix = (heapName == NULL) ? maxHeaps : findHeapIndex(out, heapName);
 867   if (ix < nHeaps) {
 868     CodeHeapStatArray[ix].StatArray             = StatArray;
 869     CodeHeapStatArray[ix].alloc_granules        = alloc_granules;
 870     CodeHeapStatArray[ix].granule_size          = granule_size;
 871     CodeHeapStatArray[ix].segment_granules      = segment_granules;
 872     CodeHeapStatArray[ix].nBlocks_t1            = nBlocks_t1;
 873     CodeHeapStatArray[ix].nBlocks_t2            = nBlocks_t2;
 874     CodeHeapStatArray[ix].nBlocks_alive         = nBlocks_alive;
 875     CodeHeapStatArray[ix].nBlocks_dead          = nBlocks_dead;
 876     CodeHeapStatArray[ix].nBlocks_unloaded      = nBlocks_unloaded;
 877     CodeHeapStatArray[ix].nBlocks_stub          = nBlocks_stub;
 878     CodeHeapStatArray[ix].FreeArray             = FreeArray;
 879     CodeHeapStatArray[ix].alloc_freeBlocks      = alloc_freeBlocks;
 880     CodeHeapStatArray[ix].TopSizeArray          = TopSizeArray;
 881     CodeHeapStatArray[ix].alloc_topSizeBlocks   = alloc_topSizeBlocks;
 882     CodeHeapStatArray[ix].used_topSizeBlocks    = used_topSizeBlocks;
 883     CodeHeapStatArray[ix].SizeDistributionArray = SizeDistributionArray;
 884     CodeHeapStatArray[ix].avgTemp               = avgTemp;
 885     CodeHeapStatArray[ix].maxTemp               = maxTemp;
 886     CodeHeapStatArray[ix].minTemp               = minTemp;
 887   }
 888 }
 889 
 890 //---<  get a new statistics array  >---
 891 void CodeHeap::prepare_StatArray(outputStream *out, size_t nElem, size_t granularity, const char* heapName) {
 892   if (StatArray == NULL) {
 893     StatArray      = new StatElement[nElem];
 894     //---<  reset some counts  >---
 895     alloc_granules = nElem;
 896     granule_size   = granularity;
 897   }
 898 
 899   if (StatArray == NULL) {
 900     //---<  just do nothing if allocation failed  >---
 901     out->print_cr("Statistics could not be collected for %s, probably out of memory.", heapName);
 902     out->print_cr("Current granularity is " SIZE_FORMAT " bytes. Try a coarser granularity.", granularity);
 903     alloc_granules = 0;
 904     granule_size   = 0;
 905   } else {
 906     //---<  initialize statistics array  >---
 907     memset(StatArray, 0, nElem*sizeof(StatElement));
 908   }
 909 }
 910 
 911 //---<  get a new free block array  >---
 912 void CodeHeap::prepare_FreeArray(outputStream *out, unsigned int nElem, const char* heapName) {
 913   if (FreeArray == NULL) {
 914     FreeArray      = new FreeBlk[nElem];
 915     //---<  reset some counts  >---
 916     alloc_freeBlocks = nElem;
 917   }
 918 
 919   if (FreeArray == NULL) {
 920     //---<  just do nothing if allocation failed  >---
 921     out->print_cr("Free space analysis cannot be done for %s, probably out of memory.", heapName);
 922     alloc_freeBlocks = 0;
 923   } else {
 924     //---<  initialize free block array  >---
 925     memset(FreeArray, 0, alloc_freeBlocks*sizeof(FreeBlk));
 926   }
 927 }
 928 
 929 //---<  get a new TopSizeArray  >---
 930 void CodeHeap::prepare_TopSizeArray(outputStream *out, unsigned int nElem, const char* heapName) {
 931   if (TopSizeArray == NULL) {
 932     TopSizeArray   = new TopSizeBlk[nElem];
 933     //---<  reset some counts  >---
 934     alloc_topSizeBlocks = nElem;
 935     used_topSizeBlocks  = 0;
 936   }
 937 
 938   if (TopSizeArray == NULL) {
 939     //---<  just do nothing if allocation failed  >---
 940     out->print_cr("Top-%d list of largest CodeHeap blocks can not be collected for %s, probably out of memory.", nElem, heapName);
 941     alloc_topSizeBlocks = 0;
 942   } else {
 943     //---<  initialize TopSizeArray  >---
 944     memset(TopSizeArray, 0, nElem*sizeof(TopSizeBlk));
 945     used_topSizeBlocks  = 0;
 946   }
 947 }
 948 
 949 //---<  get a new SizeDistributionArray  >---
 950 void CodeHeap::prepare_SizeDistArray(outputStream *out, unsigned int nElem, const char* heapName) {
 951   if (SizeDistributionArray == NULL) {
 952     SizeDistributionArray = new SizeDistributionElement[nElem];
 953   }
 954 
 955   if (SizeDistributionArray == NULL) {
 956     //---<  just do nothing if allocation failed  >---
 957     out->print_cr("Size distribution can not be collected for %s, probably out of memory.", heapName);
 958   } else {
 959     //---<  initialize SizeDistArray  >---
 960     memset(SizeDistributionArray, 0, nElem*sizeof(SizeDistributionElement));
 961     // Logarithmic range growth. First range starts at _segment_size.
 962     SizeDistributionArray[_log2_segment_size-1].rangeEnd = 1U;
 963     for (unsigned int i = _log2_segment_size; i < nElem; i++) {
 964       SizeDistributionArray[i].rangeStart = 1U << (i     - _log2_segment_size);
 965       SizeDistributionArray[i].rangeEnd   = 1U << ((i+1) - _log2_segment_size);
 966     }
 967   }
 968 }
 969 
 970 //---<  get a new SizeDistributionArray  >---
 971 void CodeHeap::update_SizeDistArray(outputStream *out, unsigned int len) {
 972   if (SizeDistributionArray != NULL) {
 973     for (unsigned int i = _log2_segment_size-1; i < nSizeDistElements; i++) {
 974       if ((SizeDistributionArray[i].rangeStart <= len) && (len < SizeDistributionArray[i].rangeEnd)) {
 975         SizeDistributionArray[i].lenSum += len;
 976         SizeDistributionArray[i].count++;
 977         break;
 978       }
 979     }
 980   }
 981 }
 982 
 983 void CodeHeap::discard_StatArray(outputStream *out) {
 984   if (StatArray != NULL) {
 985     delete StatArray;
 986     StatArray        = NULL;
 987     alloc_granules   = 0;
 988     granule_size     = 0;
 989   }
 990 }
 991 
 992 void CodeHeap::discard_FreeArray(outputStream *out) {
 993   if (FreeArray != NULL) {
 994     delete[] FreeArray;
 995     FreeArray        = NULL;
 996     alloc_freeBlocks = 0;
 997   }
 998 }
 999 
1000 void CodeHeap::discard_TopSizeArray(outputStream *out) {
1001   if (TopSizeArray != NULL) {
1002     delete[] TopSizeArray;
1003     TopSizeArray        = NULL;
1004     alloc_topSizeBlocks = 0;
1005     used_topSizeBlocks  = 0;
1006   }
1007 }
1008 
1009 void CodeHeap::discard_SizeDistArray(outputStream *out) {
1010   if (SizeDistributionArray != NULL) {
1011     delete[] SizeDistributionArray;
1012     SizeDistributionArray = NULL;
1013   }
1014 }
1015 
1016 void CodeHeap::discard(outputStream *out) {
1017   if (!initialization_complete) return;
1018 
1019   if (SegmentedCodeCache) {
1020     for (unsigned int ix = 0; ix < nHeaps; ix++) {
1021       get_HeapStatGlobals(out, CodeHeapStatArray[ix].heapName);
1022       discard_StatArray(out);
1023       discard_FreeArray(out);
1024       discard_TopSizeArray(out);
1025       discard_SizeDistArray(out);
1026       set_HeapStatGlobals(out, CodeHeapStatArray[ix].heapName);
1027       CodeHeapStatArray[ix].heapName = NULL;
1028     }
1029   } else {
1030     get_HeapStatGlobals(out, CodeHeapStatArray[0].heapName);
1031     discard_StatArray(out);
1032     discard_FreeArray(out);
1033     discard_TopSizeArray(out);
1034     discard_SizeDistArray(out);
1035     set_HeapStatGlobals(out, CodeHeapStatArray[0].heapName);
1036     CodeHeapStatArray[0].heapName = NULL;
1037   }
1038 }
1039 
1040 void CodeHeap::aggregate(outputStream *out, const char* granularity_request) {
1041   unsigned int nBlocks_free    = 0;
1042   unsigned int nBlocks_used    = 0;
1043   unsigned int nBlocks_zomb    = 0;
1044   unsigned int nBlocks_disconn = 0;
1045   unsigned int nBlocks_notentr = 0;
1046 
1047   //---<  max & min of TopSizeArray  >---
1048   //  it is sufficient to have these sizes as 32bit unsigned ints.
1049   //  The CodeHeap is limited in size to 4GB. Furthermore, the sizes
1050   //  are stored in _segment_size units, scaling them down by a factor of 64 (at least).
1051   unsigned int  currMax          = 0;
1052   unsigned int  currMin          = 0;
1053   unsigned int  currMin_ix       = 0;
1054   unsigned long total_iterations = 0;
1055 
1056   bool  done             = false;
1057   const int min_granules = 256;
1058   const int max_granules = 512*K; // limits analyzable CodeHeap (with segment_granules) to 32M..128M
1059                                   // results in StatArray size of 20M (= max_granules * 40 Bytes per element)
1060                                   // For a 1GB CodeHeap, the granule size must be at least 2kB to not violate the max_granles limit.
1061   const char *heapName   = get_heapName();
1062 
1063   if (!initialization_complete) {
1064     memset(CodeHeapStatArray, 0, sizeof(CodeHeapStatArray));
1065     initialization_complete = true;
1066 
1067     printBox(out, '=', "C O D E   H E A P   A N A L Y S I S   (general remarks)", NULL);
1068     out->print_cr("   The code heap analysis function provides deep insights into\n"
1069                   "   the inner workings and the internal state of the Java VM's\n"
1070                   "   code cache - the place where all the JVM generated machine\n"
1071                   "   code is stored.\n"
1072                   "   \n"
1073                   "   This function is designed and provided for support engineers\n"
1074                   "   to help them understand and solve issues in customer systems.\n"
1075                   "   It is not intended for use and interpretation by other persons.\n"
1076                   "   \n");
1077   }
1078   get_HeapStatGlobals(out, heapName);
1079 
1080 
1081   // Since we are (and must be) analyzing the CodeHeap contents under the CodeCache_lock,
1082   // all heap information is "constant" and can be safely extracted/calculated before we
1083   // enter the while() loop. Actually, the loop will only be iterated once.
1084   char*  low_bound   = low_boundary();
1085   size_t size        = capacity();
1086   size_t res_size    = max_capacity();
1087 
1088   // Calculate granularity of analysis (and output).
1089   //   The CodeHeap is managed (allocated) in segments (units) of CodeCacheSegmentSize.
1090   //   The CodeHeap can become fairly large, in particular in productive real-life systems.
1091   //
1092   //   It is often neither feasible nor desirable to aggregate the data with the highest possible
1093   //   level of detail, i.e. inspecting and printing each segment on its own.
1094   //
1095   //   The granularity parameter allows to specify the level of detail available in the analysis.
1096   //   It must be a positive multiple of the segment size and should be selected such that enough
1097   //   detail is provided while, at the same time, the printed output does not explode.
1098   //
1099   //   By manipulating the granularity value, we enforce that at least min_granules units
1100   //   of analysis are available. We also enforce an upper limit of max_granules units to
1101   //   keep the amount of allocated storage in check.
1102   //
1103   //   Finally, we adjust the granularity such that each granule covers at most 64k-1 segments.
1104   //   This is necessary to prevent an unsigned short overflow while accumulating space information.
1105   //
1106   size_t granularity = strtol(granularity_request, NULL, 0);
1107   if (granularity > size)               granularity = size;
1108   if (size/granularity < min_granules)  granularity = size/min_granules; // at least min_granules granules
1109   granularity = granularity & (~(_segment_size - 1));                     // must be multiple of _segment_size
1110   if (granularity < _segment_size)      granularity = _segment_size;     // must be at least _segment_size
1111   if (size/granularity > max_granules)  granularity = size/max_granules; // at most max_granules granules
1112   granularity = granularity & (~(_segment_size - 1));                     // must be multiple of _segment_size
1113   if (granularity>>_log2_segment_size >= (1L<<sizeof(unsigned short)*8)) {
1114     granularity = ((1L<<(sizeof(unsigned short)*8))-1)<<_log2_segment_size; // Limit: (64k-1) * _segment_size
1115   }
1116   segment_granules = granularity == _segment_size;
1117   size_t granules  = (size + (granularity-1))/granularity;
1118 
1119   printBox(out, '=', "C O D E   H E A P   A N A L Y S I S   (used blocks) for segment ", heapName);
1120   out->print_cr("   The aggregate step takes an aggregated snapshot of the CodeHeap.\n"
1121                 "   Subsequent print functions create their output based on this snapshot.\n"
1122                 "   The CodeHeap is a living thing, and every effort has been made for the\n"
1123                 "   collected data to be consistent. Only the method names and signatures\n"
1124                 "   are retrieved at print time. That may lead to rare cases where the\n"
1125                 "   name of a method is no longer available, e.g. because it was unloaded.\n");
1126   out->print_cr("   CodeHeap committed size " SIZE_FORMAT "K (" SIZE_FORMAT "M), reserved size " SIZE_FORMAT "K (" SIZE_FORMAT "M), %d%% occupied.",
1127                 size/(size_t)K, size/(size_t)M, res_size/(size_t)K, res_size/(size_t)M, (unsigned int)(100.0*size/res_size));
1128   out->print_cr("   CodeHeap allocation segment size is " SIZE_FORMAT " bytes. This is the smallest possible granularity.", _segment_size);
1129   out->print_cr("   CodeHeap (committed part) is mapped to " SIZE_FORMAT " granules of size " SIZE_FORMAT "bytes.", granules, granularity);
1130   out->print_cr("   Each granule takes " SIZE_FORMAT " bytes of C heap, that is " SIZE_FORMAT "K in total for statistics data.", sizeof(StatElement), (sizeof(StatElement)*granules)/(size_t)K);
1131   out->print_cr("   The number of granules is limited to %dk, requiring a granules size of at least %d bytes for a 1GB heap.", (unsigned int)(max_granules/K), (unsigned int)(G/max_granules));
1132   out->cr();
1133 
1134 
1135   while (!done) {
1136     //---<  reset counters with every aggregation  >---
1137     nBlocks_t1       = 0;
1138     nBlocks_t2       = 0;
1139     nBlocks_alive    = 0;
1140     nBlocks_dead     = 0;
1141     nBlocks_unloaded = 0;
1142     nBlocks_stub     = 0;
1143 
1144     nBlocks_free     = 0;
1145     nBlocks_used     = 0;
1146     nBlocks_zomb     = 0;
1147     nBlocks_disconn  = 0;
1148     nBlocks_notentr  = 0;
1149 
1150     //---<  discard old arrays if size does not match  >---
1151     if (granules != alloc_granules) {
1152       discard_StatArray(out);
1153       discard_TopSizeArray(out);
1154     }
1155 
1156     //---<  allocate arrays if they don't yet exist, initialize  >---
1157     prepare_StatArray(out, granules, granularity, heapName);
1158     if (StatArray == NULL) {
1159       set_HeapStatGlobals(out, heapName);
1160       return;
1161     }
1162     prepare_TopSizeArray(out, maxTopSizeBlocks, heapName);
1163     prepare_SizeDistArray(out, nSizeDistElements, heapName);
1164 
1165     latest_compilation_id = CompileBroker::get_compilation_id();
1166     unsigned int highest_compilation_id = 0;
1167     size_t       usedSpace     = 0;
1168     size_t       t1Space       = 0;
1169     size_t       t2Space       = 0;
1170     size_t       aliveSpace    = 0;
1171     size_t       disconnSpace  = 0;
1172     size_t       notentrSpace  = 0;
1173     size_t       deadSpace     = 0;
1174     size_t       unloadedSpace = 0;
1175     size_t       stubSpace     = 0;
1176     size_t       freeSpace     = 0;
1177     size_t       maxFreeSize   = 0;
1178     HeapBlock*   maxFreeBlock  = NULL;
1179     bool         insane        = false;
1180 
1181     int64_t hotnessAccumulator = 0;
1182     unsigned int n_methods     = 0;
1183     avgTemp       = 0;
1184     minTemp       = (int)(res_size > M ? (res_size/M)*2 : 1);
1185     maxTemp       = -minTemp;
1186 
1187     for (HeapBlock *h = first_block(); h != NULL && !insane; h = next_block(h)) {
1188       unsigned int hb_len     = (unsigned int)h->length();  // despite being size_t, length can never overflow an unsigned int.
1189       size_t       hb_bytelen = ((size_t)hb_len)<<_log2_segment_size;
1190       unsigned int ix_beg     = (unsigned int)(((char*)h-low_bound)/granule_size);
1191       unsigned int ix_end     = (unsigned int)(((char*)h-low_bound+(hb_bytelen-1))/granule_size);
1192       unsigned int compile_id = 0;
1193       CompLevel    comp_lvl   = CompLevel_none;
1194       compType     cType      = noComp;
1195       blobType     cbType     = noType;
1196 
1197       //---<  some sanity checks  >---
1198       // Do not assert here, just check, print error message and return.
1199       // This is a diagnostic function. It is not supposed to tear down the VM.
1200       if ((char*)h <  low_bound ) { insane = true; out->print_cr("Sanity check: HeapBlock @%p below low bound (%p)", (char*)h, low_bound); }
1201       if (ix_end   >= granules  ) { insane = true; out->print_cr("Sanity check: end index (%d) out of bounds (" SIZE_FORMAT ")", ix_end, granules); }
1202       if (size     != capacity()) { insane = true; out->print_cr("Sanity check: code heap capacity has changed (" SIZE_FORMAT "K to " SIZE_FORMAT "K)", size/(size_t)K, capacity()/(size_t)K); }
1203       if (ix_beg   >  ix_end    ) { insane = true; out->print_cr("Sanity check: end index (%d) lower than begin index (%d)", ix_end, ix_beg); }
1204       if (insane) continue;
1205 
1206       if (h->free()) {
1207         nBlocks_free++;
1208         freeSpace    += hb_bytelen;
1209         if (hb_bytelen > maxFreeSize) {
1210           maxFreeSize   = hb_bytelen;
1211           maxFreeBlock  = h;
1212         }
1213       } else {
1214         update_SizeDistArray(out, hb_len);
1215         nBlocks_used++;
1216         usedSpace    += hb_bytelen;
1217         CodeBlob *cb  = (CodeBlob*) find_start(h);
1218         if (cb != NULL) {
1219           cbType = get_cbType(cb);
1220           if (cb->is_nmethod()) {
1221             compile_id = ((nmethod*)cb)->compile_id();
1222             comp_lvl   = (CompLevel)((nmethod*)cb)->comp_level();
1223             if (((nmethod*)cb)->is_compiled_by_c1())    cType = c1;
1224             if (((nmethod*)cb)->is_compiled_by_c2())    cType = c2;
1225             if (((nmethod*)cb)->is_compiled_by_jvmci()) cType = jvmci;
1226             switch (cbType) {
1227               case nMethod_inuse: { // only for executable methods!!!
1228                 // space for these cbs is accounted for later.
1229                 int temperature = ((nmethod*)cb)->hotness_counter();
1230                 hotnessAccumulator += temperature;
1231                 n_methods++;
1232                 maxTemp = (temperature > maxTemp) ? temperature : maxTemp;
1233                 minTemp = (temperature < minTemp) ? temperature : minTemp;
1234                 break;
1235               }
1236               case nMethod_notused:
1237                 nBlocks_alive++;
1238                 nBlocks_disconn++;
1239                 aliveSpace     += hb_bytelen;
1240                 disconnSpace   += hb_bytelen;
1241                 break;
1242               case nMethod_notentrant:  // equivalent to nMethod_alive
1243                 nBlocks_alive++;
1244                 nBlocks_notentr++;
1245                 aliveSpace     += hb_bytelen;
1246                 notentrSpace   += hb_bytelen;
1247                 break;
1248               case nMethod_unloaded:
1249                 nBlocks_unloaded++;
1250                 unloadedSpace  += hb_bytelen;
1251                 break;
1252               case nMethod_dead:
1253                 nBlocks_dead++;
1254                 deadSpace      += hb_bytelen;
1255                 break;
1256               default:
1257                 break;
1258             }
1259           }
1260 
1261           //------------------------------------------
1262           //---<  register block in TopSizeArray  >---
1263           //------------------------------------------
1264           if (alloc_topSizeBlocks > 0) {
1265             if (used_topSizeBlocks == 0) {
1266               TopSizeArray[0].start    = h;
1267               TopSizeArray[0].len      = hb_len;
1268               TopSizeArray[0].index    = tsbStopper;
1269               TopSizeArray[0].compiler = cType;
1270               TopSizeArray[0].level    = comp_lvl;
1271               TopSizeArray[0].type     = cbType;
1272               currMax    = hb_len;
1273               currMin    = hb_len;
1274               currMin_ix = 0;
1275 //            out->print_cr("usedTSB = %d, ix = %d, len = %d, next_ix = %d, next_len = %d", 0, 0, hb_len, TopSizeArray[0].index, TopSizeArray[0].index >= 0 ? TopSizeArray[TopSizeArray[0].index].len : -1);
1276               used_topSizeBlocks++;
1277             // This check roughly cuts 5000 iterations (JVM98, mixed, dbg, termination stats):
1278             } else if ((used_topSizeBlocks < alloc_topSizeBlocks) && (hb_len < currMin)) {
1279               //---<  all blocks in list are larger, but there is room left in array  >---
1280               TopSizeArray[currMin_ix].index = used_topSizeBlocks;
1281               TopSizeArray[used_topSizeBlocks].start    = h;
1282               TopSizeArray[used_topSizeBlocks].len      = hb_len;
1283               TopSizeArray[used_topSizeBlocks].index    = tsbStopper;
1284               TopSizeArray[used_topSizeBlocks].compiler = cType;
1285               TopSizeArray[used_topSizeBlocks].level    = comp_lvl;
1286               TopSizeArray[used_topSizeBlocks].type     = cbType;
1287               currMin    = hb_len;
1288               currMin_ix = used_topSizeBlocks;
1289 //            out->print_cr("usedTSB = %d, ix = %d, len = %d, next_ix = %d, next_len = %d (app MIN)", used_topSizeBlocks, used_topSizeBlocks, hb_len, TopSizeArray[used_topSizeBlocks].index, TopSizeArray[used_topSizeBlocks].index >= 0 ? TopSizeArray[TopSizeArray[used_topSizeBlocks].index].len : -1);
1290               used_topSizeBlocks++;
1291             } else {
1292               // This check cuts total_iterations by a factor of 6 (JVM98, mixed, dbg, termination stats):
1293               //   We don't need to search the list if we know beforehand that the current block size is
1294               //   smaller than the currently recorded minimum and there is no free entry left in the list.
1295               if (!((used_topSizeBlocks == alloc_topSizeBlocks) && (hb_len <= currMin))) {
1296                 if (currMax < hb_len) {
1297                   currMax = hb_len;
1298                 }
1299                 unsigned int i;
1300                 unsigned int prev_i  = tsbStopper;
1301                 unsigned int limit_i =  0;
1302                 for (i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
1303                   if (limit_i++ >= alloc_topSizeBlocks) { insane = true; break; } // emergency exit
1304                   if (        i >= used_topSizeBlocks)  { insane = true; break; } // emergency exit
1305                   total_iterations++;
1306                   if (TopSizeArray[i].len < hb_len) {
1307                     //---<  We want to insert here, element <i> is smaller than the current one  >---
1308                     if (used_topSizeBlocks < alloc_topSizeBlocks) { // still room for a new entry to insert
1309                       // old entry gets moved to the next free element of the array.
1310                       // That's necessary to keep the entry for the largest block at index 0.
1311                       // This move might cause the current minimum to be moved to another place
1312                       if (i == currMin_ix) {
1313                         assert(TopSizeArray[i].len == currMin, "sort error");
1314                         currMin_ix = used_topSizeBlocks;
1315                       }
1316                       memcpy(&TopSizeArray[used_topSizeBlocks], &TopSizeArray[i], sizeof(TopSizeBlk));
1317                       TopSizeArray[i].start    = h;
1318                       TopSizeArray[i].len      = hb_len;
1319                       TopSizeArray[i].index    = used_topSizeBlocks;
1320                       TopSizeArray[i].compiler = cType;
1321                       TopSizeArray[i].level    = comp_lvl;
1322                       TopSizeArray[i].type     = cbType;
1323 //                    out->print_cr("usedTSB = %d, ix = %d, len = %d, next_ix = %d, next_len = %d (new APP)", used_topSizeBlocks, i, hb_len, TopSizeArray[i].index, TopSizeArray[i].index >= 0 ? TopSizeArray[TopSizeArray[i].index].len : -1);
1324                       used_topSizeBlocks++;
1325                     } else { // no room for new entries, current block replaces entry for smallest block
1326                       //---<  Find last entry (entry for smallest remembered block)  >---
1327                       unsigned int      j  = i;
1328                       unsigned int prev_j  = tsbStopper;
1329                       unsigned int limit_j = 0;
1330                       while (TopSizeArray[j].index != tsbStopper) {
1331                         if (limit_j++ >= alloc_topSizeBlocks) { insane = true; break; } // emergency exit
1332                         if (        j >= used_topSizeBlocks)  { insane = true; break; } // emergency exit
1333                         total_iterations++;
1334                         prev_j = j;
1335                         j      = TopSizeArray[j].index;
1336                       }
1337                       if (!insane) {
1338                         if (prev_j == tsbStopper) {
1339                           //---<  Above while loop did not iterate, we already are the min entry  >---
1340                           //---<  We have to just replace the smallest entry                      >---
1341                           currMin    = hb_len;
1342                           currMin_ix = j;
1343                           TopSizeArray[j].start    = h;
1344                           TopSizeArray[j].len      = hb_len;
1345                           TopSizeArray[j].index    = tsbStopper; // already set!!
1346                           TopSizeArray[j].compiler = cType;
1347                           TopSizeArray[j].level    = comp_lvl;
1348                           TopSizeArray[j].type     = cbType;
1349 //                        out->print_cr("usedTSB = %d, ix = %d, len = %d, next_ix = %d, next_len = %d (new MIN)", used_topSizeBlocks, j, hb_len, TopSizeArray[j].index, TopSizeArray[j].index >= 0 ? TopSizeArray[TopSizeArray[j].index].len : -1);
1350                         } else {
1351                           //---<  second-smallest entry is now smallest  >---
1352                           TopSizeArray[prev_j].index = tsbStopper;
1353                           currMin    = TopSizeArray[prev_j].len;
1354                           currMin_ix = prev_j;
1355                           //---<  smallest entry gets overwritten  >---
1356                           memcpy(&TopSizeArray[j], &TopSizeArray[i], sizeof(TopSizeBlk));
1357                           TopSizeArray[i].start    = h;
1358                           TopSizeArray[i].len      = hb_len;
1359                           TopSizeArray[i].index    = j;
1360                           TopSizeArray[i].compiler = cType;
1361                           TopSizeArray[i].level    = comp_lvl;
1362                           TopSizeArray[i].type     = cbType;
1363 //                        out->print_cr("usedTSB = %d, ix = %d, len = %d, next_ix = %d, next_len = %d (new INS)", used_topSizeBlocks, hb_len, i, TopSizeArray[i].index, TopSizeArray[i].index >= 0 ? TopSizeArray[TopSizeArray[i].index].len : -1);
1364                         }
1365                       } // insane
1366                     }
1367                     break;
1368                   }
1369                   prev_i = i;
1370                 }
1371                 if (insane) {
1372                   // Note: regular analysis could probably continue by resetting "insane" flag.
1373                   out->print_cr("Possible loop in TopSizeBlocks list detected. Analysis aborted.");
1374                   discard_TopSizeArray(out);
1375                 }
1376               }
1377             }
1378           }
1379           //----------------------------------------------
1380           //---<  END register block in TopSizeArray  >---
1381           //----------------------------------------------
1382         } else {
1383           nBlocks_zomb++;
1384         }
1385 
1386         if (ix_beg == ix_end) {
1387           StatArray[ix_beg].type = cbType;
1388           switch (cbType) {
1389             case nMethod_inuse:
1390               if (highest_compilation_id < compile_id) highest_compilation_id = compile_id;
1391               if (comp_lvl < CompLevel_full_optimization) {
1392                 nBlocks_t1++;
1393                 t1Space   += hb_bytelen;
1394                 StatArray[ix_beg].t1_count++;
1395                 StatArray[ix_beg].t1_space += (unsigned short)hb_len;
1396                 StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;
1397               } else {
1398                 nBlocks_t2++;
1399                 t2Space   += hb_bytelen;
1400                 StatArray[ix_beg].t2_count++;
1401                 StatArray[ix_beg].t2_space += (unsigned short)hb_len;
1402                 StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;
1403               }
1404               StatArray[ix_beg].level     = comp_lvl;
1405               StatArray[ix_beg].compiler  = cType;
1406               break;
1407             case nMethod_alive:
1408               StatArray[ix_beg].tx_count++;
1409               StatArray[ix_beg].tx_space += (unsigned short)hb_len;
1410               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
1411               StatArray[ix_beg].level     = comp_lvl;
1412               StatArray[ix_beg].compiler  = cType;
1413               break;
1414             case nMethod_dead:
1415             case nMethod_unloaded:
1416               StatArray[ix_beg].dead_count++;
1417               StatArray[ix_beg].dead_space += (unsigned short)hb_len;
1418               break;
1419             default:
1420               // must be a stub, if it's not a dead or alive nMethod
1421               nBlocks_stub++;
1422               stubSpace   += hb_bytelen;
1423               StatArray[ix_beg].stub_count++;
1424               StatArray[ix_beg].stub_space += (unsigned short)hb_len;
1425               break;
1426           }
1427         } else {
1428           unsigned int beg_space = (unsigned int)(granule_size - ((char*)h - low_bound - ix_beg*granule_size));
1429           unsigned int end_space = (unsigned int)(hb_bytelen - beg_space - (ix_end-ix_beg-1)*granule_size);
1430           beg_space = beg_space>>_log2_segment_size;  // store in units of _segment_size
1431           end_space = end_space>>_log2_segment_size;  // store in units of _segment_size
1432           StatArray[ix_beg].type = cbType;
1433           StatArray[ix_end].type = cbType;
1434           switch (cbType) {
1435             case nMethod_inuse:
1436               if (highest_compilation_id < compile_id) highest_compilation_id = compile_id;
1437               if (comp_lvl < CompLevel_full_optimization) {
1438                 nBlocks_t1++;
1439                 t1Space   += hb_bytelen;
1440                 StatArray[ix_beg].t1_count++;
1441                 StatArray[ix_beg].t1_space += (unsigned short)beg_space;
1442                 StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;
1443 
1444                 StatArray[ix_end].t1_count++;
1445                 StatArray[ix_end].t1_space += (unsigned short)end_space;
1446                 StatArray[ix_end].t1_age    = StatArray[ix_end].t1_age < compile_id ? compile_id : StatArray[ix_end].t1_age;
1447               } else {
1448                 nBlocks_t2++;
1449                 t2Space   += hb_bytelen;
1450                 StatArray[ix_beg].t2_count++;
1451                 StatArray[ix_beg].t2_space += (unsigned short)beg_space;
1452                 StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;
1453 
1454                 StatArray[ix_end].t2_count++;
1455                 StatArray[ix_end].t2_space += (unsigned short)end_space;
1456                 StatArray[ix_end].t2_age    = StatArray[ix_end].t2_age < compile_id ? compile_id : StatArray[ix_end].t2_age;
1457               }
1458               StatArray[ix_beg].level     = comp_lvl;
1459               StatArray[ix_beg].compiler  = cType;
1460               StatArray[ix_end].level     = comp_lvl;
1461               StatArray[ix_end].compiler  = cType;
1462               break;
1463             case nMethod_alive:
1464               StatArray[ix_beg].tx_count++;
1465               StatArray[ix_beg].tx_space += (unsigned short)beg_space;
1466               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
1467 
1468               StatArray[ix_end].tx_count++;
1469               StatArray[ix_end].tx_space += (unsigned short)end_space;
1470               StatArray[ix_end].tx_age    = StatArray[ix_end].tx_age < compile_id ? compile_id : StatArray[ix_end].tx_age;
1471 
1472               StatArray[ix_beg].level     = comp_lvl;
1473               StatArray[ix_beg].compiler  = cType;
1474               StatArray[ix_end].level     = comp_lvl;
1475               StatArray[ix_end].compiler  = cType;
1476               break;
1477             case nMethod_dead:
1478             case nMethod_unloaded:
1479               StatArray[ix_beg].dead_count++;
1480               StatArray[ix_beg].dead_space += (unsigned short)beg_space;
1481               StatArray[ix_end].dead_count++;
1482               StatArray[ix_end].dead_space += (unsigned short)end_space;
1483               break;
1484             default:
1485               // must be a stub, if it's not a dead or alive nMethod
1486               nBlocks_stub++;
1487               stubSpace   += hb_bytelen;
1488               StatArray[ix_beg].stub_count++;
1489               StatArray[ix_beg].stub_space += (unsigned short)beg_space;
1490               StatArray[ix_end].stub_count++;
1491               StatArray[ix_end].stub_space += (unsigned short)end_space;
1492               break;
1493           }
1494           for (unsigned int ix = ix_beg+1; ix < ix_end; ix++) {
1495             StatArray[ix].type = cbType;
1496             switch (cbType) {
1497               case nMethod_inuse:
1498                 if (comp_lvl < CompLevel_full_optimization) {
1499                   StatArray[ix].t1_count++;
1500                   StatArray[ix].t1_space += (unsigned short)(granule_size>>_log2_segment_size);
1501                   StatArray[ix].t1_age    = StatArray[ix].t1_age < compile_id ? compile_id : StatArray[ix].t1_age;
1502                 } else {
1503                   StatArray[ix].t2_count++;
1504                   StatArray[ix].t2_space += (unsigned short)(granule_size>>_log2_segment_size);
1505                   StatArray[ix].t2_age    = StatArray[ix].t2_age < compile_id ? compile_id : StatArray[ix].t2_age;
1506                 }
1507                 StatArray[ix].level     = comp_lvl;
1508                 StatArray[ix].compiler  = cType;
1509                 break;
1510               case nMethod_alive:
1511                 StatArray[ix].tx_count++;
1512                 StatArray[ix].tx_space += (unsigned short)(granule_size>>_log2_segment_size);
1513                 StatArray[ix].tx_age    = StatArray[ix].tx_age < compile_id ? compile_id : StatArray[ix].tx_age;
1514                 StatArray[ix].level     = comp_lvl;
1515                 StatArray[ix].compiler  = cType;
1516                 break;
1517               case nMethod_dead:
1518               case nMethod_unloaded:
1519                 StatArray[ix].dead_count++;
1520                 StatArray[ix].dead_space += (unsigned short)(granule_size>>_log2_segment_size);
1521                 break;
1522               default:
1523                 // must be a stub, if it's not a dead or alive nMethod
1524                 StatArray[ix].stub_count++;
1525                 StatArray[ix].stub_space += (unsigned short)(granule_size>>_log2_segment_size);
1526                 break;
1527             }
1528           }
1529         }
1530       }
1531     }
1532     if (n_methods > 0) {
1533       avgTemp = hotnessAccumulator/n_methods;
1534     } else {
1535       avgTemp = 0;
1536     }
1537     done = true;
1538 
1539     if (!insane) {
1540       ttyLocker ttyl; //  keep this statistics block together
1541       printBox(out, '-', "Global CodeHeap statistics for segment ", heapName);
1542       out->print_cr("freeSpace        = " SIZE_FORMAT_W(8) "k, nBlocks_free     = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", freeSpace/(size_t)K,     nBlocks_free,     (100.0*freeSpace)/size,     (100.0*freeSpace)/res_size);
1543       out->print_cr("usedSpace        = " SIZE_FORMAT_W(8) "k, nBlocks_used     = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", usedSpace/(size_t)K,     nBlocks_used,     (100.0*usedSpace)/size,     (100.0*usedSpace)/res_size);
1544       out->print_cr("  Tier1 Space    = " SIZE_FORMAT_W(8) "k, nBlocks_t1       = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", t1Space/(size_t)K,       nBlocks_t1,       (100.0*t1Space)/size,       (100.0*t1Space)/res_size);
1545       out->print_cr("  Tier2 Space    = " SIZE_FORMAT_W(8) "k, nBlocks_t2       = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", t2Space/(size_t)K,       nBlocks_t2,       (100.0*t2Space)/size,       (100.0*t2Space)/res_size);
1546       out->print_cr("  Alive Space    = " SIZE_FORMAT_W(8) "k, nBlocks_alive    = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", aliveSpace/(size_t)K,    nBlocks_alive,    (100.0*aliveSpace)/size,    (100.0*aliveSpace)/res_size);
1547       out->print_cr("    disconnected = " SIZE_FORMAT_W(8) "k, nBlocks_disconn  = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", disconnSpace/(size_t)K,  nBlocks_disconn,  (100.0*disconnSpace)/size,  (100.0*disconnSpace)/res_size);
1548       out->print_cr("    not entrant  = " SIZE_FORMAT_W(8) "k, nBlocks_notentr  = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", notentrSpace/(size_t)K,  nBlocks_notentr,  (100.0*notentrSpace)/size,  (100.0*notentrSpace)/res_size);
1549       out->print_cr("  unloadedSpace  = " SIZE_FORMAT_W(8) "k, nBlocks_unloaded = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", unloadedSpace/(size_t)K, nBlocks_unloaded, (100.0*unloadedSpace)/size, (100.0*unloadedSpace)/res_size);
1550       out->print_cr("  deadSpace      = " SIZE_FORMAT_W(8) "k, nBlocks_dead     = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", deadSpace/(size_t)K,     nBlocks_dead,     (100.0*deadSpace)/size,     (100.0*deadSpace)/res_size);
1551       out->print_cr("  stubSpace      = " SIZE_FORMAT_W(8) "k, nBlocks_stub     = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", stubSpace/(size_t)K,     nBlocks_stub,     (100.0*stubSpace)/size,     (100.0*stubSpace)/res_size);
1552       out->print_cr("ZombieBlocks     = %8d. These are HeapBlocks which could not be identified as CodeBlobs.", nBlocks_zomb);
1553       out->print_cr("latest allocated compilation id = %d", latest_compilation_id);
1554       out->print_cr("highest observed compilation id = %d", highest_compilation_id);
1555       out->print_cr("Building TopSizeList iterations = %ld", total_iterations);
1556       out->cr();
1557 
1558       int             reset_val = NMethodSweeper::hotness_counter_reset_val();
1559       double reverse_free_ratio = (res_size > size) ? (double)res_size/(double)(res_size-size) : (double)res_size;
1560       printBox(out, '-', "Method hotness information at time of this analysis", NULL);
1561       out->print_cr("Highest possible method temperature:          %12d", reset_val);
1562       out->print_cr("Threshold for method to be considered 'cold': %12.3f", -reset_val + reverse_free_ratio * NmethodSweepActivity);
1563       out->print_cr("min. hotness = %6d", minTemp);
1564       out->print_cr("avg. hotness = %6d", avgTemp);
1565       out->print_cr("max. hotness = %6d", maxTemp);
1566       out->cr();
1567 
1568       out->print("Verifying collected data...");
1569       for (unsigned int ix = 0; ix < granules; ix++) {
1570         if (StatArray[ix].t1_count   > granule_size>>_log2_segment_size) out->print_cr("t1_count[%d]   = %d", ix, StatArray[ix].t1_count);
1571         if (StatArray[ix].t2_count   > granule_size>>_log2_segment_size) out->print_cr("t2_count[%d]   = %d", ix, StatArray[ix].t2_count);
1572         if (StatArray[ix].stub_count > granule_size>>_log2_segment_size) out->print_cr("stub_count[%d] = %d", ix, StatArray[ix].stub_count);
1573         if (StatArray[ix].dead_count > granule_size>>_log2_segment_size) out->print_cr("dead_count[%d] = %d", ix, StatArray[ix].dead_count);
1574         if (StatArray[ix].t1_space   > granule_size>>_log2_segment_size) out->print_cr("t1_space[%d]   = %d", ix, StatArray[ix].t1_space);
1575         if (StatArray[ix].t2_space   > granule_size>>_log2_segment_size) out->print_cr("t2_space[%d]   = %d", ix, StatArray[ix].t2_space);
1576         if (StatArray[ix].stub_space > granule_size>>_log2_segment_size) out->print_cr("stub_space[%d] = %d", ix, StatArray[ix].stub_space);
1577         if (StatArray[ix].dead_space > granule_size>>_log2_segment_size) out->print_cr("dead_space[%d] = %d", ix, StatArray[ix].dead_space);
1578         //   this cast is awful! I need it because NT/Intel reports a signed/unsigned mismatch.
1579         if ((size_t)(StatArray[ix].t1_count+StatArray[ix].t2_count+StatArray[ix].stub_count+StatArray[ix].dead_count) > granule_size>>_log2_segment_size) out->print_cr("t1_count[%d] = %d, t2_count[%d] = %d, stub_count[%d] = %d", ix, StatArray[ix].t1_count, ix, StatArray[ix].t2_count, ix, StatArray[ix].stub_count);
1580         if ((size_t)(StatArray[ix].t1_space+StatArray[ix].t2_space+StatArray[ix].stub_space+StatArray[ix].dead_space) > granule_size>>_log2_segment_size) out->print_cr("t1_space[%d] = %d, t2_space[%d] = %d, stub_space[%d] = %d", ix, StatArray[ix].t1_space, ix, StatArray[ix].t2_space, ix, StatArray[ix].stub_space);
1581       }
1582 
1583       if (used_topSizeBlocks > 0) {
1584         unsigned int j = 0;
1585         if (TopSizeArray[0].len != currMax) out->print_cr("currMax(%d) differs from TopSizeArray[0].len(%d)", currMax, TopSizeArray[0].len);
1586         for (unsigned int i = 0; (TopSizeArray[i].index != tsbStopper) && (j++ < alloc_topSizeBlocks); i = TopSizeArray[i].index) {
1587           if (TopSizeArray[i].len < TopSizeArray[TopSizeArray[i].index].len) {
1588             out->print_cr("sort error at index %d: %d !>= %d", i, TopSizeArray[i].len, TopSizeArray[TopSizeArray[i].index].len);
1589           }
1590         }
1591         if (j >= alloc_topSizeBlocks) {
1592           out->print_cr("Possible loop in TopSizeArray chaining!\n  allocBlocks = %d, usedBlocks = %d", alloc_topSizeBlocks, used_topSizeBlocks);
1593           for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1594             out->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1595           }
1596         }
1597       }
1598       out->print_cr("...done");
1599       out->cr();
1600       out->cr();
1601     } else {
1602       // insane heap state detected. Analysis data incomplete. Just throw it away.
1603       discard_StatArray(out);
1604       discard_TopSizeArray(out);
1605     }
1606   }
1607 
1608 
1609   done        = false;
1610   while (!done && (nBlocks_free > 0)) {
1611 
1612     printBox(out, '=', "C O D E   H E A P   A N A L Y S I S   (free blocks) for segment ", heapName);
1613     out->print_cr("The aggregate step collects information about all free blocks in CodeHeap.\n"
1614                   "Subsequent print functions create their output based on this snapshot.\n");
1615     out->print_cr("Free space in %s is distributed over %d free blocks.", heapName, nBlocks_free);
1616     out->print_cr("Each free block takes " SIZE_FORMAT " bytes of C heap for statistics data, that is " SIZE_FORMAT "K in total.", sizeof(FreeBlk), (sizeof(FreeBlk)*nBlocks_free)/K);
1617     out->cr();
1618 
1619     //----------------------------------------
1620     //--  Prepare the FreeArray of FreeBlks --
1621     //----------------------------------------
1622 
1623     //---< discard old array if size does not match  >---
1624     if (nBlocks_free != alloc_freeBlocks) {
1625       discard_FreeArray(out);
1626     }
1627 
1628     prepare_FreeArray(out, nBlocks_free, heapName);
1629     if (FreeArray == NULL) {
1630       done = true;
1631       continue;
1632     }
1633 
1634     //----------------------------------------
1635     //--  Collect all FreeBlks in FreeArray --
1636     //----------------------------------------
1637 
1638     unsigned int ix = 0;
1639     FreeBlock *cur  = _freelist;
1640 
1641     while (cur != NULL) {
1642       if (ix < alloc_freeBlocks) { // don't index out of bounds if _freelist has more blocks than anticipated
1643         FreeArray[ix].start = cur;
1644         FreeArray[ix].len   = (unsigned int)(cur->length()<<_log2_segment_size);
1645         FreeArray[ix].index = ix;
1646       }
1647       cur  = cur->link();
1648       ix++;
1649     }
1650     if (ix != alloc_freeBlocks) {
1651       out->print_cr("Free block count mismatch. Expected %d free blocks, but found %d.", alloc_freeBlocks, ix);
1652       out->print_cr("I will update the counter and retry data collection");
1653       out->cr();
1654       nBlocks_free = ix;
1655       continue;
1656     }
1657     done = true;
1658   }
1659 
1660   if (!done || (nBlocks_free == 0)) {
1661     if (nBlocks_free == 0) {
1662       printBox(out, '-', "no free blocks found in", heapName);
1663     } else if (!done) {
1664       out->print_cr("Free block count mismatch could not be resolved.");
1665       out->print_cr("Try to run \"aggregate\" function to update counters");
1666     }
1667 
1668     //---< discard old array and update global values  >---
1669     discard_FreeArray(out);
1670     set_HeapStatGlobals(out, heapName);
1671     return;
1672   }
1673 
1674   //---<  calculate and fill remaining fields  >---
1675   for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1676     size_t lenSum = 0;
1677     // Make sure FreeArray is not NULL [coverity].
1678     // Program logic makes this impossible, but we need Coverity be happy.
1679     guarantee(FreeArray != NULL, "CodeHeap::aggregate - FreeArray must not be NULL");
1680     FreeArray[ix].gap = (unsigned int)((address)FreeArray[ix+1].start - ((address)FreeArray[ix].start + FreeArray[ix].len));
1681     for (HeapBlock *h = next_block(FreeArray[ix].start); (h != NULL) && (h != FreeArray[ix+1].start); h = next_block(h)) {
1682       CodeBlob *cb  = (CodeBlob*) find_start(h);
1683       if ((cb != NULL) && !cb->is_nmethod()) {
1684         FreeArray[ix].stubs_in_gap = true;
1685       }
1686       FreeArray[ix].n_gapBlocks++;
1687       lenSum += h->length()<<_log2_segment_size;
1688       if (((address)h < ((address)FreeArray[ix].start+FreeArray[ix].len)) || (h >= FreeArray[ix+1].start)) {
1689         out->print_cr("unsorted occupied CodeHeap block found @ %p, gap interval [%p, %p)", h, (address)FreeArray[ix].start+FreeArray[ix].len, FreeArray[ix+1].start);
1690       }
1691     }
1692     if (lenSum != FreeArray[ix].gap) {
1693       out->print_cr("Length mismatch for gap between FreeBlk[%d] and FreeBlk[%d]. Calculated: %d, accumulated: %d.", ix, ix+1, FreeArray[ix].gap, (unsigned int)lenSum);
1694     }
1695   }
1696   set_HeapStatGlobals(out, heapName);
1697 
1698   printBox(out, '=', "C O D E   H E A P   A N A L Y S I S   C O M P L E T E   for segment ", heapName);
1699 }
1700 
1701 
1702 void CodeHeap::print_usedSpace(outputStream *out) {
1703   if (!initialization_complete) return;
1704 
1705   const char *heapName   = get_heapName();
1706   get_HeapStatGlobals(out, heapName);
1707 
1708   if ((StatArray == NULL) || (TopSizeArray == NULL) || (used_topSizeBlocks == 0)) return;
1709 
1710   const char *frameLine;
1711   const char *textLine;
1712 
1713   STRINGSTREAM_DECL(ast, out)
1714 
1715   {
1716     ttyLocker ttyl; //  keep the header and legend block together
1717     printBox(out, '=', "U S E D   S P A C E   S T A T I S T I C S   for ", heapName);
1718     ast->print_cr("Note: The Top%d list of the largest used blocks associates method names\n"
1719                   "      and other identifying information with the block size data.\n"
1720                   "\n"
1721                   "      Method names are dynamically retrieved from the code cache at print time.\n"
1722                   "      Due to the living nature of the code cache and because the CodeCache_lock\n"
1723                   "      is not continuously held, the displayed name might be wrong or no name\n"
1724                   "      might be found at all. The likelihood for that to happen increases\n"
1725                   "      over time passed between analysis and print step.\n", used_topSizeBlocks);
1726     STRINGSTREAM_FLUSH("\n")
1727   }
1728 
1729   //----------------------------
1730   //--  Print Top Used Blocks --
1731   //----------------------------
1732   {
1733     ttyLocker ttyl; //  keep this statistics block together
1734     char*     low_bound = low_boundary();
1735 
1736     printBox(out, '-', "Largest Used Blocks in ", heapName);
1737     print_blobType_legend(ast);
1738     STRINGSTREAM_FLUSH("")
1739 
1740     ast->fill_to(51);
1741     ast->print("%4s", "blob");
1742     ast->fill_to(56);
1743     ast->print("%9s", "compiler");
1744     ast->fill_to(66);
1745     ast->print_cr("%6s", "method");
1746     ast->print_cr("%18s %13s %17s %4s %9s  %5s %s",      "Addr(module)      ", "offset", "size", "type", " type lvl", " temp", "Name");
1747     STRINGSTREAM_FLUSH("")
1748 
1749     //---<  print Top Ten Used Blocks  >---
1750     if (used_topSizeBlocks > 0) {
1751       unsigned int printed_topSizeBlocks = 0;
1752       for (unsigned int i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
1753         printed_topSizeBlocks++;
1754         CodeBlob*   this_blob = (CodeBlob *) find_start(TopSizeArray[i].start);
1755         nmethod*           nm = NULL;
1756         const char* blob_name = "unnamed blob";
1757         if (this_blob != NULL) {
1758           blob_name = this_blob->name();
1759           nm        = this_blob->as_nmethod_or_null();
1760           //---<  blob address  >---
1761           ast->print("%p", this_blob);
1762           ast->fill_to(19);
1763           //---<  blob offset from CodeHeap begin  >---
1764           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
1765           ast->fill_to(33);
1766         } else {
1767           //---<  block address  >---
1768           ast->print("%p", TopSizeArray[i].start);
1769           ast->fill_to(19);
1770           //---<  block offset from CodeHeap begin  >---
1771           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)TopSizeArray[i].start-low_bound));
1772           ast->fill_to(33);
1773         }
1774 
1775 
1776         //---<  print size, name, and signature (for nMethods)  >---
1777         if ((nm != NULL) && (nm->method() != NULL)) {
1778           ResourceMark rm;
1779           //---<  nMethod size in hex  >---
1780           unsigned int total_size = nm->total_size();
1781           ast->print(PTR32_FORMAT, total_size);
1782           ast->print("(%4ldK)", total_size/K);
1783           ast->fill_to(51);
1784           ast->print("  %c", blobTypeChar[TopSizeArray[i].type]);
1785           //---<  compiler information  >---
1786           ast->fill_to(56);
1787           ast->print("%5s %3d", compTypeName[TopSizeArray[i].compiler], TopSizeArray[i].level);
1788           //---<  method temperature  >---
1789           ast->fill_to(67);
1790           ast->print("%5d", nm->hotness_counter());
1791           //---<  name and signature  >---
1792           ast->fill_to(67+6);
1793           if (nm->is_in_use())      {blob_name = nm->method()->name_and_sig_as_C_string(); }
1794           if (nm->is_not_entrant()) {blob_name = nm->method()->name_and_sig_as_C_string(); }
1795           if (nm->is_zombie())      {ast->print("%14s", " zombie method"); }
1796           ast->print("%s", blob_name);
1797         } else {
1798           //---<  block size in hex  >---
1799           ast->print(PTR32_FORMAT, (unsigned int)(TopSizeArray[i].len<<_log2_segment_size));
1800           ast->print("(%4ldK)", (TopSizeArray[i].len<<_log2_segment_size)/K);
1801           //---<  no compiler information  >---
1802           ast->fill_to(56);
1803           //---<  name and signature  >---
1804           ast->fill_to(67+6);
1805           ast->print("%s", blob_name);
1806         }
1807         STRINGSTREAM_FLUSH("\n")
1808       }
1809       if (used_topSizeBlocks != printed_topSizeBlocks) {
1810         ast->print_cr("used blocks: %d, printed blocks: %d", used_topSizeBlocks, printed_topSizeBlocks);
1811         STRINGSTREAM_FLUSH("")
1812         for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1813           ast->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1814           STRINGSTREAM_FLUSH("")
1815         }
1816       }
1817       out->cr(); out->cr();
1818     }
1819   }
1820 
1821   //-----------------------------
1822   //--  Print Usage Histogram  --
1823   //-----------------------------
1824 
1825   if (SizeDistributionArray != NULL) {
1826     unsigned long total_count = 0;
1827     unsigned long total_size  = 0;
1828     const unsigned long pctFactor = 200;
1829 
1830     for (unsigned int i = 0; i < nSizeDistElements; i++) {
1831       total_count += SizeDistributionArray[i].count;
1832       total_size  += SizeDistributionArray[i].lenSum;
1833     }
1834 
1835     if ((total_count > 0) && (total_size > 0)) {
1836       printBox(out, '-', "Block count histogram for ", heapName);
1837       ast->print_cr("Note: The histogram indicates how many blocks (as a percentage\n"
1838                     "      of all blocks) have a size in the given range.\n"
1839                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
1840       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<_log2_segment_size)/M);
1841       ast->print_cr("total number of all blocks: %7ld\n", total_count);
1842       ast->print_cr("[Size Range)------avg.-size-+----count-+");
1843       STRINGSTREAM_FLUSH("")
1844       for (unsigned int i = 0; i < nSizeDistElements; i++) {
1845         if (SizeDistributionArray[i].rangeStart<<_log2_segment_size < K) {
1846           ast->print("[%5d ..%5d ): "
1847                     ,(SizeDistributionArray[i].rangeStart<<_log2_segment_size)
1848                     ,(SizeDistributionArray[i].rangeEnd<<_log2_segment_size)
1849                     );
1850         } else if (SizeDistributionArray[i].rangeStart<<_log2_segment_size < M) {
1851           ast->print("[%5ldK..%5ldK): "
1852                     ,(SizeDistributionArray[i].rangeStart<<_log2_segment_size)/K
1853                     ,(SizeDistributionArray[i].rangeEnd<<_log2_segment_size)/K
1854                     );
1855         } else {
1856           ast->print("[%5ldM..%5ldM): "
1857                     ,(SizeDistributionArray[i].rangeStart<<_log2_segment_size)/M
1858                     ,(SizeDistributionArray[i].rangeEnd<<_log2_segment_size)/M
1859                     );
1860         }
1861         ast->print(" %8d | %8d |",
1862                    SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<_log2_segment_size)/SizeDistributionArray[i].count : 0,
1863                    SizeDistributionArray[i].count);
1864 
1865         unsigned int percent = pctFactor*SizeDistributionArray[i].count/total_count;
1866         for (unsigned int j = 1; j <= percent; j++) {
1867           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1868         }
1869         STRINGSTREAM_FLUSH("\n")
1870       }
1871       out->print_cr("----------------------------+----------+\n\n");
1872 
1873       printBox(out, '-', "Contribution per size range to total size for ", heapName);
1874       ast->print_cr("Note: The histogram indicates how much space (as a percentage of all\n"
1875                     "      occupied space) is used by the blocks in the given size range.\n"
1876                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
1877       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<_log2_segment_size)/M);
1878       ast->print_cr("total number of all blocks: %7ld\n", total_count);
1879       ast->print_cr("[Size Range)------avg.-size-+----count-+");
1880       STRINGSTREAM_FLUSH("")
1881       for (unsigned int i = 0; i < nSizeDistElements; i++) {
1882         if (SizeDistributionArray[i].rangeStart<<_log2_segment_size < K) {
1883           ast->print("[%5d ..%5d ): "
1884                     ,(SizeDistributionArray[i].rangeStart<<_log2_segment_size)
1885                     ,(SizeDistributionArray[i].rangeEnd<<_log2_segment_size)
1886                     );
1887         } else if (SizeDistributionArray[i].rangeStart<<_log2_segment_size < M) {
1888           ast->print("[%5ldK..%5ldK): "
1889                     ,(SizeDistributionArray[i].rangeStart<<_log2_segment_size)/K
1890                     ,(SizeDistributionArray[i].rangeEnd<<_log2_segment_size)/K
1891                     );
1892         } else {
1893           ast->print("[%5ldM..%5ldM): "
1894                     ,(SizeDistributionArray[i].rangeStart<<_log2_segment_size)/M
1895                     ,(SizeDistributionArray[i].rangeEnd<<_log2_segment_size)/M
1896                     );
1897         }
1898         ast->print(" %8d | %8d |",
1899                    SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<_log2_segment_size)/SizeDistributionArray[i].count : 0,
1900                    SizeDistributionArray[i].count);
1901 
1902         unsigned int percent = pctFactor*(unsigned long)SizeDistributionArray[i].lenSum/total_size;
1903         for (unsigned int j = 1; j <= percent; j++) {
1904           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1905         }
1906         STRINGSTREAM_FLUSH("\n")
1907       }
1908       out->print_cr("----------------------------+----------+\n\n");
1909     }
1910   }
1911 }
1912 
1913 
1914 void CodeHeap::print_freeSpace(outputStream *out) {
1915   if (!initialization_complete) return;
1916 
1917   const char *heapName   = get_heapName();
1918   get_HeapStatGlobals(out, heapName);
1919 
1920   if ((StatArray == NULL) || (FreeArray == NULL) || (alloc_granules == 0)) return;
1921 
1922   const char *frameLine;
1923   const char *textLine;
1924 
1925   STRINGSTREAM_DECL(ast, out)
1926 
1927   {
1928     ttyLocker ttyl; //  keep the header and legend block together
1929     printBox(out, '=', "F R E E   S P A C E   S T A T I S T I C S   for ", heapName);
1930     ast->print_cr("Note: in this context, a gap is the occupied space between two free blocks.\n"
1931                   "      Those gaps are of interest if there is a chance that they become\n"
1932                   "      unoccupied, e.g. by class unloading. Then, the two adjacent free\n"
1933                   "      blocks, together with the now unoccupied space, form a new, large\n"
1934                   "      free block.");
1935     ast->cr();
1936     STRINGSTREAM_FLUSH("")
1937   }
1938 
1939   {
1940     ttyLocker ttyl; //  keep this statistics block together
1941     printBox(out, '-', "List of all Free Blocks in ", heapName);
1942 
1943     unsigned int ix = 0;
1944     for (ix = 0; ix < alloc_freeBlocks-1; ix++) {
1945       ast->print("%p: Len[%4d] = " HEX32_FORMAT ",", FreeArray[ix].start, ix, FreeArray[ix].len);
1946       ast->fill_to(38);
1947       ast->print("Gap[%4d..%4d]: " HEX32_FORMAT " bytes,", ix, ix+1, FreeArray[ix].gap);
1948       ast->fill_to(71);
1949       ast->print("block count: %6d", FreeArray[ix].n_gapBlocks);
1950       if (FreeArray[ix].stubs_in_gap) {
1951         ast->print(" !! permanent gap, contains stubs and/or blobs !!");
1952       }
1953       ast->cr();
1954       STRINGSTREAM_FLUSH("")
1955     }
1956     ast->print_cr("%p: Len[%4d] = " HEX32_FORMAT, FreeArray[ix].start, ix, FreeArray[ix].len);
1957     STRINGSTREAM_FLUSH("")
1958     out->cr(); out->cr();
1959   }
1960 
1961 
1962   //-----------------------------------------
1963   //--  Find and Print Top Ten Free Blocks --
1964   //-----------------------------------------
1965 
1966   //---<  find Top Ten Free Blocks  >---
1967   const unsigned int nTop = 10;
1968   unsigned int  currMax10 = 0;
1969   struct FreeBlk     *FreeTopTen[nTop];
1970   memset(FreeTopTen, 0, sizeof(FreeTopTen));
1971 
1972   for (unsigned int ix = 0; ix < alloc_freeBlocks; ix++) {
1973     if (FreeArray[ix].len > currMax10) {  // larger than the ten largest found so far
1974       unsigned int currSize = FreeArray[ix].len;
1975 
1976       unsigned int iy;
1977       for (iy = 0; iy < nTop && FreeTopTen[iy] != NULL; iy++) {
1978         if (FreeTopTen[iy]->len < currSize) {
1979           for (unsigned int iz = nTop-1; iz > iy; iz--) { // make room to insert new free block
1980             FreeTopTen[iz] = FreeTopTen[iz-1];
1981           }
1982           FreeTopTen[iy] = &FreeArray[ix];        // insert new free block
1983           if (FreeTopTen[nTop-1] != NULL) {currMax10 = FreeTopTen[nTop-1]->len; /*out->print_cr("new currMax10 = 0x%8.8d", currMax10);*/ }
1984           break; // done with this, check next free block
1985         }
1986       }
1987       if (iy >= nTop) {
1988         out->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1989                       currSize, currMax10);
1990         continue;
1991       }
1992       if (FreeTopTen[iy] == NULL) {
1993         FreeTopTen[iy] = &FreeArray[ix];
1994         if (iy == (nTop-1)) {currMax10 = currSize; /*out->print_cr("new currMax10 = 0x%8.8d", currMax10);*/ }
1995       }
1996     }
1997   }
1998 
1999   {
2000     ttyLocker ttyl; //  keep this statistics block together
2001     printBox(out, '-', "Top Ten Free Blocks in ", heapName);
2002 
2003     //---<  print Top Ten Free Blocks  >---
2004     for (unsigned int iy = 0; (iy < nTop) && (FreeTopTen[iy] != NULL); iy++) {
2005       ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTen[iy]->index, FreeTopTen[iy]->len);
2006       ast->fill_to(39);
2007       if (FreeTopTen[iy]->index == (alloc_freeBlocks-1)) {
2008         ast->print("last free block in list.");
2009       } else {
2010         ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTen[iy]->gap);
2011         ast->fill_to(63);
2012         ast->print("#blocks (in gap) %d", FreeTopTen[iy]->n_gapBlocks);
2013       }
2014       ast->cr();
2015       STRINGSTREAM_FLUSH("")
2016     }
2017     out->cr(); out->cr();
2018   }
2019 
2020 
2021   //--------------------------------------------------------
2022   //--  Find and Print Top Ten Free-Occupied-Free Triples --
2023   //--------------------------------------------------------
2024 
2025   //---<  find and print Top Ten Triples (Free-Occupied-Free)  >---
2026   currMax10 = 0;
2027   struct FreeBlk  *FreeTopTenTriple[nTop];
2028   memset(FreeTopTenTriple, 0, sizeof(FreeTopTenTriple));
2029 
2030   for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
2031     // If there are stubs in the gap, this gap will never become completely free.
2032     // The triple will thus never merge to one free block.
2033     unsigned int lenTriple  = FreeArray[ix].len + (FreeArray[ix].stubs_in_gap ? 0 : FreeArray[ix].gap + FreeArray[ix+1].len);
2034     FreeArray[ix].len = lenTriple;
2035     if (lenTriple > currMax10) {  // larger than the ten largest found so far
2036 
2037       unsigned int iy;
2038       for (iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
2039         if (FreeTopTenTriple[iy]->len < lenTriple) {
2040           for (unsigned int iz = nTop-1; iz > iy; iz--) {
2041             FreeTopTenTriple[iz] = FreeTopTenTriple[iz-1];
2042           }
2043           FreeTopTenTriple[iy] = &FreeArray[ix];
2044           if (FreeTopTenTriple[nTop-1] != NULL) {currMax10 = FreeTopTenTriple[nTop-1]->len; }
2045           break;
2046         }
2047       }
2048       if (iy == nTop) {
2049         out->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
2050                       lenTriple, currMax10);
2051         continue;
2052       }
2053       if (FreeTopTenTriple[iy] == NULL) {
2054         FreeTopTenTriple[iy] = &FreeArray[ix];
2055         if (iy == (nTop-1)) {currMax10 = lenTriple; }
2056       }
2057     }
2058   }
2059 
2060   {
2061     ttyLocker ttyl; //  keep this statistics block together
2062     printBox(out, '-', "Top Ten Free-Occupied-Free Triples in ", heapName);
2063     ast->print_cr("  Use this information to judge how likely it is that a large(r) free block\n"
2064                   "  might get created by code cache sweeping.\n"
2065                   "  If all the occupied blocks can be swept, the three free blocks will be\n"
2066                   "  merged into one (much larger) free block. That would reduce free space\n"
2067                   "  fragmentation.\n");
2068     STRINGSTREAM_FLUSH("")
2069 
2070     //---<  print Top Ten Free-Occupied-Free Triples  >---
2071     for (unsigned int iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
2072       ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTenTriple[iy]->index, FreeTopTenTriple[iy]->len);
2073       ast->fill_to(39);
2074       ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTenTriple[iy]->gap);
2075       ast->fill_to(63);
2076       ast->print("#blocks (in gap) %d", FreeTopTenTriple[iy]->n_gapBlocks);
2077       STRINGSTREAM_FLUSH("\n")
2078     }
2079     out->cr(); out->cr();
2080   }
2081 }
2082 
2083 
2084 void CodeHeap::print_count(outputStream *out) {
2085   if (!initialization_complete) return;
2086 
2087   const char *heapName   = get_heapName();
2088   get_HeapStatGlobals(out, heapName);
2089 
2090   if ((StatArray == NULL) || (alloc_granules == 0)) return;
2091 
2092   unsigned int granules_per_line = 32;
2093   char*        low_bound         = low_boundary();
2094   const char  *frameLine;
2095   const char  *textLine;
2096 
2097   STRINGSTREAM_DECL(ast, out)
2098 
2099   {
2100     ttyLocker ttyl; //  keep the header and legend block together
2101     printBox(out, '=', "B L O C K   C O U N T S   for ", heapName);
2102     ast->print_cr("  Each granule contains an individual number of heap blocks. Large blocks\n"
2103                   "  may span multiple granules and are counted for each granule they touch.\n");
2104     if (segment_granules) {
2105       ast->print_cr("  You have selected granule size to be as small as segment size.\n"
2106                     "  As a result, each granule contains exactly one block (or a part of one block)\n"
2107                     "  or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
2108                     "  Occupied granules show their BlobType character, see legend.\n");
2109       print_blobType_legend(ast);
2110     }
2111     STRINGSTREAM_FLUSH("")
2112   }
2113 
2114   {
2115     ttyLocker ttyl; //  keep this statistics block together
2116     if (segment_granules) {
2117       printBox(out, '-', "Total (all types) count for granule size == segment size", NULL);
2118 
2119       granules_per_line = 128;
2120       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2121         print_line_delim(out, ast, ix, granules_per_line);
2122         print_blobType_single(ast, StatArray[ix].type);
2123       }
2124     } else {
2125       printBox(out, '-', "Total (all tiers) count, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
2126 
2127       granules_per_line = 128;
2128       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2129         print_line_delim(out, ast, ix, granules_per_line);
2130         unsigned int count = StatArray[ix].t1_count   + StatArray[ix].t2_count   + StatArray[ix].tx_count
2131                            + StatArray[ix].stub_count + StatArray[ix].dead_count;
2132         print_count_single(ast, count);
2133       }
2134     }
2135     STRINGSTREAM_FLUSH("|")
2136     out->cr(); out->cr(); out->cr();
2137   }
2138 
2139   {
2140     ttyLocker ttyl; //  keep this statistics block together
2141     if (nBlocks_t1 > 0) {
2142       printBox(out, '-', "Tier1 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
2143 
2144       granules_per_line = 128;
2145       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2146         print_line_delim(out, ast, ix, granules_per_line);
2147         if (segment_granules && StatArray[ix].t1_count > 0) {
2148           print_blobType_single(ast, StatArray[ix].type);
2149         } else {
2150           print_count_single(ast, StatArray[ix].t1_count);
2151         }
2152       }
2153       STRINGSTREAM_FLUSH("|")
2154     } else {
2155       ast->print("No Tier1 nMethods found in CodeHeap.");
2156       STRINGSTREAM_FLUSH("")
2157     }
2158     out->cr(); out->cr(); out->cr();
2159   }
2160 
2161   {
2162     ttyLocker ttyl; //  keep this statistics block together
2163     if (nBlocks_t2 > 0) {
2164       printBox(out, '-', "Tier2 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
2165 
2166       granules_per_line = 128;
2167       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2168         print_line_delim(out, ast, ix, granules_per_line);
2169         if (segment_granules && StatArray[ix].t2_count > 0) {
2170           print_blobType_single(ast, StatArray[ix].type);
2171         } else {
2172           print_count_single(ast, StatArray[ix].t2_count);
2173         }
2174       }
2175       STRINGSTREAM_FLUSH("|")
2176     } else {
2177       ast->print("No Tier2 nMethods found in CodeHeap.");
2178       STRINGSTREAM_FLUSH("")
2179     }
2180     out->cr(); out->cr(); out->cr();
2181   }
2182 
2183   {
2184     ttyLocker ttyl; //  keep this statistics block together
2185     if (nBlocks_alive > 0) {
2186       printBox(out, '-', "not_used/not_entrant nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
2187 
2188       granules_per_line = 128;
2189       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2190         print_line_delim(out, ast, ix, granules_per_line);
2191         if (segment_granules && StatArray[ix].tx_count > 0) {
2192           print_blobType_single(ast, StatArray[ix].type);
2193         } else {
2194           print_count_single(ast, StatArray[ix].tx_count);
2195         }
2196       }
2197       STRINGSTREAM_FLUSH("|")
2198     } else {
2199       ast->print("No not_used/not_entrant nMethods found in CodeHeap.");
2200       STRINGSTREAM_FLUSH("")
2201     }
2202     out->cr(); out->cr(); out->cr();
2203   }
2204 
2205   {
2206     ttyLocker ttyl; //  keep this statistics block together
2207     if (nBlocks_stub > 0) {
2208       printBox(out, '-', "Stub & Blob count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
2209 
2210       granules_per_line = 128;
2211       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2212         print_line_delim(out, ast, ix, granules_per_line);
2213         if (segment_granules && StatArray[ix].stub_count > 0) {
2214           print_blobType_single(ast, StatArray[ix].type);
2215         } else {
2216           print_count_single(ast, StatArray[ix].stub_count);
2217         }
2218       }
2219       STRINGSTREAM_FLUSH("|")
2220     } else {
2221       ast->print("No Stubs and Blobs found in CodeHeap.");
2222       STRINGSTREAM_FLUSH("")
2223     }
2224     out->cr(); out->cr(); out->cr();
2225   }
2226 
2227   {
2228     ttyLocker ttyl; //  keep this statistics block together
2229     if (nBlocks_dead > 0) {
2230       printBox(out, '-', "Dead nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
2231 
2232       granules_per_line = 128;
2233       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2234         print_line_delim(out, ast, ix, granules_per_line);
2235         if (segment_granules && StatArray[ix].dead_count > 0) {
2236           print_blobType_single(ast, StatArray[ix].type);
2237         } else {
2238           print_count_single(ast, StatArray[ix].dead_count);
2239         }
2240       }
2241       STRINGSTREAM_FLUSH("|")
2242     } else {
2243       ast->print("No dead nMethods found in CodeHeap.");
2244       STRINGSTREAM_FLUSH("")
2245     }
2246     out->cr(); out->cr(); out->cr();
2247   }
2248 
2249   {
2250     ttyLocker ttyl; //  keep this statistics block together
2251     if (!segment_granules) { // Prevent totally redundant printouts
2252       printBox(out, '-', "Count by tier (combined, no dead blocks): <#t1>:<#t2>:<#s>, 0x0..0xf. '*' indicates >= 16 blocks", NULL);
2253 
2254       granules_per_line = 24;
2255       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2256         print_line_delim(out, ast, ix, granules_per_line);
2257 
2258         print_count_single(ast, StatArray[ix].t1_count);
2259         ast->print(":");
2260         print_count_single(ast, StatArray[ix].t2_count);
2261         ast->print(":");
2262         if (segment_granules && StatArray[ix].stub_count > 0) print_blobType_single(ast, StatArray[ix].type);
2263         else                                                  print_count_single(ast, StatArray[ix].stub_count);
2264         ast->print(" ");
2265       }
2266       STRINGSTREAM_FLUSH("|")
2267       out->cr(); out->cr(); out->cr();
2268     }
2269   }
2270 }
2271 
2272 
2273 void CodeHeap::print_space(outputStream *out) {
2274   if (!initialization_complete) return;
2275 
2276   const char *heapName   = get_heapName();
2277   get_HeapStatGlobals(out, heapName);
2278 
2279   if ((StatArray == NULL) || (alloc_granules == 0)) return;
2280 
2281   unsigned int granules_per_line = 32;
2282   const char  *frameLine;
2283   const char  *textLine;
2284 
2285   STRINGSTREAM_DECL(ast, out)
2286 
2287   {
2288     ttyLocker ttyl; //  keep the header and legend block together
2289     printBox(out, '=', "S P A C E   U S A G E  &  F R A G M E N T A T I O N   for ", heapName);
2290     ast->print_cr("  The heap space covered by one granule is occupied to a various extend.\n"
2291                   "  The granule occupancy is displayed by one decimal digit per granule.\n");
2292     if (segment_granules) {
2293       ast->print_cr("  You have selected granule size to be as small as segment size.\n"
2294                     "  As a result, each granule contains exactly one block (or a part of one block)\n"
2295                     "  or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
2296                     "  Occupied granules show their BlobType character, see legend.\n");
2297       print_blobType_legend(ast);
2298     } else {
2299       ast->print_cr("  These digits represent a fill percentage range (see legend).\n");
2300       print_space_legend(ast);
2301     }
2302     STRINGSTREAM_FLUSH("")
2303   }
2304 
2305   {
2306     ttyLocker ttyl; //  keep this statistics block together
2307     if (segment_granules) {
2308       printBox(out, '-', "Total (all types) space consumption for granule size == segment size", NULL);
2309 
2310       granules_per_line = 128;
2311       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2312         print_line_delim(out, ast, ix, granules_per_line);
2313         print_blobType_single(ast, StatArray[ix].type);
2314       }
2315     } else {
2316       printBox(out, '-', "Total (all types) space consumption. ' ' indicates empty, '*' indicates full.", NULL);
2317 
2318       granules_per_line = 128;
2319       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2320         print_line_delim(out, ast, ix, granules_per_line);
2321         unsigned int space    = StatArray[ix].t1_space   + StatArray[ix].t2_space  + StatArray[ix].tx_space
2322                               + StatArray[ix].stub_space + StatArray[ix].dead_space;
2323         print_space_single(ast, space);
2324       }
2325     }
2326     STRINGSTREAM_FLUSH("|")
2327     out->cr(); out->cr(); out->cr();
2328   }
2329 
2330   {
2331     ttyLocker ttyl; //  keep this statistics block together
2332     if (nBlocks_t1 > 0) {
2333       printBox(out, '-', "Tier1 space consumption. ' ' indicates empty, '*' indicates full", NULL);
2334 
2335       granules_per_line = 128;
2336       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2337         print_line_delim(out, ast, ix, granules_per_line);
2338         if (segment_granules && StatArray[ix].t1_space > 0) {
2339           print_blobType_single(ast, StatArray[ix].type);
2340         } else {
2341           print_space_single(ast, StatArray[ix].t1_space);
2342         }
2343       }
2344       STRINGSTREAM_FLUSH("|")
2345     } else {
2346       ast->print("No Tier1 nMethods found in CodeHeap.");
2347       STRINGSTREAM_FLUSH("")
2348     }
2349     out->cr(); out->cr(); out->cr();
2350   }
2351 
2352   {
2353     ttyLocker ttyl; //  keep this statistics block together
2354     if (nBlocks_t2 > 0) {
2355       printBox(out, '-', "Tier2 space consumption. ' ' indicates empty, '*' indicates full", NULL);
2356 
2357       granules_per_line = 128;
2358       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2359         print_line_delim(out, ast, ix, granules_per_line);
2360         if (segment_granules && StatArray[ix].t2_space > 0) {
2361           print_blobType_single(ast, StatArray[ix].type);
2362         } else {
2363           print_space_single(ast, StatArray[ix].t2_space);
2364         }
2365       }
2366       STRINGSTREAM_FLUSH("|")
2367     } else {
2368       ast->print("No Tier2 nMethods found in CodeHeap.");
2369       STRINGSTREAM_FLUSH("")
2370     }
2371     out->cr(); out->cr(); out->cr();
2372   }
2373 
2374   {
2375     ttyLocker ttyl; //  keep this statistics block together
2376     if (nBlocks_alive > 0) {
2377       printBox(out, '-', "not_used/not_entrant space consumption. ' ' indicates empty, '*' indicates full", NULL);
2378 
2379       granules_per_line = 128;
2380       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2381         print_line_delim(out, ast, ix, granules_per_line);
2382         if (segment_granules && StatArray[ix].tx_space > 0) {
2383           print_blobType_single(ast, StatArray[ix].type);
2384         } else {
2385           print_space_single(ast, StatArray[ix].tx_space);
2386         }
2387       }
2388       STRINGSTREAM_FLUSH("|")
2389     } else {
2390       ast->print("No Tier2 nMethods found in CodeHeap.");
2391       STRINGSTREAM_FLUSH("")
2392     }
2393     out->cr(); out->cr(); out->cr();
2394   }
2395 
2396   {
2397     ttyLocker ttyl; //  keep this statistics block together
2398     if (nBlocks_stub > 0) {
2399       printBox(out, '-', "Stub and Blob space consumption. ' ' indicates empty, '*' indicates full", NULL);
2400 
2401       granules_per_line = 128;
2402       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2403         print_line_delim(out, ast, ix, granules_per_line);
2404         if (segment_granules && StatArray[ix].stub_space > 0) {
2405           print_blobType_single(ast, StatArray[ix].type);
2406         } else {
2407           print_space_single(ast, StatArray[ix].stub_space);
2408         }
2409       }
2410       STRINGSTREAM_FLUSH("|")
2411     } else {
2412       ast->print("No Stubs and Blobs found in CodeHeap.");
2413       STRINGSTREAM_FLUSH("")
2414     }
2415     out->cr(); out->cr(); out->cr();
2416   }
2417 
2418   {
2419     ttyLocker ttyl; //  keep this statistics block together
2420     if (nBlocks_dead > 0) {
2421       printBox(out, '-', "Dead space consumption. ' ' indicates empty, '*' indicates full", NULL);
2422 
2423       granules_per_line = 128;
2424       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2425         print_line_delim(out, ast, ix, granules_per_line);
2426         print_space_single(ast, StatArray[ix].dead_space);
2427       }
2428       STRINGSTREAM_FLUSH("|")
2429     } else {
2430       ast->print("No dead nMethods found in CodeHeap.");
2431       STRINGSTREAM_FLUSH("")
2432     }
2433     out->cr(); out->cr(); out->cr();
2434   }
2435 
2436   {
2437     ttyLocker ttyl; //  keep this statistics block together
2438     if (!segment_granules) { // Prevent totally redundant printouts
2439       printBox(out, '-', "Space consumption by tier (combined): <t1%>:<t2%>:<s%>. ' ' indicates empty, '*' indicates full", NULL);
2440 
2441       granules_per_line = 24;
2442       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2443         print_line_delim(out, ast, ix, granules_per_line);
2444 
2445         if (segment_granules && StatArray[ix].t1_space > 0) {
2446           print_blobType_single(ast, StatArray[ix].type);
2447         } else {
2448           print_space_single(ast, StatArray[ix].t1_space);
2449         }
2450         ast->print(":");
2451         if (segment_granules && StatArray[ix].t2_space > 0) {
2452           print_blobType_single(ast, StatArray[ix].type);
2453         } else {
2454           print_space_single(ast, StatArray[ix].t2_space);
2455         }
2456         ast->print(":");
2457         if (segment_granules && StatArray[ix].stub_space > 0) {
2458           print_blobType_single(ast, StatArray[ix].type);
2459         } else {
2460           print_space_single(ast, StatArray[ix].stub_space);
2461         }
2462         ast->print(" ");
2463       }
2464       STRINGSTREAM_FLUSH("|")
2465       out->cr(); out->cr(); out->cr();
2466     }
2467   }
2468 }
2469 
2470 void CodeHeap::print_age(outputStream *out) {
2471   if (!initialization_complete) return;
2472 
2473   const char *heapName   = get_heapName();
2474   get_HeapStatGlobals(out, heapName);
2475 
2476   if ((StatArray == NULL) || (alloc_granules == 0)) return;
2477 
2478   unsigned int granules_per_line = 32;
2479   const char  *frameLine;
2480   const char  *textLine;
2481 
2482   STRINGSTREAM_DECL(ast, out)
2483 
2484   {
2485     ttyLocker ttyl; //  keep the header and legend block together
2486     printBox(out, '=', "M E T H O D   A G E   by CompileID for ", heapName);
2487     ast->print_cr("  The age of a compiled method in the CodeHeap is not available as a\n"
2488                   "  time stamp. Instead, a relative age is deducted from the method's compilation ID.\n"
2489                   "  Age information is available for tier1 and tier2 methods only. There is no\n"
2490                   "  age information for stubs and blobs, because they have no compilation ID assigned.\n"
2491                   "  Information for the youngest method (highest ID) in the granule is printed.\n"
2492                   "  Refer to the legend to learn how method age is mapped to the displayed digit.");
2493     print_age_legend(ast);
2494     STRINGSTREAM_FLUSH("")
2495   }
2496 
2497   {
2498     ttyLocker ttyl; //  keep this statistics block together
2499     printBox(out, '-', "Age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2500 
2501     granules_per_line = 128;
2502     for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2503       print_line_delim(out, ast, ix, granules_per_line);
2504       unsigned int age1      = StatArray[ix].t1_age;
2505       unsigned int age2      = StatArray[ix].t2_age;
2506       unsigned int agex      = StatArray[ix].tx_age;
2507       unsigned int age       = age1 > age2 ? age1 : age2;
2508       age       = age > agex ? age : agex;
2509       print_age_single(ast, age);
2510     }
2511     STRINGSTREAM_FLUSH("|")
2512     out->cr(); out->cr(); out->cr();
2513   }
2514 
2515   {
2516     ttyLocker ttyl; //  keep this statistics block together
2517     if (nBlocks_t1 > 0) {
2518       printBox(out, '-', "Tier1 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2519 
2520       granules_per_line = 128;
2521       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2522         print_line_delim(out, ast, ix, granules_per_line);
2523         print_age_single(ast, StatArray[ix].t1_age);
2524       }
2525       STRINGSTREAM_FLUSH("|")
2526     } else {
2527       ast->print("No Tier1 nMethods found in CodeHeap.");
2528       STRINGSTREAM_FLUSH("")
2529     }
2530     out->cr(); out->cr(); out->cr();
2531   }
2532 
2533   {
2534     ttyLocker ttyl; //  keep this statistics block together
2535     if (nBlocks_t2 > 0) {
2536       printBox(out, '-', "Tier2 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2537 
2538       granules_per_line = 128;
2539       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2540         print_line_delim(out, ast, ix, granules_per_line);
2541         print_age_single(ast, StatArray[ix].t2_age);
2542       }
2543       STRINGSTREAM_FLUSH("|")
2544     } else {
2545       ast->print("No Tier2 nMethods found in CodeHeap.");
2546       STRINGSTREAM_FLUSH("")
2547     }
2548     out->cr(); out->cr(); out->cr();
2549   }
2550 
2551   {
2552     ttyLocker ttyl; //  keep this statistics block together
2553     if (nBlocks_alive > 0) {
2554       printBox(out, '-', "not_used/not_entrant age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2555 
2556       granules_per_line = 128;
2557       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2558         print_line_delim(out, ast, ix, granules_per_line);
2559         print_age_single(ast, StatArray[ix].tx_age);
2560       }
2561       STRINGSTREAM_FLUSH("|")
2562     } else {
2563       ast->print("No Tier2 nMethods found in CodeHeap.");
2564       STRINGSTREAM_FLUSH("")
2565     }
2566     out->cr(); out->cr(); out->cr();
2567   }
2568 
2569   {
2570     ttyLocker ttyl; //  keep this statistics block together
2571     if (!segment_granules) { // Prevent totally redundant printouts
2572       printBox(out, '-', "age distribution by tier <a1>:<a2>. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2573 
2574       granules_per_line = 32;
2575       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2576         print_line_delim(out, ast, ix, granules_per_line);
2577         print_age_single(ast, StatArray[ix].t1_age);
2578         ast->print(":");
2579         print_age_single(ast, StatArray[ix].t2_age);
2580         ast->print(" ");
2581       }
2582       STRINGSTREAM_FLUSH("|")
2583       out->cr(); out->cr(); out->cr();
2584     }
2585   }
2586 }
2587 
2588 
2589 void CodeHeap::print_names(outputStream *out) {
2590   if (!initialization_complete) return;
2591 
2592   const char *heapName   = get_heapName();
2593   get_HeapStatGlobals(out, heapName);
2594 
2595   if ((StatArray == NULL) || (alloc_granules == 0)) return;
2596 
2597   unsigned int granules_per_line  = 128;
2598   char*        low_bound          = low_boundary();
2599   CodeBlob*    last_blob          = NULL;
2600   bool         name_in_addr_range = true;
2601 
2602   STRINGSTREAM_DECL(ast, out)
2603 
2604   //---<  print at least 128K per block  >---
2605   if (granules_per_line*granule_size < 128*K) {
2606     granules_per_line = (unsigned int)((128*K)/granule_size);
2607   }
2608 
2609   ttyLocker ttyl; //  keep this statistics block together
2610 
2611   printBox(out, '=', "M E T H O D   N A M E S   for ", heapName);
2612   ast->print_cr("  Method names are dynamically retrieved from the code cache at print time.\n"
2613                 "  Due to the living nature of the code heap and because the CodeCache_lock\n"
2614                 "  is not continuously held, the displayed name might be wrong or no name\n"
2615                 "  might be found at all. The likelihood for that to happen increases\n"
2616                 "  over time passed between analysis and print step.\n");
2617   STRINGSTREAM_FLUSH("")
2618 
2619   for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2620     //---<  print a new blob on a new line  >---
2621     if (ix%granules_per_line == 0) {
2622       if (!name_in_addr_range) ast->print_cr("No methods, blobs, or stubs found in this address range");
2623       name_in_addr_range = false;
2624 
2625       ast->cr();
2626       ast->print_cr("--------------------------------------------------------------------");
2627       ast->print_cr("Address range [%p,%p), " SIZE_FORMAT "k", low_bound+ix*granule_size, low_bound+(ix+granules_per_line)*granule_size, granules_per_line*granule_size/(size_t)K);
2628       ast->print_cr("--------------------------------------------------------------------");
2629       STRINGSTREAM_FLUSH("")
2630     }
2631     for (unsigned int is = 0; is < granule_size; is+=(unsigned int)_segment_size) {
2632       CodeBlob* this_blob = (CodeBlob *) find_start(low_bound+ix*granule_size+is);
2633       if ((this_blob != NULL) && (this_blob != last_blob)) {
2634         if (!name_in_addr_range) {
2635           name_in_addr_range = true;
2636           ast->fill_to(51);
2637           ast->print("%9s", "compiler");
2638           ast->fill_to(61);
2639           ast->print_cr("%6s", "method");
2640           ast->print_cr("%18s %13s %17s %9s  %5s %18s  %s", "Addr(module)      ", "offset", "size", " type lvl", " temp", "blobType          ", "Name");
2641         }
2642 
2643         //---<  Print blobTypeName as recorded during analysis  >---
2644         ast->print("%p", this_blob);
2645         ast->fill_to(19);
2646         ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
2647         ast->fill_to(33);
2648 
2649         //---<  print size, name, and signature (for nMethods)  >---
2650         const char *blob_name = this_blob->name();
2651         nmethod*           nm = this_blob->as_nmethod_or_null();
2652         blobType       cbType = noType;
2653         if (segment_granules) {
2654           cbType = (blobType)StatArray[ix].type;
2655         } else {
2656           cbType = get_cbType(this_blob);
2657         }
2658         if ((nm != NULL) && (nm->method() != NULL)) {
2659           ResourceMark rm;
2660           //---<  nMethod size in hex  >---
2661           unsigned int total_size = nm->total_size();
2662           ast->print(PTR32_FORMAT, total_size);
2663           ast->print("(%4ldK)", total_size/K);
2664           //---<  compiler information  >---
2665           ast->fill_to(51);
2666           ast->print("%5s %3d", compTypeName[StatArray[ix].compiler], StatArray[ix].level);
2667           //---<  method temperature  >---
2668           ast->fill_to(62);
2669           ast->print("%5d", nm->hotness_counter());
2670           //---<  name and signature  >---
2671           ast->fill_to(62+6);
2672           ast->print("%s", blobTypeName[cbType]);
2673           ast->fill_to(82+6);
2674           if (nm->is_in_use())      {blob_name = nm->method()->name_and_sig_as_C_string(); }
2675           if (nm->is_not_entrant()) {blob_name = nm->method()->name_and_sig_as_C_string(); }
2676           if (nm->is_zombie())      {ast->print("%14s", " zombie method"); }
2677           ast->print("%s", blob_name);
2678         } else {
2679           ast->fill_to(62+6);
2680           ast->print("%s", blobTypeName[cbType]);
2681           ast->fill_to(82+6);
2682           ast->print("%s", blob_name);
2683         }
2684         STRINGSTREAM_FLUSH("\n")
2685         last_blob          = this_blob;
2686       }
2687     }
2688   }
2689   out->cr(); out->cr();
2690 }
2691 
2692 
2693 void CodeHeap::printBox(outputStream* out, const char border, const char* text1, const char* text2) {
2694   int lineLen = 1 + 2 + 2 +1;
2695   char edge, frame;
2696 
2697   STRINGSTREAM_DECL(ast, out)
2698 
2699   if (text1 != NULL) lineLen += strlen(text1);
2700   if (text2 != NULL) lineLen += strlen(text2);
2701   if (border == '-') {
2702     edge  = '+';
2703     frame = '|';
2704   } else {
2705     edge  = border;
2706     frame = border;
2707   }
2708 
2709   ast->print("%c", edge);
2710   for (int i = 0; i < lineLen-2; i++) { ast->print("%c", border); }
2711   ast->print_cr("%c", edge);
2712 
2713   ast->print("%c  ", frame);
2714   if (text1 != NULL) ast->print("%s", text1);
2715   if (text2 != NULL) ast->print("%s", text2);
2716   ast->print_cr("  %c", frame);
2717 
2718   ast->print("%c", edge);
2719   for (int i = 0; i < lineLen-2; i++) { ast->print("%c", border); }
2720   ast->print_cr("%c", edge);
2721 
2722   STRINGSTREAM_FLUSH("")
2723 }
2724 
2725 void CodeHeap::print_blobType_legend(outputStream *out) {
2726   out->cr();
2727   out->print_cr("  +---------------------------------------------------+");
2728   out->print_cr("  |  Block types used in the following CodeHeap dump  |");
2729   out->print_cr("  +---------------------------------------------------+");
2730   for (int type = noType; type < lastType; type += 1) {
2731     out->print_cr("  %c - %s", blobTypeChar[type], blobTypeName[type]);
2732   }
2733   out->print_cr("  -----------------------------------------------------");
2734   out->cr();
2735 }
2736 
2737 void CodeHeap::print_space_legend(outputStream *out) {
2738   unsigned int indicator = 0;
2739   unsigned int age_range = 256;
2740   unsigned int range_beg = latest_compilation_id;
2741   out->cr();
2742   out->print_cr("  +--------------------------------------------+");
2743   out->print_cr("  |  Space ranges, based on granule occupancy  |");
2744   out->print_cr("  +--------------------------------------------+");
2745   out->print_cr("    -   0%% == occupancy");
2746   for (int i=0; i<=9; i++) {
2747     out->print_cr("  %d - %3d%% < occupancy < %3d%%", i, 10*i, 10*(i+1));
2748   }
2749   out->print_cr("  * - 100%% == occupancy");
2750   out->print_cr("  ----------------------------------------------");
2751   out->cr();
2752 }
2753 
2754 void CodeHeap::print_age_legend(outputStream *out) {
2755   unsigned int indicator = 0;
2756   unsigned int age_range = 256;
2757   unsigned int range_beg = latest_compilation_id;
2758   out->cr();
2759   out->print_cr("  +---------------------------------------+");
2760   out->print_cr("  |  Age ranges, based on compilation id  |");
2761   out->print_cr("  +---------------------------------------+");
2762   while (age_range > 0) {
2763     out->print_cr("  %d - %6d to %6d", indicator, range_beg, latest_compilation_id - latest_compilation_id/age_range);
2764     range_beg = latest_compilation_id - latest_compilation_id/age_range;
2765     age_range /= 2;
2766     indicator += 1;
2767   }
2768   out->print_cr("  -----------------------------------------");
2769   out->cr();
2770 }
2771 
2772 void CodeHeap::print_blobType_single(outputStream *out, u2 /* blobType */ type) {
2773   out->print("%c", blobTypeChar[type]);
2774 }
2775 
2776 void CodeHeap::print_count_single(outputStream *out, unsigned short count) {
2777   if (count >= 16)    out->print("*");
2778   else if (count > 0) out->print("%1.1x", count);
2779   else                out->print(" ");
2780 }
2781 
2782 void CodeHeap::print_space_single(outputStream *out, unsigned short space) {
2783   size_t  space_in_bytes = ((unsigned int)space)<<_log2_segment_size;
2784   char    fraction       = (space == 0) ? ' ' : (space_in_bytes >= granule_size-1) ? '*' : char('0'+10*space_in_bytes/granule_size);
2785   out->print("%c", fraction);
2786 }
2787 
2788 void CodeHeap::print_age_single(outputStream *out, unsigned int age) {
2789   unsigned int indicator = 0;
2790   unsigned int age_range = 256;
2791   if (age > 0) {
2792     while ((age_range > 0) && (latest_compilation_id-age > latest_compilation_id/age_range)) {
2793       age_range /= 2;
2794       indicator += 1;
2795     }
2796     out->print("%c", char('0'+indicator));
2797   } else {
2798     out->print(" ");
2799   }
2800 }
2801 
2802 void CodeHeap::print_line_delim(outputStream *out, outputStream* ast, unsigned int ix, unsigned int gpl) {
2803   if (ix % gpl == 0) {
2804     char* low_bound = low_boundary();
2805     if (ix > 0) {
2806       ast->print("|");
2807     }
2808     ast->cr();
2809     assert(out == ast, "must use the same stream!");
2810 
2811     ast->print("%p", low_bound + ix*granule_size);
2812     ast->fill_to(19);
2813     ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2814   }
2815 }
2816 
2817 void CodeHeap::print_line_delim(outputStream *out, bufferedStream* ast, unsigned int ix, unsigned int gpl) {
2818   if (ix % gpl == 0) {
2819     char* low_bound = low_boundary();
2820     if (ix > 0) {
2821       ast->print("|");
2822     }
2823     ast->cr();
2824     assert(out != ast, "must not use the same stream!");
2825 
2826     out->print("%s", ast->as_string());
2827     ast->reset();
2828     ast->print("%p", low_bound + ix*granule_size);
2829     ast->fill_to(19);
2830     ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2831   }
2832 }
2833 
2834 CodeHeap::blobType CodeHeap::get_cbType(CodeBlob* cb) {
2835   if (cb != NULL ) {
2836     if (cb->is_runtime_stub())                return runtimeStub;
2837     if (cb->is_deoptimization_stub())         return deoptimizationStub;
2838     if (cb->is_uncommon_trap_stub())          return uncommonTrapStub;
2839     if (cb->is_exception_stub())              return exceptionStub;
2840     if (cb->is_safepoint_stub())              return safepointStub;
2841     if (cb->is_adapter_blob())                return adapterBlob;
2842     if (cb->is_method_handles_adapter_blob()) return mh_adapterBlob;
2843     if (cb->is_buffer_blob())                 return bufferBlob;
2844 
2845     if (cb->is_nmethod() ) {
2846       if (((nmethod*)cb)->is_in_use())        return nMethod_inuse;
2847       if (((nmethod*)cb)->is_alive() && !(((nmethod*)cb)->is_not_entrant()))   return nMethod_notused;
2848       if (((nmethod*)cb)->is_alive())         return nMethod_alive;
2849       if (((nmethod*)cb)->is_unloaded())      return nMethod_unloaded;
2850       if (((nmethod*)cb)->is_zombie())        return nMethod_dead;
2851       tty->print_cr("unhandled nmethod state");
2852       return nMethod_dead;
2853     }
2854   }
2855   return noType;
2856 }
2857 //---<  END  >--- 8198691: CodeHeap State Analytics.
< prev index next >