Skip to content

Commit eb6e828

Browse files
committed
8351002: com/sun/management/OperatingSystemMXBean cpuLoad tests fail intermittently
Reviewed-by: sspitsyn, lmesnik
1 parent e32a0c9 commit eb6e828

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

test/jdk/ProblemList.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,8 @@ java/io/IO/IO.java 8337935 linux-pp
536536

537537
# jdk_management
538538

539-
# First bug for AIX, second for Windows
540-
com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java 8030957,8351002 aix-all,windows-all
541-
com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java 8030957,8351002 aix-all,windows-all
539+
com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java 8030957 aix-all
540+
com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java 8030957 aix-all
542541

543542
java/lang/management/MemoryMXBean/Pending.java 8158837 generic-all
544543
java/lang/management/MemoryMXBean/PendingAllGC.sh 8158837 generic-all

test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,45 @@
2525
* @test
2626
* @bug 7028071
2727
* @summary Basic unit test of OperatingSystemMXBean.getProcessCpuLoad()
28-
*
28+
* @library /test/lib
2929
* @run main GetProcessCpuLoad
3030
*/
3131

32-
import java.lang.management.*;
3332
import com.sun.management.OperatingSystemMXBean;
33+
import java.lang.management.*;
34+
import jdk.test.lib.Platform;
3435

3536
public class GetProcessCpuLoad {
3637
public static void main(String[] argv) throws Exception {
3738
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
3839
ManagementFactory.getOperatingSystemMXBean();
40+
41+
Exception ex = null;
3942
double load;
40-
for(int i=0; i<10; i++) {
43+
44+
for(int i = 0; i < 10; i++) {
4145
load = mbean.getProcessCpuLoad();
42-
if(load<0.0 || load>1.0) {
46+
if (load == -1.0 && Platform.isWindows()) {
47+
// Some Windows 2019 systems can return -1 for the first few reads.
48+
// Remember a -1 in case it never gets better.
49+
ex = new RuntimeException("getProcessCpuLoad() returns " + load
50+
+ " which is not in the [0.0,1.0] interval");
51+
} else if (load < 0.0 || load > 1.0) {
4352
throw new RuntimeException("getProcessCpuLoad() returns " + load
44-
+ " which is not in the [0.0,1.0] interval");
53+
+ " which is not in the [0.0,1.0] interval");
54+
} else {
55+
// A good reading: forget any previous -1.
56+
ex = null;
4557
}
4658
try {
4759
Thread.sleep(200);
4860
} catch(InterruptedException e) {
4961
e.printStackTrace();
5062
}
5163
}
64+
65+
if (ex != null) {
66+
throw ex;
67+
}
5268
}
5369
}

test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuTime.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 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
@@ -25,6 +25,7 @@
2525
* @test
2626
* @bug 4858522
2727
* @summary Basic unit test of OperatingSystemMXBean.getProcessCpuTime()
28+
* @library /test/lib
2829
* @author Steve Bohne
2930
*/
3031

@@ -45,6 +46,7 @@
4546

4647
import com.sun.management.OperatingSystemMXBean;
4748
import java.lang.management.*;
49+
import jdk.test.lib.Platform;
4850

4951
public class GetProcessCpuTime {
5052

@@ -55,9 +57,7 @@ public class GetProcessCpuTime {
5557

5658
// Careful with these values.
5759
private static final long MIN_TIME_FOR_PASS = 1;
58-
private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE;
59-
60-
// No max time.
60+
private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE / 10_000_000;
6161

6262
private static boolean trace = false;
6363

@@ -74,9 +74,22 @@ public static void main(String args[]) throws Exception {
7474
}
7575

7676
long ns = mbean.getProcessCpuTime();
77+
if (ns == -1 && Platform.isWindows()) {
78+
// Some Windows 2019 systems can return -1 for the first few reads.
79+
for (int i = 0; i < 10; i++) {
80+
ns = mbean.getProcessCpuTime();
81+
if (ns != -1) {
82+
break;
83+
}
84+
try {
85+
Thread.sleep(200);
86+
} catch (InterruptedException e) {
87+
e.printStackTrace();
88+
}
89+
}
90+
}
7791
if (ns == -1) {
78-
System.out.println("getProcessCpuTime() is not supported");
79-
return;
92+
throw new RuntimeException("getProcessCpuTime() is not supported");
8093
}
8194

8295
if (trace) {

test/jdk/com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,47 @@
2424
/*
2525
* @test
2626
* @bug 7028071
27-
* @summary Basic unit test of OperatingSystemMXBean.getSystemCpuLoad()
28-
*
27+
* @summary Basic unit test of OperatingSystemMXBean.getProcessCpuLoad()
28+
* @library /test/lib
2929
* @run main GetSystemCpuLoad
3030
*/
3131

32-
import java.lang.management.*;
3332
import com.sun.management.OperatingSystemMXBean;
33+
import java.lang.management.*;
34+
import jdk.test.lib.Platform;
3435

3536
public class GetSystemCpuLoad {
3637
public static void main(String[] argv) throws Exception {
3738
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
3839
ManagementFactory.getOperatingSystemMXBean();
40+
41+
Exception ex = null;
3942
double load;
40-
for(int i=0; i<10; i++) {
43+
44+
for (int i = 0; i < 10; i++) {
4145
load = mbean.getSystemCpuLoad();
42-
if(load<0.0 || load>1.0) {
46+
if (load == -1.0 && Platform.isWindows()) {
47+
// Some Windows 2019 systems can return -1 for the first few reads.
48+
// Remember a -1 in case it never gets better.
49+
ex = new RuntimeException("getSystemCpuLoad() returns " + load
50+
+ " which is not in the [0.0,1.0] interval");
51+
52+
} else if (load < 0.0 || load > 1.0) {
4353
throw new RuntimeException("getSystemCpuLoad() returns " + load
44-
+ " which is not in the [0.0,1.0] interval");
54+
+ " which is not in the [0.0,1.0] interval");
55+
} else {
56+
// A good reading: forget any previous -1.
57+
ex = null;
4558
}
4659
try {
4760
Thread.sleep(200);
4861
} catch(InterruptedException e) {
4962
e.printStackTrace();
5063
}
5164
}
65+
66+
if (ex != null) {
67+
throw ex;
68+
}
5269
}
5370
}

0 commit comments

Comments
 (0)