1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
31
32 public class Concurrency
33 extends AbstractFeature<Concurrency>
34 implements Visitable
35 {
36
37
38 private static Log log = Log.getLog( Concurrency.class );
39
40
41 private int maxActives;
42
43
44 private int actives;
45
46 private long firstUse;
47
48 private long lastUse;
49
50
51
52
53
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
94
95 public int getActives()
96 {
97 return actives;
98 }
99
100
101
102
103 public int getMaxActives()
104 {
105 return maxActives;
106 }
107
108
109
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
135
136
137
138 public Concurrency getFeature()
139 {
140 return this;
141 }
142
143
144
145
146
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
168
169
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 }