Skip to content

Commit

Permalink
JFR and Sampler code improvements and bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanstevanovic committed Nov 29, 2022
1 parent 45a1400 commit 4ae514e
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static void registerSigprofSignal() {
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_flags(Signal.SA_SIGINFO() | Signal.SA_NODEFER() | Signal.SA_RESTART());
structSigAction.sa_sigaction(advancedSignalDispatcher.getFunctionPointer());
Signal.sigaction(Signal.SignalEnum.SIGPROF.getCValue(), structSigAction, WordFactory.nullPointer());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import com.oracle.svm.core.deopt.SubstrateInstalledCode;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.heap.Heap;
import com.oracle.svm.core.heap.RestrictHeapAccess;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.thread.VMOperation;
import com.oracle.svm.core.util.VMError;
Expand Down Expand Up @@ -485,8 +484,8 @@ public SingleShotFrameInfoQueryResultAllocator reload() {
return this;
}

@RestrictHeapAccess(access = RestrictHeapAccess.Access.NO_ALLOCATION, reason = "Provide allocation-free StackFrameVisitor")
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public FrameInfoQueryResult newFrameInfoQueryResult() {
if (fired) {
return null;
Expand All @@ -498,26 +497,26 @@ public FrameInfoQueryResult newFrameInfoQueryResult() {
}

public static class DummyValueInfoAllocator implements ValueInfoAllocator {
@RestrictHeapAccess(access = RestrictHeapAccess.Access.NO_ALLOCATION, reason = "Provide allocation-free StackFrameVisitor")
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public FrameInfoQueryResult.ValueInfo newValueInfo() {
return null;
}

@RestrictHeapAccess(access = RestrictHeapAccess.Access.NO_ALLOCATION, reason = "Provide allocation-free StackFrameVisitor")
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public FrameInfoQueryResult.ValueInfo[] newValueInfoArray(int len) {
return null;
}

@RestrictHeapAccess(access = RestrictHeapAccess.Access.NO_ALLOCATION, reason = "Provide allocation-free StackFrameVisitor")
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public FrameInfoQueryResult.ValueInfo[][] newValueInfoArrayArray(int len) {
return null;
}

@RestrictHeapAccess(access = RestrictHeapAccess.Access.NO_ALLOCATION, reason = "Provide allocation-free StackFrameVisitor")
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void decodeConstant(FrameInfoQueryResult.ValueInfo valueInfo, NonmovableObjectArray<?> frameInfoObjectConstants) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public FrameInfoQueryResult() {
init();
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void init() {
caller = null;
deoptMethod = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.oracle.svm.core.jfr;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.StackValue;
Expand All @@ -32,6 +33,7 @@
import org.graalvm.nativeimage.c.struct.RawStructure;
import org.graalvm.nativeimage.c.struct.SizeOf;
import org.graalvm.nativeimage.c.type.CIntPointer;
import org.graalvm.nativeimage.impl.UnmanagedMemorySupport;
import org.graalvm.word.Pointer;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;
Expand All @@ -48,8 +50,6 @@
import com.oracle.svm.core.jfr.traceid.JfrTraceIdEpoch;
import com.oracle.svm.core.jfr.utils.JfrVisited;
import com.oracle.svm.core.locks.VMMutex;
import com.oracle.svm.core.sampler.SamplerBuffer;
import com.oracle.svm.core.sampler.SamplerBufferAccess;
import com.oracle.svm.core.sampler.SamplerSampleWriter;
import com.oracle.svm.core.sampler.SamplerSampleWriterData;
import com.oracle.svm.core.sampler.SamplerSampleWriterDataAccess;
Expand Down Expand Up @@ -119,7 +119,7 @@ public long getStackTraceId(int skipCount) {
Pointer start = data.getStartPos().add(SamplerSampleWriter.getHeaderSize());
stackTraceId = getStackTraceId(start, data.getCurrentPos(), data.getHashCode(), status, false);
if (JfrStackTraceTableEntryStatus.get(status, JfrStackTraceTableEntryStatus.NEW)) {
SamplerSampleWriter.end(data);
SamplerSampleWriter.end(data, SamplerSampleWriter.JFR_STACK_TRACE_END);
}
} finally {
releaseLock();
Expand All @@ -138,6 +138,7 @@ public long getStackTraceId(Pointer start, Pointer end, int hashCode, CIntPointe
UnsignedWord size = end.subtract(start);
entry.setHash(hashCode);
entry.setSize((int) size.rawValue());
entry.setSerialized(false);
/* Do not copy stacktrace into new entry unless it is necessary. */
entry.setStackTrace(start);

Expand All @@ -147,15 +148,20 @@ public long getStackTraceId(Pointer start, Pointer end, int hashCode, CIntPointe
return result.getId();
} else {
/* Replace the previous pointer with new one (entry size and hash remains the same). */
SamplerBuffer buffer = SamplerBufferAccess.allocate(size);
Pointer to = SamplerBufferAccess.getDataStart(buffer);
entry.setStackTrace(to);
/* Copy the stacktrace into separate native memory entry in hashtable. */
UnmanagedMemoryUtil.copy(start, to, size);

JfrStackTraceTableEntry newEntry = (JfrStackTraceTableEntry) epochData.visitedStackTraces.getOrPut(entry);
JfrStackTraceTableEntryStatus.update(newEntry, status, true, false, isSerializationInProgress);
return newEntry.getId();
Pointer to = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(size);
if (to.isNull()) {
/* There is not enough space to allocate a new buffer. */
JfrStackTraceTableEntryStatus.failStatus(status);
return 0;
} else {
entry.setStackTrace(to);
/* Copy the stacktrace into separate native memory entry in hashtable. */
UnmanagedMemoryUtil.copy(start, to, size);

JfrStackTraceTableEntry newEntry = (JfrStackTraceTableEntry) epochData.visitedStackTraces.getOrPut(entry);
JfrStackTraceTableEntryStatus.update(newEntry, status, true, false, isSerializationInProgress);
return newEntry.getId();
}
}
}

Expand Down Expand Up @@ -294,20 +300,35 @@ protected UninterruptibleEntry copyToHeap(UninterruptibleEntry valueOnStack) {
result.setId(++nextId);
return result;
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected void free(UninterruptibleEntry entry) {
JfrStackTraceTableEntry stackTraceEntry = (JfrStackTraceTableEntry) entry;
/* The base method will free only the entry itself, not the pointer with stacktrace. */
ImageSingletons.lookup(UnmanagedMemorySupport.class).free(stackTraceEntry.getStackTrace());
super.free(entry);
}
}

public static class JfrStackTraceTableEntryStatus {
public static final int NEW = 1;
public static final int SHOULD_SERIALIZE = NEW << 1;
public static final int SERIALIZED = SHOULD_SERIALIZE << 1;
public static final int FAILED = SERIALIZED << 1;

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static void failStatus(CIntPointer status) {
status.write(FAILED);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static void update(JfrStackTraceTableEntry entry, CIntPointer status, boolean setNew, boolean isAlreadySerialized, boolean isSerializationInProgress) {
int isRecorded = setNew ? NEW : 0;
int isNew = setNew ? NEW : 0;
int shouldSerialize = !isAlreadySerialized ? SHOULD_SERIALIZE : 0;
int isSerialized = isAlreadySerialized ? SERIALIZED : 0;
status.write(isRecorded | shouldSerialize | isSerialized);
if (!entry.getSerialized() && isSerializationInProgress) {
status.write(isNew | shouldSerialize | isSerialized);
if (!isAlreadySerialized && isSerializationInProgress) {
entry.setSerialized(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ public void setMethodSamplingInterval(long type, long intervalMillis) {

if (millis > 0) {
SubstrateJVM.get().setEnabled(type, true);
/* Stacktrace walk is disabled by default for ExecutionSample. */
SubstrateJVM.get().setStackTraceEnabled(type, true);
} else {
millis = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class ExecutionSampleEvent {

@Uninterruptible(reason = "Called from uninterruptible code.", calleeMustBe = false)
public static void tryToRegisterExecutionSampleEventCallback() {
if (SubstrateJVM.get().isEnabled(JfrEvent.ExecutionSample) && intervalMillis > 0) {
if (intervalMillis > 0) {
ImageSingletons.lookup(ThreadingSupport.class).registerRecurringCallback(intervalMillis, TimeUnit.MILLISECONDS, CALLBACK);
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public void run(Threading.RecurringCallbackAccess access) {
ExecutionSampleEvent.writeExecutionSample(
JfrTicks.elapsedTicks(),
SubstrateJVM.getThreadId(isolateThread),
SubstrateJVM.get().getStackTraceId(JfrEvent.ExecutionSample.getId(), 0),
SubstrateJVM.get().getStackTraceId(JfrEvent.ExecutionSample, 0),
JfrThreadState.getId(javaThread.getState()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

package com.oracle.svm.core.sampler;

import static com.oracle.svm.core.jfr.JfrStackTraceRepository.JfrStackTraceTableEntryStatus.FAILED;
import static com.oracle.svm.core.jfr.JfrStackTraceRepository.JfrStackTraceTableEntryStatus.SERIALIZED;
import static com.oracle.svm.core.jfr.JfrStackTraceRepository.JfrStackTraceTableEntryStatus.SHOULD_SERIALIZE;

import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.c.function.CodePointer;
import org.graalvm.nativeimage.c.type.CIntPointer;
Expand Down Expand Up @@ -119,18 +123,30 @@ public static void processSamplerBuffer(SamplerBuffer buffer) {
stackTraceRepo.acquireLock();
try {
long stackTraceId = stackTraceRepo.getStackTraceId(current, current.add(sampleSize), sampleHash, status, true);
if (JfrStackTraceRepository.JfrStackTraceTableEntryStatus.get(status, JfrStackTraceRepository.JfrStackTraceTableEntryStatus.SERIALIZED)) {
ExecutionSampleEvent.writeExecutionSample(sampleTick, buffer.getOwner(), stackTraceId, threadState);
/* Sample is already there, skip the rest of sample plus END_MARK symbol. */
current = current.add(sampleSize).add(SamplerSampleWriter.END_MARKER_SIZE);
boolean serialized = JfrStackTraceRepository.JfrStackTraceTableEntryStatus.get(status, SERIALIZED);
boolean failed = JfrStackTraceRepository.JfrStackTraceTableEntryStatus.get(status, FAILED);
if (serialized || failed) {
/*
* Sample/Stack is already there or there is not enough memory to operate, skip
* the rest of the data.
*/
current = current.add(sampleSize);
long endMarker = current.readLong(0);
if (endMarker == SamplerSampleWriter.SAMPLE_EVENT_DATA_END && serialized) {
ExecutionSampleEvent.writeExecutionSample(sampleTick, buffer.getOwner(), stackTraceId, threadState);
}
current = current.add(SamplerSampleWriter.END_MARKER_SIZE);
} else {
assert JfrStackTraceRepository.JfrStackTraceTableEntryStatus.get(status, JfrStackTraceRepository.JfrStackTraceTableEntryStatus.SHOULD_SERIALIZE);
assert JfrStackTraceRepository.JfrStackTraceTableEntryStatus.get(status, SHOULD_SERIALIZE);
/* Sample is not there. Start walking a stacktrace. */
stackTraceRepo.serializeStackTraceHeader(stackTraceId, isTruncated, sampleSize / SamplerSampleWriter.IP_SIZE);
while (current.belowThan(end)) {
long ip = current.readLong(0);
if (ip == SamplerSampleWriter.END_MARKER) {
ExecutionSampleEvent.writeExecutionSample(sampleTick, buffer.getOwner(), stackTraceId, threadState);
/* Check if we hit any of the end markers. */
if (ip == SamplerSampleWriter.JFR_STACK_TRACE_END || ip == SamplerSampleWriter.SAMPLE_EVENT_DATA_END) {
if (ip == SamplerSampleWriter.SAMPLE_EVENT_DATA_END) {
ExecutionSampleEvent.writeExecutionSample(sampleTick, buffer.getOwner(), stackTraceId, threadState);
}
current = current.add(SamplerSampleWriter.END_MARKER_SIZE);
break;
} else {
Expand All @@ -143,6 +159,7 @@ public static void processSamplerBuffer(SamplerBuffer buffer) {
stackTraceRepo.releaseLock();
}
}
SamplerBufferAccess.reinitialize(buffer);
}

@Uninterruptible(reason = "The handle should only be accessed from uninterruptible code to prevent that the GC frees the CodeInfo.", callerMustBe = true)
Expand Down
Loading

0 comments on commit 4ae514e

Please sign in to comment.