1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.core.service;
21
22 import java.util.concurrent.atomic.AtomicInteger;
23 import java.util.concurrent.atomic.AtomicLong;
24
25
26
27
28
29
30
31
32 public class IoServiceStatistics {
33
34 private AbstractIoService service;
35
36 private double readBytesThroughput;
37 private double writtenBytesThroughput;
38 private double readMessagesThroughput;
39 private double writtenMessagesThroughput;
40 private double largestReadBytesThroughput;
41 private double largestWrittenBytesThroughput;
42 private double largestReadMessagesThroughput;
43 private double largestWrittenMessagesThroughput;
44
45 private final AtomicLong readBytes = new AtomicLong();
46 private final AtomicLong writtenBytes = new AtomicLong();
47 private final AtomicLong readMessages = new AtomicLong();
48 private final AtomicLong writtenMessages = new AtomicLong();
49 private long lastReadTime;
50 private long lastWriteTime;
51
52 private long lastReadBytes;
53 private long lastWrittenBytes;
54 private long lastReadMessages;
55 private long lastWrittenMessages;
56 private long lastThroughputCalculationTime;
57
58 private final AtomicInteger scheduledWriteBytes = new AtomicInteger();
59 private final AtomicInteger scheduledWriteMessages = new AtomicInteger();
60
61 private int throughputCalculationInterval = 3;
62
63 private final Object throughputCalculationLock = new Object();
64
65 public IoServiceStatistics(AbstractIoService service) {
66 this.service = service;
67 }
68
69
70
71
72
73 public final int getLargestManagedSessionCount() {
74 return service.getListeners().getLargestManagedSessionCount();
75 }
76
77
78
79
80
81
82 public final long getCumulativeManagedSessionCount() {
83 return service.getListeners().getCumulativeManagedSessionCount();
84 }
85
86
87
88
89 public final long getLastIoTime() {
90 return Math.max(lastReadTime, lastWriteTime);
91 }
92
93
94
95
96 public final long getLastReadTime() {
97 return lastReadTime;
98 }
99
100
101
102
103 public final long getLastWriteTime() {
104 return lastWriteTime;
105 }
106
107
108
109
110
111
112
113 public final long getReadBytes() {
114 return readBytes.get();
115 }
116
117
118
119
120
121
122
123 public final long getWrittenBytes() {
124 return writtenBytes.get();
125 }
126
127
128
129
130
131
132
133 public final long getReadMessages() {
134 return readMessages.get();
135 }
136
137
138
139
140
141
142
143 public final long getWrittenMessages() {
144 return writtenMessages.get();
145 }
146
147
148
149
150 public final double getReadBytesThroughput() {
151 resetThroughput();
152 return readBytesThroughput;
153 }
154
155
156
157
158 public final double getWrittenBytesThroughput() {
159 resetThroughput();
160 return writtenBytesThroughput;
161 }
162
163
164
165
166 public final double getReadMessagesThroughput() {
167 resetThroughput();
168 return readMessagesThroughput;
169 }
170
171
172
173
174 public final double getWrittenMessagesThroughput() {
175 resetThroughput();
176 return writtenMessagesThroughput;
177 }
178
179
180
181
182 public final double getLargestReadBytesThroughput() {
183 return largestReadBytesThroughput;
184 }
185
186
187
188
189 public final double getLargestWrittenBytesThroughput() {
190 return largestWrittenBytesThroughput;
191 }
192
193
194
195
196 public final double getLargestReadMessagesThroughput() {
197 return largestReadMessagesThroughput;
198 }
199
200
201
202
203 public final double getLargestWrittenMessagesThroughput() {
204 return largestWrittenMessagesThroughput;
205 }
206
207
208
209
210
211 public final int getThroughputCalculationInterval() {
212 return throughputCalculationInterval;
213 }
214
215
216
217
218
219 public final long getThroughputCalculationIntervalInMillis() {
220 return throughputCalculationInterval * 1000L;
221 }
222
223
224
225
226
227 public final void setThroughputCalculationInterval(
228 int throughputCalculationInterval) {
229 if (throughputCalculationInterval < 0) {
230 throw new IllegalArgumentException(
231 "throughputCalculationInterval: "
232 + throughputCalculationInterval);
233 }
234
235 this.throughputCalculationInterval = throughputCalculationInterval;
236 }
237
238
239
240
241 protected final void setLastReadTime(long lastReadTime) {
242 this.lastReadTime = lastReadTime;
243 }
244
245
246
247
248 protected final void setLastWriteTime(long lastWriteTime) {
249 this.lastWriteTime = lastWriteTime;
250 }
251
252
253
254
255 private void resetThroughput() {
256 if (service.getManagedSessionCount() == 0) {
257 readBytesThroughput = 0;
258 writtenBytesThroughput = 0;
259 readMessagesThroughput = 0;
260 writtenMessagesThroughput = 0;
261 }
262 }
263
264
265
266
267 public void updateThroughput(long currentTime) {
268 synchronized (throughputCalculationLock) {
269 int interval = (int) (currentTime - lastThroughputCalculationTime);
270 long minInterval = getThroughputCalculationIntervalInMillis();
271 if (minInterval == 0 || interval < minInterval) {
272 return;
273 }
274
275 long readBytes = this.readBytes.get();
276 long writtenBytes = this.writtenBytes.get();
277 long readMessages = this.readMessages.get();
278 long writtenMessages = this.writtenMessages.get();
279
280 readBytesThroughput = (readBytes - lastReadBytes) * 1000.0
281 / interval;
282 writtenBytesThroughput = (writtenBytes - lastWrittenBytes) * 1000.0
283 / interval;
284 readMessagesThroughput = (readMessages - lastReadMessages) * 1000.0
285 / interval;
286 writtenMessagesThroughput = (writtenMessages - lastWrittenMessages)
287 * 1000.0 / interval;
288
289 if (readBytesThroughput > largestReadBytesThroughput) {
290 largestReadBytesThroughput = readBytesThroughput;
291 }
292 if (writtenBytesThroughput > largestWrittenBytesThroughput) {
293 largestWrittenBytesThroughput = writtenBytesThroughput;
294 }
295 if (readMessagesThroughput > largestReadMessagesThroughput) {
296 largestReadMessagesThroughput = readMessagesThroughput;
297 }
298 if (writtenMessagesThroughput > largestWrittenMessagesThroughput) {
299 largestWrittenMessagesThroughput = writtenMessagesThroughput;
300 }
301
302 lastReadBytes = readBytes;
303 lastWrittenBytes = writtenBytes;
304 lastReadMessages = readMessages;
305 lastWrittenMessages = writtenMessages;
306
307 lastThroughputCalculationTime = currentTime;
308 }
309 }
310
311
312
313
314 public final void increaseReadBytes(long increment, long currentTime) {
315 readBytes.addAndGet(increment);
316 lastReadTime = currentTime;
317 }
318
319
320
321
322 public final void increaseReadMessages(long currentTime) {
323 readMessages.incrementAndGet();
324 lastReadTime = currentTime;
325 }
326
327
328
329
330 public final void increaseWrittenBytes(int increment, long currentTime) {
331 writtenBytes.addAndGet(increment);
332 lastWriteTime = currentTime;
333 }
334
335
336
337
338 public final void increaseWrittenMessages(long currentTime) {
339 writtenMessages.incrementAndGet();
340 lastWriteTime = currentTime;
341 }
342
343
344
345
346 public final int getScheduledWriteBytes() {
347 return scheduledWriteBytes.get();
348 }
349
350
351
352
353 public final void increaseScheduledWriteBytes(int increment) {
354 scheduledWriteBytes.addAndGet(increment);
355 }
356
357
358
359
360 public final int getScheduledWriteMessages() {
361 return scheduledWriteMessages.get();
362 }
363
364
365
366
367 public final void increaseScheduledWriteMessages() {
368 scheduledWriteMessages.incrementAndGet();
369 }
370
371
372
373
374 public final void decreaseScheduledWriteMessages() {
375 scheduledWriteMessages.decrementAndGet();
376 }
377
378
379
380
381 protected void setLastThroughputCalculationTime(
382 long lastThroughputCalculationTime) {
383 this.lastThroughputCalculationTime = lastThroughputCalculationTime;
384 }
385 }