src/java.management/share/classes/sun/management/MemoryPoolImpl.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45646 jdk/src/java.management/share/classes/sun/management/MemoryPoolImpl.java@285619b3d129
child 49558 b2bd13eafc1c
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45646
285619b3d129 8178536: OOM ERRORS + SERVICE-THREAD TAKES A PROCESSOR TO 100%
poonam
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    28
import java.lang.management.ManagementFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.management.MemoryPoolMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.management.MemoryUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.management.MemoryType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.management.MemoryManagerMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.openmbean.CompositeData;
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    34
import javax.management.ObjectName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static java.lang.management.MemoryNotificationInfo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Implementation class for a memory pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Standard and committed hotspot-specific metrics if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * ManagementFactory.getMemoryPoolMXBeans() returns a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class MemoryPoolImpl implements MemoryPoolMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final String  name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final boolean isHeap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final boolean isValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final boolean collectionThresholdSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final boolean usageThresholdSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private MemoryManagerMXBean[] managers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private long  usageThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private long  collectionThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean usageSensorRegistered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean gcSensorRegistered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Sensor  usageSensor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private Sensor  gcSensor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    MemoryPoolImpl(String name, boolean isHeap, long usageThreshold,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                   long gcThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.isHeap = isHeap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.isValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.managers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.usageThreshold = usageThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.collectionThreshold = gcThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.usageThresholdSupported = (usageThreshold >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.collectionThresholdSupported = (gcThreshold >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.usageSensor = new PoolSensor(this, name + " usage sensor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.gcSensor = new CollectionSensor(this, name + " collection sensor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.usageSensorRegistered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.gcSensorRegistered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return isValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public MemoryType getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (isHeap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return MemoryType.HEAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return MemoryType.NON_HEAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public MemoryUsage getUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public synchronized MemoryUsage getPeakUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // synchronized since resetPeakUsage may be resetting the peak usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return getPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public synchronized long getUsageThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return usageThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void setUsageThreshold(long newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   118
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        MemoryUsage usage = getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (newThreshold < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                "Invalid threshold: " + newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (usage.getMax() != -1 && newThreshold > usage.getMax()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                "Invalid threshold: " + newThreshold +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                " must be <= maxSize." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                " Committed = " + usage.getCommitted() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                " Max = " + usage.getMax());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (!usageSensorRegistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                // pass the sensor to VM to begin monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                usageSensorRegistered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                setPoolUsageSensor(usageSensor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            setUsageThreshold0(usageThreshold, newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            this.usageThreshold = newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private synchronized MemoryManagerMXBean[] getMemoryManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (managers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            managers = getMemoryManagers0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return managers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public String[] getMemoryManagerNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        MemoryManagerMXBean[] mgrs = getMemoryManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        String[] names = new String[mgrs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        for (int i = 0; i < mgrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            names[i] = mgrs[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void resetPeakUsage() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   163
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // synchronized since getPeakUsage may be called concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            resetPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public boolean isUsageThresholdExceeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // return false if usage threshold crossing checking is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (usageThreshold == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        MemoryUsage u = getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return (u.getUsed() >= usageThreshold ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                usageSensor.isOn());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public long getUsageThresholdCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return usageSensor.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public boolean isUsageThresholdSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return usageThresholdSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public synchronized long getCollectionUsageThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return collectionThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void setCollectionUsageThreshold(long newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   215
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        MemoryUsage usage = getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (newThreshold < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                "Invalid threshold: " + newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (usage.getMax() != -1 && newThreshold > usage.getMax()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                "Invalid threshold: " + newThreshold +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                     " > max (" + usage.getMax() + ").");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (!gcSensorRegistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // pass the sensor to VM to begin monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                gcSensorRegistered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                setPoolCollectionSensor(gcSensor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            setCollectionThreshold0(collectionThreshold, newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.collectionThreshold = newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public boolean isCollectionUsageThresholdExceeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // return false if usage threshold crossing checking is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (collectionThreshold == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        MemoryUsage u = getCollectionUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return (gcSensor.isOn() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                (u != null && u.getUsed() >= collectionThreshold));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public long getCollectionUsageThresholdCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return gcSensor.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public MemoryUsage getCollectionUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return getCollectionUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public boolean isCollectionUsageThresholdSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return collectionThresholdSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    // Native VM support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private native MemoryUsage getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private native MemoryUsage getPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private native MemoryUsage getCollectionUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private native void setUsageThreshold0(long current, long newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private native void setCollectionThreshold0(long current, long newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private native void resetPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private native MemoryManagerMXBean[] getMemoryManagers0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private native void setPoolUsageSensor(Sensor s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private native void setPoolCollectionSensor(Sensor s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * PoolSensor will be triggered by the VM when the memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * usage of a memory pool is crossing the usage threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * The VM will not trigger this sensor in subsequent crossing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * unless the memory usage has returned below the threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    class PoolSensor extends Sensor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        MemoryPoolImpl pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        PoolSensor(MemoryPoolImpl pool, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            this.pool = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        void triggerAction(MemoryUsage usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // create and send notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            MemoryImpl.createNotification(MEMORY_THRESHOLD_EXCEEDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                          pool.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                          usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                          getCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        void triggerAction() {
45646
285619b3d129 8178536: OOM ERRORS + SERVICE-THREAD TAKES A PROCESSOR TO 100%
poonam
parents: 25859
diff changeset
   307
            // do nothing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        void clearAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * CollectionSensor will be triggered and cleared by the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * when the memory usage of a memory pool after GC is crossing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the collection threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * The VM will trigger this sensor in subsequent crossing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * regardless if the memory usage has changed siince the previous GC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    class CollectionSensor extends Sensor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        MemoryPoolImpl pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        CollectionSensor(MemoryPoolImpl pool, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            this.pool = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        void triggerAction(MemoryUsage usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            MemoryImpl.createNotification(MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                          pool.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                          usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                          gcSensor.getCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        void triggerAction() {
45646
285619b3d129 8178536: OOM ERRORS + SERVICE-THREAD TAKES A PROCESSOR TO 100%
poonam
parents: 25859
diff changeset
   334
            // do nothing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        void clearAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   340
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   341
    public ObjectName getObjectName() {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   342
        return Util.newObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE, getName());
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   343
    }
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   344
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
}