1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_CODE_CODEHEAPSTATE_HPP
  27 #define SHARE_CODE_CODEHEAPSTATE_HPP
  28 
  29 // #include "code/codeBlob.hpp"
  30 // #include "memory/allocation.hpp"
  31 #include "memory/heap.hpp"
  32 // #include "memory/virtualspace.hpp"
  33 // #include "utilities/macros.hpp"
  34 #include "utilities/debug.hpp"
  35 #include "utilities/globalDefinitions.hpp"
  36 #include "utilities/ostream.hpp"
  37 
  38 class CodeHeapState : public CHeapObj<mtCode> {
  39 
  40  public:
  41   enum compType {
  42     noComp = 0,     // must be! due to initialization by memset to zero
  43     c1,
  44     c2,
  45     jvmci,
  46     lastComp
  47   };
  48 
  49   enum blobType {
  50      noType = 0,         // must be! due to initialization by memset to zero
  51      // The nMethod_* values correspond 1:1 to the CompiledMethod enum values.
  52      nMethod_inuse,       // executable. This is the "normal" state for a nmethod.
  53      nMethod_notused,     // assumed inactive, marked not entrant. Could be revived if necessary.
  54      nMethod_notentrant,  // no new activations allowed, marked for deoptimization. Old activations may still exist.
  55                          // Will transition to "zombie" after all activations are gone.
  56      nMethod_zombie,      // No more activations exist, ready for purge (remove from code cache).
  57      nMethod_unloaded,    // No activations exist, should not be called. Transient state on the way to "zombie".
  58      nMethod_alive = nMethod_notentrant, // Combined state: nmethod may have activations, thus can't be purged.
  59      nMethod_dead  = nMethod_zombie,     // Combined state: nmethod does not have any activations.
  60      runtimeStub   = nMethod_unloaded + 1,
  61      ricochetStub,
  62      deoptimizationStub,
  63      uncommonTrapStub,
  64      exceptionStub,
  65      safepointStub,
  66      adapterBlob,
  67      mh_adapterBlob,
  68      bufferBlob,
  69      lastType
  70   };
  71 
  72  private:
  73   static void prepare_StatArray(outputStream* out, size_t nElem, size_t granularity, const char* heapName);
  74   static void prepare_FreeArray(outputStream* out, unsigned int nElem, const char* heapName);
  75   static void prepare_TopSizeArray(outputStream* out, unsigned int nElem, const char* heapName);
  76   static void prepare_SizeDistArray(outputStream* out, unsigned int nElem, const char* heapName);
  77   static void discard_StatArray(outputStream* out);
  78   static void discard_FreeArray(outputStream* out);
  79   static void discard_TopSizeArray(outputStream* out);
  80   static void discard_SizeDistArray(outputStream* out);
  81 
  82   static void update_SizeDistArray(outputStream* out, unsigned int len);
  83 
  84   static const char* get_heapName(CodeHeap* heap);
  85   static unsigned int findHeapIndex(outputStream* out, const char* heapName);
  86   static void get_HeapStatGlobals(outputStream* out, const char* heapName);
  87   static void set_HeapStatGlobals(outputStream* out, const char* heapName);
  88 
  89   static void printBox(outputStream* out, const char border, const char* text1, const char* text2);
  90   static void print_blobType_legend(outputStream* out);
  91   static void print_space_legend(outputStream* out);
  92   static void print_age_legend(outputStream* out);
  93   static void print_blobType_single(outputStream *ast, u2 /* blobType */ type);
  94   static void print_count_single(outputStream *ast, unsigned short count);
  95   static void print_space_single(outputStream *ast, unsigned short space);
  96   static void print_age_single(outputStream *ast, unsigned int age);
  97   static void print_line_delim(outputStream* out, bufferedStream *sst, char* low_bound, unsigned int ix, unsigned int gpl);
  98   static void print_line_delim(outputStream* out, outputStream *sst, char* low_bound, unsigned int ix, unsigned int gpl);
  99   static blobType get_cbType(CodeBlob* cb);
 100 
 101  public:
 102   static void discard(outputStream* out, CodeHeap* heap);
 103   static void aggregate(outputStream* out, CodeHeap* heap, const char* granularity);
 104   static void print_usedSpace(outputStream* out, CodeHeap* heap);
 105   static void print_freeSpace(outputStream* out, CodeHeap* heap);
 106   static void print_count(outputStream* out, CodeHeap* heap);
 107   static void print_space(outputStream* out, CodeHeap* heap);
 108   static void print_age(outputStream* out, CodeHeap* heap);
 109   static void print_names(outputStream* out, CodeHeap* heap);
 110 };
 111 
 112 //----------------
 113 //  StatElement
 114 //----------------
 115 //  Each analysis granule is represented by an instance of
 116 //  this StatElement struct. It collects and aggregates all
 117 //  information describing the allocated contents of the granule.
 118 //  Free (unallocated) contents is not considered (see FreeBlk for that).
 119 //  All StatElements of a heap segment are stored in the related StatArray.
 120 //  Current size: 40 bytes + 8 bytes class header.
 121 class StatElement : public CHeapObj<mtCode> {
 122   public:
 123     // A note on ages: The compilation_id easily overflows unsigned short in large systems
 124     unsigned int       t1_age;      // oldest compilation_id of tier1 nMethods.
 125     unsigned int       t2_age;      // oldest compilation_id of tier2 nMethods.
 126     unsigned int       tx_age;      // oldest compilation_id of inactive/not entrant nMethods.
 127     unsigned short     t1_space;    // in units of _segment_size to "prevent" overflow
 128     unsigned short     t2_space;    // in units of _segment_size to "prevent" overflow
 129     unsigned short     tx_space;    // in units of _segment_size to "prevent" overflow
 130     unsigned short     dead_space;  // in units of _segment_size to "prevent" overflow
 131     unsigned short     stub_space;  // in units of _segment_size to "prevent" overflow
 132     unsigned short     t1_count;
 133     unsigned short     t2_count;
 134     unsigned short     tx_count;
 135     unsigned short     dead_count;
 136     unsigned short     stub_count;
 137     CompLevel          level;       // optimization level (see globalDefinitions.hpp)
 138     //---<  replaced the correct enum typing with u2 to save space.
 139     u2                 compiler;    // compiler which generated this blob
 140     u2                 type;        // used only if granularity == segment_size
 141 //    CodeHeapState::compType compiler;    // compiler which generated this blob
 142 //    CodeHeapState::blobType type;        // used only if granularity == segment_size
 143 };
 144 
 145 //-----------
 146 //  FreeBlk
 147 //-----------
 148 //  Each free block in the code heap is represented by an instance
 149 //  of this FreeBlk struct. It collects all information we need to
 150 //  know about each free block.
 151 //  All FreeBlks of a heap segment are stored in the related FreeArray.
 152 struct FreeBlk : public CHeapObj<mtCode> {
 153   HeapBlock*     start;       // address of free block
 154   unsigned int   len;          // length of free block
 155 
 156   unsigned int   gap;          // gap to next free block
 157   unsigned int   index;        // sequential number of free block
 158   unsigned short n_gapBlocks;  // # used blocks in gap
 159   bool           stubs_in_gap; // The occupied space between this and the next free block contains (unmovable) stubs or blobs.
 160 };
 161 
 162 //--------------
 163 //  TopSizeBlk
 164 //--------------
 165 //  The n largest blocks in the code heap are represented in an instance
 166 //  of this TopSizeBlk struct. It collects all information we need to
 167 //  know about those largest blocks.
 168 //  All TopSizeBlks of a heap segment are stored in the related TopSizeArray.
 169 struct TopSizeBlk : public CHeapObj<mtCode> {
 170   HeapBlock*     start;       // address of block
 171   unsigned int   len;          // length of block, in _segment_size units. Will never overflow int.
 172 
 173   unsigned int   index;        // ordering index, 0 is largest block
 174                                // contains array index of next smaller block
 175                                // -1 indicates end of list
 176   CompLevel      level;        // optimization level (see globalDefinitions.hpp)
 177   u2             compiler;     // compiler which generated this blob
 178   u2             type;         // blob type
 179 };
 180 
 181 //---------------------------
 182 //  SizeDistributionElement
 183 //---------------------------
 184 //  During CodeHeap analysis, each allocated code block is associated with a
 185 //  SizeDistributionElement according to its size. Later on, the array of
 186 //  SizeDistributionElements is used to print a size distribution bar graph.
 187 //  All SizeDistributionElements of a heap segment are stored in the related SizeDistributionArray.
 188 struct SizeDistributionElement : public CHeapObj<mtCode> {
 189                                // Range is [rangeStart..rangeEnd).
 190   unsigned int   rangeStart;   // start of length range, in _segment_size units.
 191   unsigned int   rangeEnd;     // end   of length range, in _segment_size units.
 192   unsigned int   lenSum;       // length of block, in _segment_size units. Will never overflow int.
 193 
 194   unsigned int   count;        // number of blocks assigned to this range.
 195 };
 196 
 197 //----------------
 198 //  CodeHeapStat
 199 //----------------
 200 //  Because we have to deal with multiple CodeHeaps, we need to
 201 //  collect "global" information in a segment-specific way as well.
 202 //  Thats what the CodeHeapStat and CodeHeapStatArray are used for.
 203 //  Before a heap segment is processed, the contents of the CodeHeapStat
 204 //  element is copied to the global variables (get_HeapStatGlobals).
 205 //  When processing is done, the possibly modified global variables are
 206 //  copied back (set_HeapStatGlobals) to the CodeHeapStat element.
 207 struct CodeHeapStat {
 208 //    struct StatElement              *StatArray;
 209     StatElement*                     StatArray;
 210     struct FreeBlk*                  FreeArray;
 211     struct TopSizeBlk*               TopSizeArray;
 212     struct SizeDistributionElement*  SizeDistributionArray;
 213     const char*                      heapName;
 214     size_t                           segment_size;
 215     // StatElement data
 216     size_t        alloc_granules;
 217     size_t        granule_size;
 218     bool          segment_granules;
 219     unsigned int  nBlocks_t1;
 220     unsigned int  nBlocks_t2;
 221     unsigned int  nBlocks_alive;
 222     unsigned int  nBlocks_dead;
 223     unsigned int  nBlocks_unloaded;
 224     unsigned int  nBlocks_stub;
 225     // FreeBlk data
 226     unsigned int  alloc_freeBlocks;
 227     // UsedBlk data
 228     unsigned int  alloc_topSizeBlocks;
 229     unsigned int  used_topSizeBlocks;
 230     // method hotness data. Temperature range is [-reset_val..+reset_val]
 231     int           avgTemp;
 232     int           maxTemp;
 233     int           minTemp;
 234 };
 235 
 236 #endif // SHARE_CODE_CODEHEAPSTATE_HPP