Skip to content
Permalink
Browse files
8226575: OperatingSystemMXBean should be made container aware
Reviewed-by: yan
Backport-of: 7b82266
  • Loading branch information
Ekaterina Vergizova authored and Yuri Nesterenko committed Nov 26, 2020
1 parent 92da5d8 commit 89354b88f1c97dd37b83ab8fb9b8647ae642916e
@@ -29,6 +29,9 @@
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 @@ private static Metrics initContainerSubSystems() {
* 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 @@ private static Metrics initContainerSubSystems() {
*
*/
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 @@ private static Metrics initContainerSubSystems() {
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
*/
@@ -30,6 +30,9 @@
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 String path() {
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 static long getLongValueMatchingLine(SubSystem 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 static long getLongValueMatchingLine(SubSystem 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 static long getLongEntry(SubSystem subsystem, String parm, String entryna

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 &&
@@ -208,6 +208,8 @@
java.management,
jdk.management.agent,
jdk.internal.jvmstat;
exports jdk.internal.platform to
jdk.management;
exports jdk.internal.ref to
java.desktop;
exports jdk.internal.reflect to
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@@ -43,3 +43,17 @@ Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
{
return -1.0;
}

JNIEXPORT jdouble JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getSingleCpuLoad0
(JNIEnv *env, jobject dummy, jint cpu_number)
{
return -1.0;
}

JNIEXPORT jint JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
(JNIEnv *env, jobject mbean)
{
return -1;
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -198,17 +198,19 @@ static int get_jvmticks(ticks *pticks) {
* This method must be called first, before any data can be gathererd.
*/
int perfInit() {
static int initialized=1;
static int initialized = 0;

if (!initialized) {
int i;

int n = sysconf(_SC_NPROCESSORS_ONLN);
// We need to allocate counters for all CPUs, including ones that
// are currently offline as they could be turned online later.
int n = sysconf(_SC_NPROCESSORS_CONF);
if (n <= 0) {
n = 1;
}

counters.cpus = calloc(n,sizeof(ticks));
counters.nProcs = n;
if (counters.cpus != NULL) {
// For the CPU load
get_totalticks(-1, &counters.cpuTicks);
@@ -323,7 +325,7 @@ JNIEXPORT jdouble JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getSystemCpuLoad0
(JNIEnv *env, jobject dummy)
{
if(perfInit() == 0) {
if (perfInit() == 0) {
return get_cpu_load(-1);
} else {
return -1.0;
@@ -334,9 +336,31 @@ JNIEXPORT jdouble JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
(JNIEnv *env, jobject dummy)
{
if(perfInit() == 0) {
if (perfInit() == 0) {
return get_process_load();
} else {
return -1.0;
}
}

JNIEXPORT jdouble JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getSingleCpuLoad0
(JNIEnv *env, jobject mbean, jint cpu_number)
{
if (perfInit() == 0 && cpu_number >= 0 && cpu_number < counters.nProcs) {
return get_cpu_load(cpu_number);
} else {
return -1.0;
}
}

JNIEXPORT jint JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
(JNIEnv *env, jobject mbean)
{
if (perfInit() == 0) {
return counters.nProcs;
} else {
return -1;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -159,3 +159,17 @@ Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0

return cpu;
}

JNIEXPORT jdouble JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getSingleCpuLoad0
(JNIEnv *env, jobject dummy, jint cpu_number)
{
return -1.0;
}

JNIEXPORT jint JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
(JNIEnv *env, jobject mbean)
{
return -1;
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,12 @@
* on which the Java virtual machine is running.
*
* <p>
* This interface provides information about the operating environment
* on which the Java virtual machine is running. That might be a native
* operating system, a virtualized operating system environment, or a
* container-managed environment.
*
* <p>
* The {@code OperatingSystemMXBean} object returned by
* {@link java.lang.management.ManagementFactory#getOperatingSystemMXBean()}
* is an instance of the implementation class of this interface
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -239,3 +239,16 @@ Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
return get_process_load();
}

JNIEXPORT jdouble JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getSingleCpuLoad0
(JNIEnv *env, jobject mbean, jint cpu_number)
{
return -1.0;
}

JNIEXPORT jint JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getHostConfiguredCpuCount0
(JNIEnv *env, jobject mbean)
{
return -1;
}

1 comment on commit 89354b8

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 89354b8 Nov 26, 2020

Please sign in to comment.