1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2018, SAP and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 28 #include "memory/metaspace/metaspaceCommon.hpp" 29 #include "memory/metaspace/virtualSpaceNode.hpp" 30 #include "utilities/debug.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 #include "utilities/ostream.hpp" 33 34 namespace metaspace { 35 36 DEBUG_ONLY(internal_statistics_t g_internal_statistics;) 37 38 // Print a size, in words, scaled. 39 void print_scaled_words(outputStream* st, size_t word_size, size_t scale, int width) { 40 print_human_readable_size(st, word_size * sizeof(MetaWord), scale, width); 41 } 42 43 // Convenience helper: prints a size value and a percentage. 44 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale, int width) { 45 print_scaled_words(st, word_size, scale, width); 46 st->print(" ("); 47 print_percentage(st, compare_word_size, word_size); 48 st->print(")"); 49 } 50 51 52 // Print a human readable size. 53 // byte_size: size, in bytes, to be printed. 54 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively, 55 // or 0, which means the best scale is choosen dynamically. 56 // width: printing width. 57 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale, int width) { 58 if (scale == 0) { 59 // Dynamic mode. Choose scale for this value. 60 if (byte_size == 0) { 61 // Zero values are printed as bytes. 62 scale = 1; 63 } else { 64 if (byte_size >= G) { 65 scale = G; 66 } else if (byte_size >= M) { 67 scale = M; 68 } else if (byte_size >= K) { 69 scale = K; 70 } else { 71 scale = 1; 72 } 73 } 74 return print_human_readable_size(st, byte_size, scale, width); 75 } 76 77 #ifdef ASSERT 78 assert(scale == 1 || scale == BytesPerWord || scale == K || scale == M || scale == G, "Invalid scale"); 79 // Special case: printing wordsize should only be done with word-sized values 80 if (scale == BytesPerWord) { 81 assert(byte_size % BytesPerWord == 0, "not word sized"); 82 } 83 #endif 84 85 if (scale == 1) { 86 st->print("%*" PRIuPTR " bytes", width, byte_size); 87 } else if (scale == BytesPerWord) { 88 st->print("%*" PRIuPTR " words", width, byte_size / BytesPerWord); 89 } else { 90 const char* display_unit = ""; 91 switch(scale) { 92 case 1: display_unit = "bytes"; break; 93 case BytesPerWord: display_unit = "words"; break; 94 case K: display_unit = "KB"; break; 95 case M: display_unit = "MB"; break; 96 case G: display_unit = "GB"; break; 97 default: 98 ShouldNotReachHere(); 99 } 100 float display_value = (float) byte_size / scale; 101 // Since we use width to display a number with two trailing digits, increase it a bit. 102 width += 3; 103 // Prevent very small but non-null values showing up as 0.00. 104 if (byte_size > 0 && display_value < 0.01f) { 105 st->print("%*s %s", width, "<0.01", display_unit); 106 } else { 107 st->print("%*.2f %s", width, display_value, display_unit); 108 } 109 } 110 } 111 112 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values 113 // larger than 99% but not 100% are displayed as ">100%". 114 void print_percentage(outputStream* st, size_t total, size_t part) { 115 if (total == 0) { 116 st->print(" ?%%"); 117 } else if (part == 0) { 118 st->print(" 0%%"); 119 } else if (part == total) { 120 st->print("100%%"); 121 } else { 122 // Note: clearly print very-small-but-not-0% and very-large-but-not-100% percentages. 123 float p = ((float)part / total) * 100.0f; 124 if (p < 1.0f) { 125 st->print(" <1%%"); 126 } else if (p > 99.0f){ 127 st->print(">99%%"); 128 } else { 129 st->print("%3.0f%%", p); 130 } 131 } 132 } 133 134 // Returns size of this chunk type. 135 size_t get_size_for_nonhumongous_chunktype(ChunkIndex chunktype, bool is_class) { 136 assert(is_valid_nonhumongous_chunktype(chunktype), "invalid chunk type."); 137 size_t size = 0; 138 if (is_class) { 139 switch(chunktype) { 140 case SpecializedIndex: size = ClassSpecializedChunk; break; 141 case SmallIndex: size = ClassSmallChunk; break; 142 case MediumIndex: size = ClassMediumChunk; break; 143 default: 144 ShouldNotReachHere(); 145 } 146 } else { 147 switch(chunktype) { 148 case SpecializedIndex: size = SpecializedChunk; break; 149 case SmallIndex: size = SmallChunk; break; 150 case MediumIndex: size = MediumChunk; break; 151 default: 152 ShouldNotReachHere(); 153 } 154 } 155 return size; 156 } 157 158 ChunkIndex get_chunk_type_by_size(size_t size, bool is_class) { 159 if (is_class) { 160 if (size == ClassSpecializedChunk) { 161 return SpecializedIndex; 162 } else if (size == ClassSmallChunk) { 163 return SmallIndex; 164 } else if (size == ClassMediumChunk) { 165 return MediumIndex; 166 } else if (size > ClassMediumChunk) { 167 // A valid humongous chunk size is a multiple of the smallest chunk size. 168 assert(is_aligned(size, ClassSpecializedChunk), "Invalid chunk size"); 169 return HumongousIndex; 170 } 171 } else { 172 if (size == SpecializedChunk) { 173 return SpecializedIndex; 174 } else if (size == SmallChunk) { 175 return SmallIndex; 176 } else if (size == MediumChunk) { 177 return MediumIndex; 178 } else if (size > MediumChunk) { 179 // A valid humongous chunk size is a multiple of the smallest chunk size. 180 assert(is_aligned(size, SpecializedChunk), "Invalid chunk size"); 181 return HumongousIndex; 182 } 183 } 184 ShouldNotReachHere(); 185 return (ChunkIndex)-1; 186 } 187 188 ChunkIndex next_chunk_index(ChunkIndex i) { 189 assert(i < NumberOfInUseLists, "Out of bound"); 190 return (ChunkIndex) (i+1); 191 } 192 193 ChunkIndex prev_chunk_index(ChunkIndex i) { 194 assert(i > ZeroIndex, "Out of bound"); 195 return (ChunkIndex) (i-1); 196 } 197 198 199 } // namespace metaspace 200