Skip to content

Commit a2bc436

Browse files
committed
[GR-38965] Support heap dumps in GraalVM CE Native Image.
PullRequest: graal/11896
2 parents 72dada7 + c269e0f commit a2bc436

File tree

10 files changed

+2681
-1
lines changed

10 files changed

+2681
-1
lines changed

substratevm/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ This changelog summarizes major changes to GraalVM Native Image.
44

55
## Version 22.2.0
66
* (GR-20653) Re-enable the usage of all CPU features for JIT compilation on AMD64.
7-
* (GR-38413) Add support for -XX:+ExitOnOutOfMemoryError.
7+
* (GR-38413) Add support for `-XX:+ExitOnOutOfMemoryError`.
88
* (GR-37606) Add support for URLs and short descriptions to `Feature`. This info is shown as part of the build output.
9+
* (GR-38965) Heap dumps are now supported in Community Edition.
10+
* (GR-38951) Add `-XX:+DumpHeapAndExit` option to dump the initial heap of a native executable.
911

1012
## Version 22.1.0
1113
* (GR-36568) Add "Quick build" mode, enabled through option `-Ob`, for quicker native image builds.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.posix.heapdump;
26+
27+
import java.io.FileDescriptor;
28+
import java.io.FileOutputStream;
29+
import java.io.IOException;
30+
31+
import org.graalvm.nativeimage.ImageSingletons;
32+
import org.graalvm.nativeimage.Platform;
33+
import org.graalvm.nativeimage.Platforms;
34+
import org.graalvm.nativeimage.StackValue;
35+
import org.graalvm.nativeimage.c.type.CCharPointer;
36+
import org.graalvm.nativeimage.hosted.Feature;
37+
import org.graalvm.word.WordFactory;
38+
39+
import com.oracle.svm.core.annotate.AutomaticFeature;
40+
import com.oracle.svm.core.heapdump.HeapDumpWriterImpl.AllocationFreeFileOutputStream;
41+
import com.oracle.svm.core.posix.PosixUtils;
42+
import com.oracle.svm.core.posix.headers.Unistd;
43+
import com.oracle.svm.core.util.VMError;
44+
45+
/**
46+
* Posix implementation of allocation-free output stream created from FileOutputStream.
47+
*
48+
*/
49+
final class AllocationFreeFileOutputStreamPosix extends AllocationFreeFileOutputStream {
50+
51+
/**
52+
* Pre-allocated exceptions, for throwing from code that must not allocate.
53+
*/
54+
private static final IOException preallocatedIOException = new IOException("Write failed.");
55+
private static final ArrayIndexOutOfBoundsException preallocatedArrayIndexOutOfBoundsException = new ArrayIndexOutOfBoundsException();
56+
57+
private FileOutputStream fos;
58+
private FileDescriptor fileDescriptor;
59+
60+
AllocationFreeFileOutputStreamPosix() {
61+
62+
}
63+
64+
private AllocationFreeFileOutputStreamPosix(FileOutputStream fileOutputStream) throws IOException {
65+
fos = fileOutputStream;
66+
fileDescriptor = fos.getFD();
67+
68+
if (!(Platform.includedIn(Platform.LINUX.class) || Platform.includedIn(Platform.DARWIN.class))) {
69+
/* See GR-9725 */
70+
throw VMError.unsupportedFeature("Heap dump writing currently contains Posix specific code");
71+
}
72+
}
73+
74+
@Override
75+
public AllocationFreeFileOutputStream newStreamFor(FileOutputStream fileOutputStream) throws IOException {
76+
return new AllocationFreeFileOutputStreamPosix(fileOutputStream);
77+
}
78+
79+
@Override
80+
public void write(int b) throws IOException {
81+
final CCharPointer buffer = StackValue.get(CCharPointer.class);
82+
buffer.write((byte) b);
83+
final boolean writeResult = PosixUtils.writeBytes(fileDescriptor, buffer, WordFactory.unsigned(1));
84+
if (!writeResult) {
85+
throw preallocatedIOException;
86+
}
87+
}
88+
89+
@Override
90+
public void write(byte[] b, int off, int len) throws IOException {
91+
/* Sanity check the arguments. */
92+
if ((b == null) || ((off < 0) || (len < 0) || ((b.length - off) < len))) {
93+
throw preallocatedArrayIndexOutOfBoundsException;
94+
}
95+
96+
/*
97+
* Stack allocation needs an allocation size that is a compile time constant, so we split
98+
* the byte array up in multiple chunks and write them separately.
99+
*/
100+
final int chunkSize = 256;
101+
final CCharPointer bytes = StackValue.get(chunkSize);
102+
103+
int chunkOffset = off;
104+
int inputLength = len;
105+
while (inputLength > 0) {
106+
int chunkLength = Math.min(inputLength, chunkSize);
107+
108+
for (int i = 0; i < chunkLength; i++) {
109+
bytes.write(i, b[chunkOffset + i]);
110+
}
111+
112+
if (!PosixUtils.writeBytes(fileDescriptor, bytes, WordFactory.unsigned(chunkLength))) {
113+
throw preallocatedIOException;
114+
}
115+
116+
chunkOffset += chunkLength;
117+
inputLength -= chunkLength;
118+
}
119+
}
120+
121+
@Override
122+
public void close() throws IOException {
123+
fos.close();
124+
}
125+
126+
@Override
127+
public void flush() throws IOException {
128+
}
129+
130+
/**
131+
* Read the current position in a file descriptor.
132+
*/
133+
@Override
134+
protected long position() {
135+
int fd = PosixUtils.getFD(fileDescriptor);
136+
return Unistd.lseek(fd, WordFactory.zero(), Unistd.SEEK_CUR()).rawValue();
137+
}
138+
139+
/**
140+
* Set the current position in a file descriptor.
141+
*/
142+
@Override
143+
protected long position(long offset) {
144+
int fd = PosixUtils.getFD(fileDescriptor);
145+
return Unistd.lseek(fd, WordFactory.signed(offset), Unistd.SEEK_SET()).rawValue();
146+
}
147+
}
148+
149+
/*
150+
* The limitation to Linux and Darwin is necessary because the implementation currently uses
151+
* posix-dependent low-level code. See GR-9725.
152+
*/
153+
@Platforms({Platform.LINUX.class, Platform.DARWIN.class})
154+
@AutomaticFeature
155+
class AllocationFreeFileOutputStreamFeature implements Feature {
156+
157+
@Override
158+
public void afterRegistration(Feature.AfterRegistrationAccess access) {
159+
ImageSingletons.add(AllocationFreeFileOutputStream.class, new AllocationFreeFileOutputStreamPosix());
160+
}
161+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.heapdump;
26+
27+
import java.io.IOException;
28+
29+
/**
30+
* Simple interface for writing heap dump to arbitrary data source.
31+
*
32+
*/
33+
public interface AllocationFreeOutputStream {
34+
35+
void write(int b) throws IOException;
36+
37+
void write(byte[] b, int offset, int length) throws IOException;
38+
39+
/**
40+
* close method is called outside of critical section and can allocate objects.
41+
*
42+
* @throws IOException
43+
*/
44+
void close() throws IOException;
45+
46+
void flush() throws IOException;
47+
48+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.heapdump;
26+
27+
import java.io.FileOutputStream;
28+
29+
import org.graalvm.nativeimage.ImageSingletons;
30+
import org.graalvm.nativeimage.hosted.Feature;
31+
import org.graalvm.nativeimage.impl.HeapDumpSupport;
32+
33+
import com.oracle.svm.core.annotate.AutomaticFeature;
34+
35+
@AutomaticFeature
36+
public final class HeapDumpFeature implements Feature {
37+
@Override
38+
public void afterRegistration(AfterRegistrationAccess access) {
39+
ImageSingletons.add(HeapDumpSupport.class, new HeapDumpSupportImpl());
40+
}
41+
}
42+
43+
final class HeapDumpSupportImpl implements HeapDumpSupport {
44+
@Override
45+
public void dumpHeap(String outputFile, boolean live) throws java.io.IOException {
46+
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
47+
HeapDumpWriter.singleton().writeHeapTo(fileOutputStream, live);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)