Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/management/OperatingSystemImpl.java
32287 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import jdk.internal.platform.Metrics;28import java.util.concurrent.TimeUnit;2930/**31* Implementation class for the operating system.32* Standard and committed hotspot-specific metrics if any.33*34* ManagementFactory.getOperatingSystemMXBean() returns an instance35* of this class.36*/37class OperatingSystemImpl extends BaseOperatingSystemImpl38implements com.sun.management.UnixOperatingSystemMXBean {3940private static final int MAX_ATTEMPTS_NUMBER = 10;41private final Metrics containerMetrics;4243OperatingSystemImpl(VMManagement vm) {44super(vm);45this.containerMetrics = jdk.internal.platform.Container.metrics();46}4748public long getTotalSwapSpaceSize() {49if (containerMetrics != null) {50long limit = containerMetrics.getMemoryAndSwapLimit();51// The memory limit metrics is not available if JVM runs on Linux host (not in a docker container)52// or if a docker container was started without specifying a memory limit (without '--memory='53// Docker option). In latter case there is no limit on how much memory the container can use and54// it can use as much memory as the host's OS allows.55long memLimit = containerMetrics.getMemoryLimit();56if (limit >= 0 && memLimit >= 0) {57return limit - memLimit;58}59}60return getTotalSwapSpaceSize0();61}6263public long getFreeSwapSpaceSize() {64if (containerMetrics != null) {65long memSwapLimit = containerMetrics.getMemoryAndSwapLimit();66long memLimit = containerMetrics.getMemoryLimit();67if (memSwapLimit >= 0 && memLimit >= 0) {68long deltaLimit = memSwapLimit - memLimit;69// Return 0 when memSwapLimit == memLimit, which means no swap space is allowed.70// And the same for memSwapLimit < memLimit.71if (deltaLimit <= 0) {72return 0;73}74for (int attempt = 0; attempt < MAX_ATTEMPTS_NUMBER; attempt++) {75long memSwapUsage = containerMetrics.getMemoryAndSwapUsage();76long memUsage = containerMetrics.getMemoryUsage();77if (memSwapUsage > 0 && memUsage > 0) {78// We read "memory usage" and "memory and swap usage" not atomically,79// and it's possible to get the negative value when subtracting these two.80// If this happens just retry the loop for a few iterations.81long deltaUsage = memSwapUsage - memUsage;82if (deltaUsage >= 0) {83long freeSwap = deltaLimit - deltaUsage;84if (freeSwap >= 0) {85return freeSwap;86}87}88}89}90}91}92return getFreeSwapSpaceSize0();93}9495public long getFreePhysicalMemorySize() {96if (containerMetrics != null) {97long usage = containerMetrics.getMemoryUsage();98long limit = containerMetrics.getMemoryLimit();99if (usage > 0 && limit >= 0) {100return limit - usage;101}102}103return getFreePhysicalMemorySize0();104}105106public long getTotalPhysicalMemorySize() {107if (containerMetrics != null) {108long limit = containerMetrics.getMemoryLimit();109if (limit >= 0) {110return limit;111}112}113return getTotalPhysicalMemorySize0();114}115116public double getSystemCpuLoad() {117if (containerMetrics != null) {118long quota = containerMetrics.getCpuQuota();119if (quota > 0) {120long periodLength = containerMetrics.getCpuPeriod();121long numPeriods = containerMetrics.getCpuNumPeriods();122long usageNanos = containerMetrics.getCpuUsage();123if (periodLength > 0 && numPeriods > 0 && usageNanos > 0) {124long elapsedNanos = TimeUnit.MICROSECONDS.toNanos(periodLength * numPeriods);125double systemLoad = (double) usageNanos / elapsedNanos;126// Ensure the return value is in the range 0.0 -> 1.0127systemLoad = Math.max(0.0, systemLoad);128systemLoad = Math.min(1.0, systemLoad);129return systemLoad;130}131return -1;132} else {133// If CPU quotas are not active then find the average system load for134// all online CPUs that are allowed to run this container.135136// If the cpuset is the same as the host's one there is no need to iterate over each CPU137if (isCpuSetSameAsHostCpuSet()) {138return getSystemCpuLoad0();139} else {140int[] cpuSet = containerMetrics.getEffectiveCpuSetCpus();141if (cpuSet != null && cpuSet.length > 0) {142double systemLoad = 0.0;143for (int cpu : cpuSet) {144double cpuLoad = getSingleCpuLoad0(cpu);145if (cpuLoad < 0) {146return -1;147}148systemLoad += cpuLoad;149}150return systemLoad / cpuSet.length;151}152return -1;153}154}155}156return getSystemCpuLoad0();157}158159private boolean isCpuSetSameAsHostCpuSet() {160if (containerMetrics != null) {161return containerMetrics.getCpuSetCpus().length == getHostConfiguredCpuCount0();162}163return false;164}165166public native long getCommittedVirtualMemorySize();167private native long getTotalSwapSpaceSize0();168private native long getFreeSwapSpaceSize0();169public native long getProcessCpuTime();170private native long getFreePhysicalMemorySize0();171private native long getTotalPhysicalMemorySize0();172public native long getOpenFileDescriptorCount();173public native long getMaxFileDescriptorCount();174private native double getSystemCpuLoad0();175public native double getProcessCpuLoad();176private native double getSingleCpuLoad0(int cpuNum);177private native int getHostConfiguredCpuCount0();178179static {180initialize();181}182private static native void initialize();183}184185186