Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-37414] Add support for Unsafe.getLoadAverage. #4420

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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. */
}

/*
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down