Path: blob/master/test/hotspot/jtreg/containers/docker/TestCPUAwareness.java
64474 views
/*1* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @key cgroups27* @summary Test JVM's CPU resource awareness when running inside docker container28* @requires docker.support29* @library /test/lib30* @modules java.base/jdk.internal.misc31* java.base/jdk.internal.platform32* java.management33* jdk.jartool/sun.tools.jar34* @build PrintContainerInfo CheckOperatingSystemMXBean35* @run driver TestCPUAwareness36*/37import java.util.List;38import jdk.test.lib.process.OutputAnalyzer;39import jdk.test.lib.containers.docker.Common;40import jdk.test.lib.containers.docker.DockerRunOptions;41import jdk.test.lib.containers.docker.DockerTestUtils;42import jdk.test.lib.containers.cgroup.CPUSetsReader;4344public class TestCPUAwareness {45private static final String imageName = Common.imageName("cpu");46private static final int availableCPUs = Runtime.getRuntime().availableProcessors();4748public static void main(String[] args) throws Exception {49if (!DockerTestUtils.canTestDocker()) {50return;51}5253System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);54DockerTestUtils.buildJdkContainerImage(imageName);5556try {57// cpuset, period, shares, expected Active Processor Count58testComboWithCpuSets();5960// cpu shares - it should be safe to use CPU shares exceeding available CPUs61testCpuShares(256, 1);62testCpuShares(2048, 2);63testCpuShares(4096, 4);6465// leave one CPU for system and tools, otherwise this test may be unstable66int maxNrOfAvailableCpus = availableCPUs - 1;67for (int i=1; i < maxNrOfAvailableCpus; i = i * 2) {68testCpus(i, i);69}7071// If ActiveProcessorCount is set, the VM should use it, regardless of other72// container settings, host settings or available CPUs on the host.73testActiveProcessorCount(1, 1);74testActiveProcessorCount(2, 2);7576// cpu quota and period77testCpuQuotaAndPeriod(50*1000, 100*1000);78testCpuQuotaAndPeriod(100*1000, 100*1000);79testCpuQuotaAndPeriod(150*1000, 100*1000);80testCpuQuotaAndPeriod(400*1000, 100*1000);8182testOperatingSystemMXBeanAwareness("0.5", "1");83testOperatingSystemMXBeanAwareness("1.0", "1");84if (availableCPUs > 2) {85testOperatingSystemMXBeanAwareness("1.2", "2");86testOperatingSystemMXBeanAwareness("1.8", "2");87testOperatingSystemMXBeanAwareness("2.0", "2");88}8990} finally {91if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {92DockerTestUtils.removeDockerImage(imageName);93}94}95}969798private static void testComboWithCpuSets() throws Exception {99String cpuSetStr = CPUSetsReader.readFromProcStatus("Cpus_allowed_list");100System.out.println("cpuSetStr = " + cpuSetStr);101102// OLD = use the -XX:+UseContainerCpuShares flag, which103// may be removed in a future JDK release. See JDK-8281181.104boolean OLD = true;105boolean NEW = false;106107if (cpuSetStr == null) {108System.out.printf("The cpuset test cases are skipped");109} else {110List<Integer> cpuSet = CPUSetsReader.parseCpuSet(cpuSetStr);111112// Test subset of cpuset with one element113if (cpuSet.size() >= 1) {114String testCpuSet = CPUSetsReader.listToString(cpuSet, 1);115testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 4*1024, true, 1);116testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 4*1024, true, 1);117}118119// Test subset of cpuset with two elements120if (cpuSet.size() >= 2) {121String testCpuSet = CPUSetsReader.listToString(cpuSet, 2);122testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 4*1024, true, 2);123testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, true, 2);124testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, false,1);125126testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 4*1024, true, 2);127testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, true, 2);128testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, false,2);129}130131// Test subset of cpuset with three elements132if (cpuSet.size() >= 3) {133String testCpuSet = CPUSetsReader.listToString(cpuSet, 3);134testAPCCombo(OLD, testCpuSet, 100*1000, 100*1000, 2*1024, true, 1);135testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, true, 2);136testAPCCombo(OLD, testCpuSet, 200*1000, 100*1000, 1023, false,1);137138testAPCCombo(NEW, testCpuSet, 100*1000, 100*1000, 2*1024, true, 1);139testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, true, 2);140testAPCCombo(NEW, testCpuSet, 200*1000, 100*1000, 1023, false,2);141}142}143}144145146private static void testActiveProcessorCount(int valueToSet, int expectedValue) throws Exception {147Common.logNewTestCase("Test ActiveProcessorCount: valueToSet = " + valueToSet);148149DockerRunOptions opts = Common.newOpts(imageName)150.addJavaOpts("-XX:ActiveProcessorCount=" + valueToSet, "-Xlog:os=trace");151Common.run(opts)152.shouldMatch("active processor count set by user.*" + expectedValue);153}154155156private static void testCpus(int valueToSet, int expectedTraceValue) throws Exception {157Common.logNewTestCase("test cpus: " + valueToSet);158DockerRunOptions opts = Common.newOpts(imageName)159.addDockerOpts("--cpu-period=" + 10000)160.addDockerOpts("--cpu-quota=" + valueToSet * 10000);161Common.run(opts)162.shouldMatch("active_processor_count.*" + expectedTraceValue);163}164165166// Expected active processor count can not exceed available CPU count167private static int adjustExpectedAPCForAvailableCPUs(int expectedAPC) {168if (expectedAPC > availableCPUs) {169expectedAPC = availableCPUs;170System.out.println("Adjusted expectedAPC = " + expectedAPC);171}172return expectedAPC;173}174175176private static void testCpuQuotaAndPeriod(int quota, int period)177throws Exception {178Common.logNewTestCase("test cpu quota and period: ");179System.out.println("quota = " + quota);180System.out.println("period = " + period);181182int expectedAPC = (int) Math.ceil((float) quota / (float) period);183System.out.println("expectedAPC = " + expectedAPC);184expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);185186DockerRunOptions opts = Common.newOpts(imageName)187.addDockerOpts("--cpu-period=" + period)188.addDockerOpts("--cpu-quota=" + quota);189190Common.run(opts)191.shouldMatch("CPU Period is.*" + period)192.shouldMatch("CPU Quota is.*" + quota)193.shouldMatch("active_processor_count.*" + expectedAPC);194}195196197// Test correctess of automatically selected active processor count198// Note: when -XX:+UseContainerCpuShares is removed,199// useContainerCpuShares, shares, and usePreferContainerQuotaForCPUCount200// should also be removed.201private static void testAPCCombo(boolean useContainerCpuShares, String cpuset, int quota, int period, int shares,202boolean usePreferContainerQuotaForCPUCount,203int expectedAPC) throws Exception {204Common.logNewTestCase("test APC Combo");205System.out.println("cpuset = " + cpuset);206System.out.println("quota = " + quota);207System.out.println("period = " + period);208System.out.println("shares = " + shares);209System.out.println("useContainerCpuShares = " + useContainerCpuShares);210System.out.println("usePreferContainerQuotaForCPUCount = " + usePreferContainerQuotaForCPUCount);211System.out.println("expectedAPC = " + expectedAPC);212213expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);214215DockerRunOptions opts = Common.newOpts(imageName)216.addDockerOpts("--cpuset-cpus", "" + cpuset)217.addDockerOpts("--cpu-period=" + period)218.addDockerOpts("--cpu-quota=" + quota)219.addDockerOpts("--cpu-shares=" + shares);220221if (useContainerCpuShares) opts.addJavaOpts("-XX:+UseContainerCpuShares");222if (!usePreferContainerQuotaForCPUCount) opts.addJavaOpts("-XX:-PreferContainerQuotaForCPUCount");223224Common.run(opts)225.shouldMatch("active_processor_count.*" + expectedAPC);226}227228229// Note: when -XX:+UseContainerCpuShares is removed, this test should also be removed.230private static void testCpuShares(int shares, int expectedAPC) throws Exception {231Common.logNewTestCase("test cpu shares, shares = " + shares);232System.out.println("expectedAPC = " + expectedAPC);233234expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);235236DockerRunOptions opts = Common.newOpts(imageName)237.addDockerOpts("--cpu-shares=" + shares);238opts.addJavaOpts("-XX:+UseContainerCpuShares");239OutputAnalyzer out = Common.run(opts);240// Cgroups v2 needs to do some scaling of raw shares values. Hence,241// 256 CPU shares come back as 264. Raw value written to cpu.weight242// is 10. The reason this works for >= 1024 shares value is because243// post-scaling the closest multiple of 1024 is found and returned.244//245// For values < 1024, this doesn't happen so loosen the match to a246// 3-digit number and ensure the active_processor_count is as247// expected.248if (shares < 1024) {249out.shouldMatch("CPU Shares is.*\\d{3}");250} else {251out.shouldMatch("CPU Shares is.*" + shares);252}253out.shouldMatch("active_processor_count.*" + expectedAPC);254}255256private static void testOperatingSystemMXBeanAwareness(String cpuAllocation, String expectedCpus) throws Exception {257Common.logNewTestCase("Check OperatingSystemMXBean");258259DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")260.addDockerOpts(261"--cpus", cpuAllocation262)263// CheckOperatingSystemMXBean uses Metrics (jdk.internal.platform) for264// diagnostics265.addJavaOpts("--add-exports")266.addJavaOpts("java.base/jdk.internal.platform=ALL-UNNAMED");267268DockerTestUtils.dockerRunJava(opts)269.shouldHaveExitValue(0)270.shouldContain("Checking OperatingSystemMXBean")271.shouldContain("Runtime.availableProcessors: " + expectedCpus)272.shouldContain("OperatingSystemMXBean.getAvailableProcessors: " + expectedCpus)273.shouldMatch("OperatingSystemMXBean\\.getSystemCpuLoad: [0-9]+\\.[0-9]+")274.shouldMatch("OperatingSystemMXBean\\.getCpuLoad: [0-9]+\\.[0-9]+")275;276}277}278279280