Skip to content

Commit e0fb949

Browse files
committed
8335779: JFR: Hide sleep events
Reviewed-by: mgronlun
1 parent 537d20a commit e0fb949

File tree

14 files changed

+192
-100
lines changed

14 files changed

+192
-100
lines changed

src/hotspot/share/jfr/support/jfrIntrinsics.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class JfrIntrinsicSupport : AllStatic {
4747
#define JFR_HAVE_INTRINSICS
4848

4949
#define JFR_TEMPLATES(template) \
50-
template(jdk_jfr_internal_HiddenWait, "jdk/jfr/internal/HiddenWait") \
50+
template(jdk_jfr_internal_management_HiddenWait, "jdk/jfr/internal/management/HiddenWait") \
5151
template(jdk_jfr_internal_JVM, "jdk/jfr/internal/JVM") \
5252
template(jdk_jfr_internal_event_EventWriterFactory, "jdk/jfr/internal/event/EventWriterFactory") \
5353
template(jdk_jfr_internal_event_EventConfiguration_signature, "Ljdk/jfr/internal/event/EventConfiguration;") \

src/hotspot/share/runtime/objectMonitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ bool ObjectMonitor::check_owner(TRAPS) {
14421442
static inline bool is_excluded(const Klass* monitor_klass) {
14431443
assert(monitor_klass != nullptr, "invariant");
14441444
NOT_JFR_RETURN_(false);
1445-
JFR_ONLY(return vmSymbols::jdk_jfr_internal_HiddenWait() == monitor_klass->name();)
1445+
JFR_ONLY(return vmSymbols::jdk_jfr_internal_management_HiddenWait() == monitor_klass->name();)
14461446
}
14471447

14481448
static void post_monitor_wait_event(EventJavaMonitorWait* event,

src/jdk.jfr/share/classes/jdk/jfr/internal/JVM.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import jdk.jfr.Event;
3232
import jdk.jfr.internal.event.EventConfiguration;
3333
import jdk.jfr.internal.event.EventWriter;
34+
import jdk.jfr.internal.management.HiddenWait;
3435

3536
/**
3637
* Interface against the JVM.

src/jdk.jfr/share/classes/jdk/jfr/internal/JVMSupport.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import jdk.jfr.Recording;
3232
import jdk.jfr.internal.event.EventConfiguration;
33+
import jdk.jfr.internal.management.HiddenWait;
3334
import jdk.jfr.internal.util.Utils;
3435
import jdk.jfr.internal.util.ValueFormatter;
3536

@@ -118,7 +119,8 @@ private static void awaitUniqueTimestamp() {
118119
lastTimestamp = time;
119120
return;
120121
}
121-
Utils.takeNap(1);
122+
HiddenWait hiddenWait = new HiddenWait();
123+
hiddenWait.takeNap(1);
122124
}
123125
}
124126

src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import jdk.jfr.ValueDescriptor;
4848
import jdk.jfr.internal.consumer.RepositoryFiles;
4949
import jdk.jfr.internal.event.EventConfiguration;
50+
import jdk.jfr.internal.management.HiddenWait;
5051
import jdk.jfr.internal.periodic.PeriodicEvents;
5152
import jdk.jfr.internal.util.Utils;
5253

@@ -57,6 +58,7 @@ public final class MetadataRepository {
5758
private final Map<String, EventType> nativeEventTypes = LinkedHashMap.newHashMap(150);
5859
private final Map<String, EventControl> nativeControls = LinkedHashMap.newHashMap(150);
5960
private final SettingsManager settingsManager = new SettingsManager();
61+
private final HiddenWait threadSleeper = new HiddenWait();
6062
private Constructor<EventConfiguration> cachedEventConfigurationConstructor;
6163
private boolean staleMetadata = true;
6264
private boolean unregistered;
@@ -341,7 +343,7 @@ private void awaitEpochMilliShift() {
341343
lastMillis = millis;
342344
return;
343345
}
344-
Utils.takeNap(1);
346+
threadSleeper.takeNap(1);
345347
}
346348
}
347349

src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/OngoingStream.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,7 @@
3333
import jdk.jfr.internal.SecuritySupport;
3434
import jdk.jfr.internal.SecuritySupport.SafePath;
3535
import jdk.jfr.internal.management.EventByteStream;
36+
import jdk.jfr.internal.management.HiddenWait;
3637
import jdk.jfr.internal.management.ManagementSupport;
3738

3839
public final class OngoingStream extends EventByteStream {
@@ -44,6 +45,7 @@ public final class OngoingStream extends EventByteStream {
4445

4546
private final RepositoryFiles repositoryFiles;
4647
private final Recording recording;
48+
private final HiddenWait threadSleeper = new HiddenWait();
4749
private final int blockSize;
4850
private final long endTimeNanos;
4951
private final byte[] headerBytes = new byte[HEADER_SIZE];
@@ -195,19 +197,13 @@ private byte[] readWithHeader(int size) throws IOException {
195197
return bytes;
196198
}
197199
}
198-
takeNap();
200+
if (!threadSleeper.takeNap(10)) {
201+
throw new IOException("Read operation interrupted");
202+
}
199203
}
200204
return EMPTY_ARRAY;
201205
}
202206

203-
private void takeNap() throws IOException {
204-
try {
205-
Thread.sleep(10);
206-
} catch (InterruptedException ie) {
207-
throw new IOException("Read operation interrupted", ie);
208-
}
209-
}
210-
211207
private boolean ensureInput() throws IOException {
212208
if (input == null) {
213209
if (SecuritySupport.getFileSize(new SafePath(path)) < HEADER_SIZE) {

src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/RecordingInput.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,8 @@
3131
import java.io.IOException;
3232
import java.io.RandomAccessFile;
3333
import java.nio.file.Path;
34+
35+
import jdk.jfr.internal.management.HiddenWait;
3436
import jdk.jfr.internal.util.Utils;
3537

3638
public final class RecordingInput implements DataInput, AutoCloseable {
@@ -67,6 +69,7 @@ public void reset() {
6769
}
6870
private final int blockSize;
6971
private final FileAccess fileAccess;
72+
private final HiddenWait threadSleeper = new HiddenWait();
7073
private long pollCount = 1000;
7174
private RandomAccessFile file;
7275
private String filename;
@@ -453,6 +456,6 @@ public void pollWait() throws IOException {
453456
if (pollCount < 0) {
454457
throw new IOException("Recording file is stuck in locked stream state.");
455458
}
456-
Utils.takeNap(1);
459+
threadSleeper.takeNap(1);
457460
}
458461
}

src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/RepositoryFiles.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@
4646
import jdk.jfr.internal.Logger;
4747
import jdk.jfr.internal.Repository;
4848
import jdk.jfr.internal.SecuritySupport.SafePath;
49+
import jdk.jfr.internal.management.HiddenWait;;
4950

5051
public final class RepositoryFiles {
51-
private static final Object WAIT_OBJECT = new Object();
52+
private static final HiddenWait WAIT_OBJECT = new HiddenWait();
5253
private static final String DIRECTORY_PATTERN = "DDDD_DD_DD_DD_DD_DD_";
5354
public static void notifyNewFile() {
5455
synchronized (WAIT_OBJECT) {
@@ -59,15 +60,15 @@ public static void notifyNewFile() {
5960
private final FileAccess fileAccess;
6061
private final NavigableMap<Long, Path> pathSet = new TreeMap<>();
6162
private final Map<Path, Long> pathLookup = new HashMap<>();
62-
private final Object waitObject;
63+
private final HiddenWait waitObject;
6364
private boolean allowSubDirectory;
6465
private volatile boolean closed;
6566
private Path repository;
6667

6768
public RepositoryFiles(FileAccess fileAccess, Path repository, boolean allowSubDirectory) {
6869
this.repository = repository;
6970
this.fileAccess = fileAccess;
70-
this.waitObject = repository == null ? WAIT_OBJECT : new Object();
71+
this.waitObject = repository == null ? WAIT_OBJECT : new HiddenWait();
7172
this.allowSubDirectory = allowSubDirectory;
7273
}
7374

@@ -108,7 +109,7 @@ private boolean updatePaths(boolean wait) {
108109
// was accessed. Just ignore, and retry later.
109110
}
110111
if (wait) {
111-
nap();
112+
waitObject.takeNap(1000);
112113
} else {
113114
return pathLookup.size() > beforeSize;
114115
}
@@ -157,16 +158,6 @@ private Path path(long timestamp, boolean wait) {
157158
}
158159
}
159160

160-
private void nap() {
161-
try {
162-
synchronized (waitObject) {
163-
waitObject.wait(1000);
164-
}
165-
} catch (InterruptedException e) {
166-
// ignore
167-
}
168-
}
169-
170161
private boolean updatePaths() throws IOException, DirectoryIteratorException {
171162
boolean foundNew = false;
172163
Path repoPath = repository;

src/jdk.jfr/share/classes/jdk/jfr/internal/HiddenWait.java renamed to src/jdk.jfr/share/classes/jdk/jfr/internal/management/HiddenWait.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25-
package jdk.jfr.internal;
25+
package jdk.jfr.internal.management;
2626

2727
/**
2828
* The HiddenWait class is used to exclude jdk.JavaMonitorWait events
2929
* from being generated when Object.wait() is called on an object of this type.
3030
*/
3131
public final class HiddenWait {
32+
33+
public synchronized boolean takeNap(long timeoutMillis) {
34+
try {
35+
this.wait(timeoutMillis);
36+
return true;
37+
} catch (InterruptedException e) {
38+
// Ok, ignore
39+
return false;
40+
}
41+
}
3242
}

src/jdk.jfr/share/classes/jdk/jfr/internal/management/StreamBarrier.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,45 +39,57 @@
3939
* processing should not continue.
4040
*/
4141
public final class StreamBarrier implements Closeable {
42-
42+
private final HiddenWait lock = new HiddenWait();
4343
private boolean activated = false;
4444
private boolean used = false;
4545
private long end = Long.MAX_VALUE;
4646

4747
// Blocks thread until barrier is deactivated
48-
public synchronized void check() {
49-
while (activated) {
50-
try {
51-
this.wait();
52-
} catch (InterruptedException e) {
53-
Thread.currentThread().interrupt();
48+
public void check() {
49+
synchronized (lock) {
50+
while (activated) {
51+
try {
52+
lock.wait();
53+
} catch (InterruptedException e) {
54+
Thread.currentThread().interrupt();
55+
}
5456
}
5557
}
5658
}
5759

58-
public synchronized void setStreamEnd(long timestamp) {
59-
end = timestamp;
60+
public void setStreamEnd(long timestamp) {
61+
synchronized(lock) {
62+
end = timestamp;
63+
}
6064
}
6165

62-
public synchronized long getStreamEnd() {
63-
return end;
66+
public long getStreamEnd() {
67+
synchronized(lock) {
68+
return end;
69+
}
6470
}
6571

66-
public synchronized void activate() {
67-
activated = true;
68-
used = true;
72+
public void activate() {
73+
synchronized (lock) {
74+
activated = true;
75+
used = true;
76+
}
6977
}
7078

7179
@Override
7280
public synchronized void close() throws IOException {
73-
activated = false;
74-
this.notifyAll();
81+
synchronized (lock) {
82+
activated = false;
83+
lock.notifyAll();
84+
}
7585
}
7686

7787
/**
7888
* Returns {@code true) if barrier is, or has been, in active state, {@code false) otherwise.
7989
*/
80-
public synchronized boolean used() {
81-
return used;
90+
public boolean used() {
91+
synchronized (lock) {
92+
return used;
93+
}
8294
}
8395
}

0 commit comments

Comments
 (0)