Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy #121

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;

}