Skip to content

Commit 624e91b

Browse files
committed
8247469: getSystemCpuLoad() returns -1 on linux when some offline cpus are present and cpusets.effective_cpus is not available
Backport-of: 3341d36
1 parent 6deb21f commit 624e91b

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/jdk.management/aix/native/libmanagement_ext/UnixOperatingSystem.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2015 SAP SE. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2015, 2020 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -57,3 +57,10 @@ Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
5757
{
5858
return -1;
5959
}
60+
61+
JNIEXPORT jint JNICALL
62+
Java_com_sun_management_internal_OperatingSystemImpl_getHostOnlineCpuCount0
63+
(JNIEnv *env, jobject mbean)
64+
{
65+
return -1;
66+
}

src/jdk.management/linux/native/libmanagement_ext/UnixOperatingSystem.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,15 @@ Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
364364
return -1;
365365
}
366366
}
367+
368+
JNIEXPORT jint JNICALL
369+
Java_com_sun_management_internal_OperatingSystemImpl_getHostOnlineCpuCount0
370+
(JNIEnv *env, jobject mbean)
371+
{
372+
int n = sysconf(_SC_NPROCESSORS_ONLN);
373+
if (n <= 0) {
374+
n = 1;
375+
}
376+
return n;
377+
}
378+

src/jdk.management/macosx/native/libmanagement_ext/UnixOperatingSystem.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2020, 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
@@ -173,3 +173,11 @@ Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
173173
{
174174
return -1;
175175
}
176+
177+
JNIEXPORT jint JNICALL
178+
Java_com_sun_management_internal_OperatingSystemImpl_getHostOnlineCpuCount0
179+
(JNIEnv *env, jobject mbean)
180+
{
181+
return -1;
182+
}
183+

src/jdk.management/unix/classes/com/sun/management/internal/OperatingSystemImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public double getSystemCpuLoad() {
158158
return getSystemCpuLoad0();
159159
} else {
160160
int[] cpuSet = containerMetrics.getEffectiveCpuSetCpus();
161+
// in case the effectiveCPUSetCpus are not available, attempt to use just cpusets.cpus
162+
if (cpuSet == null || cpuSet.length <= 0) {
163+
cpuSet = containerMetrics.getCpuSetCpus();
164+
}
161165
if (cpuSet != null && cpuSet.length > 0) {
162166
double systemLoad = 0.0;
163167
for (int cpu : cpuSet) {
@@ -182,7 +186,7 @@ public double getProcessCpuLoad() {
182186

183187
private boolean isCpuSetSameAsHostCpuSet() {
184188
if (containerMetrics != null) {
185-
return containerMetrics.getCpuSetCpus().length == getHostConfiguredCpuCount0();
189+
return containerMetrics.getCpuSetCpus().length == getHostOnlineCpuCount0();
186190
}
187191
return false;
188192
}
@@ -200,6 +204,7 @@ private boolean isCpuSetSameAsHostCpuSet() {
200204
private native long getTotalSwapSpaceSize0();
201205
private native double getSingleCpuLoad0(int cpuNum);
202206
private native int getHostConfiguredCpuCount0();
207+
private native int getHostOnlineCpuCount0();
203208

204209
static {
205210
initialize0();

0 commit comments

Comments
 (0)