1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.util.log;
17
18 import static fulmine.util.Utils.COMMA_SPACE;
19
20 import java.util.TimerTask;
21
22 import org.apache.commons.logging.Log;
23
24 import fulmine.Domain;
25 import fulmine.Type;
26 import fulmine.event.EventFrameExecution;
27 import fulmine.event.IEventManager;
28 import fulmine.model.IModelManager;
29 import fulmine.model.container.IContainer;
30 import fulmine.model.field.LongField;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public final class Statistics extends TimerTask implements IStatistics,
45 Runnable
46 {
47 private final static Log LOG = new AsyncLog(Statistics.class);
48
49 private static final String TOTAL_POPPED = "totalPopped";
50
51 private static final String POPPED = "popped";
52
53 private static final String QUEUE_SIZE = "Q size";
54
55 private static final String MAX = "max Q time (usec)";
56
57
58 long max;
59
60
61 long queueSize;
62
63
64 long popped;
65
66
67 long totalPopped;
68
69
70 long lastStatisticsLogTime = System.currentTimeMillis();
71
72
73
74
75
76
77 public final static String TIME_PERIOD_PROPERTY =
78 Statistics.class.getSimpleName() + ".timePeriod";
79
80
81 public final static String DEFAUL_TIME_PERIOD = "10000";
82
83
84 public static final long TIME_PERIOD =
85 Integer.parseInt(System.getProperty(TIME_PERIOD_PROPERTY,
86 DEFAUL_TIME_PERIOD));
87
88
89 private final String infoRecordIdentity;
90
91
92
93
94
95 private final IModelManager context;
96
97
98 private IContainer infoRecord;
99
100
101
102
103
104
105
106
107
108 public Statistics(String infoRecordIdentity, IModelManager context)
109 {
110 super();
111 this.infoRecordIdentity = infoRecordIdentity;
112 this.context = context;
113 }
114
115 public void processEvent(long elapsedTimeMicroSecs)
116 {
117 this.max = Math.max(elapsedTimeMicroSecs, this.max);
118 }
119
120 public void intervalFinished(final int queueSize, final int popped)
121 {
122 this.popped += popped;
123 this.queueSize = queueSize;
124 }
125
126
127
128
129
130 private void updateInfoRecord()
131 {
132 if (this.context != null && this.context.isActive())
133 {
134 if (this.infoRecord == null)
135 {
136 this.infoRecord =
137 this.context.getLocalContainer(this.infoRecordIdentity,
138 Type.SYSTEM, Domain.FRAMEWORK);
139 this.infoRecord.add(new LongField(MAX));
140 this.infoRecord.add(new LongField(QUEUE_SIZE));
141 this.infoRecord.add(new LongField(POPPED));
142 this.infoRecord.add(new LongField(TOTAL_POPPED));
143 }
144 this.infoRecord.beginFrame(new EventFrameExecution());
145 try
146 {
147 this.infoRecord.getLongField(MAX).set(this.max);
148 this.infoRecord.getLongField(QUEUE_SIZE).set(this.queueSize);
149 this.infoRecord.getLongField(POPPED).set(this.popped);
150 this.infoRecord.getLongField(TOTAL_POPPED).set(this.totalPopped);
151 }
152 finally
153 {
154 this.infoRecord.endFrame();
155 }
156 }
157 }
158
159
160
161
162 @Override
163 public final String toString()
164 {
165 StringBuilder sb = new StringBuilder(80);
166 sb.append(this.max).append(COMMA_SPACE);
167 sb.append(this.queueSize).append(COMMA_SPACE);
168 sb.append(this.popped).append(COMMA_SPACE);
169 sb.append(this.totalPopped).append(COMMA_SPACE);
170 return sb.toString();
171 }
172
173
174
175
176 public void run()
177 {
178 this.totalPopped += this.popped;
179
180 if (LOG.isInfoEnabled())
181 {
182 final String data = toString();
183 StringBuilder sb =
184 new StringBuilder(data.length()
185 + this.infoRecordIdentity.length() + 8);
186 sb.append(infoRecordIdentity).append("(stats) ");
187 sb.append(data);
188 LOG.info(sb.toString());
189 }
190 updateInfoRecord();
191
192
193 this.max = 0;
194 this.popped = 0;
195 }
196 }