Fri, 18 Jan 2019 17:05:41 +0100
8217315: Proper units should print more significant digits
Reviewed-by: stuefe, tschatzl
1.1 --- a/src/share/vm/prims/jni.cpp Thu May 02 17:12:38 2019 +0100 1.2 +++ b/src/share/vm/prims/jni.cpp Fri Jan 18 17:05:41 2019 +0100 1.3 @@ -5121,6 +5121,7 @@ 1.4 run_unit_test(TestMetachunk_test()); 1.5 run_unit_test(TestVirtualSpaceNode_test()); 1.6 run_unit_test(GlobalDefinitions::test_globals()); 1.7 + run_unit_test(GlobalDefinitions::test_proper_unit()); 1.8 run_unit_test(GCTimerAllTest::all()); 1.9 run_unit_test(arrayOopDesc::test_max_array_length()); 1.10 run_unit_test(CollectedHeap::test_is_in());
2.1 --- a/src/share/vm/utilities/globalDefinitions.cpp Thu May 02 17:12:38 2019 +0100 2.2 +++ b/src/share/vm/utilities/globalDefinitions.cpp Fri Jan 18 17:05:41 2019 +0100 2.3 @@ -384,4 +384,67 @@ 2.4 } 2.5 } 2.6 2.7 +#define EXPECT_EQ(expected, actual) \ 2.8 + assert(expected == actual, "Test failed"); 2.9 +#define EXPECT_STREQ(expected, actual) \ 2.10 + assert(strcmp(expected, actual) == 0, "Test failed"); 2.11 + 2.12 +void GlobalDefinitions::test_proper_unit() { 2.13 + EXPECT_EQ(0u, byte_size_in_proper_unit(0u)); 2.14 + EXPECT_STREQ("B", proper_unit_for_byte_size(0u)); 2.15 + 2.16 + EXPECT_EQ(1u, byte_size_in_proper_unit(1u)); 2.17 + EXPECT_STREQ("B", proper_unit_for_byte_size(1u)); 2.18 + 2.19 + EXPECT_EQ(1023u, byte_size_in_proper_unit(K - 1)); 2.20 + EXPECT_STREQ("B", proper_unit_for_byte_size(K - 1)); 2.21 + 2.22 + EXPECT_EQ(1024u, byte_size_in_proper_unit(K)); 2.23 + EXPECT_STREQ("B", proper_unit_for_byte_size(K)); 2.24 + 2.25 + EXPECT_EQ(1025u, byte_size_in_proper_unit(K + 1)); 2.26 + EXPECT_STREQ("B", proper_unit_for_byte_size(K + 1)); 2.27 + 2.28 + EXPECT_EQ(51200u, byte_size_in_proper_unit(50*K)); 2.29 + EXPECT_STREQ("B", proper_unit_for_byte_size(50*K)); 2.30 + 2.31 + EXPECT_EQ(1023u, byte_size_in_proper_unit(M - 1)); 2.32 + EXPECT_STREQ("K", proper_unit_for_byte_size(M - 1)); 2.33 + 2.34 + EXPECT_EQ(1024u, byte_size_in_proper_unit(M)); 2.35 + EXPECT_STREQ("K", proper_unit_for_byte_size(M)); 2.36 + 2.37 + EXPECT_EQ(1024u, byte_size_in_proper_unit(M + 1)); 2.38 + EXPECT_STREQ("K", proper_unit_for_byte_size(M + 1)); 2.39 + 2.40 + EXPECT_EQ(1025u, byte_size_in_proper_unit(M + K)); 2.41 + EXPECT_STREQ("K", proper_unit_for_byte_size(M + K)); 2.42 + 2.43 + EXPECT_EQ(51200u, byte_size_in_proper_unit(50*M)); 2.44 + EXPECT_STREQ("K", proper_unit_for_byte_size(50*M)); 2.45 + 2.46 +#ifdef _LP64 2.47 + EXPECT_EQ(1023u, byte_size_in_proper_unit(G - 1)); 2.48 + EXPECT_STREQ("M", proper_unit_for_byte_size(G - 1)); 2.49 + 2.50 + EXPECT_EQ(1024u, byte_size_in_proper_unit(G)); 2.51 + EXPECT_STREQ("M", proper_unit_for_byte_size(G)); 2.52 + 2.53 + EXPECT_EQ(1024u, byte_size_in_proper_unit(G + 1)); 2.54 + EXPECT_STREQ("M", proper_unit_for_byte_size(G + 1)); 2.55 + 2.56 + EXPECT_EQ(1024u, byte_size_in_proper_unit(G + K)); 2.57 + EXPECT_STREQ("M", proper_unit_for_byte_size(G + K)); 2.58 + 2.59 + EXPECT_EQ(1025u, byte_size_in_proper_unit(G + M)); 2.60 + EXPECT_STREQ("M", proper_unit_for_byte_size(G + M)); 2.61 + 2.62 + EXPECT_EQ(51200u, byte_size_in_proper_unit(50*G)); 2.63 + EXPECT_STREQ("M", proper_unit_for_byte_size(50*G)); 2.64 +#endif 2.65 +} 2.66 + 2.67 +#undef EXPECT_EQ 2.68 +#undef EXPECT_STREQ 2.69 + 2.70 #endif // PRODUCT
3.1 --- a/src/share/vm/utilities/globalDefinitions.hpp Thu May 02 17:12:38 2019 +0100 3.2 +++ b/src/share/vm/utilities/globalDefinitions.hpp Fri Jan 18 17:05:41 2019 +0100 3.3 @@ -211,15 +211,20 @@ 3.4 const jlong NANOSECS_PER_SEC = CONST64(1000000000); 3.5 const jint NANOSECS_PER_MILLISEC = 1000000; 3.6 3.7 +// Proper units routines try to maintain at least three significant digits. 3.8 +// In worst case, it would print five significant digits with lower prefix. 3.9 +// G is close to MAX_SIZE on 32-bit platforms, so its product can easily overflow, 3.10 +// and therefore we need to be careful. 3.11 + 3.12 inline const char* proper_unit_for_byte_size(size_t s) { 3.13 #ifdef _LP64 3.14 - if (s >= 10*G) { 3.15 + if (s >= 100*G) { 3.16 return "G"; 3.17 } 3.18 #endif 3.19 - if (s >= 10*M) { 3.20 + if (s >= 100*M) { 3.21 return "M"; 3.22 - } else if (s >= 10*K) { 3.23 + } else if (s >= 100*K) { 3.24 return "K"; 3.25 } else { 3.26 return "B"; 3.27 @@ -229,13 +234,13 @@ 3.28 template <class T> 3.29 inline T byte_size_in_proper_unit(T s) { 3.30 #ifdef _LP64 3.31 - if (s >= 10*G) { 3.32 + if (s >= 100*G) { 3.33 return (T)(s/G); 3.34 } 3.35 #endif 3.36 - if (s >= 10*M) { 3.37 + if (s >= 100*M) { 3.38 return (T)(s/M); 3.39 - } else if (s >= 10*K) { 3.40 + } else if (s >= 100*K) { 3.41 return (T)(s/K); 3.42 } else { 3.43 return s; 3.44 @@ -1486,6 +1491,7 @@ 3.45 class GlobalDefinitions { 3.46 public: 3.47 static void test_globals(); 3.48 + static void test_proper_unit(); 3.49 }; 3.50 3.51 #endif // PRODUCT