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