8226575: OperatingSystemMXBean should be made container aware

Reviewed-by: dholmes, bobv, mchung, sspitsyn
This commit is contained in:
Daniil Titov 2019-12-11 19:20:57 -08:00
parent efdf413a20
commit 7b82266a15
16 changed files with 440 additions and 60 deletions

View file

@ -29,6 +29,9 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.stream.Stream;
import jdk.internal.platform.cgroupv1.SubSystem.MemorySubSystem;
@ -70,7 +73,7 @@ public class Metrics implements jdk.internal.platform.Metrics {
* 34 28 0:29 / /sys/fs/cgroup/MemorySubSystem rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,MemorySubSystem
*/
try (Stream<String> lines =
Files.lines(Paths.get("/proc/self/mountinfo"))) {
readFilePrivileged(Paths.get("/proc/self/mountinfo"))) {
lines.filter(line -> line.contains(" - cgroup "))
.map(line -> line.split(" "))
@ -104,7 +107,7 @@ public class Metrics implements jdk.internal.platform.Metrics {
*
*/
try (Stream<String> lines =
Files.lines(Paths.get("/proc/self/cgroup"))) {
readFilePrivileged(Paths.get("/proc/self/cgroup"))) {
lines.map(line -> line.split(":"))
.filter(line -> (line.length >= 3))
@ -122,6 +125,25 @@ public class Metrics implements jdk.internal.platform.Metrics {
return null;
}
static Stream<String> readFilePrivileged(Path path) throws IOException {
try {
PrivilegedExceptionAction<Stream<String>> pea = () -> Files.lines(path);
return AccessController.doPrivileged(pea);
} catch (PrivilegedActionException e) {
unwrapIOExceptionAndRethrow(e);
throw new InternalError(e.getCause());
}
}
static void unwrapIOExceptionAndRethrow(PrivilegedActionException pae) throws IOException {
Throwable x = pae.getCause();
if (x instanceof IOException)
throw (IOException) x;
if (x instanceof RuntimeException)
throw (RuntimeException) x;
if (x instanceof Error)
throw (Error) x;
}
/**
* createSubSystem objects and initialize mount points
*/

View file

@ -30,6 +30,9 @@ import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -90,14 +93,24 @@ public class SubSystem {
public static String getStringValue(SubSystem subsystem, String parm) {
if (subsystem == null) return null;
try(BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(subsystem.path(), parm))) {
String line = bufferedReader.readLine();
return line;
}
catch (IOException e) {
try {
return subsystem.readStringValue(parm);
} catch (IOException e) {
return null;
}
}
private String readStringValue(String param) throws IOException {
PrivilegedExceptionAction<BufferedReader> pea = () ->
Files.newBufferedReader(Paths.get(path(), param));
try (BufferedReader bufferedReader =
AccessController.doPrivileged(pea)) {
String line = bufferedReader.readLine();
return line;
} catch (PrivilegedActionException e) {
Metrics.unwrapIOExceptionAndRethrow(e);
throw new InternalError(e.getCause());
}
}
public static long getLongValueMatchingLine(SubSystem subsystem,
@ -106,8 +119,8 @@ public class SubSystem {
Function<String, Long> conversion) {
long retval = Metrics.unlimited_minimum + 1; // default unlimited
try {
List<String> lines = Files.readAllLines(Paths.get(subsystem.path(), param));
for (String line: lines) {
List<String> lines = subsystem.readMatchingLines(param);
for (String line : lines) {
if (line.startsWith(match)) {
retval = conversion.apply(line);
break;
@ -119,6 +132,17 @@ public class SubSystem {
return retval;
}
private List<String> readMatchingLines(String param) throws IOException {
try {
PrivilegedExceptionAction<List<String>> pea = () ->
Files.readAllLines(Paths.get(path(), param));
return AccessController.doPrivileged(pea);
} catch (PrivilegedActionException e) {
Metrics.unwrapIOExceptionAndRethrow(e);
throw new InternalError(e.getCause());
}
}
public static long getLongValue(SubSystem subsystem, String parm) {
String strval = getStringValue(subsystem, parm);
return convertStringToLong(strval);
@ -169,7 +193,7 @@ public class SubSystem {
if (subsystem == null) return 0L;
try (Stream<String> lines = Files.lines(Paths.get(subsystem.path(), parm))) {
try (Stream<String> lines = Metrics.readFilePrivileged(Paths.get(subsystem.path(), parm))) {
Optional<String> result = lines.map(line -> line.split(" "))
.filter(line -> (line.length == 2 &&