1 4 package com.tc.management.beans.tx; 5 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import javax.management.NotCompliantMBeanException ; 11 import javax.management.openmbean.CompositeData ; 12 import javax.management.openmbean.OpenDataException ; 13 import javax.management.openmbean.TabularData ; 14 15 import com.tc.management.AbstractTerracottaMBean; 16 import com.tc.management.opentypes.adapters.ClassCreationCount; 17 import com.tc.management.stats.AggregateInteger; 18 19 public final class ClientTxMonitor extends AbstractTerracottaMBean implements ClientTxMonitorMBean { 20 21 final AggregateInteger readTransactions; 22 final AggregateInteger writeTransactions; 23 final AggregateInteger writesPerTransaction; 24 final AggregateInteger modifiedObjectsPerTransaction; 25 final AggregateInteger newObjectsPerTransaction; 26 27 final AggregateInteger notifiesPerTransaction; 28 final AggregateInteger writesPerObject; 29 final Map objectCreationCountByClass; 30 31 public ClientTxMonitor() throws NotCompliantMBeanException { 32 super(ClientTxMonitorMBean.class, false); 33 readTransactions = new AggregateInteger("Client transactions (read)", 100); 34 writeTransactions = new AggregateInteger("Client transactions (write)", 100); 35 writesPerTransaction = new AggregateInteger("Writes per transaction (includes Object.notify() calls)", 100); 36 modifiedObjectsPerTransaction = new AggregateInteger("Objects modified per transaction", 100); 37 newObjectsPerTransaction = new AggregateInteger("New objects created per transaction", 100); 38 notifiesPerTransaction = new AggregateInteger("Object.notify() invocations per transaction"); 39 writesPerObject = new AggregateInteger("Modifications per object"); 40 objectCreationCountByClass = new HashMap (); 41 } 42 43 public int getReadTransactionCount() { 44 return readTransactions.getN(); 45 } 46 47 public int getReadTransactionRatePerSecond() { 48 return readTransactions.getSampleRate(1000); 49 } 50 51 public int getWriteTransactionCount() { 52 return writeTransactions.getN(); 53 } 54 55 public int getWriteTransactionRatePerSecond() { 56 return writeTransactions.getSampleRate(1000); 57 } 58 59 public int getMinWritesPerWriteTransaction() { 60 return writesPerTransaction.getMinimum(); 61 } 62 63 public int getMaxWritesPerWriteTransaction() { 64 return writesPerTransaction.getMaximum(); 65 } 66 67 public int getMaxModifiedObjectsPerTransaction() { 68 return modifiedObjectsPerTransaction.getMaximum(); 69 } 70 71 public int getAvgModifiedObjectsPerTransaction() { 72 return (int) modifiedObjectsPerTransaction.getAverage(); 73 } 74 75 public int getObjectModificationRatePerSecond() { 76 return modifiedObjectsPerTransaction.getSampleRate(1000); 77 } 78 79 public int getMaxNewObjectsPerTransaction() { 80 return newObjectsPerTransaction.getMaximum(); 81 } 82 83 public int getAvgNewObjectsPerTransaction() { 84 return (int) newObjectsPerTransaction.getAverage(); 85 } 86 87 public int getObjectCreationRatePerSecond() { 88 return newObjectsPerTransaction.getSampleRate(1000); 89 } 90 91 public int getMaxNotificationsPerTransaction() { 92 return notifiesPerTransaction.getMaximum(); 93 } 94 95 public int getAvgNotificationsPerTransaction() { 96 return (int) notifiesPerTransaction.getAverage(); 97 } 98 99 public int getMaxWritesPerObject() { 100 return writesPerObject.getMaximum(); 101 } 102 103 public int getAvgWritesPerObject() { 104 return (int) writesPerObject.getAverage(); 105 } 106 107 public TabularData getObjectCreationCountByClass() throws OpenDataException { 108 TabularData tabularData = ClassCreationCount.newTabularDataInstance(); 109 CompositeData compositeData; 110 111 synchronized (objectCreationCountByClass) { 112 for (Iterator iter = objectCreationCountByClass.keySet().iterator(); iter.hasNext();) { 113 Class classNameItemValue = (Class ) iter.next(); 114 AggregateInteger objectCreationCountItemValue = (AggregateInteger) objectCreationCountByClass 115 .get(classNameItemValue); 116 compositeData = new ClassCreationCount(classNameItemValue.getName(), new Integer (objectCreationCountItemValue 117 .getSum())).toCompositeData(); 118 tabularData.put(compositeData); 119 } 120 } 121 return tabularData; 122 } 123 124 public synchronized void reset() { 125 readTransactions.reset(); 126 writeTransactions.reset(); 127 writesPerTransaction.reset(); 128 modifiedObjectsPerTransaction.reset(); 129 newObjectsPerTransaction.reset(); 130 notifiesPerTransaction.reset(); 131 writesPerObject.reset(); 132 synchronized (objectCreationCountByClass) { 133 objectCreationCountByClass.clear(); 134 } 135 } 136 137 public synchronized void committedReadTransaction() { 138 if (isEnabled()) readTransactions.addSample(1); 139 } 140 141 public synchronized void committedWriteTransaction(final int notifyCount, final int modifiedObjectCount, 142 final int[] writeCountPerObject, final Map newObjectCountByClass) { 143 if (isEnabled()) { 144 writeTransactions.addSample(1); 145 modifiedObjectsPerTransaction.addSample(modifiedObjectCount); 146 notifiesPerTransaction.addSample(notifyCount); 147 int totalWriteCount = 0; 148 for (int i = 0; i < writeCountPerObject.length; i++) { 149 totalWriteCount += writeCountPerObject[i]; 150 writesPerObject.addSample(writeCountPerObject[i]); 151 } 152 writesPerTransaction.addSample(totalWriteCount + notifyCount); 153 if (newObjectCountByClass != null && !newObjectCountByClass.isEmpty()) { 154 int totalNewObjectCount = 0; 155 for (Iterator iter = newObjectCountByClass.keySet().iterator(); iter.hasNext();) { 156 final Class createdObjectClass = (Class ) iter.next(); 157 final Integer classCreationCount = (Integer ) newObjectCountByClass.get(createdObjectClass); 158 synchronized (objectCreationCountByClass) { 159 AggregateInteger instanceCounter = (AggregateInteger) objectCreationCountByClass.get(createdObjectClass); 160 if (instanceCounter == null) { 161 instanceCounter = new AggregateInteger("Object creation count for class[" + createdObjectClass.getName() 162 + "]"); 163 objectCreationCountByClass.put(createdObjectClass, instanceCounter); 164 } 165 instanceCounter.addSample(classCreationCount.intValue()); 166 totalNewObjectCount += classCreationCount.intValue(); 167 } 168 } 169 newObjectsPerTransaction.addSample(totalNewObjectCount); 170 } else { 171 newObjectsPerTransaction.addSample(0); 172 } 173 } 174 } 175 } 176 | Popular Tags |