Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/MemoryPoolImpl.java
38827 views
/*1* Copyright (c) 2003, 2017, 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 java.lang.management.ManagementFactory;28import java.lang.management.MemoryPoolMXBean;29import java.lang.management.MemoryUsage;30import java.lang.management.MemoryType;31import java.lang.management.MemoryManagerMXBean;32import javax.management.openmbean.CompositeData;33import javax.management.ObjectName;3435import static java.lang.management.MemoryNotificationInfo.*;3637/**38* Implementation class for a memory pool.39* Standard and committed hotspot-specific metrics if any.40*41* ManagementFactory.getMemoryPoolMXBeans() returns a list of42* instances of this class.43*/44class MemoryPoolImpl implements MemoryPoolMXBean {4546private final String name;47private final boolean isHeap;48private final boolean isValid;49private final boolean collectionThresholdSupported;50private final boolean usageThresholdSupported;5152private MemoryManagerMXBean[] managers;5354private long usageThreshold;55private long collectionThreshold;5657private boolean usageSensorRegistered;58private boolean gcSensorRegistered;59private Sensor usageSensor;60private Sensor gcSensor;6162MemoryPoolImpl(String name, boolean isHeap, long usageThreshold,63long gcThreshold) {64this.name = name;65this.isHeap = isHeap;66this.isValid = true;67this.managers = null;68this.usageThreshold = usageThreshold;69this.collectionThreshold = gcThreshold;70this.usageThresholdSupported = (usageThreshold >= 0);71this.collectionThresholdSupported = (gcThreshold >= 0);72this.usageSensor = new PoolSensor(this, name + " usage sensor");73this.gcSensor = new CollectionSensor(this, name + " collection sensor");74this.usageSensorRegistered = false;75this.gcSensorRegistered = false;76}7778public String getName() {79return name;80}8182public boolean isValid() {83return isValid;84}8586public MemoryType getType() {87if (isHeap) {88return MemoryType.HEAP;89} else {90return MemoryType.NON_HEAP;91}92}9394public MemoryUsage getUsage() {95return getUsage0();96}9798public synchronized MemoryUsage getPeakUsage() {99// synchronized since resetPeakUsage may be resetting the peak usage100return getPeakUsage0();101}102103public synchronized long getUsageThreshold() {104if (!isUsageThresholdSupported()) {105throw new UnsupportedOperationException(106"Usage threshold is not supported");107}108return usageThreshold;109}110111public void setUsageThreshold(long newThreshold) {112if (!isUsageThresholdSupported()) {113throw new UnsupportedOperationException(114"Usage threshold is not supported");115}116117Util.checkControlAccess();118119MemoryUsage usage = getUsage0();120if (newThreshold < 0) {121throw new IllegalArgumentException(122"Invalid threshold: " + newThreshold);123}124125if (usage.getMax() != -1 && newThreshold > usage.getMax()) {126throw new IllegalArgumentException(127"Invalid threshold: " + newThreshold +128" must be <= maxSize." +129" Committed = " + usage.getCommitted() +130" Max = " + usage.getMax());131}132133synchronized (this) {134if (!usageSensorRegistered) {135// pass the sensor to VM to begin monitoring136usageSensorRegistered = true;137setPoolUsageSensor(usageSensor);138}139setUsageThreshold0(usageThreshold, newThreshold);140this.usageThreshold = newThreshold;141}142}143144private synchronized MemoryManagerMXBean[] getMemoryManagers() {145if (managers == null) {146managers = getMemoryManagers0();147}148return managers;149}150151public String[] getMemoryManagerNames() {152MemoryManagerMXBean[] mgrs = getMemoryManagers();153154String[] names = new String[mgrs.length];155for (int i = 0; i < mgrs.length; i++) {156names[i] = mgrs[i].getName();157}158return names;159}160161public void resetPeakUsage() {162Util.checkControlAccess();163164synchronized (this) {165// synchronized since getPeakUsage may be called concurrently166resetPeakUsage0();167}168}169170public boolean isUsageThresholdExceeded() {171if (!isUsageThresholdSupported()) {172throw new UnsupportedOperationException(173"Usage threshold is not supported");174}175176// return false if usage threshold crossing checking is disabled177if (usageThreshold == 0) {178return false;179}180181MemoryUsage u = getUsage0();182return (u.getUsed() >= usageThreshold ||183usageSensor.isOn());184}185186public long getUsageThresholdCount() {187if (!isUsageThresholdSupported()) {188throw new UnsupportedOperationException(189"Usage threshold is not supported");190}191192return usageSensor.getCount();193}194195public boolean isUsageThresholdSupported() {196return usageThresholdSupported;197}198199public synchronized long getCollectionUsageThreshold() {200if (!isCollectionUsageThresholdSupported()) {201throw new UnsupportedOperationException(202"CollectionUsage threshold is not supported");203}204205return collectionThreshold;206}207208public void setCollectionUsageThreshold(long newThreshold) {209if (!isCollectionUsageThresholdSupported()) {210throw new UnsupportedOperationException(211"CollectionUsage threshold is not supported");212}213214Util.checkControlAccess();215216MemoryUsage usage = getUsage0();217if (newThreshold < 0) {218throw new IllegalArgumentException(219"Invalid threshold: " + newThreshold);220}221222if (usage.getMax() != -1 && newThreshold > usage.getMax()) {223throw new IllegalArgumentException(224"Invalid threshold: " + newThreshold +225" > max (" + usage.getMax() + ").");226}227228synchronized (this) {229if (!gcSensorRegistered) {230// pass the sensor to VM to begin monitoring231gcSensorRegistered = true;232setPoolCollectionSensor(gcSensor);233}234setCollectionThreshold0(collectionThreshold, newThreshold);235this.collectionThreshold = newThreshold;236}237}238239public boolean isCollectionUsageThresholdExceeded() {240if (!isCollectionUsageThresholdSupported()) {241throw new UnsupportedOperationException(242"CollectionUsage threshold is not supported");243}244245// return false if usage threshold crossing checking is disabled246if (collectionThreshold == 0) {247return false;248}249250MemoryUsage u = getCollectionUsage0();251return (gcSensor.isOn() ||252(u != null && u.getUsed() >= collectionThreshold));253}254255public long getCollectionUsageThresholdCount() {256if (!isCollectionUsageThresholdSupported()) {257throw new UnsupportedOperationException(258"CollectionUsage threshold is not supported");259}260261return gcSensor.getCount();262}263264public MemoryUsage getCollectionUsage() {265return getCollectionUsage0();266}267268public boolean isCollectionUsageThresholdSupported() {269return collectionThresholdSupported;270}271272// Native VM support273private native MemoryUsage getUsage0();274private native MemoryUsage getPeakUsage0();275private native MemoryUsage getCollectionUsage0();276private native void setUsageThreshold0(long current, long newThreshold);277private native void setCollectionThreshold0(long current, long newThreshold);278private native void resetPeakUsage0();279private native MemoryManagerMXBean[] getMemoryManagers0();280private native void setPoolUsageSensor(Sensor s);281private native void setPoolCollectionSensor(Sensor s);282283// package private284285/**286* PoolSensor will be triggered by the VM when the memory287* usage of a memory pool is crossing the usage threshold.288* The VM will not trigger this sensor in subsequent crossing289* unless the memory usage has returned below the threshold.290*/291class PoolSensor extends Sensor {292MemoryPoolImpl pool;293294PoolSensor(MemoryPoolImpl pool, String name) {295super(name);296this.pool = pool;297}298void triggerAction(MemoryUsage usage) {299// create and send notification300MemoryImpl.createNotification(MEMORY_THRESHOLD_EXCEEDED,301pool.getName(),302usage,303getCount());304}305void triggerAction() {306// do nothing307}308void clearAction() {309// do nothing310}311}312313/**314* CollectionSensor will be triggered and cleared by the VM315* when the memory usage of a memory pool after GC is crossing316* the collection threshold.317* The VM will trigger this sensor in subsequent crossing318* regardless if the memory usage has changed siince the previous GC.319*/320class CollectionSensor extends Sensor {321MemoryPoolImpl pool;322CollectionSensor(MemoryPoolImpl pool, String name) {323super(name);324this.pool = pool;325}326void triggerAction(MemoryUsage usage) {327MemoryImpl.createNotification(MEMORY_COLLECTION_THRESHOLD_EXCEEDED,328pool.getName(),329usage,330gcSensor.getCount());331}332void triggerAction() {333// do nothing334}335void clearAction() {336// do nothing337}338}339340public ObjectName getObjectName() {341return Util.newObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE, getName());342}343344}345346347