View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.swingmonitor;
22  
23  //import org.apache.log4j.Logger;
24  
25  import java.awt.*;
26  import java.awt.event.*;
27  import javax.swing.*;
28  import org.jfree.chart.*;
29  import org.jfree.chart.axis.DateAxis;
30  import org.jfree.chart.axis.NumberAxis;
31  import org.jfree.chart.plot.XYPlot;
32  import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
33  import org.jfree.data.time.*;
34  import org.jfree.ui.Spacer;
35  
36  /***
37   * Show Memory usage in Monitor Application.
38   * This is different from client (Application under test) Memory usage.
39   *
40   * Created on 15.12.2004
41   * Contributing Author(s):
42   *
43   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
44   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
45   *
46   * @author vlads
47   * @version $Revision: 1.3 $ ($Author: vlads $)  $Date: 2004/12/16 05:35:51 $
48   */
49  public class MonitorMemoryUsagePanel extends MonitoringJPanel {
50      
51      private TimeSeries total;
52  
53      private TimeSeries free;
54  
55      class MemoryDataGenerator extends Timer implements ActionListener {
56  
57          public MemoryDataGenerator(int delay) {
58              super(delay, null);
59              addActionListener(this);
60          }
61  
62          public void actionPerformed(ActionEvent actionevent) {
63              updateData();
64          }
65      }
66  
67      public MonitorMemoryUsagePanel(int historyMin, int delay) {
68          setLayout(new BorderLayout());
69      	int periods = historyMin * 60 * 1000;
70      	
71          total = new TimeSeries("Total Memory", org.jfree.data.time.Millisecond.class);
72          total.setHistoryCount(periods);
73          free = new TimeSeries("Free Memory", org.jfree.data.time.Millisecond.class);
74          free.setHistoryCount(periods);
75          TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
76          timeseriescollection.addSeries(total);
77          timeseriescollection.addSeries(free);
78          DateAxis dateaxis = new DateAxis("Time");
79          NumberAxis numberaxis = new NumberAxis("Memory (MB)");
80          dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
81          numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
82          dateaxis.setLabelFont(new Font("SansSerif", 0, 14));
83          numberaxis.setLabelFont(new Font("SansSerif", 0, 14));
84          DefaultXYItemRenderer defaultxyitemrenderer = new DefaultXYItemRenderer();
85          defaultxyitemrenderer.setSeriesPaint(0, Color.red);
86          defaultxyitemrenderer.setSeriesPaint(1, Color.green);
87          defaultxyitemrenderer.setStroke(new BasicStroke(3F, 0, 2));
88          XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis, defaultxyitemrenderer);
89          xyplot.setBackgroundPaint(Color.lightGray);
90          xyplot.setDomainGridlinePaint(Color.white);
91          xyplot.setRangeGridlinePaint(Color.white);
92          xyplot.setAxisOffset(new Spacer(1, 5D, 5D, 5D, 5D));
93          dateaxis.setAutoRange(true);
94          dateaxis.setLowerMargin(0.0D);
95          dateaxis.setUpperMargin(0.0D);
96          dateaxis.setTickLabelsVisible(true);
97          numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
98          
99          JFreeChart jfreechart = new JFreeChart("Analyzer JVM Memory Usage", new Font("SansSerif", 1, 24), xyplot, true);
100         jfreechart.setBackgroundPaint(Color.white);
101         
102         ChartPanel chartpanel = new ChartPanel(jfreechart);
103         chartpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4),
104                 	BorderFactory.createLineBorder(Color.black)));
105         
106         StandardLegend standardlegend = (StandardLegend) jfreechart.getLegend();
107         standardlegend.setItemFont(new Font("SansSerif", 0, 12));
108         
109         chartpanel.setMouseZoomable(true, false);
110         
111         add(chartpanel);
112         
113         updateData();
114 
115         (new MemoryDataGenerator(delay)).start();
116     }
117     
118     public boolean canCloseTab() {
119         return false;
120     }
121 
122     
123     public void updateData() {
124         long freeMemory = Runtime.getRuntime().freeMemory();
125         long totalMemory = Runtime.getRuntime().totalMemory();
126         addTotalObservation(totalMemory / (1024 * 1024));
127         addFreeObservation(freeMemory / (1024 * 1024));
128     }
129     
130     private void addTotalObservation(double d) {
131         total.add(new Millisecond(), d);
132     }
133 
134     private void addFreeObservation(double d) {
135         free.add(new Millisecond(), d);
136     }
137 
138 }