001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.lang.management;
019:
020: import java.lang.management.ManagementPermission;
021: import java.lang.management.MemoryManagerMXBean;
022: import java.lang.management.MemoryPoolMXBean;
023: import java.lang.management.MemoryType;
024: import java.lang.management.MemoryUsage;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.List;
028:
029: /**
030: * Runtime type for {@link java.lang.management.MemoryPoolMXBean}
031: *
032: * @since 1.5
033: */
034: public final class MemoryPoolMXBeanImpl extends DynamicMXBeanImpl
035: implements MemoryPoolMXBean {
036: private final String name;
037:
038: /**
039: * IMPORTANT: bean identifier for use by VM
040: */
041: @SuppressWarnings("unused")
042: private final int id;
043:
044: private final MemoryType type;
045:
046: private final MemoryMXBeanImpl memBean;
047:
048: /**
049: * Sets the metadata for this bean.
050: *
051: * @param name
052: * @param type
053: * @param id
054: * @param memBean
055: */
056: MemoryPoolMXBeanImpl(String name, MemoryType type, int id,
057: MemoryMXBeanImpl memBean) {
058: this .name = name;
059: this .type = type;
060: this .id = id;
061: this .memBean = memBean;
062: setMBeanInfo(ManagementUtils
063: .getMBeanInfo(java.lang.management.MemoryPoolMXBean.class
064: .getName()));
065: if (isUsageThresholdSupported()
066: || isCollectionUsageThresholdSupported()) {
067: MemoryNotificationThread t = new MemoryNotificationThread(
068: memBean, this , id);
069: t.setDaemon(true);
070: t.setName("MemoryPoolMXBean notification dispatcher");
071: t.setPriority(Thread.NORM_PRIORITY + 1);
072: t.start();
073: }
074: }
075:
076: /**
077: * @return a {@link MemoryUsage}object that may be interrogated by the
078: * caller to determine the details of the memory usage. Returns
079: * <code>null</code> if the virtual machine does not support this
080: * method.
081: * @see #getCollectionUsage()
082: */
083: private native MemoryUsage getCollectionUsageImpl();
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see java.lang.management.MemoryPoolMXBean#getCollectionUsage()
089: */
090: public MemoryUsage getCollectionUsage() {
091: return this .getCollectionUsageImpl();
092: }
093:
094: /**
095: * @return the collection usage threshold in bytes. The default value as set
096: * by the virtual machine will be zero.
097: * @see #getCollectionUsageThreshold()
098: */
099: private native long getCollectionUsageThresholdImpl();
100:
101: /*
102: * (non-Javadoc)
103: *
104: * @see java.lang.management.MemoryPoolMXBean#getCollectionUsageThreshold()
105: */
106: public long getCollectionUsageThreshold() {
107: if (!isCollectionUsageThresholdSupported()) {
108: throw new UnsupportedOperationException(
109: "VM does not support collection usage threshold.");
110: }
111: return this .getCollectionUsageThresholdImpl();
112: }
113:
114: /**
115: * @return a count of the number of times that the collection usage
116: * threshold has been surpassed.
117: * @see #getCollectionUsageThresholdCount()
118: */
119: private native long getCollectionUsageThresholdCountImpl();
120:
121: /*
122: * (non-Javadoc)
123: *
124: * @see java.lang.management.MemoryPoolMXBean#getCollectionUsageThresholdCount()
125: */
126: public long getCollectionUsageThresholdCount() {
127: if (!isCollectionUsageThresholdSupported()) {
128: throw new UnsupportedOperationException(
129: "VM does not support collection usage threshold.");
130: }
131: return this .getCollectionUsageThresholdCountImpl();
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see java.lang.management.MemoryPoolMXBean#getMemoryManagerNames()
138: */
139: public String[] getMemoryManagerNames() {
140: /* get the memory managers and check which of them manage this pool */
141: Iterator<MemoryManagerMXBean> iter = memBean
142: .getMemoryManagerMXBeans().iterator();
143: List<String> result = new LinkedList<String>();
144: while (iter.hasNext()) {
145: MemoryManagerMXBean bean = iter.next();
146: String[] managedPools = bean.getMemoryPoolNames();
147: for (int i = 0; i < managedPools.length; i++) {
148: if (managedPools[i].equals(name)) {
149: result.add(bean.getName());
150: break;
151: }
152: }
153: }
154: return result.toArray(new String[0]);
155: }
156:
157: /*
158: * (non-Javadoc)
159: *
160: * @see java.lang.management.MemoryPoolMXBean#getName()
161: */
162: public String getName() {
163: return this .name;
164: }
165:
166: /**
167: * @return a {@link MemoryUsage}which can be interrogated by the caller to
168: * determine details of the peak memory usage. A <code>null</code>
169: * value will be returned if the memory pool no longer exists (and
170: * the pool is therefore considered to be invalid).
171: * @see #getPeakUsage()
172: */
173: private native MemoryUsage getPeakUsageImpl();
174:
175: /*
176: * (non-Javadoc)
177: *
178: * @see java.lang.management.MemoryPoolMXBean#getPeakUsage()
179: */
180: public MemoryUsage getPeakUsage() {
181: return this .getPeakUsageImpl();
182: }
183:
184: /*
185: * (non-Javadoc)
186: *
187: * @see java.lang.management.MemoryPoolMXBean#getType()
188: */
189: public MemoryType getType() {
190: return this .type;
191: }
192:
193: /**
194: * @return an instance of {@link MemoryUsage}that can be interrogated by
195: * the caller to determine details on the pool's current memory
196: * usage. A <code>null</code> value will be returned if the memory
197: * pool no longer exists (in which case it is considered to be
198: * invalid).
199: * @see #getUsage()
200: */
201: private native MemoryUsage getUsageImpl();
202:
203: /*
204: * (non-Javadoc)
205: *
206: * @see java.lang.management.MemoryPoolMXBean#getUsage()
207: */
208: public MemoryUsage getUsage() {
209: return this .getUsageImpl();
210: }
211:
212: /**
213: * @return the usage threshold in bytes. The default value as set by the
214: * virtual machine depends on the platform the virtual machine is
215: * running on. will be zero.
216: * @see #getUsageThreshold()
217: */
218: private native long getUsageThresholdImpl();
219:
220: /*
221: * (non-Javadoc)
222: *
223: * @see java.lang.management.MemoryPoolMXBean#getUsageThreshold()
224: */
225: public long getUsageThreshold() {
226: if (!isUsageThresholdSupported()) {
227: throw new UnsupportedOperationException(
228: "VM does not support usage threshold.");
229: }
230: return this .getUsageThresholdImpl();
231: }
232:
233: /**
234: * @return a count of the number of times that the usage threshold has been
235: * surpassed.
236: * @see #getUsageThresholdCount()
237: */
238: private native long getUsageThresholdCountImpl();
239:
240: /*
241: * (non-Javadoc)
242: *
243: * @see java.lang.management.MemoryPoolMXBean#getUsageThresholdCount()
244: */
245: public long getUsageThresholdCount() {
246: if (!isUsageThresholdSupported()) {
247: throw new UnsupportedOperationException(
248: "VM does not support usage threshold.");
249: }
250: return this .getUsageThresholdCountImpl();
251: }
252:
253: /**
254: * @return <code>true</code> if the collection usage threshold was
255: * surpassed after the latest garbage collection run, otherwise
256: * <code>false</code>.
257: * @see #isCollectionUsageThresholdExceeded()
258: */
259: private native boolean isCollectionUsageThresholdExceededImpl();
260:
261: /*
262: * (non-Javadoc)
263: *
264: * @see java.lang.management.MemoryPoolMXBean#isCollectionUsageThresholdExceeded()
265: */
266: public boolean isCollectionUsageThresholdExceeded() {
267: if (!isCollectionUsageThresholdSupported()) {
268: throw new UnsupportedOperationException(
269: "VM does not support collection usage threshold.");
270: }
271: return this .isCollectionUsageThresholdExceededImpl();
272: }
273:
274: /**
275: * @return <code>true</code> if supported, <code>false</code> otherwise.
276: * @see #isCollectionUsageThresholdSupported()
277: */
278: private native boolean isCollectionUsageThresholdSupportedImpl();
279:
280: /*
281: * (non-Javadoc)
282: *
283: * @see java.lang.management.MemoryPoolMXBean#isCollectionUsageThresholdSupported()
284: */
285: public boolean isCollectionUsageThresholdSupported() {
286: return this .isCollectionUsageThresholdSupportedImpl();
287: }
288:
289: /**
290: * @return <code>true</code> if the usage threshold has been surpassed,
291: * otherwise <code>false</code>.
292: * @see #isUsageThresholdExceeded()
293: */
294: private native boolean isUsageThresholdExceededImpl();
295:
296: /*
297: * (non-Javadoc)
298: *
299: * @see java.lang.management.MemoryPoolMXBean#isUsageThresholdExceeded()
300: */
301: public boolean isUsageThresholdExceeded() {
302: if (!isUsageThresholdSupported()) {
303: throw new UnsupportedOperationException(
304: "VM does not support usage threshold.");
305: }
306: return this .isUsageThresholdExceededImpl();
307: }
308:
309: /**
310: * @return <code>true</code> if supported, <code>false</code> otherwise.
311: * @see #isUsageThresholdSupported()
312: */
313: private native boolean isUsageThresholdSupportedImpl();
314:
315: /*
316: * (non-Javadoc)
317: *
318: * @see java.lang.management.MemoryPoolMXBean#isUsageThresholdSupported()
319: */
320: public boolean isUsageThresholdSupported() {
321: return this .isUsageThresholdSupportedImpl();
322: }
323:
324: /**
325: * @return <code>true</code> if the memory pool has not been removed by
326: * the virtual machine, <code>false</code> otherwise.
327: * @see #isValid()
328: */
329: private native boolean isValidImpl();
330:
331: /*
332: * (non-Javadoc)
333: *
334: * @see java.lang.management.MemoryPoolMXBean#isValid()
335: */
336: public boolean isValid() {
337: return this .isValidImpl();
338: }
339:
340: /**
341: * @see #resetPeakUsage()
342: */
343: private native void resetPeakUsageImpl();
344:
345: /*
346: * (non-Javadoc)
347: *
348: * @see java.lang.management.MemoryPoolMXBean#resetPeakUsage()
349: */
350: public void resetPeakUsage() {
351: SecurityManager security = System.getSecurityManager();
352: if (security != null) {
353: security
354: .checkPermission(new ManagementPermission("control"));
355: }
356: this .resetPeakUsageImpl();
357: }
358:
359: /**
360: * @param threshold
361: * the size of the new collection usage threshold expressed in
362: * bytes.
363: * @see #setCollectionUsageThreshold(long)
364: */
365: private native void setCollectionUsageThresholdImpl(long threshold);
366:
367: /*
368: * (non-Javadoc)
369: *
370: * @see java.lang.management.MemoryPoolMXBean#setCollectionUsageThreshold(long)
371: */
372: public void setCollectionUsageThreshold(long threshold) {
373: if (!isCollectionUsageThresholdSupported()) {
374: throw new UnsupportedOperationException(
375: "VM does not support collection usage threshold.");
376: }
377:
378: SecurityManager security = System.getSecurityManager();
379: if (security != null) {
380: security
381: .checkPermission(new ManagementPermission("control"));
382: }
383:
384: if (threshold < 0) {
385: throw new IllegalArgumentException(
386: "Collection usage threshold cannot be negative.");
387: }
388:
389: if (exceedsMaxPoolSize(threshold)) {
390: throw new IllegalArgumentException(
391: "Collection usage threshold cannot exceed maximum amount of memory for pool.");
392: }
393: this .setCollectionUsageThresholdImpl(threshold);
394: }
395:
396: /**
397: * @param threshold
398: * the size of the new usage threshold expressed in bytes.
399: * @see #setUsageThreshold(long)
400: */
401: private native void setUsageThresholdImpl(long threshold);
402:
403: /*
404: * (non-Javadoc)
405: *
406: * @see java.lang.management.MemoryPoolMXBean#setUsageThreshold(long)
407: */
408: public void setUsageThreshold(long threshold) {
409: if (!isUsageThresholdSupported()) {
410: throw new UnsupportedOperationException(
411: "VM does not support usage threshold.");
412: }
413:
414: SecurityManager security = System.getSecurityManager();
415: if (security != null) {
416: security
417: .checkPermission(new ManagementPermission("control"));
418: }
419:
420: if (threshold < 0) {
421: throw new IllegalArgumentException(
422: "Usage threshold cannot be negative.");
423: }
424:
425: if (exceedsMaxPoolSize(threshold)) {
426: throw new IllegalArgumentException(
427: "Usage threshold cannot exceed maximum amount of memory for pool.");
428: }
429: this .setUsageThresholdImpl(threshold);
430: }
431:
432: /**
433: * @param value
434: * @return <code>true</code> if <code>value</code> is greater than the
435: * maximum size of the corresponding memory pool. <code>false</code>
436: * if <code>value</code> does not exceed the maximum memory pool
437: * size or else no memory pool maximum size has been defined.
438: */
439: private boolean exceedsMaxPoolSize(long value) {
440: MemoryUsage m = getUsage();
441: return (m.getMax() != -1 && m.getMax() < value);
442: }
443: }
|