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-18215] SIGPROF signal handling #4536

Closed
wants to merge 3 commits into from
Closed
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,143 @@
/*
* 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.IsolateThread;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.c.function.CEntryPoint;
import org.graalvm.nativeimage.c.function.CEntryPointLiteral;
import org.graalvm.nativeimage.c.struct.SizeOf;
import org.graalvm.nativeimage.c.type.VoidPointer;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.annotate.RestrictHeapAccess;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.c.function.CEntryPointOptions;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.posix.headers.Pthread;
import com.oracle.svm.core.posix.headers.Signal;
import com.oracle.svm.core.posix.headers.Time;
import com.oracle.svm.core.sampler.SubstrateSigprofHandler;

@AutomaticFeature
@SuppressWarnings("unused")
class PosixSubstrateSigprofHandlerFeature implements Feature {
@Override
public void afterRegistration(AfterRegistrationAccess access) {
ImageSingletons.add(SubstrateSigprofHandler.class, new PosixSubstrateSigprofHandler());
}
}

public class PosixSubstrateSigprofHandler extends SubstrateSigprofHandler {

public static final long INTERVAL_S = 0;
public static final long INTERVAL_uS = 20_000;

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

/** The address of the signal handler for signals handled by Java code, below. */
private static final CEntryPointLiteral<Signal.AdvancedSignalDispatcher> advancedSignalDispatcher = CEntryPointLiteral.create(PosixSubstrateSigprofHandler.class,
"dispatch", int.class, Signal.siginfo_t.class, Signal.ucontext_t.class);

@SuppressWarnings("unused")
@CEntryPoint(include = CEntryPoint.NotIncludedAutomatically.class, publishAs = CEntryPoint.Publish.NotPublished)
@CEntryPointOptions(prologue = CEntryPointOptions.NoPrologue.class, epilogue = CEntryPointOptions.NoEpilogue.class)
@RestrictHeapAccess(access = RestrictHeapAccess.Access.NO_ALLOCATION, reason = "Must not allocate in sigprof signal handler.")
@Uninterruptible(reason = "Signal handler may only execute uninterruptible code.")
private static void dispatch(@SuppressWarnings("unused") int signalNumber, @SuppressWarnings("unused") Signal.siginfo_t sigInfo, Signal.ucontext_t uContext) {
tryEnterIsolateAndDoWalk(uContext);
}

private static void registerSigprofSignal() {
int structSigActionSize = SizeOf.get(Signal.sigaction.class);
Signal.sigaction structSigAction = StackValue.get(structSigActionSize);
LibC.memset(structSigAction, WordFactory.signed(0), WordFactory.unsigned(structSigActionSize));

/* Register sa_sigaction signal handler */
structSigAction.sa_flags(Signal.SA_SIGINFO() | Signal.SA_NODEFER());
structSigAction.sa_sigaction(advancedSignalDispatcher.getFunctionPointer());
Signal.sigaction(Signal.SignalEnum.SIGPROF.getCValue(), structSigAction, WordFactory.nullPointer());
}

private static int callSetitimer() {
/* Call setitimer to start profiling. */
Time.itimerval newValue = StackValue.get(Time.itimerval.class);
Time.itimerval oldValue = StackValue.get(Time.itimerval.class);

newValue.it_value().set_tv_sec(INTERVAL_S);
newValue.it_value().set_tv_usec(INTERVAL_uS);
newValue.it_interval().set_tv_sec(INTERVAL_S);
newValue.it_interval().set_tv_usec(INTERVAL_uS);

return Time.NoTransitions.setitimer(Time.TimerTypeEnum.ITIMER_PROF, newValue, oldValue);
}

@Override
protected void install0() {
registerSigprofSignal();
PosixUtils.checkStatusIs0(callSetitimer(), "setitimer(which, newValue, oldValue): wrong arguments.");
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected UnsignedWord createThreadLocalKey() {
Pthread.pthread_key_tPointer key = StackValue.get(Pthread.pthread_key_tPointer.class);
PosixUtils.checkStatusIs0(Pthread.pthread_key_create(key, WordFactory.nullPointer()), "pthread_key_create(key, keyDestructor): failed.");
return key.read();
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected void deleteThreadLocalKey(UnsignedWord key) {
int resultCode = Pthread.pthread_key_delete((Pthread.pthread_key_t) key);
PosixUtils.checkStatusIs0(resultCode, "pthread_key_delete(key): failed.");
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected void setThreadLocalKeyValue(UnsignedWord key, IsolateThread value) {
int resultCode = Pthread.pthread_setspecific((Pthread.pthread_key_t) key, (VoidPointer) value);
PosixUtils.checkStatusIs0(resultCode, "pthread_setspecific(key, value): wrong arguments.");
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected IsolateThread getThreadLocalKeyValue(UnsignedWord key) {
/*
* Although this method is not async-signal-safe in general we rely on
* implementation-specific behavior here.
*/
return (IsolateThread) Pthread.pthread_getspecific((Pthread.pthread_key_t) key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ public interface pthread_key_tPointer extends PointerBase {
@CFunction(transition = Transition.NO_TRANSITION)
public static native int pthread_key_create(pthread_key_tPointer key, PointerBase keyDestructor);

@CFunction(transition = Transition.NO_TRANSITION)
public static native int pthread_key_delete(pthread_key_t key);

@CFunction(transition = Transition.NO_TRANSITION)
public static native int pthread_setspecific(pthread_key_t key, VoidPointer value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,19 @@ public void afterCreateIsolate(Isolate isolate) {
}
}

@Uninterruptible(reason = "The isolate teardown is in progress.")
public void onIsolateTeardown() {
for (int i = 0; i < listeners.length; i++) {
listeners[i].onIsolateTeardown();
}
}

public interface IsolateListener {
@Uninterruptible(reason = "Thread state not yet set up.")
void afterCreateIsolate(Isolate isolate);

@Uninterruptible(reason = "The isolate teardown is in progress.")
default void onIsolateTeardown() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public boolean isCurrentThreadCpuTimeSupported() {
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getThreadCount() {
return threadCount.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ public interface JfrNativeEventWriterData extends PointerBase {
void setCurrentPos(Pointer value);

/**
* Returns the end position for the current event write. Writing of data cannot exceed this
* position.
* Returns the position where the buffer ends. Writing of data cannot exceed this position.
*/
@RawField
Pointer getEndPos();

/**
* Sets the end position for the current event write.
* Sets the position where the buffer ends.
*/
@RawField
void setEndPos(Pointer value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ public long getThreadId(Thread thread) {

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public long getThreadId(IsolateThread isolateThread) {
return threadLocal.getTraceId(isolateThread);
long threadId = threadLocal.getTraceId(isolateThread);
VMError.guarantee(threadId > 0);
return threadId;
}

/** See {@link JVM#storeMetadataDescriptor}. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.sampler;

import org.graalvm.nativeimage.c.struct.RawField;
import org.graalvm.nativeimage.c.struct.RawStructure;
import org.graalvm.word.Pointer;
import org.graalvm.word.PointerBase;
import org.graalvm.word.UnsignedWord;

/**
* A {@link SamplerBuffer} is a block of native memory into which the results of stack walks are
* written.
*/
@RawStructure
interface SamplerBuffer extends PointerBase {

/**
* Returns the buffer that is next in the {@link SamplerBufferStack}, otherwise null.
*/
@RawField
SamplerBuffer getNext();

/**
* Sets the successor to this buffer in the {@link SamplerBufferStack}.
*/
@RawField
void setNext(SamplerBuffer buffer);

/**
* Returns the JFR id of the thread that owns this buffer.
*/
@RawField
long getOwner();

/**
* Sets the JFR id of the thread that owns this buffer.
*/
@RawField
void setOwner(long threadId);

/**
* Returns the current position. Any data before this position is valid sample data.
*/
@RawField
Pointer getPos();

/**
* Sets the current position.
*/
@RawField
void setPos(Pointer pos);

/**
* Returns the size of the buffer. This excludes the header of the buffer.
*/
@RawField
UnsignedWord getSize();

/**
* Sets the size of the buffer.
*/
@RawField
void setSize(UnsignedWord value);

/**
* Should this buffer be freed after processing the data in it.
*/
@RawField
boolean getFreeable();

/**
* Sets the freeable status of the buffer.
*/
@RawField
void setFreeable(boolean freeable);
}
Loading