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 AbstractIoService 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 public IoServiceStatistics(AbstractIoService service) {
98 this.service = service;
99 }
100
101
102
103
104
105 public final int getLargestManagedSessionCount() {
106 return service.getListeners().getLargestManagedSessionCount();
107 }
108
109
110
111
112
113
114 public final long getCumulativeManagedSessionCount() {
115 return service.getListeners().getCumulativeManagedSessionCount();
116 }
117
118
119
120
121
122 public final long getLastIoTime() {
123 throughputCalculationLock.lock();
124
125 try {
126 return Math.max(lastReadTime, lastWriteTime);
127 } finally {
128 throughputCalculationLock.unlock();
129 }
130 }
131
132
133
134
135 public final long getLastReadTime() {
136 throughputCalculationLock.lock();
137
138 try {
139 return lastReadTime;
140 } finally {
141 throughputCalculationLock.unlock();
142 }
143 }
144
145
146
147
148 public final long getLastWriteTime() {
149 throughputCalculationLock.lock();
150
151 try {
152 return lastWriteTime;
153 } finally {
154 throughputCalculationLock.unlock();
155 }
156 }
157
158
159
160
161 public final long getReadBytes() {
162 throughputCalculationLock.lock();
163
164 try {
165 return readBytes;
166 } finally {
167 throughputCalculationLock.unlock();
168 }
169 }
170
171
172
173
174 public final long getWrittenBytes() {
175 throughputCalculationLock.lock();
176
177 try {
178 return writtenBytes;
179 } finally {
180 throughputCalculationLock.unlock();
181 }
182 }
183
184
185
186
187 public final long getReadMessages() {
188 throughputCalculationLock.lock();
189
190 try {
191 return readMessages;
192 } finally {
193 throughputCalculationLock.unlock();
194 }
195 }
196
197
198
199
200 public final long getWrittenMessages() {
201 throughputCalculationLock.lock();
202
203 try {
204 return writtenMessages;
205 } finally {
206 throughputCalculationLock.unlock();
207 }
208 }
209
210
211
212
213 public final double getReadBytesThroughput() {
214 throughputCalculationLock.lock();
215
216 try {
217 resetThroughput();
218 return readBytesThroughput;
219 } finally {
220 throughputCalculationLock.unlock();
221 }
222 }
223
224
225
226
227 public final double getWrittenBytesThroughput() {
228 throughputCalculationLock.lock();
229
230 try {
231 resetThroughput();
232 return writtenBytesThroughput;
233 } finally {
234 throughputCalculationLock.unlock();
235 }
236 }
237
238
239
240
241 public final double getReadMessagesThroughput() {
242 throughputCalculationLock.lock();
243
244 try {
245 resetThroughput();
246 return readMessagesThroughput;
247 } finally {
248 throughputCalculationLock.unlock();
249 }
250 }
251
252
253
254
255 public final double getWrittenMessagesThroughput() {
256 throughputCalculationLock.lock();
257
258 try {
259 resetThroughput();
260 return writtenMessagesThroughput;
261 } finally {
262 throughputCalculationLock.unlock();
263 }
264 }
265
266
267
268
269
270 public final double getLargestReadBytesThroughput() {
271 throughputCalculationLock.lock();
272
273 try {
274 return largestReadBytesThroughput;
275 } finally {
276 throughputCalculationLock.unlock();
277 }
278 }
279
280
281
282
283
284 public final double getLargestWrittenBytesThroughput() {
285 throughputCalculationLock.lock();
286
287 try {
288 return largestWrittenBytesThroughput;
289 } finally {
290 throughputCalculationLock.unlock();
291 }
292 }
293
294
295
296
297
298 public final double getLargestReadMessagesThroughput() {
299 throughputCalculationLock.lock();
300
301 try {
302 return largestReadMessagesThroughput;
303 } finally {
304 throughputCalculationLock.unlock();
305 }
306 }
307
308
309
310
311
312 public final double getLargestWrittenMessagesThroughput() {
313 throughputCalculationLock.lock();
314
315 try {
316 return largestWrittenMessagesThroughput;
317 } finally {
318 throughputCalculationLock.unlock();
319 }
320 }
321
322
323
324
325
326 public final int getThroughputCalculationInterval() {
327 return throughputCalculationInterval.get();
328 }
329
330
331
332
333
334 public final long getThroughputCalculationIntervalInMillis() {
335 return throughputCalculationInterval.get() * 1000L;
336 }
337
338
339
340
341
342
343
344 public final void setThroughputCalculationInterval(int throughputCalculationInterval) {
345 if (throughputCalculationInterval < 0) {
346 throw new IllegalArgumentException("throughputCalculationInterval: " + throughputCalculationInterval);
347 }
348
349 this.throughputCalculationInterval.set(throughputCalculationInterval);
350 }
351
352
353
354
355
356
357
358 protected final void setLastReadTime(long lastReadTime) {
359 throughputCalculationLock.lock();
360
361 try {
362 this.lastReadTime = lastReadTime;
363 } finally {
364 throughputCalculationLock.unlock();
365 }
366 }
367
368
369
370
371
372
373
374 protected final void setLastWriteTime(long lastWriteTime) {
375 throughputCalculationLock.lock();
376
377 try {
378 this.lastWriteTime = lastWriteTime;
379 } finally {
380 throughputCalculationLock.unlock();
381 }
382 }
383
384
385
386
387
388 private void resetThroughput() {
389 if (service.getManagedSessionCount() == 0) {
390 readBytesThroughput = 0;
391 writtenBytesThroughput = 0;
392 readMessagesThroughput = 0;
393 writtenMessagesThroughput = 0;
394 }
395 }
396
397
398
399
400
401
402 public void updateThroughput(long currentTime) {
403 throughputCalculationLock.lock();
404
405 try {
406 int interval = (int) (currentTime - lastThroughputCalculationTime);
407 long minInterval = getThroughputCalculationIntervalInMillis();
408
409 if ((minInterval == 0) || (interval < minInterval)) {
410 return;
411 }
412
413 long readBytes = this.readBytes;
414 long writtenBytes = this.writtenBytes;
415 long readMessages = this.readMessages;
416 long writtenMessages = this.writtenMessages;
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 }