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 jdk.internal.platform.Metrics;
|
|
26 |
|
|
27 |
public class MetricsMemoryTester {
|
|
28 |
public static void main(String[] args) {
|
|
29 |
System.out.println(Arrays.toString(args));
|
|
30 |
switch (args[0]) {
|
|
31 |
case "memory":
|
|
32 |
testMemoryLimit(args[1]);
|
|
33 |
break;
|
|
34 |
case "memoryswap":
|
|
35 |
testMemoryAndSwapLimit(args[1], args[2]);
|
|
36 |
break;
|
|
37 |
case "kernelmem":
|
|
38 |
testKernelMemoryLimit(args[1]);
|
|
39 |
break;
|
|
40 |
case "oomkill":
|
|
41 |
testOomKillFlag(Boolean.parseBoolean(args[2]));
|
|
42 |
break;
|
|
43 |
case "failcount":
|
|
44 |
testMemoryFailCount();
|
|
45 |
break;
|
|
46 |
case "softlimit":
|
|
47 |
testMemorySoftLimit(args[1]);
|
|
48 |
break;
|
|
49 |
}
|
|
50 |
}
|
|
51 |
|
|
52 |
private static void testMemoryLimit(String value) {
|
|
53 |
long limit = getMemoryValue(value);
|
|
54 |
|
|
55 |
if (limit != Metrics.systemMetrics().getMemoryLimit()) {
|
|
56 |
throw new RuntimeException("Memory limit not equal, expected : ["
|
|
57 |
+ limit + "]" + ", got : ["
|
|
58 |
+ Metrics.systemMetrics().getMemoryLimit() + "]");
|
|
59 |
}
|
|
60 |
System.out.println("TEST PASSED!!!");
|
|
61 |
}
|
|
62 |
|
|
63 |
private static void testMemoryFailCount() {
|
|
64 |
long count = Metrics.systemMetrics().getMemoryFailCount();
|
|
65 |
|
|
66 |
// Allocate 512M of data
|
|
67 |
long[][] longs = new long[64][];
|
|
68 |
for (int i = 1; i <= 64; i++) {
|
|
69 |
try {
|
|
70 |
longs[i] = new long[8 * 1024 * 1024];
|
|
71 |
} catch (Error e) { // OOM error
|
|
72 |
break;
|
|
73 |
}
|
|
74 |
}
|
|
75 |
if (Metrics.systemMetrics().getMemoryFailCount() <= count) {
|
|
76 |
throw new RuntimeException("Memory fail count : new : ["
|
|
77 |
+ Metrics.systemMetrics().getMemoryFailCount() + "]"
|
|
78 |
+ ", old : [" + count + "]");
|
|
79 |
}
|
|
80 |
System.out.println("TEST PASSED!!!");
|
|
81 |
}
|
|
82 |
|
|
83 |
private static void testMemorySoftLimit(String softLimit) {
|
|
84 |
|
|
85 |
long memorySoftLimit = Metrics.systemMetrics().getMemorySoftLimit();
|
|
86 |
long newmemorySoftLimit = getMemoryValue(softLimit);
|
|
87 |
|
|
88 |
if (newmemorySoftLimit != memorySoftLimit) {
|
|
89 |
throw new RuntimeException("Memory softlimit not equal, Actual : ["
|
|
90 |
+ newmemorySoftLimit + "]" + ", Expected : ["
|
|
91 |
+ memorySoftLimit + "]");
|
|
92 |
}
|
|
93 |
System.out.println("TEST PASSED!!!");
|
|
94 |
}
|
|
95 |
|
|
96 |
private static void testKernelMemoryLimit(String value) {
|
|
97 |
long limit = getMemoryValue(value);
|
|
98 |
if (limit != Metrics.systemMetrics().getKernelMemoryLimit()) {
|
|
99 |
throw new RuntimeException("Kernel Memory limit not equal, expected : ["
|
|
100 |
+ limit + "]" + ", got : ["
|
|
101 |
+ Metrics.systemMetrics().getKernelMemoryLimit() + "]");
|
|
102 |
}
|
|
103 |
System.out.println("TEST PASSED!!!");
|
|
104 |
}
|
|
105 |
|
|
106 |
private static void testMemoryAndSwapLimit(String memory, String memAndSwap) {
|
|
107 |
long expectedMem = getMemoryValue(memory);
|
|
108 |
long expectedMemAndSwap = getMemoryValue(memAndSwap);
|
|
109 |
|
|
110 |
if (expectedMem != Metrics.systemMetrics().getMemoryLimit()
|
|
111 |
|| expectedMemAndSwap != Metrics.systemMetrics().getMemoryAndSwapLimit()) {
|
|
112 |
System.err.println("Memory and swap limit not equal, expected : ["
|
|
113 |
+ expectedMem + ", " + expectedMemAndSwap + "]"
|
|
114 |
+ ", got : [" + Metrics.systemMetrics().getMemoryLimit()
|
|
115 |
+ ", " + Metrics.systemMetrics().getMemoryAndSwapLimit() + "]");
|
|
116 |
}
|
|
117 |
System.out.println("TEST PASSED!!!");
|
|
118 |
}
|
|
119 |
|
|
120 |
private static long getMemoryValue(String value) {
|
|
121 |
long result;
|
|
122 |
if (value.endsWith("m")) {
|
|
123 |
result = Long.parseLong(value.substring(0, value.length() - 1))
|
|
124 |
* 1024 * 1024;
|
|
125 |
} else if (value.endsWith("g")) {
|
|
126 |
result = Long.parseLong(value.substring(0, value.length() - 1))
|
|
127 |
* 1024 * 1024 * 1024;
|
|
128 |
} else {
|
|
129 |
result = Long.parseLong(value);
|
|
130 |
}
|
|
131 |
return result;
|
|
132 |
}
|
|
133 |
|
|
134 |
private static void testOomKillFlag(boolean oomKillFlag) {
|
|
135 |
if (!(oomKillFlag ^ Metrics.systemMetrics().isMemoryOOMKillEnabled())) {
|
|
136 |
throw new RuntimeException("oomKillFlag error");
|
|
137 |
}
|
|
138 |
System.out.println("TEST PASSED!!!");
|
|
139 |
}
|
|
140 |
}
|