Skip to content

Commit 9ef987f

Browse files
committed
Fix and simplify ByteFormattingUtil.
It shows KB/MB/GB but calculated KiB/MiB/GiB.
1 parent 6e5cb30 commit 9ef987f

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ByteFormattingUtil.java

+12-24
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,23 @@
2525
package com.oracle.svm.hosted;
2626

2727
public class ByteFormattingUtil {
28-
private static final double BYTES_TO_KiB = 1024d;
29-
private static final double BYTES_TO_MiB = 1024d * 1024d;
30-
private static final double BYTES_TO_GiB = 1024d * 1024d * 1024d;
28+
private static final double BYTES_TO_KB = 1000d;
29+
private static final double BYTES_TO_MB = 1000d * 1000d;
30+
private static final double BYTES_TO_GB = 1000d * 1000d * 1000d;
3131

3232
public static String bytesToHuman(long bytes) {
33-
return bytesToHuman("%4.2f", bytes);
34-
}
35-
36-
public static String bytesToHuman(String format, long bytes) {
37-
if (bytes < BYTES_TO_KiB) {
38-
return String.format(format, (double) bytes) + "B";
39-
} else if (bytes < BYTES_TO_MiB) {
40-
return String.format(format, bytesToKiB(bytes)) + "kB";
41-
} else if (bytes < BYTES_TO_GiB) {
42-
return String.format(format, bytesToMiB(bytes)) + "MB";
33+
if (bytes < BYTES_TO_KB) {
34+
return toHuman((double) bytes, "B");
35+
} else if (bytes < BYTES_TO_MB) {
36+
return toHuman(bytes / BYTES_TO_KB, "KB");
37+
} else if (bytes < BYTES_TO_GB) {
38+
return toHuman(bytes / BYTES_TO_MB, "MB");
4339
} else {
44-
return String.format(format, bytesToGiB(bytes)) + "GB";
40+
return toHuman(bytes / BYTES_TO_GB, "GB");
4541
}
4642
}
4743

48-
static double bytesToKiB(long bytes) {
49-
return bytes / BYTES_TO_KiB;
50-
}
51-
52-
static double bytesToGiB(long bytes) {
53-
return bytes / BYTES_TO_GiB;
54-
}
55-
56-
static double bytesToMiB(long bytes) {
57-
return bytes / BYTES_TO_MiB;
44+
private static String toHuman(double value, String unit) {
45+
return "%.2f%s".formatted(value, unit);
5846
}
5947
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ProgressReporter.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ private void printResourceInfo() {
447447

448448
l().printLineSeparator();
449449
l().yellowBold().doclink("Build resources", "#glossary-build-resources").a(":").reset().println();
450-
l().a(" - %.2fGB of memory (%.1f%% of system memory, reason: %s)", ByteFormattingUtil.bytesToGiB(maxMemory), Utils.toPercentage(maxMemory, totalMemorySize), memoryUsageReason).println();
450+
l().a(" - %s of memory (%.1f%% of system memory, reason: %s)", ByteFormattingUtil.bytesToHuman(maxMemory), Utils.toPercentage(maxMemory, totalMemorySize), memoryUsageReason).println();
451451
l().a(" - %s thread(s) (%.1f%% of %s available processor(s), %s)",
452452
maxNumberOfThreads, Utils.toPercentage(maxNumberOfThreads, availableProcessors), availableProcessors, maxNumberOfThreadsSuffix).println();
453453
}
@@ -838,7 +838,7 @@ private void printResourceStatistics() {
838838
.doclink("GCs", "#glossary-garbage-collections");
839839
long peakRSS = ProgressReporterCHelper.getPeakRSS();
840840
if (peakRSS >= 0) {
841-
p.a(" | ").doclink("Peak RSS", "#glossary-peak-rss").a(": ").a("%.2fGB", ByteFormattingUtil.bytesToGiB(peakRSS));
841+
p.a(" | ").doclink("Peak RSS", "#glossary-peak-rss").a(": ").a(ByteFormattingUtil.bytesToHuman(peakRSS));
842842
}
843843
recordJsonMetric(ResourceUsageKey.PEAK_RSS, (peakRSS >= 0 ? peakRSS : UNAVAILABLE_METRIC));
844844
long processCPUTime = getOperatingSystemMXBean().getProcessCpuTime();
@@ -863,7 +863,7 @@ private void checkForExcessiveGarbageCollection() {
863863
.a(": %.1fs spent in %d GCs during the last stage, taking up %.2f%% of the time.",
864864
Utils.millisToSeconds(gcTimeDeltaMillis), currentGCStats.totalCount - lastGCStats.totalCount, ratio * 100)
865865
.println();
866-
l().a(" Please ensure more than %.2fGB of memory is available for Native Image", ByteFormattingUtil.bytesToGiB(ProgressReporterCHelper.getPeakRSS())).println();
866+
l().a(" Please ensure more than %s of memory is available for Native Image", ByteFormattingUtil.bytesToHuman(ProgressReporterCHelper.getPeakRSS())).println();
867867
l().a(" to reduce GC overhead and improve image build time.").println();
868868
}
869869
lastGCStats = currentGCStats;
@@ -899,8 +899,8 @@ private static double nanosToSeconds(double nanos) {
899899
return nanos / NANOS_TO_SECONDS;
900900
}
901901

902-
private static double getUsedMemory() {
903-
return ByteFormattingUtil.bytesToGiB(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
902+
private static String getUsedMemory() {
903+
return ByteFormattingUtil.bytesToHuman(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
904904
}
905905

906906
private static String stringFilledWith(int size, String fill) {
@@ -1229,7 +1229,7 @@ void end(double totalTime) {
12291229
a("]").reset();
12301230
}
12311231

1232-
String suffix = String.format("(%.1fs @ %.2fGB)", Utils.millisToSeconds(totalTime), Utils.getUsedMemory());
1232+
String suffix = String.format("(%.1fs @ %s)", Utils.millisToSeconds(totalTime), Utils.getUsedMemory());
12331233
int textLength = getCurrentTextLength();
12341234
// TODO: `assert textLength > 0;` should be used here but tests do not start stages
12351235
// properly (GR-35721)

0 commit comments

Comments
 (0)