Skip to content

Commit e2a9bb1

Browse files
committed
8346875: Test jdk/jdk/jfr/event/os/TestCPULoad.java fails on macOS
Backport-of: a3eef6c2416eb0e02fbd154d84c98b12bcb66e97
1 parent 48509ab commit e2a9bb1

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

test/jdk/jdk/jfr/event/os/TestCPULoad.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, 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
@@ -30,7 +30,6 @@
3030
import jdk.test.lib.jfr.EventNames;
3131
import jdk.test.lib.jfr.Events;
3232

33-
3433
/**
3534
* @test
3635
* @key jfr
@@ -41,13 +40,32 @@
4140
public class TestCPULoad {
4241
private final static String EVENT_NAME = EventNames.CPULoad;
4342

43+
public static boolean isPrime(int num) {
44+
if (num <= 1) return false;
45+
for (int i = 2; i <= Math.sqrt(num); i++) {
46+
if (num % i == 0) return false;
47+
}
48+
return true;
49+
}
50+
51+
public static int burnCpuCycles(int limit) {
52+
int primeCount = 0;
53+
for (int i = 2; i < limit; i++) {
54+
if (isPrime(i)) {
55+
primeCount++;
56+
}
57+
}
58+
return primeCount;
59+
}
60+
4461
public static void main(String[] args) throws Throwable {
4562
Recording recording = new Recording();
4663
recording.enable(EVENT_NAME);
4764
recording.start();
48-
// Need to sleep so a time delta can be calculated
49-
Thread.sleep(100);
65+
// burn some cycles to check increase of CPU related counters
66+
int pn = burnCpuCycles(2500000);
5067
recording.stop();
68+
System.out.println("Found " + pn + " primes while burning cycles");
5169

5270
List<RecordedEvent> events = Events.fromRecording(recording);
5371
if (events.isEmpty()) {

0 commit comments

Comments
 (0)