author | mbaesken |
Thu, 25 Jul 2019 12:15:27 +0200 | |
changeset 57535 | 51286afcbf49 |
parent 54814 | 207b7bf04f49 |
permissions | -rw-r--r-- |
50545 | 1 |
/* |
54813
0fe908af327e
8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents:
54577
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
50545 | 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.internal.platform.cgroupv1; |
|
27 |
||
28 |
import java.io.BufferedReader; |
|
29 |
import java.io.IOException; |
|
50989
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
30 |
import java.math.BigInteger; |
50545 | 31 |
import java.nio.file.Files; |
32 |
import java.nio.file.Paths; |
|
33 |
import java.util.ArrayList; |
|
54577
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
34 |
import java.util.List; |
50545 | 35 |
import java.util.Optional; |
54577
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
36 |
import java.util.function.Function; |
50545 | 37 |
import java.util.stream.Stream; |
38 |
||
39 |
public class SubSystem { |
|
40 |
String root; |
|
41 |
String mountPoint; |
|
42 |
String path; |
|
43 |
||
44 |
public SubSystem(String root, String mountPoint) { |
|
45 |
this.root = root; |
|
46 |
this.mountPoint = mountPoint; |
|
47 |
} |
|
48 |
||
49 |
public void setPath(String cgroupPath) { |
|
50 |
if (root != null && cgroupPath != null) { |
|
51 |
if (root.equals("/")) { |
|
54176
2786541e4f91
8220579: [Containers] SubSystem.java out of sync with osContainer_linux.cpp
sgehwolf
parents:
50989
diff
changeset
|
52 |
if (!cgroupPath.equals("/")) { |
50545 | 53 |
path = mountPoint + cgroupPath; |
54 |
} |
|
55 |
else { |
|
56 |
path = mountPoint; |
|
57 |
} |
|
58 |
} |
|
59 |
else { |
|
60 |
if (root.equals(cgroupPath)) { |
|
61 |
path = mountPoint; |
|
62 |
} |
|
63 |
else { |
|
54813
0fe908af327e
8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents:
54577
diff
changeset
|
64 |
if (cgroupPath.startsWith(root)) { |
50545 | 65 |
if (cgroupPath.length() > root.length()) { |
66 |
String cgroupSubstr = cgroupPath.substring(root.length()); |
|
67 |
path = mountPoint + cgroupSubstr; |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
public String path() { |
|
76 |
return path; |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* getSubSystemStringValue |
|
81 |
* |
|
82 |
* Return the first line of the file "parm" argument from the subsystem. |
|
83 |
* |
|
84 |
* TODO: Consider using weak references for caching BufferedReader object. |
|
85 |
* |
|
86 |
* @param subsystem |
|
87 |
* @param parm |
|
88 |
* @return Returns the contents of the file specified by param. |
|
89 |
*/ |
|
90 |
public static String getStringValue(SubSystem subsystem, String parm) { |
|
91 |
if (subsystem == null) return null; |
|
92 |
||
93 |
try(BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(subsystem.path(), parm))) { |
|
94 |
String line = bufferedReader.readLine(); |
|
95 |
return line; |
|
96 |
} |
|
97 |
catch (IOException e) { |
|
98 |
return null; |
|
99 |
} |
|
100 |
||
101 |
} |
|
102 |
||
54577
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
103 |
public static long getLongValueMatchingLine(SubSystem subsystem, |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
104 |
String param, |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
105 |
String match, |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
106 |
Function<String, Long> conversion) { |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
107 |
long retval = Metrics.unlimited_minimum + 1; // default unlimited |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
108 |
try { |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
109 |
List<String> lines = Files.readAllLines(Paths.get(subsystem.path(), param)); |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
110 |
for (String line: lines) { |
54814
207b7bf04f49
8222533: jtreg test jdk/internal/platform/cgroup/TestCgroupMetrics.java fails on SLES12.3 linux ppc64le machine
bobv
parents:
54813
diff
changeset
|
111 |
if (line.startsWith(match)) { |
54577
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
112 |
retval = conversion.apply(line); |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
113 |
break; |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
114 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
115 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
116 |
} catch (IOException e) { |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
117 |
// Ignore. Default is unlimited. |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
118 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
119 |
return retval; |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
120 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
121 |
|
50545 | 122 |
public static long getLongValue(SubSystem subsystem, String parm) { |
123 |
String strval = getStringValue(subsystem, parm); |
|
54577
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
124 |
return convertStringToLong(strval); |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
125 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
126 |
|
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
127 |
public static long convertStringToLong(String strval) { |
50989
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
128 |
long retval = 0; |
50545 | 129 |
if (strval == null) return 0L; |
130 |
||
50989
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
131 |
try { |
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
132 |
retval = Long.parseLong(strval); |
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
133 |
} catch (NumberFormatException e) { |
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
134 |
// For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long. |
57535
51286afcbf49
8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents:
54814
diff
changeset
|
135 |
// In this case, return Long.MAX_VALUE |
50989
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
136 |
BigInteger b = new BigInteger(strval); |
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
137 |
if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { |
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
138 |
return Long.MAX_VALUE; |
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
139 |
} |
3bc865cc2122
8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents:
50545
diff
changeset
|
140 |
} |
50545 | 141 |
return retval; |
142 |
} |
|
143 |
||
144 |
public static double getDoubleValue(SubSystem subsystem, String parm) { |
|
145 |
String strval = getStringValue(subsystem, parm); |
|
146 |
||
147 |
if (strval == null) return 0L; |
|
148 |
||
149 |
double retval = Double.parseDouble(strval); |
|
150 |
||
151 |
return retval; |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* getSubSystemlongEntry |
|
156 |
* |
|
157 |
* Return the long value from the line containing the string "entryname" |
|
158 |
* within file "parm" in the "subsystem". |
|
159 |
* |
|
160 |
* TODO: Consider using weak references for caching BufferedReader object. |
|
161 |
* |
|
162 |
* @param subsystem |
|
163 |
* @param parm |
|
164 |
* @param entryname |
|
165 |
* @return long value |
|
166 |
*/ |
|
167 |
public static long getLongEntry(SubSystem subsystem, String parm, String entryname) { |
|
168 |
String val = null; |
|
169 |
||
170 |
if (subsystem == null) return 0L; |
|
171 |
||
172 |
try (Stream<String> lines = Files.lines(Paths.get(subsystem.path(), parm))) { |
|
173 |
||
174 |
Optional<String> result = lines.map(line -> line.split(" ")) |
|
175 |
.filter(line -> (line.length == 2 && |
|
176 |
line[0].equals(entryname))) |
|
177 |
.map(line -> line[1]) |
|
178 |
.findFirst(); |
|
179 |
||
180 |
return result.isPresent() ? Long.parseLong(result.get()) : 0L; |
|
181 |
} |
|
182 |
catch (IOException e) { |
|
183 |
return 0L; |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
public static int getIntValue(SubSystem subsystem, String parm) { |
|
188 |
String val = getStringValue(subsystem, parm); |
|
189 |
||
190 |
if (val == null) return 0; |
|
191 |
||
192 |
return Integer.parseInt(val); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* StringRangeToIntArray |
|
197 |
* |
|
198 |
* Convert a string in the form of 1,3-4,6 to an array of |
|
199 |
* integers containing all the numbers in the range. |
|
200 |
* |
|
201 |
* @param range |
|
202 |
* @return int[] containing a sorted list of processors or memory nodes |
|
203 |
*/ |
|
204 |
public static int[] StringRangeToIntArray(String range) { |
|
205 |
int[] ints = new int[0]; |
|
206 |
||
207 |
if (range == null) return ints; |
|
208 |
||
209 |
ArrayList<Integer> results = new ArrayList<>(); |
|
210 |
String strs[] = range.split(","); |
|
211 |
for (String str : strs) { |
|
212 |
if (str.contains("-")) { |
|
213 |
String lohi[] = str.split("-"); |
|
214 |
// validate format |
|
215 |
if (lohi.length != 2) { |
|
216 |
continue; |
|
217 |
} |
|
218 |
int lo = Integer.parseInt(lohi[0]); |
|
219 |
int hi = Integer.parseInt(lohi[1]); |
|
220 |
for (int i = lo; i <= hi; i++) { |
|
221 |
results.add(i); |
|
222 |
} |
|
223 |
} |
|
224 |
else { |
|
225 |
results.add(Integer.parseInt(str)); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
// sort results |
|
230 |
results.sort(null); |
|
231 |
||
232 |
// convert ArrayList to primitive int array |
|
233 |
ints = new int[results.size()]; |
|
234 |
int i = 0; |
|
235 |
for (Integer n : results) { |
|
236 |
ints[i++] = n; |
|
237 |
} |
|
238 |
||
239 |
return ints; |
|
240 |
} |
|
54577
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
241 |
|
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
242 |
public static class MemorySubSystem extends SubSystem { |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
243 |
|
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
244 |
private boolean hierarchical; |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
245 |
|
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
246 |
public MemorySubSystem(String root, String mountPoint) { |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
247 |
super(root, mountPoint); |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
248 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
249 |
|
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
250 |
boolean isHierarchical() { |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
251 |
return hierarchical; |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
252 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
253 |
|
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
254 |
void setHierarchical(boolean hierarchical) { |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
255 |
this.hierarchical = hierarchical; |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
256 |
} |
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
257 |
|
1c242c2d037f
8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents:
54176
diff
changeset
|
258 |
} |
50545 | 259 |
} |