Skip to content
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
Expand Up @@ -53,8 +53,11 @@ public enum JfrEvent {
GCPhasePauseLevel4Event("jdk.GCPhasePauseLevel4"),
SafepointBegin("jdk.SafepointBegin"),
SafepointEnd("jdk.SafepointEnd"),
ThreadSleep("jdk.ThreadSleep"),
ExecuteVMOperation("jdk.ExecuteVMOperation"),
JavaMonitorEnter("jdk.JavaMonitorEnter");
JavaMonitorEnter("jdk.JavaMonitorEnter"),
JavaMonitorWait("jdk.JavaMonitorWait");


private final long id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ public long getStackTraceId(long eventTypeId, int skipCount) {
}
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public long getStackTraceId(JfrEvent eventType, int skipCount) {
return getStackTraceId(eventType.getId(), skipCount);
}

/** See {@link JVM#getStackTraceId}. */
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public long getStackTraceId(int skipCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static void writeExecutionSample(IsolateThread isolateThread, Thread.Stat
JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.ExecutionSample);
JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks());
JfrNativeEventWriter.putThread(data, isolateThread);
JfrNativeEventWriter.putLong(data, svm.getStackTraceId(JfrEvent.ExecutionSample.getId(), 0));
JfrNativeEventWriter.putLong(data, svm.getStackTraceId(JfrEvent.ExecutionSample, 0));
JfrNativeEventWriter.putLong(data, JfrThreadState.getId(threadState));
JfrNativeEventWriter.endSmallEvent(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.oracle.svm.core.jfr.JfrNativeEventWriterDataAccess;
import com.oracle.svm.core.jfr.JfrTicks;
import com.oracle.svm.core.jfr.SubstrateJVM;
import org.graalvm.compiler.word.Word;

public class JavaMonitorEnterEvent {
public static void emit(Object obj, long previousOwnerTid, long startTicks) {
Expand All @@ -54,10 +55,11 @@ public static void emit0(Object obj, long previousOwnerTid, long startTicks) {
JfrNativeEventWriter.putLong(data, startTicks);
JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks() - startTicks);
JfrNativeEventWriter.putEventThread(data);
JfrNativeEventWriter.putLong(data, SubstrateJVM.get().getStackTraceId(JfrEvent.JavaMonitorEnter.getId(), 0));
JfrNativeEventWriter.putLong(data, SubstrateJVM.get().getStackTraceId(JfrEvent.JavaMonitorEnter, 0));
JfrNativeEventWriter.putClass(data, obj.getClass());

JfrNativeEventWriter.putLong(data, previousOwnerTid);
JfrNativeEventWriter.putLong(data, org.graalvm.compiler.word.Word.objectToUntrackedPointer(obj).rawValue());
JfrNativeEventWriter.putLong(data, Word.objectToUntrackedPointer(obj).rawValue());
JfrNativeEventWriter.endSmallEvent(data);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2022, Red Hat Inc. 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.jfr.events;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.jfr.JfrEvent;
import com.oracle.svm.core.jfr.JfrNativeEventWriter;
import com.oracle.svm.core.jfr.JfrNativeEventWriterData;
import com.oracle.svm.core.jfr.JfrNativeEventWriterDataAccess;
import com.oracle.svm.core.jfr.JfrTicks;
import com.oracle.svm.core.jfr.SubstrateJVM;
import org.graalvm.compiler.word.Word;
import com.oracle.svm.core.jfr.HasJfrSupport;
public class JavaMonitorWaitEvent {
public static void emit(long startTicks, Object obj, long notifier, long timeout, boolean timedOut) {
if (HasJfrSupport.get()) {
emit0(startTicks, obj, notifier, timeout, timedOut);
}
}

@Uninterruptible(reason = "Accesses a JFR buffer.")
private static void emit0(long startTicks, Object obj, long notifier, long timeout, boolean timedOut) {
if (SubstrateJVM.isRecording() && SubstrateJVM.get().isEnabled(JfrEvent.JavaMonitorWait)) {
JfrNativeEventWriterData data = org.graalvm.nativeimage.StackValue.get(JfrNativeEventWriterData.class);
JfrNativeEventWriterDataAccess.initializeThreadLocalNativeBuffer(data);
JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.JavaMonitorWait);
JfrNativeEventWriter.putLong(data, startTicks);
JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks() - startTicks);
JfrNativeEventWriter.putEventThread(data);
JfrNativeEventWriter.putLong(data, SubstrateJVM.get().getStackTraceId(JfrEvent.JavaMonitorWait, 0));
JfrNativeEventWriter.putClass(data, obj.getClass());
JfrNativeEventWriter.putLong(data, notifier);
JfrNativeEventWriter.putLong(data, timeout);
JfrNativeEventWriter.putBoolean(data, timedOut);
JfrNativeEventWriter.putLong(data, Word.objectToUntrackedPointer(obj).rawValue());
JfrNativeEventWriter.endSmallEvent(data);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2022, Red Hat Inc. 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.jfr.events;

import org.graalvm.nativeimage.StackValue;

import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.jfr.JfrEvent;
import com.oracle.svm.core.jfr.JfrNativeEventWriter;
import com.oracle.svm.core.jfr.JfrNativeEventWriterData;
import com.oracle.svm.core.jfr.JfrNativeEventWriterDataAccess;
import com.oracle.svm.core.jfr.JfrTicks;
import com.oracle.svm.core.jfr.SubstrateJVM;

public class ThreadSleepEvent {

public static void emit(long time, long startTicks) {
if (com.oracle.svm.core.jfr.HasJfrSupport.get()) {
emit0(time, startTicks);
}
}

@Uninterruptible(reason = "Accesses a JFR buffer.")
private static void emit0(long time, long startTicks) {
if (SubstrateJVM.isRecording() && SubstrateJVM.get().isEnabled(JfrEvent.ThreadSleep)) {
JfrNativeEventWriterData data = StackValue.get(JfrNativeEventWriterData.class);
JfrNativeEventWriterDataAccess.initializeThreadLocalNativeBuffer(data);

JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.ThreadSleep);
JfrNativeEventWriter.putLong(data, startTicks);
JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks() - startTicks);
JfrNativeEventWriter.putEventThread(data);
JfrNativeEventWriter.putLong(data, SubstrateJVM.get().getStackTraceId(JfrEvent.ThreadSleep, 0));
JfrNativeEventWriter.putLong(data, time);
JfrNativeEventWriter.endSmallEvent(data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void emit(IsolateThread isolateThread) {
JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.ThreadStart);
JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks());
JfrNativeEventWriter.putEventThread(data);
JfrNativeEventWriter.putLong(data, svm.getStackTraceId(JfrEvent.ThreadStart.getId(), 0));
JfrNativeEventWriter.putLong(data, svm.getStackTraceId(JfrEvent.ThreadStart, 0));
JfrNativeEventWriter.putThread(data, isolateThread);
JfrNativeEventWriter.putLong(data, SubstrateJVM.getParentThreadId(isolateThread));
JfrNativeEventWriter.endSmallEvent(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

package com.oracle.svm.core.monitor;

import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import org.graalvm.nativeimage.CurrentIsolate;
Expand All @@ -34,12 +38,88 @@
import com.oracle.svm.core.jfr.JfrTicks;
import com.oracle.svm.core.jfr.SubstrateJVM;
import com.oracle.svm.core.jfr.events.JavaMonitorEnterEvent;
import com.oracle.svm.core.jfr.events.JavaMonitorWaitEvent;

public class JavaMonitor extends ReentrantLock {
private static final long serialVersionUID = 3921577070627519721L;

private int finishedWaiters = 0;
private Condition condition;

private LinkedList<Waiter> waiters = new LinkedList<>();
private long latestJfrTid;

private long getNotifierTid() {
assert isHeldByCurrentThread();
long curr = Thread.currentThread().getId();
Waiter target = null;
// No guarantee on the order in which the waiters reacquire the lock.
// Must compare TIDs. Can be expensive if many waiters and lock acquisition order is opposite wait order.
for (Waiter waiter : waiters) {
if (waiter.getWaiterTid() == curr) {
target = waiter;
break;
}
}
assert waiters.remove(target);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only executed when assertions are enabled, so you are currently leaking memory.

finishedWaiters--;

return target.getNotifierTid(); // notified if TID > 0
}

public void setNotifier(boolean notifyAll) {
assert isHeldByCurrentThread();

// If there are extra notifications, there will be no waiters to respond to them, so don't record them.
if (finishedWaiters == waiters.size()) {
return;
}

long curr = Thread.currentThread().getId();
int notifications = 1;
if (notifyAll) {
notifications = waiters.size() - finishedWaiters;
}
Collection<Thread> waitingThreads = this.getWaitingThreads(condition);
while (notifications > 0 && finishedWaiters < waiters.size()) {
Waiter waiter = waiters.get(finishedWaiters);

// Only add NotifierTid to queue if the thread is still waiting.
// waitingThreads collection is not guaranteed to be in any order.
if (waitingThreads.contains(waiter.getThread())) {
waiter.setNotifierTid(curr);
notifications--;
} else if (notifyAll) {
notifications--;
}
finishedWaiters++;
}
}

public void doWait(Object obj, long timeoutMillis, Condition c) throws InterruptedException {
this.condition = c;
waiters.addLast(new Waiter());
long startTicks = com.oracle.svm.core.jfr.JfrTicks.elapsedTicks();
try {
if (timeoutMillis == 0L) {
this.condition.await();
JavaMonitorWaitEvent.emit(startTicks, obj, getNotifierTid(), timeoutMillis, false);
} else {
if (this.condition.await(timeoutMillis, TimeUnit.MILLISECONDS)) {
JavaMonitorWaitEvent.emit(startTicks, obj, getNotifierTid(), timeoutMillis, false);
} else {
// remove waiter from queue and check it wasn't notified
assert getNotifierTid() < 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only executed when assertions are enabled, so probably not what you want.
Besides that, the thread won't hold the lock when a wait times out. So, the assertion in `getNotifierTid()So, the assertion in getNotifierTid() should fail and without assertions there will be a race. should fail. Without assertions enabled there will be a race.

JavaMonitorWaitEvent.emit(startTicks, obj, 0, timeoutMillis, true);
}
}
} catch (InterruptedException e) {
// Similar to hotspot, we should not emit event if interrupted
getNotifierTid();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the thread is interrupted, it won't hold the lock. So, the assertion in getNotifierTid() should fail. Without assertions enabled there will be a race.

throw e; // pass it back up in case it needs to be handled elsewhere
}
}

public JavaMonitor() {
Target_java_util_concurrent_locks_ReentrantLock lock = SubstrateUtil.cast(this, Target_java_util_concurrent_locks_ReentrantLock.class);
Target_java_util_concurrent_locks_ReentrantLock_NonfairSync sync = SubstrateUtil.cast(lock.sync, Target_java_util_concurrent_locks_ReentrantLock_NonfairSync.class);
Expand Down Expand Up @@ -77,4 +157,29 @@ public void monitorEnter(Object obj) {

latestJfrTid = SubstrateJVM.getThreadId(CurrentIsolate.getCurrentThread());
}

class Waiter {
private Thread thread;
private long notifierTid = -1;

Waiter() {
this.thread = Thread.currentThread();
}

public Thread getThread() {
return thread;
}

public long getNotifierTid() {
return notifierTid;
}

public long getWaiterTid() {
return thread.getId();
}

public void setNotifierTid(long notifierTid) {
this.notifierTid = notifierTid;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractOwnableSynchronizer;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
Expand Down Expand Up @@ -374,16 +373,12 @@ public boolean isLockedByAnyThread(Object obj) {
@Override
protected void doWait(Object obj, long timeoutMillis) throws InterruptedException {
/*
* Ensure that the current thread holds the lock. Required by the specification of
* Object.wait, and also required for our implementation.
* Ensure that the current thread holds the lock. Required by the specification
* of Object.wait, and also required for our implementation.
*/
JavaMonitor lock = ensureLocked(obj);
Condition condition = getOrCreateCondition(lock, true);
if (timeoutMillis == 0L) {
condition.await();
} else {
condition.await(timeoutMillis, TimeUnit.MILLISECONDS);
}
lock.doWait(obj, timeoutMillis, condition);
}

@Override
Expand All @@ -394,6 +389,7 @@ public void notify(Object obj, boolean notifyAll) {
Condition condition = getOrCreateCondition(lock, false);
/* If the receiver does not have a condition, then it has not been waited on. */
if (condition != null) {
lock.setNotifier(notifyAll);
if (notifyAll) {
condition.signalAll();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.oracle.svm.core.monitor;

import com.oracle.svm.core.annotate.Uninterruptible;

import com.oracle.svm.core.jfr.events.JavaMonitorWaitEvent;
/**
* Without support for threads, there is no need for any monitor operations.
*/
Expand Down Expand Up @@ -74,7 +74,9 @@ protected void doWait(Object obj, long timeoutMillis) throws InterruptedExceptio
* questionable whether this implementation is useful, especially waiting without a timeout.
* But it is the best thing we can do.
*/
long startTicks = com.oracle.svm.core.jfr.JfrTicks.elapsedTicks();
Thread.sleep(timeoutMillis == 0 ? Long.MAX_VALUE : timeoutMillis);
JavaMonitorWaitEvent.emit(startTicks, obj, 0, timeoutMillis, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.oracle.svm.core.jfr.events.ThreadSleepEvent;

import org.graalvm.compiler.api.replacements.Fold;
import org.graalvm.compiler.core.common.SuppressFBWarnings;
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
Expand Down Expand Up @@ -301,11 +303,13 @@ static void initializeNewThread(
}

static void sleep(long millis) throws InterruptedException {
long startTicks = com.oracle.svm.core.jfr.JfrTicks.elapsedTicks();
if (supportsVirtual() && isVirtualDisallowLoom(Thread.currentThread())) {
VirtualThreads.singleton().sleepMillis(millis);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does HotSpot really emit this JFR event for virtual threads?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it out using virtual threads with openjdk and the event does get emitted. Please see here

} else {
PlatformThreads.sleep(millis);
}
ThreadSleepEvent.emit(millis, startTicks);
}

static boolean isAlive(Thread thread) {
Expand Down