8369736: Add management interface for AOT cache creation

Reviewed-by: kvn
Backport-of: 413f852bdb4767b2a1c29431144616668888138d
This commit is contained in:
Ioi Lam 2025-12-12 14:32:58 +00:00
parent 85eb6b752e
commit d9bc822168
10 changed files with 335 additions and 3 deletions

View File

@ -96,6 +96,7 @@
#include "runtime/vmOperations.hpp"
#include "runtime/vmThread.hpp"
#include "sanitizers/leak.hpp"
#include "services/management.hpp"
#include "utilities/align.hpp"
#include "utilities/bitMap.inline.hpp"
#include "utilities/defaultStream.hpp"

View File

@ -87,6 +87,9 @@ JVM_InternString(JNIEnv *env, jstring str);
/*
* java.lang.System
*/
JNIEXPORT jboolean JNICALL
JVM_AOTEndRecording(JNIEnv *env);
JNIEXPORT jlong JNICALL
JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored);

View File

@ -229,6 +229,19 @@ extern void trace_class_resolution(Klass* to_class) {
// java.lang.System //////////////////////////////////////////////////////////////////////
JVM_ENTRY(jboolean, JVM_AOTEndRecording(JNIEnv *env))
#if INCLUDE_CDS
if (CDSConfig::is_dumping_preimage_static_archive()) {
if (!AOTMetaspace::preimage_static_archive_dumped()) {
AOTMetaspace::dump_static_archive(THREAD);
return JNI_TRUE;
}
}
return JNI_FALSE;
#else
return JNI_FALSE;
#endif // INCLUDE_CDS
JVM_END
JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
return os::javaTimeMillis();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -48,6 +48,9 @@ public interface VMManagement {
public boolean isGcNotificationSupported();
public boolean isRemoteDiagnosticCommandsSupported();
// AOT Subsystem
public boolean endAOTRecording();
// Class Loading Subsystem
public long getTotalClassCount();
public int getLoadedClassCount();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -117,6 +117,9 @@ class VMManagementImpl implements VMManagement {
public native boolean isThreadCpuTimeEnabled();
public native boolean isThreadAllocatedMemoryEnabled();
// AOT Subsystem
public native boolean endAOTRecording();
// Class Loading Subsystem
public int getLoadedClassCount() {
long count = getTotalClassCount() - getUnloadedClassCount();

View File

@ -101,6 +101,13 @@ Java_sun_management_VMManagementImpl_getVmArguments0
return JVM_GetVmArguments(env);
}
JNIEXPORT jboolean JNICALL
Java_sun_management_VMManagementImpl_endAOTRecording
(JNIEnv *env, jobject dummy)
{
return JVM_AOTEndRecording(env);
}
JNIEXPORT jlong JNICALL
Java_sun_management_VMManagementImpl_getTotalClassCount
(JNIEnv *env, jobject dummy)

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2025, Microsoft, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.management.internal;
import javax.management.ObjectName;
import jdk.management.HotSpotAOTCacheMXBean;
import sun.management.Util;
import sun.management.VMManagement;
/**
* Implementation class for the AOT Cache subsystem.
*
* ManagementFactory.getRuntimeMXBean() returns an instance
* of this class.
*/
public class HotSpotAOTCacheImpl implements HotSpotAOTCacheMXBean {
private final VMManagement jvm;
/**
* Constructor of HotSpotAOTCacheImpl class.
*/
HotSpotAOTCacheImpl(VMManagement vm) {
this.jvm = vm;
}
public boolean endRecording() {
return jvm.endAOTRecording();
}
public ObjectName getObjectName() {
return Util.newObjectName("jdk.management:type=HotSpotAOTCache");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -39,6 +39,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.management.DynamicMBean;
import jdk.management.HotSpotAOTCacheMXBean;
import jdk.management.VirtualThreadSchedulerMXBean;
import sun.management.ManagementFactoryHelper;
import sun.management.spi.PlatformMBeanProvider;
@ -159,6 +160,41 @@ public final class PlatformMBeanProviderImpl extends PlatformMBeanProvider {
}
});
/**
* HotSpotAOTCacheMXBean.
*/
initMBeanList.add(new PlatformComponent<HotSpotAOTCacheMXBean>() {
private final Set<Class<? extends HotSpotAOTCacheMXBean>> mbeanInterfaces =
Set.of(HotSpotAOTCacheMXBean.class);
private final Set<String> mbeanInterfaceNames =
Set.of(HotSpotAOTCacheMXBean.class.getName());
private HotSpotAOTCacheMXBean impl;
@Override
public Set<Class<? extends HotSpotAOTCacheMXBean>> mbeanInterfaces() {
return mbeanInterfaces;
}
@Override
public Set<String> mbeanInterfaceNames() {
return mbeanInterfaceNames;
}
@Override
public String getObjectNamePattern() {
return "jdk.management:type=HotSpotAOTCache";
}
@Override
public Map<String, HotSpotAOTCacheMXBean> nameToMBeanMap() {
HotSpotAOTCacheMXBean impl = this.impl;
if (impl == null) {
this.impl = impl = new HotSpotAOTCacheImpl(ManagementFactoryHelper.getVMManagement());
}
return Map.of("jdk.management:type=HotSpotAOTCache", impl);
}
});
/**
* VirtualThreadSchedulerMXBean.
*/

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2025, Microsoft, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.management;
import java.lang.management.ManagementFactory;
import java.lang.management.PlatformManagedObject;
import javax.management.MBeanServer;
import javax.management.ObjectName;
/**
* Management interface for the JDK's Ahead of Time (AOT) Cache.
*
* <p> The management interface is registered with the platform {@link MBeanServer
* MBeanServer}. The {@link ObjectName ObjectName} that uniquely identifies the management
* interface within the {@code MBeanServer} is {@code jdk.management:type=HotSpotAOTCache}.
*
* <p> Direct access to the MXBean interface can be obtained with
* {@link ManagementFactory#getPlatformMXBean(Class)}.
*
* @since 26
*/
public interface HotSpotAOTCacheMXBean extends PlatformManagedObject {
/**
* If an AOT recording is in progress, ends the recording. This method returns
* after the AOT artifacts have been completely written.
*
* <p>The JVM will start recording AOT artifacts upon start-up if appropriate JVM options are
* given in the command-line. The recording will stop when the JVM exits, or when
* the {@code endRecording} method is called. Examples:
*
* <p> ${@code java -XX:AOTCacheOutput=app.aot ....}
*
* <blockquote>
* The JVM records optimization information for the current application in the AOT cache file
* {@code app.aot}. In a future run of the application, the option {@code -XX:AOTCache=app.aot} will
* cause the JVM to use the cache to improve the application's startup and warmup performance.
* </blockquote>
*
* <p> ${@code java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconfig ....}
*
* <blockquote>
* The JVM records optimization information for the current application in the AOT configuration
* file {@code app.aotconfig}. Subsequently, an AOT cache file can be created with the command:
*
* <p>${@code java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconfig -XX:AOTCache=app.aot ...}
* </blockquote>
*
* <p>For more information about creating and using the AOT artifacts, and detailed
* specification of the corresponding JVM command-line options, please refer
* to <a href="https://openjdk.org/jeps/483">JEP 483</a> and <a href="https://openjdk.org/jeps/514">JEP 514</a>.
*
* <p>Currently there are no APIs to start an AOT recording. AOT recordings must be
* started using JVM command-line options such as {@code -XX:AOTCacheOutput}.
* There are also no APIs to query whether an AOT recording is in progress, or what AOT
* artifacts are being recorded.
*
* <p> This method enables an application to end its own AOT recording
* programatically, but that is not necessarily the best approach. Doing so
* requires changing the applications code, which might not be
* feasible. Even when it is feasible, injecting training-specific logic
* into the application reduces the similarity between training runs and
* production runs, potentially making the AOT cache less effective. It may
* be better to arrange for an external agent to end the training run,
* thereby creating an AOT cache without interfering with the applications
* code.
*
* @return {@code true} if a recording was in progress and has been ended
* successfully; {@code false} otherwise.
*/
public boolean endRecording();
}

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2025, Microsoft, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
/*
* @test
* @summary Sanity test for HotSpotAOTCache MXBean
* @requires vm.cds.write.archived.java.heap
* @library /test/jdk/lib/testlibrary /test/lib
* @build HotSpotAOTCacheMXBeanTest
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar HotSpotAOTCacheMXBeanApp
* @run driver HotSpotAOTCacheMXBeanTest
*/
import java.io.IOException;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import jdk.management.HotSpotAOTCacheMXBean;
import jdk.test.lib.cds.CDSAppTester;
import jdk.test.lib.helpers.ClassFileInstaller;
import jdk.test.lib.process.OutputAnalyzer;
public class HotSpotAOTCacheMXBeanTest {
static final String appJar = ClassFileInstaller.getJarPath("app.jar");
static final String mainClass = "HotSpotAOTCacheMXBeanApp";
public static void main(String[] args) throws Exception {
Tester tester = new Tester();
tester.runAOTWorkflow();
}
static class Tester extends CDSAppTester {
public Tester() {
super(mainClass);
}
@Override
public String classpath(RunMode runMode) {
return appJar;
}
@Override
public String[] vmArgs(RunMode runMode) {
return new String[] {
"-Xlog:cds+class=trace",
"--add-modules=jdk.management"
};
}
@Override
public String[] appCommandLine(RunMode runMode) {
return new String[] {
mainClass, runMode.name()
};
}
@Override
public void checkExecution(OutputAnalyzer out, RunMode runMode) {
var name = runMode.name();
if (runMode.isApplicationExecuted()) {
if(runMode == RunMode.TRAINING) {
out.shouldContain("Hello Leyden " + name);
out.shouldContain("Successfully stopped recording");
} else if (runMode == RunMode.ASSEMBLY) {
out.shouldNotContain("Hello Leyden ");
} else if (runMode == RunMode.PRODUCTION) {
out.shouldContain("Hello Leyden " + name);
out.shouldContain("Failed to stop recording");
}
out.shouldNotContain("HotSpotAOTCacheMXBean is not available");
out.shouldNotContain("IOException occurred!");
}
}
}
}
class HotSpotAOTCacheMXBeanApp {
public static void main(String[] args) {
System.out.println("Hello Leyden " + args[0]);
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotAOTCacheMXBean aotBean = ManagementFactory.newPlatformMXBeanProxy(server,
"jdk.management:type=HotSpotAOTCache",
HotSpotAOTCacheMXBean.class);
if (aotBean == null) {
System.out.println("HotSpotAOTCacheMXBean is not available");
return;
}
if (aotBean.endRecording()) {
System.out.println("Successfully stopped recording");
} else {
System.out.println("Failed to stop recording");
}
} catch (IOException e) {
System.out.println("IOException occurred!");
}
}
}