Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/linux/classes/jdk/internal/platform/CgroupSubsystemController.java
40948 views
1
/*
2
* Copyright (c) 2020, Red Hat Inc.
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;
27
28
import java.io.IOException;
29
import java.io.UncheckedIOException;
30
import java.math.BigInteger;
31
import java.nio.file.Path;
32
import java.nio.file.Paths;
33
import java.util.ArrayList;
34
import java.util.List;
35
import java.util.Optional;
36
import java.util.function.Function;
37
import java.util.stream.Stream;
38
39
/**
40
* Cgroup version agnostic controller logic
41
*
42
*/
43
public interface CgroupSubsystemController {
44
45
public static final String EMPTY_STR = "";
46
47
public String path();
48
49
/**
50
* getStringValue
51
*
52
* Return the first line of the file "param" argument from the controller.
53
*
54
* TODO: Consider using weak references for caching BufferedReader object.
55
*
56
* @param controller
57
* @param param
58
* @return Returns the contents of the file specified by param or null if
59
* an error occurs.
60
*/
61
public static String getStringValue(CgroupSubsystemController controller, String param) {
62
if (controller == null) return null;
63
64
try {
65
return CgroupUtil.readStringValue(controller, param);
66
}
67
catch (IOException e) {
68
return null;
69
}
70
71
}
72
73
/**
74
* Get an entry from file "param" within the "controller" directory path
75
* which matches string "match". Applies "conversion" to the matching line.
76
*
77
* @param controller
78
* @param param
79
* @param match
80
* @param conversion
81
* @param defaultRetval
82
* @return The long value as derived by applying "conversion" to the matching
83
* line or "defaultRetval" if there was an error or no match found.
84
*/
85
public static long getLongValueMatchingLine(CgroupSubsystemController controller,
86
String param,
87
String match,
88
Function<String, Long> conversion,
89
long defaultRetval) {
90
long retval = defaultRetval;
91
if (controller == null) {
92
return retval;
93
}
94
try {
95
Path filePath = Paths.get(controller.path(), param);
96
List<String> lines = CgroupUtil.readAllLinesPrivileged(filePath);
97
for (String line : lines) {
98
if (line.startsWith(match)) {
99
retval = conversion.apply(line);
100
break;
101
}
102
}
103
} catch (IOException e) {
104
// Ignore. Default is unlimited.
105
}
106
return retval;
107
}
108
109
/**
110
* Get a long value from directory "controller" and file "param", by
111
* applying "conversion" to the string value within the file.
112
*
113
* @param controller
114
* @param param
115
* @param conversion
116
* @param defaultRetval
117
* @return The converted long value or "defaultRetval" if there was an
118
* error.
119
*/
120
public static long getLongValue(CgroupSubsystemController controller,
121
String param,
122
Function<String, Long> conversion,
123
long defaultRetval) {
124
String strval = getStringValue(controller, param);
125
if (strval == null) return defaultRetval;
126
return conversion.apply(strval);
127
}
128
129
/**
130
* Get a double value from file "param" within "controller".
131
*
132
* @param controller
133
* @param param
134
* @param defaultRetval
135
* @return The double value or "defaultRetval" if there was an error.
136
*/
137
public static double getDoubleValue(CgroupSubsystemController controller, String param, double defaultRetval) {
138
String strval = getStringValue(controller, param);
139
140
if (strval == null) return defaultRetval;
141
142
double retval = Double.parseDouble(strval);
143
144
return retval;
145
}
146
147
/**
148
* getLongEntry
149
*
150
* Return the long value from the line containing the string "entryname"
151
* within file "param" in the "controller".
152
*
153
* TODO: Consider using weak references for caching BufferedReader object.
154
*
155
* @param controller
156
* @param param
157
* @param entryname
158
* @return long value or "defaultRetval" if there was an error or no match
159
* was found.
160
*/
161
public static long getLongEntry(CgroupSubsystemController controller, String param, String entryname, long defaultRetval) {
162
if (controller == null) return defaultRetval;
163
164
try (Stream<String> lines = CgroupUtil.readFilePrivileged(Paths.get(controller.path(), param))) {
165
166
Optional<String> result = lines.map(line -> line.split(" "))
167
.filter(line -> (line.length == 2 &&
168
line[0].equals(entryname)))
169
.map(line -> line[1])
170
.findFirst();
171
172
return result.isPresent() ? Long.parseLong(result.get()) : defaultRetval;
173
} catch (UncheckedIOException e) {
174
return defaultRetval;
175
} catch (IOException e) {
176
return defaultRetval;
177
}
178
}
179
180
/**
181
* stringRangeToIntArray
182
*
183
* Convert a string in the form of 1,3-4,6 to an array of
184
* integers containing all the numbers in the range.
185
*
186
* @param range
187
* @return int[] containing a sorted list of numbers as represented by
188
* the string range. Returns null if there was an error or the input
189
* was an empty string.
190
*/
191
public static int[] stringRangeToIntArray(String range) {
192
if (range == null || EMPTY_STR.equals(range)) return null;
193
194
ArrayList<Integer> results = new ArrayList<>();
195
String strs[] = range.split(",");
196
for (String str : strs) {
197
if (str.contains("-")) {
198
String lohi[] = str.split("-");
199
// validate format
200
if (lohi.length != 2) {
201
continue;
202
}
203
int lo = Integer.parseInt(lohi[0]);
204
int hi = Integer.parseInt(lohi[1]);
205
for (int i = lo; i <= hi; i++) {
206
results.add(i);
207
}
208
}
209
else {
210
results.add(Integer.parseInt(str));
211
}
212
}
213
214
// sort results
215
results.sort(null);
216
217
// convert ArrayList to primitive int array
218
int[] ints = new int[results.size()];
219
int i = 0;
220
for (Integer n : results) {
221
ints[i++] = n;
222
}
223
224
return ints;
225
}
226
227
/**
228
* Convert a number from its string representation to a long.
229
*
230
* @param strval
231
* @param overflowRetval
232
* @param defaultRetval
233
* @return The converted long value. "overflowRetval" is returned if the
234
* string representation exceeds the range of type long.
235
* "defaultRetval" is returned if another type of error occurred
236
* during conversion.
237
*/
238
public static long convertStringToLong(String strval, long overflowRetval, long defaultRetval) {
239
long retval = defaultRetval;
240
if (strval == null) return retval;
241
242
try {
243
retval = Long.parseLong(strval);
244
} catch (NumberFormatException e) {
245
// For some properties (e.g. memory.limit_in_bytes, cgroups v1) we may overflow
246
// the range of signed long. In this case, return overflowRetval
247
BigInteger b = new BigInteger(strval);
248
if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
249
return overflowRetval;
250
}
251
}
252
return retval;
253
}
254
255
}
256
257