01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.runtime;
05:
06: import com.tc.util.runtime.Vm;
07:
08: import java.lang.management.GarbageCollectorMXBean;
09: import java.lang.management.ManagementFactory;
10: import java.lang.management.MemoryPoolMXBean;
11: import java.lang.management.MemoryType;
12: import java.util.ArrayList;
13: import java.util.Arrays;
14: import java.util.HashMap;
15: import java.util.Iterator;
16: import java.util.List;
17:
18: class TCMemoryManagerJdk15PoolMonitor extends TCMemoryManagerJdk15Basic {
19:
20: private static final String OLD_GEN_NAME = "OLD GEN";
21: // this pool is used when jdk is run with -client option
22: private static final String TENURED_GEN_NAME = "TENURED GEN";
23: private static final String IBMJDK_TENURED_GEN_NAME = "Java heap";
24:
25: private final MemoryPoolMXBean oldGenBean;
26: private final GarbageCollectorMXBean oldGenCollectorBean;
27:
28: public TCMemoryManagerJdk15PoolMonitor() {
29: super ();
30: oldGenBean = getOldGenMemoryPoolBean();
31: oldGenCollectorBean = getOldGenCollectorBean();
32: }
33:
34: private MemoryPoolMXBean getOldGenMemoryPoolBean() {
35: List pools = ManagementFactory.getMemoryPoolMXBeans();
36: List<String> poolNames = new ArrayList<String>();
37: for (Iterator i = pools.iterator(); i.hasNext();) {
38: MemoryPoolMXBean mpBean = (MemoryPoolMXBean) i.next();
39: String name = mpBean.getName();
40: poolNames.add(name);
41: if (mpBean.getType() == MemoryType.HEAP && isOldGen(name)) {
42: // Got it
43: return mpBean;
44: }
45: }
46: throw new AssertionError(
47: "Old or Tenured Memory pool Not found : " + poolNames);
48: }
49:
50: private GarbageCollectorMXBean getOldGenCollectorBean() {
51: List gcs = ManagementFactory.getGarbageCollectorMXBeans();
52: HashMap<String, List<String>> gcs2Pools = new HashMap<String, List<String>>();
53: for (Iterator i = gcs.iterator(); i.hasNext();) {
54: GarbageCollectorMXBean gc = (GarbageCollectorMXBean) i
55: .next();
56: String[] managedPools = gc.getMemoryPoolNames();
57: if (gc.isValid() && managedPools != null) {
58: for (int j = 0; j < managedPools.length; j++) {
59: if (isOldGen(managedPools[j])) {
60: return gc;
61: }
62: }
63: gcs2Pools
64: .put(gc.getName(), Arrays.asList(managedPools));
65: }
66: }
67: throw new AssertionError(
68: "Old or Tenured Memory pool does not have a garbage collector : "
69: + gcs2Pools);
70: }
71:
72: private boolean isOldGen(String name) {
73: if (Vm.isIBM()) {
74: return (name.indexOf(IBMJDK_TENURED_GEN_NAME) > -1);
75: } else {
76: return (name.toUpperCase().indexOf(OLD_GEN_NAME) > -1 || name
77: .toUpperCase().indexOf(TENURED_GEN_NAME) > -1);
78: }
79: }
80:
81: public boolean isMemoryPoolMonitoringSupported() {
82: return true;
83: }
84:
85: public MemoryUsage getOldGenUsage() {
86: java.lang.management.MemoryUsage oldGenUsage = oldGenBean
87: .getUsage();
88: return new Jdk15MemoryUsage(oldGenUsage, oldGenBean.getName(),
89: oldGenCollectorBean.getCollectionCount());
90: }
91: }
|