1 /*
   2  * Copyright (c) 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.test.lib.containers.cgroup;
  27 
  28 import java.io.IOException;
  29 import java.math.BigInteger;
  30 import java.util.stream.IntStream;
  31 import java.util.stream.Stream;
  32 
  33 interface CgroupMetricsTester {
  34 
  35     public static final double ERROR_MARGIN = 0.1;
  36     public static final String EMPTY_STR = "";
  37 
  38     public void testMemorySubsystem();
  39     public void testCpuAccounting();
  40     public void testCpuSchedulingMetrics();
  41     public void testCpuSets();
  42     public void testCpuConsumption() throws IOException, InterruptedException;
  43     public void testMemoryUsage() throws Exception;
  44     public void testMisc();
  45 
  46     public static long convertStringToLong(String strval, long overflowRetval) {
  47         long retval = 0;
  48         if (strval == null) return 0L;
  49 
  50         try {
  51             retval = Long.parseLong(strval);
  52         } catch (NumberFormatException e) {
  53             // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
  54             // In this case, return Long.MAX_VALUE
  55             BigInteger b = new BigInteger(strval);
  56             if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
  57                 return overflowRetval;
  58             }
  59         }
  60         return retval;
  61     }
  62 
  63     public static boolean compareWithErrorMargin(long oldVal, long newVal) {
  64         return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
  65     }
  66 
  67     public static boolean compareWithErrorMargin(double oldVal, double newVal) {
  68         return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
  69     }
  70 
  71     public static void fail(String controller, String metric, long oldVal, long testVal) {
  72         throw new RuntimeException("Test failed for - " + controller + ":"
  73                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  74     }
  75 
  76     public static void fail(String controller, String metric, String oldVal, String testVal) {
  77         throw new RuntimeException("Test failed for - " + controller + ":"
  78                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  79     }
  80 
  81     public static void fail(String controller, String metric, double oldVal, double testVal) {
  82         throw new RuntimeException("Test failed for - " + controller + ":"
  83                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  84     }
  85 
  86     public static void fail(String controller, String metric, boolean oldVal, boolean testVal) {
  87         throw new RuntimeException("Test failed for - " + controller + ":"
  88                 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
  89     }
  90 
  91     public static void warn(String controller, String metric, long oldVal, long testVal) {
  92         System.err.println("Warning - " + controller + ":" + metric
  93                 + ", expected [" + oldVal + "], got [" + testVal + "]");
  94     }
  95 
  96     public static Integer[] convertCpuSetsToArray(String cpusstr) {
  97         if (cpusstr == null || EMPTY_STR.equals(cpusstr)) {
  98             return new Integer[0];
  99         }
 100         // Parse range string in the format 1,2-6,7
 101         Integer[] cpuSets = Stream.of(cpusstr.split(",")).flatMap(a -> {
 102             if (a.contains("-")) {
 103                 String[] range = a.split("-");
 104                 return IntStream.rangeClosed(Integer.parseInt(range[0]),
 105                         Integer.parseInt(range[1])).boxed();
 106             } else {
 107                 return Stream.of(Integer.parseInt(a));
 108             }
 109         }).toArray(Integer[]::new);
 110         return cpuSets;
 111     }
 112 
 113 }