1 /*
   2  * Copyright (c) 2018, 2019, 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(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, size_t granularity) {
 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   assert(granularity > 0, "granularity should be positive.");
 517 
 518   if (granularity > size) {
 519     granularity = size;
 520   }
 521   if (size/granularity < min_granules) {
 522     granularity = size/min_granules;                                   // at least min_granules granules
 523   }
 524   granularity = granularity & (~(seg_size - 1));                       // must be multiple of seg_size
 525   if (granularity < seg_size) {
 526     granularity = seg_size;                                            // must be at least seg_size
 527   }
 528   if (size/granularity > max_granules) {
 529     granularity = size/max_granules;                                   // at most max_granules granules
 530   }
 531   granularity = granularity & (~(seg_size - 1));                       // must be multiple of seg_size
 532   if (granularity>>log2_seg_size >= (1L<<sizeof(unsigned short)*8)) {
 533     granularity = ((1L<<(sizeof(unsigned short)*8))-1)<<log2_seg_size; // Limit: (64k-1) * seg_size
 534   }
 535   segment_granules = granularity == seg_size;
 536   size_t granules  = (size + (granularity-1))/granularity;
 537 
 538   printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (used blocks) for segment ", heapName);
 539   ast->print_cr("   The aggregate step takes an aggregated snapshot of the CodeHeap.\n"
 540                 "   Subsequent print functions create their output based on this snapshot.\n"
 541                 "   The CodeHeap is a living thing, and every effort has been made for the\n"
 542                 "   collected data to be consistent. Only the method names and signatures\n"
 543                 "   are retrieved at print time. That may lead to rare cases where the\n"
 544                 "   name of a method is no longer available, e.g. because it was unloaded.\n");
 545   ast->print_cr("   CodeHeap committed size " SIZE_FORMAT "K (" SIZE_FORMAT "M), reserved size " SIZE_FORMAT "K (" SIZE_FORMAT "M), %d%% occupied.",
 546                 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));
 547   ast->print_cr("   CodeHeap allocation segment size is " SIZE_FORMAT " bytes. This is the smallest possible granularity.", seg_size);
 548   ast->print_cr("   CodeHeap (committed part) is mapped to " SIZE_FORMAT " granules of size " SIZE_FORMAT " bytes.", granules, granularity);
 549   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);
 550   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));
 551   STRINGSTREAM_FLUSH("\n")
 552 
 553 
 554   while (!done) {
 555     //---<  reset counters with every aggregation  >---
 556     nBlocks_t1       = 0;
 557     nBlocks_t2       = 0;
 558     nBlocks_alive    = 0;
 559     nBlocks_dead     = 0;
 560     nBlocks_inconstr = 0;
 561     nBlocks_unloaded = 0;
 562     nBlocks_stub     = 0;
 563 
 564     nBlocks_free     = 0;
 565     nBlocks_used     = 0;
 566     nBlocks_zomb     = 0;
 567     nBlocks_disconn  = 0;
 568     nBlocks_notentr  = 0;
 569 
 570     //---<  discard old arrays if size does not match  >---
 571     if (granules != alloc_granules) {
 572       discard_StatArray(out);
 573       discard_TopSizeArray(out);
 574     }
 575 
 576     //---<  allocate arrays if they don't yet exist, initialize  >---
 577     prepare_StatArray(out, granules, granularity, heapName);
 578     if (StatArray == NULL) {
 579       set_HeapStatGlobals(out, heapName);
 580       return;
 581     }
 582     prepare_TopSizeArray(out, maxTopSizeBlocks, heapName);
 583     prepare_SizeDistArray(out, nSizeDistElements, heapName);
 584 
 585     latest_compilation_id = CompileBroker::get_compilation_id();
 586     unsigned int highest_compilation_id = 0;
 587     size_t       usedSpace     = 0;
 588     size_t       t1Space       = 0;
 589     size_t       t2Space       = 0;
 590     size_t       aliveSpace    = 0;
 591     size_t       disconnSpace  = 0;
 592     size_t       notentrSpace  = 0;
 593     size_t       deadSpace     = 0;
 594     size_t       inconstrSpace = 0;
 595     size_t       unloadedSpace = 0;
 596     size_t       stubSpace     = 0;
 597     size_t       freeSpace     = 0;
 598     size_t       maxFreeSize   = 0;
 599     HeapBlock*   maxFreeBlock  = NULL;
 600     bool         insane        = false;
 601 
 602     int64_t hotnessAccumulator = 0;
 603     unsigned int n_methods     = 0;
 604     avgTemp       = 0;
 605     minTemp       = (int)(res_size > M ? (res_size/M)*2 : 1);
 606     maxTemp       = -minTemp;
 607 
 608     for (HeapBlock *h = heap->first_block(); h != NULL && !insane; h = heap->next_block(h)) {
 609       unsigned int hb_len     = (unsigned int)h->length();  // despite being size_t, length can never overflow an unsigned int.
 610       size_t       hb_bytelen = ((size_t)hb_len)<<log2_seg_size;
 611       unsigned int ix_beg     = (unsigned int)(((char*)h-low_bound)/granule_size);
 612       unsigned int ix_end     = (unsigned int)(((char*)h-low_bound+(hb_bytelen-1))/granule_size);
 613       unsigned int compile_id = 0;
 614       CompLevel    comp_lvl   = CompLevel_none;
 615       compType     cType      = noComp;
 616       blobType     cbType     = noType;
 617 
 618       //---<  some sanity checks  >---
 619       // Do not assert here, just check, print error message and return.
 620       // This is a diagnostic function. It is not supposed to tear down the VM.
 621       if ((char*)h <  low_bound) {
 622         insane = true; ast->print_cr("Sanity check: HeapBlock @%p below low bound (%p)", (char*)h, low_bound);
 623       }
 624       if ((char*)h >  (low_bound + res_size)) {
 625         insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside reserved range (%p)", (char*)h, low_bound + res_size);
 626       }
 627       if ((char*)h >  (low_bound + size)) {
 628         insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside used range (%p)", (char*)h, low_bound + size);
 629       }
 630       if (ix_end   >= granules) {
 631         insane = true; ast->print_cr("Sanity check: end index (%d) out of bounds (" SIZE_FORMAT ")", ix_end, granules);
 632       }
 633       if (size     != heap->capacity()) {
 634         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);
 635       }
 636       if (ix_beg   >  ix_end) {
 637         insane = true; ast->print_cr("Sanity check: end index (%d) lower than begin index (%d)", ix_end, ix_beg);
 638       }
 639       if (insane) {
 640         STRINGSTREAM_FLUSH("")
 641         continue;
 642       }
 643 
 644       if (h->free()) {
 645         nBlocks_free++;
 646         freeSpace    += hb_bytelen;
 647         if (hb_bytelen > maxFreeSize) {
 648           maxFreeSize   = hb_bytelen;
 649           maxFreeBlock  = h;
 650         }
 651       } else {
 652         update_SizeDistArray(out, hb_len);
 653         nBlocks_used++;
 654         usedSpace    += hb_bytelen;
 655         CodeBlob* cb  = (CodeBlob*)heap->find_start(h);
 656         if (cb != NULL) {
 657           cbType = get_cbType(cb);
 658           if (cb->is_nmethod()) {
 659             compile_id = ((nmethod*)cb)->compile_id();
 660             comp_lvl   = (CompLevel)((nmethod*)cb)->comp_level();
 661             if (((nmethod*)cb)->is_compiled_by_c1()) {
 662               cType = c1;
 663             }
 664             if (((nmethod*)cb)->is_compiled_by_c2()) {
 665               cType = c2;
 666             }
 667             if (((nmethod*)cb)->is_compiled_by_jvmci()) {
 668               cType = jvmci;
 669             }
 670             switch (cbType) {
 671               case nMethod_inuse: { // only for executable methods!!!
 672                 // space for these cbs is accounted for later.
 673                 int temperature = ((nmethod*)cb)->hotness_counter();
 674                 hotnessAccumulator += temperature;
 675                 n_methods++;
 676                 maxTemp = (temperature > maxTemp) ? temperature : maxTemp;
 677                 minTemp = (temperature < minTemp) ? temperature : minTemp;
 678                 break;
 679               }
 680               case nMethod_notused:
 681                 nBlocks_alive++;
 682                 nBlocks_disconn++;
 683                 aliveSpace     += hb_bytelen;
 684                 disconnSpace   += hb_bytelen;
 685                 break;
 686               case nMethod_notentrant:  // equivalent to nMethod_alive
 687                 nBlocks_alive++;
 688                 nBlocks_notentr++;
 689                 aliveSpace     += hb_bytelen;
 690                 notentrSpace   += hb_bytelen;
 691                 break;
 692               case nMethod_unloaded:
 693                 nBlocks_unloaded++;
 694                 unloadedSpace  += hb_bytelen;
 695                 break;
 696               case nMethod_dead:
 697                 nBlocks_dead++;
 698                 deadSpace      += hb_bytelen;
 699                 break;
 700               case nMethod_inconstruction:
 701                 nBlocks_inconstr++;
 702                 inconstrSpace  += hb_bytelen;
 703                 break;
 704               default:
 705                 break;
 706             }
 707           }
 708 
 709           //------------------------------------------
 710           //---<  register block in TopSizeArray  >---
 711           //------------------------------------------
 712           if (alloc_topSizeBlocks > 0) {
 713             if (used_topSizeBlocks == 0) {
 714               TopSizeArray[0].start    = h;
 715               TopSizeArray[0].len      = hb_len;
 716               TopSizeArray[0].index    = tsbStopper;
 717               TopSizeArray[0].compiler = cType;
 718               TopSizeArray[0].level    = comp_lvl;
 719               TopSizeArray[0].type     = cbType;
 720               currMax    = hb_len;
 721               currMin    = hb_len;
 722               currMin_ix = 0;
 723               used_topSizeBlocks++;
 724             // This check roughly cuts 5000 iterations (JVM98, mixed, dbg, termination stats):
 725             } else if ((used_topSizeBlocks < alloc_topSizeBlocks) && (hb_len < currMin)) {
 726               //---<  all blocks in list are larger, but there is room left in array  >---
 727               TopSizeArray[currMin_ix].index = used_topSizeBlocks;
 728               TopSizeArray[used_topSizeBlocks].start    = h;
 729               TopSizeArray[used_topSizeBlocks].len      = hb_len;
 730               TopSizeArray[used_topSizeBlocks].index    = tsbStopper;
 731               TopSizeArray[used_topSizeBlocks].compiler = cType;
 732               TopSizeArray[used_topSizeBlocks].level    = comp_lvl;
 733               TopSizeArray[used_topSizeBlocks].type     = cbType;
 734               currMin    = hb_len;
 735               currMin_ix = used_topSizeBlocks;
 736               used_topSizeBlocks++;
 737             } else {
 738               // This check cuts total_iterations by a factor of 6 (JVM98, mixed, dbg, termination stats):
 739               //   We don't need to search the list if we know beforehand that the current block size is
 740               //   smaller than the currently recorded minimum and there is no free entry left in the list.
 741               if (!((used_topSizeBlocks == alloc_topSizeBlocks) && (hb_len <= currMin))) {
 742                 if (currMax < hb_len) {
 743                   currMax = hb_len;
 744                 }
 745                 unsigned int i;
 746                 unsigned int prev_i  = tsbStopper;
 747                 unsigned int limit_i =  0;
 748                 for (i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
 749                   if (limit_i++ >= alloc_topSizeBlocks) {
 750                     insane = true; break; // emergency exit
 751                   }
 752                   if (i >= used_topSizeBlocks)  {
 753                     insane = true; break; // emergency exit
 754                   }
 755                   total_iterations++;
 756                   if (TopSizeArray[i].len < hb_len) {
 757                     //---<  We want to insert here, element <i> is smaller than the current one  >---
 758                     if (used_topSizeBlocks < alloc_topSizeBlocks) { // still room for a new entry to insert
 759                       // old entry gets moved to the next free element of the array.
 760                       // That's necessary to keep the entry for the largest block at index 0.
 761                       // This move might cause the current minimum to be moved to another place
 762                       if (i == currMin_ix) {
 763                         assert(TopSizeArray[i].len == currMin, "sort error");
 764                         currMin_ix = used_topSizeBlocks;
 765                       }
 766                       memcpy((void*)&TopSizeArray[used_topSizeBlocks], (void*)&TopSizeArray[i], sizeof(TopSizeBlk));
 767                       TopSizeArray[i].start    = h;
 768                       TopSizeArray[i].len      = hb_len;
 769                       TopSizeArray[i].index    = used_topSizeBlocks;
 770                       TopSizeArray[i].compiler = cType;
 771                       TopSizeArray[i].level    = comp_lvl;
 772                       TopSizeArray[i].type     = cbType;
 773                       used_topSizeBlocks++;
 774                     } else { // no room for new entries, current block replaces entry for smallest block
 775                       //---<  Find last entry (entry for smallest remembered block)  >---
 776                       unsigned int      j  = i;
 777                       unsigned int prev_j  = tsbStopper;
 778                       unsigned int limit_j = 0;
 779                       while (TopSizeArray[j].index != tsbStopper) {
 780                         if (limit_j++ >= alloc_topSizeBlocks) {
 781                           insane = true; break; // emergency exit
 782                         }
 783                         if (j >= used_topSizeBlocks)  {
 784                           insane = true; break; // emergency exit
 785                         }
 786                         total_iterations++;
 787                         prev_j = j;
 788                         j      = TopSizeArray[j].index;
 789                       }
 790                       if (!insane) {
 791                         if (prev_j == tsbStopper) {
 792                           //---<  Above while loop did not iterate, we already are the min entry  >---
 793                           //---<  We have to just replace the smallest entry                      >---
 794                           currMin    = hb_len;
 795                           currMin_ix = j;
 796                           TopSizeArray[j].start    = h;
 797                           TopSizeArray[j].len      = hb_len;
 798                           TopSizeArray[j].index    = tsbStopper; // already set!!
 799                           TopSizeArray[j].compiler = cType;
 800                           TopSizeArray[j].level    = comp_lvl;
 801                           TopSizeArray[j].type     = cbType;
 802                         } else {
 803                           //---<  second-smallest entry is now smallest  >---
 804                           TopSizeArray[prev_j].index = tsbStopper;
 805                           currMin    = TopSizeArray[prev_j].len;
 806                           currMin_ix = prev_j;
 807                           //---<  smallest entry gets overwritten  >---
 808                           memcpy((void*)&TopSizeArray[j], (void*)&TopSizeArray[i], sizeof(TopSizeBlk));
 809                           TopSizeArray[i].start    = h;
 810                           TopSizeArray[i].len      = hb_len;
 811                           TopSizeArray[i].index    = j;
 812                           TopSizeArray[i].compiler = cType;
 813                           TopSizeArray[i].level    = comp_lvl;
 814                           TopSizeArray[i].type     = cbType;
 815                         }
 816                       } // insane
 817                     }
 818                     break;
 819                   }
 820                   prev_i = i;
 821                 }
 822                 if (insane) {
 823                   // Note: regular analysis could probably continue by resetting "insane" flag.
 824                   out->print_cr("Possible loop in TopSizeBlocks list detected. Analysis aborted.");
 825                   discard_TopSizeArray(out);
 826                 }
 827               }
 828             }
 829           }
 830           //----------------------------------------------
 831           //---<  END register block in TopSizeArray  >---
 832           //----------------------------------------------
 833         } else {
 834           nBlocks_zomb++;
 835         }
 836 
 837         if (ix_beg == ix_end) {
 838           StatArray[ix_beg].type = cbType;
 839           switch (cbType) {
 840             case nMethod_inuse:
 841               highest_compilation_id = (highest_compilation_id >= compile_id) ? highest_compilation_id : compile_id;
 842               if (comp_lvl < CompLevel_full_optimization) {
 843                 nBlocks_t1++;
 844                 t1Space   += hb_bytelen;
 845                 StatArray[ix_beg].t1_count++;
 846                 StatArray[ix_beg].t1_space += (unsigned short)hb_len;
 847                 StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;
 848               } else {
 849                 nBlocks_t2++;
 850                 t2Space   += hb_bytelen;
 851                 StatArray[ix_beg].t2_count++;
 852                 StatArray[ix_beg].t2_space += (unsigned short)hb_len;
 853                 StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;
 854               }
 855               StatArray[ix_beg].level     = comp_lvl;
 856               StatArray[ix_beg].compiler  = cType;
 857               break;
 858             case nMethod_inconstruction: // let's count "in construction" nmethods here.
 859             case nMethod_alive:
 860               StatArray[ix_beg].tx_count++;
 861               StatArray[ix_beg].tx_space += (unsigned short)hb_len;
 862               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
 863               StatArray[ix_beg].level     = comp_lvl;
 864               StatArray[ix_beg].compiler  = cType;
 865               break;
 866             case nMethod_dead:
 867             case nMethod_unloaded:
 868               StatArray[ix_beg].dead_count++;
 869               StatArray[ix_beg].dead_space += (unsigned short)hb_len;
 870               break;
 871             default:
 872               // must be a stub, if it's not a dead or alive nMethod
 873               nBlocks_stub++;
 874               stubSpace   += hb_bytelen;
 875               StatArray[ix_beg].stub_count++;
 876               StatArray[ix_beg].stub_space += (unsigned short)hb_len;
 877               break;
 878           }
 879         } else {
 880           unsigned int beg_space = (unsigned int)(granule_size - ((char*)h - low_bound - ix_beg*granule_size));
 881           unsigned int end_space = (unsigned int)(hb_bytelen - beg_space - (ix_end-ix_beg-1)*granule_size);
 882           beg_space = beg_space>>log2_seg_size;  // store in units of _segment_size
 883           end_space = end_space>>log2_seg_size;  // store in units of _segment_size
 884           StatArray[ix_beg].type = cbType;
 885           StatArray[ix_end].type = cbType;
 886           switch (cbType) {
 887             case nMethod_inuse:
 888               highest_compilation_id = (highest_compilation_id >= compile_id) ? highest_compilation_id : compile_id;
 889               if (comp_lvl < CompLevel_full_optimization) {
 890                 nBlocks_t1++;
 891                 t1Space   += hb_bytelen;
 892                 StatArray[ix_beg].t1_count++;
 893                 StatArray[ix_beg].t1_space += (unsigned short)beg_space;
 894                 StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;
 895 
 896                 StatArray[ix_end].t1_count++;
 897                 StatArray[ix_end].t1_space += (unsigned short)end_space;
 898                 StatArray[ix_end].t1_age    = StatArray[ix_end].t1_age < compile_id ? compile_id : StatArray[ix_end].t1_age;
 899               } else {
 900                 nBlocks_t2++;
 901                 t2Space   += hb_bytelen;
 902                 StatArray[ix_beg].t2_count++;
 903                 StatArray[ix_beg].t2_space += (unsigned short)beg_space;
 904                 StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;
 905 
 906                 StatArray[ix_end].t2_count++;
 907                 StatArray[ix_end].t2_space += (unsigned short)end_space;
 908                 StatArray[ix_end].t2_age    = StatArray[ix_end].t2_age < compile_id ? compile_id : StatArray[ix_end].t2_age;
 909               }
 910               StatArray[ix_beg].level     = comp_lvl;
 911               StatArray[ix_beg].compiler  = cType;
 912               StatArray[ix_end].level     = comp_lvl;
 913               StatArray[ix_end].compiler  = cType;
 914               break;
 915             case nMethod_inconstruction: // let's count "in construction" nmethods here.
 916             case nMethod_alive:
 917               StatArray[ix_beg].tx_count++;
 918               StatArray[ix_beg].tx_space += (unsigned short)beg_space;
 919               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
 920 
 921               StatArray[ix_end].tx_count++;
 922               StatArray[ix_end].tx_space += (unsigned short)end_space;
 923               StatArray[ix_end].tx_age    = StatArray[ix_end].tx_age < compile_id ? compile_id : StatArray[ix_end].tx_age;
 924 
 925               StatArray[ix_beg].level     = comp_lvl;
 926               StatArray[ix_beg].compiler  = cType;
 927               StatArray[ix_end].level     = comp_lvl;
 928               StatArray[ix_end].compiler  = cType;
 929               break;
 930             case nMethod_dead:
 931             case nMethod_unloaded:
 932               StatArray[ix_beg].dead_count++;
 933               StatArray[ix_beg].dead_space += (unsigned short)beg_space;
 934               StatArray[ix_end].dead_count++;
 935               StatArray[ix_end].dead_space += (unsigned short)end_space;
 936               break;
 937             default:
 938               // must be a stub, if it's not a dead or alive nMethod
 939               nBlocks_stub++;
 940               stubSpace   += hb_bytelen;
 941               StatArray[ix_beg].stub_count++;
 942               StatArray[ix_beg].stub_space += (unsigned short)beg_space;
 943               StatArray[ix_end].stub_count++;
 944               StatArray[ix_end].stub_space += (unsigned short)end_space;
 945               break;
 946           }
 947           for (unsigned int ix = ix_beg+1; ix < ix_end; ix++) {
 948             StatArray[ix].type = cbType;
 949             switch (cbType) {
 950               case nMethod_inuse:
 951                 if (comp_lvl < CompLevel_full_optimization) {
 952                   StatArray[ix].t1_count++;
 953                   StatArray[ix].t1_space += (unsigned short)(granule_size>>log2_seg_size);
 954                   StatArray[ix].t1_age    = StatArray[ix].t1_age < compile_id ? compile_id : StatArray[ix].t1_age;
 955                 } else {
 956                   StatArray[ix].t2_count++;
 957                   StatArray[ix].t2_space += (unsigned short)(granule_size>>log2_seg_size);
 958                   StatArray[ix].t2_age    = StatArray[ix].t2_age < compile_id ? compile_id : StatArray[ix].t2_age;
 959                 }
 960                 StatArray[ix].level     = comp_lvl;
 961                 StatArray[ix].compiler  = cType;
 962                 break;
 963               case nMethod_inconstruction: // let's count "in construction" nmethods here.
 964               case nMethod_alive:
 965                 StatArray[ix].tx_count++;
 966                 StatArray[ix].tx_space += (unsigned short)(granule_size>>log2_seg_size);
 967                 StatArray[ix].tx_age    = StatArray[ix].tx_age < compile_id ? compile_id : StatArray[ix].tx_age;
 968                 StatArray[ix].level     = comp_lvl;
 969                 StatArray[ix].compiler  = cType;
 970                 break;
 971               case nMethod_dead:
 972               case nMethod_unloaded:
 973                 StatArray[ix].dead_count++;
 974                 StatArray[ix].dead_space += (unsigned short)(granule_size>>log2_seg_size);
 975                 break;
 976               default:
 977                 // must be a stub, if it's not a dead or alive nMethod
 978                 StatArray[ix].stub_count++;
 979                 StatArray[ix].stub_space += (unsigned short)(granule_size>>log2_seg_size);
 980                 break;
 981             }
 982           }
 983         }
 984       }
 985     }
 986     done = true;
 987 
 988     if (!insane) {
 989       // There is a risk for this block (because it contains many print statements) to get
 990       // interspersed with print data from other threads. We take this risk intentionally.
 991       // Getting stalled waiting for tty_lock while holding the CodeCache_lock is not desirable.
 992       printBox(ast, '-', "Global CodeHeap statistics for segment ", heapName);
 993       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);
 994       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);
 995       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);
 996       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);
 997       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);
 998       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);
 999       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);
1000       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);
1001       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);
1002       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);
1003       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);
1004       ast->print_cr("ZombieBlocks     = %8d. These are HeapBlocks which could not be identified as CodeBlobs.", nBlocks_zomb);
1005       ast->cr();
1006       ast->print_cr("Segment start          = " INTPTR_FORMAT ", used space      = " SIZE_FORMAT_W(8)"k", p2i(low_bound), size/K);
1007       ast->print_cr("Segment end (used)     = " INTPTR_FORMAT ", remaining space = " SIZE_FORMAT_W(8)"k", p2i(low_bound) + size, (res_size - size)/K);
1008       ast->print_cr("Segment end (reserved) = " INTPTR_FORMAT ", reserved space  = " SIZE_FORMAT_W(8)"k", p2i(low_bound) + res_size, res_size/K);
1009       ast->cr();
1010       ast->print_cr("latest allocated compilation id = %d", latest_compilation_id);
1011       ast->print_cr("highest observed compilation id = %d", highest_compilation_id);
1012       ast->print_cr("Building TopSizeList iterations = %ld", total_iterations);
1013       ast->cr();
1014 
1015       int             reset_val = NMethodSweeper::hotness_counter_reset_val();
1016       double reverse_free_ratio = (res_size > size) ? (double)res_size/(double)(res_size-size) : (double)res_size;
1017       printBox(ast, '-', "Method hotness information at time of this analysis", NULL);
1018       ast->print_cr("Highest possible method temperature:          %12d", reset_val);
1019       ast->print_cr("Threshold for method to be considered 'cold': %12.3f", -reset_val + reverse_free_ratio * NmethodSweepActivity);
1020       if (n_methods > 0) {
1021         avgTemp = hotnessAccumulator/n_methods;
1022         ast->print_cr("min. hotness = %6d", minTemp);
1023         ast->print_cr("avg. hotness = %6d", avgTemp);
1024         ast->print_cr("max. hotness = %6d", maxTemp);
1025       } else {
1026         avgTemp = 0;
1027         ast->print_cr("No hotness data available");
1028       }
1029       STRINGSTREAM_FLUSH("\n")
1030 
1031       // This loop is intentionally printing directly to "out".
1032       out->print("Verifying collected data...");
1033       size_t granule_segs = granule_size>>log2_seg_size;
1034       for (unsigned int ix = 0; ix < granules; ix++) {
1035         if (StatArray[ix].t1_count   > granule_segs) {
1036           out->print_cr("t1_count[%d]   = %d", ix, StatArray[ix].t1_count);
1037         }
1038         if (StatArray[ix].t2_count   > granule_segs) {
1039           out->print_cr("t2_count[%d]   = %d", ix, StatArray[ix].t2_count);
1040         }
1041         if (StatArray[ix].tx_count   > granule_segs) {
1042           out->print_cr("tx_count[%d]   = %d", ix, StatArray[ix].tx_count);
1043         }
1044         if (StatArray[ix].stub_count > granule_segs) {
1045           out->print_cr("stub_count[%d] = %d", ix, StatArray[ix].stub_count);
1046         }
1047         if (StatArray[ix].dead_count > granule_segs) {
1048           out->print_cr("dead_count[%d] = %d", ix, StatArray[ix].dead_count);
1049         }
1050         if (StatArray[ix].t1_space   > granule_segs) {
1051           out->print_cr("t1_space[%d]   = %d", ix, StatArray[ix].t1_space);
1052         }
1053         if (StatArray[ix].t2_space   > granule_segs) {
1054           out->print_cr("t2_space[%d]   = %d", ix, StatArray[ix].t2_space);
1055         }
1056         if (StatArray[ix].tx_space   > granule_segs) {
1057           out->print_cr("tx_space[%d]   = %d", ix, StatArray[ix].tx_space);
1058         }
1059         if (StatArray[ix].stub_space > granule_segs) {
1060           out->print_cr("stub_space[%d] = %d", ix, StatArray[ix].stub_space);
1061         }
1062         if (StatArray[ix].dead_space > granule_segs) {
1063           out->print_cr("dead_space[%d] = %d", ix, StatArray[ix].dead_space);
1064         }
1065         //   this cast is awful! I need it because NT/Intel reports a signed/unsigned mismatch.
1066         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) {
1067           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);
1068         }
1069         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) {
1070           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);
1071         }
1072       }
1073 
1074       // This loop is intentionally printing directly to "out".
1075       if (used_topSizeBlocks > 0) {
1076         unsigned int j = 0;
1077         if (TopSizeArray[0].len != currMax) {
1078           out->print_cr("currMax(%d) differs from TopSizeArray[0].len(%d)", currMax, TopSizeArray[0].len);
1079         }
1080         for (unsigned int i = 0; (TopSizeArray[i].index != tsbStopper) && (j++ < alloc_topSizeBlocks); i = TopSizeArray[i].index) {
1081           if (TopSizeArray[i].len < TopSizeArray[TopSizeArray[i].index].len) {
1082             out->print_cr("sort error at index %d: %d !>= %d", i, TopSizeArray[i].len, TopSizeArray[TopSizeArray[i].index].len);
1083           }
1084         }
1085         if (j >= alloc_topSizeBlocks) {
1086           out->print_cr("Possible loop in TopSizeArray chaining!\n  allocBlocks = %d, usedBlocks = %d", alloc_topSizeBlocks, used_topSizeBlocks);
1087           for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1088             out->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1089           }
1090         }
1091       }
1092       out->print_cr("...done\n\n");
1093     } else {
1094       // insane heap state detected. Analysis data incomplete. Just throw it away.
1095       discard_StatArray(out);
1096       discard_TopSizeArray(out);
1097     }
1098   }
1099 
1100 
1101   done        = false;
1102   while (!done && (nBlocks_free > 0)) {
1103 
1104     printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (free blocks) for segment ", heapName);
1105     ast->print_cr("   The aggregate step collects information about all free blocks in CodeHeap.\n"
1106                   "   Subsequent print functions create their output based on this snapshot.\n");
1107     ast->print_cr("   Free space in %s is distributed over %d free blocks.", heapName, nBlocks_free);
1108     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);
1109     STRINGSTREAM_FLUSH("\n")
1110 
1111     //----------------------------------------
1112     //--  Prepare the FreeArray of FreeBlks --
1113     //----------------------------------------
1114 
1115     //---< discard old array if size does not match  >---
1116     if (nBlocks_free != alloc_freeBlocks) {
1117       discard_FreeArray(out);
1118     }
1119 
1120     prepare_FreeArray(out, nBlocks_free, heapName);
1121     if (FreeArray == NULL) {
1122       done = true;
1123       continue;
1124     }
1125 
1126     //----------------------------------------
1127     //--  Collect all FreeBlks in FreeArray --
1128     //----------------------------------------
1129 
1130     unsigned int ix = 0;
1131     FreeBlock* cur  = heap->freelist();
1132 
1133     while (cur != NULL) {
1134       if (ix < alloc_freeBlocks) { // don't index out of bounds if _freelist has more blocks than anticipated
1135         FreeArray[ix].start = cur;
1136         FreeArray[ix].len   = (unsigned int)(cur->length()<<log2_seg_size);
1137         FreeArray[ix].index = ix;
1138       }
1139       cur  = cur->link();
1140       ix++;
1141     }
1142     if (ix != alloc_freeBlocks) {
1143       ast->print_cr("Free block count mismatch. Expected %d free blocks, but found %d.", alloc_freeBlocks, ix);
1144       ast->print_cr("I will update the counter and retry data collection");
1145       STRINGSTREAM_FLUSH("\n")
1146       nBlocks_free = ix;
1147       continue;
1148     }
1149     done = true;
1150   }
1151 
1152   if (!done || (nBlocks_free == 0)) {
1153     if (nBlocks_free == 0) {
1154       printBox(ast, '-', "no free blocks found in ", heapName);
1155     } else if (!done) {
1156       ast->print_cr("Free block count mismatch could not be resolved.");
1157       ast->print_cr("Try to run \"aggregate\" function to update counters");
1158     }
1159     STRINGSTREAM_FLUSH("")
1160 
1161     //---< discard old array and update global values  >---
1162     discard_FreeArray(out);
1163     set_HeapStatGlobals(out, heapName);
1164     return;
1165   }
1166 
1167   //---<  calculate and fill remaining fields  >---
1168   if (FreeArray != NULL) {
1169     // This loop is intentionally printing directly to "out".
1170     for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1171       size_t lenSum = 0;
1172       FreeArray[ix].gap = (unsigned int)((address)FreeArray[ix+1].start - ((address)FreeArray[ix].start + FreeArray[ix].len));
1173       for (HeapBlock *h = heap->next_block(FreeArray[ix].start); (h != NULL) && (h != FreeArray[ix+1].start); h = heap->next_block(h)) {
1174         CodeBlob *cb  = (CodeBlob*)(heap->find_start(h));
1175         if ((cb != NULL) && !cb->is_nmethod()) {
1176           FreeArray[ix].stubs_in_gap = true;
1177         }
1178         FreeArray[ix].n_gapBlocks++;
1179         lenSum += h->length()<<log2_seg_size;
1180         if (((address)h < ((address)FreeArray[ix].start+FreeArray[ix].len)) || (h >= FreeArray[ix+1].start)) {
1181           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);
1182         }
1183       }
1184       if (lenSum != FreeArray[ix].gap) {
1185         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);
1186       }
1187     }
1188   }
1189   set_HeapStatGlobals(out, heapName);
1190 
1191   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);
1192   STRINGSTREAM_FLUSH("\n")
1193 }
1194 
1195 
1196 void CodeHeapState::print_usedSpace(outputStream* out, CodeHeap* heap) {
1197   if (!initialization_complete) {
1198     return;
1199   }
1200 
1201   const char* heapName   = get_heapName(heap);
1202   get_HeapStatGlobals(out, heapName);
1203 
1204   if ((StatArray == NULL) || (TopSizeArray == NULL) || (used_topSizeBlocks == 0)) {
1205     return;
1206   }
1207   STRINGSTREAM_DECL(ast, out)
1208 
1209   {
1210     printBox(ast, '=', "U S E D   S P A C E   S T A T I S T I C S   for ", heapName);
1211     ast->print_cr("Note: The Top%d list of the largest used blocks associates method names\n"
1212                   "      and other identifying information with the block size data.\n"
1213                   "\n"
1214                   "      Method names are dynamically retrieved from the code cache at print time.\n"
1215                   "      Due to the living nature of the code cache and because the CodeCache_lock\n"
1216                   "      is not continuously held, the displayed name might be wrong or no name\n"
1217                   "      might be found at all. The likelihood for that to happen increases\n"
1218                   "      over time passed between analysis and print step.\n", used_topSizeBlocks);
1219     STRINGSTREAM_FLUSH_LOCKED("\n")
1220   }
1221 
1222   //----------------------------
1223   //--  Print Top Used Blocks --
1224   //----------------------------
1225   {
1226     char*     low_bound = heap->low_boundary();
1227 
1228     printBox(ast, '-', "Largest Used Blocks in ", heapName);
1229     print_blobType_legend(ast);
1230 
1231     ast->fill_to(51);
1232     ast->print("%4s", "blob");
1233     ast->fill_to(56);
1234     ast->print("%9s", "compiler");
1235     ast->fill_to(66);
1236     ast->print_cr("%6s", "method");
1237     ast->print_cr("%18s %13s %17s %4s %9s  %5s %s",      "Addr(module)      ", "offset", "size", "type", " type lvl", " temp", "Name");
1238     STRINGSTREAM_FLUSH_LOCKED("")
1239 
1240     //---<  print Top Ten Used Blocks  >---
1241     if (used_topSizeBlocks > 0) {
1242       unsigned int printed_topSizeBlocks = 0;
1243       for (unsigned int i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
1244         printed_topSizeBlocks++;
1245         CodeBlob*   this_blob = (CodeBlob*)(heap->find_start(TopSizeArray[i].start));
1246         nmethod*           nm = NULL;
1247         const char* blob_name = "unnamed blob";
1248         if (this_blob != NULL) {
1249           blob_name = this_blob->name();
1250           nm        = this_blob->as_nmethod_or_null();
1251           //---<  blob address  >---
1252           ast->print(INTPTR_FORMAT, p2i(this_blob));
1253           ast->fill_to(19);
1254           //---<  blob offset from CodeHeap begin  >---
1255           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
1256           ast->fill_to(33);
1257         } else {
1258           //---<  block address  >---
1259           ast->print(INTPTR_FORMAT, p2i(TopSizeArray[i].start));
1260           ast->fill_to(19);
1261           //---<  block offset from CodeHeap begin  >---
1262           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)TopSizeArray[i].start-low_bound));
1263           ast->fill_to(33);
1264         }
1265 
1266 
1267         //---<  print size, name, and signature (for nMethods)  >---
1268         if ((nm != NULL) && (nm->method() != NULL)) {
1269           ResourceMark rm;
1270           //---<  nMethod size in hex  >---
1271           unsigned int total_size = nm->total_size();
1272           ast->print(PTR32_FORMAT, total_size);
1273           ast->print("(" SIZE_FORMAT_W(4) "K)", total_size/K);
1274           ast->fill_to(51);
1275           ast->print("  %c", blobTypeChar[TopSizeArray[i].type]);
1276           //---<  compiler information  >---
1277           ast->fill_to(56);
1278           ast->print("%5s %3d", compTypeName[TopSizeArray[i].compiler], TopSizeArray[i].level);
1279           //---<  method temperature  >---
1280           ast->fill_to(67);
1281           ast->print("%5d", nm->hotness_counter());
1282           //---<  name and signature  >---
1283           ast->fill_to(67+6);
1284           if (nm->is_in_use())        {blob_name = nm->method()->name_and_sig_as_C_string(); }
1285           if (nm->is_not_entrant())   {blob_name = nm->method()->name_and_sig_as_C_string(); }
1286           if (nm->is_not_installed()) {ast->print("%s", " not (yet) installed method "); }
1287           if (nm->is_zombie())        {ast->print("%s", " zombie method "); }
1288           ast->print("%s", blob_name);
1289         } else {
1290           //---<  block size in hex  >---
1291           ast->print(PTR32_FORMAT, (unsigned int)(TopSizeArray[i].len<<log2_seg_size));
1292           ast->print("(" SIZE_FORMAT_W(4) "K)", (TopSizeArray[i].len<<log2_seg_size)/K);
1293           //---<  no compiler information  >---
1294           ast->fill_to(56);
1295           //---<  name and signature  >---
1296           ast->fill_to(67+6);
1297           ast->print("%s", blob_name);
1298         }
1299         STRINGSTREAM_FLUSH_LOCKED("\n")
1300       }
1301       if (used_topSizeBlocks != printed_topSizeBlocks) {
1302         ast->print_cr("used blocks: %d, printed blocks: %d", used_topSizeBlocks, printed_topSizeBlocks);
1303         STRINGSTREAM_FLUSH("")
1304         for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1305           ast->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1306           STRINGSTREAM_FLUSH("")
1307         }
1308       }
1309       STRINGSTREAM_FLUSH_LOCKED("\n\n")
1310     }
1311   }
1312 
1313   //-----------------------------
1314   //--  Print Usage Histogram  --
1315   //-----------------------------
1316 
1317   if (SizeDistributionArray != NULL) {
1318     unsigned long total_count = 0;
1319     unsigned long total_size  = 0;
1320     const unsigned long pctFactor = 200;
1321 
1322     for (unsigned int i = 0; i < nSizeDistElements; i++) {
1323       total_count += SizeDistributionArray[i].count;
1324       total_size  += SizeDistributionArray[i].lenSum;
1325     }
1326 
1327     if ((total_count > 0) && (total_size > 0)) {
1328       printBox(ast, '-', "Block count histogram for ", heapName);
1329       ast->print_cr("Note: The histogram indicates how many blocks (as a percentage\n"
1330                     "      of all blocks) have a size in the given range.\n"
1331                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
1332       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1333       ast->print_cr("total number of all blocks: %7ld\n", total_count);
1334       STRINGSTREAM_FLUSH_LOCKED("")
1335 
1336       ast->print_cr("[Size Range)------avg.-size-+----count-+");
1337       for (unsigned int i = 0; i < nSizeDistElements; i++) {
1338         if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1339           ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1340                     ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1341                     ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1342                     );
1343         } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1344           ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1345                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1346                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1347                     );
1348         } else {
1349           ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1350                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1351                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1352                     );
1353         }
1354         ast->print(" %8d | %8d |",
1355                    SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1356                    SizeDistributionArray[i].count);
1357 
1358         unsigned int percent = pctFactor*SizeDistributionArray[i].count/total_count;
1359         for (unsigned int j = 1; j <= percent; j++) {
1360           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1361         }
1362         ast->cr();
1363       }
1364       ast->print_cr("----------------------------+----------+\n\n");
1365       STRINGSTREAM_FLUSH_LOCKED("\n")
1366 
1367       printBox(ast, '-', "Contribution per size range to total size for ", heapName);
1368       ast->print_cr("Note: The histogram indicates how much space (as a percentage of all\n"
1369                     "      occupied space) is used by the blocks in the given size range.\n"
1370                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
1371       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1372       ast->print_cr("total number of all blocks: %7ld\n", total_count);
1373       STRINGSTREAM_FLUSH_LOCKED("")
1374 
1375       ast->print_cr("[Size Range)------avg.-size-+----count-+");
1376       for (unsigned int i = 0; i < nSizeDistElements; i++) {
1377         if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1378           ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1379                     ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1380                     ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1381                     );
1382         } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1383           ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1384                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1385                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1386                     );
1387         } else {
1388           ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1389                     ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1390                     ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1391                     );
1392         }
1393         ast->print(" %8d | %8d |",
1394                    SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1395                    SizeDistributionArray[i].count);
1396 
1397         unsigned int percent = pctFactor*(unsigned long)SizeDistributionArray[i].lenSum/total_size;
1398         for (unsigned int j = 1; j <= percent; j++) {
1399           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1400         }
1401         ast->cr();
1402       }
1403       ast->print_cr("----------------------------+----------+");
1404       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1405     }
1406   }
1407 }
1408 
1409 
1410 void CodeHeapState::print_freeSpace(outputStream* out, CodeHeap* heap) {
1411   if (!initialization_complete) {
1412     return;
1413   }
1414 
1415   const char* heapName   = get_heapName(heap);
1416   get_HeapStatGlobals(out, heapName);
1417 
1418   if ((StatArray == NULL) || (FreeArray == NULL) || (alloc_granules == 0)) {
1419     return;
1420   }
1421   STRINGSTREAM_DECL(ast, out)
1422 
1423   {
1424     printBox(ast, '=', "F R E E   S P A C E   S T A T I S T I C S   for ", heapName);
1425     ast->print_cr("Note: in this context, a gap is the occupied space between two free blocks.\n"
1426                   "      Those gaps are of interest if there is a chance that they become\n"
1427                   "      unoccupied, e.g. by class unloading. Then, the two adjacent free\n"
1428                   "      blocks, together with the now unoccupied space, form a new, large\n"
1429                   "      free block.");
1430     STRINGSTREAM_FLUSH_LOCKED("\n")
1431   }
1432 
1433   {
1434     printBox(ast, '-', "List of all Free Blocks in ", heapName);
1435     STRINGSTREAM_FLUSH_LOCKED("")
1436 
1437     unsigned int ix = 0;
1438     for (ix = 0; ix < alloc_freeBlocks-1; ix++) {
1439       ast->print(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT ",", p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1440       ast->fill_to(38);
1441       ast->print("Gap[%4d..%4d]: " HEX32_FORMAT " bytes,", ix, ix+1, FreeArray[ix].gap);
1442       ast->fill_to(71);
1443       ast->print("block count: %6d", FreeArray[ix].n_gapBlocks);
1444       if (FreeArray[ix].stubs_in_gap) {
1445         ast->print(" !! permanent gap, contains stubs and/or blobs !!");
1446       }
1447       STRINGSTREAM_FLUSH_LOCKED("\n")
1448     }
1449     ast->print_cr(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT, p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1450     STRINGSTREAM_FLUSH_LOCKED("\n\n")
1451   }
1452 
1453 
1454   //-----------------------------------------
1455   //--  Find and Print Top Ten Free Blocks --
1456   //-----------------------------------------
1457 
1458   //---<  find Top Ten Free Blocks  >---
1459   const unsigned int nTop = 10;
1460   unsigned int  currMax10 = 0;
1461   struct FreeBlk* FreeTopTen[nTop];
1462   memset(FreeTopTen, 0, sizeof(FreeTopTen));
1463 
1464   for (unsigned int ix = 0; ix < alloc_freeBlocks; ix++) {
1465     if (FreeArray[ix].len > currMax10) {  // larger than the ten largest found so far
1466       unsigned int currSize = FreeArray[ix].len;
1467 
1468       unsigned int iy;
1469       for (iy = 0; iy < nTop && FreeTopTen[iy] != NULL; iy++) {
1470         if (FreeTopTen[iy]->len < currSize) {
1471           for (unsigned int iz = nTop-1; iz > iy; iz--) { // make room to insert new free block
1472             FreeTopTen[iz] = FreeTopTen[iz-1];
1473           }
1474           FreeTopTen[iy] = &FreeArray[ix];        // insert new free block
1475           if (FreeTopTen[nTop-1] != NULL) {
1476             currMax10 = FreeTopTen[nTop-1]->len;
1477           }
1478           break; // done with this, check next free block
1479         }
1480       }
1481       if (iy >= nTop) {
1482         ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1483                       currSize, currMax10);
1484         continue;
1485       }
1486       if (FreeTopTen[iy] == NULL) {
1487         FreeTopTen[iy] = &FreeArray[ix];
1488         if (iy == (nTop-1)) {
1489           currMax10 = currSize;
1490         }
1491       }
1492     }
1493   }
1494   STRINGSTREAM_FLUSH_LOCKED("")
1495 
1496   {
1497     printBox(ast, '-', "Top Ten Free Blocks in ", heapName);
1498 
1499     //---<  print Top Ten Free Blocks  >---
1500     for (unsigned int iy = 0; (iy < nTop) && (FreeTopTen[iy] != NULL); iy++) {
1501       ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTen[iy]->index, FreeTopTen[iy]->len);
1502       ast->fill_to(39);
1503       if (FreeTopTen[iy]->index == (alloc_freeBlocks-1)) {
1504         ast->print("last free block in list.");
1505       } else {
1506         ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTen[iy]->gap);
1507         ast->fill_to(63);
1508         ast->print("#blocks (in gap) %d", FreeTopTen[iy]->n_gapBlocks);
1509       }
1510       ast->cr();
1511     }
1512     STRINGSTREAM_FLUSH_LOCKED("\n\n")
1513   }
1514 
1515 
1516   //--------------------------------------------------------
1517   //--  Find and Print Top Ten Free-Occupied-Free Triples --
1518   //--------------------------------------------------------
1519 
1520   //---<  find and print Top Ten Triples (Free-Occupied-Free)  >---
1521   currMax10 = 0;
1522   struct FreeBlk  *FreeTopTenTriple[nTop];
1523   memset(FreeTopTenTriple, 0, sizeof(FreeTopTenTriple));
1524 
1525   for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1526     // If there are stubs in the gap, this gap will never become completely free.
1527     // The triple will thus never merge to one free block.
1528     unsigned int lenTriple  = FreeArray[ix].len + (FreeArray[ix].stubs_in_gap ? 0 : FreeArray[ix].gap + FreeArray[ix+1].len);
1529     FreeArray[ix].len = lenTriple;
1530     if (lenTriple > currMax10) {  // larger than the ten largest found so far
1531 
1532       unsigned int iy;
1533       for (iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1534         if (FreeTopTenTriple[iy]->len < lenTriple) {
1535           for (unsigned int iz = nTop-1; iz > iy; iz--) {
1536             FreeTopTenTriple[iz] = FreeTopTenTriple[iz-1];
1537           }
1538           FreeTopTenTriple[iy] = &FreeArray[ix];
1539           if (FreeTopTenTriple[nTop-1] != NULL) {
1540             currMax10 = FreeTopTenTriple[nTop-1]->len;
1541           }
1542           break;
1543         }
1544       }
1545       if (iy == nTop) {
1546         ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1547                       lenTriple, currMax10);
1548         continue;
1549       }
1550       if (FreeTopTenTriple[iy] == NULL) {
1551         FreeTopTenTriple[iy] = &FreeArray[ix];
1552         if (iy == (nTop-1)) {
1553           currMax10 = lenTriple;
1554         }
1555       }
1556     }
1557   }
1558   STRINGSTREAM_FLUSH_LOCKED("")
1559 
1560   {
1561     printBox(ast, '-', "Top Ten Free-Occupied-Free Triples in ", heapName);
1562     ast->print_cr("  Use this information to judge how likely it is that a large(r) free block\n"
1563                   "  might get created by code cache sweeping.\n"
1564                   "  If all the occupied blocks can be swept, the three free blocks will be\n"
1565                   "  merged into one (much larger) free block. That would reduce free space\n"
1566                   "  fragmentation.\n");
1567 
1568     //---<  print Top Ten Free-Occupied-Free Triples  >---
1569     for (unsigned int iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1570       ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTenTriple[iy]->index, FreeTopTenTriple[iy]->len);
1571       ast->fill_to(39);
1572       ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTenTriple[iy]->gap);
1573       ast->fill_to(63);
1574       ast->print("#blocks (in gap) %d", FreeTopTenTriple[iy]->n_gapBlocks);
1575       ast->cr();
1576     }
1577     STRINGSTREAM_FLUSH_LOCKED("\n\n")
1578   }
1579 }
1580 
1581 
1582 void CodeHeapState::print_count(outputStream* out, CodeHeap* heap) {
1583   if (!initialization_complete) {
1584     return;
1585   }
1586 
1587   const char* heapName   = get_heapName(heap);
1588   get_HeapStatGlobals(out, heapName);
1589 
1590   if ((StatArray == NULL) || (alloc_granules == 0)) {
1591     return;
1592   }
1593   STRINGSTREAM_DECL(ast, out)
1594 
1595   unsigned int granules_per_line = 32;
1596   char*        low_bound         = heap->low_boundary();
1597 
1598   {
1599     printBox(ast, '=', "B L O C K   C O U N T S   for ", heapName);
1600     ast->print_cr("  Each granule contains an individual number of heap blocks. Large blocks\n"
1601                   "  may span multiple granules and are counted for each granule they touch.\n");
1602     if (segment_granules) {
1603       ast->print_cr("  You have selected granule size to be as small as segment size.\n"
1604                     "  As a result, each granule contains exactly one block (or a part of one block)\n"
1605                     "  or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1606                     "  Occupied granules show their BlobType character, see legend.\n");
1607       print_blobType_legend(ast);
1608     }
1609     STRINGSTREAM_FLUSH_LOCKED("")
1610   }
1611 
1612   {
1613     if (segment_granules) {
1614       printBox(ast, '-', "Total (all types) count for granule size == segment size", NULL);
1615       STRINGSTREAM_FLUSH_LOCKED("")
1616 
1617       granules_per_line = 128;
1618       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1619         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1620         print_blobType_single(ast, StatArray[ix].type);
1621       }
1622     } else {
1623       printBox(ast, '-', "Total (all tiers) count, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1624       STRINGSTREAM_FLUSH_LOCKED("")
1625 
1626       granules_per_line = 128;
1627       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1628         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1629         unsigned int count = StatArray[ix].t1_count   + StatArray[ix].t2_count   + StatArray[ix].tx_count
1630                            + StatArray[ix].stub_count + StatArray[ix].dead_count;
1631         print_count_single(ast, count);
1632       }
1633     }
1634     STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1635   }
1636 
1637   {
1638     if (nBlocks_t1 > 0) {
1639       printBox(ast, '-', "Tier1 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1640       STRINGSTREAM_FLUSH_LOCKED("")
1641 
1642       granules_per_line = 128;
1643       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1644         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1645         if (segment_granules && StatArray[ix].t1_count > 0) {
1646           print_blobType_single(ast, StatArray[ix].type);
1647         } else {
1648           print_count_single(ast, StatArray[ix].t1_count);
1649         }
1650       }
1651       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1652     } else {
1653       ast->print("No Tier1 nMethods found in CodeHeap.");
1654       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1655     }
1656   }
1657 
1658   {
1659     if (nBlocks_t2 > 0) {
1660       printBox(ast, '-', "Tier2 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1661       STRINGSTREAM_FLUSH_LOCKED("")
1662 
1663       granules_per_line = 128;
1664       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1665         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1666         if (segment_granules && StatArray[ix].t2_count > 0) {
1667           print_blobType_single(ast, StatArray[ix].type);
1668         } else {
1669           print_count_single(ast, StatArray[ix].t2_count);
1670         }
1671       }
1672       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1673     } else {
1674       ast->print("No Tier2 nMethods found in CodeHeap.");
1675       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1676     }
1677   }
1678 
1679   {
1680     if (nBlocks_alive > 0) {
1681       printBox(ast, '-', "not_used/not_entrant/not_installed nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1682       STRINGSTREAM_FLUSH_LOCKED("")
1683 
1684       granules_per_line = 128;
1685       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1686         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1687         if (segment_granules && StatArray[ix].tx_count > 0) {
1688           print_blobType_single(ast, StatArray[ix].type);
1689         } else {
1690           print_count_single(ast, StatArray[ix].tx_count);
1691         }
1692       }
1693       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1694     } else {
1695       ast->print("No not_used/not_entrant nMethods found in CodeHeap.");
1696       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1697     }
1698   }
1699 
1700   {
1701     if (nBlocks_stub > 0) {
1702       printBox(ast, '-', "Stub & Blob count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1703       STRINGSTREAM_FLUSH_LOCKED("")
1704 
1705       granules_per_line = 128;
1706       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1707         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1708         if (segment_granules && StatArray[ix].stub_count > 0) {
1709           print_blobType_single(ast, StatArray[ix].type);
1710         } else {
1711           print_count_single(ast, StatArray[ix].stub_count);
1712         }
1713       }
1714       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1715     } else {
1716       ast->print("No Stubs and Blobs found in CodeHeap.");
1717       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1718     }
1719   }
1720 
1721   {
1722     if (nBlocks_dead > 0) {
1723       printBox(ast, '-', "Dead nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1724       STRINGSTREAM_FLUSH_LOCKED("")
1725 
1726       granules_per_line = 128;
1727       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1728         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1729         if (segment_granules && StatArray[ix].dead_count > 0) {
1730           print_blobType_single(ast, StatArray[ix].type);
1731         } else {
1732           print_count_single(ast, StatArray[ix].dead_count);
1733         }
1734       }
1735       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1736     } else {
1737       ast->print("No dead nMethods found in CodeHeap.");
1738       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1739     }
1740   }
1741 
1742   {
1743     if (!segment_granules) { // Prevent totally redundant printouts
1744       printBox(ast, '-', "Count by tier (combined, no dead blocks): <#t1>:<#t2>:<#s>, 0x0..0xf. '*' indicates >= 16 blocks", NULL);
1745       STRINGSTREAM_FLUSH_LOCKED("")
1746 
1747       granules_per_line = 24;
1748       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1749         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1750 
1751         print_count_single(ast, StatArray[ix].t1_count);
1752         ast->print(":");
1753         print_count_single(ast, StatArray[ix].t2_count);
1754         ast->print(":");
1755         if (segment_granules && StatArray[ix].stub_count > 0) {
1756           print_blobType_single(ast, StatArray[ix].type);
1757         } else {
1758           print_count_single(ast, StatArray[ix].stub_count);
1759         }
1760         ast->print(" ");
1761       }
1762       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1763     }
1764   }
1765 }
1766 
1767 
1768 void CodeHeapState::print_space(outputStream* out, CodeHeap* heap) {
1769   if (!initialization_complete) {
1770     return;
1771   }
1772 
1773   const char* heapName   = get_heapName(heap);
1774   get_HeapStatGlobals(out, heapName);
1775 
1776   if ((StatArray == NULL) || (alloc_granules == 0)) {
1777     return;
1778   }
1779   STRINGSTREAM_DECL(ast, out)
1780 
1781   unsigned int granules_per_line = 32;
1782   char*        low_bound         = heap->low_boundary();
1783 
1784   {
1785     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);
1786     ast->print_cr("  The heap space covered by one granule is occupied to a various extend.\n"
1787                   "  The granule occupancy is displayed by one decimal digit per granule.\n");
1788     if (segment_granules) {
1789       ast->print_cr("  You have selected granule size to be as small as segment size.\n"
1790                     "  As a result, each granule contains exactly one block (or a part of one block)\n"
1791                     "  or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1792                     "  Occupied granules show their BlobType character, see legend.\n");
1793       print_blobType_legend(ast);
1794     } else {
1795       ast->print_cr("  These digits represent a fill percentage range (see legend).\n");
1796       print_space_legend(ast);
1797     }
1798     STRINGSTREAM_FLUSH_LOCKED("")
1799   }
1800 
1801   {
1802     if (segment_granules) {
1803       printBox(ast, '-', "Total (all types) space consumption for granule size == segment size", NULL);
1804       STRINGSTREAM_FLUSH_LOCKED("")
1805 
1806       granules_per_line = 128;
1807       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1808         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1809         print_blobType_single(ast, StatArray[ix].type);
1810       }
1811     } else {
1812       printBox(ast, '-', "Total (all types) space consumption. ' ' indicates empty, '*' indicates full.", NULL);
1813       STRINGSTREAM_FLUSH_LOCKED("")
1814 
1815       granules_per_line = 128;
1816       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1817         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1818         unsigned int space    = StatArray[ix].t1_space   + StatArray[ix].t2_space  + StatArray[ix].tx_space
1819                               + StatArray[ix].stub_space + StatArray[ix].dead_space;
1820         print_space_single(ast, space);
1821       }
1822     }
1823     STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1824   }
1825 
1826   {
1827     if (nBlocks_t1 > 0) {
1828       printBox(ast, '-', "Tier1 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1829       STRINGSTREAM_FLUSH_LOCKED("")
1830 
1831       granules_per_line = 128;
1832       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1833         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1834         if (segment_granules && StatArray[ix].t1_space > 0) {
1835           print_blobType_single(ast, StatArray[ix].type);
1836         } else {
1837           print_space_single(ast, StatArray[ix].t1_space);
1838         }
1839       }
1840       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1841     } else {
1842       ast->print("No Tier1 nMethods found in CodeHeap.");
1843       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1844     }
1845   }
1846 
1847   {
1848     if (nBlocks_t2 > 0) {
1849       printBox(ast, '-', "Tier2 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1850       STRINGSTREAM_FLUSH_LOCKED("")
1851 
1852       granules_per_line = 128;
1853       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1854         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1855         if (segment_granules && StatArray[ix].t2_space > 0) {
1856           print_blobType_single(ast, StatArray[ix].type);
1857         } else {
1858           print_space_single(ast, StatArray[ix].t2_space);
1859         }
1860       }
1861       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1862     } else {
1863       ast->print("No Tier2 nMethods found in CodeHeap.");
1864       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1865     }
1866   }
1867 
1868   {
1869     if (nBlocks_alive > 0) {
1870       printBox(ast, '-', "not_used/not_entrant/not_installed space consumption. ' ' indicates empty, '*' indicates full", NULL);
1871 
1872       granules_per_line = 128;
1873       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1874         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1875         if (segment_granules && StatArray[ix].tx_space > 0) {
1876           print_blobType_single(ast, StatArray[ix].type);
1877         } else {
1878           print_space_single(ast, StatArray[ix].tx_space);
1879         }
1880       }
1881       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1882     } else {
1883       ast->print("No Tier2 nMethods found in CodeHeap.");
1884       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1885     }
1886   }
1887 
1888   {
1889     if (nBlocks_stub > 0) {
1890       printBox(ast, '-', "Stub and Blob space consumption. ' ' indicates empty, '*' indicates full", NULL);
1891       STRINGSTREAM_FLUSH_LOCKED("")
1892 
1893       granules_per_line = 128;
1894       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1895         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1896         if (segment_granules && StatArray[ix].stub_space > 0) {
1897           print_blobType_single(ast, StatArray[ix].type);
1898         } else {
1899           print_space_single(ast, StatArray[ix].stub_space);
1900         }
1901       }
1902       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1903     } else {
1904       ast->print("No Stubs and Blobs found in CodeHeap.");
1905       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1906     }
1907   }
1908 
1909   {
1910     if (nBlocks_dead > 0) {
1911       printBox(ast, '-', "Dead space consumption. ' ' indicates empty, '*' indicates full", NULL);
1912       STRINGSTREAM_FLUSH_LOCKED("")
1913 
1914       granules_per_line = 128;
1915       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1916         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1917         print_space_single(ast, StatArray[ix].dead_space);
1918       }
1919       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1920     } else {
1921       ast->print("No dead nMethods found in CodeHeap.");
1922       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1923     }
1924   }
1925 
1926   {
1927     if (!segment_granules) { // Prevent totally redundant printouts
1928       printBox(ast, '-', "Space consumption by tier (combined): <t1%>:<t2%>:<s%>. ' ' indicates empty, '*' indicates full", NULL);
1929       STRINGSTREAM_FLUSH_LOCKED("")
1930 
1931       granules_per_line = 24;
1932       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1933         print_line_delim(out, ast, low_bound, ix, granules_per_line);
1934 
1935         if (segment_granules && StatArray[ix].t1_space > 0) {
1936           print_blobType_single(ast, StatArray[ix].type);
1937         } else {
1938           print_space_single(ast, StatArray[ix].t1_space);
1939         }
1940         ast->print(":");
1941         if (segment_granules && StatArray[ix].t2_space > 0) {
1942           print_blobType_single(ast, StatArray[ix].type);
1943         } else {
1944           print_space_single(ast, StatArray[ix].t2_space);
1945         }
1946         ast->print(":");
1947         if (segment_granules && StatArray[ix].stub_space > 0) {
1948           print_blobType_single(ast, StatArray[ix].type);
1949         } else {
1950           print_space_single(ast, StatArray[ix].stub_space);
1951         }
1952         ast->print(" ");
1953       }
1954       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1955     }
1956   }
1957 }
1958 
1959 void CodeHeapState::print_age(outputStream* out, CodeHeap* heap) {
1960   if (!initialization_complete) {
1961     return;
1962   }
1963 
1964   const char* heapName   = get_heapName(heap);
1965   get_HeapStatGlobals(out, heapName);
1966 
1967   if ((StatArray == NULL) || (alloc_granules == 0)) {
1968     return;
1969   }
1970   STRINGSTREAM_DECL(ast, out)
1971 
1972   unsigned int granules_per_line = 32;
1973   char*        low_bound         = heap->low_boundary();
1974 
1975   {
1976     printBox(ast, '=', "M E T H O D   A G E   by CompileID for ", heapName);
1977     ast->print_cr("  The age of a compiled method in the CodeHeap is not available as a\n"
1978                   "  time stamp. Instead, a relative age is deducted from the method's compilation ID.\n"
1979                   "  Age information is available for tier1 and tier2 methods only. There is no\n"
1980                   "  age information for stubs and blobs, because they have no compilation ID assigned.\n"
1981                   "  Information for the youngest method (highest ID) in the granule is printed.\n"
1982                   "  Refer to the legend to learn how method age is mapped to the displayed digit.");
1983     print_age_legend(ast);
1984     STRINGSTREAM_FLUSH_LOCKED("")
1985   }
1986 
1987   {
1988     printBox(ast, '-', "Age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
1989     STRINGSTREAM_FLUSH_LOCKED("")
1990 
1991     granules_per_line = 128;
1992     for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1993       print_line_delim(out, ast, low_bound, ix, granules_per_line);
1994       unsigned int age1      = StatArray[ix].t1_age;
1995       unsigned int age2      = StatArray[ix].t2_age;
1996       unsigned int agex      = StatArray[ix].tx_age;
1997       unsigned int age       = age1 > age2 ? age1 : age2;
1998       age       = age > agex ? age : agex;
1999       print_age_single(ast, age);
2000     }
2001     STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2002   }
2003 
2004   {
2005     if (nBlocks_t1 > 0) {
2006       printBox(ast, '-', "Tier1 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2007       STRINGSTREAM_FLUSH_LOCKED("")
2008 
2009       granules_per_line = 128;
2010       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2011         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2012         print_age_single(ast, StatArray[ix].t1_age);
2013       }
2014       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2015     } else {
2016       ast->print("No Tier1 nMethods found in CodeHeap.");
2017       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2018     }
2019   }
2020 
2021   {
2022     if (nBlocks_t2 > 0) {
2023       printBox(ast, '-', "Tier2 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2024       STRINGSTREAM_FLUSH_LOCKED("")
2025 
2026       granules_per_line = 128;
2027       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2028         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2029         print_age_single(ast, StatArray[ix].t2_age);
2030       }
2031       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2032     } else {
2033       ast->print("No Tier2 nMethods found in CodeHeap.");
2034       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2035     }
2036   }
2037 
2038   {
2039     if (nBlocks_alive > 0) {
2040       printBox(ast, '-', "not_used/not_entrant/not_installed age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2041       STRINGSTREAM_FLUSH_LOCKED("")
2042 
2043       granules_per_line = 128;
2044       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2045         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2046         print_age_single(ast, StatArray[ix].tx_age);
2047       }
2048       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2049     } else {
2050       ast->print("No Tier2 nMethods found in CodeHeap.");
2051       STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2052     }
2053   }
2054 
2055   {
2056     if (!segment_granules) { // Prevent totally redundant printouts
2057       printBox(ast, '-', "age distribution by tier <a1>:<a2>. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2058       STRINGSTREAM_FLUSH_LOCKED("")
2059 
2060       granules_per_line = 32;
2061       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2062         print_line_delim(out, ast, low_bound, ix, granules_per_line);
2063         print_age_single(ast, StatArray[ix].t1_age);
2064         ast->print(":");
2065         print_age_single(ast, StatArray[ix].t2_age);
2066         ast->print(" ");
2067       }
2068       STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2069     }
2070   }
2071 }
2072 
2073 
2074 void CodeHeapState::print_names(outputStream* out, CodeHeap* heap) {
2075   if (!initialization_complete) {
2076     return;
2077   }
2078 
2079   const char* heapName   = get_heapName(heap);
2080   get_HeapStatGlobals(out, heapName);
2081 
2082   if ((StatArray == NULL) || (alloc_granules == 0)) {
2083     return;
2084   }
2085   STRINGSTREAM_DECL(ast, out)
2086 
2087   unsigned int granules_per_line  = 128;
2088   char*        low_bound          = heap->low_boundary();
2089   CodeBlob*    last_blob          = NULL;
2090   bool         name_in_addr_range = true;
2091 
2092   //---<  print at least 128K per block (i.e. between headers)  >---
2093   if (granules_per_line*granule_size < 128*K) {
2094     granules_per_line = (unsigned int)((128*K)/granule_size);
2095   }
2096 
2097   printBox(ast, '=', "M E T H O D   N A M E S   for ", heapName);
2098   ast->print_cr("  Method names are dynamically retrieved from the code cache at print time.\n"
2099                 "  Due to the living nature of the code heap and because the CodeCache_lock\n"
2100                 "  is not continuously held, the displayed name might be wrong or no name\n"
2101                 "  might be found at all. The likelihood for that to happen increases\n"
2102                 "  over time passed between aggregtion and print steps.\n");
2103   STRINGSTREAM_FLUSH_LOCKED("")
2104 
2105   for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2106     //---<  print a new blob on a new line  >---
2107     if (ix%granules_per_line == 0) {
2108       if (!name_in_addr_range) {
2109         ast->print_cr("No methods, blobs, or stubs found in this address range");
2110       }
2111       name_in_addr_range = false;
2112 
2113       size_t end_ix = (ix+granules_per_line <= alloc_granules) ? ix+granules_per_line : alloc_granules;
2114       ast->cr();
2115       ast->print_cr("--------------------------------------------------------------------");
2116       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);
2117       ast->print_cr("--------------------------------------------------------------------");
2118       STRINGSTREAM_FLUSH_LOCKED("")
2119     }
2120     // Only check granule if it contains at least one blob.
2121     unsigned int nBlobs  = StatArray[ix].t1_count   + StatArray[ix].t2_count + StatArray[ix].tx_count +
2122                            StatArray[ix].stub_count + StatArray[ix].dead_count;
2123     if (nBlobs > 0 ) {
2124     for (unsigned int is = 0; is < granule_size; is+=(unsigned int)seg_size) {
2125       // heap->find_start() is safe. Only working with _segmap. Returns NULL or void*. Returned CodeBlob may be uninitialized.
2126       CodeBlob* this_blob = (CodeBlob *)(heap->find_start(low_bound+ix*granule_size+is));
2127       bool blob_initialized = (this_blob != NULL) && (this_blob->header_size() >= 0) && (this_blob->relocation_size() >= 0) &&
2128                               ((address)this_blob + this_blob->header_size() == (address)(this_blob->relocation_begin())) &&
2129                               ((address)this_blob + CodeBlob::align_code_offset(this_blob->header_size() + this_blob->relocation_size()) == (address)(this_blob->content_begin())) &&
2130                               os::is_readable_pointer((address)(this_blob->relocation_begin())) &&
2131                               os::is_readable_pointer(this_blob->content_begin());
2132       // blob could have been flushed, freed, and merged.
2133       // this_blob < last_blob is an indicator for that.
2134       if (blob_initialized && (this_blob > last_blob)) {
2135         last_blob          = this_blob;
2136 
2137         //---<  get type and name  >---
2138         blobType       cbType = noType;
2139         if (segment_granules) {
2140           cbType = (blobType)StatArray[ix].type;
2141         } else {
2142           cbType = get_cbType(this_blob);
2143         }
2144         // this_blob->name() could return NULL if no name was given to CTOR. Inlined, maybe invisible on stack
2145         const char* blob_name = this_blob->name();
2146         if ((blob_name == NULL) || !os::is_readable_pointer(blob_name)) {
2147           blob_name = "<unavailable>";
2148         }
2149 
2150         //---<  print table header for new print range  >---
2151         if (!name_in_addr_range) {
2152           name_in_addr_range = true;
2153           ast->fill_to(51);
2154           ast->print("%9s", "compiler");
2155           ast->fill_to(61);
2156           ast->print_cr("%6s", "method");
2157           ast->print_cr("%18s %13s %17s %9s  %5s %18s  %s", "Addr(module)      ", "offset", "size", " type lvl", " temp", "blobType          ", "Name");
2158           STRINGSTREAM_FLUSH_LOCKED("")
2159         }
2160 
2161         //---<  print line prefix (address and offset from CodeHeap start)  >---
2162         ast->print(INTPTR_FORMAT, p2i(this_blob));
2163         ast->fill_to(19);
2164         ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
2165         ast->fill_to(33);
2166 
2167         // this_blob->as_nmethod_or_null() is safe. Inlined, maybe invisible on stack.
2168         nmethod*    nm     = this_blob->as_nmethod_or_null();
2169         if (CompiledMethod::nmethod_access_is_safe(nm)) {
2170           Method* method = nm->method();
2171           ResourceMark rm;
2172           //---<  collect all data to locals as quickly as possible  >---
2173           unsigned int total_size = nm->total_size();
2174           int          hotness    = nm->hotness_counter();
2175           bool         get_name   = (cbType == nMethod_inuse) || (cbType == nMethod_notused);
2176           //---<  nMethod size in hex  >---
2177           ast->print(PTR32_FORMAT, total_size);
2178           ast->print("(" SIZE_FORMAT_W(4) "K)", total_size/K);
2179           //---<  compiler information  >---
2180           ast->fill_to(51);
2181           ast->print("%5s %3d", compTypeName[StatArray[ix].compiler], StatArray[ix].level);
2182           //---<  method temperature  >---
2183           ast->fill_to(62);
2184           ast->print("%5d", hotness);
2185           //---<  name and signature  >---
2186           ast->fill_to(62+6);
2187           ast->print("%s", blobTypeName[cbType]);
2188           ast->fill_to(82+6);
2189           if (cbType == nMethod_dead) {
2190             ast->print("%14s", " zombie method");
2191           }
2192 
2193           if (get_name) {
2194             Symbol* methName  = method->name();
2195             const char*   methNameS = (methName == NULL) ? NULL : methName->as_C_string();
2196             methNameS = (methNameS == NULL) ? "<method name unavailable>" : methNameS;
2197             Symbol* methSig   = method->signature();
2198             const char*   methSigS  = (methSig  == NULL) ? NULL : methSig->as_C_string();
2199             methSigS  = (methSigS  == NULL) ? "<method signature unavailable>" : methSigS;
2200             ast->print("%s", methNameS);
2201             ast->print("%s", methSigS);
2202           } else {
2203             ast->print("%s", blob_name);
2204           }
2205         } else {
2206           ast->fill_to(62+6);
2207           ast->print("%s", blobTypeName[cbType]);
2208           ast->fill_to(82+6);
2209           ast->print("%s", blob_name);
2210         }
2211         STRINGSTREAM_FLUSH_LOCKED("\n")
2212       } else if (!blob_initialized && (this_blob != last_blob) && (this_blob != NULL)) {
2213         last_blob          = this_blob;
2214         STRINGSTREAM_FLUSH_LOCKED("\n")
2215       }
2216     }
2217     } // nBlobs > 0
2218   }
2219   STRINGSTREAM_FLUSH_LOCKED("\n\n")
2220 }
2221 
2222 
2223 void CodeHeapState::printBox(outputStream* ast, const char border, const char* text1, const char* text2) {
2224   unsigned int lineLen = 1 + 2 + 2 + 1;
2225   char edge, frame;
2226 
2227   if (text1 != NULL) {
2228     lineLen += (unsigned int)strlen(text1); // text1 is much shorter than MAX_INT chars.
2229   }
2230   if (text2 != NULL) {
2231     lineLen += (unsigned int)strlen(text2); // text2 is much shorter than MAX_INT chars.
2232   }
2233   if (border == '-') {
2234     edge  = '+';
2235     frame = '|';
2236   } else {
2237     edge  = border;
2238     frame = border;
2239   }
2240 
2241   ast->print("%c", edge);
2242   for (unsigned int i = 0; i < lineLen-2; i++) {
2243     ast->print("%c", border);
2244   }
2245   ast->print_cr("%c", edge);
2246 
2247   ast->print("%c  ", frame);
2248   if (text1 != NULL) {
2249     ast->print("%s", text1);
2250   }
2251   if (text2 != NULL) {
2252     ast->print("%s", text2);
2253   }
2254   ast->print_cr("  %c", frame);
2255 
2256   ast->print("%c", edge);
2257   for (unsigned int i = 0; i < lineLen-2; i++) {
2258     ast->print("%c", border);
2259   }
2260   ast->print_cr("%c", edge);
2261 }
2262 
2263 void CodeHeapState::print_blobType_legend(outputStream* out) {
2264   out->cr();
2265   printBox(out, '-', "Block types used in the following CodeHeap dump", NULL);
2266   for (int type = noType; type < lastType; type += 1) {
2267     out->print_cr("  %c - %s", blobTypeChar[type], blobTypeName[type]);
2268   }
2269   out->print_cr("  -----------------------------------------------------");
2270   out->cr();
2271 }
2272 
2273 void CodeHeapState::print_space_legend(outputStream* out) {
2274   unsigned int indicator = 0;
2275   unsigned int age_range = 256;
2276   unsigned int range_beg = latest_compilation_id;
2277   out->cr();
2278   printBox(out, '-', "Space ranges, based on granule occupancy", NULL);
2279   out->print_cr("    -   0%% == occupancy");
2280   for (int i=0; i<=9; i++) {
2281     out->print_cr("  %d - %3d%% < occupancy < %3d%%", i, 10*i, 10*(i+1));
2282   }
2283   out->print_cr("  * - 100%% == occupancy");
2284   out->print_cr("  ----------------------------------------------");
2285   out->cr();
2286 }
2287 
2288 void CodeHeapState::print_age_legend(outputStream* out) {
2289   unsigned int indicator = 0;
2290   unsigned int age_range = 256;
2291   unsigned int range_beg = latest_compilation_id;
2292   out->cr();
2293   printBox(out, '-', "Age ranges, based on compilation id", NULL);
2294   while (age_range > 0) {
2295     out->print_cr("  %d - %6d to %6d", indicator, range_beg, latest_compilation_id - latest_compilation_id/age_range);
2296     range_beg = latest_compilation_id - latest_compilation_id/age_range;
2297     age_range /= 2;
2298     indicator += 1;
2299   }
2300   out->print_cr("  -----------------------------------------");
2301   out->cr();
2302 }
2303 
2304 void CodeHeapState::print_blobType_single(outputStream* out, u2 /* blobType */ type) {
2305   out->print("%c", blobTypeChar[type]);
2306 }
2307 
2308 void CodeHeapState::print_count_single(outputStream* out, unsigned short count) {
2309   if (count >= 16)    out->print("*");
2310   else if (count > 0) out->print("%1.1x", count);
2311   else                out->print(" ");
2312 }
2313 
2314 void CodeHeapState::print_space_single(outputStream* out, unsigned short space) {
2315   size_t  space_in_bytes = ((unsigned int)space)<<log2_seg_size;
2316   char    fraction       = (space == 0) ? ' ' : (space_in_bytes >= granule_size-1) ? '*' : char('0'+10*space_in_bytes/granule_size);
2317   out->print("%c", fraction);
2318 }
2319 
2320 void CodeHeapState::print_age_single(outputStream* out, unsigned int age) {
2321   unsigned int indicator = 0;
2322   unsigned int age_range = 256;
2323   if (age > 0) {
2324     while ((age_range > 0) && (latest_compilation_id-age > latest_compilation_id/age_range)) {
2325       age_range /= 2;
2326       indicator += 1;
2327     }
2328     out->print("%c", char('0'+indicator));
2329   } else {
2330     out->print(" ");
2331   }
2332 }
2333 
2334 void CodeHeapState::print_line_delim(outputStream* out, outputStream* ast, char* low_bound, unsigned int ix, unsigned int gpl) {
2335   if (ix % gpl == 0) {
2336     if (ix > 0) {
2337       ast->print("|");
2338     }
2339     ast->cr();
2340     assert(out == ast, "must use the same stream!");
2341 
2342     ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2343     ast->fill_to(19);
2344     ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2345   }
2346 }
2347 
2348 void CodeHeapState::print_line_delim(outputStream* out, bufferedStream* ast, char* low_bound, unsigned int ix, unsigned int gpl) {
2349   assert(out != ast, "must not use the same stream!");
2350   if (ix % gpl == 0) {
2351     if (ix > 0) {
2352       ast->print("|");
2353     }
2354     ast->cr();
2355 
2356     { // can't use STRINGSTREAM_FLUSH_LOCKED("") here.
2357       ttyLocker ttyl;
2358       out->print("%s", ast->as_string());
2359       ast->reset();
2360     }
2361 
2362     ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2363     ast->fill_to(19);
2364     ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2365   }
2366 }
2367 
2368 CodeHeapState::blobType CodeHeapState::get_cbType(CodeBlob* cb) {
2369   if ((cb != NULL) && os::is_readable_pointer(cb)) {
2370     if (cb->is_runtime_stub())                return runtimeStub;
2371     if (cb->is_deoptimization_stub())         return deoptimizationStub;
2372     if (cb->is_uncommon_trap_stub())          return uncommonTrapStub;
2373     if (cb->is_exception_stub())              return exceptionStub;
2374     if (cb->is_safepoint_stub())              return safepointStub;
2375     if (cb->is_adapter_blob())                return adapterBlob;
2376     if (cb->is_method_handles_adapter_blob()) return mh_adapterBlob;
2377     if (cb->is_buffer_blob())                 return bufferBlob;
2378 
2379     nmethod*  nm = cb->as_nmethod_or_null();
2380     if (nm != NULL) { // no is_readable check required, nm = (nmethod*)cb.
2381       if (nm->is_not_installed()) return nMethod_inconstruction;
2382       if (nm->is_zombie())        return nMethod_dead;
2383       if (nm->is_unloaded())      return nMethod_unloaded;
2384       if (nm->is_in_use())        return nMethod_inuse;
2385       if (nm->is_alive() && !(nm->is_not_entrant()))   return nMethod_notused;
2386       if (nm->is_alive())         return nMethod_alive;
2387       return nMethod_dead;
2388     }
2389   }
2390   return noType;
2391 }