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.25;
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);
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 }
|
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.Arrays;
29 import java.util.stream.IntStream;
30 import java.util.stream.Stream;
31
32 interface CgroupMetricsTester {
33
34 public static final double ERROR_MARGIN = 0.25;
35 public static final String EMPTY_STR = "";
36
37 public void testMemorySubsystem();
38 public void testCpuAccounting();
39 public void testCpuSchedulingMetrics();
40 public void testCpuSets();
41 public void testCpuConsumption() throws IOException, InterruptedException;
42 public void testMemoryUsage() throws Exception;
43 public void testMisc();
44
45 public static long convertStringToLong(String strval, long initialVal, long overflowRetval) {
46 long retval = initialVal;
47 if (strval == null) return retval;
48
49 try {
50 retval = Long.parseLong(strval);
51 } catch (NumberFormatException e) {
52 // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
53 // In this case, return Long.MAX_VALUE
54 BigInteger b = new BigInteger(strval);
55 if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
56 return overflowRetval;
57 }
58 }
59 return retval;
60 }
61
62 public static boolean compareWithErrorMargin(long oldVal, long newVal) {
63 return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
64 }
65
66 public static boolean compareWithErrorMargin(double oldVal, double newVal) {
67 return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
77 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
78 }
79
80 public static void fail(String controller, String metric, double oldVal, double testVal) {
81 throw new RuntimeException("Test failed for - " + controller + ":"
82 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
83 }
84
85 public static void fail(String controller, String metric, boolean oldVal, boolean testVal) {
86 throw new RuntimeException("Test failed for - " + controller + ":"
87 + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
88 }
89
90 public static void warn(String controller, String metric, long oldVal, long testVal) {
91 System.err.println("Warning - " + controller + ":" + metric
92 + ", expected [" + oldVal + "], got [" + testVal + "]");
93 }
94
95 public static Integer[] convertCpuSetsToArray(String cpusstr) {
96 if (cpusstr == null || EMPTY_STR.equals(cpusstr)) {
97 return null;
98 }
99 // Parse range string in the format 1,2-6,7
100 Integer[] cpuSets = Stream.of(cpusstr.split(",")).flatMap(a -> {
101 if (a.contains("-")) {
102 String[] range = a.split("-");
103 return IntStream.rangeClosed(Integer.parseInt(range[0]),
104 Integer.parseInt(range[1])).boxed();
105 } else {
106 return Stream.of(Integer.parseInt(a));
107 }
108 }).toArray(Integer[]::new);
109 return cpuSets;
110 }
111
112 public static Integer[] boxedArrayOrNull(int[] primitiveArray) {
113 if (primitiveArray == null) {
114 return null;
115 }
116 return Arrays.stream(primitiveArray).boxed().toArray(Integer[]::new);
117 }
118
119 public static Integer[] sortAllowNull(Integer[] array) {
120 if (array == null) {
121 return null;
122 }
123 Arrays.sort(array);
124 return array;
125 }
126
127 }
|