Skip to content

Commit

Permalink
8231111: Cgroups v2: Rework Metrics in java.base so as to recognize u…
Browse files Browse the repository at this point in the history
…nified hierarchy

8275713: TestDockerMemoryMetrics test fails on recent runc
8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)

Reviewed-by: sgehwolf, andrew
Backport-of: 4def210a22faaec6b47912dd314e6365ea48d28f
  • Loading branch information
Jonathan Dowland committed Dec 5, 2022
1 parent 5510145 commit c9007cd
Show file tree
Hide file tree
Showing 31 changed files with 3,328 additions and 1,289 deletions.
2 changes: 1 addition & 1 deletion jdk/make/lib/CoreLibraries.gmk
Expand Up @@ -165,7 +165,7 @@ endif

# Make it possible to override this variable
ifeq ($(OPENJDK_TARGET_OS), linux)
# Linux-only symbol Java_jdk_internal_platform_cgroupv1_Metrics_isUseContainerSupport
# Linux-only symbol Java_jdk_internal_platform_CgroupMetrics_isUseContainerSupport
LIBJAVA_MAPFILE ?= $(JDK_TOPDIR)/make/mapfiles/libjava/mapfile-linux
else
LIBJAVA_MAPFILE ?= $(JDK_TOPDIR)/make/mapfiles/libjava/mapfile-vers
Expand Down
2 changes: 1 addition & 1 deletion jdk/make/mapfiles/libjava/mapfile-linux
Expand Up @@ -278,7 +278,7 @@ SUNWprivate_1.1 {
Java_sun_misc_VM_initialize;
Java_sun_misc_VMSupport_initAgentProperties;
Java_sun_misc_VMSupport_getVMTemporaryDirectory;
Java_jdk_internal_platform_cgroupv1_Metrics_isUseContainerSupport;
Java_jdk_internal_platform_CgroupMetrics_isUseContainerSupport;

# ZipFile.c needs this one
throwFileNotFoundException;
Expand Down
70 changes: 70 additions & 0 deletions jdk/src/linux/classes/jdk/internal/platform/CgroupInfo.java
@@ -0,0 +1,70 @@
/*
* 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;

/**
* Data structure to hold info from /proc/self/cgroup
*
* man 7 cgroups
*
* @see CgroupSubsystemFactory
*/
class CgroupInfo {

private final String name;
private final int hierarchyId;
private final boolean enabled;

private CgroupInfo(String name, int hierarchyId, boolean enabled) {
this.name = name;
this.hierarchyId = hierarchyId;
this.enabled = enabled;
}

String getName() {
return name;
}

int getHierarchyId() {
return hierarchyId;
}

boolean isEnabled() {
return enabled;
}

static CgroupInfo fromCgroupsLine(String line) {
String[] tokens = line.split("\\s+");
if (tokens.length != 4) {
return null;
}
// discard 3'rd field, num_cgroups
return new CgroupInfo(tokens[0] /* name */,
Integer.parseInt(tokens[1]) /* hierarchyId */,
(Integer.parseInt(tokens[3]) == 1) /* enabled */);
}

}
172 changes: 172 additions & 0 deletions jdk/src/linux/classes/jdk/internal/platform/CgroupMetrics.java
@@ -0,0 +1,172 @@
/*
* 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.util.Objects;

public class CgroupMetrics implements Metrics {

private final CgroupSubsystem subsystem;

CgroupMetrics(CgroupSubsystem subsystem) {
this.subsystem = Objects.requireNonNull(subsystem);
}

@Override
public String getProvider() {
return subsystem.getProvider();
}

@Override
public long getCpuUsage() {
return subsystem.getCpuUsage();
}

@Override
public long[] getPerCpuUsage() {
return subsystem.getPerCpuUsage();
}

@Override
public long getCpuUserUsage() {
return subsystem.getCpuUserUsage();
}

@Override
public long getCpuSystemUsage() {
return subsystem.getCpuSystemUsage();
}

@Override
public long getCpuPeriod() {
return subsystem.getCpuPeriod();
}

@Override
public long getCpuQuota() {
return subsystem.getCpuQuota();
}

@Override
public long getCpuShares() {
return subsystem.getCpuShares();
}

@Override
public long getCpuNumPeriods() {
return subsystem.getCpuNumPeriods();
}

@Override
public long getCpuNumThrottled() {
return subsystem.getCpuNumThrottled();
}

@Override
public long getCpuThrottledTime() {
return subsystem.getCpuThrottledTime();
}

@Override
public long getEffectiveCpuCount() {
return subsystem.getEffectiveCpuCount();
}

@Override
public int[] getCpuSetCpus() {
return subsystem.getCpuSetCpus();
}

@Override
public int[] getEffectiveCpuSetCpus() {
return subsystem.getEffectiveCpuSetCpus();
}

@Override
public int[] getCpuSetMems() {
return subsystem.getCpuSetMems();
}

@Override
public int[] getEffectiveCpuSetMems() {
return subsystem.getEffectiveCpuSetMems();
}

public long getMemoryFailCount() {
return subsystem.getMemoryFailCount();
}

@Override
public long getMemoryLimit() {
return subsystem.getMemoryLimit();
}

@Override
public long getMemoryUsage() {
return subsystem.getMemoryUsage();
}

@Override
public long getTcpMemoryUsage() {
return subsystem.getTcpMemoryUsage();
}

@Override
public long getMemoryAndSwapLimit() {
return subsystem.getMemoryAndSwapLimit();
}

@Override
public long getMemoryAndSwapUsage() {
return subsystem.getMemoryAndSwapUsage();
}

@Override
public long getMemorySoftLimit() {
return subsystem.getMemorySoftLimit();
}

@Override
public long getBlkIOServiceCount() {
return subsystem.getBlkIOServiceCount();
}

@Override
public long getBlkIOServiced() {
return subsystem.getBlkIOServiced();
}

public static Metrics getInstance() {
if (!isUseContainerSupport()) {
// Return null on -XX:-UseContainerSupport
return null;
}
return CgroupSubsystemFactory.create();
}

private static native boolean isUseContainerSupport();

}
40 changes: 40 additions & 0 deletions jdk/src/linux/classes/jdk/internal/platform/CgroupSubsystem.java
@@ -0,0 +1,40 @@
/*
* 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;

/**
* Marker interface for cgroup-based metrics
*
*/
public interface CgroupSubsystem extends Metrics {

/**
* Returned for metrics of type long if the underlying implementation
* has determined that no limit is being imposed.
*/
public static final long LONG_RETVAL_UNLIMITED = -1;

}

1 comment on commit c9007cd

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.