Skip to content

Commit 37f36d5

Browse files
committed
8265836: OperatingSystemImpl.getCpuLoad() returns incorrect CPU load inside a container
Backport-of: ef368b3
1 parent 9dc8826 commit 37f36d5

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ Java_com_sun_management_internal_OperatingSystemImpl_getSingleCpuLoad0
5151
return -1.0;
5252
}
5353

54+
JNIEXPORT jlong JNICALL
55+
Java_com_sun_management_internal_OperatingSystemImpl_getHostTotalCpuTicks0
56+
(JNIEnv *env, jobject mbean)
57+
{
58+
return -1.0;
59+
}
60+
5461
JNIEXPORT jint JNICALL
5562
Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
5663
(JNIEnv *env, jobject mbean)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static struct perfbuf {
5959
} counters;
6060

6161
#define DEC_64 "%"SCNd64
62+
#define NS_PER_SEC 1000000000
6263

6364
static void next_line(FILE *f) {
6465
while (fgetc(f) != '\n');
@@ -365,6 +366,31 @@ Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
365366
}
366367
}
367368

369+
// Return the host cpu ticks since boot in nanoseconds
370+
JNIEXPORT jlong JNICALL
371+
Java_com_sun_management_internal_OperatingSystemImpl_getHostTotalCpuTicks0
372+
(JNIEnv *env, jobject mbean)
373+
{
374+
if (perfInit() == 0) {
375+
if (get_totalticks(-1, &counters.cpuTicks) < 0) {
376+
return -1;
377+
} else {
378+
long ticks_per_sec = sysconf(_SC_CLK_TCK);
379+
jlong result = (jlong)counters.cpuTicks.total;
380+
if (ticks_per_sec <= NS_PER_SEC) {
381+
long scale_factor = NS_PER_SEC/ticks_per_sec;
382+
result = result * scale_factor;
383+
} else {
384+
long scale_factor = ticks_per_sec/NS_PER_SEC;
385+
result = result / scale_factor;
386+
}
387+
return result;
388+
}
389+
} else {
390+
return -1;
391+
}
392+
}
393+
368394
JNIEXPORT jint JNICALL
369395
Java_com_sun_management_internal_OperatingSystemImpl_getHostOnlineCpuCount0
370396
(JNIEnv *env, jobject mbean)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ Java_com_sun_management_internal_OperatingSystemImpl_getSingleCpuLoad0
167167
return -1.0;
168168
}
169169

170+
JNIEXPORT jlong JNICALL
171+
Java_com_sun_management_internal_OperatingSystemImpl_getHostTotalCpuTicks0
172+
(JNIEnv *env, jobject mbean)
173+
{
174+
return -1.0;
175+
}
176+
170177
JNIEXPORT jint JNICALL
171178
Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
172179
(JNIEnv *env, jobject mbean)

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

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class OperatingSystemImpl extends BaseOperatingSystemImpl
4242

4343
private static final int MAX_ATTEMPTS_NUMBER = 10;
4444
private final Metrics containerMetrics;
45+
private long usageTicks = 0; // used for cpu load calculation
46+
private long totalTicks = 0; // used for cpu load calculation
4547

4648
OperatingSystemImpl(VMManagement vm) {
4749
super(vm);
@@ -133,24 +135,56 @@ public long getMaxFileDescriptorCount() {
133135
return getMaxFileDescriptorCount0();
134136
}
135137

138+
private double getUsageDividesTotal(long usageTicks, long totalTicks) {
139+
// If cpu quota or cpu shares are in effect calculate the cpu load
140+
// based on the following formula (similar to how
141+
// getCpuLoad0() is being calculated):
142+
//
143+
// | usageTicks - usageTicks' |
144+
// ------------------------------
145+
// | totalTicks - totalTicks' |
146+
//
147+
// where usageTicks' and totalTicks' are historical values
148+
// retrieved via an earlier call of this method.
149+
//
150+
// Total ticks should be scaled to the container effective number
151+
// of cpus, if cpu shares are in effect.
152+
if (usageTicks < 0 || totalTicks <= 0) {
153+
return -1;
154+
}
155+
long distance = usageTicks - this.usageTicks;
156+
this.usageTicks = usageTicks;
157+
long totalDistance = totalTicks - this.totalTicks;
158+
this.totalTicks = totalTicks;
159+
160+
double systemLoad = 0.0;
161+
if (distance > 0 && totalDistance > 0) {
162+
systemLoad = ((double)distance) / totalDistance;
163+
}
164+
// Ensure the return value is in the range 0.0 -> 1.0
165+
systemLoad = Math.max(0.0, systemLoad);
166+
systemLoad = Math.min(1.0, systemLoad);
167+
return systemLoad;
168+
}
169+
136170
public double getSystemCpuLoad() {
137171
if (containerMetrics != null) {
138172
long quota = containerMetrics.getCpuQuota();
173+
long share = containerMetrics.getCpuShares();
174+
long usageNanos = containerMetrics.getCpuUsage();
139175
if (quota > 0) {
140-
long periodLength = containerMetrics.getCpuPeriod();
141176
long numPeriods = containerMetrics.getCpuNumPeriods();
142-
long usageNanos = containerMetrics.getCpuUsage();
143-
if (periodLength > 0 && numPeriods > 0 && usageNanos > 0) {
144-
long elapsedNanos = TimeUnit.MICROSECONDS.toNanos(periodLength * numPeriods);
145-
double systemLoad = (double) usageNanos / elapsedNanos;
146-
// Ensure the return value is in the range 0.0 -> 1.0
147-
systemLoad = Math.max(0.0, systemLoad);
148-
systemLoad = Math.min(1.0, systemLoad);
149-
return systemLoad;
150-
}
151-
return -1;
177+
long quotaNanos = TimeUnit.MICROSECONDS.toNanos(quota * numPeriods);
178+
return getUsageDividesTotal(usageNanos, quotaNanos);
179+
} else if (share > 0) {
180+
long hostTicks = getHostTotalCpuTicks0();
181+
int totalCPUs = getHostOnlineCpuCount0();
182+
int containerCPUs = getAvailableProcessors();
183+
// scale the total host load to the actual container cpus
184+
hostTicks = hostTicks * containerCPUs / totalCPUs;
185+
return getUsageDividesTotal(usageNanos, hostTicks);
152186
} else {
153-
// If CPU quotas are not active then find the average system load for
187+
// If CPU quotas and shares are not active then find the average system load for
154188
// all online CPUs that are allowed to run this container.
155189

156190
// If the cpuset is the same as the host's one there is no need to iterate over each CPU
@@ -205,6 +239,8 @@ private boolean isCpuSetSameAsHostCpuSet() {
205239
private native double getSingleCpuLoad0(int cpuNum);
206240
private native int getHostConfiguredCpuCount0();
207241
private native int getHostOnlineCpuCount0();
242+
// CPU ticks since boot in nanoseconds
243+
private native long getHostTotalCpuTicks0();
208244

209245
static {
210246
initialize0();

0 commit comments

Comments
 (0)