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         // Check to see if this metric is supported on this platform
  99         if (effectiveCpus.length != 0) {
 100             if (!Arrays.equals(ipCpuSet, effectiveCpus)) {
 101                 throw new RuntimeException("Effective Cpusets not equal, expected : "
 102                         + Arrays.toString(ipCpuSet) + ", got : "
 103                         + Arrays.toString(effectiveCpus));
 104             }
 105         }
 106         System.out.println("TEST PASSED!!!");
 107     }
 108 
 109     private static void testCpuSetMemNodes(String cpusetMems) {
 110         Metrics metrics = Metrics.systemMetrics();
 111         int[] cpuSets = metrics.getCpuSetMems();
 112 
 113         int[] ipCpuSet;
 114         String[] tokens = cpusetMems.split("-");
 115         if (tokens.length > 1) { // we are given range of CPUs
 116             ipCpuSet = IntStream.rangeClosed(Integer.parseInt(tokens[0]),
 117                     Integer.parseInt(tokens[1])).toArray();
 118         } else if (cpusetMems.split(",").length > 1) {   // list of cpus
 119             ipCpuSet = Stream.of(cpusetMems.split(",")).mapToInt(Integer::parseInt).toArray();
 120         } else { // just a single cpu
 121             ipCpuSet = new int[]{Integer.parseInt(cpusetMems)};
 122         }
 123 
 124         int[] effectiveMems = metrics.getEffectiveCpuSetMems();
 125 
 126 
 127         if (!Arrays.equals(ipCpuSet, cpuSets)) {
 128             throw new RuntimeException("Cpuset.mems not equal, expected : "
 129                     + Arrays.toString(ipCpuSet) + ", got : "
 130                     + Arrays.toString(cpuSets));
 131         }
 132 
 133         // Check to see if this metric is supported on this platform
 134         if (effectiveMems.length != 0) {
 135             if (!Arrays.equals(ipCpuSet, effectiveMems)) {
 136                 throw new RuntimeException("Effective mem nodes not equal, expected : "
 137                         + Arrays.toString(ipCpuSet) + ", got : "
 138                         + Arrays.toString(effectiveMems));
 139             }
 140         }
 141         System.out.println("TEST PASSED!!!");
 142     }
 143 
 144     private static void testCpuShares(long shares) {
 145         Metrics metrics = Metrics.systemMetrics();
 146         if ("cgroupv2".equals(metrics.getProvider()) && shares < 1024) {
 147             // Adjust input shares for < 1024 cpu shares as the
 148             // impl. rounds up to the next multiple of 1024
 149             shares = 1024;
 150         }
 151         long newShares = metrics.getCpuShares();
 152         if (newShares != shares) {
 153             throw new RuntimeException("CPU shares not equal, expected : ["
 154                     + shares + "]" + ", got : " + "[" + newShares + "]");
 155         }
 156         System.out.println("TEST PASSED!!!");
 157     }
 158 
 159     private static void testCpuThrottling() {
 160         Metrics metrics = Metrics.systemMetrics();
 161         long throttledTime = metrics.getCpuThrottledTime();
 162         long numThrottled = metrics.getCpuNumThrottled();
 163 
 164         long current = System.currentTimeMillis();
 165 
 166         while (System.currentTimeMillis() - current < 2000) ;  // 2 sec
 167 
 168         long newthrottledTime = metrics.getCpuThrottledTime();
 169         long newnumThrottled = metrics.getCpuNumThrottled();
 170         if (newthrottledTime <= throttledTime) {
 171             throw new RuntimeException("CPU throttle failed, expected : ["
 172                     + newthrottledTime + "]" + ", got : "
 173                     + "[" + throttledTime + "]");
 174         }
 175 
 176         if (newnumThrottled <= numThrottled) {
 177             throw new RuntimeException("CPU num throttle failed, expected : ["
 178                     + newnumThrottled + "]" + ", got : " + "["
 179                     + numThrottled + "]");
 180         }
 181         System.out.println("TEST PASSED!!!");
 182     }
 183 
 184     private static void testCombo(String cpuset, long quota, long period, long shares) {
 185         testCpuSets(cpuset);
 186         testCpuQuotaAndPeriod(quota, period);
 187         testCpuShares(shares);
 188     }
 189 }