author | bobv |
Thu, 19 Jul 2018 12:57:12 -0400 | |
changeset 51162 | 52f96cca600f |
parent 50545 | 292a4a87c321 |
permissions | -rw-r--r-- |
50545 | 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 |
||
51162
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
98 |
// Check to see if this metric is supported on this platform |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
99 |
if (effectiveCpus.length != 0) { |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
100 |
if (!Arrays.equals(ipCpuSet, effectiveCpus)) { |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
101 |
throw new RuntimeException("Effective Cpusets not equal, expected : " |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
102 |
+ Arrays.toString(ipCpuSet) + ", got : " |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
103 |
+ Arrays.toString(effectiveCpus)); |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
104 |
} |
50545 | 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 |
||
51162
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
133 |
// Check to see if this metric is supported on this platform |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
134 |
if (effectiveMems.length != 0) { |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
135 |
if (!Arrays.equals(ipCpuSet, effectiveMems)) { |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
136 |
throw new RuntimeException("Effective mem nodes not equal, expected : " |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
137 |
+ Arrays.toString(ipCpuSet) + ", got : " |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
138 |
+ Arrays.toString(effectiveMems)); |
52f96cca600f
8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents:
50545
diff
changeset
|
139 |
} |
50545 | 140 |
} |
141 |
System.out.println("TEST PASSED!!!"); |
|
142 |
} |
|
143 |
||
144 |
private static void testCpuShares(long shares) { |
|
145 |
Metrics metrics = Metrics.systemMetrics(); |
|
146 |
long newShares = metrics.getCpuShares(); |
|
147 |
if (newShares != shares) { |
|
148 |
throw new RuntimeException("CPU shares not equal, expected : [" |
|
149 |
+ shares + "]" + ", got : " + "[" + newShares + "]"); |
|
150 |
} |
|
151 |
System.out.println("TEST PASSED!!!"); |
|
152 |
} |
|
153 |
||
154 |
private static void testCpuThrottling() { |
|
155 |
Metrics metrics = Metrics.systemMetrics(); |
|
156 |
long throttledTime = metrics.getCpuThrottledTime(); |
|
157 |
long numThrottled = metrics.getCpuNumThrottled(); |
|
158 |
||
159 |
long current = System.currentTimeMillis(); |
|
160 |
||
161 |
while (System.currentTimeMillis() - current < 2000) ; // 2 sec |
|
162 |
||
163 |
long newthrottledTime = metrics.getCpuThrottledTime(); |
|
164 |
long newnumThrottled = metrics.getCpuNumThrottled(); |
|
165 |
if (newthrottledTime <= throttledTime) { |
|
166 |
throw new RuntimeException("CPU throttle failed, expected : [" |
|
167 |
+ newthrottledTime + "]" + ", got : " |
|
168 |
+ "[" + throttledTime + "]"); |
|
169 |
} |
|
170 |
||
171 |
if (newnumThrottled <= numThrottled) { |
|
172 |
throw new RuntimeException("CPU num throttle failed, expected : [" |
|
173 |
+ newnumThrottled + "]" + ", got : " + "[" |
|
174 |
+ numThrottled + "]"); |
|
175 |
} |
|
176 |
System.out.println("TEST PASSED!!!"); |
|
177 |
} |
|
178 |
||
179 |
private static void testCombo(String cpuset, long quota, long period, long shares) { |
|
180 |
testCpuSets(cpuset); |
|
181 |
testCpuQuotaAndPeriod(quota, period); |
|
182 |
testCpuShares(shares); |
|
183 |
} |
|
184 |
} |