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.locks.Lock;
24 import java.util.concurrent.locks.ReentrantLock;
25
26
27
28
29
30
31
32 public class IoServiceStatistics {
33
34 private IoService service;
35
36
37 private double readBytesThroughput;
38
39
40 private double writtenBytesThroughput;
41
42
43 private double readMessagesThroughput;
44
45
46 private double writtenMessagesThroughput;
47
48
49 private double largestReadBytesThroughput;
50
51
52 private double largestWrittenBytesThroughput;
53
54
55 private double largestReadMessagesThroughput;
56
57
58 private double largestWrittenMessagesThroughput;
59
60
61 private long readBytes;
62
63
64 private long writtenBytes;
65
66
67 private long readMessages;
68
69
70 private long writtenMessages;
71
72
73 private long lastReadTime;
74
75
76 private long lastWriteTime;
77
78 private long lastReadBytes;
79
80 private long lastWrittenBytes;
81
82 private long lastReadMessages;
83
84 private long lastWrittenMessages;
85
86 private long lastThroughputCalculationTime;
87
88 private int scheduledWriteBytes;
89
90 private int scheduledWriteMessages;
91
92
93 private final AtomicInteger throughputCalculationInterval = new AtomicInteger(3);
94
95 private final Lock throughputCalculationLock = new ReentrantLock();
96
97
98
99
100
101
102 public IoServiceStatistics(IoService service) {
103 this.service = service;
104 }
105
106
107
108
109
110 public final int getLargestManagedSessionCount() {
111 return ((AbstractIoService)service).getListeners().getLargestManagedSessionCount();
112 }
113
114
115
116
117
118
119 public final long getCumulativeManagedSessionCount() {
120 return ((AbstractIoService)service).getListeners().getCumulativeManagedSessionCount();
121 }
122
123
124
125
126
127 public final long getLastIoTime() {
128 throughputCalculationLock.lock();
129
130 try {
131 return Math.max(lastReadTime, lastWriteTime);
132 } finally {
133 throughputCalculationLock.unlock();
134 }
135 }
136
137
138
139
140 public final long getLastReadTime() {
141 throughputCalculationLock.lock();
142
143 try {
144 return lastReadTime;
145 } finally {
146 throughputCalculationLock.unlock();
147 }
148 }
149
150
151
152
153 public final long getLastWriteTime() {
154 throughputCalculationLock.lock();
155
156 try {
157 return lastWriteTime;
158 } finally {
159 throughputCalculationLock.unlock();
160 }
161 }
162
163
164
165
166 public final long getReadBytes() {
167 throughputCalculationLock.lock();
168
169 try {
170 return readBytes;
171 } finally {
172 throughputCalculationLock.unlock();
173 }
174 }
175
176
177
178
179 public final long getWrittenBytes() {
180 throughputCalculationLock.lock();
181
182 try {
183 return writtenBytes;
184 } finally {
185 throughputCalculationLock.unlock();
186 }
187 }
188
189
190
191
192 public final long getReadMessages() {
193 throughputCalculationLock.lock();
194
195 try {
196 return readMessages;
197 } finally {
198 throughputCalculationLock.unlock();
199 }
200 }
201
202
203
204
205 public final long getWrittenMessages() {
206 throughputCalculationLock.lock();
207
208 try {
209 return writtenMessages;
210 } finally {
211 throughputCalculationLock.unlock();
212 }
213 }
214
215
216
217
218 public final double getReadBytesThroughput() {
219 throughputCalculationLock.lock();
220
221 try {
222 resetThroughput();
223 return readBytesThroughput;
224 } finally {
225 throughputCalculationLock.unlock();
226 }
227 }
228
229
230
231
232 public final double getWrittenBytesThroughput() {
233 throughputCalculationLock.lock();
234
235 try {
236 resetThroughput();
237 return writtenBytesThroughput;
238 } finally {
239 throughputCalculationLock.unlock();
240 }
241 }
242
243
244
245
246 public final double getReadMessagesThroughput() {
247 throughputCalculationLock.lock();
248
249 try {
250 resetThroughput();
251 return readMessagesThroughput;
252 } finally {
253 throughputCalculationLock.unlock();
254 }
255 }
256
257
258
259
260 public final double getWrittenMessagesThroughput() {
261 throughputCalculationLock.lock();
262
263 try {
264 resetThroughput();
265 return writtenMessagesThroughput;
266 } finally {
267 throughputCalculationLock.unlock();
268 }
269 }
270
271
272
273
274
275 public final double getLargestReadBytesThroughput() {
276 throughputCalculationLock.lock();
277
278 try {
279 return largestReadBytesThroughput;
280 } finally {
281 throughputCalculationLock.unlock();
282 }
283 }
284
285
286
287
288
289 public final double getLargestWrittenBytesThroughput() {
290 throughputCalculationLock.lock();
291
292 try {
293 return largestWrittenBytesThroughput;
294 } finally {
295 throughputCalculationLock.unlock();
296 }
297 }
298
299
300
301
302
303 public final double getLargestReadMessagesThroughput() {
304 throughputCalculationLock.lock();
305
306 try {
307 return largestReadMessagesThroughput;
308 } finally {
309 throughputCalculationLock.unlock();
310 }
311 }
312
313
314
315
316
317 public final double getLargestWrittenMessagesThroughput() {
318 throughputCalculationLock.lock();
319
320 try {
321 return largestWrittenMessagesThroughput;
322 } finally {
323 throughputCalculationLock.unlock();
324 }
325 }
326
327
328
329
330
331 public final int getThroughputCalculationInterval() {
332 return throughputCalculationInterval.get();
333 }
334
335
336
337
338
339 public final long getThroughputCalculationIntervalInMillis() {
340 return throughputCalculationInterval.get() * 1000L;
341 }
342
343
344
345
346
347
348
349 public final void setThroughputCalculationInterval(int throughputCalculationInterval) {
350 if (throughputCalculationInterval < 0) {
351 throw new IllegalArgumentException("throughputCalculationInterval: " + throughputCalculationInterval);
352 }
353
354 this.throughputCalculationInterval.set(throughputCalculationInterval);
355 }
356
357
358
359
360
361
362
363 protected final void setLastReadTime(long lastReadTime) {
364 throughputCalculationLock.lock();
365
366 try {
367 this.lastReadTime = lastReadTime;
368 } finally {
369 throughputCalculationLock.unlock();
370 }
371 }
372
373
374
375
376
377
378
379 protected final void setLastWriteTime(long lastWriteTime) {
380 throughputCalculationLock.lock();
381
382 try {
383 this.lastWriteTime = lastWriteTime;
384 } finally {
385 throughputCalculationLock.unlock();
386 }
387 }
388
389
390
391
392
393 private void resetThroughput() {
394 if (service.getManagedSessionCount() == 0) {
395 readBytesThroughput = 0;
396 writtenBytesThroughput = 0;
397 readMessagesThroughput = 0;
398 writtenMessagesThroughput = 0;
399 }
400 }
401
402
403
404
405
406
407 public void updateThroughput(long currentTime) {
408 throughputCalculationLock.lock();
409
410 try {
411 int interval = (int) (currentTime - lastThroughputCalculationTime);
412 long minInterval = getThroughputCalculationIntervalInMillis();
413
414 if ((minInterval == 0) || (interval < minInterval)) {
415 return;
416 }
417
418 readBytesThroughput = (readBytes - lastReadBytes) * 1000.0 / interval;
419 writtenBytesThroughput = (writtenBytes - lastWrittenBytes) * 1000.0 / interval;
420 readMessagesThroughput = (readMessages - lastReadMessages) * 1000.0 / interval;
421 writtenMessagesThroughput = (writtenMessages - lastWrittenMessages) * 1000.0 / interval;
422
423 if (readBytesThroughput > largestReadBytesThroughput) {
424 largestReadBytesThroughput = readBytesThroughput;
425 }
426
427 if (writtenBytesThroughput > largestWrittenBytesThroughput) {
428 largestWrittenBytesThroughput = writtenBytesThroughput;
429 }
430
431 if (readMessagesThroughput > largestReadMessagesThroughput) {
432 largestReadMessagesThroughput = readMessagesThroughput;
433 }
434
435 if (writtenMessagesThroughput > largestWrittenMessagesThroughput) {
436 largestWrittenMessagesThroughput = writtenMessagesThroughput;
437 }
438
439 lastReadBytes = readBytes;
440 lastWrittenBytes = writtenBytes;
441 lastReadMessages = readMessages;
442 lastWrittenMessages = writtenMessages;
443
444 lastThroughputCalculationTime = currentTime;
445 } finally {
446 throughputCalculationLock.unlock();
447 }
448 }
449
450
451
452
453
454
455
456
457
458
459 public final void increaseReadBytes(long nbBytesRead, long currentTime) {
460 throughputCalculationLock.lock();
461
462 try {
463 readBytes += nbBytesRead;
464 lastReadTime = currentTime;
465 } finally {
466 throughputCalculationLock.unlock();
467 }
468 }
469
470
471
472
473
474
475
476
477 public final void increaseReadMessages(long currentTime) {
478 throughputCalculationLock.lock();
479
480 try {
481 readMessages++;
482 lastReadTime = currentTime;
483 } finally {
484 throughputCalculationLock.unlock();
485 }
486 }
487
488
489
490
491
492
493
494
495
496
497 public final void increaseWrittenBytes(int nbBytesWritten, long currentTime) {
498 throughputCalculationLock.lock();
499
500 try {
501 writtenBytes += nbBytesWritten;
502 lastWriteTime = currentTime;
503 } finally {
504 throughputCalculationLock.unlock();
505 }
506 }
507
508
509
510
511
512
513
514
515 public final void increaseWrittenMessages(long currentTime) {
516 throughputCalculationLock.lock();
517
518 try {
519 writtenMessages++;
520 lastWriteTime = currentTime;
521 } finally {
522 throughputCalculationLock.unlock();
523 }
524 }
525
526
527
528
529 public final int getScheduledWriteBytes() {
530 throughputCalculationLock.lock();
531
532 try {
533 return scheduledWriteBytes;
534 } finally {
535 throughputCalculationLock.unlock();
536 }
537 }
538
539
540
541
542
543
544 public final void increaseScheduledWriteBytes(int increment) {
545 throughputCalculationLock.lock();
546
547 try {
548 scheduledWriteBytes += increment;
549 } finally {
550 throughputCalculationLock.unlock();
551 }
552 }
553
554
555
556
557 public final int getScheduledWriteMessages() {
558 throughputCalculationLock.lock();
559
560 try {
561 return scheduledWriteMessages;
562 } finally {
563 throughputCalculationLock.unlock();
564 }
565 }
566
567
568
569
570 public final void increaseScheduledWriteMessages() {
571 throughputCalculationLock.lock();
572
573 try {
574 scheduledWriteMessages++;
575 } finally {
576 throughputCalculationLock.unlock();
577 }
578 }
579
580
581
582
583 public final void decreaseScheduledWriteMessages() {
584 throughputCalculationLock.lock();
585
586 try {
587 scheduledWriteMessages--;
588 } finally {
589 throughputCalculationLock.unlock();
590 }
591 }
592
593
594
595
596
597
598 protected void setLastThroughputCalculationTime(long lastThroughputCalculationTime) {
599 throughputCalculationLock.lock();
600
601 try {
602 this.lastThroughputCalculationTime = lastThroughputCalculationTime;
603 } finally {
604 throughputCalculationLock.unlock();
605 }
606 }
607 }