Skip to content

Commit b159484

Browse files
author
duke
committed
Backport 487cc3c5be769d15d61cb950137d52ba0eb982b5
1 parent ece596a commit b159484

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

test/jdk/jdk/jfr/event/profiling/TestCPUTimeSampleThrottling.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package jdk.jfr.event.profiling;
2525
import java.lang.management.ManagementFactory;
26+
import java.lang.management.ThreadMXBean;
2627
import java.time.Duration;
2728
import java.time.Instant;
2829
import java.util.List;
@@ -71,7 +72,7 @@ float rate() {
7172
}
7273

7374
/**
74-
* Counting the events that are emitted for a given throttle in a given time.
75+
* Counting the events that are emitted for a given throttle in a given (CPU) time.
7576
* <p>
7677
* The result is wall-clock independent; it only records the CPU-time and the number of
7778
* emitted events. The result, therefore, does not depend on the load of the machine.
@@ -83,35 +84,30 @@ private static EventCount countEvents(int timeMs, String throttle) throws Except
8384
recording.enable(EventNames.CPUTimeSample)
8485
.with("throttle", throttle);
8586

86-
var bean = ManagementFactory.getThreadMXBean();
87-
8887
recording.start();
8988

90-
long startThreadCpuTime = bean.getCurrentThreadCpuTime();
91-
92-
wasteCPU(timeMs);
93-
94-
long spendCPUTime = bean.getCurrentThreadCpuTime() - startThreadCpuTime;
89+
long spendCPUTime = wasteCPU(timeMs);
9590

9691
recording.stop();
9792

9893
long eventCount = Events.fromRecording(recording).stream()
9994
.filter(e -> e.getThread().getJavaName()
10095
.equals(Thread.currentThread().getName()))
10196
.count();
102-
10397
return new EventCount(eventCount, spendCPUTime / 1_000_000_000f);
10498
}
10599
}
106100

107-
private static void wasteCPU(int durationMs) {
108-
long start = System.currentTimeMillis();
101+
private static long wasteCPU(int durationMs) {
102+
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
103+
long start = bean.getCurrentThreadCpuTime();
109104
double i = 0;
110-
while (System.currentTimeMillis() - start < durationMs) {
105+
while (bean.getCurrentThreadCpuTime() - start < durationMs * 1_000_000) {
111106
for (int j = 0; j < 100000; j++) {
112107
i = Math.sqrt(i * Math.pow(Math.sqrt(Math.random()), Math.random()));
113108
}
114109
}
110+
return bean.getCurrentThreadCpuTime() - start;
115111
}
116112

117113
}

0 commit comments

Comments
 (0)