Skip to content

Commit

Permalink
Use log function from libm.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhaeubl committed Feb 10, 2024
1 parent a7f26c7 commit d721c1b
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, 2024, 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 com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.headers.LibMSupport;
import com.oracle.svm.core.posix.headers.PosixLibM;

@AutomaticallyRegisteredImageSingleton(LibMSupport.class)
public class PosixLibMSupport implements LibMSupport {
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public double log(double value) {
return PosixLibM.log(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024, 2024, 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.headers;

import java.util.Collections;
import java.util.List;

import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.function.CFunction;

import com.oracle.svm.core.jfr.HasJfrSupport;

@CContext(value = LibMDependencies.class)
public class PosixLibM {
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
public static native double log(double value);
}

class LibMDependencies implements CContext.Directives {
@Override
public boolean isInConfiguration() {
return HasJfrSupport.get();
}

@Override
public List<String> getHeaderFiles() {
return Collections.singletonList("<math.h>");
}

@Override
public List<String> getLibraries() {
return Collections.singletonList("m");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, 2024, 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.windows;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.headers.LibMSupport;
import com.oracle.svm.core.windows.headers.WindowsLibC;

@AutomaticallyRegisteredImageSingleton(LibMSupport.class)
public class WindowsLibMSupport implements LibMSupport {
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public double log(double value) {
return WindowsLibC.log(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class WindowsDirectives implements CContext.Directives {
"<stdio.h>",
"<stdlib.h>",
"<string.h>",
"<io.h>"
"<io.h>",
"<math.h>"
};

@Override
Expand All @@ -55,8 +56,7 @@ public boolean isInConfiguration() {
@Override
public List<String> getHeaderFiles() {
if (Platform.includedIn(Platform.WINDOWS.class)) {
List<String> result = new ArrayList<>(Arrays.asList(windowsLibs));
return result;
return new ArrayList<>(Arrays.asList(windowsLibs));
} else {
throw VMError.shouldNotReachHere("Unsupported OS");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,7 @@ public interface WCharPointer extends PointerBase {

@CFunction(transition = CFunction.Transition.NO_TRANSITION)
public static native UnsignedWord strtoull(CCharPointer string, CCharPointerPointer endPtr, int base);

@CFunction(transition = CFunction.Transition.NO_TRANSITION)
public static native double log(double value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package com.oracle.svm.core.headers;

import jdk.graal.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CCharPointerPointer;
Expand All @@ -34,6 +33,8 @@

import com.oracle.svm.core.Uninterruptible;

import jdk.graal.compiler.api.replacements.Fold;

public class LibC {
public static final int EXIT_CODE_ABORT = 99;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024, 2024, 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.headers;

import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;

import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.Uninterruptible;

import jdk.graal.compiler.api.replacements.Fold;

public class LibM {
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public static double log(double value) {
return libm().log(value);
}

@Fold
static LibMSupport libm() {
return ImageSingletons.lookup(LibMSupport.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024, 2024, 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.headers;

import com.oracle.svm.core.Uninterruptible;

public interface LibMSupport {
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
double log(double value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
package com.oracle.svm.core.jfr.throttling;

import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
import static java.lang.Math.log;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.headers.LibM;
import com.oracle.svm.core.jdk.UninterruptibleUtils;
import com.oracle.svm.core.jfr.utils.JfrRandom;
import com.oracle.svm.core.thread.JavaSpinLockUtils;
Expand Down Expand Up @@ -172,7 +172,7 @@ private long deriveSamplingInterval(double sampleSize, JfrSamplerWindow expired)

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private double projectPopulationSize(JfrSamplerWindow expired) {
avgPopulationSize = exponentiallyWeightedMovingAverage((double) expired.getPopulationSize(), ewmaPopulationSizeAlpha, avgPopulationSize);
avgPopulationSize = exponentiallyWeightedMovingAverage(expired.getPopulationSize(), ewmaPopulationSizeAlpha, avgPopulationSize);
return avgPopulationSize;
}

Expand All @@ -191,7 +191,7 @@ private long nextGeometric(double p) {
} else if (u == 1.0) {
u = 0.99;
}
return UninterruptibleUtils.Math.ceilToLong(log(1.0 - u) / log(1.0 - p));
return UninterruptibleUtils.Math.ceilToLong(LibM.log(1.0 - u) / LibM.log(1.0 - p));
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
Expand Down

0 comments on commit d721c1b

Please sign in to comment.