src/java.management/share/classes/sun/management/MemoryPoolImpl.java
author lucy
Thu, 23 May 2019 18:50:48 +0200
changeset 55024 948385f851f2
parent 49558 b2bd13eafc1c
permissions -rw-r--r--
8224652: 32-bit build failures after JDK-8213084 Reviewed-by: thartmann, shade Contributed-by: shade@redhat.com
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
49558
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    58
    private boolean usageSensorRegistered; // VM-initialized to false
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    59
    private boolean gcSensorRegistered;    // VM-initialized to false
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    60
    private final Sensor usageSensor;
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    61
    private final Sensor gcSensor;
2
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return isValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public MemoryType getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (isHeap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return MemoryType.HEAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return MemoryType.NON_HEAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public MemoryUsage getUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public synchronized MemoryUsage getPeakUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // synchronized since resetPeakUsage may be resetting the peak usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return getPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public synchronized long getUsageThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return usageThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void setUsageThreshold(long newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   116
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        MemoryUsage usage = getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (newThreshold < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                "Invalid threshold: " + newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (usage.getMax() != -1 && newThreshold > usage.getMax()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                "Invalid threshold: " + newThreshold +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                " must be <= maxSize." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                " Committed = " + usage.getCommitted() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                " Max = " + usage.getMax());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (!usageSensorRegistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // pass the sensor to VM to begin monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                usageSensorRegistered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                setPoolUsageSensor(usageSensor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            setUsageThreshold0(usageThreshold, newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            this.usageThreshold = newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private synchronized MemoryManagerMXBean[] getMemoryManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (managers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            managers = getMemoryManagers0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return managers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public String[] getMemoryManagerNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        MemoryManagerMXBean[] mgrs = getMemoryManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String[] names = new String[mgrs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        for (int i = 0; i < mgrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            names[i] = mgrs[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void resetPeakUsage() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   161
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // synchronized since getPeakUsage may be called concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            resetPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public boolean isUsageThresholdExceeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // return false if usage threshold crossing checking is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (usageThreshold == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        MemoryUsage u = getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return (u.getUsed() >= usageThreshold ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                usageSensor.isOn());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public long getUsageThresholdCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (!isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                "Usage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return usageSensor.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public boolean isUsageThresholdSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return usageThresholdSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public synchronized long getCollectionUsageThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return collectionThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void setCollectionUsageThreshold(long newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   213
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        MemoryUsage usage = getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (newThreshold < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                "Invalid threshold: " + newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (usage.getMax() != -1 && newThreshold > usage.getMax()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                "Invalid threshold: " + newThreshold +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                     " > max (" + usage.getMax() + ").");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (!gcSensorRegistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                // pass the sensor to VM to begin monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                gcSensorRegistered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                setPoolCollectionSensor(gcSensor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            setCollectionThreshold0(collectionThreshold, newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.collectionThreshold = newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public boolean isCollectionUsageThresholdExceeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // return false if usage threshold crossing checking is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (collectionThreshold == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        MemoryUsage u = getCollectionUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return (gcSensor.isOn() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                (u != null && u.getUsed() >= collectionThreshold));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public long getCollectionUsageThresholdCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (!isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                "CollectionUsage threshold is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return gcSensor.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public MemoryUsage getCollectionUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return getCollectionUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public boolean isCollectionUsageThresholdSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return collectionThresholdSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // Native VM support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private native MemoryUsage getUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private native MemoryUsage getPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private native MemoryUsage getCollectionUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private native void setUsageThreshold0(long current, long newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private native void setCollectionThreshold0(long current, long newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private native void resetPeakUsage0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private native MemoryManagerMXBean[] getMemoryManagers0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private native void setPoolUsageSensor(Sensor s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private native void setPoolCollectionSensor(Sensor s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * PoolSensor will be triggered by the VM when the memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * usage of a memory pool is crossing the usage threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * The VM will not trigger this sensor in subsequent crossing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * unless the memory usage has returned below the threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    class PoolSensor extends Sensor {
49558
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
   291
        final MemoryPoolImpl pool;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        PoolSensor(MemoryPoolImpl pool, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            this.pool = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        void triggerAction(MemoryUsage usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // create and send notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            MemoryImpl.createNotification(MEMORY_THRESHOLD_EXCEEDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                          pool.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                          usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                          getCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        void triggerAction() {
45646
285619b3d129 8178536: OOM ERRORS + SERVICE-THREAD TAKES A PROCESSOR TO 100%
poonam
parents: 25859
diff changeset
   305
            // do nothing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        void clearAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * CollectionSensor will be triggered and cleared by the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * when the memory usage of a memory pool after GC is crossing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * the collection threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * The VM will trigger this sensor in subsequent crossing
49558
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
   317
     * regardless if the memory usage has changed since the previous GC.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    class CollectionSensor extends Sensor {
49558
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
   320
        final MemoryPoolImpl pool;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        CollectionSensor(MemoryPoolImpl pool, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            this.pool = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        void triggerAction(MemoryUsage usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            MemoryImpl.createNotification(MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                          pool.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                          usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                          gcSensor.getCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        void triggerAction() {
45646
285619b3d129 8178536: OOM ERRORS + SERVICE-THREAD TAKES A PROCESSOR TO 100%
poonam
parents: 25859
diff changeset
   332
            // do nothing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        void clearAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   338
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   339
    public ObjectName getObjectName() {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   340
        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
   341
    }
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   342
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}