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 */
017package org.apache.activemq.tool;
018
019import java.io.DataOutputStream;
020import java.lang.management.ManagementFactory;
021import java.lang.management.MemoryMXBean;
022import java.util.Properties;
023import java.util.concurrent.atomic.AtomicBoolean;
024
025public class MemoryMonitoringTool implements Runnable {
026
027    protected Properties testSettings = new Properties();
028    protected ReportGenerator reportGenerator = new ReportGenerator();
029
030    private long checkpointInterval = 5000;          // 5 sec sample checkpointInterval
031    private long resultIndex;
032    private AtomicBoolean isRunning = new AtomicBoolean(false);
033    private DataOutputStream dataDoutputStream;
034    private MemoryMXBean memoryBean;
035
036    public Properties getTestSettings() {
037        return testSettings;
038    }
039
040    public void setTestSettings(Properties sysTestSettings) {
041        this.testSettings = sysTestSettings;
042    }
043
044    public DataOutputStream getDataOutputStream() {
045        return dataDoutputStream;
046    }
047
048    public void setDataOutputStream(DataOutputStream dataDoutputStream) {
049        this.dataDoutputStream = dataDoutputStream;
050    }
051
052
053    public void stopMonitor() {
054        isRunning.set(false);
055    }
056
057
058    public long getCheckpointInterval() {
059        return checkpointInterval;
060    }
061
062    public void setCheckpointInterval(long checkpointInterval) {
063        this.checkpointInterval = checkpointInterval;
064    }
065
066
067    public Thread startMonitor() {
068
069        String intervalStr = this.getTestSettings().getProperty("checkpoint_interval");
070        checkpointInterval = new Integer(intervalStr).intValue();
071        this.getTestSettings().remove("checkpoint_interval");
072
073        memoryBean = ManagementFactory.getMemoryMXBean();
074        reportGenerator.setTestSettings(getTestSettings());
075        addTestInformation();
076
077        Thread t = new Thread(this);
078        t.setName("Memory monitoring tool");
079        isRunning.set(true);
080        t.start();
081
082        return t;
083
084    }
085
086
087    public void addTestInformation() {
088        reportGenerator.setReportName(this.getTestSettings().getProperty("report_name"));
089        reportGenerator.setReportDirectory(this.getTestSettings().getProperty("report_directory"));
090        reportGenerator.startGenerateReport();
091
092        reportGenerator.addTestInformation();
093        reportGenerator.writeWithIndent(4, "<jvm_memory_settings>");
094        reportGenerator.writeWithIndent(6, "<heap_memory>");
095        reportGenerator.writeWithIndent(8, "<committed>" + memoryBean.getHeapMemoryUsage().getCommitted() + "</committed>");
096        reportGenerator.writeWithIndent(8, "<max>" + memoryBean.getHeapMemoryUsage().getMax() + "</max>");
097        reportGenerator.writeWithIndent(6, "</heap_memory>");
098        reportGenerator.writeWithIndent(6, "<non_heap_memory>");
099        reportGenerator.writeWithIndent(8, "<committed>" + memoryBean.getNonHeapMemoryUsage().getCommitted() + "</committed>");
100        reportGenerator.writeWithIndent(8, "<max>" + memoryBean.getNonHeapMemoryUsage().getMax() + "</max>");
101        reportGenerator.writeWithIndent(6, "</non_heap_memory>");
102        reportGenerator.writeWithIndent(4, "</jvm_memory_settings>");
103
104        reportGenerator.addClientSettings();
105        reportGenerator.endTestInformation();
106    }
107
108
109    public void run() {
110
111        long nonHeapMB = 0;
112        long heapMB = 0;
113        long oneMB = 1024 * 1024;
114
115        reportGenerator.startTestResult(getCheckpointInterval());
116        while (isRunning.get()) {
117
118            try {
119                //wait every check point before getting the next memory usage
120                Thread.sleep(checkpointInterval);
121
122                nonHeapMB = memoryBean.getNonHeapMemoryUsage().getUsed() / oneMB;
123                heapMB = memoryBean.getHeapMemoryUsage().getUsed() / oneMB;
124
125                reportGenerator.writeWithIndent(6, "<memory_usage index='" + resultIndex
126                                                + "' non_heap_mb='" + nonHeapMB
127                                                + "' non_heap_bytes='"
128                                                + memoryBean.getNonHeapMemoryUsage().getUsed()
129                                                + "' heap_mb='" + heapMB
130                                                + "' heap_bytes='" + memoryBean.getHeapMemoryUsage().getUsed() + "'/>");
131
132                resultIndex++;
133
134            } catch (Exception e) {
135                e.printStackTrace();
136
137            }
138
139
140        }
141        reportGenerator.endTestResult();
142        reportGenerator.stopGenerateReport();
143
144    }
145
146
147}