View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
15   */
16  package org.jmonit.features;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.jmonit.events.ExecutionEvent;
22  import org.jmonit.events.MonitoringEvent;
23  import org.jmonit.log.Log;
24  import org.jmonit.reporting.Visitable;
25  import org.jmonit.reporting.Visitor;
26  
27  /**
28   * Computes some statistics from monitored datas.
29   * 
30   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
31   */
32  public class Concurrency
33      extends AbstractFeature<Concurrency>
34      implements Visitable
35  {
36  
37      /** logger */
38      private static Log log = Log.getLog( Concurrency.class );
39  
40      /** max number of active threads */
41      private int maxActives;
42  
43      /** current active threads */
44      private int actives;
45  
46      private long firstUse;
47  
48      private long lastUse;
49  
50      /**
51       * {@inheritDoc}
52       * 
53       * @see org.jmonit.events.MonitoringEventListener#onMonitoringEvent(org.jmonit.events.MonitoringEvent)
54       */
55      public void onMonitoringEvent( MonitoringEvent event )
56      {
57          if ( event instanceof ExecutionEvent )
58          {
59              ExecutionEvent probeEvent = (ExecutionEvent) event;
60              switch ( probeEvent.getState() )
61              {
62                  case ExecutionEvent.STARTED:
63                      entered();
64                      break;
65  
66                  case ExecutionEvent.STOPPED:
67                  case ExecutionEvent.CANCELED:
68                      leaved();
69                      break;
70  
71                  default:
72                      log.warn( "unsupported ProbeEvent state" + probeEvent.getState() );
73                      break;
74              }
75          }
76      }
77  
78      private synchronized void entered()
79      {
80          actives++;
81          if ( maxActives < actives )
82          {
83              maxActives = actives;
84          }
85          lastUse = System.currentTimeMillis();
86          if ( firstUse == 0 )
87          {
88              firstUse = lastUse;
89          }
90      }
91  
92      /**
93       * @return the actives
94       */
95      public int getActives()
96      {
97          return actives;
98      }
99  
100     /**
101      * @return the maxActives
102      */
103     public int getMaxActives()
104     {
105         return maxActives;
106     }
107 
108     /**
109      * @return the monitor load : active time / total time used
110      */
111     public double getLoad()
112     {
113         if ( lastUse > firstUse )
114         {
115             long periodns = ( lastUse - firstUse ) * 1000000;
116             double workedTime = getFeature( Statistics.class ).getTotal();
117             return workedTime / periodns;
118         }
119         return 0D;
120     }
121 
122     private synchronized void leaved()
123     {
124         actives--;
125         lastUse = System.currentTimeMillis();
126     }
127 
128     public void clear()
129     {
130         maxActives = 0;
131     }
132 
133     /**
134      * {@inheritDoc}
135      * 
136      * @see org.jmonit.spi.Plugin#getFeature()
137      */
138     public Concurrency getFeature()
139     {
140         return this;
141     }
142 
143     /**
144      * {@inheritDoc}
145      * 
146      * @see org.jmonit.reporting.Visitable#accept(org.jmonit.reporting.Visitor)
147      */
148     public synchronized void accept( Visitor visitor )
149     {
150         Map<String, Object> attributes = new HashMap<String, Object>();
151         attributes.put( "actives", getActives() );
152         attributes.put( "maxActives", getMaxActives() );
153         attributes.put( "load", getLoad() );
154         visitor.visit( attributes );
155     }
156 
157     public static final org.jmonit.spi.Factory<Concurrency> FACTORY =
158         new org.jmonit.spi.Factory<Concurrency>()
159         {
160             public Concurrency newPluginInstance()
161             {
162                 return new Concurrency();
163             }
164         };
165 
166     /**
167      * {@inheritDoc}
168      * 
169      * @see java.lang.Object#toString()
170      */
171     public String toString()
172     {
173         StringBuffer stb = new StringBuffer( "Concurrency" );
174         stb.append( "[\n" );
175         stb.append( "  max actives = " ).append( this.maxActives ).append( "\n" );
176         stb.append( "  actives     = " ).append( this.actives ).append( "\n" );
177         stb.append( "  load        = " ).append( getLoad() ).append( "\n" );
178         stb.append( "]" );
179         return stb.toString();
180     }
181 
182 }