Path: blob/master/test/hotspot/jtreg/containers/docker/TestCPUAwareness.java
40942 views
/*1* Copyright (c) 2017, 2020, 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.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");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);101102if (cpuSetStr == null) {103System.out.printf("The cpuset test cases are skipped");104} else {105List<Integer> cpuSet = CPUSetsReader.parseCpuSet(cpuSetStr);106107// Test subset of cpuset with one element108if (cpuSet.size() >= 1) {109String testCpuSet = CPUSetsReader.listToString(cpuSet, 1);110testAPCCombo(testCpuSet, 200*1000, 100*1000, 4*1024, true, 1);111}112113// Test subset of cpuset with two elements114if (cpuSet.size() >= 2) {115String testCpuSet = CPUSetsReader.listToString(cpuSet, 2);116testAPCCombo(testCpuSet, 200*1000, 100*1000, 4*1024, true, 2);117testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, true, 2);118testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, false, 1);119}120121// Test subset of cpuset with three elements122if (cpuSet.size() >= 3) {123String testCpuSet = CPUSetsReader.listToString(cpuSet, 3);124testAPCCombo(testCpuSet, 100*1000, 100*1000, 2*1024, true, 1);125testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, true, 2);126testAPCCombo(testCpuSet, 200*1000, 100*1000, 1023, false, 1);127}128}129}130131132private static void testActiveProcessorCount(int valueToSet, int expectedValue) throws Exception {133Common.logNewTestCase("Test ActiveProcessorCount: valueToSet = " + valueToSet);134135DockerRunOptions opts = Common.newOpts(imageName)136.addJavaOpts("-XX:ActiveProcessorCount=" + valueToSet, "-Xlog:os=trace");137Common.run(opts)138.shouldMatch("active processor count set by user.*" + expectedValue);139}140141142private static void testCpus(int valueToSet, int expectedTraceValue) throws Exception {143Common.logNewTestCase("test cpus: " + valueToSet);144DockerRunOptions opts = Common.newOpts(imageName)145.addDockerOpts("--cpu-period=" + 10000)146.addDockerOpts("--cpu-quota=" + valueToSet * 10000);147Common.run(opts)148.shouldMatch("active_processor_count.*" + expectedTraceValue);149}150151152// Expected active processor count can not exceed available CPU count153private static int adjustExpectedAPCForAvailableCPUs(int expectedAPC) {154if (expectedAPC > availableCPUs) {155expectedAPC = availableCPUs;156System.out.println("Adjusted expectedAPC = " + expectedAPC);157}158return expectedAPC;159}160161162private static void testCpuQuotaAndPeriod(int quota, int period)163throws Exception {164Common.logNewTestCase("test cpu quota and period: ");165System.out.println("quota = " + quota);166System.out.println("period = " + period);167168int expectedAPC = (int) Math.ceil((float) quota / (float) period);169System.out.println("expectedAPC = " + expectedAPC);170expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);171172DockerRunOptions opts = Common.newOpts(imageName)173.addDockerOpts("--cpu-period=" + period)174.addDockerOpts("--cpu-quota=" + quota);175176Common.run(opts)177.shouldMatch("CPU Period is.*" + period)178.shouldMatch("CPU Quota is.*" + quota)179.shouldMatch("active_processor_count.*" + expectedAPC);180}181182183// Test correctess of automatically selected active processor cound184private static void testAPCCombo(String cpuset, int quota, int period, int shares,185boolean usePreferContainerQuotaForCPUCount,186int expectedAPC) throws Exception {187Common.logNewTestCase("test APC Combo");188System.out.println("cpuset = " + cpuset);189System.out.println("quota = " + quota);190System.out.println("period = " + period);191System.out.println("shares = " + shares);192System.out.println("usePreferContainerQuotaForCPUCount = " + usePreferContainerQuotaForCPUCount);193System.out.println("expectedAPC = " + expectedAPC);194195expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);196197DockerRunOptions opts = Common.newOpts(imageName)198.addDockerOpts("--cpuset-cpus", "" + cpuset)199.addDockerOpts("--cpu-period=" + period)200.addDockerOpts("--cpu-quota=" + quota)201.addDockerOpts("--cpu-shares=" + shares);202203if (!usePreferContainerQuotaForCPUCount) opts.addJavaOpts("-XX:-PreferContainerQuotaForCPUCount");204205Common.run(opts)206.shouldMatch("active_processor_count.*" + expectedAPC);207}208209210private static void testCpuShares(int shares, int expectedAPC) throws Exception {211Common.logNewTestCase("test cpu shares, shares = " + shares);212System.out.println("expectedAPC = " + expectedAPC);213214expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);215216DockerRunOptions opts = Common.newOpts(imageName)217.addDockerOpts("--cpu-shares=" + shares);218OutputAnalyzer out = Common.run(opts);219// Cgroups v2 needs to do some scaling of raw shares values. Hence,220// 256 CPU shares come back as 264. Raw value written to cpu.weight221// is 10. The reason this works for >= 1024 shares value is because222// post-scaling the closest multiple of 1024 is found and returned.223//224// For values < 1024, this doesn't happen so loosen the match to a225// 3-digit number and ensure the active_processor_count is as226// expected.227if (shares < 1024) {228out.shouldMatch("CPU Shares is.*\\d{3}");229} else {230out.shouldMatch("CPU Shares is.*" + shares);231}232out.shouldMatch("active_processor_count.*" + expectedAPC);233}234235private static void testOperatingSystemMXBeanAwareness(String cpuAllocation, String expectedCpus) throws Exception {236Common.logNewTestCase("Check OperatingSystemMXBean");237238DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")239.addDockerOpts(240"--cpus", cpuAllocation241)242// CheckOperatingSystemMXBean uses Metrics (jdk.internal.platform) for243// diagnostics244.addJavaOpts("--add-exports")245.addJavaOpts("java.base/jdk.internal.platform=ALL-UNNAMED");246247DockerTestUtils.dockerRunJava(opts)248.shouldHaveExitValue(0)249.shouldContain("Checking OperatingSystemMXBean")250.shouldContain("Runtime.availableProcessors: " + expectedCpus)251.shouldContain("OperatingSystemMXBean.getAvailableProcessors: " + expectedCpus)252.shouldMatch("OperatingSystemMXBean\\.getSystemCpuLoad: [0-9]+\\.[0-9]+")253.shouldMatch("OperatingSystemMXBean\\.getCpuLoad: [0-9]+\\.[0-9]+")254;255}256}257258259