void CodeHeapState::aggregate()

in src/hotspot/share/code/codeHeapState.cpp [512:1222]


void CodeHeapState::aggregate(outputStream* out, CodeHeap* heap, size_t granularity) {
  unsigned int nBlocks_free    = 0;
  unsigned int nBlocks_used    = 0;
  unsigned int nBlocks_zomb    = 0;
  unsigned int nBlocks_disconn = 0;
  unsigned int nBlocks_notentr = 0;

  //---<  max & min of TopSizeArray  >---
  //  it is sufficient to have these sizes as 32bit unsigned ints.
  //  The CodeHeap is limited in size to 4GB. Furthermore, the sizes
  //  are stored in _segment_size units, scaling them down by a factor of 64 (at least).
  unsigned int  currMax          = 0;
  unsigned int  currMin          = 0;
  unsigned int  currMin_ix       = 0;
  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 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);
  BUFFEREDSTREAM_DECL(ast, out)

  if (!initialization_complete) {
    memset(CodeHeapStatArray, 0, sizeof(CodeHeapStatArray));
    initialization_complete = true;

    printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (general remarks)", nullptr);
    ast->print_cr("   The code heap analysis function provides deep insights into\n"
                  "   the inner workings and the internal state of the Java VM's\n"
                  "   code cache - the place where all the JVM generated machine\n"
                  "   code is stored.\n"
                  "   \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");
    BUFFEREDSTREAM_FLUSH("")
  }
  get_HeapStatGlobals(out, heapName);


  // Since we are (and must be) analyzing the CodeHeap contents under the CodeCache_lock,
  // all heap information is "constant" and can be safely extracted/calculated before we
  // enter the while() loop. Actually, the loop will only be iterated once.
  char*  low_bound     = heap->low_boundary();
  size_t size          = heap->capacity();
  size_t res_size      = heap->max_capacity();
  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);
    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.
  //   The CodeHeap can become fairly large, in particular in productive real-life systems.
  //
  //   It is often neither feasible nor desirable to aggregate the data with the highest possible
  //   level of detail, i.e. inspecting and printing each segment on its own.
  //
  //   The granularity parameter allows to specify the level of detail available in the analysis.
  //   It must be a positive multiple of the segment size and should be selected such that enough
  //   detail is provided while, at the same time, the printed output does not explode.
  //
  //   By manipulating the granularity value, we enforce that at least min_granules units
  //   of analysis are available. We also enforce an upper limit of max_granules units to
  //   keep the amount of allocated storage in check.
  //
  //   Finally, we adjust the granularity such that each granule covers at most 64k-1 segments.
  //   This is necessary to prevent an unsigned short overflow while accumulating space information.
  //
  assert(granularity > 0, "granularity should be positive.");

  if (granularity > size) {
    granularity = size;
  }
  if (size/granularity < min_granules) {
    granularity = size/min_granules;                                   // at least min_granules granules
  }
  granularity = granularity & (~(seg_size - 1));                       // must be multiple of seg_size
  if (granularity < seg_size) {
    granularity = seg_size;                                            // must be at least seg_size
  }
  if (size/granularity > max_granules) {
    granularity = size/max_granules;                                   // at most max_granules granules
  }
  granularity = granularity & (~(seg_size - 1));                       // must be multiple of seg_size
  if (granularity>>log2_seg_size >= (1L<<sizeof(unsigned short)*8)) {
    granularity = ((1L<<(sizeof(unsigned short)*8))-1)<<log2_seg_size; // Limit: (64k-1) * seg_size
  }
  segment_granules = granularity == seg_size;
  size_t granules  = (size + (granularity-1))/granularity;

  printBox(ast, '=', "C O D E   H E A P   A N A L Y S I S   (used blocks) for segment ", heapName);
  ast->print_cr("   The aggregate step takes an aggregated snapshot of the CodeHeap.\n"
                "   Subsequent print functions create their output based on this snapshot.\n"
                "   The CodeHeap is a living thing, and every effort has been made for the\n"
                "   collected data to be consistent. Only the method names and signatures\n"
                "   are retrieved at print time. That may lead to rare cases where the\n"
                "   name of a method is no longer available, e.g. because it was unloaded.\n");
  ast->print_cr("   CodeHeap committed size %zuK (%zuM), reserved size %zuK (%zuM), %d%% occupied.",
                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 %zu bytes. This is the smallest possible granularity.", seg_size);
  ast->print_cr("   CodeHeap (committed part) is mapped to %zu granules of size %zu bytes.", granules, granularity);
  ast->print_cr("   Each granule takes %zu bytes of C heap, that is %zuK 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));
  BUFFEREDSTREAM_FLUSH("\n")


  while (!done) {
    //---<  reset counters with every aggregation  >---
    nBlocks_t1       = 0;
    nBlocks_t2       = 0;
    nBlocks_alive    = 0;
    nBlocks_stub     = 0;

    nBlocks_free     = 0;
    nBlocks_used     = 0;
    nBlocks_zomb     = 0;
    nBlocks_disconn  = 0;
    nBlocks_notentr  = 0;

    //---<  discard old arrays if size does not match  >---
    if (granules != alloc_granules) {
      discard_StatArray(out);
      discard_TopSizeArray(out);
    }

    //---<  allocate arrays if they don't yet exist, initialize  >---
    prepare_StatArray(out, granules, granularity, heapName);
    if (StatArray == nullptr) {
      set_HeapStatGlobals(out, heapName);
      return;
    }
    prepare_TopSizeArray(out, maxTopSizeBlocks, heapName);
    prepare_SizeDistArray(out, nSizeDistElements, heapName);

    latest_compilation_id = CompileBroker::get_compilation_id();
    int          highest_compilation_id = 0;
    size_t       usedSpace              = 0;
    size_t       t1Space                = 0;
    size_t       t2Space                = 0;
    size_t       aliveSpace             = 0;
    size_t       disconnSpace           = 0;
    size_t       notentrSpace           = 0;
    size_t       stubSpace              = 0;
    size_t       freeSpace              = 0;
    size_t       maxFreeSize            = 0;
    HeapBlock*   maxFreeBlock           = nullptr;
    bool         insane                 = false;

    unsigned int n_methods              = 0;

    for (HeapBlock *h = heap->first_block(); h != nullptr && !insane; h = heap->next_block(h)) {
      unsigned int hb_len     = (unsigned int)h->length();  // despite being size_t, length can never overflow an unsigned int.
      size_t       hb_bytelen = ((size_t)hb_len)<<log2_seg_size;
      unsigned int ix_beg     = (unsigned int)(((char*)h-low_bound)/granule_size);
      unsigned int ix_end     = (unsigned int)(((char*)h-low_bound+(hb_bytelen-1))/granule_size);
      int compile_id = 0;
      CompLevel    comp_lvl   = CompLevel_none;
      compType     cType      = noComp;
      blobType     cbType     = noType;

      //---<  some sanity checks  >---
      // Do not assert here, just check, print error message and return.
      // This is a diagnostic function. It is not supposed to tear down the VM.
      if ((char*)h <  low_bound) {
        insane = true; ast->print_cr("Sanity check: HeapBlock @%p below low bound (%p)", (char*)h, low_bound);
      }
      if ((char*)h >  (low_bound + res_size)) {
        insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside reserved range (%p)", (char*)h, low_bound + res_size);
      }
      if ((char*)h >  (low_bound + size)) {
        insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside used range (%p)", (char*)h, low_bound + size);
      }
      if (ix_end   >= granules) {
        insane = true; ast->print_cr("Sanity check: end index (%d) out of bounds (%zu)", ix_end, granules);
      }
      if (size     != heap->capacity()) {
        insane = true; ast->print_cr("Sanity check: code heap capacity has changed (%zuK to %zuK)", size/(size_t)K, heap->capacity()/(size_t)K);
      }
      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) {
        BUFFEREDSTREAM_FLUSH("")
        continue;
      }

      if (h->free()) {
        nBlocks_free++;
        freeSpace    += hb_bytelen;
        if (hb_bytelen > maxFreeSize) {
          maxFreeSize   = hb_bytelen;
          maxFreeBlock  = h;
        }
      } else {
        update_SizeDistArray(out, hb_len);
        nBlocks_used++;
        usedSpace    += hb_bytelen;
        CodeBlob* cb  = (CodeBlob*)heap->find_start(h);
        cbType = get_cbType(cb);  // Will check for cb == nullptr and other safety things.
        if (cbType != noType) {
          const char* blob_name  = nullptr;
          unsigned int nm_size   = 0;
          nmethod*  nm = cb->as_nmethod_or_null();
          if (nm != nullptr) { // no is_readable check required, nm = (nmethod*)cb.
            ResourceMark rm;
            Method* method = nm->method();
            if (nm->is_in_use() || nm->is_not_entrant()) {
              blob_name = os::strdup(method->name_and_sig_as_C_string());
            } else {
              blob_name = os::strdup(cb->name());
            }
#if INCLUDE_JVMCI
            const char* jvmci_name = nm->jvmci_name();
            if (jvmci_name != nullptr) {
              size_t size = ::strlen(blob_name) + ::strlen(" jvmci_name=") + ::strlen(jvmci_name) + 1;
              char* new_blob_name = (char*)os::malloc(size, mtInternal);
              os::snprintf_checked(new_blob_name, size, "%s jvmci_name=%s", blob_name, jvmci_name);
              os::free((void*)blob_name);
              blob_name = new_blob_name;
            }
#endif
            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 (nm->is_compiled_by_c2()) {
              cType = c2;
            }
            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.
                n_methods++;
                break;
              }
              case nMethod_notused:
                nBlocks_alive++;
                nBlocks_disconn++;
                aliveSpace     += hb_bytelen;
                disconnSpace   += hb_bytelen;
                break;
              case nMethod_notentrant:  // equivalent to nMethod_alive
                nBlocks_alive++;
                nBlocks_notentr++;
                aliveSpace     += hb_bytelen;
                notentrSpace   += hb_bytelen;
                break;
              default:
                break;
            }
          } else {
            blob_name  = os::strdup(cb->name());
          }

          //------------------------------------------
          //---<  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].compiler    = cType;
              TopSizeArray[0].level       = comp_lvl;
              TopSizeArray[0].type        = cbType;
              currMax    = hb_len;
              currMin    = hb_len;
              currMin_ix = 0;
              used_topSizeBlocks++;
              blob_name  = nullptr; // 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].compiler    = cType;
              TopSizeArray[used_topSizeBlocks].level       = comp_lvl;
              TopSizeArray[used_topSizeBlocks].type        = cbType;
              currMin    = hb_len;
              currMin_ix = used_topSizeBlocks;
              used_topSizeBlocks++;
              blob_name  = nullptr; // 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))) {
                if (currMax < hb_len) {
                  currMax = hb_len;
                }
                unsigned int i;
                unsigned int prev_i  = tsbStopper;
                unsigned int limit_i =  0;
                for (i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
                  if (limit_i++ >= alloc_topSizeBlocks) {
                    insane = true; break; // emergency exit
                  }
                  if (i >= used_topSizeBlocks)  {
                    insane = true; break; // emergency exit
                  }
                  total_iterations++;
                  if (TopSizeArray[i].len < hb_len) {
                    //---<  We want to insert here, element <i> is smaller than the current one  >---
                    if (used_topSizeBlocks < alloc_topSizeBlocks) { // still room for a new entry to insert
                      // old entry gets moved to the next free element of the array.
                      // That's necessary to keep the entry for the largest block at index 0.
                      // This move might cause the current minimum to be moved to another place
                      if (i == currMin_ix) {
                        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].compiler    = cType;
                      TopSizeArray[i].level       = comp_lvl;
                      TopSizeArray[i].type        = cbType;
                      used_topSizeBlocks++;
                      blob_name  = nullptr; // 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) {
                          insane = true; break; // emergency exit
                        }
                        if (j >= used_topSizeBlocks)  {
                          insane = true; break; // emergency exit
                        }
                        total_iterations++;
                        prev_j = j;
                        j      = TopSizeArray[j].index;
                      }
                      if (!insane) {
                        if (TopSizeArray[j].blob_name != nullptr) {
                          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[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;
                          //---<  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].compiler    = cType;
                          TopSizeArray[i].level       = comp_lvl;
                          TopSizeArray[i].type        = cbType;
                        }
                        blob_name  = nullptr; // indicate blob_name was consumed
                      } // insane
                    }
                    break;
                  }
                  prev_i = i;
                }
                if (insane) {
                  // Note: regular analysis could probably continue by resetting "insane" flag.
                  out->print_cr("Possible loop in TopSizeBlocks list detected. Analysis aborted.");
                  discard_TopSizeArray(out);
                }
              }
            }
          }
          if (blob_name != nullptr) {
            os::free((void*)blob_name);
            blob_name = nullptr;
          }
          //----------------------------------------------
          //---<  END register block in TopSizeArray  >---
          //----------------------------------------------
        } else {
          nBlocks_zomb++;
        }

        if (ix_beg == ix_end) {
          StatArray[ix_beg].type = cbType;
          switch (cbType) {
            case nMethod_inuse:
              highest_compilation_id = (highest_compilation_id >= compile_id) ? highest_compilation_id : compile_id;
              if (comp_lvl < CompLevel_full_optimization) {
                nBlocks_t1++;
                t1Space   += hb_bytelen;
                StatArray[ix_beg].t1_count++;
                StatArray[ix_beg].t1_space += (unsigned short)hb_len;
                StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;
              } else {
                nBlocks_t2++;
                t2Space   += hb_bytelen;
                StatArray[ix_beg].t2_count++;
                StatArray[ix_beg].t2_space += (unsigned short)hb_len;
                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;
            default:
              nBlocks_stub++;
              stubSpace   += hb_bytelen;
              StatArray[ix_beg].stub_count++;
              StatArray[ix_beg].stub_space += (unsigned short)hb_len;
              break;
          }
        } else {
          unsigned int beg_space = (unsigned int)(granule_size - ((char*)h - low_bound - ix_beg*granule_size));
          unsigned int end_space = (unsigned int)(hb_bytelen - beg_space - (ix_end-ix_beg-1)*granule_size);
          beg_space = beg_space>>log2_seg_size;  // store in units of _segment_size
          end_space = end_space>>log2_seg_size;  // store in units of _segment_size
          StatArray[ix_beg].type = cbType;
          StatArray[ix_end].type = cbType;
          switch (cbType) {
            case nMethod_inuse:
              highest_compilation_id = (highest_compilation_id >= compile_id) ? highest_compilation_id : compile_id;
              if (comp_lvl < CompLevel_full_optimization) {
                nBlocks_t1++;
                t1Space   += hb_bytelen;
                StatArray[ix_beg].t1_count++;
                StatArray[ix_beg].t1_space += (unsigned short)beg_space;
                StatArray[ix_beg].t1_age    = StatArray[ix_beg].t1_age < compile_id ? compile_id : StatArray[ix_beg].t1_age;

                StatArray[ix_end].t1_count++;
                StatArray[ix_end].t1_space += (unsigned short)end_space;
                StatArray[ix_end].t1_age    = StatArray[ix_end].t1_age < compile_id ? compile_id : StatArray[ix_end].t1_age;
              } else {
                nBlocks_t2++;
                t2Space   += hb_bytelen;
                StatArray[ix_beg].t2_count++;
                StatArray[ix_beg].t2_space += (unsigned short)beg_space;
                StatArray[ix_beg].t2_age    = StatArray[ix_beg].t2_age < compile_id ? compile_id : StatArray[ix_beg].t2_age;

                StatArray[ix_end].t2_count++;
                StatArray[ix_end].t2_space += (unsigned short)end_space;
                StatArray[ix_end].t2_age    = StatArray[ix_end].t2_age < compile_id ? compile_id : StatArray[ix_end].t2_age;
              }
              StatArray[ix_beg].level     = comp_lvl;
              StatArray[ix_beg].compiler  = cType;
              StatArray[ix_end].level     = comp_lvl;
              StatArray[ix_end].compiler  = cType;
              break;
            default:
              nBlocks_stub++;
              stubSpace   += hb_bytelen;
              StatArray[ix_beg].stub_count++;
              StatArray[ix_beg].stub_space += (unsigned short)beg_space;
              StatArray[ix_end].stub_count++;
              StatArray[ix_end].stub_space += (unsigned short)end_space;
              break;
          }
          for (unsigned int ix = ix_beg+1; ix < ix_end; ix++) {
            StatArray[ix].type = cbType;
            switch (cbType) {
              case nMethod_inuse:
                if (comp_lvl < CompLevel_full_optimization) {
                  StatArray[ix].t1_count++;
                  StatArray[ix].t1_space += (unsigned short)(granule_size>>log2_seg_size);
                  StatArray[ix].t1_age    = StatArray[ix].t1_age < compile_id ? compile_id : StatArray[ix].t1_age;
                } else {
                  StatArray[ix].t2_count++;
                  StatArray[ix].t2_space += (unsigned short)(granule_size>>log2_seg_size);
                  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;
              default:
                StatArray[ix].stub_count++;
                StatArray[ix].stub_space += (unsigned short)(granule_size>>log2_seg_size);
                break;
            }
          }
        }
      }
    }
    done = true;

    if (!insane) {
      // There is a risk for this block (because it contains many print statements) to get
      // interspersed with print data from other threads. We take this risk intentionally.
      // Getting stalled waiting for tty_lock while holding the CodeCache_lock is not desirable.
      printBox(ast, '-', "Global CodeHeap statistics for segment ", heapName);
      ast->print_cr("freeSpace        = %8zuk, 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);
      ast->print_cr("usedSpace        = %8zuk, 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);
      ast->print_cr("  Tier1 Space    = %8zuk, 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    = %8zuk, 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    = %8zuk, 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 = %8zuk, 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  = %8zuk, 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("  stubSpace      = %8zuk, 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();
      ast->print_cr("Segment start          = " INTPTR_FORMAT ", used space      = %8zuk", p2i(low_bound), size/K);
      ast->print_cr("Segment end (used)     = " INTPTR_FORMAT ", remaining space = %8zuk", p2i(low_bound) + size, (res_size - size)/K);
      ast->print_cr("Segment end (reserved) = " INTPTR_FORMAT ", reserved space  = %8zuk", p2i(low_bound) + res_size, res_size/K);
      ast->cr();
      ast->print_cr("latest allocated compilation id = %d", latest_compilation_id);
      ast->print_cr("highest observed compilation id = %d", highest_compilation_id);
      ast->print_cr("Building TopSizeList iterations = %ld", total_iterations);
      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);
        }
        if (StatArray[ix].t2_count   > granule_segs) {
          out->print_cr("t2_count[%d]   = %d", ix, StatArray[ix].t2_count);
        }
        if (StatArray[ix].tx_count   > granule_segs) {
          out->print_cr("tx_count[%d]   = %d", ix, StatArray[ix].tx_count);
        }
        if (StatArray[ix].stub_count > granule_segs) {
          out->print_cr("stub_count[%d] = %d", ix, StatArray[ix].stub_count);
        }
        if (StatArray[ix].t1_space   > granule_segs) {
          out->print_cr("t1_space[%d]   = %d", ix, StatArray[ix].t1_space);
        }
        if (StatArray[ix].t2_space   > granule_segs) {
          out->print_cr("t2_space[%d]   = %d", ix, StatArray[ix].t2_space);
        }
        if (StatArray[ix].tx_space   > granule_segs) {
          out->print_cr("tx_space[%d]   = %d", ix, StatArray[ix].tx_space);
        }
        if (StatArray[ix].stub_space > granule_segs) {
          out->print_cr("stub_space[%d] = %d", ix, StatArray[ix].stub_space);
        }
        //   this cast is awful! I need it because NT/Intel reports a signed/unsigned mismatch.
        if ((size_t)(StatArray[ix].t1_count+StatArray[ix].t2_count+StatArray[ix].tx_count+StatArray[ix].stub_count) > granule_segs) {
          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);
        }
        if ((size_t)(StatArray[ix].t1_space+StatArray[ix].t2_space+StatArray[ix].tx_space+StatArray[ix].stub_space) > granule_segs) {
          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);
        }
        for (unsigned int i = 0; (TopSizeArray[i].index != tsbStopper) && (j++ < alloc_topSizeBlocks); i = TopSizeArray[i].index) {
          if (TopSizeArray[i].len < TopSizeArray[TopSizeArray[i].index].len) {
            out->print_cr("sort error at index %d: %d !>= %d", i, TopSizeArray[i].len, TopSizeArray[TopSizeArray[i].index].len);
          }
        }
        if (j >= alloc_topSizeBlocks) {
          out->print_cr("Possible loop in TopSizeArray chaining!\n  allocBlocks = %d, usedBlocks = %d", alloc_topSizeBlocks, used_topSizeBlocks);
          for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
            out->print_cr("  TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
          }
        }
      }
      out->print_cr("...done\n\n");
    } else {
      // insane heap state detected. Analysis data incomplete. Just throw it away.
      discard_StatArray(out);
      discard_TopSizeArray(out);
    }
  }


  done        = false;
  while (!done && (nBlocks_free > 0)) {

    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 %zu bytes of C heap for statistics data, that is %zuK in total.", sizeof(FreeBlk), (sizeof(FreeBlk)*nBlocks_free)/K);
    BUFFEREDSTREAM_FLUSH("\n")

    //----------------------------------------
    //--  Prepare the FreeArray of FreeBlks --
    //----------------------------------------

    //---< discard old array if size does not match  >---
    if (nBlocks_free != alloc_freeBlocks) {
      discard_FreeArray(out);
    }

    prepare_FreeArray(out, nBlocks_free, heapName);
    if (FreeArray == nullptr) {
      done = true;
      continue;
    }

    //----------------------------------------
    //--  Collect all FreeBlks in FreeArray --
    //----------------------------------------

    unsigned int ix = 0;
    FreeBlock* cur  = heap->freelist();

    while (cur != nullptr) {
      if (ix < alloc_freeBlocks) { // don't index out of bounds if _freelist has more blocks than anticipated
        FreeArray[ix].start = cur;
        FreeArray[ix].len   = (unsigned int)(cur->length()<<log2_seg_size);
        FreeArray[ix].index = ix;
      }
      cur  = cur->link();
      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");
      BUFFEREDSTREAM_FLUSH("\n")
      nBlocks_free = ix;
      continue;
    }
    done = true;
  }

  if (!done || (nBlocks_free == 0)) {
    if (nBlocks_free == 0) {
      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");
    }
    BUFFEREDSTREAM_FLUSH("")

    //---< discard old array and update global values  >---
    discard_FreeArray(out);
    set_HeapStatGlobals(out, heapName);
    return;
  }

  //---<  calculate and fill remaining fields  >---
  if (FreeArray != nullptr) {
    // 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 != nullptr) && (h != FreeArray[ix+1].start); h = heap->next_block(h)) {
        CodeBlob *cb  = (CodeBlob*)(heap->find_start(h));
        if ((cb != nullptr) && !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)) {
          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);
        }
      }
      if (lenSum != FreeArray[ix].gap) {
        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);
      }
    }
  }
  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);
  BUFFEREDSTREAM_FLUSH("\n")
}