Skip to content

Commit 413f852

Browse files
Mat Carteriklam
authored andcommitted
8369736: Add management interface for AOT cache creation
Reviewed-by: mr, iklam, kevinw
1 parent 11aa6e1 commit 413f852

File tree

10 files changed

+335
-3
lines changed

10 files changed

+335
-3
lines changed

src/hotspot/share/cds/aotMetaspace.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#include "runtime/vmOperations.hpp"
9797
#include "runtime/vmThread.hpp"
9898
#include "sanitizers/leak.hpp"
99+
#include "services/management.hpp"
99100
#include "utilities/align.hpp"
100101
#include "utilities/bitMap.inline.hpp"
101102
#include "utilities/defaultStream.hpp"

src/hotspot/share/include/jvm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ JVM_InternString(JNIEnv *env, jstring str);
8787
/*
8888
* java.lang.System
8989
*/
90+
JNIEXPORT jboolean JNICALL
91+
JVM_AOTEndRecording(JNIEnv *env);
92+
9093
JNIEXPORT jlong JNICALL
9194
JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored);
9295

src/hotspot/share/prims/jvm.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,19 @@ extern void trace_class_resolution(Klass* to_class) {
229229

230230
// java.lang.System //////////////////////////////////////////////////////////////////////
231231

232+
JVM_ENTRY(jboolean, JVM_AOTEndRecording(JNIEnv *env))
233+
#if INCLUDE_CDS
234+
if (CDSConfig::is_dumping_preimage_static_archive()) {
235+
if (!AOTMetaspace::preimage_static_archive_dumped()) {
236+
AOTMetaspace::dump_static_archive(THREAD);
237+
return JNI_TRUE;
238+
}
239+
}
240+
return JNI_FALSE;
241+
#else
242+
return JNI_FALSE;
243+
#endif // INCLUDE_CDS
244+
JVM_END
232245

233246
JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
234247
return os::javaTimeMillis();

src/java.management/share/classes/sun/management/VMManagement.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -48,6 +48,9 @@ public interface VMManagement {
4848
public boolean isGcNotificationSupported();
4949
public boolean isRemoteDiagnosticCommandsSupported();
5050

51+
// AOT Subsystem
52+
public boolean endAOTRecording();
53+
5154
// Class Loading Subsystem
5255
public long getTotalClassCount();
5356
public int getLoadedClassCount();

src/java.management/share/classes/sun/management/VMManagementImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -117,6 +117,9 @@ public boolean isRemoteDiagnosticCommandsSupported() {
117117
public native boolean isThreadCpuTimeEnabled();
118118
public native boolean isThreadAllocatedMemoryEnabled();
119119

120+
// AOT Subsystem
121+
public native boolean endAOTRecording();
122+
120123
// Class Loading Subsystem
121124
public int getLoadedClassCount() {
122125
long count = getTotalClassCount() - getUnloadedClassCount();

src/java.management/share/native/libmanagement/VMManagementImpl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ Java_sun_management_VMManagementImpl_getVmArguments0
101101
return JVM_GetVmArguments(env);
102102
}
103103

104+
JNIEXPORT jboolean JNICALL
105+
Java_sun_management_VMManagementImpl_endAOTRecording
106+
(JNIEnv *env, jobject dummy)
107+
{
108+
return JVM_AOTEndRecording(env);
109+
}
110+
104111
JNIEXPORT jlong JNICALL
105112
Java_sun_management_VMManagementImpl_getTotalClassCount
106113
(JNIEnv *env, jobject dummy)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2025, Microsoft, Inc. 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.sun.management.internal;
26+
27+
import javax.management.ObjectName;
28+
import jdk.management.HotSpotAOTCacheMXBean;
29+
import sun.management.Util;
30+
import sun.management.VMManagement;
31+
32+
/**
33+
* Implementation class for the AOT Cache subsystem.
34+
*
35+
* ManagementFactory.getRuntimeMXBean() returns an instance
36+
* of this class.
37+
*/
38+
public class HotSpotAOTCacheImpl implements HotSpotAOTCacheMXBean {
39+
40+
private final VMManagement jvm;
41+
/**
42+
* Constructor of HotSpotAOTCacheImpl class.
43+
*/
44+
HotSpotAOTCacheImpl(VMManagement vm) {
45+
this.jvm = vm;
46+
}
47+
48+
public boolean endRecording() {
49+
return jvm.endAOTRecording();
50+
}
51+
52+
public ObjectName getObjectName() {
53+
return Util.newObjectName("jdk.management:type=HotSpotAOTCache");
54+
}
55+
}

src/jdk.management/share/classes/com/sun/management/internal/PlatformMBeanProviderImpl.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,7 @@
3939
import java.util.stream.Collectors;
4040
import java.util.stream.Stream;
4141
import javax.management.DynamicMBean;
42+
import jdk.management.HotSpotAOTCacheMXBean;
4243
import jdk.management.VirtualThreadSchedulerMXBean;
4344
import sun.management.ManagementFactoryHelper;
4445
import sun.management.spi.PlatformMBeanProvider;
@@ -159,6 +160,41 @@ public synchronized Map<String, java.lang.management.ThreadMXBean> nameToMBeanMa
159160
}
160161
});
161162

163+
/**
164+
* HotSpotAOTCacheMXBean.
165+
*/
166+
initMBeanList.add(new PlatformComponent<HotSpotAOTCacheMXBean>() {
167+
private final Set<Class<? extends HotSpotAOTCacheMXBean>> mbeanInterfaces =
168+
Set.of(HotSpotAOTCacheMXBean.class);
169+
private final Set<String> mbeanInterfaceNames =
170+
Set.of(HotSpotAOTCacheMXBean.class.getName());
171+
private HotSpotAOTCacheMXBean impl;
172+
173+
@Override
174+
public Set<Class<? extends HotSpotAOTCacheMXBean>> mbeanInterfaces() {
175+
return mbeanInterfaces;
176+
}
177+
178+
@Override
179+
public Set<String> mbeanInterfaceNames() {
180+
return mbeanInterfaceNames;
181+
}
182+
183+
@Override
184+
public String getObjectNamePattern() {
185+
return "jdk.management:type=HotSpotAOTCache";
186+
}
187+
188+
@Override
189+
public Map<String, HotSpotAOTCacheMXBean> nameToMBeanMap() {
190+
HotSpotAOTCacheMXBean impl = this.impl;
191+
if (impl == null) {
192+
this.impl = impl = new HotSpotAOTCacheImpl(ManagementFactoryHelper.getVMManagement());
193+
}
194+
return Map.of("jdk.management:type=HotSpotAOTCache", impl);
195+
}
196+
});
197+
162198
/**
163199
* VirtualThreadSchedulerMXBean.
164200
*/
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2025, Microsoft, Inc. 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 jdk.management;
26+
27+
import java.lang.management.ManagementFactory;
28+
import java.lang.management.PlatformManagedObject;
29+
import javax.management.MBeanServer;
30+
import javax.management.ObjectName;
31+
32+
/**
33+
* Management interface for the JDK's Ahead of Time (AOT) Cache.
34+
*
35+
* <p> The management interface is registered with the platform {@link MBeanServer
36+
* MBeanServer}. The {@link ObjectName ObjectName} that uniquely identifies the management
37+
* interface within the {@code MBeanServer} is {@code jdk.management:type=HotSpotAOTCache}.
38+
*
39+
* <p> Direct access to the MXBean interface can be obtained with
40+
* {@link ManagementFactory#getPlatformMXBean(Class)}.
41+
*
42+
* @since 26
43+
*/
44+
public interface HotSpotAOTCacheMXBean extends PlatformManagedObject {
45+
/**
46+
* If an AOT recording is in progress, ends the recording. This method returns
47+
* after the AOT artifacts have been completely written.
48+
*
49+
* <p>The JVM will start recording AOT artifacts upon start-up if appropriate JVM options are
50+
* given in the command-line. The recording will stop when the JVM exits, or when
51+
* the {@code endRecording} method is called. Examples:
52+
*
53+
* <p> ${@code java -XX:AOTCacheOutput=app.aot ....}
54+
*
55+
* <blockquote>
56+
* The JVM records optimization information for the current application in the AOT cache file
57+
* {@code app.aot}. In a future run of the application, the option {@code -XX:AOTCache=app.aot} will
58+
* cause the JVM to use the cache to improve the application's startup and warmup performance.
59+
* </blockquote>
60+
*
61+
* <p> ${@code java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconfig ....}
62+
*
63+
* <blockquote>
64+
* The JVM records optimization information for the current application in the AOT configuration
65+
* file {@code app.aotconfig}. Subsequently, an AOT cache file can be created with the command:
66+
*
67+
* <p>${@code java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconfig -XX:AOTCache=app.aot ...}
68+
* </blockquote>
69+
*
70+
* <p>For more information about creating and using the AOT artifacts, and detailed
71+
* specification of the corresponding JVM command-line options, please refer
72+
* to <a href="https://openjdk.org/jeps/483">JEP 483</a> and <a href="https://openjdk.org/jeps/514">JEP 514</a>.
73+
*
74+
* <p>Currently there are no APIs to start an AOT recording. AOT recordings must be
75+
* started using JVM command-line options such as {@code -XX:AOTCacheOutput}.
76+
* There are also no APIs to query whether an AOT recording is in progress, or what AOT
77+
* artifacts are being recorded.
78+
*
79+
* <p> This method enables an application to end its own AOT recording
80+
* programatically, but that is not necessarily the best approach. Doing so
81+
* requires changing the application’s code, which might not be
82+
* feasible. Even when it is feasible, injecting training-specific logic
83+
* into the application reduces the similarity between training runs and
84+
* production runs, potentially making the AOT cache less effective. It may
85+
* be better to arrange for an external agent to end the training run,
86+
* thereby creating an AOT cache without interfering with the application’s
87+
* code.
88+
*
89+
* @return {@code true} if a recording was in progress and has been ended
90+
* successfully; {@code false} otherwise.
91+
*/
92+
public boolean endRecording();
93+
}

0 commit comments

Comments
 (0)