Skip to content

Commit

Permalink
Replace setitimer with timer_create method. Implement isHidden entry …
Browse files Browse the repository at this point in the history
…in JfrMethod.
  • Loading branch information
jovanstevanovic committed Mar 6, 2024
1 parent 4d7b630 commit e2c0a65
Show file tree
Hide file tree
Showing 15 changed files with 454 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.c.function.CEntryPoint;
import org.graalvm.nativeimage.c.function.CEntryPointLiteral;
import org.graalvm.nativeimage.c.function.CodePointer;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.word.Pointer;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.IsolateListenerSupport;
import com.oracle.svm.core.IsolateListenerSupportFeature;
Expand All @@ -48,31 +46,47 @@
import com.oracle.svm.core.c.function.CEntryPointOptions;
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
import com.oracle.svm.core.feature.InternalFeature;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.heap.RestrictHeapAccess;
import com.oracle.svm.core.jfr.JfrExecutionSamplerSupported;
import com.oracle.svm.core.jfr.JfrFeature;
import com.oracle.svm.core.jfr.sampler.JfrExecutionSampler;
import com.oracle.svm.core.option.HostedOptionKey;
import com.oracle.svm.core.option.SubstrateOptionsParser;
import com.oracle.svm.core.posix.darwin.DarwinSubstrateSigprofHandler;
import com.oracle.svm.core.posix.headers.Signal;
import com.oracle.svm.core.posix.headers.Time;
import com.oracle.svm.core.posix.linux.LinuxSubstrateSigprofHandler;
import com.oracle.svm.core.sampler.SubstrateSigprofHandler;
import com.oracle.svm.core.thread.ThreadListenerSupport;
import com.oracle.svm.core.thread.ThreadListenerSupportFeature;
import com.oracle.svm.core.util.TimeUtils;
import com.oracle.svm.core.util.UserError;
import com.oracle.svm.core.util.VMError;

import jdk.graal.compiler.options.Option;

public final class PosixSubstrateSigprofHandler extends SubstrateSigprofHandler {
/**
* <p>
* This class serves as the core for POSIX-based SIGPROF signal handlers.
* </p>
*
* <p>
* POSIX supports two types of timers: the global timer and per-thread timer. Both timers can
* interrupt threads that are blocked. This may result in situations where the VM operation changes
* unexpectedly while a thread executes signal handler code:
* <ul>
* <li>Thread A requests a safepoint.
* <li>Thread B is blocked because of the safepoint but the VM did not start executing the VM
* operation yet (i.e., there is no VM operation in progress).
* <li>Thread B receives a SIGPROF signal and starts executing the signal handler.
* <li>The VM reaches a safepoint and thread A starts executing the VM operation.
* <li>Thread B continues executing the signal handler while the VM operation is now suddenly in
* progress.
* </ul>
* </p>
*/
public abstract class PosixSubstrateSigprofHandler extends SubstrateSigprofHandler {
private static final CEntryPointLiteral<Signal.AdvancedSignalDispatcher> advancedSignalDispatcher = CEntryPointLiteral.create(PosixSubstrateSigprofHandler.class,
"dispatch", int.class, Signal.siginfo_t.class, Signal.ucontext_t.class);

@Platforms(Platform.HOSTED_ONLY.class)
public PosixSubstrateSigprofHandler() {
}

@SuppressWarnings("unused")
@CEntryPoint(include = CEntryPoint.NotIncludedAutomatically.class, publishAs = CEntryPoint.Publish.NotPublished)
@CEntryPointOptions(prologue = CEntryPointOptions.NoPrologue.class, epilogue = CEntryPointOptions.NoEpilogue.class)
Expand All @@ -87,40 +101,9 @@ private static void dispatch(@SuppressWarnings("unused") int signalNumber, @Supp
}
}

private static void registerSigprofSignal(Signal.AdvancedSignalDispatcher dispatcher) {
PosixUtils.installSignalHandler(Signal.SignalEnum.SIGPROF, dispatcher, Signal.SA_RESTART());
}

@Override
protected void updateInterval() {
updateInterval(TimeUtils.millisToMicros(newIntervalMillis));
}

public static void updateInterval(long us) {
Time.itimerval newValue = UnsafeStackValue.get(Time.itimerval.class);
newValue.it_value().set_tv_sec(us / TimeUtils.microsPerSecond);
newValue.it_value().set_tv_usec(us % TimeUtils.microsPerSecond);
newValue.it_interval().set_tv_sec(us / TimeUtils.microsPerSecond);
newValue.it_interval().set_tv_usec(us % TimeUtils.microsPerSecond);

int status = Time.NoTransitions.setitimer(Time.TimerTypeEnum.ITIMER_PROF, newValue, WordFactory.nullPointer());
PosixUtils.checkStatusIs0(status, "setitimer(which, newValue, oldValue): wrong arguments.");
}

@Override
protected void installSignalHandler() {
registerSigprofSignal(advancedSignalDispatcher.getFunctionPointer());
updateInterval();
}

@Override
protected void uninstallSignalHandler() {
/*
* Only disable the sampling but do not replace the signal handler with the default one
* because a signal might be pending for some thread (the default signal handler would print
* "Profiling timer expired" to the output).
*/
updateInterval(0);
PosixUtils.installSignalHandler(Signal.SignalEnum.SIGPROF, advancedSignalDispatcher.getFunctionPointer(), Signal.SA_RESTART());
}

static boolean isSignalHandlerBasedExecutionSamplerEnabled() {
Expand Down Expand Up @@ -159,12 +142,22 @@ public List<Class<? extends Feature>> getRequiredFeatures() {
@Override
public void afterRegistration(AfterRegistrationAccess access) {
if (JfrExecutionSamplerSupported.isSupported() && isSignalHandlerBasedExecutionSamplerEnabled()) {
SubstrateSigprofHandler sampler = new PosixSubstrateSigprofHandler();
SubstrateSigprofHandler sampler = makeNewSigprofHandler();
ImageSingletons.add(JfrExecutionSampler.class, sampler);
ImageSingletons.add(SubstrateSigprofHandler.class, sampler);

ThreadListenerSupport.get().register(sampler);
IsolateListenerSupport.singleton().register(sampler);
}
}

private static SubstrateSigprofHandler makeNewSigprofHandler() {
if (Platform.includedIn(Platform.LINUX.class)) {
return new LinuxSubstrateSigprofHandler();
} else if (Platform.includedIn(Platform.DARWIN.class)) {
return new DarwinSubstrateSigprofHandler();
} else {
throw VMError.shouldNotReachHere("The JFR-based sampler is not supported on this platform.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.darwin;

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

import org.graalvm.nativeimage.IsolateThread;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.posix.PosixSubstrateSigprofHandler;
import com.oracle.svm.core.posix.PosixUtils;
import com.oracle.svm.core.posix.headers.Time;
import com.oracle.svm.core.util.TimeUtils;

/**
* Darwin supports only global timer from POSIX (see {@link PosixSubstrateSigprofHandler}).
*/
public final class DarwinSubstrateSigprofHandler extends PosixSubstrateSigprofHandler {

@Platforms(Platform.HOSTED_ONLY.class)
public DarwinSubstrateSigprofHandler() {
}

@Override
protected void updateInterval() {
updateInterval(TimeUtils.millisToMicros(newIntervalMillis));
}

private static void updateInterval(long us) {
Time.itimerval newValue = UnsafeStackValue.get(Time.itimerval.class);
newValue.it_value().set_tv_sec(us / TimeUtils.microsPerSecond);
newValue.it_value().set_tv_usec(us % TimeUtils.microsPerSecond);
newValue.it_interval().set_tv_sec(us / TimeUtils.microsPerSecond);
newValue.it_interval().set_tv_usec(us % TimeUtils.microsPerSecond);

int status = Time.NoTransitions.setitimer(Time.TimerTypeEnum.ITIMER_PROF, newValue, WordFactory.nullPointer());
PosixUtils.checkStatusIs0(status, "setitimer(which, newValue, oldValue): wrong arguments.");
}

@Override
protected void installSignalHandler() {
super.installSignalHandler();
updateInterval();
}

@Override
protected void uninstallSignalHandler() {
/* Disable sampling. */
updateInterval(0);
}

@Override
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
protected void install0(IsolateThread thread) {
/* The timer is global, so nothing to do here. */
}

@Override
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
protected void uninstall0(IsolateThread thread) {
/* The timer is global, so nothing to do here. */
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public class Signal {
@CConstant
public static native int SIG_SETMASK();

@CConstant
public static native int SIGEV_SIGNAL();

@CFunction
public static native int sigprocmask(int how, sigset_tPointer set, sigset_tPointer oldset);

Expand Down Expand Up @@ -179,10 +182,22 @@ public interface sigaction extends PointerBase {
sigset_tPointer sa_mask();
}

@CStruct(addStructKeyword = true)
public interface sigevent extends PointerBase {
@CField
void sigev_notify(int value);

@CField
void sigev_signo(int value);
}

/** Don't call this function directly, see {@link PosixUtils#sigaction}. */
@CFunction
public static native int sigaction(int signum, sigaction act, sigaction oldact);

@CConstant
public static native int SIGPROF();

@CEnum
@CContext(PosixDirectives.class)
public enum SignalEnum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
import org.graalvm.nativeimage.c.constant.CConstant;
import org.graalvm.nativeimage.c.function.CFunction;
import org.graalvm.nativeimage.c.function.CLibrary;
import org.graalvm.nativeimage.c.struct.CFieldAddress;
import org.graalvm.nativeimage.c.struct.CPointerTo;
import org.graalvm.nativeimage.c.struct.CStruct;
import org.graalvm.word.PointerBase;

import com.oracle.svm.core.posix.headers.PosixDirectives;
import com.oracle.svm.core.posix.headers.Signal;
import com.oracle.svm.core.posix.headers.Time;

// Checkstyle: stop
Expand All @@ -39,6 +44,25 @@
*/
@CContext(PosixDirectives.class)
public class LinuxTime extends Time {

@CStruct
public interface timer_t extends PointerBase {
}

@CPointerTo(timer_t.class)
public interface timer_tPointer extends PointerBase {
timer_t read();
}

@CStruct(addStructKeyword = true)
public interface itimerspec extends PointerBase {
@CFieldAddress
Time.timespec it_interval();

@CFieldAddress
Time.timespec it_value();
}

@CConstant
public static native int CLOCK_MONOTONIC();

Expand All @@ -50,5 +74,14 @@ public static class NoTransitions {
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
@CLibrary("rt")
public static native int clock_gettime(int clock_id, timespec tp);

@CFunction(transition = CFunction.Transition.NO_TRANSITION)
public static native int timer_create(int clockid, Signal.sigevent sevp, timer_tPointer timerid);

@CFunction(transition = CFunction.Transition.NO_TRANSITION)
public static native int timer_settime(timer_t timerid, int flags, itimerspec newValue, itimerspec oldValue);

@CFunction(transition = CFunction.Transition.NO_TRANSITION)
public static native int timer_delete(timer_t timerid);
}
}
Loading

0 comments on commit e2c0a65

Please sign in to comment.