1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* Copyright (c) 2020, Red Hat Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.platform;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Cgroup version agnostic controller logic
*
*/
public interface CgroupSubsystemController {
public static final String EMPTY_STR = "";
public String path();
/**
* getStringValue
*
* Return the first line of the file "param" argument from the controller.
*
* TODO: Consider using weak references for caching BufferedReader object.
*
* @param controller
* @param param
* @return Returns the contents of the file specified by param or null if
* an error occurs.
*/
public static String getStringValue(CgroupSubsystemController controller, String param) {
if (controller == null) return null;
try {
return CgroupUtil.readStringValue(controller, param);
}
catch (IOException e) {
return null;
}
}
/**
* Get an entry from file "param" within the "controller" directory path
* which matches string "match". Applies "conversion" to the matching line.
*
* @param controller
* @param param
* @param match
* @param conversion
* @param defaultRetval
* @return The long value as derived by applying "conversion" to the matching
* line or "defaultRetval" if there was an error or no match found.
*/
public static long getLongValueMatchingLine(CgroupSubsystemController controller,
String param,
String match,
Function<String, Long> conversion,
long defaultRetval) {
long retval = defaultRetval;
if (controller == null) {
return retval;
}
try {
Path filePath = Paths.get(controller.path(), param);
List<String> lines = CgroupUtil.readAllLinesPrivileged(filePath);
for (String line : lines) {
if (line.startsWith(match)) {
retval = conversion.apply(line);
break;
}
}
} catch (IOException e) {
// Ignore. Default is unlimited.
}
return retval;
}
/**
* Get a long value from directory "controller" and file "param", by
* applying "conversion" to the string value within the file.
*
* @param controller
* @param param
* @param conversion
* @param defaultRetval
* @return The converted long value or "defaultRetval" if there was an
* error.
*/
public static long getLongValue(CgroupSubsystemController controller,
String param,
Function<String, Long> conversion,
long defaultRetval) {
String strval = getStringValue(controller, param);
if (strval == null) return defaultRetval;
return conversion.apply(strval);
}
/**
* Get a double value from file "param" within "controller".
*
* @param controller
* @param param
* @param defaultRetval
* @return The double value or "defaultRetval" if there was an error.
*/
public static double getDoubleValue(CgroupSubsystemController controller, String param, double defaultRetval) {
String strval = getStringValue(controller, param);
if (strval == null) return defaultRetval;
double retval = Double.parseDouble(strval);
return retval;
}
/**
* getLongEntry
*
* Return the long value from the line containing the string "entryname"
* within file "param" in the "controller".
*
* TODO: Consider using weak references for caching BufferedReader object.
*
* @param controller
* @param param
* @param entryname
* @return long value or "defaultRetval" if there was an error or no match
* was found.
*/
public static long getLongEntry(CgroupSubsystemController controller, String param, String entryname, long defaultRetval) {
if (controller == null) return defaultRetval;
try (Stream<String> lines = CgroupUtil.readFilePrivileged(Paths.get(controller.path(), param))) {
Optional<String> result = lines.map(line -> line.split(" "))
.filter(line -> (line.length == 2 &&
line[0].equals(entryname)))
.map(line -> line[1])
.findFirst();
return result.isPresent() ? Long.parseLong(result.get()) : defaultRetval;
} catch (UncheckedIOException e) {
return defaultRetval;
} catch (IOException e) {
return defaultRetval;
}
}
/**
* stringRangeToIntArray
*
* Convert a string in the form of 1,3-4,6 to an array of
* integers containing all the numbers in the range.
*
* @param range
* @return int[] containing a sorted list of numbers as represented by
* the string range. Returns null if there was an error or the input
* was an empty string.
*/
public static int[] stringRangeToIntArray(String range) {
if (range == null || EMPTY_STR.equals(range)) return null;
ArrayList<Integer> results = new ArrayList<>();
String strs[] = range.split(",");
for (String str : strs) {
if (str.contains("-")) {
String lohi[] = str.split("-");
// validate format
if (lohi.length != 2) {
continue;
}
int lo = Integer.parseInt(lohi[0]);
int hi = Integer.parseInt(lohi[1]);
for (int i = lo; i <= hi; i++) {
results.add(i);
}
}
else {
results.add(Integer.parseInt(str));
}
}
// sort results
results.sort(null);
// convert ArrayList to primitive int array
int[] ints = new int[results.size()];
int i = 0;
for (Integer n : results) {
ints[i++] = n;
}
return ints;
}
/**
* Convert a number from its string representation to a long.
*
* @param strval
* @param overflowRetval
* @param defaultRetval
* @return The converted long value. "overflowRetval" is returned if the
* string representation exceeds the range of type long.
* "defaultRetval" is returned if another type of error occurred
* during conversion.
*/
public static long convertStringToLong(String strval, long overflowRetval, long defaultRetval) {
long retval = defaultRetval;
if (strval == null) return retval;
try {
retval = Long.parseLong(strval);
} catch (NumberFormatException e) {
// For some properties (e.g. memory.limit_in_bytes, cgroups v1) we may overflow
// the range of signed long. In this case, return overflowRetval
BigInteger b = new BigInteger(strval);
if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
return overflowRetval;
}
}
return retval;
}
}
|