8170860: Get rid of the humanReadableByteCount() method in openjdk/hotspot

Reviewed-by: mgerdin, mchernov
This commit is contained in:
Dmitry Fazunenko 2016-12-22 10:17:18 +00:00
parent 168244b3dc
commit f82263efb8
4 changed files with 41 additions and 44 deletions

View File

@ -27,7 +27,7 @@
* @requires vm.gc.G1
* @summary Verify that heap shrinks after GC in the presence of fragmentation
* due to humongous objects
* @library /test/lib
* @library /test/lib /
* @modules java.base/jdk.internal.misc
* @modules java.management/sun.management
* @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10
@ -40,6 +40,8 @@ import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.List;
import java.text.NumberFormat;
import gc.testlibrary.Helpers;
import static jdk.test.lib.Asserts.*;
public class TestHumongousShrinkHeap {
@ -70,9 +72,9 @@ public class TestHumongousShrinkHeap {
System.out.format("Running with %s initial heap size of %s maximum heap size. "
+ "Will allocate humongous object of %s size %d times.%n",
MemoryUsagePrinter.humanReadableByteCount(TOTAL_MEMORY, false),
MemoryUsagePrinter.humanReadableByteCount(MAX_MEMORY, false),
MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),
MemoryUsagePrinter.NF.format(TOTAL_MEMORY),
MemoryUsagePrinter.NF.format(MAX_MEMORY),
MemoryUsagePrinter.NF.format(HUMON_SIZE),
HUMON_COUNT
);
new TestHumongousShrinkHeap().test();
@ -134,24 +136,16 @@ public class TestHumongousShrinkHeap {
*/
class MemoryUsagePrinter {
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) {
return bytes + " B";
}
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
public static final NumberFormat NF = Helpers.numberFormatter();
public static void printMemoryUsage(String label) {
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
label,
humanReadableByteCount(memusage.getInit(), false),
humanReadableByteCount(memusage.getUsed(), false),
humanReadableByteCount(memusage.getCommitted(), false),
NF.format(memusage.getInit()),
NF.format(memusage.getUsed()),
NF.format(memusage.getCommitted()),
freeratio * 100
);
}

View File

@ -31,7 +31,7 @@
* "..................................H"
* 3. invoke gc and check that memory returned to the system (amount of committed memory got down)
*
* @library /test/lib
* @library /test/lib /
* @modules java.base/jdk.internal.misc
* java.management/sun.management
*/
@ -39,10 +39,12 @@ import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.List;
import java.text.NumberFormat;
import static jdk.test.lib.Asserts.*;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import com.sun.management.HotSpotDiagnosticMXBean;
import gc.testlibrary.Helpers;
public class TestShrinkDefragmentedHeap {
// Since we store all the small objects, they become old and old regions are also allocated at the bottom of the heap
@ -114,8 +116,8 @@ public class TestShrinkDefragmentedHeap {
private void allocate() {
System.out.format("Will allocate objects of small size = %s and humongous size = %s",
MemoryUsagePrinter.humanReadableByteCount(SMALL_OBJS_SIZE, false),
MemoryUsagePrinter.humanReadableByteCount(HUMONG_OBJS_SIZE, false)
MemoryUsagePrinter.NF.format(SMALL_OBJS_SIZE),
MemoryUsagePrinter.NF.format(HUMONG_OBJS_SIZE)
);
for (int i = 0; i < ALLOCATE_COUNT; i++) {
@ -170,24 +172,16 @@ public class TestShrinkDefragmentedHeap {
*/
static class MemoryUsagePrinter {
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) {
return bytes + " B";
}
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
public static final NumberFormat NF = Helpers.numberFormatter();
public static void printMemoryUsage(String label) {
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
label,
humanReadableByteCount(memusage.getInit(), false),
humanReadableByteCount(memusage.getUsed(), false),
humanReadableByteCount(memusage.getCommitted(), false),
NF.format(memusage.getInit()),
NF.format(memusage.getUsed()),
NF.format(memusage.getCommitted()),
freeratio * 100
);
}

View File

@ -28,15 +28,17 @@
* @summary Verify that the heap shrinks after full GC according to the current values of the Min/MaxHeapFreeRatio flags
* @modules java.base/jdk.internal.misc
* @modules jdk.management
* @library /test/lib
* @library /test/lib /
* @run main/othervm -XX:+UseAdaptiveSizePolicyWithSystemGC -XX:+UseParallelGC -XX:MinHeapFreeRatio=0 -XX:MaxHeapFreeRatio=100 -Xmx1g -verbose:gc TestDynShrinkHeap
*/
import jdk.test.lib.DynamicVMOption;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.text.NumberFormat;
import static jdk.test.lib.Asserts.assertLessThan;
import com.sun.management.HotSpotDiagnosticMXBean;
import gc.testlibrary.Helpers;
public class TestDynShrinkHeap {
@ -101,24 +103,16 @@ public class TestDynShrinkHeap {
*/
class MemoryUsagePrinter {
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) {
return bytes + " B";
}
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
public static final NumberFormat NF = Helpers.numberFormatter();
public static void printMemoryUsage(String label) {
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
label,
humanReadableByteCount(memusage.getInit(), true),
humanReadableByteCount(memusage.getUsed(), true),
humanReadableByteCount(memusage.getCommitted(), true),
NF.format(memusage.getInit()),
NF.format(memusage.getUsed()),
NF.format(memusage.getCommitted()),
freeratio * 100
);
}

View File

@ -31,6 +31,9 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
public class Helpers {
@ -320,4 +323,16 @@ public class Helpers {
}
}
/**
* @return a number formatter instance which prints numbers in a human
* readable form, like 9_223_372_036_854_775_807.
*/
public static NumberFormat numberFormatter() {
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
dfs.setGroupingSeparator('_');
dfs.setDecimalSeparator('.');
df.setDecimalFormatSymbols(dfs);
return df;
}
}