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 "utilities/globalDefinitions.hpp" 30 #include "utilities/ostream.hpp" 31 32 namespace metaspace { 33 namespace internals { 34 35 // Print a size, in words, scaled. 36 void print_scaled_words(outputStream* st, size_t word_size, size_t scale, int width) { 37 print_human_readable_size(st, word_size * sizeof(MetaWord), scale, width); 38 } 39 40 // Convenience helper: prints a size value and a percentage. 41 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale, int width) { 42 print_scaled_words(st, word_size, scale, width); 43 st->print(" ("); 44 print_percentage(st, compare_word_size, word_size); 45 st->print(")"); 46 } 47 48 49 // Print a human readable size. 50 // byte_size: size, in bytes, to be printed. 51 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively, 52 // or 0, which means the best scale is choosen dynamically. 53 // width: printing width. 54 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale, int width) { 55 if (scale == 0) { 56 // Dynamic mode. Choose scale for this value. 57 if (byte_size == 0) { 58 // Zero values are printed as bytes. 59 scale = 1; 60 } else { 61 if (byte_size >= G) { 62 scale = G; 63 } else if (byte_size >= M) { 64 scale = M; 65 } else if (byte_size >= K) { 66 scale = K; 67 } else { 68 scale = 1; 69 } 70 } 71 return print_human_readable_size(st, byte_size, scale, width); 72 } 73 74 #ifdef ASSERT 75 assert(scale == 1 || scale == BytesPerWord || scale == K || scale == M || scale == G, "Invalid scale"); 76 // Special case: printing wordsize should only be done with word-sized values 77 if (scale == BytesPerWord) { 78 assert(byte_size % BytesPerWord == 0, "not word sized"); 79 } 80 #endif 81 82 if (scale == 1) { 83 st->print("%*" PRIuPTR " bytes", width, byte_size); 84 } else if (scale == BytesPerWord) { 85 st->print("%*" PRIuPTR " words", width, byte_size / BytesPerWord); 86 } else { 87 const char* display_unit = ""; 88 switch(scale) { 89 case 1: display_unit = "bytes"; break; 90 case BytesPerWord: display_unit = "words"; break; 91 case K: display_unit = "KB"; break; 92 case M: display_unit = "MB"; break; 93 case G: display_unit = "GB"; break; 94 default: 95 ShouldNotReachHere(); 96 } 97 float display_value = (float) byte_size / scale; 98 // Since we use width to display a number with two trailing digits, increase it a bit. 99 width += 3; 100 // Prevent very small but non-null values showing up as 0.00. 101 if (byte_size > 0 && display_value < 0.01f) { 102 st->print("%*s %s", width, "<0.01", display_unit); 103 } else { 104 st->print("%*.2f %s", width, display_value, display_unit); 105 } 106 } 107 } 108 109 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values 110 // larger than 99% but not 100% are displayed as ">100%". 111 void print_percentage(outputStream* st, size_t total, size_t part) { 112 if (total == 0) { 113 st->print(" ?%%"); 114 } else if (part == 0) { 115 st->print(" 0%%"); 116 } else if (part == total) { 117 st->print("100%%"); 118 } else { 119 // Note: clearly print very-small-but-not-0% and very-large-but-not-100% percentages. 120 float p = ((float)part / total) * 100.0f; 121 if (p < 1.0f) { 122 st->print(" <1%%"); 123 } else if (p > 99.0f){ 124 st->print(">99%%"); 125 } else { 126 st->print("%3.0f%%", p); 127 } 128 } 129 } 130 131 } // namespace internals 132 } // namespace metaspace