< prev index next >

src/hotspot/share/code/codeHeapState.cpp

Print this page

        

@@ -1,8 +1,8 @@
 /*
  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2018 SAP SE. All rights reserved.
+ * Copyright (c) 2018, 2019 SAP SE. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -24,10 +24,11 @@
  */
 
 #include "precompiled.hpp"
 #include "code/codeHeapState.hpp"
 #include "compiler/compileBroker.hpp"
+#include "runtime/safepoint.hpp"
 #include "runtime/sweeper.hpp"
 
 // -------------------------
 // |  General Description  |
 // -------------------------

@@ -36,21 +37,23 @@
 // information that is believed useful/important.
 //
 // Aggregation condenses the information of a piece of the CodeHeap
 // (4096 bytes by default) into an analysis granule. These granules
 // contain enough detail to gain initial insight while keeping the
-// internal sttructure sizes in check.
-//
-// The CodeHeap is a living thing. Therefore, the aggregate is collected
-// under the CodeCache_lock. The subsequent print steps are only locked
-// against concurrent aggregations. That keeps the impact on
-// "normal operation" (JIT compiler and sweeper activity) to a minimum.
+// internal structure sizes in check.
 //
 // The second part, which consists of several, independent steps,
 // prints the previously collected information with emphasis on
 // various aspects.
 //
+// The CodeHeap is a living thing. Therefore, protection against concurrent
+// modification (by acquiring the CodeCache_lock) is necessary. It has
+// to be provided by the caller of the analysis functions.
+// If the CodeCache_lock is not held, the analysis functions may print
+// less detailed information or may just do nothing. It is by intention
+// that an unprotected invocation is not abnormally terminated.
+//
 // Data collection and printing is done on an "on request" basis.
 // While no request is being processed, there is no impact on performance.
 // The CodeHeap state analytics do have some memory footprint.
 // The "aggregate" step allocates some data structures to hold the aggregated
 // information for later output. These data structures live until they are

@@ -61,67 +64,155 @@
 // Requests for real-time, on-the-fly analysis can be issued via
 //   jcmd <pid> Compiler.CodeHeap_Analytics [<function>] [<granularity>]
 //
 // If you are (only) interested in how the CodeHeap looks like after running
 // a sample workload, you can use the command line option
-//   -Xlog:codecache=Trace
-//
-// To see the CodeHeap state in case of a "CodeCache full" condition, start the
-// VM with the
-//   -Xlog:codecache=Debug
-// command line option. It will produce output only for the first time the
-// condition is recognized.
+//   -XX:+PrintCodeHeapAnalytics
+// It will cause a full analysis to be written to tty. In addition, a full
+// analysis will be written the first time a "CodeCache full" condition is
+// detected.
 //
-// Both command line option variants produce output identical to the jcmd function
+// The command line option produces output identical to the jcmd function
 //   jcmd <pid> Compiler.CodeHeap_Analytics all 4096
 // ---------------------------------------------------------------------------------
 
 // With this declaration macro, it is possible to switch between
 //  - direct output into an argument-passed outputStream and
 //  - buffered output into a bufferedStream with subsequent flush
 //    of the filled buffer to the outputStream.
-#define USE_STRINGSTREAM
-#define HEX32_FORMAT  "0x%x"  // just a helper format string used below multiple times
-//
+#define USE_BUFFEREDSTREAM
+
+// There are instances when composing an output line or a small set of
+// output lines out of many tty->print() calls creates significant overhead.
 // Writing to a bufferedStream buffer first has a significant advantage:
-// It uses noticeably less cpu cycles and reduces (when wirting to a
-// network file) the required bandwidth by at least a factor of ten.
+// It uses noticeably less cpu cycles and reduces (when writing to a
+// network file) the required bandwidth by at least a factor of ten. Observed on MacOS.
 // That clearly makes up for the increased code complexity.
-#if defined(USE_STRINGSTREAM)
-#define STRINGSTREAM_DECL(_anyst, _outst)                 \
+//
+// Conversion of existing code is easy and straightforward, if the code already
+// uses a parameterized output destination, e.g. "outputStream st".
+//  - rename the formal parameter to any other name, e.g. out_st.
+//  - at a suitable place in your code, insert
+//      BUFFEREDSTEAM_DECL(buf_st, out_st)
+// This will provide all the declarations necessary. After that, all
+// buf_st->print() (and the like) calls will be directed to a bufferedStream object.
+// Once a block of output (a line or a small set of lines) is composed, insert
+//      BUFFEREDSTREAM_FLUSH(termstring)
+// to flush the bufferedStream to the final destination out_st. termstring is just
+// an arbitrary string (e.g. "\n") which is appended to the bufferedStream before
+// being written to out_st. Be aware that the last character written MUST be a '\n'.
+// Otherwise, buf_st->position() does not correspond to out_st->position() any longer.
+//      BUFFEREDSTREAM_FLUSH_LOCKED(termstring)
+// does the same thing, protected by the ttyLocker lock.
+//      BUFFEREDSTREAM_FLUSH_IF(termstring, remSize)
+// does a flush only if the remaining buffer space is less than remSize.
+//
+// To activate, #define USE_BUFFERED_STREAM before including this header.
+// If not activated, output will directly go to the originally used outputStream
+// with no additional overhead.
+//
+#if defined(USE_BUFFEREDSTREAM)
+// All necessary declarations to print via a bufferedStream
+// This macro must be placed before any other BUFFEREDSTREAM*
+// macro in the function.
+#define BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, _capa)       \
+    ResourceMark         _rm;                                 \
     /* _anyst  name of the stream as used in the code */  \
     /* _outst  stream where final output will go to   */  \
-    ResourceMark rm;                                      \
-    bufferedStream   _sstobj(4*K);                        \
+    /* _capa   allocated capacity of stream buffer    */      \
+    size_t           _nflush = 0;                             \
+    size_t     _nforcedflush = 0;                             \
+    size_t      _nsavedflush = 0;                             \
+    size_t     _nlockedflush = 0;                             \
+    size_t     _nflush_bytes = 0;                             \
+    size_t         _capacity = _capa;                         \
+    bufferedStream   _sstobj(_capa);                          \
     bufferedStream*  _sstbuf = &_sstobj;                  \
     outputStream*    _outbuf = _outst;                    \
     bufferedStream*  _anyst  = &_sstobj; /* any stream. Use this to just print - no buffer flush.  */
 
-#define STRINGSTREAM_FLUSH(termString)                    \
-    _sstbuf->print("%s", termString);                     \
+// Same as above, but with fixed buffer size.
+#define BUFFEREDSTREAM_DECL(_anyst, _outst)                   \
+    BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, 4*K);
+
+// Flush the buffer contents unconditionally.
+// No action if the buffer is empty.
+#define BUFFEREDSTREAM_FLUSH(_termString)                     \
+    if (((_termString) != NULL) && (strlen(_termString) > 0)){\
+      _sstbuf->print("%s", _termString);                      \
+    }                                                         \
+    if (_sstbuf != _outbuf) {                                 \
+      if (_sstbuf->size() != 0) {                             \
+        _nforcedflush++; _nflush_bytes += _sstbuf->size();    \
     _outbuf->print("%s", _sstbuf->as_string());           \
-    _sstbuf->reset();
-
-#define STRINGSTREAM_FLUSH_LOCKED(termString)             \
-    { ttyLocker ttyl;/* keep this output block together */\
-      STRINGSTREAM_FLUSH(termString)                      \
+        _sstbuf->reset();                                     \
+      }                                                       \
     }
+
+// Flush the buffer contents if the remaining capacity is
+// less than the given threshold.
+#define BUFFEREDSTREAM_FLUSH_IF(_termString, _remSize)        \
+    if (((_termString) != NULL) && (strlen(_termString) > 0)){\
+      _sstbuf->print("%s", _termString);                      \
+    }                                                         \
+    if (_sstbuf != _outbuf) {                                 \
+      if ((_capacity - _sstbuf->size()) < (size_t)(_remSize)){\
+        _nflush++; _nforcedflush--;                           \
+        BUFFEREDSTREAM_FLUSH("")                              \
+      } else {                                                \
+        _nsavedflush++;                                       \
+      }                                                       \
+    }
+
+// Flush the buffer contents if the remaining capacity is less
+// than the calculated threshold (256 bytes + capacity/16)
+// That should suffice for all reasonably sized output lines.
+#define BUFFEREDSTREAM_FLUSH_AUTO(_termString)                \
+    BUFFEREDSTREAM_FLUSH_IF(_termString, 256+(_capacity>>4))
+
+#define BUFFEREDSTREAM_FLUSH_LOCKED(_termString)              \
+    { ttyLocker ttyl;/* keep this output block together */    \
+      _nlockedflush++;                                        \
+      BUFFEREDSTREAM_FLUSH(_termString)                       \
+    }
+
+// #define BUFFEREDSTREAM_FLUSH_STAT()                           \
+//     if (_sstbuf != _outbuf) {                                 \
+//       _outbuf->print_cr("%ld flushes (buffer full), %ld forced, %ld locked, %ld bytes total, %ld flushes saved", _nflush, _nforcedflush, _nlockedflush, _nflush_bytes, _nsavedflush); \
+//    }
+
+#define BUFFEREDSTREAM_FLUSH_STAT()
 #else
-#define STRINGSTREAM_DECL(_anyst, _outst)                 \
+#define BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, _capa)       \
+    size_t       _capacity = _capa;                           \
     outputStream*  _outbuf = _outst;                      \
     outputStream*  _anyst  = _outst;   /* any stream. Use this to just print - no buffer flush.  */
 
-#define STRINGSTREAM_FLUSH(termString)                    \
-    _outbuf->print("%s", termString);
+#define BUFFEREDSTREAM_DECL(_anyst, _outst)                   \
+    BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, 4*K)
+
+#define BUFFEREDSTREAM_FLUSH(_termString)                     \
+    if (((_termString) != NULL) && (strlen(_termString) > 0)){\
+      _outbuf->print("%s", _termString);                      \
+    }
+
+#define BUFFEREDSTREAM_FLUSH_IF(_termString, _remSize)        \
+    BUFFEREDSTREAM_FLUSH(_termString)
+
+#define BUFFEREDSTREAM_FLUSH_AUTO(_termString)                \
+    BUFFEREDSTREAM_FLUSH(_termString)
+
+#define BUFFEREDSTREAM_FLUSH_LOCKED(_termString)              \
+    BUFFEREDSTREAM_FLUSH(_termString)
 
-#define STRINGSTREAM_FLUSH_LOCKED(termString)             \
-    _outbuf->print("%s", termString);
+#define BUFFEREDSTREAM_FLUSH_STAT()
 #endif
+#define HEX32_FORMAT  "0x%x"  // just a helper format string used below multiple times
 
 const char  blobTypeChar[] = {' ', 'C', 'N', 'I', 'X', 'Z', 'U', 'R', '?', 'D', 'T', 'E', 'S', 'A', 'M', 'B', 'L' };
 const char* blobTypeName[] = {"noType"
-                             ,     "nMethod (under construction)"
+                             ,     "nMethod (under construction), cannot be observed"
                              ,          "nMethod (active)"
                              ,               "nMethod (inactive)"
                              ,                    "nMethod (deopt)"
                              ,                         "nMethod (zombie)"
                              ,                              "nMethod (unloaded)"

@@ -138,11 +229,11 @@
                              };
 const char* compTypeName[] = { "none", "c1", "c2", "jvmci" };
 
 // Be prepared for ten different CodeHeap segments. Should be enough for a few years.
 const  unsigned int        nSizeDistElements = 31;  // logarithmic range growth, max size: 2**32
-const  unsigned int        maxTopSizeBlocks  = 50;
+const  unsigned int        maxTopSizeBlocks  = 100;
 const  unsigned int        tsbStopper        = 2 * maxTopSizeBlocks;
 const  unsigned int        maxHeaps          = 10;
 static unsigned int        nHeaps            = 0;
 static struct CodeHeapStat CodeHeapStatArray[maxHeaps];
 

@@ -155,11 +246,10 @@
 static bool         segment_granules      = false;
 static unsigned int nBlocks_t1            = 0;  // counting "in_use" nmethods only.
 static unsigned int nBlocks_t2            = 0;  // counting "in_use" nmethods only.
 static unsigned int nBlocks_alive         = 0;  // counting "not_used" and "not_entrant" nmethods only.
 static unsigned int nBlocks_dead          = 0;  // counting "zombie" and "unloaded" methods only.
-static unsigned int nBlocks_inconstr      = 0;  // counting "inconstruction" nmethods only. This is a transient state.
 static unsigned int nBlocks_unloaded      = 0;  // counting "unloaded" nmethods only. This is a transient state.
 static unsigned int nBlocks_stub          = 0;
 
 static struct FreeBlk*          FreeArray = NULL;
 static unsigned int      alloc_freeBlocks = 0;

@@ -226,11 +316,10 @@
     segment_granules      = CodeHeapStatArray[ix].segment_granules;
     nBlocks_t1            = CodeHeapStatArray[ix].nBlocks_t1;
     nBlocks_t2            = CodeHeapStatArray[ix].nBlocks_t2;
     nBlocks_alive         = CodeHeapStatArray[ix].nBlocks_alive;
     nBlocks_dead          = CodeHeapStatArray[ix].nBlocks_dead;
-    nBlocks_inconstr      = CodeHeapStatArray[ix].nBlocks_inconstr;
     nBlocks_unloaded      = CodeHeapStatArray[ix].nBlocks_unloaded;
     nBlocks_stub          = CodeHeapStatArray[ix].nBlocks_stub;
     FreeArray             = CodeHeapStatArray[ix].FreeArray;
     alloc_freeBlocks      = CodeHeapStatArray[ix].alloc_freeBlocks;
     TopSizeArray          = CodeHeapStatArray[ix].TopSizeArray;

@@ -249,11 +338,10 @@
     segment_granules      = false;
     nBlocks_t1            = 0;
     nBlocks_t2            = 0;
     nBlocks_alive         = 0;
     nBlocks_dead          = 0;
-    nBlocks_inconstr      = 0;
     nBlocks_unloaded      = 0;
     nBlocks_stub          = 0;
     FreeArray             = NULL;
     alloc_freeBlocks      = 0;
     TopSizeArray          = NULL;

@@ -276,11 +364,10 @@
     CodeHeapStatArray[ix].segment_granules      = segment_granules;
     CodeHeapStatArray[ix].nBlocks_t1            = nBlocks_t1;
     CodeHeapStatArray[ix].nBlocks_t2            = nBlocks_t2;
     CodeHeapStatArray[ix].nBlocks_alive         = nBlocks_alive;
     CodeHeapStatArray[ix].nBlocks_dead          = nBlocks_dead;
-    CodeHeapStatArray[ix].nBlocks_inconstr      = nBlocks_inconstr;
     CodeHeapStatArray[ix].nBlocks_unloaded      = nBlocks_unloaded;
     CodeHeapStatArray[ix].nBlocks_stub          = nBlocks_stub;
     CodeHeapStatArray[ix].FreeArray             = FreeArray;
     CodeHeapStatArray[ix].alloc_freeBlocks      = alloc_freeBlocks;
     CodeHeapStatArray[ix].TopSizeArray          = TopSizeArray;

@@ -403,10 +490,15 @@
   }
 }
 
 void CodeHeapState::discard_TopSizeArray(outputStream* out) {
   if (TopSizeArray != NULL) {
+    for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
+      if (TopSizeArray[i].blob_name != NULL) {
+        os::free((void*)TopSizeArray[i].blob_name);
+      }
+    }
     delete[] TopSizeArray;
     TopSizeArray        = NULL;
     alloc_topSizeBlocks = 0;
     used_topSizeBlocks  = 0;
   }

@@ -457,14 +549,14 @@
   unsigned long total_iterations = 0;
 
   bool  done             = false;
   const int min_granules = 256;
   const int max_granules = 512*K; // limits analyzable CodeHeap (with segment_granules) to 32M..128M
-                                  // results in StatArray size of 20M (= max_granules * 40 Bytes per element)
+                                  // results in StatArray size of 24M (= max_granules * 48 Bytes per element)
                                   // For a 1GB CodeHeap, the granule size must be at least 2kB to not violate the max_granles limit.
   const char* heapName   = get_heapName(heap);
-  STRINGSTREAM_DECL(ast, out)
+  BUFFEREDSTREAM_DECL(ast, out)
 
   if (!initialization_complete) {
     memset(CodeHeapStatArray, 0, sizeof(CodeHeapStatArray));
     initialization_complete = true;
 

@@ -476,11 +568,11 @@
                   "   \n"
                   "   This function is designed and provided for support engineers\n"
                   "   to help them understand and solve issues in customer systems.\n"
                   "   It is not intended for use and interpretation by other persons.\n"
                   "   \n");
-    STRINGSTREAM_FLUSH("")
+    BUFFEREDSTREAM_FLUSH("")
   }
   get_HeapStatGlobals(out, heapName);
 
 
   // Since we are (and must be) analyzing the CodeHeap contents under the CodeCache_lock,

@@ -492,11 +584,17 @@
   seg_size             = heap->segment_size();
   log2_seg_size        = seg_size == 0 ? 0 : exact_log2(seg_size);  // This is a global static value.
 
   if (seg_size == 0) {
     printBox(ast, '-', "Heap not fully initialized yet, segment size is zero for segment ", heapName);
-    STRINGSTREAM_FLUSH("")
+    BUFFEREDSTREAM_FLUSH("")
+    return;
+  }
+
+  if (!holding_required_locks()) {
+    printBox(ast, '-', "Must be at safepoint or hold Compile_lock and CodeCache_lock when calling aggregate function for ", heapName);
+    BUFFEREDSTREAM_FLUSH("")
     return;
   }
 
   // Calculate granularity of analysis (and output).
   //   The CodeHeap is managed (allocated) in segments (units) of CodeCacheSegmentSize.

@@ -549,20 +647,19 @@
                 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));
   ast->print_cr("   CodeHeap allocation segment size is " SIZE_FORMAT " bytes. This is the smallest possible granularity.", seg_size);
   ast->print_cr("   CodeHeap (committed part) is mapped to " SIZE_FORMAT " granules of size " SIZE_FORMAT " bytes.", granules, granularity);
   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);
   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));
-  STRINGSTREAM_FLUSH("\n")
+  BUFFEREDSTREAM_FLUSH("\n")
 
 
   while (!done) {
     //---<  reset counters with every aggregation  >---
     nBlocks_t1       = 0;
     nBlocks_t2       = 0;
     nBlocks_alive    = 0;
     nBlocks_dead     = 0;
-    nBlocks_inconstr = 0;
     nBlocks_unloaded = 0;
     nBlocks_stub     = 0;
 
     nBlocks_free     = 0;
     nBlocks_used     = 0;

@@ -592,11 +689,10 @@
     size_t       t2Space       = 0;
     size_t       aliveSpace    = 0;
     size_t       disconnSpace  = 0;
     size_t       notentrSpace  = 0;
     size_t       deadSpace     = 0;
-    size_t       inconstrSpace = 0;
     size_t       unloadedSpace = 0;
     size_t       stubSpace     = 0;
     size_t       freeSpace     = 0;
     size_t       maxFreeSize   = 0;
     HeapBlock*   maxFreeBlock  = NULL;

@@ -638,11 +734,11 @@
       }
       if (ix_beg   >  ix_end) {
         insane = true; ast->print_cr("Sanity check: end index (%d) lower than begin index (%d)", ix_end, ix_beg);
       }
       if (insane) {
-        STRINGSTREAM_FLUSH("")
+        BUFFEREDSTREAM_FLUSH("")
         continue;
       }
 
       if (h->free()) {
         nBlocks_free++;

@@ -654,28 +750,42 @@
       } else {
         update_SizeDistArray(out, hb_len);
         nBlocks_used++;
         usedSpace    += hb_bytelen;
         CodeBlob* cb  = (CodeBlob*)heap->find_start(h);
-        if (cb != NULL) {
-          cbType = get_cbType(cb);
-          if (cb->is_nmethod()) {
-            compile_id = ((nmethod*)cb)->compile_id();
-            comp_lvl   = (CompLevel)((nmethod*)cb)->comp_level();
-            if (((nmethod*)cb)->is_compiled_by_c1()) {
+        cbType = get_cbType(cb);  // Will check for cb == NULL and other safety things.
+        if (cbType != noType) {
+          const char* blob_name  = os::strdup(cb->name());
+          unsigned int nm_size   = 0;
+          int temperature        = 0;
+          nmethod*  nm = cb->as_nmethod_or_null();
+          if (nm != NULL) { // no is_readable check required, nm = (nmethod*)cb.
+            ResourceMark rm;
+            Method* method = nm->method();
+            if (nm->is_in_use()) {
+              blob_name = os::strdup(method->name_and_sig_as_C_string());
+            }
+            if (nm->is_not_entrant()) {
+              blob_name = os::strdup(method->name_and_sig_as_C_string());
+            }
+
+            nm_size    = nm->total_size();
+            compile_id = nm->compile_id();
+            comp_lvl   = (CompLevel)(nm->comp_level());
+            if (nm->is_compiled_by_c1()) {
               cType = c1;
             }
-            if (((nmethod*)cb)->is_compiled_by_c2()) {
+            if (nm->is_compiled_by_c2()) {
               cType = c2;
             }
-            if (((nmethod*)cb)->is_compiled_by_jvmci()) {
+            if (nm->is_compiled_by_jvmci()) {
               cType = jvmci;
             }
             switch (cbType) {
               case nMethod_inuse: { // only for executable methods!!!
                 // space for these cbs is accounted for later.
-                int temperature = ((nmethod*)cb)->hotness_counter();
+                temperature = nm->hotness_counter();
                 hotnessAccumulator += temperature;
                 n_methods++;
                 maxTemp = (temperature > maxTemp) ? temperature : maxTemp;
                 minTemp = (temperature < minTemp) ? temperature : minTemp;
                 break;

@@ -698,14 +808,10 @@
                 break;
               case nMethod_dead:
                 nBlocks_dead++;
                 deadSpace      += hb_bytelen;
                 break;
-              case nMethod_inconstruction:
-                nBlocks_inconstr++;
-                inconstrSpace  += hb_bytelen;
-                break;
               default:
                 break;
             }
           }
 

@@ -713,32 +819,40 @@
           //---<  register block in TopSizeArray  >---
           //------------------------------------------
           if (alloc_topSizeBlocks > 0) {
             if (used_topSizeBlocks == 0) {
               TopSizeArray[0].start    = h;
+              TopSizeArray[0].blob_name   = blob_name;
               TopSizeArray[0].len      = hb_len;
               TopSizeArray[0].index    = tsbStopper;
+              TopSizeArray[0].nm_size     = nm_size;
+              TopSizeArray[0].temperature = temperature;
               TopSizeArray[0].compiler = cType;
               TopSizeArray[0].level    = comp_lvl;
               TopSizeArray[0].type     = cbType;
               currMax    = hb_len;
               currMin    = hb_len;
               currMin_ix = 0;
               used_topSizeBlocks++;
+              blob_name  = NULL; // indicate blob_name was consumed
             // This check roughly cuts 5000 iterations (JVM98, mixed, dbg, termination stats):
             } else if ((used_topSizeBlocks < alloc_topSizeBlocks) && (hb_len < currMin)) {
               //---<  all blocks in list are larger, but there is room left in array  >---
               TopSizeArray[currMin_ix].index = used_topSizeBlocks;
               TopSizeArray[used_topSizeBlocks].start    = h;
+              TopSizeArray[used_topSizeBlocks].blob_name   = blob_name;
               TopSizeArray[used_topSizeBlocks].len      = hb_len;
               TopSizeArray[used_topSizeBlocks].index    = tsbStopper;
+              TopSizeArray[used_topSizeBlocks].nm_size     = nm_size;
+              TopSizeArray[used_topSizeBlocks].temperature = temperature;
               TopSizeArray[used_topSizeBlocks].compiler = cType;
               TopSizeArray[used_topSizeBlocks].level    = comp_lvl;
               TopSizeArray[used_topSizeBlocks].type     = cbType;
               currMin    = hb_len;
               currMin_ix = used_topSizeBlocks;
               used_topSizeBlocks++;
+              blob_name  = NULL; // indicate blob_name was consumed
             } else {
               // This check cuts total_iterations by a factor of 6 (JVM98, mixed, dbg, termination stats):
               //   We don't need to search the list if we know beforehand that the current block size is
               //   smaller than the currently recorded minimum and there is no free entry left in the list.
               if (!((used_topSizeBlocks == alloc_topSizeBlocks) && (hb_len <= currMin))) {

@@ -766,18 +880,28 @@
                         assert(TopSizeArray[i].len == currMin, "sort error");
                         currMin_ix = used_topSizeBlocks;
                       }
                       memcpy((void*)&TopSizeArray[used_topSizeBlocks], (void*)&TopSizeArray[i], sizeof(TopSizeBlk));
                       TopSizeArray[i].start    = h;
+                      TopSizeArray[i].blob_name   = blob_name;
                       TopSizeArray[i].len      = hb_len;
                       TopSizeArray[i].index    = used_topSizeBlocks;
+                      TopSizeArray[i].nm_size     = nm_size;
+                      TopSizeArray[i].temperature = temperature;
                       TopSizeArray[i].compiler = cType;
                       TopSizeArray[i].level    = comp_lvl;
                       TopSizeArray[i].type     = cbType;
                       used_topSizeBlocks++;
+                      blob_name  = NULL; // indicate blob_name was consumed
                     } else { // no room for new entries, current block replaces entry for smallest block
                       //---<  Find last entry (entry for smallest remembered block)  >---
+                      // We either want to insert right before the smallest entry, which is when <i>
+                      // indexes the smallest entry. We then just overwrite the smallest entry.
+                      // What's more likely:
+                      // We want to insert somewhere in the list. The smallest entry (@<j>) then falls off the cliff.
+                      // The element at the insert point <i> takes it's slot. The second-smallest entry now becomes smallest.
+                      // Data of the current block is filled in at index <i>.
                       unsigned int      j  = i;
                       unsigned int prev_j  = tsbStopper;
                       unsigned int limit_j = 0;
                       while (TopSizeArray[j].index != tsbStopper) {
                         if (limit_j++ >= alloc_topSizeBlocks) {

@@ -789,35 +913,45 @@
                         total_iterations++;
                         prev_j = j;
                         j      = TopSizeArray[j].index;
                       }
                       if (!insane) {
+                        if (TopSizeArray[j].blob_name != NULL) {
+                          os::free((void*)TopSizeArray[j].blob_name);
+                        }
                         if (prev_j == tsbStopper) {
                           //---<  Above while loop did not iterate, we already are the min entry  >---
                           //---<  We have to just replace the smallest entry                      >---
                           currMin    = hb_len;
                           currMin_ix = j;
                           TopSizeArray[j].start    = h;
+                          TopSizeArray[j].blob_name   = blob_name;
                           TopSizeArray[j].len      = hb_len;
                           TopSizeArray[j].index    = tsbStopper; // already set!!
+                          TopSizeArray[i].nm_size     = nm_size;
+                          TopSizeArray[i].temperature = temperature;
                           TopSizeArray[j].compiler = cType;
                           TopSizeArray[j].level    = comp_lvl;
                           TopSizeArray[j].type     = cbType;
                         } else {
                           //---<  second-smallest entry is now smallest  >---
                           TopSizeArray[prev_j].index = tsbStopper;
                           currMin    = TopSizeArray[prev_j].len;
                           currMin_ix = prev_j;
-                          //---<  smallest entry gets overwritten  >---
+                          //---<  previously smallest entry gets overwritten  >---
                           memcpy((void*)&TopSizeArray[j], (void*)&TopSizeArray[i], sizeof(TopSizeBlk));
                           TopSizeArray[i].start    = h;
+                          TopSizeArray[i].blob_name   = blob_name;
                           TopSizeArray[i].len      = hb_len;
                           TopSizeArray[i].index    = j;
+                          TopSizeArray[i].nm_size     = nm_size;
+                          TopSizeArray[i].temperature = temperature;
                           TopSizeArray[i].compiler = cType;
                           TopSizeArray[i].level    = comp_lvl;
                           TopSizeArray[i].type     = cbType;
                         }
+                        blob_name  = NULL; // indicate blob_name was consumed
                       } // insane
                     }
                     break;
                   }
                   prev_i = i;

@@ -828,10 +962,14 @@
                   discard_TopSizeArray(out);
                 }
               }
             }
           }
+          if (blob_name != NULL) {
+            os::free((void*)blob_name);
+            blob_name = NULL;
+          }
           //----------------------------------------------
           //---<  END register block in TopSizeArray  >---
           //----------------------------------------------
         } else {
           nBlocks_zomb++;

@@ -856,11 +994,10 @@
                 StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;
               }
               StatArray[ix_beg].level     = comp_lvl;
               StatArray[ix_beg].compiler  = cType;
               break;
-            case nMethod_inconstruction: // let's count "in construction" nmethods here.
             case nMethod_alive:
               StatArray[ix_beg].tx_count++;
               StatArray[ix_beg].tx_space += (unsigned short)hb_len;
               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
               StatArray[ix_beg].level     = comp_lvl;

@@ -913,11 +1050,10 @@
               StatArray[ix_beg].level     = comp_lvl;
               StatArray[ix_beg].compiler  = cType;
               StatArray[ix_end].level     = comp_lvl;
               StatArray[ix_end].compiler  = cType;
               break;
-            case nMethod_inconstruction: // let's count "in construction" nmethods here.
             case nMethod_alive:
               StatArray[ix_beg].tx_count++;
               StatArray[ix_beg].tx_space += (unsigned short)beg_space;
               StatArray[ix_beg].tx_age    = StatArray[ix_beg].tx_age < compile_id ? compile_id : StatArray[ix_beg].tx_age;
 

@@ -961,11 +1097,10 @@
                   StatArray[ix].t2_age    = StatArray[ix].t2_age < compile_id ? compile_id : StatArray[ix].t2_age;
                 }
                 StatArray[ix].level     = comp_lvl;
                 StatArray[ix].compiler  = cType;
                 break;
-              case nMethod_inconstruction: // let's count "in construction" nmethods here.
               case nMethod_alive:
                 StatArray[ix].tx_count++;
                 StatArray[ix].tx_space += (unsigned short)(granule_size>>log2_seg_size);
                 StatArray[ix].tx_age    = StatArray[ix].tx_age < compile_id ? compile_id : StatArray[ix].tx_age;
                 StatArray[ix].level     = comp_lvl;

@@ -998,11 +1133,10 @@
       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);
       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);
       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);
       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);
       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);
-      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);
       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);
       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);
       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);
       ast->print_cr("ZombieBlocks     = %8d. These are HeapBlocks which could not be identified as CodeBlobs.", nBlocks_zomb);
       ast->cr();

@@ -1027,13 +1161,14 @@
         ast->print_cr("max. hotness = %6d", maxTemp);
       } else {
         avgTemp = 0;
         ast->print_cr("No hotness data available");
       }
-      STRINGSTREAM_FLUSH("\n")
+      BUFFEREDSTREAM_FLUSH("\n")
 
       // This loop is intentionally printing directly to "out".
+      // It should not print anything, anyway.
       out->print("Verifying collected data...");
       size_t granule_segs = granule_size>>log2_seg_size;
       for (unsigned int ix = 0; ix < granules; ix++) {
         if (StatArray[ix].t1_count   > granule_segs) {
           out->print_cr("t1_count[%d]   = %d", ix, StatArray[ix].t1_count);

@@ -1073,10 +1208,11 @@
           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);
         }
       }
 
       // This loop is intentionally printing directly to "out".
+      // It should not print anything, anyway.
       if (used_topSizeBlocks > 0) {
         unsigned int j = 0;
         if (TopSizeArray[0].len != currMax) {
           out->print_cr("currMax(%d) differs from TopSizeArray[0].len(%d)", currMax, TopSizeArray[0].len);
         }

@@ -1107,11 +1243,11 @@
     printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (free blocks) for segment ", heapName);
     ast->print_cr("   The aggregate step collects information about all free blocks in CodeHeap.\n"
                   "   Subsequent print functions create their output based on this snapshot.\n");
     ast->print_cr("   Free space in %s is distributed over %d free blocks.", heapName, nBlocks_free);
     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);
-    STRINGSTREAM_FLUSH("\n")
+    BUFFEREDSTREAM_FLUSH("\n")
 
     //----------------------------------------
     //--  Prepare the FreeArray of FreeBlks --
     //----------------------------------------
 

@@ -1143,11 +1279,11 @@
       ix++;
     }
     if (ix != alloc_freeBlocks) {
       ast->print_cr("Free block count mismatch. Expected %d free blocks, but found %d.", alloc_freeBlocks, ix);
       ast->print_cr("I will update the counter and retry data collection");
-      STRINGSTREAM_FLUSH("\n")
+      BUFFEREDSTREAM_FLUSH("\n")
       nBlocks_free = ix;
       continue;
     }
     done = true;
   }

@@ -1157,27 +1293,28 @@
       printBox(ast, '-', "no free blocks found in ", heapName);
     } else if (!done) {
       ast->print_cr("Free block count mismatch could not be resolved.");
       ast->print_cr("Try to run \"aggregate\" function to update counters");
     }
-    STRINGSTREAM_FLUSH("")
+    BUFFEREDSTREAM_FLUSH("")
 
     //---< discard old array and update global values  >---
     discard_FreeArray(out);
     set_HeapStatGlobals(out, heapName);
     return;
   }
 
   //---<  calculate and fill remaining fields  >---
   if (FreeArray != NULL) {
     // This loop is intentionally printing directly to "out".
+    // It should not print anything, anyway.
     for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
       size_t lenSum = 0;
       FreeArray[ix].gap = (unsigned int)((address)FreeArray[ix+1].start - ((address)FreeArray[ix].start + FreeArray[ix].len));
       for (HeapBlock *h = heap->next_block(FreeArray[ix].start); (h != NULL) && (h != FreeArray[ix+1].start); h = heap->next_block(h)) {
         CodeBlob *cb  = (CodeBlob*)(heap->find_start(h));
-        if ((cb != NULL) && !cb->is_nmethod()) {
+        if ((cb != NULL) && !cb->is_nmethod()) { // checks equivalent to those in get_cbType()
           FreeArray[ix].stubs_in_gap = true;
         }
         FreeArray[ix].n_gapBlocks++;
         lenSum += h->length()<<log2_seg_size;
         if (((address)h < ((address)FreeArray[ix].start+FreeArray[ix].len)) || (h >= FreeArray[ix+1].start)) {

@@ -1190,11 +1327,11 @@
     }
   }
   set_HeapStatGlobals(out, heapName);
 
   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);
-  STRINGSTREAM_FLUSH("\n")
+  BUFFEREDSTREAM_FLUSH("\n")
 }
 
 
 void CodeHeapState::print_usedSpace(outputStream* out, CodeHeap* heap) {
   if (!initialization_complete) {

@@ -1205,11 +1342,11 @@
   get_HeapStatGlobals(out, heapName);
 
   if ((StatArray == NULL) || (TopSizeArray == NULL) || (used_topSizeBlocks == 0)) {
     return;
   }
-  STRINGSTREAM_DECL(ast, out)
+  BUFFEREDSTREAM_DECL(ast, out)
 
   {
     printBox(ast, '=', "U S E D   S P A C E   S T A T I S T I C S   for ", heapName);
     ast->print_cr("Note: The Top%d list of the largest used blocks associates method names\n"
                   "      and other identifying information with the block size data.\n"

@@ -1217,11 +1354,11 @@
                   "      Method names are dynamically retrieved from the code cache at print time.\n"
                   "      Due to the living nature of the code cache and because the CodeCache_lock\n"
                   "      is not continuously held, the displayed name might be wrong or no name\n"
                   "      might be found at all. The likelihood for that to happen increases\n"
                   "      over time passed between analysis and print step.\n", used_topSizeBlocks);
-    STRINGSTREAM_FLUSH_LOCKED("\n")
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n")
   }
 
   //----------------------------
   //--  Print Top Used Blocks --
   //----------------------------

@@ -1236,23 +1373,26 @@
     ast->fill_to(56);
     ast->print("%9s", "compiler");
     ast->fill_to(66);
     ast->print_cr("%6s", "method");
     ast->print_cr("%18s %13s %17s %4s %9s  %5s %s",      "Addr(module)      ", "offset", "size", "type", " type lvl", " temp", "Name");
-    STRINGSTREAM_FLUSH_LOCKED("")
+    BUFFEREDSTREAM_FLUSH_LOCKED("")
 
     //---<  print Top Ten Used Blocks  >---
     if (used_topSizeBlocks > 0) {
       unsigned int printed_topSizeBlocks = 0;
       for (unsigned int i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
         printed_topSizeBlocks++;
-        CodeBlob*   this_blob = (CodeBlob*)(heap->find_start(TopSizeArray[i].start));
-        nmethod*           nm = NULL;
-        const char* blob_name = "unnamed blob";
+        if (TopSizeArray[i].blob_name == NULL) {
+          TopSizeArray[i].blob_name = os::strdup("unnamed blob or blob name unavailable");
+        }
+        // heap->find_start() is safe. Only works on _segmap.
+        // Returns NULL or void*. Returned CodeBlob may be uninitialized.
+        HeapBlock* heapBlock = TopSizeArray[i].start;
+        CodeBlob*  this_blob = (CodeBlob*)(heap->find_start(heapBlock));
         if (this_blob != NULL) {
-          blob_name = this_blob->name();
-          nm        = this_blob->as_nmethod_or_null();
+          //---<  access these fields only if we own the CodeCache_lock  >---
           //---<  blob address  >---
           ast->print(INTPTR_FORMAT, p2i(this_blob));
           ast->fill_to(19);
           //---<  blob offset from CodeHeap begin  >---
           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));

@@ -1264,54 +1404,51 @@
           //---<  block offset from CodeHeap begin  >---
           ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)TopSizeArray[i].start-low_bound));
           ast->fill_to(33);
         }
 
-
         //---<  print size, name, and signature (for nMethods)  >---
-        if ((nm != NULL) && (nm->method() != NULL)) {
-          ResourceMark rm;
+        bool is_nmethod = TopSizeArray[i].nm_size > 0;
+        if (is_nmethod) {
           //---<  nMethod size in hex  >---
-          unsigned int total_size = nm->total_size();
-          ast->print(PTR32_FORMAT, total_size);
-          ast->print("(" SIZE_FORMAT_W(4) "K)", total_size/K);
+          ast->print(PTR32_FORMAT, TopSizeArray[i].nm_size);
+          ast->print("(" SIZE_FORMAT_W(4) "K)", TopSizeArray[i].nm_size/K);
           ast->fill_to(51);
           ast->print("  %c", blobTypeChar[TopSizeArray[i].type]);
           //---<  compiler information  >---
           ast->fill_to(56);
           ast->print("%5s %3d", compTypeName[TopSizeArray[i].compiler], TopSizeArray[i].level);
           //---<  method temperature  >---
           ast->fill_to(67);
-          ast->print("%5d", nm->hotness_counter());
+          ast->print("%5d", TopSizeArray[i].temperature);
           //---<  name and signature  >---
           ast->fill_to(67+6);
-          if (nm->is_in_use())        {blob_name = nm->method()->name_and_sig_as_C_string(); }
-          if (nm->is_not_entrant())   {blob_name = nm->method()->name_and_sig_as_C_string(); }
-          if (nm->is_not_installed()) {ast->print("%s", " not (yet) installed method "); }
-          if (nm->is_zombie())        {ast->print("%s", " zombie method "); }
-          ast->print("%s", blob_name);
+          if (TopSizeArray[i].type == nMethod_dead) {
+            ast->print(" zombie method ");
+          }
+          ast->print("%s", TopSizeArray[i].blob_name);
         } else {
           //---<  block size in hex  >---
           ast->print(PTR32_FORMAT, (unsigned int)(TopSizeArray[i].len<<log2_seg_size));
           ast->print("(" SIZE_FORMAT_W(4) "K)", (TopSizeArray[i].len<<log2_seg_size)/K);
           //---<  no compiler information  >---
           ast->fill_to(56);
           //---<  name and signature  >---
           ast->fill_to(67+6);
-          ast->print("%s", blob_name);
+          ast->print("%s", TopSizeArray[i].blob_name);
         }
-        STRINGSTREAM_FLUSH_LOCKED("\n")
+        ast->cr();
+        BUFFEREDSTREAM_FLUSH_AUTO("")
       }
       if (used_topSizeBlocks != printed_topSizeBlocks) {
         ast->print_cr("used blocks: %d, printed blocks: %d", used_topSizeBlocks, printed_topSizeBlocks);
-        STRINGSTREAM_FLUSH("")
         for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
           ast->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
-          STRINGSTREAM_FLUSH("")
+          BUFFEREDSTREAM_FLUSH_AUTO("")
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("\n\n")
+      BUFFEREDSTREAM_FLUSH("\n\n")
     }
   }
 
   //-----------------------------
   //--  Print Usage Histogram  --

@@ -1332,11 +1469,11 @@
       ast->print_cr("Note: The histogram indicates how many blocks (as a percentage\n"
                     "      of all blocks) have a size in the given range.\n"
                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
       ast->print_cr("total number of all blocks: %7ld\n", total_count);
-      STRINGSTREAM_FLUSH_LOCKED("")
+      BUFFEREDSTREAM_FLUSH_LOCKED("")
 
       ast->print_cr("[Size Range)------avg.-size-+----count-+");
       for (unsigned int i = 0; i < nSizeDistElements; i++) {
         if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
           ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "

@@ -1361,21 +1498,22 @@
         unsigned int percent = pctFactor*SizeDistributionArray[i].count/total_count;
         for (unsigned int j = 1; j <= percent; j++) {
           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
         }
         ast->cr();
+        BUFFEREDSTREAM_FLUSH_AUTO("")
       }
-      ast->print_cr("----------------------------+----------+\n\n");
-      STRINGSTREAM_FLUSH_LOCKED("\n")
+      ast->print_cr("----------------------------+----------+");
+      BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
 
       printBox(ast, '-', "Contribution per size range to total size for ", heapName);
       ast->print_cr("Note: The histogram indicates how much space (as a percentage of all\n"
                     "      occupied space) is used by the blocks in the given size range.\n"
                     "      %ld characters are printed per percentage point.\n", pctFactor/100);
       ast->print_cr("total size   of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
       ast->print_cr("total number of all blocks: %7ld\n", total_count);
-      STRINGSTREAM_FLUSH_LOCKED("")
+      BUFFEREDSTREAM_FLUSH_LOCKED("")
 
       ast->print_cr("[Size Range)------avg.-size-+----count-+");
       for (unsigned int i = 0; i < nSizeDistElements; i++) {
         if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
           ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "

@@ -1400,13 +1538,14 @@
         unsigned int percent = pctFactor*(unsigned long)SizeDistributionArray[i].lenSum/total_size;
         for (unsigned int j = 1; j <= percent; j++) {
           ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
         }
         ast->cr();
+        BUFFEREDSTREAM_FLUSH_AUTO("")
       }
       ast->print_cr("----------------------------+----------+");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
+      BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
     }
   }
 }
 
 

@@ -1419,25 +1558,24 @@
   get_HeapStatGlobals(out, heapName);
 
   if ((StatArray == NULL) || (FreeArray == NULL) || (alloc_granules == 0)) {
     return;
   }
-  STRINGSTREAM_DECL(ast, out)
+  BUFFEREDSTREAM_DECL(ast, out)
 
   {
     printBox(ast, '=', "F R E E   S P A C E   S T A T I S T I C S   for ", heapName);
     ast->print_cr("Note: in this context, a gap is the occupied space between two free blocks.\n"
                   "      Those gaps are of interest if there is a chance that they become\n"
                   "      unoccupied, e.g. by class unloading. Then, the two adjacent free\n"
                   "      blocks, together with the now unoccupied space, form a new, large\n"
                   "      free block.");
-    STRINGSTREAM_FLUSH_LOCKED("\n")
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n")
   }
 
   {
     printBox(ast, '-', "List of all Free Blocks in ", heapName);
-    STRINGSTREAM_FLUSH_LOCKED("")
 
     unsigned int ix = 0;
     for (ix = 0; ix < alloc_freeBlocks-1; ix++) {
       ast->print(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT ",", p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
       ast->fill_to(38);

@@ -1445,14 +1583,15 @@
       ast->fill_to(71);
       ast->print("block count: %6d", FreeArray[ix].n_gapBlocks);
       if (FreeArray[ix].stubs_in_gap) {
         ast->print(" !! permanent gap, contains stubs and/or blobs !!");
       }
-      STRINGSTREAM_FLUSH_LOCKED("\n")
+      ast->cr();
+      BUFFEREDSTREAM_FLUSH_AUTO("")
     }
     ast->print_cr(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT, p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
-    STRINGSTREAM_FLUSH_LOCKED("\n\n")
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
   }
 
 
   //-----------------------------------------
   //--  Find and Print Top Ten Free Blocks --

@@ -1492,11 +1631,11 @@
           currMax10 = currSize;
         }
       }
     }
   }
-  STRINGSTREAM_FLUSH_LOCKED("")
+  BUFFEREDSTREAM_FLUSH_AUTO("")
 
   {
     printBox(ast, '-', "Top Ten Free Blocks in ", heapName);
 
     //---<  print Top Ten Free Blocks  >---

@@ -1509,13 +1648,14 @@
         ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTen[iy]->gap);
         ast->fill_to(63);
         ast->print("#blocks (in gap) %d", FreeTopTen[iy]->n_gapBlocks);
       }
       ast->cr();
+      BUFFEREDSTREAM_FLUSH_AUTO("")
     }
-    STRINGSTREAM_FLUSH_LOCKED("\n\n")
   }
+  BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
 
 
   //--------------------------------------------------------
   //--  Find and Print Top Ten Free-Occupied-Free Triples --
   //--------------------------------------------------------

@@ -1556,11 +1696,11 @@
           currMax10 = lenTriple;
         }
       }
     }
   }
-  STRINGSTREAM_FLUSH_LOCKED("")
+  BUFFEREDSTREAM_FLUSH_AUTO("")
 
   {
     printBox(ast, '-', "Top Ten Free-Occupied-Free Triples in ", heapName);
     ast->print_cr("  Use this information to judge how likely it is that a large(r) free block\n"
                   "  might get created by code cache sweeping.\n"

@@ -1574,13 +1714,14 @@
       ast->fill_to(39);
       ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTenTriple[iy]->gap);
       ast->fill_to(63);
       ast->print("#blocks (in gap) %d", FreeTopTenTriple[iy]->n_gapBlocks);
       ast->cr();
+      BUFFEREDSTREAM_FLUSH_AUTO("")
     }
-    STRINGSTREAM_FLUSH_LOCKED("\n\n")
   }
+  BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
 }
 
 
 void CodeHeapState::print_count(outputStream* out, CodeHeap* heap) {
   if (!initialization_complete) {

@@ -1591,11 +1732,11 @@
   get_HeapStatGlobals(out, heapName);
 
   if ((StatArray == NULL) || (alloc_granules == 0)) {
     return;
   }
-  STRINGSTREAM_DECL(ast, out)
+  BUFFEREDSTREAM_DECL(ast, out)
 
   unsigned int granules_per_line = 32;
   char*        low_bound         = heap->low_boundary();
 
   {

@@ -1607,147 +1748,139 @@
                     "  As a result, each granule contains exactly one block (or a part of one block)\n"
                     "  or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
                     "  Occupied granules show their BlobType character, see legend.\n");
       print_blobType_legend(ast);
     }
-    STRINGSTREAM_FLUSH_LOCKED("")
+    BUFFEREDSTREAM_FLUSH_LOCKED("")
   }
 
   {
     if (segment_granules) {
       printBox(ast, '-', "Total (all types) count for granule size == segment size", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         print_blobType_single(ast, StatArray[ix].type);
       }
     } else {
       printBox(ast, '-', "Total (all tiers) count, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         unsigned int count = StatArray[ix].t1_count   + StatArray[ix].t2_count   + StatArray[ix].tx_count
                            + StatArray[ix].stub_count + StatArray[ix].dead_count;
         print_count_single(ast, count);
       }
     }
-    STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+    BUFFEREDSTREAM_FLUSH_LOCKED("|\n\n\n")
   }
 
   {
     if (nBlocks_t1 > 0) {
       printBox(ast, '-', "Tier1 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].t1_count > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_count_single(ast, StatArray[ix].t1_count);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier1 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_t2 > 0) {
       printBox(ast, '-', "Tier2 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].t2_count > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_count_single(ast, StatArray[ix].t2_count);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier2 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_alive > 0) {
       printBox(ast, '-', "not_used/not_entrant/not_installed nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].tx_count > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_count_single(ast, StatArray[ix].tx_count);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No not_used/not_entrant nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_stub > 0) {
       printBox(ast, '-', "Stub & Blob count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].stub_count > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_count_single(ast, StatArray[ix].stub_count);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Stubs and Blobs found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_dead > 0) {
       printBox(ast, '-', "Dead nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].dead_count > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_count_single(ast, StatArray[ix].dead_count);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No dead nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (!segment_granules) { // Prevent totally redundant printouts
       printBox(ast, '-', "Count by tier (combined, no dead blocks): <#t1>:<#t2>:<#s>, 0x0..0xf. '*' indicates >= 16 blocks", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 24;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
 

@@ -1760,11 +1893,11 @@
         } else {
           print_count_single(ast, StatArray[ix].stub_count);
         }
         ast->print(" ");
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      BUFFEREDSTREAM_FLUSH_LOCKED("|\n\n\n")
     }
   }
 }
 
 

@@ -1777,11 +1910,11 @@
   get_HeapStatGlobals(out, heapName);
 
   if ((StatArray == NULL) || (alloc_granules == 0)) {
     return;
   }
-  STRINGSTREAM_DECL(ast, out)
+  BUFFEREDSTREAM_DECL(ast, out)
 
   unsigned int granules_per_line = 32;
   char*        low_bound         = heap->low_boundary();
 
   {

@@ -1796,78 +1929,74 @@
       print_blobType_legend(ast);
     } else {
       ast->print_cr("  These digits represent a fill percentage range (see legend).\n");
       print_space_legend(ast);
     }
-    STRINGSTREAM_FLUSH_LOCKED("")
+    BUFFEREDSTREAM_FLUSH_LOCKED("")
   }
 
   {
     if (segment_granules) {
       printBox(ast, '-', "Total (all types) space consumption for granule size == segment size", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         print_blobType_single(ast, StatArray[ix].type);
       }
     } else {
       printBox(ast, '-', "Total (all types) space consumption. ' ' indicates empty, '*' indicates full.", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         unsigned int space    = StatArray[ix].t1_space   + StatArray[ix].t2_space  + StatArray[ix].tx_space
                               + StatArray[ix].stub_space + StatArray[ix].dead_space;
         print_space_single(ast, space);
       }
     }
-    STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+    BUFFEREDSTREAM_FLUSH_LOCKED("|\n\n\n")
   }
 
   {
     if (nBlocks_t1 > 0) {
       printBox(ast, '-', "Tier1 space consumption. ' ' indicates empty, '*' indicates full", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].t1_space > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_space_single(ast, StatArray[ix].t1_space);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier1 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_t2 > 0) {
       printBox(ast, '-', "Tier2 space consumption. ' ' indicates empty, '*' indicates full", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].t2_space > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_space_single(ast, StatArray[ix].t2_space);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier2 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_alive > 0) {
       printBox(ast, '-', "not_used/not_entrant/not_installed space consumption. ' ' indicates empty, '*' indicates full", NULL);

@@ -1879,59 +2008,56 @@
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_space_single(ast, StatArray[ix].tx_space);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier2 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_stub > 0) {
       printBox(ast, '-', "Stub and Blob space consumption. ' ' indicates empty, '*' indicates full", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         if (segment_granules && StatArray[ix].stub_space > 0) {
           print_blobType_single(ast, StatArray[ix].type);
         } else {
           print_space_single(ast, StatArray[ix].stub_space);
         }
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Stubs and Blobs found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_dead > 0) {
       printBox(ast, '-', "Dead space consumption. ' ' indicates empty, '*' indicates full", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         print_space_single(ast, StatArray[ix].dead_space);
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No dead nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (!segment_granules) { // Prevent totally redundant printouts
       printBox(ast, '-', "Space consumption by tier (combined): <t1%>:<t2%>:<s%>. ' ' indicates empty, '*' indicates full", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 24;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
 

@@ -1952,11 +2078,12 @@
         } else {
           print_space_single(ast, StatArray[ix].stub_space);
         }
         ast->print(" ");
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
+      BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
     }
   }
 }
 
 void CodeHeapState::print_age(outputStream* out, CodeHeap* heap) {

@@ -1968,11 +2095,11 @@
   get_HeapStatGlobals(out, heapName);
 
   if ((StatArray == NULL) || (alloc_granules == 0)) {
     return;
   }
-  STRINGSTREAM_DECL(ast, out)
+  BUFFEREDSTREAM_DECL(ast, out)
 
   unsigned int granules_per_line = 32;
   char*        low_bound         = heap->low_boundary();
 
   {

@@ -1982,16 +2109,15 @@
                   "  Age information is available for tier1 and tier2 methods only. There is no\n"
                   "  age information for stubs and blobs, because they have no compilation ID assigned.\n"
                   "  Information for the youngest method (highest ID) in the granule is printed.\n"
                   "  Refer to the legend to learn how method age is mapped to the displayed digit.");
     print_age_legend(ast);
-    STRINGSTREAM_FLUSH_LOCKED("")
+    BUFFEREDSTREAM_FLUSH_LOCKED("")
   }
 
   {
     printBox(ast, '-', "Age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
-    STRINGSTREAM_FLUSH_LOCKED("")
 
     granules_per_line = 128;
     for (unsigned int ix = 0; ix < alloc_granules; ix++) {
       print_line_delim(out, ast, low_bound, ix, granules_per_line);
       unsigned int age1      = StatArray[ix].t1_age;

@@ -1999,78 +2125,76 @@
       unsigned int agex      = StatArray[ix].tx_age;
       unsigned int age       = age1 > age2 ? age1 : age2;
       age       = age > agex ? age : agex;
       print_age_single(ast, age);
     }
-    STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+    ast->print("|");
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_t1 > 0) {
       printBox(ast, '-', "Tier1 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         print_age_single(ast, StatArray[ix].t1_age);
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier1 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_t2 > 0) {
       printBox(ast, '-', "Tier2 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         print_age_single(ast, StatArray[ix].t2_age);
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier2 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (nBlocks_alive > 0) {
       printBox(ast, '-', "not_used/not_entrant/not_installed age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 128;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         print_age_single(ast, StatArray[ix].tx_age);
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
     } else {
       ast->print("No Tier2 nMethods found in CodeHeap.");
-      STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
     }
+    BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
   }
 
   {
     if (!segment_granules) { // Prevent totally redundant printouts
       printBox(ast, '-', "age distribution by tier <a1>:<a2>. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
-      STRINGSTREAM_FLUSH_LOCKED("")
 
       granules_per_line = 32;
       for (unsigned int ix = 0; ix < alloc_granules; ix++) {
         print_line_delim(out, ast, low_bound, ix, granules_per_line);
         print_age_single(ast, StatArray[ix].t1_age);
         ast->print(":");
         print_age_single(ast, StatArray[ix].t2_age);
         ast->print(" ");
       }
-      STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
+      ast->print("|");
+      BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
     }
   }
 }
 
 

@@ -2083,16 +2207,17 @@
   get_HeapStatGlobals(out, heapName);
 
   if ((StatArray == NULL) || (alloc_granules == 0)) {
     return;
   }
-  STRINGSTREAM_DECL(ast, out)
+  BUFFEREDSTREAM_DECL(ast, out)
 
   unsigned int granules_per_line  = 128;
   char*        low_bound          = heap->low_boundary();
   CodeBlob*    last_blob          = NULL;
   bool         name_in_addr_range = true;
+  bool         have_locks          = holding_required_locks();
 
   //---<  print at least 128K per block (i.e. between headers)  >---
   if (granules_per_line*granule_size < 128*K) {
     granules_per_line = (unsigned int)((128*K)/granule_size);
   }

@@ -2100,12 +2225,12 @@
   printBox(ast, '=', "M E T H O D   N A M E S   for ", heapName);
   ast->print_cr("  Method names are dynamically retrieved from the code cache at print time.\n"
                 "  Due to the living nature of the code heap and because the CodeCache_lock\n"
                 "  is not continuously held, the displayed name might be wrong or no name\n"
                 "  might be found at all. The likelihood for that to happen increases\n"
-                "  over time passed between aggregtion and print steps.\n");
-  STRINGSTREAM_FLUSH_LOCKED("")
+                "  over time passed between aggregation and print steps.\n");
+  BUFFEREDSTREAM_FLUSH_LOCKED("")
 
   for (unsigned int ix = 0; ix < alloc_granules; ix++) {
     //---<  print a new blob on a new line  >---
     if (ix%granules_per_line == 0) {
       if (!name_in_addr_range) {

@@ -2116,62 +2241,70 @@
       size_t end_ix = (ix+granules_per_line <= alloc_granules) ? ix+granules_per_line : alloc_granules;
       ast->cr();
       ast->print_cr("--------------------------------------------------------------------");
       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);
       ast->print_cr("--------------------------------------------------------------------");
-      STRINGSTREAM_FLUSH_LOCKED("")
+      BUFFEREDSTREAM_FLUSH_AUTO("")
     }
     // Only check granule if it contains at least one blob.
     unsigned int nBlobs  = StatArray[ix].t1_count   + StatArray[ix].t2_count + StatArray[ix].tx_count +
                            StatArray[ix].stub_count + StatArray[ix].dead_count;
     if (nBlobs > 0 ) {
     for (unsigned int is = 0; is < granule_size; is+=(unsigned int)seg_size) {
-      // heap->find_start() is safe. Only working with _segmap. Returns NULL or void*. Returned CodeBlob may be uninitialized.
-      CodeBlob* this_blob = (CodeBlob *)(heap->find_start(low_bound+ix*granule_size+is));
-      bool blob_initialized = (this_blob != NULL) && (this_blob->header_size() >= 0) && (this_blob->relocation_size() >= 0) &&
-                              ((address)this_blob + this_blob->header_size() == (address)(this_blob->relocation_begin())) &&
-                              ((address)this_blob + CodeBlob::align_code_offset(this_blob->header_size() + this_blob->relocation_size()) == (address)(this_blob->content_begin())) &&
-                              os::is_readable_pointer((address)(this_blob->relocation_begin())) &&
-                              os::is_readable_pointer(this_blob->content_begin());
+      // heap->find_start() is safe. Only works on _segmap.
+      // Returns NULL or void*. Returned CodeBlob may be uninitialized.
+      char*     this_seg  = low_bound + ix*granule_size + is;
+      CodeBlob* this_blob = (CodeBlob*)(heap->find_start(this_seg));
+      bool   blob_is_safe = blob_access_is_safe(this_blob);
       // blob could have been flushed, freed, and merged.
       // this_blob < last_blob is an indicator for that.
-      if (blob_initialized && (this_blob > last_blob)) {
+      if (blob_is_safe && (this_blob > last_blob)) {
         last_blob          = this_blob;
 
         //---<  get type and name  >---
         blobType       cbType = noType;
         if (segment_granules) {
           cbType = (blobType)StatArray[ix].type;
         } else {
+          //---<  access these fields only if we own the CodeCache_lock  >---
+          if (have_locks) {
           cbType = get_cbType(this_blob);
         }
+        }
+
+        //---<  access these fields only if we own the CodeCache_lock  >---
+        const char* blob_name = "<unavailable>";
+        nmethod*           nm = NULL;
+        if (have_locks) {
+          blob_name = this_blob->name();
+          nm        = this_blob->as_nmethod_or_null();
         // this_blob->name() could return NULL if no name was given to CTOR. Inlined, maybe invisible on stack
-        const char* blob_name = this_blob->name();
-        if ((blob_name == NULL) || !os::is_readable_pointer(blob_name)) {
+          if (blob_name == NULL) {
           blob_name = "<unavailable>";
         }
+        }
 
         //---<  print table header for new print range  >---
         if (!name_in_addr_range) {
           name_in_addr_range = true;
           ast->fill_to(51);
           ast->print("%9s", "compiler");
           ast->fill_to(61);
           ast->print_cr("%6s", "method");
           ast->print_cr("%18s %13s %17s %9s  %5s %18s  %s", "Addr(module)      ", "offset", "size", " type lvl", " temp", "blobType          ", "Name");
-          STRINGSTREAM_FLUSH_LOCKED("")
+          BUFFEREDSTREAM_FLUSH_AUTO("")
         }
 
         //---<  print line prefix (address and offset from CodeHeap start)  >---
         ast->print(INTPTR_FORMAT, p2i(this_blob));
         ast->fill_to(19);
         ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
         ast->fill_to(33);
 
-        // this_blob->as_nmethod_or_null() is safe. Inlined, maybe invisible on stack.
-        nmethod*    nm     = this_blob->as_nmethod_or_null();
-        if (CompiledMethod::nmethod_access_is_safe(nm)) {
+        // access nmethod and Method fields only if we own the CodeCache_lock.
+        // This fact is implicitly transported via nm != NULL.
+        if (nmethod_access_is_safe(nm)) {
           Method* method = nm->method();
           ResourceMark rm;
           //---<  collect all data to locals as quickly as possible  >---
           unsigned int total_size = nm->total_size();
           int          hotness    = nm->hotness_counter();

@@ -2203,25 +2336,28 @@
             ast->print("%s", methNameS);
             ast->print("%s", methSigS);
           } else {
             ast->print("%s", blob_name);
           }
-        } else {
+        } else if (blob_is_safe) {
           ast->fill_to(62+6);
           ast->print("%s", blobTypeName[cbType]);
           ast->fill_to(82+6);
           ast->print("%s", blob_name);
+        } else {
+          ast->fill_to(62+6);
+          ast->print("<stale blob>");
         }
-        STRINGSTREAM_FLUSH_LOCKED("\n")
-      } else if (!blob_initialized && (this_blob != last_blob) && (this_blob != NULL)) {
+        ast->cr();
+        BUFFEREDSTREAM_FLUSH_AUTO("")
+      } else if (!blob_is_safe && (this_blob != last_blob) && (this_blob != NULL)) {
         last_blob          = this_blob;
-        STRINGSTREAM_FLUSH_LOCKED("\n")
       }
     }
     } // nBlobs > 0
   }
-  STRINGSTREAM_FLUSH_LOCKED("\n\n")
+  BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
 }
 
 
 void CodeHeapState::printBox(outputStream* ast, const char border, const char* text1, const char* text2) {
   unsigned int lineLen = 1 + 2 + 2 + 1;

@@ -2354,11 +2490,15 @@
     if (ix > 0) {
       ast->print("|");
     }
     ast->cr();
 
-    { // can't use STRINGSTREAM_FLUSH_LOCKED("") here.
+    // can't use BUFFEREDSTREAM_FLUSH_IF("", 512) here.
+    // can't use this expression. bufferedStream::capacity() does not exist.
+    // if ((ast->capacity() - ast->size()) < 512) {
+    // Assume instead that default bufferedStream capacity (4K) was used.
+    if (ast->size() > 3*K) {
       ttyLocker ttyl;
       out->print("%s", ast->as_string());
       ast->reset();
     }
 

@@ -2366,29 +2506,54 @@
     ast->fill_to(19);
     ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
   }
 }
 
+// Find out which blob type we have at hand.
+// Return "noType" if anything abnormal is detected.
 CodeHeapState::blobType CodeHeapState::get_cbType(CodeBlob* cb) {
-  if ((cb != NULL) && os::is_readable_pointer(cb)) {
+  if (cb != NULL) {
     if (cb->is_runtime_stub())                return runtimeStub;
     if (cb->is_deoptimization_stub())         return deoptimizationStub;
     if (cb->is_uncommon_trap_stub())          return uncommonTrapStub;
     if (cb->is_exception_stub())              return exceptionStub;
     if (cb->is_safepoint_stub())              return safepointStub;
     if (cb->is_adapter_blob())                return adapterBlob;
     if (cb->is_method_handles_adapter_blob()) return mh_adapterBlob;
     if (cb->is_buffer_blob())                 return bufferBlob;
 
+    //---<  access these fields only if we own CodeCache_lock and Compile_lock  >---
+    // Should be ensured by caller. aggregate() and print_names() do that.
+    if (holding_required_locks()) {
     nmethod*  nm = cb->as_nmethod_or_null();
     if (nm != NULL) { // no is_readable check required, nm = (nmethod*)cb.
-      if (nm->is_not_installed()) return nMethod_inconstruction;
       if (nm->is_zombie())        return nMethod_dead;
       if (nm->is_unloaded())      return nMethod_unloaded;
       if (nm->is_in_use())        return nMethod_inuse;
       if (nm->is_alive() && !(nm->is_not_entrant()))   return nMethod_notused;
       if (nm->is_alive())         return nMethod_alive;
       return nMethod_dead;
     }
   }
+  }
   return noType;
 }
+
+// make sure the blob at hand is not garbage.
+bool CodeHeapState::blob_access_is_safe(CodeBlob* this_blob) {
+  return (this_blob != NULL) && // a blob must have been found, obviously
+         (this_blob->header_size() >= 0) &&
+         (this_blob->relocation_size() >= 0) &&
+         ((address)this_blob + this_blob->header_size() == (address)(this_blob->relocation_begin())) &&
+         ((address)this_blob + CodeBlob::align_code_offset(this_blob->header_size() + this_blob->relocation_size()) == (address)(this_blob->content_begin()));
+}
+
+// make sure the nmethod at hand (and the linked method) is not garbage.
+bool CodeHeapState::nmethod_access_is_safe(nmethod* nm) {
+  Method* method = (nm == NULL) ? NULL : nm->method(); // nm->method() was found to be uninitialized, i.e. != NULL, but invalid.
+  return (nm != NULL) && (method != NULL) && nm->is_alive() && (method->signature() != NULL);
+}
+
+bool CodeHeapState::holding_required_locks() {
+  return SafepointSynchronize::is_at_safepoint() ||
+        (CodeCache_lock->owned_by_self() && Compile_lock->owned_by_self());
+}
< prev index next >