1/*
2 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.management;
27
28import java.lang.management.ManagementFactory;
29import java.lang.management.MemoryMXBean;
30import java.lang.management.MemoryUsage;
31import java.lang.management.MemoryNotificationInfo;
32import java.lang.management.MemoryManagerMXBean;
33import java.lang.management.MemoryPoolMXBean;
34import javax.management.ObjectName;
35import javax.management.MBeanNotificationInfo;
36import javax.management.Notification;
37import javax.management.openmbean.CompositeData;
38
39/**
40 * Implementation class for the memory subsystem.
41 * Standard and committed hotspot-specific metrics if any.
42 *
43 * ManagementFactory.getMemoryMXBean() returns an instance
44 * of this class.
45 */
46class MemoryImpl extends NotificationEmitterSupport
47                 implements MemoryMXBean {
48
49    private final VMManagement jvm;
50
51    private static MemoryPoolMXBean[] pools = null;
52    private static MemoryManagerMXBean[] mgrs = null;
53
54    /**
55     * Constructor of MemoryImpl class
56     */
57    MemoryImpl(VMManagement vm) {
58        this.jvm = vm;
59    }
60
61    public int getObjectPendingFinalizationCount() {
62        return jdk.internal.misc.VM.getFinalRefCount();
63    }
64
65    public void gc() {
66        Runtime.getRuntime().gc();
67    }
68
69    // Need to make a VM call to get coherent value
70    public MemoryUsage getHeapMemoryUsage() {
71        return getMemoryUsage0(true);
72    }
73
74    public MemoryUsage getNonHeapMemoryUsage() {
75        return getMemoryUsage0(false);
76    }
77
78    public boolean isVerbose() {
79        return jvm.getVerboseGC();
80    }
81
82    public void setVerbose(boolean value) {
83        Util.checkControlAccess();
84
85        setVerboseGC(value);
86    }
87
88    // The current Hotspot implementation does not support
89    // dynamically add or remove memory pools & managers.
90    static synchronized MemoryPoolMXBean[] getMemoryPools() {
91        if (pools == null) {
92            pools = getMemoryPools0();
93        }
94        return pools;
95    }
96    static synchronized MemoryManagerMXBean[] getMemoryManagers() {
97        if (mgrs == null) {
98            mgrs = getMemoryManagers0();
99        }
100        return mgrs;
101    }
102    private static native MemoryPoolMXBean[] getMemoryPools0();
103    private static native MemoryManagerMXBean[] getMemoryManagers0();
104    private native MemoryUsage getMemoryUsage0(boolean heap);
105    private native void setVerboseGC(boolean value);
106
107    private final static String notifName =
108        "javax.management.Notification";
109    private final static String[] notifTypes = {
110        MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
111        MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED
112    };
113    private final static String[] notifMsgs  = {
114        "Memory usage exceeds usage threshold",
115        "Memory usage exceeds collection usage threshold"
116    };
117
118    public MBeanNotificationInfo[] getNotificationInfo() {
119        return new MBeanNotificationInfo[] {
120            new MBeanNotificationInfo(notifTypes, notifName, "Memory Notification")
121        };
122    }
123
124    private static String getNotifMsg(String notifType) {
125        for (int i = 0; i < notifTypes.length; i++) {
126            if (notifType == notifTypes[i]) {
127                return notifMsgs[i];
128            }
129        }
130        return "Unknown message";
131    }
132
133    private static long seqNumber = 0;
134    private static long getNextSeqNumber() {
135        return ++seqNumber;
136    }
137
138    static void createNotification(String notifType,
139                                   String poolName,
140                                   MemoryUsage usage,
141                                   long count) {
142        MemoryImpl mbean = (MemoryImpl) ManagementFactory.getMemoryMXBean();
143        if (!mbean.hasListeners()) {
144            // if no listener is registered.
145            return;
146        }
147        long timestamp = System.currentTimeMillis();
148        String msg = getNotifMsg(notifType);
149        Notification notif = new Notification(notifType,
150                                              mbean.getObjectName(),
151                                              getNextSeqNumber(),
152                                              timestamp,
153                                              msg);
154        MemoryNotificationInfo info =
155            new MemoryNotificationInfo(poolName,
156                                       usage,
157                                       count);
158        CompositeData cd =
159            MemoryNotifInfoCompositeData.toCompositeData(info);
160        notif.setUserData(cd);
161        mbean.sendNotification(notif);
162    }
163
164    public ObjectName getObjectName() {
165        return Util.newObjectName(ManagementFactory.MEMORY_MXBEAN_NAME);
166    }
167
168}
169