1 /* 2 * Copyright (c) 2018, 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 import java.util.Arrays; 25 import java.util.stream.IntStream; 26 import java.util.stream.Stream; 27 import jdk.internal.platform.Metrics; 28 29 public class MetricsCpuTester { 30 public static void main(String[] args) { 31 System.out.println(Arrays.toString(args)); 32 switch (args[0]) { 33 case "cpusets": 34 testCpuSets(args[1]); 35 break; 36 case "cpuquota": 37 testCpuQuotaAndPeriod(Long.parseLong(args[1]), Long.parseLong(args[2])); 38 break; 39 case "cpushares": 40 testCpuShares(Long.parseLong(args[1])); 41 break; 42 case "cpus": 43 testCpuThrottling(); 44 break; 45 case "cpumems": 46 testCpuSetMemNodes(args[1]); 47 break; 48 case "combo": 49 testCombo(args[1], Long.parseLong(args[2]), Long.parseLong(args[3]), Long.parseLong(args[4])); 50 break; 51 } 52 } 53 54 private static void testCpuQuotaAndPeriod(long quota, long period) { 55 Metrics metrics = Metrics.systemMetrics(); 56 long newQuota = metrics.getCpuQuota(); 57 long newPeriod = metrics.getCpuPeriod(); 58 if (quota != newQuota || period != newPeriod) { 59 throw new RuntimeException("CPU quota or period not equal, expected : [" 60 + quota + "," + period + "]" + ", got : " + "[" + newQuota 61 + "," + newPeriod + "]"); 62 } 63 64 long cpuNumPeriods = metrics.getCpuNumPeriods(); 65 long current = System.currentTimeMillis(); 66 while (System.currentTimeMillis() - current < 1000) ; // 1sec 67 long newCpuNumPeriods = metrics.getCpuNumPeriods(); 68 if (newCpuNumPeriods <= cpuNumPeriods) { 69 throw new RuntimeException("CPU shares failed, expected : [" 70 + cpuNumPeriods + "]" + ", got : " + "[" 71 + newCpuNumPeriods + "]"); 72 } 73 System.out.println("TEST PASSED!!!"); 74 } 75 76 private static void testCpuSets(String cpuset) { 77 int[] ipCpuSet; 78 String[] tokens = cpuset.split("-"); 79 if (tokens.length > 1) { // we are given range of CPUs 80 ipCpuSet = IntStream.rangeClosed(Integer.parseInt(tokens[0]), 81 Integer.parseInt(tokens[1])).toArray(); 82 } else if (cpuset.split(",").length > 1) { // list of cpus 83 ipCpuSet = Stream.of(cpuset.split(",")).mapToInt(Integer::parseInt).toArray(); 84 } else { // just a single cpu 85 ipCpuSet = new int[]{Integer.parseInt(cpuset)}; 86 } 87 88 Metrics metrics = Metrics.systemMetrics(); 89 int[] cpuSets = metrics.getCpuSetCpus(); 90 91 int[] effectiveCpus = metrics.getEffectiveCpuSetCpus(); 92 93 if (!Arrays.equals(ipCpuSet, cpuSets)) { 94 throw new RuntimeException("Cpusets not equal, expected : " 95 + Arrays.toString(ipCpuSet) + ", got : " + Arrays.toString(cpuSets)); 96 } 97 98 if (!Arrays.equals(ipCpuSet, effectiveCpus)) { 99 throw new RuntimeException("Effective Cpusets not equal, expected : " 100 + Arrays.toString(ipCpuSet) + ", got : " 101 + Arrays.toString(effectiveCpus)); 102 } 103 System.out.println("TEST PASSED!!!"); 104 } 105 106 private static void testCpuSetMemNodes(String cpusetMems) { 107 Metrics metrics = Metrics.systemMetrics(); 108 int[] cpuSets = metrics.getCpuSetMems(); 109 110 int[] ipCpuSet; 111 String[] tokens = cpusetMems.split("-"); 112 if (tokens.length > 1) { // we are given range of CPUs 113 ipCpuSet = IntStream.rangeClosed(Integer.parseInt(tokens[0]), 114 Integer.parseInt(tokens[1])).toArray(); 115 } else if (cpusetMems.split(",").length > 1) { // list of cpus 116 ipCpuSet = Stream.of(cpusetMems.split(",")).mapToInt(Integer::parseInt).toArray(); 117 } else { // just a single cpu 118 ipCpuSet = new int[]{Integer.parseInt(cpusetMems)}; 119 } 120 121 int[] effectiveMems = metrics.getEffectiveCpuSetMems(); 122 123 124 if (!Arrays.equals(ipCpuSet, cpuSets)) { 125 throw new RuntimeException("Cpuset.mems not equal, expected : " 126 + Arrays.toString(ipCpuSet) + ", got : " 127 + Arrays.toString(cpuSets)); 128 } 129 130 if (!Arrays.equals(ipCpuSet, effectiveMems)) { 131 throw new RuntimeException("Effective mem nodes not equal, expected : " 132 + Arrays.toString(ipCpuSet) + ", got : " 133 + Arrays.toString(effectiveMems)); 134 } 135 System.out.println("TEST PASSED!!!"); 136 } 137 138 private static void testCpuShares(long shares) { 139 Metrics metrics = Metrics.systemMetrics(); 140 long newShares = metrics.getCpuShares(); 141 if (newShares != shares) { 142 throw new RuntimeException("CPU shares not equal, expected : [" 143 + shares + "]" + ", got : " + "[" + newShares + "]"); 144 } 145 System.out.println("TEST PASSED!!!"); 146 } 147 148 private static void testCpuThrottling() { 149 Metrics metrics = Metrics.systemMetrics(); 150 long throttledTime = metrics.getCpuThrottledTime(); 151 long numThrottled = metrics.getCpuNumThrottled(); 152 153 long current = System.currentTimeMillis(); 154 155 while (System.currentTimeMillis() - current < 2000) ; // 2 sec 156 157 long newthrottledTime = metrics.getCpuThrottledTime(); 158 long newnumThrottled = metrics.getCpuNumThrottled(); 159 if (newthrottledTime <= throttledTime) { 160 throw new RuntimeException("CPU throttle failed, expected : [" 161 + newthrottledTime + "]" + ", got : " 162 + "[" + throttledTime + "]"); 163 } 164 165 if (newnumThrottled <= numThrottled) { 166 throw new RuntimeException("CPU num throttle failed, expected : [" 167 + newnumThrottled + "]" + ", got : " + "[" 168 + numThrottled + "]"); 169 } 170 System.out.println("TEST PASSED!!!"); 171 } 172 173 private static void testCombo(String cpuset, long quota, long period, long shares) { 174 testCpuSets(cpuset); 175 testCpuQuotaAndPeriod(quota, period); 176 testCpuShares(shares); 177 } 178 }