1/*
2 * Copyright (c) 2003, 2017, 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 java.lang.management;
27import javax.management.openmbean.CompositeData;
28import sun.management.MemoryNotifInfoCompositeData;
29
30/**
31 * The information about a memory notification.
32 *
33 * <p>
34 * A memory notification is emitted by {@link MemoryMXBean}
35 * when the Java virtual machine detects that the memory usage
36 * of a memory pool is exceeding a threshold value.
37 * The notification emitted will contain the memory notification
38 * information about the detected condition:
39 * <ul>
40 *   <li>The name of the memory pool.</li>
41 *   <li>The memory usage of the memory pool when the notification
42 *       was constructed.</li>
43 *   <li>The number of times that the memory usage has crossed
44 *       a threshold when the notification was constructed.
45 *       For usage threshold notifications, this count will be the
46 *       {@link MemoryPoolMXBean#getUsageThresholdCount usage threshold
47 *       count}.  For collection threshold notifications,
48 *       this count will be the
49 *       {@link MemoryPoolMXBean#getCollectionUsageThresholdCount
50 *       collection usage threshold count}.
51 *       </li>
52 * </ul>
53 *
54 * <p>
55 * A {@link CompositeData CompositeData} representing
56 * the {@code MemoryNotificationInfo} object
57 * is stored in the
58 * {@link javax.management.Notification#setUserData user data}
59 * of a {@link javax.management.Notification notification}.
60 * The {@link #from from} method is provided to convert from
61 * a {@code CompositeData} to a {@code MemoryNotificationInfo}
62 * object. For example:
63 *
64 * <blockquote><pre>
65 *      Notification notif;
66 *
67 *      // receive the notification emitted by MemoryMXBean and set to notif
68 *      ...
69 *
70 *      String notifType = notif.getType();
71 *      if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
72 *          notifType.equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
73 *          // retrieve the memory notification information
74 *          CompositeData cd = (CompositeData) notif.getUserData();
75 *          MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
76 *          ....
77 *      }
78 * </pre></blockquote>
79 *
80 * <p>
81 * The types of notifications emitted by {@code MemoryMXBean} are:
82 * <ul>
83 *   <li>A {@link #MEMORY_THRESHOLD_EXCEEDED
84 *       usage threshold exceeded notification}.
85 *       <br>This notification will be emitted when
86 *       the memory usage of a memory pool is increased and has reached
87 *       or exceeded its
88 *       <a href="MemoryPoolMXBean.html#UsageThreshold"> usage threshold</a> value.
89 *       Subsequent crossing of the usage threshold value does not cause
90 *       further notification until the memory usage has returned
91 *       to become less than the usage threshold value.
92 *       </li>
93 *   <li>A {@link #MEMORY_COLLECTION_THRESHOLD_EXCEEDED
94 *       collection usage threshold exceeded notification}.
95 *       <br>This notification will be emitted when
96 *       the memory usage of a memory pool is greater than or equal to its
97 *       <a href="MemoryPoolMXBean.html#CollectionThreshold">
98 *       collection usage threshold</a> after the Java virtual machine
99 *       has expended effort in recycling unused objects in that
100 *       memory pool.</li>
101 * </ul>
102 *
103 * @author  Mandy Chung
104 * @since   1.5
105 *
106 */
107public class MemoryNotificationInfo {
108    private final String poolName;
109    private final MemoryUsage usage;
110    private final long count;
111
112    /**
113     * Notification type denoting that
114     * the memory usage of a memory pool has
115     * reached or exceeded its
116     * <a href="MemoryPoolMXBean.html#UsageThreshold"> usage threshold</a> value.
117     * This notification is emitted by {@link MemoryMXBean}.
118     * Subsequent crossing of the usage threshold value does not cause
119     * further notification until the memory usage has returned
120     * to become less than the usage threshold value.
121     * The value of this notification type is
122     * {@code java.management.memory.threshold.exceeded}.
123     */
124    public static final String MEMORY_THRESHOLD_EXCEEDED =
125        "java.management.memory.threshold.exceeded";
126
127    /**
128     * Notification type denoting that
129     * the memory usage of a memory pool is greater than or equal to its
130     * <a href="MemoryPoolMXBean.html#CollectionThreshold">
131     * collection usage threshold</a> after the Java virtual machine
132     * has expended effort in recycling unused objects in that
133     * memory pool.
134     * This notification is emitted by {@link MemoryMXBean}.
135     * The value of this notification type is
136     * {@code java.management.memory.collection.threshold.exceeded}.
137     */
138    public static final String MEMORY_COLLECTION_THRESHOLD_EXCEEDED =
139        "java.management.memory.collection.threshold.exceeded";
140
141    /**
142     * Constructs a {@code MemoryNotificationInfo} object.
143     *
144     * @param poolName The name of the memory pool which triggers this notification.
145     * @param usage Memory usage of the memory pool.
146     * @param count The threshold crossing count.
147     */
148    public MemoryNotificationInfo(String poolName,
149                                  MemoryUsage usage,
150                                  long count) {
151        if (poolName == null) {
152            throw new NullPointerException("Null poolName");
153        }
154        if (usage == null) {
155            throw new NullPointerException("Null usage");
156        }
157
158        this.poolName = poolName;
159        this.usage = usage;
160        this.count = count;
161    }
162
163    MemoryNotificationInfo(CompositeData cd) {
164        MemoryNotifInfoCompositeData.validateCompositeData(cd);
165
166        this.poolName = MemoryNotifInfoCompositeData.getPoolName(cd);
167        this.usage = MemoryNotifInfoCompositeData.getUsage(cd);
168        this.count = MemoryNotifInfoCompositeData.getCount(cd);
169    }
170
171    /**
172     * Returns the name of the memory pool that triggers this notification.
173     * The memory pool usage has crossed a threshold.
174     *
175     * @return the name of the memory pool that triggers this notification.
176     */
177    public String getPoolName() {
178        return poolName;
179    }
180
181    /**
182     * Returns the memory usage of the memory pool
183     * when this notification was constructed.
184     *
185     * @return the memory usage of the memory pool
186     * when this notification was constructed.
187     */
188    public MemoryUsage getUsage() {
189        return usage;
190    }
191
192    /**
193     * Returns the number of times that the memory usage has crossed
194     * a threshold when the notification was constructed.
195     * For usage threshold notifications, this count will be the
196     * {@link MemoryPoolMXBean#getUsageThresholdCount threshold
197     * count}.  For collection threshold notifications,
198     * this count will be the
199     * {@link MemoryPoolMXBean#getCollectionUsageThresholdCount
200     * collection usage threshold count}.
201     *
202     * @return the number of times that the memory usage has crossed
203     * a threshold when the notification was constructed.
204     */
205    public long getCount() {
206        return count;
207    }
208
209    /**
210     * Returns a {@code MemoryNotificationInfo} object represented by the
211     * given {@code CompositeData}.
212     * The given {@code CompositeData} must contain
213     * the following attributes:
214     * <table class="striped" style="margin-left:2em">
215     * <caption style="display:none">The attributes and the types the given CompositeData contains</caption>
216     * <thead>
217     * <tr>
218     *   <th scope="col">Attribute Name</th>
219     *   <th scope="col">Type</th>
220     * </tr>
221     * </thead>
222     * <tbody style="text-align:left">
223     * <tr>
224     *   <th scope="row">poolName</th>
225     *   <td>{@code java.lang.String}</td>
226     * </tr>
227     * <tr>
228     *   <th scope="row">usage</th>
229     *   <td>{@code javax.management.openmbean.CompositeData}</td>
230     * </tr>
231     * <tr>
232     *   <th scope="row">count</th>
233     *   <td>{@code java.lang.Long}</td>
234     * </tr>
235     * </tbody>
236     * </table>
237     *
238     * @param cd {@code CompositeData} representing a
239     *           {@code MemoryNotificationInfo}
240     *
241     * @throws IllegalArgumentException if {@code cd} does not
242     *   represent a {@code MemoryNotificationInfo} object.
243     *
244     * @return a {@code MemoryNotificationInfo} object represented
245     *         by {@code cd} if {@code cd} is not {@code null};
246     *         {@code null} otherwise.
247     */
248    public static MemoryNotificationInfo from(CompositeData cd) {
249        if (cd == null) {
250            return null;
251        }
252
253        if (cd instanceof MemoryNotifInfoCompositeData) {
254            return ((MemoryNotifInfoCompositeData) cd).getMemoryNotifInfo();
255        } else {
256            return new MemoryNotificationInfo(cd);
257        }
258    }
259}
260