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 java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  
28  import net.sf.jour.filter.MatchFilter;
29  import net.sf.jour.rt.agent.JVMInfoEvent;
30  import net.sf.jour.rt.view.ViewFilterItem;
31  import net.sf.jour.statistic.TimeBaseAccumulationList;
32  
33  import org.apache.log4j.Logger;
34  
35  
36  /***
37   * TODO Add docs
38   *
39   * Created on 15.12.2004
40   * Contributing Author(s):
41   *
42   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
43   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
44   *
45   * @author vlads
46   * @version $Revision: 1.1 $ ($Author: vlads $)  $Date: 2004/12/16 05:35:51 $
47   */
48  public class JMVRuntimeStatistics {
49  
50      protected static final Logger log = Logger.getLogger(JMVRuntimeStatistics.class);
51      
52      private HashMap hostJVMs = new HashMap();
53      
54      public static final String ATTRIBUTE_HOST_NAME = "JMVRuntimeHost.NAME";
55      
56      public JMVRuntimeStatistics() {
57          
58      }
59      
60      protected TimeBaseAccumulationList getByHost(JVMInfoEvent event, MatchFilter filter) {
61          if (event.getSystemInfo() == null) {
62              log.warn("No host info in JVMInfoEvent");
63              return null;
64          }
65          String hostName = event.getSystemInfo().getHostID();
66          TimeBaseAccumulationList memoryUsage = (TimeBaseAccumulationList)hostJVMs.get(hostName);        
67          
68          if (memoryUsage == null) {
69              memoryUsage = new TimeBaseAccumulationList();
70              memoryUsage.setAttribute(ATTRIBUTE_HOST_NAME, hostName);
71              synchronized (this) {
72                  hostJVMs.put(hostName, memoryUsage);
73              }
74          }
75          
76          return memoryUsage;
77      }
78      
79      public boolean processEvent(JVMInfoEvent event, MatchFilter filter) {
80          
81          TimeBaseAccumulationList memoryUsage = getByHost(event, filter);
82          if (memoryUsage == null) {
83              return false;
84          }
85              
86          double usedMem = event.getTotalMemory() - event.getFreeMemory();
87  
88          if ((memoryUsage.getCount() == 0)  && (filter != null) && (filter instanceof ViewFilterItem)) {
89              memoryUsage.setTimeFrame(((ViewFilterItem)filter).getTimeFrame());
90          }
91          memoryUsage.add(event.getTimestamp(),  new Double(usedMem));
92          
93          return true;
94      }
95      
96      public synchronized Collection getHostsJVM() {
97          return Collections.unmodifiableCollection(hostJVMs.values());
98      }
99      
100     public synchronized void reset() {
101         Iterator iter = hostJVMs.values().iterator();
102         while (iter.hasNext()) {
103             TimeBaseAccumulationList j = (TimeBaseAccumulationList) iter.next();
104             j.reset();
105         }
106     }
107 }