Skip to content

Commit ab50d01

Browse files
committed
Ensure other use of HeapBreakdownEntry is unaffected by truncation
1 parent 51eded6 commit ab50d01

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

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

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected void calculate(BeforeImageWriteAccessImpl access, boolean resourcesAre
114114
}
115115
long objectSize = o.getSize();
116116
totalObjectSize += objectSize;
117-
classToDataMap.computeIfAbsent(o.getClazz(), c -> new HeapBreakdownEntry(c)).add(objectSize);
117+
classToDataMap.computeIfAbsent(o.getClazz(), HeapBreakdownEntry::of).add(objectSize);
118118
if (reportStringBytesConstant && o.getObject() instanceof String string) {
119119
byte[] bytes = getInternalByteArray(string);
120120
/* Ensure every byte[] is counted only once. */
@@ -140,24 +140,24 @@ protected void calculate(BeforeImageWriteAccessImpl access, boolean resourcesAre
140140
long heapAlignmentSize = getTotalHeapSize() - totalObjectSize;
141141
assert heapAlignmentSize >= 0 : "Incorrect heap alignment detected: " + heapAlignmentSize;
142142
if (heapAlignmentSize > 0) {
143-
HeapBreakdownEntry heapAlignmentEntry = new HeapBreakdownEntry("", "heap alignment", "#glossary-heap-alignment");
143+
HeapBreakdownEntry heapAlignmentEntry = HeapBreakdownEntry.of("", "heap alignment", "#glossary-heap-alignment");
144144
heapAlignmentEntry.add(heapAlignmentSize);
145145
entries.add(heapAlignmentEntry);
146146
}
147147

148148
/* Extract byte[] for Strings. */
149149
if (stringByteArrayTotalSize > 0) {
150-
addEntry(entries, byteArrayEntry, new HeapBreakdownEntry(BYTE_ARRAY_PREFIX + "string data"), stringByteArrayTotalSize, stringByteArrayTotalCount);
150+
addEntry(entries, byteArrayEntry, HeapBreakdownEntry.of(BYTE_ARRAY_PREFIX + "string data"), stringByteArrayTotalSize, stringByteArrayTotalCount);
151151
}
152152
/* Extract byte[] for code info. */
153153
List<Integer> codeInfoByteArrayLengths = CodeInfoTable.getCurrentLayerImageCodeCache().getTotalByteArrayLengths();
154154
long codeInfoSize = codeInfoByteArrayLengths.stream().map(l -> objectLayout.getArraySize(JavaKind.Byte, l, true)).reduce(0L, Long::sum);
155-
addEntry(entries, byteArrayEntry, new HeapBreakdownEntry(BYTE_ARRAY_PREFIX, "code metadata", "#glossary-code-metadata"), codeInfoSize, codeInfoByteArrayLengths.size());
155+
addEntry(entries, byteArrayEntry, HeapBreakdownEntry.of(BYTE_ARRAY_PREFIX, "code metadata", "#glossary-code-metadata"), codeInfoSize, codeInfoByteArrayLengths.size());
156156
/* Extract byte[] for metadata. */
157157
int metadataByteLength = ImageSingletons.lookup(RuntimeMetadataDecoder.class).getMetadataByteLength();
158158
if (metadataByteLength > 0) {
159159
long metadataSize = objectLayout.getArraySize(JavaKind.Byte, metadataByteLength, true);
160-
addEntry(entries, byteArrayEntry, new HeapBreakdownEntry(BYTE_ARRAY_PREFIX, "reflection metadata", "#glossary-reflection-metadata"), metadataSize, 1);
160+
addEntry(entries, byteArrayEntry, HeapBreakdownEntry.of(BYTE_ARRAY_PREFIX, "reflection metadata", "#glossary-reflection-metadata"), metadataSize, 1);
161161
}
162162
ProgressReporter reporter = ProgressReporter.singleton();
163163
long resourcesByteArraySize = 0;
@@ -177,19 +177,19 @@ protected void calculate(BeforeImageWriteAccessImpl access, boolean resourcesAre
177177
}
178178
}
179179
if (resourcesByteArraySize > 0) {
180-
addEntry(entries, byteArrayEntry, new HeapBreakdownEntry(BYTE_ARRAY_PREFIX, "embedded resources", "#glossary-embedded-resources"), resourcesByteArraySize, resourcesByteArrayCount);
180+
addEntry(entries, byteArrayEntry, HeapBreakdownEntry.of(BYTE_ARRAY_PREFIX, "embedded resources", "#glossary-embedded-resources"), resourcesByteArraySize, resourcesByteArrayCount);
181181
}
182182
}
183183
reporter.recordJsonMetric(ImageDetailKey.RESOURCE_SIZE_BYTES, resourcesByteArraySize);
184184
/* Extract byte[] for graph encodings. */
185185
if (graphEncodingByteLength >= 0) {
186186
long graphEncodingSize = objectLayout.getArraySize(JavaKind.Byte, graphEncodingByteLength, true);
187187
reporter.recordJsonMetric(ImageDetailKey.GRAPH_ENCODING_SIZE, graphEncodingSize);
188-
addEntry(entries, byteArrayEntry, new HeapBreakdownEntry(BYTE_ARRAY_PREFIX, "graph encodings", "#glossary-graph-encodings"), graphEncodingSize, 1);
188+
addEntry(entries, byteArrayEntry, HeapBreakdownEntry.of(BYTE_ARRAY_PREFIX, "graph encodings", "#glossary-graph-encodings"), graphEncodingSize, 1);
189189
}
190190
/* Add remaining byte[]. */
191191
assert byteArrayEntry.byteSize >= 0 && byteArrayEntry.count >= 0;
192-
addEntry(entries, byteArrayEntry, new HeapBreakdownEntry(BYTE_ARRAY_PREFIX, "general heap data", "#glossary-general-heap-data"), byteArrayEntry.byteSize, byteArrayEntry.count);
192+
addEntry(entries, byteArrayEntry, HeapBreakdownEntry.of(BYTE_ARRAY_PREFIX, "general heap data", "#glossary-general-heap-data"), byteArrayEntry.byteSize, byteArrayEntry.count);
193193
assert byteArrayEntry.byteSize == 0 && byteArrayEntry.count == 0;
194194
setBreakdownEntries(entries);
195195
}
@@ -209,27 +209,23 @@ private static byte[] getInternalByteArray(String string) {
209209
}
210210
}
211211

212-
public static class HeapBreakdownEntry {
213-
final HeapBreakdownLabel label;
212+
public abstract static class HeapBreakdownEntry {
214213
long byteSize;
215214
int count;
216215

217-
public HeapBreakdownEntry(HostedClass hostedClass) {
218-
this(ProgressReporter.moduleNamePrefix(hostedClass.getJavaClass().getModule()) +
219-
ProgressReporter.Utils.truncateFQN(hostedClass.toJavaName(true), 0.29));
216+
public static HeapBreakdownEntry of(HostedClass hostedClass) {
217+
return new HeapBreakdownEntryForClass(hostedClass.getJavaClass());
220218
}
221219

222-
public HeapBreakdownEntry(String name) {
223-
label = new SimpleHeapObjectKindName(name);
220+
public static HeapBreakdownEntry of(String name) {
221+
return new HeapBreakdownEntryFixed(new SimpleHeapObjectKindName(name));
224222
}
225223

226-
HeapBreakdownEntry(String prefix, String name, String htmlAnchor) {
227-
label = new LinkyHeapObjectKindName(prefix, name, htmlAnchor);
224+
public static HeapBreakdownEntry of(String prefix, String name, String htmlAnchor) {
225+
return new HeapBreakdownEntryFixed(new LinkyHeapObjectKindName(prefix, name, htmlAnchor));
228226
}
229227

230-
public HeapBreakdownLabel getLabel() {
231-
return label;
232-
}
228+
public abstract HeapBreakdownLabel getLabel(boolean truncate);
233229

234230
public long getByteSize() {
235231
return byteSize;
@@ -254,6 +250,39 @@ void remove(long subByteSize, int subCount) {
254250
}
255251
}
256252

253+
static class HeapBreakdownEntryFixed extends HeapBreakdownEntry {
254+
255+
private final HeapBreakdownLabel label;
256+
257+
HeapBreakdownEntryFixed(HeapBreakdownLabel label) {
258+
this.label = label;
259+
}
260+
261+
@Override
262+
public HeapBreakdownLabel getLabel(boolean unused) {
263+
return label;
264+
}
265+
}
266+
267+
static class HeapBreakdownEntryForClass extends HeapBreakdownEntry {
268+
269+
private final Class<?> clazz;
270+
271+
HeapBreakdownEntryForClass(Class<?> clazz) {
272+
this.clazz = clazz;
273+
}
274+
275+
@Override
276+
public HeapBreakdownLabel getLabel(boolean truncate) {
277+
if (truncate) {
278+
return new SimpleHeapObjectKindName(ProgressReporter.moduleNamePrefix(clazz.getModule()) +
279+
ProgressReporter.Utils.truncateFQN(clazz.getTypeName(), 0.29));
280+
} else {
281+
return new SimpleHeapObjectKindName(clazz.getTypeName());
282+
}
283+
}
284+
}
285+
257286
public interface HeapBreakdownLabel {
258287
String renderToString(LinkStrategy linkStrategy);
259288
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
import com.oracle.svm.core.VM;
7878
import com.oracle.svm.core.heap.Heap;
7979
import com.oracle.svm.core.hub.ClassForNameSupport;
80-
import com.oracle.svm.core.hub.DynamicHub;
8180
import com.oracle.svm.core.jdk.Resources;
8281
import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue;
8382
import com.oracle.svm.core.option.HostedOptionKey;
@@ -745,7 +744,7 @@ private void printBreakdowns() {
745744
/* <- 10% for module name -><- 29% for class FQN -> */
746745
if (typesBySizeInHeap.hasNext()) {
747746
HeapBreakdownProvider.HeapBreakdownEntry e = typesBySizeInHeap.next();
748-
String labelString = e.label.renderToString(linkStrategy);
747+
String labelString = e.getLabel(true).renderToString(linkStrategy);
749748
long byteSize = e.byteSize;
750749
heapSizePart = String.format("%9s %s", ByteFormattingUtil.bytesToHuman(byteSize), labelString);
751750
printedHeapBytes += byteSize;

web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/WebImageJSHeapBreakdownProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected void calculate(FeatureImpl.BeforeImageWriteAccessImpl access, boolean
5656
for (ConstantIdentityMapping.IdentityNode node : identityMapping.identityNodes()) {
5757
HostedClass type = (HostedClass) providers.getMetaAccess().lookupJavaType(node.getDefinition().getConstant());
5858
long size = node.getDefinition().getSize();
59-
objectTypeEntries.computeIfAbsent(type, HeapBreakdownEntry::new).add(size);
59+
objectTypeEntries.computeIfAbsent(type, HeapBreakdownEntry::of).add(size);
6060
totalByteSize += size;
6161
}
6262

0 commit comments

Comments
 (0)