8217315: Proper units should print more significant digits
Reviewed-by: stuefe, tschatzl
--- a/src/hotspot/share/utilities/globalDefinitions.hpp Fri Jan 18 16:40:24 2019 +0100
+++ b/src/hotspot/share/utilities/globalDefinitions.hpp Fri Jan 18 17:05:41 2019 +0100
@@ -230,15 +230,20 @@
const jlong NANOSECS_PER_SEC = CONST64(1000000000);
const jint NANOSECS_PER_MILLISEC = 1000000;
+// Proper units routines try to maintain at least three significant digits.
+// In worst case, it would print five significant digits with lower prefix.
+// G is close to MAX_SIZE on 32-bit platforms, so its product can easily overflow,
+// and therefore we need to be careful.
+
inline const char* proper_unit_for_byte_size(size_t s) {
#ifdef _LP64
- if (s >= 10*G) {
+ if (s >= 100*G) {
return "G";
}
#endif
- if (s >= 10*M) {
+ if (s >= 100*M) {
return "M";
- } else if (s >= 10*K) {
+ } else if (s >= 100*K) {
return "K";
} else {
return "B";
@@ -248,13 +253,13 @@
template <class T>
inline T byte_size_in_proper_unit(T s) {
#ifdef _LP64
- if (s >= 10*G) {
+ if (s >= 100*G) {
return (T)(s/G);
}
#endif
- if (s >= 10*M) {
+ if (s >= 100*M) {
return (T)(s/M);
- } else if (s >= 10*K) {
+ } else if (s >= 100*K) {
return (T)(s/K);
} else {
return s;
--- a/test/hotspot/gtest/utilities/test_globalDefinitions.cpp Fri Jan 18 16:40:24 2019 +0100
+++ b/test/hotspot/gtest/utilities/test_globalDefinitions.cpp Fri Jan 18 17:05:41 2019 +0100
@@ -93,6 +93,61 @@
}
}
+TEST(globalDefinitions, proper_unit) {
+ EXPECT_EQ(0u, byte_size_in_proper_unit(0u));
+ EXPECT_STREQ("B", proper_unit_for_byte_size(0u));
+
+ EXPECT_EQ(1u, byte_size_in_proper_unit(1u));
+ EXPECT_STREQ("B", proper_unit_for_byte_size(1u));
+
+ EXPECT_EQ(1023u, byte_size_in_proper_unit(K - 1));
+ EXPECT_STREQ("B", proper_unit_for_byte_size(K - 1));
+
+ EXPECT_EQ(1024u, byte_size_in_proper_unit(K));
+ EXPECT_STREQ("B", proper_unit_for_byte_size(K));
+
+ EXPECT_EQ(1025u, byte_size_in_proper_unit(K + 1));
+ EXPECT_STREQ("B", proper_unit_for_byte_size(K + 1));
+
+ EXPECT_EQ(51200u, byte_size_in_proper_unit(50*K));
+ EXPECT_STREQ("B", proper_unit_for_byte_size(50*K));
+
+ EXPECT_EQ(1023u, byte_size_in_proper_unit(M - 1));
+ EXPECT_STREQ("K", proper_unit_for_byte_size(M - 1));
+
+ EXPECT_EQ(1024u, byte_size_in_proper_unit(M));
+ EXPECT_STREQ("K", proper_unit_for_byte_size(M));
+
+ EXPECT_EQ(1024u, byte_size_in_proper_unit(M + 1));
+ EXPECT_STREQ("K", proper_unit_for_byte_size(M + 1));
+
+ EXPECT_EQ(1025u, byte_size_in_proper_unit(M + K));
+ EXPECT_STREQ("K", proper_unit_for_byte_size(M + K));
+
+ EXPECT_EQ(51200u, byte_size_in_proper_unit(50*M));
+ EXPECT_STREQ("K", proper_unit_for_byte_size(50*M));
+
+#ifdef _LP64
+ EXPECT_EQ(1023u, byte_size_in_proper_unit(G - 1));
+ EXPECT_STREQ("M", proper_unit_for_byte_size(G - 1));
+
+ EXPECT_EQ(1024u, byte_size_in_proper_unit(G));
+ EXPECT_STREQ("M", proper_unit_for_byte_size(G));
+
+ EXPECT_EQ(1024u, byte_size_in_proper_unit(G + 1));
+ EXPECT_STREQ("M", proper_unit_for_byte_size(G + 1));
+
+ EXPECT_EQ(1024u, byte_size_in_proper_unit(G + K));
+ EXPECT_STREQ("M", proper_unit_for_byte_size(G + K));
+
+ EXPECT_EQ(1025u, byte_size_in_proper_unit(G + M));
+ EXPECT_STREQ("M", proper_unit_for_byte_size(G + M));
+
+ EXPECT_EQ(51200u, byte_size_in_proper_unit(50*G));
+ EXPECT_STREQ("M", proper_unit_for_byte_size(50*G));
+#endif
+}
+
TEST(globalDefinitions, exact_unit_for_byte_size) {
EXPECT_STREQ("B", exact_unit_for_byte_size(0));
EXPECT_STREQ("B", exact_unit_for_byte_size(1));