KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > MemoryMonitoringTool


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.tool;
19
20 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
21
22
23 import java.io.DataOutputStream JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.lang.management.MemoryMXBean JavaDoc;
26 import java.lang.management.ManagementFactory JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31 public class MemoryMonitoringTool implements Runnable JavaDoc {
32
33     private long checkpointInterval = 5000; // 5 sec sample checkpointInterval
34
private long resultIndex = 0;
35
36     private AtomicBoolean JavaDoc isRunning = new AtomicBoolean JavaDoc(false);
37     private DataOutputStream JavaDoc dataDoutputStream = null;
38
39     protected Properties JavaDoc testSettings = new Properties JavaDoc();
40     protected ReportGenerator reportGenerator = new ReportGenerator();
41     private MemoryMXBean JavaDoc memoryBean;
42
43     public Properties JavaDoc getTestSettings() {
44         return testSettings;
45     }
46
47     public void setTestSettings(Properties JavaDoc sysTestSettings) {
48         this.testSettings = sysTestSettings;
49     }
50
51     public DataOutputStream JavaDoc getDataOutputStream() {
52         return dataDoutputStream;
53     }
54
55     public void setDataOutputStream(DataOutputStream JavaDoc 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 JavaDoc startMonitor() {
75
76         String JavaDoc intervalStr = this.getTestSettings().getProperty("checkpoint_interval");
77         checkpointInterval = new Integer JavaDoc(intervalStr).intValue();
78         this.getTestSettings().remove("checkpoint_interval");
79
80         memoryBean = ManagementFactory.getMemoryMXBean();
81         reportGenerator.setTestSettings(getTestSettings());
82         addTestInformation();
83
84         Thread JavaDoc t = new Thread JavaDoc(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                 //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 JavaDoc e) {
137                 e.printStackTrace();
138
139             }
140
141
142         }
143         reportGenerator.endTestResult();
144         reportGenerator.stopGenerateReport();
145
146     }
147
148
149 }
150
Popular Tags