1 18 package org.apache.activemq.tool; 19 20 import java.util.concurrent.atomic.AtomicBoolean ; 21 22 23 import java.io.DataOutputStream ; 24 import java.util.Properties ; 25 import java.lang.management.MemoryMXBean ; 26 import java.lang.management.ManagementFactory ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 31 public class MemoryMonitoringTool implements Runnable { 32 33 private long checkpointInterval = 5000; private long resultIndex = 0; 35 36 private AtomicBoolean isRunning = new AtomicBoolean (false); 37 private DataOutputStream dataDoutputStream = null; 38 39 protected Properties testSettings = new Properties (); 40 protected ReportGenerator reportGenerator = new ReportGenerator(); 41 private MemoryMXBean memoryBean; 42 43 public Properties getTestSettings() { 44 return testSettings; 45 } 46 47 public void setTestSettings(Properties sysTestSettings) { 48 this.testSettings = sysTestSettings; 49 } 50 51 public DataOutputStream getDataOutputStream() { 52 return dataDoutputStream; 53 } 54 55 public void setDataOutputStream(DataOutputStream dataDoutputStream) { 56 this.dataDoutputStream = dataDoutputStream; 57 } 58 59 60 public void stopMonitor() { 61 isRunning.set(false); 62 } 63 64 65 public long getCheckpointInterval() { 66 return checkpointInterval; 67 } 68 69 public void setCheckpointInterval(long checkpointInterval) { 70 this.checkpointInterval = checkpointInterval; 71 } 72 73 74 public Thread startMonitor() { 75 76 String intervalStr = this.getTestSettings().getProperty("checkpoint_interval"); 77 checkpointInterval = new Integer (intervalStr).intValue(); 78 this.getTestSettings().remove("checkpoint_interval"); 79 80 memoryBean = ManagementFactory.getMemoryMXBean(); 81 reportGenerator.setTestSettings(getTestSettings()); 82 addTestInformation(); 83 84 Thread t = new Thread (this); 85 t.setName("Memory monitoring tool"); 86 isRunning.set(true); 87 t.start(); 88 89 return t; 90 91 } 92 93 94 public void addTestInformation() { 95 reportGenerator.setReportName(this.getTestSettings().getProperty("report_name")); 96 reportGenerator.setReportDirectory(this.getTestSettings().getProperty("report_directory")); 97 reportGenerator.startGenerateReport(); 98 99 reportGenerator.addTestInformation(); 100 reportGenerator.writeWithIndent(4, "<jvm_memory_settings>"); 101 reportGenerator.writeWithIndent(6, "<heap_memory>"); 102 reportGenerator.writeWithIndent(8, "<committed>" + memoryBean.getHeapMemoryUsage().getCommitted() + "</committed>"); 103 reportGenerator.writeWithIndent(8, "<max>" + memoryBean.getHeapMemoryUsage().getMax() + "</max>"); 104 reportGenerator.writeWithIndent(6, "</heap_memory>"); 105 reportGenerator.writeWithIndent(6, "<non_heap_memory>"); 106 reportGenerator.writeWithIndent(8, "<committed>" + memoryBean.getNonHeapMemoryUsage().getCommitted() + "</committed>"); 107 reportGenerator.writeWithIndent(8, "<max>" + memoryBean.getNonHeapMemoryUsage().getMax() + "</max>"); 108 reportGenerator.writeWithIndent(6, "</non_heap_memory>"); 109 reportGenerator.writeWithIndent(4, "</jvm_memory_settings>"); 110 111 reportGenerator.addClientSettings(); 112 reportGenerator.endTestInformation(); 113 } 114 115 116 public void run() { 117 118 long nonHeapMB = 0; 119 long heapMB = 0; 120 long oneMB = 1024 * 1024; 121 122 reportGenerator.startTestResult(getCheckpointInterval()); 123 while (isRunning.get()) { 124 125 try { 126 Thread.sleep(checkpointInterval); 128 129 nonHeapMB = memoryBean.getNonHeapMemoryUsage().getUsed() / oneMB; 130 heapMB = memoryBean.getHeapMemoryUsage().getUsed() / oneMB; 131 132 reportGenerator.writeWithIndent(6, "<memory_usage index=" + resultIndex + " non_heap_mb=" + nonHeapMB + " non_heap_bytes=" + memoryBean.getNonHeapMemoryUsage().getUsed() + " heap_mb=" + heapMB + " heap_bytes=" + memoryBean.getHeapMemoryUsage().getUsed() + "/>"); 133 134 resultIndex++; 135 136 } catch (Exception e) { 137 e.printStackTrace(); 138 139 } 140 141 142 } 143 reportGenerator.endTestResult(); 144 reportGenerator.stopGenerateReport(); 145 146 } 147 148 149 } 150 | Popular Tags |