Path: blob/master/src/java.base/share/classes/jdk/internal/platform/Metrics.java
41159 views
/*1* Copyright (c) 2018, 2021, 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*/24package jdk.internal.platform;2526import java.lang.reflect.Method;2728/**29* Operating System Metrics class30*31* @implNote Some of the APIs within this class return metrics for an32* "Isolation Group" or "Container". When the term "Isolation Group"33* is used in the API description, this refers to either:34*35*<ol>36*<li> All processes, including the current process within a container.37*38*<li> All processes, including the current process running together39* isolated from other non-isolated processes.40*41*<li> All processes running on a host when that there is no isolation42* in effect.43*</ol>44*45* @author bobv46* @since 1147*/4849public interface Metrics {5051/**52* Returns an instance of the Metrics class.53*54* @return Metrics object or null if not supported on this platform.55*/56public static Metrics systemMetrics() {57return SystemMetrics.instance();58}5960/**61* Returns the interface responsible for providing the62* platform metrics.63*64* @implNote65* Metrics are currently only supported Linux.66* The provider for Linux is cgroups (version 1 or 2).67*68* @return The name of the provider.69*70*/71public String getProvider();727374/*****************************************************************75* CPU Accounting Subsystem76****************************************************************/7778/**79* Returns the aggregate time, in nanoseconds, consumed by all80* tasks in the Isolation Group.81*82* @return Time in nanoseconds, -1 if unknown or83* -2 if the metric is not supported.84*85*/86public long getCpuUsage();8788/**89* Returns the aggregate time, in nanoseconds, consumed by all tasks in90* the Isolation Group, separated by CPU. If the current process91* is running within a container, the reported time will only be92* valid for processes running within the same container. The values93* are returned in an array, one entry for each physical processor94* on the system. Time values for processors unavailable to this95* Group are undefined.96*97* @return long array of time values. The size of the array is equal98* to the total number of physical processors in the system. If99* this metric is not supported or not available, null will be100* returned.101*102*/103public long[] getPerCpuUsage();104105/**106* Returns the aggregate user time, in nanoseconds, consumed by all107* tasks in the Isolation Group.108*109* @return User time in nanoseconds, -1 if the metric is not available or110* -2 if the metric is not supported.111*112*/113public long getCpuUserUsage();114115/**116* Returns the aggregate system time, in nanoseconds, consumed by117* all tasks in the Isolation Group.118*119* @return System time in nanoseconds, -1 if the metric is not available or120* -2 if the metric is not supported.121*122*/123public long getCpuSystemUsage();124125/*****************************************************************126* CPU Scheduling Metrics127****************************************************************/128129/**130* Returns the length of the scheduling period, in131* microseconds, for processes within the Isolation Group.132*133* @return time in microseconds, -1 if the metric is not available or134* -2 if the metric is not supported.135*136*/137public long getCpuPeriod();138139/**140* Returns the total available run-time allowed, in microseconds,141* during each scheduling period for all tasks in the Isolation142* Group.143*144* @return time in microseconds, -1 if the quota is unlimited or145* -2 if not supported.146*147*/148public long getCpuQuota();149150151/**152* Returns the relative weighting of processes with the Isolation153* Group used for prioritizing the scheduling of processes across154* all Isolation Groups running on a host.155*156* @implNote157* Popular container orchestration systems have standardized shares158* to be multiples of 1024, where 1024 is interpreted as 1 CPU share159* of execution. Users can distribute CPU resources to multiple160* Isolation Groups by specifying the CPU share weighting needed by161* each process. To request 2 CPUS worth of execution time, CPU shares162* would be set to 2048.163*164* @return shares value, -1 if the metric is not available or165* -2 if cpu shares are not supported.166*167*/168public long getCpuShares();169170/**171* Returns the number of time-slice periods that have elapsed if172* a CPU quota has been setup for the Isolation Group173*174* @return count of elapsed periods, -1 if the metric is not available175* or -2 if the metric is not supported.176*177*/178public long getCpuNumPeriods();179180/**181* Returns the number of time-slice periods that the group has182* been throttled or limited due to the group exceeding its quota183* if a CPU quota has been setup for the Isolation Group.184*185* @return count of throttled periods, -1 if the metric is not available or186* -2 if it is not supported.187*188*/189public long getCpuNumThrottled();190191/**192* Returns the total time duration, in nanoseconds, that the193* group has been throttled or limited due to the group exceeding194* its quota if a CPU quota has been setup for the Isolation Group.195*196* @return Throttled time in nanoseconds, -1 if the metric is not available197* or -2 if it is not supported.198*199*/200public long getCpuThrottledTime();201202203/**204* Returns the number of effective processors that this Isolation205* group has available to it. This effective processor count is206* computed based on the number of dedicated CPUs, CPU shares and207* CPU quotas in effect for this isolation group.208*209* This method returns the same value as210* {@link java.lang.Runtime#availableProcessors()}.211*212* @return The number of effective CPUs.213*214*/215public long getEffectiveCpuCount();216217/*****************************************************************218* CPU Sets219****************************************************************/220221/**222* Returns the CPUS that are available for execution of processes223* in the current Isolation Group. The size of the array is equal224* to the total number of CPUs and the elements in the array are the225* physical CPU numbers that are available. Some of the CPUs returned226* may be offline. To get the current online CPUs, use227* {@link getEffectiveCpuSetCpus()}.228*229* @return An array of available CPUs. Returns null if the metric is not230* available or the metric is not supported.231*232*/233public int[] getCpuSetCpus();234235/**236* Returns the CPUS that are available and online for execution of237* processes within the current Isolation Group. The size of the238* array is equal to the total number of CPUs and the elements in239* the array are the physical CPU numbers.240*241* @return An array of available and online CPUs. Returns null242* if the metric is not available or the metric is not supported.243*244*/245public int[] getEffectiveCpuSetCpus();246247/**248* Returns the memory nodes that are available for use by processes249* in the current Isolation Group. The size of the array is equal250* to the total number of nodes and the elements in the array are the251* physical node numbers that are available. Some of the nodes returned252* may be offline. To get the current online memory nodes, use253* {@link getEffectiveCpuSetMems()}.254*255* @return An array of available memory nodes or null256* if the metric is not available or is not supported.257*258*/259public int[] getCpuSetMems();260261/**262* Returns the memory nodes that are available and online for use by263* processes within the current Isolation Group. The size of the264* array is equal to the total number of nodes and the elements in265* the array are the physical node numbers.266*267* @return An array of available and online nodes or null268* if the metric is not available or is not supported.269*270*/271public int[] getEffectiveCpuSetMems();272273/*****************************************************************274* Memory Subsystem275****************************************************************/276277/**278* Returns the number of times that user memory requests in the279* Isolation Group have exceeded the memory limit.280*281* @return The number of exceeded requests or -1 if the metric282* is not available. Returns -2 if the metric is not283* supported.284*285*/286public long getMemoryFailCount();287288/**289* Returns the maximum amount of physical memory, in bytes, that290* can be allocated in the Isolation Group.291*292* @return The maximum amount of memory in bytes or -1 if293* there is no limit or -2 if this metric is not supported.294*295*/296public long getMemoryLimit();297298/**299* Returns the amount of physical memory, in bytes, that is currently300* allocated in the current Isolation Group.301*302* @return The amount of memory in bytes allocated or -1 if303* the metric is not available or -2 if the metric is not304* supported.305*306*/307public long getMemoryUsage();308309/**310* Returns the amount of networking physical memory, in bytes, that311* is currently allocated in the current Isolation Group.312*313* @return The amount of memory in bytes allocated or -1 if the metric314* is not available. Returns -2 if this metric is not supported.315*316*/317public long getTcpMemoryUsage();318319/**320* Returns the maximum amount of physical memory and swap space,321* in bytes, that can be allocated in the Isolation Group.322*323* @return The maximum amount of memory in bytes or -1 if324* there is no limit set or -2 if this metric is not supported.325*326*/327public long getMemoryAndSwapLimit();328329/**330* Returns the amount of physical memory and swap space, in bytes,331* that is currently allocated in the current Isolation Group.332*333* @return The amount of memory in bytes allocated or -1 if334* the metric is not available. Returns -2 if this metric is not335* supported.336*337*/338public long getMemoryAndSwapUsage();339340/**341* Returns the hint to the operating system that allows groups342* to specify the minimum amount of physical memory that they need to343* achieve reasonable performance in low memory systems. This allows344* host systems to provide greater sharing of memory.345*346* @return The minimum amount of physical memory, in bytes, that the347* operating system will try to maintain under low memory348* conditions. If this metric is not available, -1 will be349* returned. Returns -2 if the metric is not supported.350*351*/352public long getMemorySoftLimit();353354/*****************************************************************355* BlKIO Subsystem356****************************************************************/357358/**359* Returns the number of block I/O requests to the disk that have been360* issued by the Isolation Group.361*362* @return The count of requests or -1 if the metric is not available.363* Returns -2 if this metric is not supported.364*365*/366public long getBlkIOServiceCount();367368/**369* Returns the number of block I/O bytes that have been transferred370* to/from the disk by the Isolation Group.371*372* @return The number of bytes transferred or -1 if the metric is not373* available. Returns -2 if this metric is not supported.374*375*/376public long getBlkIOServiced();377}378379380