1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "code/codeHeapState.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "runtime/sweeper.hpp"
  30 
  31 // -------------------------
  32 // |  General Description  |
  33 // -------------------------
  34 // The CodeHeap state analytics are divided in two parts.
  35 // The first part examines the entire CodeHeap and aggregates all
  36 // information that is believed useful/important.
  37 //
  38 // Aggregation condenses the information of a piece of the CodeHeap
  39 // (4096 bytes by default) into an analysis granule. These granules
  40 // contain enough detail to gain initial insight while keeping the
  41 // internal structure sizes in check.
  42 //
  43 // The CodeHeap is a living thing. Therefore, the aggregate is collected
  44 // under the CodeCache_lock. The subsequent print steps are only locked
  45 // against concurrent aggregations. That keeps the impact on
  46 // "normal operation" (JIT compiler and sweeper activity) to a minimum.
  47 //
  48 // The second part, which consists of several, independent steps,
  49 // prints the previously collected information with emphasis on
  50 // various aspects.
  51 //
  52 // Data collection and printing is done on an "on request" basis.
  53 // While no request is being processed, there is no impact on performance.
  54 // The CodeHeap state analytics do have some memory footprint.
  55 // The "aggregate" step allocates some data structures to hold the aggregated
  56 // information for later output. These data structures live until they are
  57 // explicitly discarded (function "discard") or until the VM terminates.
  58 // There is one exception: the function "all" does not leave any data
  59 // structures allocated.
  60 //
  61 // Requests for real-time, on-the-fly analysis can be issued via
  62 //   jcmd <pid> Compiler.CodeHeap_Analytics [<function>] [<granularity>]
  63 //
  64 // If you are (only) interested in how the CodeHeap looks like after running
  65 // a sample workload, you can use the command line option
  66 //   -XX:+PrintCodeHeapAnalytics
  67 // It will cause a full analysis to be written to tty. In addition, a full
  68 // analysis will be written the first time a "CodeCache full" condition is
  69 // detected.
  70 //
  71 // The command line option produces output identical to the jcmd function
  72 //   jcmd <pid> Compiler.CodeHeap_Analytics all 4096
  73 // ---------------------------------------------------------------------------------
  74 
  75 // With this declaration macro, it is possible to switch between
  76 //  - direct output into an argument-passed outputStream and
  77 //  - buffered output into a bufferedStream with subsequent flush
  78 //    of the filled buffer to the outputStream.
  79 #define USE_STRINGSTREAM
  80 #define HEX32_FORMAT  "0x%x"  // just a helper format string used below multiple times
  81 //
  82 // Writing to a bufferedStream buffer first has a significant advantage:
  83 // It uses noticeably less cpu cycles and reduces (when wirting to a
  84 // network file) the required bandwidth by at least a factor of ten.
  85 // That clearly makes up for the increased code complexity.
  86 #if defined(USE_STRINGSTREAM)
  87 #define STRINGSTREAM_DECL(_anyst, _outst)                 \
  88     /* _anyst  name of the stream as used in the code */  \
  89     /* _outst  stream where final output will go to   */  \
  90     ResourceMark rm;                                      \
  91     bufferedStream   _sstobj = bufferedStream(4*K);       \
  92     bufferedStream*  _sstbuf = &_sstobj;                  \
  93     outputStream*    _outbuf = _outst;                    \
  94     bufferedStream*  _anyst  = &_sstobj; /* any stream. Use this to just print - no buffer flush.  */
  95 
  96 #define STRINGSTREAM_FLUSH(termString)                    \
  97     _sstbuf->print("%s", termString);                     \
  98     _outbuf->print("%s", _sstbuf->as_string());           \
  99     _sstbuf->reset();
 100 
 101 #define STRINGSTREAM_FLUSH_LOCKED(termString)             \
 102     { ttyLocker ttyl;/* keep this output block together */\
 103       STRINGSTREAM_FLUSH(termString)                      \
 104     }
 105 #else
 106 #define STRINGSTREAM_DECL(_anyst, _outst)                 \
 107     outputStream*  _outbuf = _outst;                      \
 108     outputStream*  _anyst  = _outst;   /* any stream. Use this to just print - no buffer flush.  */
 109 
 110 #define STRINGSTREAM_FLUSH(termString)                    \
 111     _outbuf->print("%s", termString);
 112 
 113 #define STRINGSTREAM_FLUSH_LOCKED(termString)             \
 114     _outbuf->print("%s", termString);
 115 #endif
 116 
 117 const char  blobTypeChar[] = {' ', 'C', 'N', 'I', 'X', 'Z', 'U', 'R', '?', 'D', 'T', 'E', 'S', 'A', 'M', 'B', 'L' };
 118 const char* blobTypeName[] = {"noType"
 119                              ,     "nMethod (under construction)"
 120                              ,          "nMethod (active)"
 121                              ,               "nMethod (inactive)"
 122                              ,                    "nMethod (deopt)"
 123                              ,                         "nMethod (zombie)"
 124                              ,                              "nMethod (unloaded)"
 125                              ,                                   "runtime stub"
 126                              ,                                        "ricochet stub"
 127                              ,                                             "deopt stub"
 128                              ,                                                  "uncommon trap stub"
 129                              ,                                                       "exception stub"
 130                              ,                                                            "safepoint stub"
 131                              ,                                                                 "adapter blob"
 132                              ,                                                                      "MH adapter blob"
 133                              ,                                                                           "buffer blob"
 134                              ,                                                                                "lastType"
 135                              };
 136 const char* compTypeName[] = { "none", "c1", "c2", "jvmci" };
 137 
 138 // Be prepared for ten different CodeHeap segments. Should be enough for a few years.
 139 const  unsigned int        nSizeDistElements = 31;  // logarithmic range growth, max size: 2**32
 140 const  unsigned int        maxTopSizeBlocks  = 50;
 141 const  unsigned int        tsbStopper        = 2 * maxTopSizeBlocks;
 142 const  unsigned int        maxHeaps          = 10;
 143 static unsigned int        nHeaps            = 0;
 144 static struct CodeHeapStat CodeHeapStatArray[maxHeaps];
 145 
 146 // static struct StatElement *StatArray      = NULL;
 147 static StatElement* StatArray             = NULL;
 148 static int          log2_seg_size         = 0;
 149 static size_t       seg_size              = 0;
 150 static size_t       alloc_granules        = 0;
 151 static size_t       granule_size          = 0;
 152 static bool         segment_granules      = false;
 153 static unsigned int nBlocks_t1            = 0;  // counting "in_use" nmethods only.
 154 static unsigned int nBlocks_t2            = 0;  // counting "in_use" nmethods only.
 155 static unsigned int nBlocks_alive         = 0;  // counting "not_used" and "not_entrant" nmethods only.
 156 static unsigned int nBlocks_dead          = 0;  // counting "zombie" and "unloaded" methods only.
 157 static unsigned int nBlocks_inconstr      = 0;  // counting "inconstruction" nmethods only. This is a transient state.
 158 static unsigned int nBlocks_unloaded      = 0;  // counting "unloaded" nmethods only. This is a transient state.
 159 static unsigned int nBlocks_stub          = 0;
 160 
 161 static struct FreeBlk*          FreeArray = NULL;
 162 static unsigned int      alloc_freeBlocks = 0;
 163 
 164 static struct TopSizeBlk*    TopSizeArray = NULL;
 165 static unsigned int   alloc_topSizeBlocks = 0;
 166 static unsigned int    used_topSizeBlocks = 0;
 167 
 168 static struct SizeDistributionElement*  SizeDistributionArray = NULL;
 169 
 170 // nMethod temperature (hotness) indicators.
 171 static int                     avgTemp    = 0;
 172 static int                     maxTemp    = 0;
 173 static int                     minTemp    = 0;
 174 
 175 static unsigned int  latest_compilation_id   = 0;
 176 static volatile bool initialization_complete = false;
 177 
 178 const char* CodeHeapState::get_heapName(CodeHeap* heap) {
 179   if (SegmentedCodeCache) {
 180     return heap->name();
 181   } else {
 182     return "CodeHeap";
 183   }
 184 }
 185 
 186 // returns the index for the heap being processed.
 187 unsigned int CodeHeapState::findHeapIndex(outputStream* out, const char* heapName) {
 188   if (heapName == NULL) {
 189     return maxHeaps;
 190   }
 191   if (SegmentedCodeCache) {
 192     // Search for a pre-existing entry. If found, return that index.
 193     for (unsigned int i = 0; i < nHeaps; i++) {
 194       if (CodeHeapStatArray[i].heapName != NULL && strcmp(heapName, CodeHeapStatArray[i].heapName) == 0) {
 195         return i;
 196       }
 197     }
 198 
 199     // check if there are more code heap segments than we can handle.
 200     if (nHeaps == maxHeaps) {
 201       out->print_cr("Too many heap segments for current limit(%d).", maxHeaps);
 202       return maxHeaps;
 203     }
 204 
 205     // allocate new slot in StatArray.
 206     CodeHeapStatArray[nHeaps].heapName = heapName;
 207     return nHeaps++;
 208   } else {
 209     nHeaps = 1;
 210     CodeHeapStatArray[0].heapName = heapName;
 211     return 0; // This is the default index if CodeCache is not segmented.
 212   }
 213 }
 214 
 215 void CodeHeapState::get_HeapStatGlobals(outputStream* out, const char* heapName) {
 216   unsigned int ix = findHeapIndex(out, heapName);
 217   if (ix < maxHeaps) {
 218     StatArray             = CodeHeapStatArray[ix].StatArray;
 219     seg_size              = CodeHeapStatArray[ix].segment_size;
 220     log2_seg_size         = seg_size == 0 ? 0 : exact_log2(seg_size);
 221     alloc_granules        = CodeHeapStatArray[ix].alloc_granules;
 222     granule_size          = CodeHeapStatArray[ix].granule_size;
 223     segment_granules      = CodeHeapStatArray[ix].segment_granules;
 224     nBlocks_t1            = CodeHeapStatArray[ix].nBlocks_t1;
 225     nBlocks_t2            = CodeHeapStatArray[ix].nBlocks_t2;
 226     nBlocks_alive         = CodeHeapStatArray[ix].nBlocks_alive;
 227     nBlocks_dead          = CodeHeapStatArray[ix].nBlocks_dead;
 228     nBlocks_inconstr      = CodeHeapStatArray[ix].nBlocks_inconstr;
 229     nBlocks_unloaded      = CodeHeapStatArray[ix].nBlocks_unloaded;
 230     nBlocks_stub          = CodeHeapStatArray[ix].nBlocks_stub;
 231     FreeArray             = CodeHeapStatArray[ix].FreeArray;
 232     alloc_freeBlocks      = CodeHeapStatArray[ix].alloc_freeBlocks;
 233     TopSizeArray          = CodeHeapStatArray[ix].TopSizeArray;
 234     alloc_topSizeBlocks   = CodeHeapStatArray[ix].alloc_topSizeBlocks;
 235     used_topSizeBlocks    = CodeHeapStatArray[ix].used_topSizeBlocks;
 236     SizeDistributionArray = CodeHeapStatArray[ix].SizeDistributionArray;
 237     avgTemp               = CodeHeapStatArray[ix].avgTemp;
 238     maxTemp               = CodeHeapStatArray[ix].maxTemp;
 239     minTemp               = CodeHeapStatArray[ix].minTemp;
 240   } else {
 241     StatArray             = NULL;
 242     seg_size              = 0;
 243     log2_seg_size         = 0;
 244     alloc_granules        = 0;
 245     granule_size          = 0;
 246     segment_granules      = false;
 247     nBlocks_t1            = 0;
 248     nBlocks_t2            = 0;
 249     nBlocks_alive         = 0;
 250     nBlocks_dead          = 0;
 251     nBlocks_inconstr      = 0;
 252     nBlocks_unloaded      = 0;
 253     nBlocks_stub          = 0;
 254     FreeArray             = NULL;
 255     alloc_freeBlocks      = 0;
 256     TopSizeArray          = NULL;
 257     alloc_topSizeBlocks   = 0;
 258     used_topSizeBlocks    = 0;
 259     SizeDistributionArray = NULL;
 260     avgTemp               = 0;
 261     maxTemp               = 0;
 262     minTemp               = 0;
 263   }
 264 }
 265 
 266 void CodeHeapState::set_HeapStatGlobals(outputStream* out, const char* heapName) {
 267   unsigned int ix = findHeapIndex(out, heapName);
 268   if (ix < maxHeaps) {
 269     CodeHeapStatArray[ix].StatArray             = StatArray;
 270     CodeHeapStatArray[ix].segment_size          = seg_size;
 271     CodeHeapStatArray[ix].alloc_granules        = alloc_granules;
 272     CodeHeapStatArray[ix].granule_size          = granule_size;
 273     CodeHeapStatArray[ix].segment_granules      = segment_granules;
 274     CodeHeapStatArray[ix].nBlocks_t1            = nBlocks_t1;
 275     CodeHeapStatArray[ix].nBlocks_t2            = nBlocks_t2;
 276     CodeHeapStatArray[ix].nBlocks_alive         = nBlocks_alive;
 277     CodeHeapStatArray[ix].nBlocks_dead          = nBlocks_dead;
 278     CodeHeapStatArray[ix].nBlocks_inconstr      = nBlocks_inconstr;
 279     CodeHeapStatArray[ix].nBlocks_unloaded      = nBlocks_unloaded;
 280     CodeHeapStatArray[ix].nBlocks_stub          = nBlocks_stub;
 281     CodeHeapStatArray[ix].FreeArray             = FreeArray;
 282     CodeHeapStatArray[ix].alloc_freeBlocks      = alloc_freeBlocks;
 283     CodeHeapStatArray[ix].TopSizeArray          = TopSizeArray;
 284     CodeHeapStatArray[ix].alloc_topSizeBlocks   = alloc_topSizeBlocks;
 285     CodeHeapStatArray[ix].used_topSizeBlocks    = used_topSizeBlocks;
 286     CodeHeapStatArray[ix].SizeDistributionArray = SizeDistributionArray;
 287     CodeHeapStatArray[ix].avgTemp               = avgTemp;
 288     CodeHeapStatArray[ix].maxTemp               = maxTemp;
 289     CodeHeapStatArray[ix].minTemp               = minTemp;
 290   }
 291 }
 292 
 293 //---<  get a new statistics array  >---
 294 void CodeHeapState::prepare_StatArray(outputStream* out, size_t nElem, size_t granularity, const char* heapName) {
 295   if (StatArray == NULL) {
 296     StatArray      = new StatElement[nElem];
 297     //---<  reset some counts  >---
 298     alloc_granules = nElem;
 299     granule_size   = granularity;
 300   }
 301 
 302   if (StatArray == NULL) {
 303     //---<  just do nothing if allocation failed  >---
 304     out->print_cr("Statistics could not be collected for %s, probably out of memory.", heapName);
 305     out->print_cr("Current granularity is " SIZE_FORMAT " bytes. Try a coarser granularity.", granularity);
 306     alloc_granules = 0;
 307     granule_size   = 0;
 308   } else {
 309     //---<  initialize statistics array  >---
 310     memset((void*)StatArray, 0, nElem*sizeof(StatElement));
 311   }
 312 }
 313 
 314 //---<  get a new free block array  >---
 315 void CodeHeapState::prepare_FreeArray(outputStream* out, unsigned int nElem, const char* heapName) {
 316   if (FreeArray == NULL) {
 317     FreeArray      = new FreeBlk[nElem];
 318     //---<  reset some counts  >---
 319     alloc_freeBlocks = nElem;
 320   }
 321 
 322   if (FreeArray == NULL) {
 323     //---<  just do nothing if allocation failed  >---
 324     out->print_cr("Free space analysis cannot be done for %s, probably out of memory.", heapName);
 325     alloc_freeBlocks = 0;
 326   } else {
 327     //---<  initialize free block array  >---
 328     memset((void*)FreeArray, 0, alloc_freeBlocks*sizeof(FreeBlk));
 329   }
 330 }
 331 
 332 //---<  get a new TopSizeArray  >---
 333 void CodeHeapState::prepare_TopSizeArray(outputStream* out, unsigned int nElem, const char* heapName) {
 334   if (TopSizeArray == NULL) {
 335     TopSizeArray   = new TopSizeBlk[nElem];
 336     //---<  reset some counts  >---
 337     alloc_topSizeBlocks = nElem;
 338     used_topSizeBlocks  = 0;
 339   }
 340 
 341   if (TopSizeArray == NULL) {
 342     //---<  just do nothing if allocation failed  >---
 343     out->print_cr("Top-%d list of largest CodeHeap blocks can not be collected for %s, probably out of memory.", nElem, heapName);
 344     alloc_topSizeBlocks = 0;
 345   } else {
 346     //---<  initialize TopSizeArray  >---
 347     memset((void*)TopSizeArray, 0, nElem*sizeof(TopSizeBlk));
 348     used_topSizeBlocks  = 0;
 349   }
 350 }
 351 
 352 //---<  get a new SizeDistributionArray  >---
 353 void CodeHeapState::prepare_SizeDistArray(outputStream* out, unsigned int nElem, const char* heapName) {
 354   if (SizeDistributionArray == NULL) {
 355     SizeDistributionArray = new SizeDistributionElement[nElem];
 356   }
 357 
 358   if (SizeDistributionArray == NULL) {
 359     //---<  just do nothing if allocation failed  >---
 360     out->print_cr("Size distribution can not be collected for %s, probably out of memory.", heapName);
 361   } else {
 362     //---<  initialize SizeDistArray  >---
 363     memset((void*)SizeDistributionArray, 0, nElem*sizeof(SizeDistributionElement));
 364     // Logarithmic range growth. First range starts at _segment_size.
 365     SizeDistributionArray[log2_seg_size-1].rangeEnd = 1U;
 366     for (unsigned int i = log2_seg_size; i < nElem; i++) {
 367       SizeDistributionArray[i].rangeStart = 1U << (i     - log2_seg_size);
 368       SizeDistributionArray[i].rangeEnd   = 1U << ((i+1) - log2_seg_size);
 369     }
 370   }
 371 }
 372 
 373 //---<  get a new SizeDistributionArray  >---
 374 void CodeHeapState::update_SizeDistArray(outputStream* out, unsigned int len) {
 375   if (SizeDistributionArray != NULL) {
 376     for (unsigned int i = log2_seg_size-1; i < nSizeDistElements; i++) {
 377       if ((SizeDistributionArray[i].rangeStart <= len) && (len < SizeDistributionArray[i].rangeEnd)) {
 378         SizeDistributionArray[i].lenSum += len;
 379         SizeDistributionArray[i].count++;
 380         break;
 381       }
 382     }
 383   }
 384 }
 385 
 386 void CodeHeapState::discard_StatArray(outputStream* out) {
 387   if (StatArray != NULL) {
 388     delete StatArray;
 389     StatArray        = NULL;
 390     alloc_granules   = 0;
 391     granule_size     = 0;
 392   }
 393 }
 394 
 395 void CodeHeapState::discard_FreeArray(outputStream* out) {
 396   if (FreeArray != NULL) {
 397     delete[] FreeArray;
 398     FreeArray        = NULL;
 399     alloc_freeBlocks = 0;
 400   }
 401 }
 402 
 403 void CodeHeapState::discard_TopSizeArray(outputStream* out) {
 404   if (TopSizeArray != NULL) {
 405     delete[] TopSizeArray;
 406     TopSizeArray        = NULL;
 407     alloc_topSizeBlocks = 0;
 408     used_topSizeBlocks  = 0;
 409   }
 410 }
 411 
 412 void CodeHeapState::discard_SizeDistArray(outputStream* out) {
 413   if (SizeDistributionArray != NULL) {
 414     delete[] SizeDistributionArray;
 415     SizeDistributionArray = NULL;
 416   }
 417 }
 418 
 419 // Discard all allocated internal data structures.
 420 // This should be done after an analysis session is completed.
 421 void CodeHeapState::discard(outputStream* out, CodeHeap* heap) {
 422   if (!initialization_complete) {
 423     return;
 424   }
 425 
 426   if (nHeaps > 0) {
 427     for (unsigned int ix = 0; ix < nHeaps; ix++) {
 428       get_HeapStatGlobals(out, CodeHeapStatArray[ix].heapName);
 429       discard_StatArray(out);
 430       discard_FreeArray(out);
 431       discard_TopSizeArray(out);
 432       discard_SizeDistArray(out);
 433       set_HeapStatGlobals(out, CodeHeapStatArray[ix].heapName);
 434       CodeHeapStatArray[ix].heapName = NULL;
 435     }
 436     nHeaps = 0;
 437   }
 438 }
 439 
 440 void CodeHeapState::aggregate(outputStream* out, CodeHeap* heap, const char* granularity_request) {
 441   unsigned int nBlocks_free    = 0;
 442   unsigned int nBlocks_used    = 0;
 443   unsigned int nBlocks_zomb    = 0;
 444   unsigned int nBlocks_disconn = 0;
 445   unsigned int nBlocks_notentr = 0;
 446 
 447   //---<  max & min of TopSizeArray  >---
 448   //  it is sufficient to have these sizes as 32bit unsigned ints.
 449   //  The CodeHeap is limited in size to 4GB. Furthermore, the sizes
 450   //  are stored in _segment_size units, scaling them down by a factor of 64 (at least).
 451   unsigned int  currMax          = 0;
 452   unsigned int  currMin          = 0;
 453   unsigned int  currMin_ix       = 0;
 454   unsigned long total_iterations = 0;
 455 
 456   bool  done             = false;
 457   const int min_granules = 256;
 458   const int max_granules = 512*K; // limits analyzable CodeHeap (with segment_granules) to 32M..128M
 459                                   // results in StatArray size of 20M (= max_granules * 40 Bytes per element)
 460                                   // For a 1GB CodeHeap, the granule size must be at least 2kB to not violate the max_granles limit.
 461   const char* heapName   = get_heapName(heap);
 462   STRINGSTREAM_DECL(ast, out)
 463 
 464   if (!initialization_complete) {
 465     memset(CodeHeapStatArray, 0, sizeof(CodeHeapStatArray));
 466     initialization_complete = true;
 467 
 468     printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (general remarks)", NULL);
 469     ast->print_cr("   The code heap analysis function provides deep insights into\n"
 470                   "   the inner workings and the internal state of the Java VM's\n"
 471                   "   code cache - the place where all the JVM generated machine\n"
 472                   "   code is stored.\n"
 473                   "   \n"
 474                   "   This function is designed and provided for support engineers\n"
 475                   "   to help them understand and solve issues in customer systems.\n"
 476                   "   It is not intended for use and interpretation by other persons.\n"
 477                   "   \n");
 478     STRINGSTREAM_FLUSH("")
 479   }
 480   get_HeapStatGlobals(out, heapName);
 481 
 482 
 483   // Since we are (and must be) analyzing the CodeHeap contents under the CodeCache_lock,
 484   // all heap information is "constant" and can be safely extracted/calculated before we
 485   // enter the while() loop. Actually, the loop will only be iterated once.
 486   char*  low_bound     = heap->low_boundary();
 487   size_t size          = heap->capacity();
 488   size_t res_size      = heap->max_capacity();
 489   seg_size             = heap->segment_size();
 490   log2_seg_size        = seg_size == 0 ? 0 : exact_log2(seg_size);  // This is a global static value.
 491 
 492   if (seg_size == 0) {
 493     printBox(ast, '-', "Heap not fully initialized yet, segment size is zero for segment ", heapName);
 494     STRINGSTREAM_FLUSH("")
 495     return;
 496   }
 497 
 498   // Calculate granularity of analysis (and output).
 499   //   The CodeHeap is managed (allocated) in segments (units) of CodeCacheSegmentSize.
 500   //   The CodeHeap can become fairly large, in particular in productive real-life systems.
 501   //
 502   //   It is often neither feasible nor desirable to aggregate the data with the highest possible
 503   //   level of detail, i.e. inspecting and printing each segment on its own.
 504   //
 505   //   The granularity parameter allows to specify the level of detail available in the analysis.
 506   //   It must be a positive multiple of the segment size and should be selected such that enough
 507   //   detail is provided while, at the same time, the printed output does not explode.
 508   //
 509   //   By manipulating the granularity value, we enforce that at least min_granules units
 510   //   of analysis are available. We also enforce an upper limit of max_granules units to
 511   //   keep the amount of allocated storage in check.
 512   //
 513   //   Finally, we adjust the granularity such that each granule covers at most 64k-1 segments.
 514   //   This is necessary to prevent an unsigned short overflow while accumulating space information.
 515   //
 516   size_t granularity = strtol(granularity_request, NULL, 0);
 517   if (granularity > size) {
 518     granularity = size;
 519   }
 520   if (size/granularity < min_granules) {
 521     granularity = size/min_granules;                                   // at least min_granules granules
 522   }
 523   granularity = granularity & (~(seg_size - 1));                       // must be multiple of seg_size
 524   if (granularity < seg_size) {
 525     granularity = seg_size;                                            // must be at least seg_size
 526   }
 527   if (size/granularity > max_granules) {
 528     granularity = size/max_granules;                                   // at most max_granules granules
 529   }
 530   granularity = granularity & (~(seg_size - 1));                       // must be multiple of seg_size
 531   if (granularity>>log2_seg_size >= (1L<<sizeof(unsigned short)*8)) {
 532     granularity = ((1L<<(sizeof(unsigned short)*8))-1)<<log2_seg_size; // Limit: (64k-1) * seg_size
 533   }
 534   segment_granules = granularity == seg_size;
 535   size_t granules  = (size + (granularity-1))/granularity;
 536 
 537   printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (used blocks) for segment ", heapName);
 538   ast->print_cr("   The aggregate step takes an aggregated snapshot of the CodeHeap.\n"
 539                 "   Subsequent print functions create their output based on this snapshot.\n"
 540                 "   The CodeHeap is a living thing, and every effort has been made for the\n"
 541                 "   collected data to be consistent. Only the method names and signatures\n"
 542                 "   are retrieved at print time. That may lead to rare cases where the\n"
 543                 "   name of a method is no longer available, e.g. because it was unloaded.\n");
 544   ast->print_cr("   CodeHeap committed size " SIZE_FORMAT "K (" SIZE_FORMAT "M), reserved size " SIZE_FORMAT "K (" SIZE_FORMAT "M), %d%% occupied.",
 545                 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));
 546   ast->print_cr("   CodeHeap allocation segment size is " SIZE_FORMAT " bytes. This is the smallest possible granularity.", seg_size);
 547   ast->print_cr("   CodeHeap (committed part) is mapped to " SIZE_FORMAT " granules of size " SIZE_FORMAT " bytes.", granules, granularity);
 548   ast->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);
 549   ast->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));
 550   STRINGSTREAM_FLUSH("\n")
 551 
 552 
 553   while (!done) {
 554     //---<  reset counters with every aggregation  >---
 555     nBlocks_t1       = 0;
 556     nBlocks_t2       = 0;
 557     nBlocks_alive    = 0;
 558     nBlocks_dead     = 0;
 559     nBlocks_inconstr = 0;
 560     nBlocks_unloaded = 0;
 561     nBlocks_stub     = 0;
 562 
 563     nBlocks_free     = 0;
 564     nBlocks_used     = 0;
 565     nBlocks_zomb     = 0;
 566     nBlocks_disconn  = 0;
 567     nBlocks_notentr  = 0;
 568 
 569     //---<  discard old arrays if size does not match  >---
 570     if (granules != alloc_granules) {
 571       discard_StatArray(out);
 572       discard_TopSizeArray(out);
 573     }
 574 
 575     //---<  allocate arrays if they don't yet exist, initialize  >---
 576     prepare_StatArray(out, granules, granularity, heapName);
 577     if (StatArray == NULL) {
 578       set_HeapStatGlobals(out, heapName);
 579       return;
 580     }
 581     prepare_TopSizeArray(out, maxTopSizeBlocks, heapName);
 582     prepare_SizeDistArray(out, nSizeDistElements, heapName);
 583 
 584     latest_compilation_id = CompileBroker::get_compilation_id();
 585     unsigned int highest_compilation_id = 0;
 586     size_t       usedSpace     = 0;
 587     size_t       t1Space       = 0;
 588     size_t       t2Space       = 0;
 589     size_t       aliveSpace    = 0;
 590     size_t       disconnSpace  = 0;
 591     size_t       notentrSpace  = 0;
 592     size_t       deadSpace     = 0;
 593     size_t       inconstrSpace = 0;
 594     size_t       unloadedSpace = 0;
 595     size_t       stubSpace     = 0;
 596     size_t       freeSpace     = 0;
 597     size_t       maxFreeSize   = 0;
 598     HeapBlock*   maxFreeBlock  = NULL;
 599     bool         insane        = false;
 600 
 601     int64_t hotnessAccumulator = 0;
 602     unsigned int n_methods     = 0;
 603     avgTemp       = 0;
 604     minTemp       = (int)(res_size > M ? (res_size/M)*2 : 1);
 605     maxTemp       = -minTemp;
 606 
 607     for (HeapBlock *h = heap->first_block(); h != NULL && !insane; h = heap->next_block(h)) {
 608       unsigned int hb_len     = (unsigned int)h->length();  // despite being size_t, length can never overflow an unsigned int.
 609       size_t       hb_bytelen = ((size_t)hb_len)<<log2_seg_size;
 610       unsigned int ix_beg     = (unsigned int)(((char*)h-low_bound)/granule_size);
 611       unsigned int ix_end     = (unsigned int)(((char*)h-low_bound+(hb_bytelen-1))/granule_size);
 612       unsigned int compile_id = 0;
 613       CompLevel    comp_lvl   = CompLevel_none;
 614       compType     cType      = noComp;
 615       blobType     cbType     = noType;
 616 
 617       //---<  some sanity checks  >---
 618       // Do not assert here, just check, print error message and return.
 619       // This is a diagnostic function. It is not supposed to tear down the VM.
 620       if ((char*)h <  low_bound) {
 621         insane = true; ast->print_cr("Sanity check: HeapBlock @%p below low bound (%p)", (char*)h, low_bound);
 622       }
 623       if ((char*)h >  (low_bound + res_size)) {
 624         insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside reserved range (%p)", (char*)h, low_bound + res_size);
 625       }
 626       if ((char*)h >  (low_bound + size)) {
 627         insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside used range (%p)", (char*)h, low_bound + size);
 628       }
 629       if (ix_end   >= granules) {
 630         insane = true; ast->print_cr("Sanity check: end index (%d) out of bounds (" SIZE_FORMAT ")", ix_end, granules);
 631       }
 632       if (size     != heap->capacity()) {
 633         insane = true; ast->print_cr("Sanity check: code heap capacity has changed (" SIZE_FORMAT "K to " SIZE_FORMAT "K)", size/(size_t)K, heap->capacity()/(size_t)K);
 634       }
 635       if (ix_beg   >  ix_end) {
 636         insane = true; ast->print_cr("Sanity check: end index (%d) lower than begin index (%d)", ix_end, ix_beg);
 637       }
 638       if (insane) {
 639         STRINGSTREAM_FLUSH("")
 640         continue;
 641       }
 642 
 643       if (h->free()) {
 644         nBlocks_free++;
 645         freeSpace    += hb_bytelen;
 646         if (hb_bytelen > maxFreeSize) {
 647           maxFreeSize   = hb_bytelen;
 648           maxFreeBlock  = h;
 649         }
 650       } else {
 651         update_SizeDistArray(out, hb_len);
 652         nBlocks_used++;
 653         usedSpace    += hb_bytelen;
 654         CodeBlob* cb  = (CodeBlob*)heap->find_start(h);
 655         if (cb != NULL) {
 656           cbType = get_cbType(cb);
 657           if (cb->is_nmethod()) {
 658             compile_id = ((nmethod*)cb)->compile_id();
 659             comp_lvl   = (CompLevel)((nmethod*)cb)->comp_level();
 660             if (((nmethod*)cb)->is_compiled_by_c1()) {
 661               cType = c1;
 662             }
 663             if (((nmethod*)cb)->is_compiled_by_c2()) {
 664               cType = c2;
 665             }
 666             if (((nmethod*)cb)->is_compiled_by_jvmci()) {
 667               cType = jvmci;
 668             }
 669             switch (cbType) {
 670               case nMethod_inuse: { // only for executable methods!!!
 671                 // space for these cbs is accounted for later.
 672                 int temperature = ((nmethod*)cb)->hotness_counter();
 673                 hotnessAccumulator += temperature;
 674                 n_methods++;
 675                 maxTemp = (temperature > maxTemp) ? temperature : maxTemp;
 676                 minTemp = (temperature < minTemp) ? temperature : minTemp;
 677                 break;
 678               }
 679               case nMethod_notused:
 680                 nBlocks_alive++;
 681                 nBlocks_disconn++;
 682                 aliveSpace     += hb_bytelen;
 683                 disconnSpace   += hb_bytelen;
 684                 break;
 685               case nMethod_notentrant:  // equivalent to nMethod_alive
 686                 nBlocks_alive++;
 687                 nBlocks_notentr++;
 688                 aliveSpace     += hb_bytelen;
 689                 notentrSpace   += hb_bytelen;
 690                 break;
 691               case nMethod_unloaded:
 692                 nBlocks_unloaded++;
 693                 unloadedSpace  += hb_bytelen;
 694                 break;
 695               case nMethod_dead:
 696                 nBlocks_dead++;
 697                 deadSpace      += hb_bytelen;
 698                 break;
 699               case nMethod_inconstruction:
 700                 nBlocks_inconstr++;
 701                 inconstrSpace  += hb_bytelen;
 702                 break;
 703               default:
 704                 break;
 705             }
 706           }
 707 
 708           //------------------------------------------
 709           //---<  register block in TopSizeArray  >---
 710           //------------------------------------------
 711           if (alloc_topSizeBlocks > 0) {
 712             if (used_topSizeBlocks == 0) {
 713               TopSizeArray[0].start    = h;
 714               TopSizeArray[0].len      = hb_len;
 715               TopSizeArray[0].index    = tsbStopper;
 716               TopSizeArray[0].compiler = cType;
 717               TopSizeArray[0].level    = comp_lvl;
 718               TopSizeArray[0].type     = cbType;
 719               currMax    = hb_len;
 720               currMin    = hb_len;
 721               currMin_ix = 0;
 722               used_topSizeBlocks++;
 723             // This check roughly cuts 5000 iterations (JVM98, mixed, dbg, termination stats):
 724             } else if ((used_topSizeBlocks < alloc_topSizeBlocks) && (hb_len < currMin)) {
 725               //---<  all blocks in list are larger, but there is room left in array  >---
 726               TopSizeArray[currMin_ix].index = used_topSizeBlocks;
 727               TopSizeArray[used_topSizeBlocks].start    = h;
 728               TopSizeArray[used_topSizeBlocks].len      = hb_len;
 729               TopSizeArray[used_topSizeBlocks].index    = tsbStopper;
 730               TopSizeArray[used_topSizeBlocks].compiler = cType;
 731               TopSizeArray[used_topSizeBlocks].level    = comp_lvl;
 732               TopSizeArray[used_topSizeBlocks].type     = cbType;
 733               currMin    = hb_len;
 734               currMin_ix = used_topSizeBlocks;
 735               used_topSizeBlocks++;
 736             } else {
 737               // This check cuts total_iterations by a factor of 6 (JVM98, mixed, dbg, termination stats):
 738               //   We don't need to search the list if we know beforehand that the current block size is
 739               //   smaller than the currently recorded minimum and there is no free entry left in the list.
 740               if (!((used_topSizeBlocks == alloc_topSizeBlocks) && (hb_len <= currMin))) {
 741                 if (currMax < hb_len) {
 742                   currMax = hb_len;
 743                 }
 744                 unsigned int i;
 745                 unsigned int prev_i  = tsbStopper;
 746                 unsigned int limit_i =  0;
 747                 for (i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
 748                   if (limit_i++ >= alloc_topSizeBlocks) {
 749                     insane = true; break; // emergency exit
 750                   }
 751                   if (i >= used_topSizeBlocks)  {
 752                     insane = true; break; // emergency exit
 753                   }
 754                   total_iterations++;
 755                   if (TopSizeArray[i].len < hb_len) {
 756                     //---<  We want to insert here, element <i> is smaller than the current one  >---
 757                     if (used_topSizeBlocks < alloc_topSizeBlocks) { // still room for a new entry to insert
 758                       // old entry gets moved to the next free element of the array.
 759                       // That's necessary to keep the entry for the largest block at index 0.
 760                       // This move might cause the current minimum to be moved to another place
 761                       if (i == currMin_ix) {
 762                         assert(TopSizeArray[i].len == currMin, "sort error");
 763                         currMin_ix = used_topSizeBlocks;
 764                       }
 765                       memcpy((void*)&TopSizeArray[used_topSizeBlocks], (void*)&TopSizeArray[i], sizeof(TopSizeBlk));
 766                       TopSizeArray[i].start    = h;
 767                       TopSizeArray[i].len      = hb_len;
 768                       TopSizeArray[i].index    = used_topSizeBlocks;
 769                       TopSizeArray[i].compiler = cType;
 770                       TopSizeArray[i].level    = comp_lvl;
 771                       TopSizeArray[i].type     = cbType;
 772                       used_topSizeBlocks++;
 773                     } else { // no room for new entries, current block replaces entry for smallest block
 774                       //---<  Find last entry (entry for smallest remembered block)  >---
 775                       unsigned int      j  = i;
 776                       unsigned int prev_j  = tsbStopper;
 777                       unsigned int limit_j = 0;
 778                       while (TopSizeArray[j].index != tsbStopper) {
 779                         if (limit_j++ >= alloc_topSizeBlocks) {
 780                           insane = true; break; // emergency exit
 781                         }
 782                         if (j >= used_topSizeBlocks)  {
 783                           insane = true; break; // emergency exit
 784                         }
 785                         total_iterations++;
 786                         prev_j = j;
 787                         j      = TopSizeArray[j].index;
 788                       }
 789                       if (!insane) {
 790                         if (prev_j == tsbStopper) {
 791                           //---<  Above while loop did not iterate, we already are the min entry  >---
 792                           //---<  We have to just replace the smallest entry                      >---
 793                           currMin    = hb_len;
 794                           currMin_ix = j;
 795                           TopSizeArray[j].start    = h;
 796                           TopSizeArray[j].len      = hb_len;
 797                           TopSizeArray[j].index    = tsbStopper; // already set!!
 798                           TopSizeArray[j].compiler = cType;
 799                           TopSizeArray[j].level    = comp_lvl;
 800                           TopSizeArray[j].type     = cbType;
 801                         } else {
 802                           //---<  second-smallest entry is now smallest  >---
 803                           TopSizeArray[prev_j].index = tsbStopper;
 804                           currMin    = TopSizeArray[prev_j].len;
 805                           currMin_ix = prev_j;
 806                           //---<  smallest entry gets overwritten  >---
 807                           memcpy((void*)&TopSizeArray[j], (void*)&TopSizeArray[i], sizeof(TopSizeBlk));
 808                           TopSizeArray[i].start    = h;
 809                           TopSizeArray[i].len      = hb_len;
 810                           TopSizeArray[i].index    = j;
 811                           TopSizeArray[i].compiler = cType;
 812                           TopSizeArray[i].level    = comp_lvl;
 813                           TopSizeArray[i].type     = cbType;
 814                         }
 815                       } // insane
 816                     }
 817                     break;
 818                   }
 819                   prev_i = i;
 820                 }
 821                 if (insane) {
 822                   // Note: regular analysis could probably continue by resetting "insane" flag.
 823                   out->print_cr("Possible loop in TopSizeBlocks list detected. Analysis aborted.");
 824                   discard_TopSizeArray(out);
 825                 }
 826               }
 827             }
 828           }
 829           //----------------------------------------------
 830           //---<  END register block in TopSizeArray  >---
 831           //----------------------------------------------
 832         } else {
 833           nBlocks_zomb++;
 834         }
 835 
 836         if (ix_beg == ix_end) {
 837           StatArray[ix_beg].type = cbType;
 838           switch (cbType) {
 839             case nMethod_inuse:
 840               highest_compilation_id = (highest_compilation_id >= compile_id) ? highest_compilation_id : compile_id;
 841               if (comp_lvl < CompLevel_full_optimization) {
 842                 nBlocks_t1++;
 843                 t1Space   += hb_bytelen;
 844                 StatArray[ix_beg].t1_count++;
 845                 StatArray[ix_beg].t1_space += (unsigned short)hb_len;
 846                 StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;
 847               } else {
 848                 nBlocks_t2++;
 849                 t2Space   += hb_bytelen;
 850                 StatArray[ix_beg].t2_count++;
 851                 StatArray[ix_beg].t2_space += (unsigned short)hb_len;
 852                 StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;
 853               }
 854               StatArray[ix_beg].level     = comp_lvl;
 855               StatArray[ix_beg].compiler  = cType;
 856               break;
 857             case nMethod_inconstruction: // let's count "in construction" nmethods here.
 858             case nMethod_alive:
 859               StatArray[ix_beg].tx_count++;
 860               StatArray[ix_beg].tx_space += (unsigned short)hb_len;
 861               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
 862               StatArray[ix_beg].level     = comp_lvl;
 863               StatArray[ix_beg].compiler  = cType;
 864               break;
 865             case nMethod_dead:
 866             case nMethod_unloaded:
 867               StatArray[ix_beg].dead_count++;
 868               StatArray[ix_beg].dead_space += (unsigned short)hb_len;
 869               break;
 870             default:
 871               // must be a stub, if it's not a dead or alive nMethod
 872               nBlocks_stub++;
 873               stubSpace   += hb_bytelen;
 874               StatArray[ix_beg].stub_count++;
 875               StatArray[ix_beg].stub_space += (unsigned short)hb_len;
 876               break;
 877           }
 878         } else {
 879           unsigned int beg_space = (unsigned int)(granule_size - ((char*)h - low_bound - ix_beg*granule_size));
 880           unsigned int end_space = (unsigned int)(hb_bytelen - beg_space - (ix_end-ix_beg-1)*granule_size);
 881           beg_space = beg_space>>log2_seg_size;  // store in units of _segment_size
 882           end_space = end_space>>log2_seg_size;  // store in units of _segment_size
 883           StatArray[ix_beg].type = cbType;
 884           StatArray[ix_end].type = cbType;
 885           switch (cbType) {
 886             case nMethod_inuse:
 887               highest_compilation_id = (highest_compilation_id >= compile_id) ? highest_compilation_id : compile_id;
 888               if (comp_lvl < CompLevel_full_optimization) {
 889                 nBlocks_t1++;
 890                 t1Space   += hb_bytelen;
 891                 StatArray[ix_beg].t1_count++;
 892                 StatArray[ix_beg].t1_space += (unsigned short)beg_space;
 893                 StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;
 894 
 895                 StatArray[ix_end].t1_count++;
 896                 StatArray[ix_end].t1_space += (unsigned short)end_space;
 897                 StatArray[ix_end].t1_age    = StatArray[ix_end].t1_age < compile_id ? compile_id : StatArray[ix_end].t1_age;
 898               } else {
 899                 nBlocks_t2++;
 900                 t2Space   += hb_bytelen;
 901                 StatArray[ix_beg].t2_count++;
 902                 StatArray[ix_beg].t2_space += (unsigned short)beg_space;
 903                 StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;
 904 
 905                 StatArray[ix_end].t2_count++;
 906                 StatArray[ix_end].t2_space += (unsigned short)end_space;
 907                 StatArray[ix_end].t2_age    = StatArray[ix_end].t2_age < compile_id ? compile_id : StatArray[ix_end].t2_age;
 908               }
 909               StatArray[ix_beg].level     = comp_lvl;
 910               StatArray[ix_beg].compiler  = cType;
 911               StatArray[ix_end].level     = comp_lvl;
 912               StatArray[ix_end].compiler  = cType;
 913               break;
 914             case nMethod_inconstruction: // let's count "in construction" nmethods here.
 915             case nMethod_alive:
 916               StatArray[ix_beg].tx_count++;
 917               StatArray[ix_beg].tx_space += (unsigned short)beg_space;
 918               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
 919 
 920               StatArray[ix_end].tx_count++;
 921               StatArray[ix_end].tx_space += (unsigned short)end_space;
 922               StatArray[ix_end].tx_age    = StatArray[ix_end].tx_age < compile_id ? compile_id : StatArray[ix_end].tx_age;
 923 
 924               StatArray[ix_beg].level     = comp_lvl;
 925               StatArray[ix_beg].compiler  = cType;
 926               StatArray[ix_end].level     = comp_lvl;
 927               StatArray[ix_end].compiler  = cType;
 928               break;
 929             case nMethod_dead:
 930             case nMethod_unloaded:
 931               StatArray[ix_beg].dead_count++;
 932               StatArray[ix_beg].dead_space += (unsigned short)beg_space;
 933               StatArray[ix_end].dead_count++;
 934               StatArray[ix_end].dead_space += (unsigned short)end_space;
 935               break;
 936             default:
 937               // must be a stub, if it's not a dead or alive nMethod
 938               nBlocks_stub++;
 939               stubSpace   += hb_bytelen;
 940               StatArray[ix_beg].stub_count++;
 941               StatArray[ix_beg].stub_space += (unsigned short)beg_space;
 942               StatArray[ix_end].stub_count++;
 943               StatArray[ix_end].stub_space += (unsigned short)end_space;
 944               break;
 945           }
 946           for (unsigned int ix = ix_beg+1; ix < ix_end; ix++) {
 947             StatArray[ix].type = cbType;
 948             switch (cbType) {
 949               case nMethod_inuse:
 950                 if (comp_lvl < CompLevel_full_optimization) {
 951                   StatArray[ix].t1_count++;
 952                   StatArray[ix].t1_space += (unsigned short)(granule_size>>log2_seg_size);
 953                   StatArray[ix].t1_age    = StatArray[ix].t1_age < compile_id ? compile_id : StatArray[ix].t1_age;
 954                 } else {
 955                   StatArray[ix].t2_count++;
 956                   StatArray[ix].t2_space += (unsigned short)(granule_size>>log2_seg_size);
 957                   StatArray[ix].t2_age    = StatArray[ix].t2_age < compile_id ? compile_id : StatArray[ix].t2_age;
 958                 }
 959                 StatArray[ix].level     = comp_lvl;
 960                 StatArray[ix].compiler  = cType;
 961                 break;
 962               case nMethod_inconstruction: // let's count "in construction" nmethods here.
 963               case nMethod_alive:
 964                 StatArray[ix].tx_count++;
 965                 StatArray[ix].tx_space += (unsigned short)(granule_size>>log2_seg_size);
 966                 StatArray[ix].tx_age    = StatArray[ix].tx_age < compile_id ? compile_id : StatArray[ix].tx_age;
 967                 StatArray[ix].level     = comp_lvl;
 968                 StatArray[ix].compiler  = cType;
 969                 break;
 970               case nMethod_dead:
 971               case nMethod_unloaded:
 972                 StatArray[ix].dead_count++;
 973                 StatArray[ix].dead_space += (unsigned short)(granule_size>>log2_seg_size);
 974                 break;
 975               default:
 976                 // must be a stub, if it's not a dead or alive nMethod
 977                 StatArray[ix].stub_count++;
 978                 StatArray[ix].stub_space += (unsigned short)(granule_size>>log2_seg_size);
 979                 break;
 980             }
 981           }
 982         }
 983       }
 984     }
 985     done = true;
 986 
 987     if (!insane) {
 988       // There is a risk for this block (because it contains many print statements) to get
 989       // interspersed with print data from other threads. We take this risk intentionally.
 990       // Getting stalled waiting for tty_lock while holding the CodeCache_lock is not desirable.
 991       printBox(ast, '-', "Global CodeHeap statistics for segment ", heapName);
 992       ast->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);
 993       ast->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);
 994       ast->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);
 995       ast->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);
 996       ast->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);
 997       ast->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);
 998       ast->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);
 999       ast->print_cr("  inconstrSpace  = " SIZE_FORMAT_W(8) "k, nBlocks_inconstr = %6d, %10.3f%% of capacity, %10.3f%% of max_capacity", inconstrSpace/(size_t)K, nBlocks_inconstr, (100.0*inconstrSpace)/size, (100.0*inconstrSpace)/res_size);
1000       ast->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);
1001       ast->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);
1002       ast->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);
1003       ast->print_cr("ZombieBlocks     = %8d. These are HeapBlocks which could not be identified as CodeBlobs.", nBlocks_zomb);
1004       ast->cr();
1005       ast->print_cr("Segment start          = " INTPTR_FORMAT ", used space      = " SIZE_FORMAT_W(8)"k", p2i(low_bound), size/K);
1006       ast->print_cr("Segment end (used)     = " INTPTR_FORMAT ", remaining space = " SIZE_FORMAT_W(8)"k", p2i(low_bound) + size, (res_size - size)/K);
1007       ast->print_cr("Segment end (reserved) = " INTPTR_FORMAT ", reserved space  = " SIZE_FORMAT_W(8)"k", p2i(low_bound) + res_size, res_size/K);
1008       ast->cr();
1009       ast->print_cr("latest allocated compilation id = %d", latest_compilation_id);
1010       ast->print_cr("highest observed compilation id = %d", highest_compilation_id);
1011       ast->print_cr("Building TopSizeList iterations = %ld", total_iterations);
1012       ast->cr();
1013 
1014       int             reset_val = NMethodSweeper::hotness_counter_reset_val();
1015       double reverse_free_ratio = (res_size > size) ? (double)res_size/(double)(res_size-size) : (double)res_size;
1016       printBox(ast, '-', "Method hotness information at time of this analysis", NULL);
1017       ast->print_cr("Highest possible method temperature:          %12d", reset_val);
1018       ast->print_cr("Threshold for method to be considered 'cold': %12.3f", -reset_val + reverse_free_ratio * NmethodSweepActivity);
1019       if (n_methods > 0) {
1020         avgTemp = hotnessAccumulator/n_methods;
1021         ast->print_cr("min. hotness = %6d", minTemp);
1022         ast->print_cr("avg. hotness = %6d", avgTemp);
1023         ast->print_cr("max. hotness = %6d", maxTemp);
1024       } else {
1025         avgTemp = 0;
1026         ast->print_cr("No hotness data available");
1027       }
1028       STRINGSTREAM_FLUSH("\n")
1029 
1030       // This loop is intentionally printing directly to "out".
1031       out->print("Verifying collected data...");
1032       size_t granule_segs = granule_size>>log2_seg_size;
1033       for (unsigned int ix = 0; ix < granules; ix++) {
1034         if (StatArray[ix].t1_count   > granule_segs) {
1035           out->print_cr("t1_count[%d]   = %d", ix, StatArray[ix].t1_count);
1036         }
1037         if (StatArray[ix].t2_count   > granule_segs) {
1038           out->print_cr("t2_count[%d]   = %d", ix, StatArray[ix].t2_count);
1039         }
1040         if (StatArray[ix].tx_count   > granule_segs) {
1041           out->print_cr("tx_count[%d]   = %d", ix, StatArray[ix].tx_count);
1042         }
1043         if (StatArray[ix].stub_count > granule_segs) {
1044           out->print_cr("stub_count[%d] = %d", ix, StatArray[ix].stub_count);
1045         }
1046         if (StatArray[ix].dead_count > granule_segs) {
1047           out->print_cr("dead_count[%d] = %d", ix, StatArray[ix].dead_count);
1048         }
1049         if (StatArray[ix].t1_space   > granule_segs) {
1050           out->print_cr("t1_space[%d]   = %d", ix, StatArray[ix].t1_space);
1051         }
1052         if (StatArray[ix].t2_space   > granule_segs) {
1053           out->print_cr("t2_space[%d]   = %d", ix, StatArray[ix].t2_space);
1054         }
1055         if (StatArray[ix].tx_space   > granule_segs) {
1056           out->print_cr("tx_space[%d]   = %d", ix, StatArray[ix].tx_space);
1057         }
1058         if (StatArray[ix].stub_space > granule_segs) {
1059           out->print_cr("stub_space[%d] = %d", ix, StatArray[ix].stub_space);
1060         }
1061         if (StatArray[ix].dead_space > granule_segs) {
1062           out->print_cr("dead_space[%d] = %d", ix, StatArray[ix].dead_space);
1063         }
1064         //   this cast is awful! I need it because NT/Intel reports a signed/unsigned mismatch.
1065         if ((size_t)(StatArray[ix].t1_count+StatArray[ix].t2_count+StatArray[ix].tx_count+StatArray[ix].stub_count+StatArray[ix].dead_count) > granule_segs) {
1066           out->print_cr("t1_count[%d] = %d, t2_count[%d] = %d, tx_count[%d] = %d, stub_count[%d] = %d", ix, StatArray[ix].t1_count, ix, StatArray[ix].t2_count, ix, StatArray[ix].tx_count, ix, StatArray[ix].stub_count);
1067         }
1068         if ((size_t)(StatArray[ix].t1_space+StatArray[ix].t2_space+StatArray[ix].tx_space+StatArray[ix].stub_space+StatArray[ix].dead_space) > granule_segs) {
1069           out->print_cr("t1_space[%d] = %d, t2_space[%d] = %d, tx_space[%d] = %d, stub_space[%d] = %d", ix, StatArray[ix].t1_space, ix, StatArray[ix].t2_space, ix, StatArray[ix].tx_space, ix, StatArray[ix].stub_space);
1070         }
1071       }
1072 
1073       // This loop is intentionally printing directly to "out".
1074       if (used_topSizeBlocks > 0) {
1075         unsigned int j = 0;
1076         if (TopSizeArray[0].len != currMax) {
1077           out->print_cr("currMax(%d) differs from TopSizeArray[0].len(%d)", currMax, TopSizeArray[0].len);
1078         }
1079         for (unsigned int i = 0; (TopSizeArray[i].index != tsbStopper) && (j++ < alloc_topSizeBlocks); i = TopSizeArray[i].index) {
1080           if (TopSizeArray[i].len < TopSizeArray[TopSizeArray[i].index].len) {
1081             out->print_cr("sort error at index %d: %d !>= %d", i, TopSizeArray[i].len, TopSizeArray[TopSizeArray[i].index].len);
1082           }
1083         }
1084         if (j >= alloc_topSizeBlocks) {
1085           out->print_cr("Possible loop in TopSizeArray chaining!\n  allocBlocks = %d, usedBlocks = %d", alloc_topSizeBlocks, used_topSizeBlocks);
1086           for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1087             out->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1088           }
1089         }
1090       }
1091       out->print_cr("...done\n\n");
1092     } else {
1093       // insane heap state detected. Analysis data incomplete. Just throw it away.
1094       discard_StatArray(out);
1095       discard_TopSizeArray(out);
1096     }
1097   }
1098 
1099 
1100   done        = false;
1101   while (!done && (nBlocks_free > 0)) {
1102 
1103     printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (free blocks) for segment ", heapName);
1104     ast->print_cr("   The aggregate step collects information about all free blocks in CodeHeap.\n"
1105                   "   Subsequent print functions create their output based on this snapshot.\n");
1106     ast->print_cr("   Free space in %s is distributed over %d free blocks.", heapName, nBlocks_free);
1107     ast->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);
1108     STRINGSTREAM_FLUSH("\n")
1109 
1110     //----------------------------------------
1111     //--  Prepare the FreeArray of FreeBlks --
1112     //----------------------------------------
1113 
1114     //---< discard old array if size does not match  >---
1115     if (nBlocks_free != alloc_freeBlocks) {
1116       discard_FreeArray(out);
1117     }
1118 
1119     prepare_FreeArray(out, nBlocks_free, heapName);
1120     if (FreeArray == NULL) {
1121       done = true;
1122       continue;
1123     }
1124 
1125     //----------------------------------------
1126     //--  Collect all FreeBlks in FreeArray --
1127     //----------------------------------------
1128 
1129     unsigned int ix = 0;
1130     FreeBlock* cur  = heap->freelist();
1131 
1132     while (cur != NULL) {
1133       if (ix < alloc_freeBlocks) { // don't index out of bounds if _freelist has more blocks than anticipated
1134         FreeArray[ix].start = cur;
1135         FreeArray[ix].len   = (unsigned int)(cur->length()<<log2_seg_size);
1136         FreeArray[ix].index = ix;
1137       }
1138       cur  = cur->link();
1139       ix++;
1140     }
1141     if (ix != alloc_freeBlocks) {
1142       ast->print_cr("Free block count mismatch. Expected %d free blocks, but found %d.", alloc_freeBlocks, ix);
1143       ast->print_cr("I will update the counter and retry data collection");
1144       STRINGSTREAM_FLUSH("\n")
1145       nBlocks_free = ix;
1146       continue;
1147     }
1148     done = true;
1149   }
1150 
1151   if (!done || (nBlocks_free == 0)) {
1152     if (nBlocks_free == 0) {
1153       printBox(ast, '-', "no free blocks found in ", heapName);
1154     } else if (!done) {
1155       ast->print_cr("Free block count mismatch could not be resolved.");
1156       ast->print_cr("Try to run \"aggregate\" function to update counters");
1157     }
1158     STRINGSTREAM_FLUSH("")
1159 
1160     //---< discard old array and update global values  >---
1161     discard_FreeArray(out);
1162     set_HeapStatGlobals(out, heapName);
1163     return;
1164   }
1165 
1166   //---<  calculate and fill remaining fields  >---
1167   if (FreeArray != NULL) {
1168     // This loop is intentionally printing directly to "out".
1169     for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1170       size_t lenSum = 0;
1171       FreeArray[ix].gap = (unsigned int)((address)FreeArray[ix+1].start - ((address)FreeArray[ix].start + FreeArray[ix].len));
1172       for (HeapBlock *h = heap->next_block(FreeArray[ix].start); (h != NULL) && (h != FreeArray[ix+1].start); h = heap->next_block(h)) {
1173         CodeBlob *cb  = (CodeBlob*)(heap->find_start(h));
1174         if ((cb != NULL) && !cb->is_nmethod()) {
1175           FreeArray[ix].stubs_in_gap = true;
1176         }
1177         FreeArray[ix].n_gapBlocks++;
1178         lenSum += h->length()<<log2_seg_size;
1179         if (((address)h < ((address)FreeArray[ix].start+FreeArray[ix].len)) || (h >= FreeArray[ix+1].start)) {
1180           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);
1181         }
1182       }
1183       if (lenSum != FreeArray[ix].gap) {
1184         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);
1185       }
1186     }
1187   }
1188   set_HeapStatGlobals(out, heapName);
1189 
1190   printBox(ast, '=', "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);
1191   STRINGSTREAM_FLUSH("\n")
1192 }
1193 
1194 
1195 void CodeHeapState::print_usedSpace(outputStream* out, CodeHeap* heap) {
1196   if (!initialization_complete) {
1197     return;
1198   }
1199 
1200   const char* heapName   = get_heapName(heap);
1201   get_HeapStatGlobals(out, heapName);
1202 
1203   if ((StatArray == NULL) || (TopSizeArray == NULL) || (used_topSizeBlocks == 0)) {
1204     return;
1205   }
1206   STRINGSTREAM_DECL(ast, out)
1207 
1208   {
1209     printBox(ast, '=', "U S E D   S P A C E   S T A T I S T I C S   for ", heapName);
1210     ast->print_cr("Note: The Top%d list of the largest used blocks associates method names\n"
1211                   "      and other identifying information with the block size data.\n"
1212                   "\n"
1213                   "      Method names are dynamically retrieved from the code cache at print time.\n"
1214                   "      Due to the living nature of the code cache and because the CodeCache_lock\n"
1215                   "      is not continuously held, the displayed name might be wrong or no name\n"
1216                   "      might be found at all. The likelihood for that to happen increases\n"
1217                   "      over time passed between analysis and print step.\n", used_topSizeBlocks);
1218     STRINGSTREAM_FLUSH_LOCKED("\n")
1219   }
1220 
1221   //----------------------------
1222   //--  Print Top Used Blocks --
1223   //----------------------------
1224   {
1225     char*     low_bound = heap->low_boundary();
1226 
1227     printBox(ast, '-', "Largest Used Blocks in ", heapName);
1228     print_blobType_legend(ast);
1229 
1230     ast->fill_to(51);
1231     ast->print("%4s", "blob");
1232     ast->fill_to(56);
1233     ast->print("%9s", "compiler");
1234     ast->fill_to(66);
1235     ast->print_cr("%6s", "method");
1236     ast->print_cr("%18s %13s %17s %4s %9s  %5s %s",      "Addr(module)      ", "offset", "size", "type", " type lvl", " temp", "Name");
1237     STRINGSTREAM_FLUSH_LOCKED("")
1238 
1239     //---<  print Top Ten Used Blocks  >---
1240     if (used_topSizeBlocks > 0) {
1241       unsigned int printed_topSizeBlocks = 0;
1242       for (unsigned int i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
1243         printed_topSizeBlocks++;
1244         CodeBlob*   this_blob = (CodeBlob*)(heap->find_start(TopSizeArray[i].start));
1245         nmethod*           nm = NULL;
1246         const char* blob_name = "unnamed blob";
1247         if (this_blob != NULL) {
1248           blob_name = this_blob->name();
1249           nm        = this_blob->as_nmethod_or_null();
1250           //---<  blob address  >---
1251           ast->print(INTPTR_FORMAT, p2i(this_blob));
1252           ast->fill_to(19);
1253           //---<  blob offset from CodeHeap begin  >---
1254           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
1255           ast->fill_to(33);
1256         } else {
1257           //---<  block address  >---
1258           ast->print(INTPTR_FORMAT, p2i(TopSizeArray[i].start));
1259           ast->fill_to(19);
1260           //---<  block offset from CodeHeap begin  >---
1261           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)TopSizeArray[i].start-low_bound));
1262           ast->fill_to(33);
1263         }
1264 
1265 
1266         //---<  print size, name, and signature (for nMethods)  >---
1267         if ((nm != NULL) && (nm->method() != NULL)) {
1268           ResourceMark rm;
1269           //---<  nMethod size in hex  >---
1270           unsigned int total_size = nm->total_size();
1271           ast->print(PTR32_FORMAT, total_size);
1272           ast->print("(" SIZE_FORMAT_W(4) "K)", total_size/K);
1273           ast->fill_to(51);
1274           ast->print("  %c", blobTypeChar[TopSizeArray[i].type]);
1275           //---<  compiler information  >---
1276           ast->fill_to(56);
1277           ast->print("%5s %3d", compTypeName[TopSizeArray[i].compiler], TopSizeArray[i].level);
1278           //---<  method temperature  >---
1279           ast->fill_to(67);
1280           ast->print("%5d", nm->hotness_counter());
1281           //---<  name and signature  >---
1282           ast->fill_to(67+6);
1283           if (nm->is_in_use())        {blob_name = nm->method()->name_and_sig_as_C_string(); }
1284           if (nm->is_not_entrant())   {blob_name = nm->method()->name_and_sig_as_C_string(); }
1285           if (nm->is_not_installed()) {ast->print("%s", " not (yet) installed method "); }
1286           if (nm->is_zombie())        {ast->print("%s", " zombie method "); }
1287           ast->print("%s", blob_name);
1288         } else {
1289           //---<  block size in hex  >---
1290           ast->print(PTR32_FORMAT, (unsigned int)(TopSizeArray[i].len<<log2_seg_size));
1291           ast->print("(" SIZE_FORMAT_W(4) "K)", (TopSizeArray[i].len<<log2_seg_size)/K);
1292           //---<  no compiler information  >---
1293           ast->fill_to(56);
1294           //---<  name and signature  >---
1295           ast->fill_to(67+6);
1296           ast->print("%s", blob_name);
1297         }
1298         STRINGSTREAM_FLUSH_LOCKED("\n")
1299       }
1300       if (used_topSizeBlocks != printed_topSizeBlocks) {
1301         ast->print_cr("used blocks: %d, printed blocks: %d", used_topSizeBlocks, printed_topSizeBlocks);
1302         STRINGSTREAM_FLUSH("")
1303         for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1304           ast->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1305           STRINGSTREAM_FLUSH("")
1306         }
1307       }
1308       STRINGSTREAM_FLUSH_LOCKED("\n\n")
1309     }
1310   }
1311 
1312   //-----------------------------
1313   //--  Print Usage Histogram  --
1314   //-----------------------------
1315 
1316   if (SizeDistributionArray != NULL) {
1317     unsigned long total_count = 0;
1318     unsigned long total_size  = 0;
1319     const unsigned long pctFactor = 200;
1320 
1321     for (unsigned int i = 0; i < nSizeDistElements; i++) {
1322       total_count += SizeDistributionArray[i].count;
1323       total_size  += SizeDistributionArray[i].lenSum;
1324     }
1325 
1326     if ((total_count > 0) && (total_size > 0)) {
1327       printBox(ast, '-', "Block count histogram for ", heapName);
1328       ast->print_cr("Note: The histogram indicates how many blocks (as a percentage\n"
1329                     "      of all blocks) have a size in the given range.\n"
1330                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
1331       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1332       ast->print_cr("total number of all blocks: %7ld\n", total_count);
1333       STRINGSTREAM_FLUSH_LOCKED("")
1334 
1335       ast->print_cr("[Size Range)------avg.-size-+----count-+");
1336       for (unsigned int i = 0; i < nSizeDistElements; i++) {
1337         if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1338           ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1339                     ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1340                     ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1341                     );
1342         } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1343           ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1344                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1345                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1346                     );
1347         } else {
1348           ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1349                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1350                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1351                     );
1352         }
1353         ast->print(" %8d | %8d |",
1354                    SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1355                    SizeDistributionArray[i].count);
1356 
1357         unsigned int percent = pctFactor*SizeDistributionArray[i].count/total_count;
1358         for (unsigned int j = 1; j <= percent; j++) {
1359           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1360         }
1361         ast->cr();
1362       }
1363       ast->print_cr("----------------------------+----------+\n\n");
1364       STRINGSTREAM_FLUSH_LOCKED("\n")
1365 
1366       printBox(ast, '-', "Contribution per size range to total size for ", heapName);
1367       ast->print_cr("Note: The histogram indicates how much space (as a percentage of all\n"
1368                     "      occupied space) is used by the blocks in the given size range.\n"
1369                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
1370       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1371       ast->print_cr("total number of all blocks: %7ld\n", total_count);
1372       STRINGSTREAM_FLUSH_LOCKED("")
1373 
1374       ast->print_cr("[Size Range)------avg.-size-+----count-+");
1375       for (unsigned int i = 0; i < nSizeDistElements; i++) {
1376         if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1377           ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1378                     ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1379                     ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1380                     );
1381         } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1382           ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1383                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1384                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1385                     );
1386         } else {
1387           ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1388                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1389                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1390                     );
1391         }
1392         ast->print(" %8d | %8d |",
1393                    SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1394                    SizeDistributionArray[i].count);
1395 
1396         unsigned int percent = pctFactor*(unsigned long)SizeDistributionArray[i].lenSum/total_size;
1397         for (unsigned int j = 1; j <= percent; j++) {
1398           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1399         }
1400         ast->cr();
1401       }
1402       ast->print_cr("----------------------------+----------+");
1403       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1404     }
1405   }
1406 }
1407 
1408 
1409 void CodeHeapState::print_freeSpace(outputStream* out, CodeHeap* heap) {
1410   if (!initialization_complete) {
1411     return;
1412   }
1413 
1414   const char* heapName   = get_heapName(heap);
1415   get_HeapStatGlobals(out, heapName);
1416 
1417   if ((StatArray == NULL) || (FreeArray == NULL) || (alloc_granules == 0)) {
1418     return;
1419   }
1420   STRINGSTREAM_DECL(ast, out)
1421 
1422   {
1423     printBox(ast, '=', "F R E E   S P A C E   S T A T I S T I C S   for ", heapName);
1424     ast->print_cr("Note: in this context, a gap is the occupied space between two free blocks.\n"
1425                   "      Those gaps are of interest if there is a chance that they become\n"
1426                   "      unoccupied, e.g. by class unloading. Then, the two adjacent free\n"
1427                   "      blocks, together with the now unoccupied space, form a new, large\n"
1428                   "      free block.");
1429     STRINGSTREAM_FLUSH_LOCKED("\n")
1430   }
1431 
1432   {
1433     printBox(ast, '-', "List of all Free Blocks in ", heapName);
1434     STRINGSTREAM_FLUSH_LOCKED("")
1435 
1436     unsigned int ix = 0;
1437     for (ix = 0; ix < alloc_freeBlocks-1; ix++) {
1438       ast->print(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT ",", p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1439       ast->fill_to(38);
1440       ast->print("Gap[%4d..%4d]: " HEX32_FORMAT " bytes,", ix, ix+1, FreeArray[ix].gap);
1441       ast->fill_to(71);
1442       ast->print("block count: %6d", FreeArray[ix].n_gapBlocks);
1443       if (FreeArray[ix].stubs_in_gap) {
1444         ast->print(" !! permanent gap, contains stubs and/or blobs !!");
1445       }
1446       STRINGSTREAM_FLUSH_LOCKED("\n")
1447     }
1448     ast->print_cr(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT, p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1449     STRINGSTREAM_FLUSH_LOCKED("\n\n")
1450   }
1451 
1452 
1453   //-----------------------------------------
1454   //--  Find and Print Top Ten Free Blocks --
1455   //-----------------------------------------
1456 
1457   //---<  find Top Ten Free Blocks  >---
1458   const unsigned int nTop = 10;
1459   unsigned int  currMax10 = 0;
1460   struct FreeBlk* FreeTopTen[nTop];
1461   memset(FreeTopTen, 0, sizeof(FreeTopTen));
1462 
1463   for (unsigned int ix = 0; ix < alloc_freeBlocks; ix++) {
1464     if (FreeArray[ix].len > currMax10) {  // larger than the ten largest found so far
1465       unsigned int currSize = FreeArray[ix].len;
1466 
1467       unsigned int iy;
1468       for (iy = 0; iy < nTop && FreeTopTen[iy] != NULL; iy++) {
1469         if (FreeTopTen[iy]->len < currSize) {
1470           for (unsigned int iz = nTop-1; iz > iy; iz--) { // make room to insert new free block
1471             FreeTopTen[iz] = FreeTopTen[iz-1];
1472           }
1473           FreeTopTen[iy] = &FreeArray[ix];        // insert new free block
1474           if (FreeTopTen[nTop-1] != NULL) {
1475             currMax10 = FreeTopTen[nTop-1]->len;
1476           }
1477           break; // done with this, check next free block
1478         }
1479       }
1480       if (iy >= nTop) {
1481         ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1482                       currSize, currMax10);
1483         continue;
1484       }
1485       if (FreeTopTen[iy] == NULL) {
1486         FreeTopTen[iy] = &FreeArray[ix];
1487         if (iy == (nTop-1)) {
1488           currMax10 = currSize;
1489         }
1490       }
1491     }
1492   }
1493   STRINGSTREAM_FLUSH_LOCKED("")
1494 
1495   {
1496     printBox(ast, '-', "Top Ten Free Blocks in ", heapName);
1497 
1498     //---<  print Top Ten Free Blocks  >---
1499     for (unsigned int iy = 0; (iy < nTop) && (FreeTopTen[iy] != NULL); iy++) {
1500       ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTen[iy]->index, FreeTopTen[iy]->len);
1501       ast->fill_to(39);
1502       if (FreeTopTen[iy]->index == (alloc_freeBlocks-1)) {
1503         ast->print("last free block in list.");
1504       } else {
1505         ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTen[iy]->gap);
1506         ast->fill_to(63);
1507         ast->print("#blocks (in gap) %d", FreeTopTen[iy]->n_gapBlocks);
1508       }
1509       ast->cr();
1510     }
1511     STRINGSTREAM_FLUSH_LOCKED("\n\n")
1512   }
1513 
1514 
1515   //--------------------------------------------------------
1516   //--  Find and Print Top Ten Free-Occupied-Free Triples --
1517   //--------------------------------------------------------
1518 
1519   //---<  find and print Top Ten Triples (Free-Occupied-Free)  >---
1520   currMax10 = 0;
1521   struct FreeBlk  *FreeTopTenTriple[nTop];
1522   memset(FreeTopTenTriple, 0, sizeof(FreeTopTenTriple));
1523 
1524   for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1525     // If there are stubs in the gap, this gap will never become completely free.
1526     // The triple will thus never merge to one free block.
1527     unsigned int lenTriple  = FreeArray[ix].len + (FreeArray[ix].stubs_in_gap ? 0 : FreeArray[ix].gap + FreeArray[ix+1].len);
1528     FreeArray[ix].len = lenTriple;
1529     if (lenTriple > currMax10) {  // larger than the ten largest found so far
1530 
1531       unsigned int iy;
1532       for (iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1533         if (FreeTopTenTriple[iy]->len < lenTriple) {
1534           for (unsigned int iz = nTop-1; iz > iy; iz--) {
1535             FreeTopTenTriple[iz] = FreeTopTenTriple[iz-1];
1536           }
1537           FreeTopTenTriple[iy] = &FreeArray[ix];
1538           if (FreeTopTenTriple[nTop-1] != NULL) {
1539             currMax10 = FreeTopTenTriple[nTop-1]->len;
1540           }
1541           break;
1542         }
1543       }
1544       if (iy == nTop) {
1545         ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1546                       lenTriple, currMax10);
1547         continue;
1548       }
1549       if (FreeTopTenTriple[iy] == NULL) {
1550         FreeTopTenTriple[iy] = &FreeArray[ix];
1551         if (iy == (nTop-1)) {
1552           currMax10 = lenTriple;
1553         }
1554       }
1555     }
1556   }
1557   STRINGSTREAM_FLUSH_LOCKED("")
1558 
1559   {
1560     printBox(ast, '-', "Top Ten Free-Occupied-Free Triples in ", heapName);
1561     ast->print_cr("  Use this information to judge how likely it is that a large(r) free block\n"
1562                   "  might get created by code cache sweeping.\n"
1563                   "  If all the occupied blocks can be swept, the three free blocks will be\n"
1564                   "  merged into one (much larger) free block. That would reduce free space\n"
1565                   "  fragmentation.\n");
1566 
1567     //---<  print Top Ten Free-Occupied-Free Triples  >---
1568     for (unsigned int iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1569       ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTenTriple[iy]->index, FreeTopTenTriple[iy]->len);
1570       ast->fill_to(39);
1571       ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTenTriple[iy]->gap);
1572       ast->fill_to(63);
1573       ast->print("#blocks (in gap) %d", FreeTopTenTriple[iy]->n_gapBlocks);
1574       ast->cr();
1575     }
1576     STRINGSTREAM_FLUSH_LOCKED("\n\n")
1577   }
1578 }
1579 
1580 
1581 void CodeHeapState::print_count(outputStream* out, CodeHeap* heap) {
1582   if (!initialization_complete) {
1583     return;
1584   }
1585 
1586   const char* heapName   = get_heapName(heap);
1587   get_HeapStatGlobals(out, heapName);
1588 
1589   if ((StatArray == NULL) || (alloc_granules == 0)) {
1590     return;
1591   }
1592   STRINGSTREAM_DECL(ast, out)
1593 
1594   unsigned int granules_per_line = 32;
1595   char*        low_bound         = heap->low_boundary();
1596 
1597   {
1598     printBox(ast, '=', "B L O C K   C O U N T S   for ", heapName);
1599     ast->print_cr("  Each granule contains an individual number of heap blocks. Large blocks\n"
1600                   "  may span multiple granules and are counted for each granule they touch.\n");
1601     if (segment_granules) {
1602       ast->print_cr("  You have selected granule size to be as small as segment size.\n"
1603                     "  As a result, each granule contains exactly one block (or a part of one block)\n"
1604                     "  or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1605                     "  Occupied granules show their BlobType character, see legend.\n");
1606       print_blobType_legend(ast);
1607     }
1608     STRINGSTREAM_FLUSH_LOCKED("")
1609   }
1610 
1611   {
1612     if (segment_granules) {
1613       printBox(ast, '-', "Total (all types) count for granule size == segment size", NULL);
1614       STRINGSTREAM_FLUSH_LOCKED("")
1615 
1616       granules_per_line = 128;
1617       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1618         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1619         print_blobType_single(ast, StatArray[ix].type);
1620       }
1621     } else {
1622       printBox(ast, '-', "Total (all tiers) count, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1623       STRINGSTREAM_FLUSH_LOCKED("")
1624 
1625       granules_per_line = 128;
1626       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1627         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1628         unsigned int count = StatArray[ix].t1_count   + StatArray[ix].t2_count   + StatArray[ix].tx_count
1629                            + StatArray[ix].stub_count + StatArray[ix].dead_count;
1630         print_count_single(ast, count);
1631       }
1632     }
1633     STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1634   }
1635 
1636   {
1637     if (nBlocks_t1 > 0) {
1638       printBox(ast, '-', "Tier1 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1639       STRINGSTREAM_FLUSH_LOCKED("")
1640 
1641       granules_per_line = 128;
1642       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1643         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1644         if (segment_granules && StatArray[ix].t1_count > 0) {
1645           print_blobType_single(ast, StatArray[ix].type);
1646         } else {
1647           print_count_single(ast, StatArray[ix].t1_count);
1648         }
1649       }
1650       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1651     } else {
1652       ast->print("No Tier1 nMethods found in CodeHeap.");
1653       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1654     }
1655   }
1656 
1657   {
1658     if (nBlocks_t2 > 0) {
1659       printBox(ast, '-', "Tier2 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1660       STRINGSTREAM_FLUSH_LOCKED("")
1661 
1662       granules_per_line = 128;
1663       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1664         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1665         if (segment_granules && StatArray[ix].t2_count > 0) {
1666           print_blobType_single(ast, StatArray[ix].type);
1667         } else {
1668           print_count_single(ast, StatArray[ix].t2_count);
1669         }
1670       }
1671       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1672     } else {
1673       ast->print("No Tier2 nMethods found in CodeHeap.");
1674       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1675     }
1676   }
1677 
1678   {
1679     if (nBlocks_alive > 0) {
1680       printBox(ast, '-', "not_used/not_entrant/not_installed nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1681       STRINGSTREAM_FLUSH_LOCKED("")
1682 
1683       granules_per_line = 128;
1684       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1685         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1686         if (segment_granules && StatArray[ix].tx_count > 0) {
1687           print_blobType_single(ast, StatArray[ix].type);
1688         } else {
1689           print_count_single(ast, StatArray[ix].tx_count);
1690         }
1691       }
1692       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1693     } else {
1694       ast->print("No not_used/not_entrant nMethods found in CodeHeap.");
1695       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1696     }
1697   }
1698 
1699   {
1700     if (nBlocks_stub > 0) {
1701       printBox(ast, '-', "Stub & Blob count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1702       STRINGSTREAM_FLUSH_LOCKED("")
1703 
1704       granules_per_line = 128;
1705       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1706         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1707         if (segment_granules && StatArray[ix].stub_count > 0) {
1708           print_blobType_single(ast, StatArray[ix].type);
1709         } else {
1710           print_count_single(ast, StatArray[ix].stub_count);
1711         }
1712       }
1713       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1714     } else {
1715       ast->print("No Stubs and Blobs found in CodeHeap.");
1716       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1717     }
1718   }
1719 
1720   {
1721     if (nBlocks_dead > 0) {
1722       printBox(ast, '-', "Dead nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1723       STRINGSTREAM_FLUSH_LOCKED("")
1724 
1725       granules_per_line = 128;
1726       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1727         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1728         if (segment_granules && StatArray[ix].dead_count > 0) {
1729           print_blobType_single(ast, StatArray[ix].type);
1730         } else {
1731           print_count_single(ast, StatArray[ix].dead_count);
1732         }
1733       }
1734       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1735     } else {
1736       ast->print("No dead nMethods found in CodeHeap.");
1737       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1738     }
1739   }
1740 
1741   {
1742     if (!segment_granules) { // Prevent totally redundant printouts
1743       printBox(ast, '-', "Count by tier (combined, no dead blocks): <#t1>:<#t2>:<#s>, 0x0..0xf. '*' indicates >= 16 blocks", NULL);
1744       STRINGSTREAM_FLUSH_LOCKED("")
1745 
1746       granules_per_line = 24;
1747       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1748         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1749 
1750         print_count_single(ast, StatArray[ix].t1_count);
1751         ast->print(":");
1752         print_count_single(ast, StatArray[ix].t2_count);
1753         ast->print(":");
1754         if (segment_granules && StatArray[ix].stub_count > 0) {
1755           print_blobType_single(ast, StatArray[ix].type);
1756         } else {
1757           print_count_single(ast, StatArray[ix].stub_count);
1758         }
1759         ast->print(" ");
1760       }
1761       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1762     }
1763   }
1764 }
1765 
1766 
1767 void CodeHeapState::print_space(outputStream* out, CodeHeap* heap) {
1768   if (!initialization_complete) {
1769     return;
1770   }
1771 
1772   const char* heapName   = get_heapName(heap);
1773   get_HeapStatGlobals(out, heapName);
1774 
1775   if ((StatArray == NULL) || (alloc_granules == 0)) {
1776     return;
1777   }
1778   STRINGSTREAM_DECL(ast, out)
1779 
1780   unsigned int granules_per_line = 32;
1781   char*        low_bound         = heap->low_boundary();
1782 
1783   {
1784     printBox(ast, '=', "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);
1785     ast->print_cr("  The heap space covered by one granule is occupied to a various extend.\n"
1786                   "  The granule occupancy is displayed by one decimal digit per granule.\n");
1787     if (segment_granules) {
1788       ast->print_cr("  You have selected granule size to be as small as segment size.\n"
1789                     "  As a result, each granule contains exactly one block (or a part of one block)\n"
1790                     "  or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1791                     "  Occupied granules show their BlobType character, see legend.\n");
1792       print_blobType_legend(ast);
1793     } else {
1794       ast->print_cr("  These digits represent a fill percentage range (see legend).\n");
1795       print_space_legend(ast);
1796     }
1797     STRINGSTREAM_FLUSH_LOCKED("")
1798   }
1799 
1800   {
1801     if (segment_granules) {
1802       printBox(ast, '-', "Total (all types) space consumption for granule size == segment size", NULL);
1803       STRINGSTREAM_FLUSH_LOCKED("")
1804 
1805       granules_per_line = 128;
1806       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1807         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1808         print_blobType_single(ast, StatArray[ix].type);
1809       }
1810     } else {
1811       printBox(ast, '-', "Total (all types) space consumption. ' ' indicates empty, '*' indicates full.", NULL);
1812       STRINGSTREAM_FLUSH_LOCKED("")
1813 
1814       granules_per_line = 128;
1815       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1816         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1817         unsigned int space    = StatArray[ix].t1_space   + StatArray[ix].t2_space  + StatArray[ix].tx_space
1818                               + StatArray[ix].stub_space + StatArray[ix].dead_space;
1819         print_space_single(ast, space);
1820       }
1821     }
1822     STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1823   }
1824 
1825   {
1826     if (nBlocks_t1 > 0) {
1827       printBox(ast, '-', "Tier1 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1828       STRINGSTREAM_FLUSH_LOCKED("")
1829 
1830       granules_per_line = 128;
1831       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1832         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1833         if (segment_granules && StatArray[ix].t1_space > 0) {
1834           print_blobType_single(ast, StatArray[ix].type);
1835         } else {
1836           print_space_single(ast, StatArray[ix].t1_space);
1837         }
1838       }
1839       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1840     } else {
1841       ast->print("No Tier1 nMethods found in CodeHeap.");
1842       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1843     }
1844   }
1845 
1846   {
1847     if (nBlocks_t2 > 0) {
1848       printBox(ast, '-', "Tier2 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1849       STRINGSTREAM_FLUSH_LOCKED("")
1850 
1851       granules_per_line = 128;
1852       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1853         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1854         if (segment_granules && StatArray[ix].t2_space > 0) {
1855           print_blobType_single(ast, StatArray[ix].type);
1856         } else {
1857           print_space_single(ast, StatArray[ix].t2_space);
1858         }
1859       }
1860       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1861     } else {
1862       ast->print("No Tier2 nMethods found in CodeHeap.");
1863       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1864     }
1865   }
1866 
1867   {
1868     if (nBlocks_alive > 0) {
1869       printBox(ast, '-', "not_used/not_entrant/not_installed space consumption. ' ' indicates empty, '*' indicates full", NULL);
1870 
1871       granules_per_line = 128;
1872       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1873         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1874         if (segment_granules && StatArray[ix].tx_space > 0) {
1875           print_blobType_single(ast, StatArray[ix].type);
1876         } else {
1877           print_space_single(ast, StatArray[ix].tx_space);
1878         }
1879       }
1880       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1881     } else {
1882       ast->print("No Tier2 nMethods found in CodeHeap.");
1883       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1884     }
1885   }
1886 
1887   {
1888     if (nBlocks_stub > 0) {
1889       printBox(ast, '-', "Stub and Blob space consumption. ' ' indicates empty, '*' indicates full", NULL);
1890       STRINGSTREAM_FLUSH_LOCKED("")
1891 
1892       granules_per_line = 128;
1893       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1894         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1895         if (segment_granules && StatArray[ix].stub_space > 0) {
1896           print_blobType_single(ast, StatArray[ix].type);
1897         } else {
1898           print_space_single(ast, StatArray[ix].stub_space);
1899         }
1900       }
1901       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1902     } else {
1903       ast->print("No Stubs and Blobs found in CodeHeap.");
1904       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1905     }
1906   }
1907 
1908   {
1909     if (nBlocks_dead > 0) {
1910       printBox(ast, '-', "Dead space consumption. ' ' indicates empty, '*' indicates full", NULL);
1911       STRINGSTREAM_FLUSH_LOCKED("")
1912 
1913       granules_per_line = 128;
1914       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1915         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1916         print_space_single(ast, StatArray[ix].dead_space);
1917       }
1918       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1919     } else {
1920       ast->print("No dead nMethods found in CodeHeap.");
1921       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1922     }
1923   }
1924 
1925   {
1926     if (!segment_granules) { // Prevent totally redundant printouts
1927       printBox(ast, '-', "Space consumption by tier (combined): <t1%>:<t2%>:<s%>. ' ' indicates empty, '*' indicates full", NULL);
1928       STRINGSTREAM_FLUSH_LOCKED("")
1929 
1930       granules_per_line = 24;
1931       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1932         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1933 
1934         if (segment_granules && StatArray[ix].t1_space > 0) {
1935           print_blobType_single(ast, StatArray[ix].type);
1936         } else {
1937           print_space_single(ast, StatArray[ix].t1_space);
1938         }
1939         ast->print(":");
1940         if (segment_granules && StatArray[ix].t2_space > 0) {
1941           print_blobType_single(ast, StatArray[ix].type);
1942         } else {
1943           print_space_single(ast, StatArray[ix].t2_space);
1944         }
1945         ast->print(":");
1946         if (segment_granules && StatArray[ix].stub_space > 0) {
1947           print_blobType_single(ast, StatArray[ix].type);
1948         } else {
1949           print_space_single(ast, StatArray[ix].stub_space);
1950         }
1951         ast->print(" ");
1952       }
1953       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1954     }
1955   }
1956 }
1957 
1958 void CodeHeapState::print_age(outputStream* out, CodeHeap* heap) {
1959   if (!initialization_complete) {
1960     return;
1961   }
1962 
1963   const char* heapName   = get_heapName(heap);
1964   get_HeapStatGlobals(out, heapName);
1965 
1966   if ((StatArray == NULL) || (alloc_granules == 0)) {
1967     return;
1968   }
1969   STRINGSTREAM_DECL(ast, out)
1970 
1971   unsigned int granules_per_line = 32;
1972   char*        low_bound         = heap->low_boundary();
1973 
1974   {
1975     printBox(ast, '=', "M E T H O D   A G E   by CompileID for ", heapName);
1976     ast->print_cr("  The age of a compiled method in the CodeHeap is not available as a\n"
1977                   "  time stamp. Instead, a relative age is deducted from the method's compilation ID.\n"
1978                   "  Age information is available for tier1 and tier2 methods only. There is no\n"
1979                   "  age information for stubs and blobs, because they have no compilation ID assigned.\n"
1980                   "  Information for the youngest method (highest ID) in the granule is printed.\n"
1981                   "  Refer to the legend to learn how method age is mapped to the displayed digit.");
1982     print_age_legend(ast);
1983     STRINGSTREAM_FLUSH_LOCKED("")
1984   }
1985 
1986   {
1987     printBox(ast, '-', "Age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
1988     STRINGSTREAM_FLUSH_LOCKED("")
1989 
1990     granules_per_line = 128;
1991     for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1992       print_line_delim(out, ast, low_bound, ix, granules_per_line);
1993       unsigned int age1      = StatArray[ix].t1_age;
1994       unsigned int age2      = StatArray[ix].t2_age;
1995       unsigned int agex      = StatArray[ix].tx_age;
1996       unsigned int age       = age1 > age2 ? age1 : age2;
1997       age       = age > agex ? age : agex;
1998       print_age_single(ast, age);
1999     }
2000     STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2001   }
2002 
2003   {
2004     if (nBlocks_t1 > 0) {
2005       printBox(ast, '-', "Tier1 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2006       STRINGSTREAM_FLUSH_LOCKED("")
2007 
2008       granules_per_line = 128;
2009       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2010         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2011         print_age_single(ast, StatArray[ix].t1_age);
2012       }
2013       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2014     } else {
2015       ast->print("No Tier1 nMethods found in CodeHeap.");
2016       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2017     }
2018   }
2019 
2020   {
2021     if (nBlocks_t2 > 0) {
2022       printBox(ast, '-', "Tier2 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2023       STRINGSTREAM_FLUSH_LOCKED("")
2024 
2025       granules_per_line = 128;
2026       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2027         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2028         print_age_single(ast, StatArray[ix].t2_age);
2029       }
2030       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2031     } else {
2032       ast->print("No Tier2 nMethods found in CodeHeap.");
2033       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2034     }
2035   }
2036 
2037   {
2038     if (nBlocks_alive > 0) {
2039       printBox(ast, '-', "not_used/not_entrant/not_installed age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2040       STRINGSTREAM_FLUSH_LOCKED("")
2041 
2042       granules_per_line = 128;
2043       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2044         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2045         print_age_single(ast, StatArray[ix].tx_age);
2046       }
2047       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2048     } else {
2049       ast->print("No Tier2 nMethods found in CodeHeap.");
2050       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2051     }
2052   }
2053 
2054   {
2055     if (!segment_granules) { // Prevent totally redundant printouts
2056       printBox(ast, '-', "age distribution by tier <a1>:<a2>. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2057       STRINGSTREAM_FLUSH_LOCKED("")
2058 
2059       granules_per_line = 32;
2060       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2061         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2062         print_age_single(ast, StatArray[ix].t1_age);
2063         ast->print(":");
2064         print_age_single(ast, StatArray[ix].t2_age);
2065         ast->print(" ");
2066       }
2067       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2068     }
2069   }
2070 }
2071 
2072 
2073 void CodeHeapState::print_names(outputStream* out, CodeHeap* heap) {
2074   if (!initialization_complete) {
2075     return;
2076   }
2077 
2078   const char* heapName   = get_heapName(heap);
2079   get_HeapStatGlobals(out, heapName);
2080 
2081   if ((StatArray == NULL) || (alloc_granules == 0)) {
2082     return;
2083   }
2084   STRINGSTREAM_DECL(ast, out)
2085 
2086   unsigned int granules_per_line  = 128;
2087   char*        low_bound          = heap->low_boundary();
2088   CodeBlob*    last_blob          = NULL;
2089   bool         name_in_addr_range = true;
2090 
2091   //---<  print at least 128K per block (i.e. between headers)  >---
2092   if (granules_per_line*granule_size < 128*K) {
2093     granules_per_line = (unsigned int)((128*K)/granule_size);
2094   }
2095 
2096   printBox(ast, '=', "M E T H O D   N A M E S   for ", heapName);
2097   ast->print_cr("  Method names are dynamically retrieved from the code cache at print time.\n"
2098                 "  Due to the living nature of the code heap and because the CodeCache_lock\n"
2099                 "  is not continuously held, the displayed name might be wrong or no name\n"
2100                 "  might be found at all. The likelihood for that to happen increases\n"
2101                 "  over time passed between aggregtion and print steps.\n");
2102   STRINGSTREAM_FLUSH_LOCKED("")
2103 
2104   for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2105     //---<  print a new blob on a new line  >---
2106     if (ix%granules_per_line == 0) {
2107       if (!name_in_addr_range) {
2108         ast->print_cr("No methods, blobs, or stubs found in this address range");
2109       }
2110       name_in_addr_range = false;
2111 
2112       size_t end_ix = (ix+granules_per_line <= alloc_granules) ? ix+granules_per_line : alloc_granules;
2113       ast->cr();
2114       ast->print_cr("--------------------------------------------------------------------");
2115       ast->print_cr("Address range [" INTPTR_FORMAT "," INTPTR_FORMAT "), " SIZE_FORMAT "k", p2i(low_bound+ix*granule_size), p2i(low_bound + end_ix*granule_size), (end_ix - ix)*granule_size/(size_t)K);
2116       ast->print_cr("--------------------------------------------------------------------");
2117       STRINGSTREAM_FLUSH_LOCKED("")
2118     }
2119     // Only check granule if it contains at least one blob.
2120     unsigned int nBlobs  = StatArray[ix].t1_count   + StatArray[ix].t2_count + StatArray[ix].tx_count +
2121                            StatArray[ix].stub_count + StatArray[ix].dead_count;
2122     if (nBlobs > 0 ) {
2123     for (unsigned int is = 0; is < granule_size; is+=(unsigned int)seg_size) {
2124       // heap->find_start() is safe. Only working with _segmap. Returns NULL or void*. Returned CodeBlob may be uninitialized.
2125       CodeBlob* this_blob = (CodeBlob *)(heap->find_start(low_bound+ix*granule_size+is));
2126       bool blob_initialized = (this_blob != NULL) && (this_blob->header_size() >= 0) && (this_blob->relocation_size() >= 0) &&
2127                               ((address)this_blob + this_blob->header_size() == (address)(this_blob->relocation_begin())) &&
2128                               ((address)this_blob + CodeBlob::align_code_offset(this_blob->header_size() + this_blob->relocation_size()) == (address)(this_blob->content_begin())) &&
2129                               os::is_readable_pointer((address)(this_blob->relocation_begin())) &&
2130                               os::is_readable_pointer(this_blob->content_begin());
2131       // blob could have been flushed, freed, and merged.
2132       // this_blob < last_blob is an indicator for that.
2133       if (blob_initialized && (this_blob > last_blob)) {
2134         last_blob          = this_blob;
2135 
2136         //---<  get type and name  >---
2137         blobType       cbType = noType;
2138         if (segment_granules) {
2139           cbType = (blobType)StatArray[ix].type;
2140         } else {
2141           cbType = get_cbType(this_blob);
2142         }
2143         // this_blob->name() could return NULL if no name was given to CTOR. Inlined, maybe invisible on stack
2144         const char* blob_name = this_blob->name();
2145         if ((blob_name == NULL) || !os::is_readable_pointer(blob_name)) {
2146           blob_name = "<unavailable>";
2147         }
2148 
2149         //---<  print table header for new print range  >---
2150         if (!name_in_addr_range) {
2151           name_in_addr_range = true;
2152           ast->fill_to(51);
2153           ast->print("%9s", "compiler");
2154           ast->fill_to(61);
2155           ast->print_cr("%6s", "method");
2156           ast->print_cr("%18s %13s %17s %9s  %5s %18s  %s", "Addr(module)      ", "offset", "size", " type lvl", " temp", "blobType          ", "Name");
2157           STRINGSTREAM_FLUSH_LOCKED("")
2158         }
2159 
2160         //---<  print line prefix (address and offset from CodeHeap start)  >---
2161         ast->print(INTPTR_FORMAT, p2i(this_blob));
2162         ast->fill_to(19);
2163         ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
2164         ast->fill_to(33);
2165 
2166         // this_blob->as_nmethod_or_null() is safe. Inlined, maybe invisible on stack.
2167         nmethod*    nm     = this_blob->as_nmethod_or_null();
2168         if (CompiledMethod::nmethod_access_is_safe(nm)) {
2169           Method* method = nm->method();
2170           ResourceMark rm;
2171           //---<  collect all data to locals as quickly as possible  >---
2172           unsigned int total_size = nm->total_size();
2173           int          hotness    = nm->hotness_counter();
2174           bool         get_name   = (cbType == nMethod_inuse) || (cbType == nMethod_notused);
2175           //---<  nMethod size in hex  >---
2176           ast->print(PTR32_FORMAT, total_size);
2177           ast->print("(" SIZE_FORMAT_W(4) "K)", total_size/K);
2178           //---<  compiler information  >---
2179           ast->fill_to(51);
2180           ast->print("%5s %3d", compTypeName[StatArray[ix].compiler], StatArray[ix].level);
2181           //---<  method temperature  >---
2182           ast->fill_to(62);
2183           ast->print("%5d", hotness);
2184           //---<  name and signature  >---
2185           ast->fill_to(62+6);
2186           ast->print("%s", blobTypeName[cbType]);
2187           ast->fill_to(82+6);
2188           if (cbType == nMethod_dead) {
2189             ast->print("%14s", " zombie method");
2190           }
2191 
2192           if (get_name) {
2193             Symbol* methName  = method->name();
2194             const char*   methNameS = (methName == NULL) ? NULL : methName->as_C_string();
2195             methNameS = (methNameS == NULL) ? "<method name unavailable>" : methNameS;
2196             Symbol* methSig   = method->signature();
2197             const char*   methSigS  = (methSig  == NULL) ? NULL : methSig->as_C_string();
2198             methSigS  = (methSigS  == NULL) ? "<method signature unavailable>" : methSigS;
2199             ast->print("%s", methNameS);
2200             ast->print("%s", methSigS);
2201           } else {
2202             ast->print("%s", blob_name);
2203           }
2204         } else {
2205           ast->fill_to(62+6);
2206           ast->print("%s", blobTypeName[cbType]);
2207           ast->fill_to(82+6);
2208           ast->print("%s", blob_name);
2209         }
2210         STRINGSTREAM_FLUSH_LOCKED("\n")
2211       } else if (!blob_initialized && (this_blob != last_blob) && (this_blob != NULL)) {
2212         last_blob          = this_blob;
2213         STRINGSTREAM_FLUSH_LOCKED("\n")
2214       }
2215     }
2216     } // nBlobs > 0
2217   }
2218   STRINGSTREAM_FLUSH_LOCKED("\n\n")
2219 }
2220 
2221 
2222 void CodeHeapState::printBox(outputStream* ast, const char border, const char* text1, const char* text2) {
2223   unsigned int lineLen = 1 + 2 + 2 + 1;
2224   char edge, frame;
2225 
2226   if (text1 != NULL) {
2227     lineLen += (unsigned int)strlen(text1); // text1 is much shorter than MAX_INT chars.
2228   }
2229   if (text2 != NULL) {
2230     lineLen += (unsigned int)strlen(text2); // text2 is much shorter than MAX_INT chars.
2231   }
2232   if (border == '-') {
2233     edge  = '+';
2234     frame = '|';
2235   } else {
2236     edge  = border;
2237     frame = border;
2238   }
2239 
2240   ast->print("%c", edge);
2241   for (unsigned int i = 0; i < lineLen-2; i++) {
2242     ast->print("%c", border);
2243   }
2244   ast->print_cr("%c", edge);
2245 
2246   ast->print("%c  ", frame);
2247   if (text1 != NULL) {
2248     ast->print("%s", text1);
2249   }
2250   if (text2 != NULL) {
2251     ast->print("%s", text2);
2252   }
2253   ast->print_cr("  %c", frame);
2254 
2255   ast->print("%c", edge);
2256   for (unsigned int i = 0; i < lineLen-2; i++) {
2257     ast->print("%c", border);
2258   }
2259   ast->print_cr("%c", edge);
2260 }
2261 
2262 void CodeHeapState::print_blobType_legend(outputStream* out) {
2263   out->cr();
2264   printBox(out, '-', "Block types used in the following CodeHeap dump", NULL);
2265   for (int type = noType; type < lastType; type += 1) {
2266     out->print_cr("  %c - %s", blobTypeChar[type], blobTypeName[type]);
2267   }
2268   out->print_cr("  -----------------------------------------------------");
2269   out->cr();
2270 }
2271 
2272 void CodeHeapState::print_space_legend(outputStream* out) {
2273   unsigned int indicator = 0;
2274   unsigned int age_range = 256;
2275   unsigned int range_beg = latest_compilation_id;
2276   out->cr();
2277   printBox(out, '-', "Space ranges, based on granule occupancy", NULL);
2278   out->print_cr("    -   0%% == occupancy");
2279   for (int i=0; i<=9; i++) {
2280     out->print_cr("  %d - %3d%% < occupancy < %3d%%", i, 10*i, 10*(i+1));
2281   }
2282   out->print_cr("  * - 100%% == occupancy");
2283   out->print_cr("  ----------------------------------------------");
2284   out->cr();
2285 }
2286 
2287 void CodeHeapState::print_age_legend(outputStream* out) {
2288   unsigned int indicator = 0;
2289   unsigned int age_range = 256;
2290   unsigned int range_beg = latest_compilation_id;
2291   out->cr();
2292   printBox(out, '-', "Age ranges, based on compilation id", NULL);
2293   while (age_range > 0) {
2294     out->print_cr("  %d - %6d to %6d", indicator, range_beg, latest_compilation_id - latest_compilation_id/age_range);
2295     range_beg = latest_compilation_id - latest_compilation_id/age_range;
2296     age_range /= 2;
2297     indicator += 1;
2298   }
2299   out->print_cr("  -----------------------------------------");
2300   out->cr();
2301 }
2302 
2303 void CodeHeapState::print_blobType_single(outputStream* out, u2 /* blobType */ type) {
2304   out->print("%c", blobTypeChar[type]);
2305 }
2306 
2307 void CodeHeapState::print_count_single(outputStream* out, unsigned short count) {
2308   if (count >= 16)    out->print("*");
2309   else if (count > 0) out->print("%1.1x", count);
2310   else                out->print(" ");
2311 }
2312 
2313 void CodeHeapState::print_space_single(outputStream* out, unsigned short space) {
2314   size_t  space_in_bytes = ((unsigned int)space)<<log2_seg_size;
2315   char    fraction       = (space == 0) ? ' ' : (space_in_bytes >= granule_size-1) ? '*' : char('0'+10*space_in_bytes/granule_size);
2316   out->print("%c", fraction);
2317 }
2318 
2319 void CodeHeapState::print_age_single(outputStream* out, unsigned int age) {
2320   unsigned int indicator = 0;
2321   unsigned int age_range = 256;
2322   if (age > 0) {
2323     while ((age_range > 0) && (latest_compilation_id-age > latest_compilation_id/age_range)) {
2324       age_range /= 2;
2325       indicator += 1;
2326     }
2327     out->print("%c", char('0'+indicator));
2328   } else {
2329     out->print(" ");
2330   }
2331 }
2332 
2333 void CodeHeapState::print_line_delim(outputStream* out, outputStream* ast, char* low_bound, unsigned int ix, unsigned int gpl) {
2334   if (ix % gpl == 0) {
2335     if (ix > 0) {
2336       ast->print("|");
2337     }
2338     ast->cr();
2339     assert(out == ast, "must use the same stream!");
2340 
2341     ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2342     ast->fill_to(19);
2343     ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2344   }
2345 }
2346 
2347 void CodeHeapState::print_line_delim(outputStream* out, bufferedStream* ast, char* low_bound, unsigned int ix, unsigned int gpl) {
2348   assert(out != ast, "must not use the same stream!");
2349   if (ix % gpl == 0) {
2350     if (ix > 0) {
2351       ast->print("|");
2352     }
2353     ast->cr();
2354 
2355     { // can't use STRINGSTREAM_FLUSH_LOCKED("") here.
2356       ttyLocker ttyl;
2357       out->print("%s", ast->as_string());
2358       ast->reset();
2359     }
2360 
2361     ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2362     ast->fill_to(19);
2363     ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2364   }
2365 }
2366 
2367 CodeHeapState::blobType CodeHeapState::get_cbType(CodeBlob* cb) {
2368   if ((cb != NULL) && os::is_readable_pointer(cb)) {
2369     if (cb->is_runtime_stub())                return runtimeStub;
2370     if (cb->is_deoptimization_stub())         return deoptimizationStub;
2371     if (cb->is_uncommon_trap_stub())          return uncommonTrapStub;
2372     if (cb->is_exception_stub())              return exceptionStub;
2373     if (cb->is_safepoint_stub())              return safepointStub;
2374     if (cb->is_adapter_blob())                return adapterBlob;
2375     if (cb->is_method_handles_adapter_blob()) return mh_adapterBlob;
2376     if (cb->is_buffer_blob())                 return bufferBlob;
2377 
2378     nmethod*  nm = cb->as_nmethod_or_null();
2379     if (nm != NULL) { // no is_readable check required, nm = (nmethod*)cb.
2380       if (nm->is_not_installed()) return nMethod_inconstruction;
2381       if (nm->is_zombie())        return nMethod_dead;
2382       if (nm->is_unloaded())      return nMethod_unloaded;
2383       if (nm->is_in_use())        return nMethod_inuse;
2384       if (nm->is_alive() && !(nm->is_not_entrant()))   return nMethod_notused;
2385       if (nm->is_alive())         return nMethod_alive;
2386       return nMethod_dead;
2387     }
2388   }
2389   return noType;
2390 }