1 /*
   2  * Copyright (c) 2020, Red Hat Inc.
   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 package jdk.test.lib.containers.cgroup;
  25 
  26 import java.io.IOException;
  27 import java.math.BigInteger;
  28 import java.util.stream.IntStream;
  29 import java.util.stream.Stream;
  30 
  31 interface CgroupMetricsTester {
  32 
  33     public static final double ERROR_MARGIN = 0.1;
  34     public static final String EMPTY_STR = "";
  35 
  36     public void testMemorySubsystem();
  37     public void testCpuAccounting();
  38     public void testCpuSchedulingMetrics();
  39     public void testCpuSets();
  40     public void testCpuConsumption() throws IOException, InterruptedException;
  41     public void testMemoryUsage() throws Exception;
  42     public void testMisc();
  43 
  44     public static long convertStringToLong(String strval, long overflowRetval) {
  45         long retval = 0;
  46         if (strval == null) return 0L;
  47 
  48         try {
  49             retval = Long.parseLong(strval);
  50         } catch (NumberFormatException e) {
  51             // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
  52             // In this case, return Long.MAX_VALUE
  53             BigInteger b = new BigInteger(strval);
  54             if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
  55                 return overflowRetval;
  56             }
  57         }
  58         return retval;
  59     }
  60 
  61     public static boolean compareWithErrorMargin(long oldVal, long newVal) {
  62         return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
  63     }
  64 
  65     public static boolean compareWithErrorMargin(double oldVal, double newVal) {
  66         return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
  67     }
  68 
  69     public static void fail(String controller, String metric, long oldVal, long testVal) {
  70         throw new RuntimeException("Test failed for - " + controller + ":"
  71                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  72     }
  73 
  74     public static void fail(String controller, String metric, String oldVal, String testVal) {
  75         throw new RuntimeException("Test failed for - " + controller + ":"
  76                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  77     }
  78 
  79     public static void fail(String controller, String metric, double oldVal, double testVal) {
  80         throw new RuntimeException("Test failed for - " + controller + ":"
  81                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  82     }
  83 
  84     public static void fail(String controller, String metric, boolean oldVal, boolean testVal) {
  85         throw new RuntimeException("Test failed for - " + controller + ":"
  86                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  87     }
  88 
  89     public static void warn(String controller, String metric, long oldVal, long testVal) {
  90         System.err.println("Warning - " + controller + ":" + metric
  91                 + ", expected [" + oldVal + "], got [" + testVal + "]");
  92     }
  93 
  94     public static Integer[] convertCpuSetsToArray(String cpusstr) {
  95         if (cpusstr == null || EMPTY_STR.equals(cpusstr)) {
  96             return new Integer[0];
  97         }
  98         // Parse range string in the format 1,2-6,7
  99         Integer[] cpuSets = Stream.of(cpusstr.split(",")).flatMap(a -> {
 100             if (a.contains("-")) {
 101                 String[] range = a.split("-");
 102                 return IntStream.rangeClosed(Integer.parseInt(range[0]),
 103                         Integer.parseInt(range[1])).boxed();
 104             } else {
 105                 return Stream.of(Integer.parseInt(a));
 106             }
 107         }).toArray(Integer[]::new);
 108         return cpuSets;
 109     }
 110 
 111 }