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 namespace internals { 36 37 DEBUG_ONLY(internal_statistics_t g_internal_statistics;) 38 39 // Print a size, in words, scaled. 40 void print_scaled_words(outputStream* st, size_t word_size, size_t scale, int width) { 41 print_human_readable_size(st, word_size * sizeof(MetaWord), scale, width); 42 } 43 44 // Convenience helper: prints a size value and a percentage. 45 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale, int width) { 46 print_scaled_words(st, word_size, scale, width); 47 st->print(" ("); 48 print_percentage(st, compare_word_size, word_size); 49 st->print(")"); 50 } 51 52 53 // Print a human readable size. 54 // byte_size: size, in bytes, to be printed. 55 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively, 56 // or 0, which means the best scale is choosen dynamically. 57 // width: printing width. 58 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale, int width) { 59 if (scale == 0) { 60 // Dynamic mode. Choose scale for this value. 61 if (byte_size == 0) { 62 // Zero values are printed as bytes. 63 scale = 1; 64 } else { 65 if (byte_size >= G) { 66 scale = G; 67 } else if (byte_size >= M) { 68 scale = M; 69 } else if (byte_size >= K) { 70 scale = K; 71 } else { 72 scale = 1; 73 } 74 } 75 return print_human_readable_size(st, byte_size, scale, width); 76 } 77 78 #ifdef ASSERT 79 assert(scale == 1 || scale == BytesPerWord || scale == K || scale == M || scale == G, "Invalid scale"); 80 // Special case: printing wordsize should only be done with word-sized values 81 if (scale == BytesPerWord) { 82 assert(byte_size % BytesPerWord == 0, "not word sized"); 83 } 84 #endif 85 86 if (scale == 1) { 87 st->print("%*" PRIuPTR " bytes", width, byte_size); 88 } else if (scale == BytesPerWord) { 89 st->print("%*" PRIuPTR " words", width, byte_size / BytesPerWord); 90 } else { 91 const char* display_unit = ""; 92 switch(scale) { 93 case 1: display_unit = "bytes"; break; 94 case BytesPerWord: display_unit = "words"; break; 95 case K: display_unit = "KB"; break; 96 case M: display_unit = "MB"; break; 97 case G: display_unit = "GB"; break; 98 default: 99 ShouldNotReachHere(); 100 } 101 float display_value = (float) byte_size / scale; 102 // Since we use width to display a number with two trailing digits, increase it a bit. 103 width += 3; 104 // Prevent very small but non-null values showing up as 0.00. 105 if (byte_size > 0 && display_value < 0.01f) { 106 st->print("%*s %s", width, "<0.01", display_unit); 107 } else { 108 st->print("%*.2f %s", width, display_value, display_unit); 109 } 110 } 111 } 112 113 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values 114 // larger than 99% but not 100% are displayed as ">100%". 115 void print_percentage(outputStream* st, size_t total, size_t part) { 116 if (total == 0) { 117 st->print(" ?%%"); 118 } else if (part == 0) { 119 st->print(" 0%%"); 120 } else if (part == total) { 121 st->print("100%%"); 122 } else { 123 // Note: clearly print very-small-but-not-0% and very-large-but-not-100% percentages. 124 float p = ((float)part / total) * 100.0f; 125 if (p < 1.0f) { 126 st->print(" <1%%"); 127 } else if (p > 99.0f){ 128 st->print(">99%%"); 129 } else { 130 st->print("%3.0f%%", p); 131 } 132 } 133 } 134 135 // Returns size of this chunk type. 136 size_t get_size_for_nonhumongous_chunktype(ChunkIndex chunktype, bool is_class) { 137 assert(is_valid_nonhumongous_chunktype(chunktype), "invalid chunk type."); 138 size_t size = 0; 139 if (is_class) { 140 switch(chunktype) { 141 case SpecializedIndex: size = ClassSpecializedChunk; break; 142 case SmallIndex: size = ClassSmallChunk; break; 143 case MediumIndex: size = ClassMediumChunk; break; 144 default: 145 ShouldNotReachHere(); 146 } 147 } else { 148 switch(chunktype) { 149 case SpecializedIndex: size = SpecializedChunk; break; 150 case SmallIndex: size = SmallChunk; break; 151 case MediumIndex: size = MediumChunk; break; 152 default: 153 ShouldNotReachHere(); 154 } 155 } 156 return size; 157 } 158 159 ChunkIndex get_chunk_type_by_size(size_t size, bool is_class) { 160 if (is_class) { 161 if (size == ClassSpecializedChunk) { 162 return SpecializedIndex; 163 } else if (size == ClassSmallChunk) { 164 return SmallIndex; 165 } else if (size == ClassMediumChunk) { 166 return MediumIndex; 167 } else if (size > ClassMediumChunk) { 168 // A valid humongous chunk size is a multiple of the smallest chunk size. 169 assert(is_aligned(size, ClassSpecializedChunk), "Invalid chunk size"); 170 return HumongousIndex; 171 } 172 } else { 173 if (size == SpecializedChunk) { 174 return SpecializedIndex; 175 } else if (size == SmallChunk) { 176 return SmallIndex; 177 } else if (size == MediumChunk) { 178 return MediumIndex; 179 } else if (size > MediumChunk) { 180 // A valid humongous chunk size is a multiple of the smallest chunk size. 181 assert(is_aligned(size, SpecializedChunk), "Invalid chunk size"); 182 return HumongousIndex; 183 } 184 } 185 ShouldNotReachHere(); 186 return (ChunkIndex)-1; 187 } 188 189 ChunkIndex next_chunk_index(ChunkIndex i) { 190 assert(i < NumberOfInUseLists, "Out of bound"); 191 return (ChunkIndex) (i+1); 192 } 193 194 ChunkIndex prev_chunk_index(ChunkIndex i) { 195 assert(i > ZeroIndex, "Out of bound"); 196 return (ChunkIndex) (i-1); 197 } 198 199 200 } // namespace internals 201 } // namespace metaspace