1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 /* 26 * @test 27 * @summary Test JVM's CPU resource awareness when running inside docker container 28 * @requires docker.support 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 * jdk.jartool/sun.tools.jar 33 * @build Common 34 * @run driver TestCPUAwareness 35 */ 36 import jdk.test.lib.containers.docker.DockerRunOptions; 37 import jdk.test.lib.containers.docker.DockerTestUtils; 38 39 40 public class TestCPUAwareness { 41 private static final String imageName = Common.imageName("cpu"); 42 43 public static void main(String[] args) throws Exception { 44 if (!DockerTestUtils.canTestDocker()) { 45 return; 46 } 47 48 int availableCPUs = Runtime.getRuntime().availableProcessors(); 49 System.out.println("Test Environment: detected availableCPUs = " + availableCPUs); 50 DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker"); 51 52 try { 53 // cpuset, period, shares, expected Active Processor Count 54 testAPCCombo("0", 200*1000, 100*1000, 4*1024, 1); 55 if (availableCPUs >= 2) { 56 testAPCCombo("0,1", 200*1000, 100*1000, 4*1024, 2); 57 testAPCCombo("0,1", 200*1000, 100*1000, 1*1024, 2); 58 } 59 60 // cpu shares 61 testCpuShares(256, 1); 62 if (availableCPUs >= 2) { 63 testCpuShares(2048, 2); 64 } 65 if (availableCPUs >= 4) { 66 testCpuShares(4096, 4); 67 } 68 69 // leave one CPU for system and tools, otherwise this test may be unstable 70 int maxNrOfAvailableCpus = availableCPUs - 1; 71 for (int i=1; i < maxNrOfAvailableCpus; i = i * 2) { 72 testCpus(i, i); 73 } 74 75 // If ActiveProcessorCount is set, the VM should use it, regardless of other 76 // container settings, host settings or available CPUs on the host. 77 testActiveProcessorCount(1, 1); 78 testActiveProcessorCount(2, 2); 79 80 // cpu quota and period 81 testCpuQuotaAndPeriod(50*1000, 100*1000); 82 testCpuQuotaAndPeriod(100*1000, 100*1000); 83 if (availableCPUs >= 2) { 84 testCpuQuotaAndPeriod(150*1000, 100*1000); 85 } 86 87 } finally { 88 DockerTestUtils.removeDockerImage(imageName); 89 } 90 } 91 92 93 private static void testActiveProcessorCount(int valueToSet, int expectedValue) throws Exception { 94 Common.logNewTestCase("Test ActiveProcessorCount: valueToSet = " + valueToSet); 95 96 DockerRunOptions opts = Common.newOpts(imageName) 97 .addJavaOpts("-XX:ActiveProcessorCount=" + valueToSet, "-Xlog:os=trace"); 98 Common.run(opts) 99 .shouldMatch("active processor count set by user.*" + expectedValue); 100 } 101 102 103 private static void testCpus(int valueToSet, int expectedTraceValue) throws Exception { 104 Common.logNewTestCase("test cpus: " + valueToSet); 105 DockerRunOptions opts = Common.newOpts(imageName) 106 .addDockerOpts("--cpus", "" + valueToSet); 107 Common.run(opts) 108 .shouldMatch("active_processor_count.*" + expectedTraceValue); 109 } 110 111 112 private static void testCpuQuotaAndPeriod(int quota, int period) 113 throws Exception { 114 Common.logNewTestCase("test cpu quota and period: "); 115 System.out.println("quota = " + quota); 116 System.out.println("period = " + period); 117 118 int expectedAPC = (int) Math.ceil((float) quota / (float) period); 119 System.out.println("expectedAPC = " + expectedAPC); 120 121 DockerRunOptions opts = Common.newOpts(imageName) 122 .addDockerOpts("--cpu-period=" + period) 123 .addDockerOpts("--cpu-quota=" + quota); 124 125 Common.run(opts) 126 .shouldMatch("CPU Period is.*" + period) 127 .shouldMatch("CPU Quota is.*" + quota) 128 .shouldMatch("active_processor_count.*" + expectedAPC); 129 } 130 131 132 // Test correctess of automatically selected active processor cound 133 private static void testAPCCombo(String cpuset, int quota, int period, int shares, 134 int expectedAPC) throws Exception { 135 Common.logNewTestCase("test APC Combo"); 136 System.out.println("cpuset = " + cpuset); 137 System.out.println("quota = " + quota); 138 System.out.println("period = " + period); 139 System.out.println("shares = " + period); 140 System.out.println("expectedAPC = " + expectedAPC); 141 142 DockerRunOptions opts = Common.newOpts(imageName) 143 .addDockerOpts("--cpuset-cpus", "" + cpuset) 144 .addDockerOpts("--cpu-period=" + period) 145 .addDockerOpts("--cpu-quota=" + quota) 146 .addDockerOpts("--cpu-shares=" + shares); 147 Common.run(opts) 148 .shouldMatch("active_processor_count.*" + expectedAPC); 149 } 150 151 152 private static void testCpuShares(int shares, int expectedAPC) throws Exception { 153 Common.logNewTestCase("test cpu shares, shares = " + shares); 154 DockerRunOptions opts = Common.newOpts(imageName) 155 .addDockerOpts("--cpu-shares=" + shares); 156 Common.run(opts) 157 .shouldMatch("CPU Shares is.*" + shares) 158 .shouldMatch("active_processor_count.*" + expectedAPC); 159 } 160 }