8281181: Do not use CPU Shares to compute active processor count

Reviewed-by: dholmes, sgehwolf
This commit is contained in:
Ioi Lam 2022-03-04 20:14:11 +00:00
parent 9c817d3880
commit e07fd395bd
6 changed files with 52 additions and 18 deletions

View File

@ -495,7 +495,12 @@ int CgroupSubsystem::active_processor_count() {
cpu_count = limit_count = os::Linux::active_processor_count(); cpu_count = limit_count = os::Linux::active_processor_count();
int quota = cpu_quota(); int quota = cpu_quota();
int period = cpu_period(); int period = cpu_period();
int share = cpu_shares();
// It's not a good idea to use cpu_shares() to limit the number
// of CPUs used by the JVM. See JDK-8281181.
// UseContainerCpuShares and PreferContainerQuotaForCPUCount are
// deprecated and will be removed in the next JDK release.
int share = UseContainerCpuShares ? cpu_shares() : -1;
if (quota > -1 && period > 0) { if (quota > -1 && period > 0) {
quota_count = ceilf((float)quota / (float)period); quota_count = ceilf((float)quota / (float)period);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -59,10 +59,14 @@
product(bool, UseContainerSupport, true, \ product(bool, UseContainerSupport, true, \
"Enable detection and runtime container configuration support") \ "Enable detection and runtime container configuration support") \
\ \
product(bool, UseContainerCpuShares, false, \
"(Deprecated) Include CPU shares in the CPU availability" \
" calculation.") \
\
product(bool, PreferContainerQuotaForCPUCount, true, \ product(bool, PreferContainerQuotaForCPUCount, true, \
"Calculate the container CPU availability based on the value" \ "(Deprecated) Calculate the container CPU availability based" \
" of quotas (if set), when true. Otherwise, use the CPU" \ " on the value of quotas (if set), when true. Otherwise, use" \
" shares value, provided it is less than quota.") \ " the CPU shares value, provided it is less than quota.") \
\ \
product(bool, AdjustStackSizeForTLS, false, \ product(bool, AdjustStackSizeForTLS, false, \
"Increase the thread stack size to include space for glibc " \ "Increase the thread stack size to include space for glibc " \

View File

@ -538,6 +538,8 @@ static SpecialFlag const special_jvm_flags[] = {
{ "UseHeavyMonitors", JDK_Version::jdk(18), JDK_Version::jdk(19), JDK_Version::jdk(20) }, { "UseHeavyMonitors", JDK_Version::jdk(18), JDK_Version::jdk(19), JDK_Version::jdk(20) },
#endif #endif
{ "ExtendedDTraceProbes", JDK_Version::jdk(19), JDK_Version::jdk(20), JDK_Version::jdk(21) }, { "ExtendedDTraceProbes", JDK_Version::jdk(19), JDK_Version::jdk(20), JDK_Version::jdk(21) },
{ "UseContainerCpuShares", JDK_Version::jdk(19), JDK_Version::jdk(20), JDK_Version::jdk(21) },
{ "PreferContainerQuotaForCPUCount", JDK_Version::jdk(19), JDK_Version::jdk(20), JDK_Version::jdk(21) },
// --- Deprecated alias flags (see also aliased_jvm_flags) - sorted by obsolete_in then expired_in: // --- Deprecated alias flags (see also aliased_jvm_flags) - sorted by obsolete_in then expired_in:
{ "DefaultMaxRAMFraction", JDK_Version::jdk(8), JDK_Version::undefined(), JDK_Version::undefined() }, { "DefaultMaxRAMFraction", JDK_Version::jdk(8), JDK_Version::undefined(), JDK_Version::undefined() },

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -50,7 +50,7 @@ public class PlainRead {
static final String good_value = "(\\d+|-1|-2|Unlimited)"; static final String good_value = "(\\d+|-1|-2|Unlimited)";
static final String bad_value = "(failed)"; static final String bad_value = "(failed)";
static final String[] variables = {"Memory Limit is:", "CPU Shares is:", "CPU Quota is:", "CPU Period is:", "active_processor_count:"}; static final String[] variables = {"Memory Limit is:", "CPU Quota is:", "CPU Period is:", "active_processor_count:"};
static public void isContainer(OutputAnalyzer oa) { static public void isContainer(OutputAnalyzer oa) {
for (String v: variables) { for (String v: variables) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -100,6 +100,11 @@ public class TestCPUAwareness {
String cpuSetStr = CPUSetsReader.readFromProcStatus("Cpus_allowed_list"); String cpuSetStr = CPUSetsReader.readFromProcStatus("Cpus_allowed_list");
System.out.println("cpuSetStr = " + cpuSetStr); System.out.println("cpuSetStr = " + cpuSetStr);
// OLD = use the deprecated -XX:+UseContainerCpuShares flag, which
// will be removed in the next JDK release. See JDK-8281181.
boolean OLD = true;
boolean NEW = false;
if (cpuSetStr == null) { if (cpuSetStr == null) {
System.out.printf("The cpuset test cases are skipped"); System.out.printf("The cpuset test cases are skipped");
} else { } else {
@ -108,23 +113,32 @@ public class TestCPUAwareness {
// Test subset of cpuset with one element // Test subset of cpuset with one element
if (cpuSet.size() >= 1) { if (cpuSet.size() >= 1) {
String testCpuSet = CPUSetsReader.listToString(cpuSet, 1); String testCpuSet = CPUSetsReader.listToString(cpuSet, 1);
testAPCCombo(testCpuSet, 200*1000, 100*1000, 4*1024, true, 1); testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 4*1024, true, 1);
testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 4*1024, true, 1);
} }
// Test subset of cpuset with two elements // Test subset of cpuset with two elements
if (cpuSet.size() >= 2) { if (cpuSet.size() >= 2) {
String testCpuSet = CPUSetsReader.listToString(cpuSet, 2); String testCpuSet = CPUSetsReader.listToString(cpuSet, 2);
testAPCCombo(testCpuSet, 200*1000, 100*1000, 4*1024, true, 2); testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 4*1024, true, 2);
testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, true, 2); testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, true, 2);
testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, false, 1); testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, false,1);
testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 4*1024, true, 2);
testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, true, 2);
testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, false,2);
} }
// Test subset of cpuset with three elements // Test subset of cpuset with three elements
if (cpuSet.size() >= 3) { if (cpuSet.size() >= 3) {
String testCpuSet = CPUSetsReader.listToString(cpuSet, 3); String testCpuSet = CPUSetsReader.listToString(cpuSet, 3);
testAPCCombo(testCpuSet, 100*1000, 100*1000, 2*1024, true, 1); testAPCCombo(OLD, testCpuSet, 100*1000, 100*1000, 2*1024, true, 1);
testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, true, 2); testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, true, 2);
testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, false, 1); testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, false,1);
testAPCCombo(NEW, testCpuSet, 100*1000, 100*1000, 2*1024, true, 1);
testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, true, 2);
testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, false,2);
} }
} }
} }
@ -181,8 +195,11 @@ public class TestCPUAwareness {
} }
// Test correctess of automatically selected active processor cound // Test correctess of automatically selected active processor count
private static void testAPCCombo(String cpuset, int quota, int period, int shares, // Note: when -XX:+UseContainerCpuShares is removed,
// useContainerCpuShares, shares, and usePreferContainerQuotaForCPUCount
// should also be removed.
private static void testAPCCombo(boolean useContainerCpuShares, String cpuset, int quota, int period, int shares,
boolean usePreferContainerQuotaForCPUCount, boolean usePreferContainerQuotaForCPUCount,
int expectedAPC) throws Exception { int expectedAPC) throws Exception {
Common.logNewTestCase("test APC Combo"); Common.logNewTestCase("test APC Combo");
@ -190,6 +207,7 @@ public class TestCPUAwareness {
System.out.println("quota = " + quota); System.out.println("quota = " + quota);
System.out.println("period = " + period); System.out.println("period = " + period);
System.out.println("shares = " + shares); System.out.println("shares = " + shares);
System.out.println("useContainerCpuShares = " + useContainerCpuShares);
System.out.println("usePreferContainerQuotaForCPUCount = " + usePreferContainerQuotaForCPUCount); System.out.println("usePreferContainerQuotaForCPUCount = " + usePreferContainerQuotaForCPUCount);
System.out.println("expectedAPC = " + expectedAPC); System.out.println("expectedAPC = " + expectedAPC);
@ -201,13 +219,15 @@ public class TestCPUAwareness {
.addDockerOpts("--cpu-quota=" + quota) .addDockerOpts("--cpu-quota=" + quota)
.addDockerOpts("--cpu-shares=" + shares); .addDockerOpts("--cpu-shares=" + shares);
if (!usePreferContainerQuotaForCPUCount) opts.addJavaOpts("-XX:-PreferContainerQuotaForCPUCount"); if (useContainerCpuShares) opts.addJavaOpts("-XX:+UseContainerCpuShares"); // deprecated
if (!usePreferContainerQuotaForCPUCount) opts.addJavaOpts("-XX:-PreferContainerQuotaForCPUCount"); // deprecated
Common.run(opts) Common.run(opts)
.shouldMatch("active_processor_count.*" + expectedAPC); .shouldMatch("active_processor_count.*" + expectedAPC);
} }
// Note: when -XX:+UseContainerCpuShares is removed, this test should also be removed.
private static void testCpuShares(int shares, int expectedAPC) throws Exception { private static void testCpuShares(int shares, int expectedAPC) throws Exception {
Common.logNewTestCase("test cpu shares, shares = " + shares); Common.logNewTestCase("test cpu shares, shares = " + shares);
System.out.println("expectedAPC = " + expectedAPC); System.out.println("expectedAPC = " + expectedAPC);
@ -216,6 +236,7 @@ public class TestCPUAwareness {
DockerRunOptions opts = Common.newOpts(imageName) DockerRunOptions opts = Common.newOpts(imageName)
.addDockerOpts("--cpu-shares=" + shares); .addDockerOpts("--cpu-shares=" + shares);
opts.addJavaOpts("-XX:+UseContainerCpuShares"); // deprecated
OutputAnalyzer out = Common.run(opts); OutputAnalyzer out = Common.run(opts);
// Cgroups v2 needs to do some scaling of raw shares values. Hence, // Cgroups v2 needs to do some scaling of raw shares values. Hence,
// 256 CPU shares come back as 264. Raw value written to cpu.weight // 256 CPU shares come back as 264. Raw value written to cpu.weight

View File

@ -54,6 +54,8 @@ public class VMDeprecatedOptions {
{"InitialRAMFraction", "64"}, {"InitialRAMFraction", "64"},
{"TLABStats", "false"}, {"TLABStats", "false"},
{"AllowRedefinitionToAddDeleteMethods", "true"}, {"AllowRedefinitionToAddDeleteMethods", "true"},
{"UseContainerCpuShares", "false"},
{"PreferContainerQuotaForCPUCount", "true"},
// deprecated alias flags (see also aliased_jvm_flags): // deprecated alias flags (see also aliased_jvm_flags):
{"DefaultMaxRAMFraction", "4"}, {"DefaultMaxRAMFraction", "4"},