001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.activemq.tool;
019
020 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
021
022
023 import java.io.DataOutputStream;
024 import java.util.Properties;
025 import java.lang.management.MemoryMXBean;
026 import java.lang.management.ManagementFactory;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029
030
031 public class MemoryMonitoringTool implements Runnable {
032
033 private long checkpointInterval = 5000; // 5 sec sample checkpointInterval
034 private long resultIndex = 0;
035
036 private AtomicBoolean isRunning = new AtomicBoolean(false);
037 private DataOutputStream dataDoutputStream = null;
038
039 protected Properties testSettings = new Properties();
040 protected ReportGenerator reportGenerator = new ReportGenerator();
041 private MemoryMXBean memoryBean;
042
043 public Properties getTestSettings() {
044 return testSettings;
045 }
046
047 public void setTestSettings(Properties sysTestSettings) {
048 this.testSettings = sysTestSettings;
049 }
050
051 public DataOutputStream getDataOutputStream() {
052 return dataDoutputStream;
053 }
054
055 public void setDataOutputStream(DataOutputStream dataDoutputStream) {
056 this.dataDoutputStream = dataDoutputStream;
057 }
058
059
060 public void stopMonitor() {
061 isRunning.set(false);
062 }
063
064
065 public long getCheckpointInterval() {
066 return checkpointInterval;
067 }
068
069 public void setCheckpointInterval(long checkpointInterval) {
070 this.checkpointInterval = checkpointInterval;
071 }
072
073
074 public Thread startMonitor() {
075
076 String intervalStr = this.getTestSettings().getProperty("checkpoint_interval");
077 checkpointInterval = new Integer(intervalStr).intValue();
078 this.getTestSettings().remove("checkpoint_interval");
079
080 memoryBean = ManagementFactory.getMemoryMXBean();
081 reportGenerator.setTestSettings(getTestSettings());
082 addTestInformation();
083
084 Thread t = new Thread(this);
085 t.setName("Memory monitoring tool");
086 isRunning.set(true);
087 t.start();
088
089 return t;
090
091 }
092
093
094 public void addTestInformation() {
095 reportGenerator.setReportName(this.getTestSettings().getProperty("report_name"));
096 reportGenerator.setReportDirectory(this.getTestSettings().getProperty("report_directory"));
097 reportGenerator.startGenerateReport();
098
099 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 //wait every check point before getting the next memory usage
127 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 }