8295223: JFR: At most one native periodic event thread at a time

Reviewed-by: mgronlun
This commit is contained in:
Erik Gahlin 2022-10-13 15:53:33 +00:00
parent 7e4868de7b
commit c7f65438bb
2 changed files with 21 additions and 3 deletions

View File

@ -237,7 +237,11 @@ public final class PlatformEventType extends Type {
}
}
public boolean isEveryChunk() {
/**
* Returns true if "beginChunk", "endChunk" or "everyChunk" have
* been set.
*/
public boolean isChunkTime() {
return period == 0;
}

View File

@ -33,6 +33,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
import jdk.jfr.Event;
import jdk.jfr.EventType;
@ -40,6 +41,7 @@ import jdk.jfr.EventType;
public final class RequestEngine {
private static final JVM jvm = JVM.getJVM();
private static final ReentrantLock lock = new ReentrantLock();
static final class RequestHook {
private final Runnable hook;
@ -66,7 +68,7 @@ public final class RequestEngine {
if (type.isJDK()) {
hook.run();
} else {
jvm.emitEvent(type.getId(), JVM.counterTime(), 0);
emitJVMEvent(type);
}
if (Logger.shouldLog(LogTag.JFR_SYSTEM, LogLevel.DEBUG)) {
Logger.log(LogTag.JFR_SYSTEM, LogLevel.DEBUG, "Executed periodic hook for " + type.getLogName());
@ -80,6 +82,18 @@ public final class RequestEngine {
}
}
private void emitJVMEvent(PlatformEventType type) {
try {
// There should only be one thread in native at a time.
// ReentrantLock is used to avoid JavaMonitorBlocked event
// from synchronized block.
lock.lock();
jvm.emitEvent(type.getId(), JVM.counterTime(), 0);
} finally {
lock.unlock();
}
}
@SuppressWarnings("removal")
private void executeSecure() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@ -221,7 +235,7 @@ public final class RequestEngine {
long left = 0;
PlatformEventType es = he.type;
// Not enabled, skip.
if (!es.isEnabled() || es.isEveryChunk()) {
if (!es.isEnabled() || es.isChunkTime()) {
continue;
}
long r_period = es.getPeriod();