Skip to content

Commit b688237

Browse files
committed
Fix spacing in the Top 10 {origins of code area,object types in image heap} section
* The extra 'i' in the unit means we need one more for the width. * It also means we should compare to multiples of 1000 to avoid increasing the width further.
1 parent d985f57 commit b688237

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,45 @@
2525
package com.oracle.svm.hosted;
2626

2727
public class ByteFormattingUtil {
28-
private static final long KiB_TO_BYTES = 1024L;
29-
private static final long MiB_TO_BYTES = 1024L * KiB_TO_BYTES;
30-
private static final long GiB_TO_BYTES = 1024L * MiB_TO_BYTES;
28+
// "123.12KiB".length() = 9, holds as long as it's not >= 1000GiB
29+
private static final int MAX_WIDTH = 9;
30+
public static final String RIGHT_ALIGNED_FORMAT = "%" + MAX_WIDTH + "s";
3131

32+
private enum Unit {
33+
KiB(1024L),
34+
MiB(1024L * 1024L),
35+
GiB(1024L * 1024L * 1024L);
36+
37+
private final long value;
38+
39+
Unit(long value) {
40+
this.value = value;
41+
}
42+
}
43+
44+
// We want to respect MAX_WIDTH and keep it concise,
45+
// so we prefer to show 0.99MiB than 1010.00KiB (length 10).
3246
public static String bytesToHuman(long bytes) {
3347
assert bytes >= 0;
34-
if (bytes < KiB_TO_BYTES) {
48+
if (bytes < 1_000) {
3549
return bytes + "B";
36-
} else if (bytes < MiB_TO_BYTES) {
37-
return toHuman((double) bytes / KiB_TO_BYTES, "KiB");
38-
} else if (bytes < GiB_TO_BYTES) {
39-
return toHuman((double) bytes / MiB_TO_BYTES, "MiB");
50+
} else if (bytes < 1_000 * Unit.KiB.value) {
51+
return toHuman(bytes, Unit.KiB);
52+
} else if (bytes < 1_000 * Unit.MiB.value) {
53+
return toHuman(bytes, Unit.MiB);
4054
} else {
4155
return bytesToHumanGB(bytes);
4256
}
4357
}
4458

4559
public static String bytesToHumanGB(long bytes) {
46-
return toHuman((double) bytes / GiB_TO_BYTES, "GiB");
60+
return toHuman(bytes, Unit.GiB);
4761
}
4862

49-
private static String toHuman(double value, String unit) {
50-
return "%.2f%s".formatted(value, unit);
63+
private static String toHuman(long value, Unit unit) {
64+
String string = "%.2f%s".formatted((double) value / unit.value, unit);
65+
assert string.length() <= MAX_WIDTH || value >= 1000L * Unit.GiB.value;
66+
return string;
5167
}
5268

5369
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public class ProgressReporter {
123123
public static final String DOCS_BASE_URL = "https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md";
124124
private static final double EXCESSIVE_GC_MIN_THRESHOLD_MILLIS = TimeUtils.secondsToMillis(15);
125125
private static final double EXCESSIVE_GC_RATIO = 0.5;
126+
// Use a leading space like in the rest of Native Image output
127+
private static final String BYTES_TO_HUMAN_FORMAT = " " + ByteFormattingUtil.RIGHT_ALIGNED_FORMAT;
126128

127129
private final NativeImageSystemIOWrappers builderIO;
128130

@@ -598,7 +600,7 @@ public void printCreationEnd(int imageFileSize, int heapObjectCount, long imageH
598600
Timer archiveTimer = getTimer(TimerCollection.Registry.ARCHIVE_LAYER);
599601
stagePrinter.end(imageTimer.getTotalTime() + writeTimer.getTotalTime() + archiveTimer.getTotalTime());
600602
creationStageEndCompleted = true;
601-
String format = "%9s (%5.2f%%) for ";
603+
String format = BYTES_TO_HUMAN_FORMAT + " (%5.2f%%) for ";
602604
l().a(format, ByteFormattingUtil.bytesToHuman(codeAreaSize), ProgressReporterUtils.toPercentage(codeAreaSize, imageFileSize))
603605
.doclink("code area", "#glossary-code-area").a(":%,10d compilation units", numCompilations).println();
604606
int numResources = 0;
@@ -632,7 +634,7 @@ public void printCreationEnd(int imageFileSize, int heapObjectCount, long imageH
632634
recordJsonMetric(ImageDetailKey.NUM_COMP_UNITS, numCompilations);
633635
l().a(format, ByteFormattingUtil.bytesToHuman(otherBytes), ProgressReporterUtils.toPercentage(otherBytes, imageFileSize))
634636
.doclink("other data", "#glossary-other-data").println();
635-
l().a("%9s in total image size", ByteFormattingUtil.bytesToHuman(imageFileSize));
637+
l().a(BYTES_TO_HUMAN_FORMAT + " in total image size", ByteFormattingUtil.bytesToHuman(imageFileSize));
636638
if (imageDiskFileSize >= 0) {
637639
l().a(", %s in total file size", ByteFormattingUtil.bytesToHuman(imageDiskFileSize));
638640
}
@@ -723,14 +725,15 @@ private void printBreakdowns() {
723725
int numHeapItems = heapBreakdown.getSortedBreakdownEntries().size();
724726
long totalCodeBytes = codeBreakdown.values().stream().mapToLong(Long::longValue).sum();
725727

726-
p.l().a(String.format("%9s for %s more packages", ByteFormattingUtil.bytesToHuman(totalCodeBytes - printedCodeBytes), numCodeItems - printedCodeItems))
728+
p.l().a(String.format(BYTES_TO_HUMAN_FORMAT + " for %s more packages", ByteFormattingUtil.bytesToHuman(totalCodeBytes - printedCodeBytes), numCodeItems - printedCodeItems))
727729
.jumpToMiddle()
728-
.a(String.format("%9s for %s more object types", ByteFormattingUtil.bytesToHuman(heapBreakdown.getTotalHeapSize() - printedHeapBytes), numHeapItems - printedHeapItems))
730+
.a(String.format(BYTES_TO_HUMAN_FORMAT + " for %s more object types", ByteFormattingUtil.bytesToHuman(heapBreakdown.getTotalHeapSize() - printedHeapBytes),
731+
numHeapItems - printedHeapItems))
729732
.flushln();
730733
}
731734

732735
private static String getBreakdownSizeString(long sizeInBytes) {
733-
return String.format("%9s ", ByteFormattingUtil.bytesToHuman(sizeInBytes));
736+
return String.format(BYTES_TO_HUMAN_FORMAT + " ", ByteFormattingUtil.bytesToHuman(sizeInBytes));
734737
}
735738

736739
private void printRecommendations() {

0 commit comments

Comments
 (0)