diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixLoadAverageSupport.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixLoadAverageSupport.java new file mode 100644 index 000000000000..8c9338fac518 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixLoadAverageSupport.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022, 2022, 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 + * 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.oracle.svm.core.posix; + +import org.graalvm.nativeimage.ImageSingletons; +import org.graalvm.nativeimage.PinnedObject; +import org.graalvm.nativeimage.hosted.Feature; + +import com.oracle.svm.core.annotate.AutomaticFeature; +import com.oracle.svm.core.jdk.LoadAverageSupport; +import com.oracle.svm.core.posix.headers.Stdlib; + +class PosixLoadAverageSupport implements LoadAverageSupport { + @Override + public int getLoadAverage(double[] loadavg, int nelems) { + /* + * Adapted from `os::loadavg` which is the same in both `src/hotspot/os/linux/os_linux.cpp` + * and `src/hotspot/os/bsd/os_bsd.cpp`. + */ + try (PinnedObject pinnedLoadavg = PinnedObject.create(loadavg)) { + return Stdlib.getloadavg(pinnedLoadavg.addressOfArrayElement(0), nelems); + } + } +} + +@AutomaticFeature +class PosixLoadAverageSupportFeature implements Feature { + @Override + public void afterRegistration(AfterRegistrationAccess access) { + ImageSingletons.add(LoadAverageSupport.class, new PosixLoadAverageSupport()); + } +} diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Stdlib.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Stdlib.java index a45702411a3d..9f7d514dd06c 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Stdlib.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Stdlib.java @@ -29,6 +29,7 @@ import org.graalvm.nativeimage.c.CContext; import org.graalvm.nativeimage.c.function.CFunction; import org.graalvm.nativeimage.c.type.CCharPointer; +import org.graalvm.nativeimage.c.type.CDoublePointer; // Checkstyle: stop @@ -43,4 +44,7 @@ public class Stdlib { @CFunction public static native CCharPointer realpath(CCharPointer name, CCharPointer resolved); + + @CFunction + public static native int getloadavg(CDoublePointer loadavg, int nelem); } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/LoadAverageSupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/LoadAverageSupport.java new file mode 100644 index 000000000000..e48c40d16aa7 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/LoadAverageSupport.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022, 2022, 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 + * 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.oracle.svm.core.jdk; + +public interface LoadAverageSupport { + int getLoadAverage(double[] loadavg, int nelems); +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java index 16687d21aae1..b5aef82293b3 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java @@ -30,6 +30,7 @@ import java.security.ProtectionDomain; import org.graalvm.compiler.nodes.extended.MembarNode; +import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.UnmanagedMemory; import org.graalvm.word.WordFactory; @@ -144,8 +145,12 @@ private Class defineAnonymousClass(Class hostClass, byte[] data, Object[] } @Substitute - private int getLoadAverage(double[] loadavg, int nelems) { - throw VMError.unsupportedFeature("Unsupported method of Unsafe"); + private int getLoadAverage0(double[] loadavg, int nelems) { + /* Adapted from `Unsafe_GetLoadAverage0` in `src/hotspot/share/prims/unsafe.cpp`. */ + if (ImageSingletons.contains(LoadAverageSupport.class)) { + return ImageSingletons.lookup(LoadAverageSupport.class).getLoadAverage(loadavg, nelems); + } + return -1; /* The load average is unobtainable. */ } /* @@ -196,9 +201,6 @@ private Class defineClass0(String name, byte[] b, int off, int len, ClassLoad @TargetElement(onlyWith = JDK11OrEarlier.class) private native Class defineAnonymousClass0(Class hostClass, byte[] data, Object[] cpPatches); - @Delete - private native int getLoadAverage0(double[] loadavg, int nelems); - @Delete @TargetElement(onlyWith = JDK11OrEarlier.class) private native boolean unalignedAccess0(); diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/management/Target_sun_management_BaseOperatingSystemImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/management/Target_sun_management_BaseOperatingSystemImpl.java index 3a934b557cba..a0f86813aa3f 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/management/Target_sun_management_BaseOperatingSystemImpl.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/management/Target_sun_management_BaseOperatingSystemImpl.java @@ -39,11 +39,6 @@ final class Target_sun_management_BaseOperatingSystemImpl { Target_sun_management_BaseOperatingSystemImpl(@SuppressWarnings("unused") VMManagement vm) { } - @Substitute - public double getSystemLoadAverage() { - return -1; /* Workaround until we add support for `Unsafe.getLoadAverage` (GR-37414). */ - } - /* * The following substitutions eliminate the use of the `BaseOperatingSystemImpl.jvm` field, * which we set to `null` to avoid dealing with `VMManagementImpl` that we do not support.