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