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