< prev index next >

src/hotspot/share/utilities/ostream.cpp

Print this page
rev 49926 : [mq]: 8201572-improve-metaspace-reporting

@@ -204,10 +204,14 @@
 
 void outputStream::cr() {
   this->write("\n", 1);
 }
 
+void outputStream::cr_indent() {
+  cr(); indent();
+}
+
 void outputStream::stamp() {
   if (! _stamp.is_updated()) {
     _stamp.update(); // start at 0 on first call to stamp()
   }
 

@@ -301,10 +305,92 @@
       cr();
     }
   }
 }
 
+// Print a human readable size.
+// byte_size: size, in bytes, to be printed.
+// scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,
+//         or 0, which means the best scale is choosen dynamically.
+// width: printing width.
+void outputStream::print_human_readable_size(size_t byte_size, size_t scale, int width)  {
+  if (scale == 0) {
+    // Dynamic mode. Choose scale for this value.
+    if (byte_size == 0) {
+      // Zero values are printed as bytes.
+      scale = 1;
+    } else {
+      if (byte_size >= G) {
+        scale = G;
+      } else if (byte_size >= M) {
+        scale = M;
+      } else if (byte_size >= K) {
+        scale = K;
+      } else {
+        scale = 1;
+      }
+    }
+    return print_human_readable_size(byte_size, scale, width);
+  }
+
+#ifdef ASSERT
+  assert(scale == 1 || scale == BytesPerWord || scale == K || scale == M || scale == G, "Invalid scale");
+  // Special case: printing wordsize should only be done with word-sized values
+  if (scale == BytesPerWord) {
+    assert(byte_size % BytesPerWord == 0, "not word sized");
+  }
+#endif
+
+  if (scale == 1) {
+    print("%*" PRIuPTR " bytes", width, byte_size);
+  } else if (scale == BytesPerWord) {
+    print("%*" PRIuPTR " words", width, byte_size / BytesPerWord);
+  } else {
+    const char* display_unit = "";
+    switch(scale) {
+      case 1: display_unit = "bytes"; break;
+      case BytesPerWord: display_unit = "words"; break;
+      case K: display_unit = "KB"; break;
+      case M: display_unit = "MB"; break;
+      case G: display_unit = "GB"; break;
+      default:
+        ShouldNotReachHere();
+    }
+    float display_value = (float) byte_size / scale;
+    // Since we use width to display a number with two trailing digits, increase it a bit.
+    width += 3;
+    // Prevent very small but non-null values showing up as 0.00.
+    if (byte_size > 0 && display_value < 0.01f) {
+      print("%*s %s", width, "<0.01", display_unit);
+    } else {
+      print("%*.2f %s", width, display_value, display_unit);
+    }
+  }
+}
+
+// Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
+// larger than 99% but not 100% are displayed as ">100%".
+void outputStream::print_percentage(size_t total, size_t part) {
+  if (total == 0) {
+    print("  ?%%");
+  } else if (part == 0) {
+    print("  0%%");
+  } else if (part == total) {
+    print("100%%");
+  } else {
+    // Note: clearly print very-small-but-not-0% and very-large-but-not-100% percentages.
+    float p = ((float)part / total) * 100.0f;
+    if (p < 1.0f) {
+      print(" <1%%");
+    } else if (p > 99.0f){
+      print(">99%%");
+    } else {
+      print("%3.0f%%", p);
+    }
+  }
+}
+
 stringStream::stringStream(size_t initial_size) : outputStream() {
   buffer_length = initial_size;
   buffer        = NEW_RESOURCE_ARRAY(char, buffer_length);
   buffer_pos    = 0;
   buffer_fixed  = false;
< prev index next >