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 public final void setThroughputCalculationInterval(int throughputCalculationInterval) {
343 if (throughputCalculationInterval < 0) {
344 throw new IllegalArgumentException("throughputCalculationInterval: " + throughputCalculationInterval);
345 }
346
347 this.throughputCalculationInterval.set(throughputCalculationInterval);
348 }
349
350
351
352
353
354
355
356 protected final void setLastReadTime(long lastReadTime) {
357 throughputCalculationLock.lock();
358
359 try {
360 this.lastReadTime = lastReadTime;
361 } finally {
362 throughputCalculationLock.unlock();
363 }
364 }
365
366
367
368
369
370
371
372 protected final void setLastWriteTime(long lastWriteTime) {
373 throughputCalculationLock.lock();
374
375 try {
376 this.lastWriteTime = lastWriteTime;
377 } finally {
378 throughputCalculationLock.unlock();
379 }
380 }
381
382
383
384
385
386 private void resetThroughput() {
387 if (service.getManagedSessionCount() == 0) {
388 readBytesThroughput = 0;
389 writtenBytesThroughput = 0;
390 readMessagesThroughput = 0;
391 writtenMessagesThroughput = 0;
392 }
393 }
394
395
396
397
398 public void updateThroughput(long currentTime) {
399 throughputCalculationLock.lock();
400
401 try {
402 int interval = (int) (currentTime - lastThroughputCalculationTime);
403 long minInterval = getThroughputCalculationIntervalInMillis();
404
405 if ((minInterval == 0) || (interval < minInterval)) {
406 return;
407 }
408
409 long readBytes = this.readBytes;
410 long writtenBytes = this.writtenBytes;
411 long readMessages = this.readMessages;
412 long writtenMessages = this.writtenMessages;
413
414 readBytesThroughput = (readBytes - lastReadBytes) * 1000.0 / interval;
415 writtenBytesThroughput = (writtenBytes - lastWrittenBytes) * 1000.0 / interval;
416 readMessagesThroughput = (readMessages - lastReadMessages) * 1000.0 / interval;
417 writtenMessagesThroughput = (writtenMessages - lastWrittenMessages) * 1000.0 / interval;
418
419 if (readBytesThroughput > largestReadBytesThroughput) {
420 largestReadBytesThroughput = readBytesThroughput;
421 }
422
423 if (writtenBytesThroughput > largestWrittenBytesThroughput) {
424 largestWrittenBytesThroughput = writtenBytesThroughput;
425 }
426
427 if (readMessagesThroughput > largestReadMessagesThroughput) {
428 largestReadMessagesThroughput = readMessagesThroughput;
429 }
430
431 if (writtenMessagesThroughput > largestWrittenMessagesThroughput) {
432 largestWrittenMessagesThroughput = writtenMessagesThroughput;
433 }
434
435 lastReadBytes = readBytes;
436 lastWrittenBytes = writtenBytes;
437 lastReadMessages = readMessages;
438 lastWrittenMessages = writtenMessages;
439
440 lastThroughputCalculationTime = currentTime;
441 } finally {
442 throughputCalculationLock.unlock();
443 }
444 }
445
446
447
448
449
450
451
452
453
454
455 public final void increaseReadBytes(long nbBytesRead, long currentTime) {
456 throughputCalculationLock.lock();
457
458 try {
459 readBytes += nbBytesRead;
460 lastReadTime = currentTime;
461 } finally {
462 throughputCalculationLock.unlock();
463 }
464 }
465
466
467
468
469
470
471
472
473 public final void increaseReadMessages(long currentTime) {
474 throughputCalculationLock.lock();
475
476 try {
477 readMessages++;
478 lastReadTime = currentTime;
479 } finally {
480 throughputCalculationLock.unlock();
481 }
482 }
483
484
485
486
487
488
489
490
491
492
493 public final void increaseWrittenBytes(int nbBytesWritten, long currentTime) {
494 throughputCalculationLock.lock();
495
496 try {
497 writtenBytes += nbBytesWritten;
498 lastWriteTime = currentTime;
499 } finally {
500 throughputCalculationLock.unlock();
501 }
502 }
503
504
505
506
507
508
509
510
511 public final void increaseWrittenMessages(long currentTime) {
512 throughputCalculationLock.lock();
513
514 try {
515 writtenMessages++;
516 lastWriteTime = currentTime;
517 } finally {
518 throughputCalculationLock.unlock();
519 }
520 }
521
522
523
524
525 public final int getScheduledWriteBytes() {
526 throughputCalculationLock.lock();
527
528 try {
529 return scheduledWriteBytes;
530 } finally {
531 throughputCalculationLock.unlock();
532 }
533 }
534
535
536
537
538 public final void increaseScheduledWriteBytes(int increment) {
539 throughputCalculationLock.lock();
540
541 try {
542 scheduledWriteBytes += increment;
543 } finally {
544 throughputCalculationLock.unlock();
545 }
546 }
547
548
549
550
551 public final int getScheduledWriteMessages() {
552 throughputCalculationLock.lock();
553
554 try {
555 return scheduledWriteMessages;
556 } finally {
557 throughputCalculationLock.unlock();
558 }
559 }
560
561
562
563
564 public final void increaseScheduledWriteMessages() {
565 throughputCalculationLock.lock();
566
567 try {
568 scheduledWriteMessages++;
569 } finally {
570 throughputCalculationLock.unlock();
571 }
572 }
573
574
575
576
577 public final void decreaseScheduledWriteMessages() {
578 throughputCalculationLock.lock();
579
580 try {
581 scheduledWriteMessages--;
582 } finally {
583 throughputCalculationLock.unlock();
584 }
585 }
586
587
588
589
590 protected void setLastThroughputCalculationTime(long lastThroughputCalculationTime) {
591 throughputCalculationLock.lock();
592
593 try {
594 this.lastThroughputCalculationTime = lastThroughputCalculationTime;
595 } finally {
596 throughputCalculationLock.unlock();
597 }
598 }
599 }