~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/statistics/sampled/SampledCacheStatisticsImpl.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
15
15
 */
16
16
package net.sf.ehcache.statistics.sampled;
17
17
 
18
 
import java.util.concurrent.atomic.AtomicBoolean;
19
 
import java.util.concurrent.atomic.AtomicInteger;
20
 
 
21
18
import net.sf.ehcache.CacheException;
22
19
import net.sf.ehcache.Statistics;
23
20
import net.sf.ehcache.statistics.CacheUsageListener;
30
27
import net.sf.ehcache.util.counter.sampled.SampledRateCounter;
31
28
import net.sf.ehcache.util.counter.sampled.SampledRateCounterConfig;
32
29
 
 
30
import java.util.concurrent.atomic.AtomicBoolean;
 
31
import java.util.concurrent.atomic.AtomicInteger;
 
32
 
33
33
/**
34
 
 * An implementation of {@link SampledCacheStatistics} This also implements {@link CacheUsageListener} and depends on the notification
 
34
 * An implementation of {@link SampledCacheStatistics} This also implements {@link CacheUsageListener} and depends on
 
35
 * the notification
35
36
 * received from
36
37
 * these to update the stats
37
 
 * <p />
 
38
 * <p/>
38
39
 *
39
40
 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
40
41
 * @since 1.7
41
42
 */
42
 
public class SampledCacheStatisticsImpl implements CacheUsageListener, SampledCacheStatistics {
 
43
public class SampledCacheStatisticsImpl implements CacheUsageListener, CacheStatisticsSampler {
 
44
    private static final int MILLIS_PER_SECOND = 1000;
 
45
    private static final int NANOS_PER_MILLI = MILLIS_PER_SECOND * MILLIS_PER_SECOND;
 
46
    private static final int HIT_RATIO_MULTIPLIER = 100;
43
47
 
44
 
    private static final int AVERAGE_SEARCH_SAMPLE_INTERVAL = 10;
45
 
    private static final int DEFAULT_HISTORY_SIZE = 30;
46
 
    private static final int DEFAULT_INTERVAL_SECS = 1;
47
 
    private static final SampledCounterConfig DEFAULT_SAMPLED_COUNTER_CONFIG = new SampledCounterConfig(DEFAULT_INTERVAL_SECS,
 
48
    private static final SampledCounterConfig DEFAULT_SAMPLED_COUNTER_CONFIG = new SampledCounterConfig(
 
49
            DEFAULT_INTERVAL_SECS,
48
50
            DEFAULT_HISTORY_SIZE, true, 0L);
49
 
    private static final SampledRateCounterConfig DEFAULT_SAMPLED_RATE_COUNTER_CONFIG = new SampledRateCounterConfig(DEFAULT_INTERVAL_SECS,
50
 
            DEFAULT_HISTORY_SIZE, true);
 
51
    private static final SampledRateCounterConfig DEFAULT_RATE_COUNTER_CONFIG = new SampledRateCounterConfig(
 
52
            DEFAULT_INTERVAL_SECS, DEFAULT_HISTORY_SIZE, true);
 
53
    private static final SampledRateCounterConfig DEFAULT_AVG_SEARCH_COUNTER_CONFIG = new SampledRateCounterConfig(
 
54
            DEFAULT_SEARCH_INTERVAL_SEC, DEFAULT_HISTORY_SIZE, true);
51
55
 
52
56
    private volatile CounterManager counterManager;
53
57
    private final SampledCounter cacheHitCount;
60
64
    private final SampledCounter cacheMissOnDiskCount;
61
65
    private final SampledCounter cacheMissExpiredCount;
62
66
    private final SampledCounter cacheMissNotFoundCount;
 
67
    private final SampledRateCounter cacheHitRatio;
63
68
    private final SampledCounter cacheElementEvictedCount;
64
69
    private final SampledCounter cacheElementRemoved;
65
70
    private final SampledCounter cacheElementExpired;
69
74
    private final SampledCounter cacheXaCommitCount;
70
75
    private final SampledCounter cacheXaRollbackCount;
71
76
    private final SampledRateCounter averageGetTime;
 
77
    private final SampledRateCounter averageGetTimeNanos;
72
78
    private final SampledRateCounter averageSearchTime;
73
79
 
74
80
    private final AtomicBoolean sampledStatisticsEnabled;
75
81
    private final AtomicInteger statisticsAccuracy;
76
82
 
77
83
    /**
 
84
     * The default constructor
 
85
     *
 
86
     * @param timer
 
87
     */
 
88
    public SampledCacheStatisticsImpl(FailSafeTimer timer) {
 
89
        this(timer, DEFAULT_SAMPLED_COUNTER_CONFIG, DEFAULT_RATE_COUNTER_CONFIG, DEFAULT_AVG_SEARCH_COUNTER_CONFIG);
 
90
    }
 
91
 
 
92
    /**
 
93
     * @param timer
 
94
     * @param config
 
95
     */
 
96
    public SampledCacheStatisticsImpl(FailSafeTimer timer, SampledCounterConfig config) {
 
97
        this(timer, config, DEFAULT_RATE_COUNTER_CONFIG, DEFAULT_AVG_SEARCH_COUNTER_CONFIG);
 
98
    }
 
99
 
 
100
    /**
78
101
     * Constructor that accepts a timer which will be used to schedule the
79
102
     * sampled counters
80
103
     */
81
 
    public SampledCacheStatisticsImpl(FailSafeTimer timer) {
 
104
    public SampledCacheStatisticsImpl(FailSafeTimer timer,
 
105
                                      SampledCounterConfig config,
 
106
                                      SampledRateCounterConfig rateGetConfig,
 
107
                                      SampledRateCounterConfig rateSearchConfig) {
82
108
        counterManager = new CounterManagerImpl(timer);
83
 
        cacheHitCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
84
 
        cacheHitInMemoryCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
85
 
        cacheHitOffHeapCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
86
 
        cacheHitOnDiskCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
87
 
        cacheMissCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
88
 
        cacheMissInMemoryCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
89
 
        cacheMissOffHeapCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
90
 
        cacheMissOnDiskCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
91
 
        cacheMissExpiredCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
92
 
        cacheMissNotFoundCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
93
 
        cacheElementEvictedCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
94
 
        cacheElementRemoved = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
95
 
        cacheElementExpired = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
96
 
        cacheElementPut = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
97
 
        cacheElementUpdated = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
98
 
        cacheSearchCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
99
 
        cacheXaCommitCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
100
 
        cacheXaRollbackCount = createSampledCounter(DEFAULT_SAMPLED_COUNTER_CONFIG);
 
109
        cacheHitCount = createSampledCounter(config);
 
110
        cacheHitInMemoryCount = createSampledCounter(config);
 
111
        cacheHitOffHeapCount = createSampledCounter(config);
 
112
        cacheHitOnDiskCount = createSampledCounter(config);
 
113
        cacheMissCount = createSampledCounter(config);
 
114
        cacheMissInMemoryCount = createSampledCounter(config);
 
115
        cacheMissOffHeapCount = createSampledCounter(config);
 
116
        cacheMissOnDiskCount = createSampledCounter(config);
 
117
        cacheMissExpiredCount = createSampledCounter(config);
 
118
        cacheMissNotFoundCount = createSampledCounter(config);
 
119
        cacheElementEvictedCount = createSampledCounter(config);
 
120
        cacheElementRemoved = createSampledCounter(config);
 
121
        cacheElementExpired = createSampledCounter(config);
 
122
        cacheElementPut = createSampledCounter(config);
 
123
        cacheElementUpdated = createSampledCounter(config);
 
124
        cacheSearchCount = createSampledCounter(config);
 
125
        cacheXaCommitCount = createSampledCounter(config);
 
126
        cacheXaRollbackCount = createSampledCounter(config);
101
127
 
102
 
        averageGetTime = (SampledRateCounter) createSampledCounter(DEFAULT_SAMPLED_RATE_COUNTER_CONFIG);
103
 
        averageSearchTime = (SampledRateCounter) createSampledCounter(
104
 
                new SampledRateCounterConfig(AVERAGE_SEARCH_SAMPLE_INTERVAL, DEFAULT_HISTORY_SIZE, true));
 
128
        averageGetTime = (SampledRateCounter) createSampledCounter(rateGetConfig);
 
129
        averageGetTimeNanos = (SampledRateCounter) createSampledCounter(rateGetConfig);
 
130
        averageSearchTime = (SampledRateCounter) createSampledCounter(rateSearchConfig);
 
131
        cacheHitRatio = (SampledRateCounter) createSampledCounter(rateGetConfig);
105
132
 
106
133
        this.sampledStatisticsEnabled = new AtomicBoolean(true);
107
134
        this.statisticsAccuracy = new AtomicInteger(Statistics.STATISTICS_ACCURACY_BEST_EFFORT);
134
161
        cacheMissOnDiskCount.getAndReset();
135
162
        cacheMissExpiredCount.getAndReset();
136
163
        cacheMissNotFoundCount.getAndReset();
 
164
        cacheHitRatio.getAndReset();
137
165
        cacheElementEvictedCount.getAndReset();
138
166
        cacheElementRemoved.getAndReset();
139
167
        cacheElementExpired.getAndReset();
143
171
        cacheXaCommitCount.getAndReset();
144
172
        cacheXaRollbackCount.getAndReset();
145
173
        averageGetTime.getAndReset();
 
174
        averageGetTimeNanos.getAndReset();
146
175
        averageSearchTime.getAndReset();
147
176
    }
148
177
 
158
187
     */
159
188
    public void notifyCacheHitInMemory() {
160
189
        incrementIfStatsEnabled(cacheHitCount, cacheHitInMemoryCount);
 
190
        cacheHitRatio.increment(1 * HIT_RATIO_MULTIPLIER, 1);
161
191
    }
162
192
 
163
193
    /**
165
195
     */
166
196
    public void notifyCacheHitOffHeap() {
167
197
        incrementIfStatsEnabled(cacheHitCount, cacheHitOffHeapCount);
 
198
        cacheHitRatio.increment(1 * HIT_RATIO_MULTIPLIER, 1);
168
199
    }
169
200
 
170
201
    /**
172
203
     */
173
204
    public void notifyCacheHitOnDisk() {
174
205
        incrementIfStatsEnabled(cacheHitCount, cacheHitOnDiskCount);
 
206
        cacheHitRatio.increment(1 * HIT_RATIO_MULTIPLIER, 1);
175
207
    }
176
208
 
177
209
    /**
179
211
     */
180
212
    public void notifyCacheMissedWithExpired() {
181
213
        incrementIfStatsEnabled(cacheMissCount, cacheMissExpiredCount);
 
214
        cacheHitRatio.increment(0, 1);
182
215
    }
183
216
 
184
217
    /**
186
219
     */
187
220
    public void notifyCacheMissedWithNotFound() {
188
221
        incrementIfStatsEnabled(cacheMissCount, cacheMissNotFoundCount);
 
222
        cacheHitRatio.increment(0, 1);
189
223
    }
190
224
 
191
225
    /**
192
226
     * {@inheritDoc}
193
227
     */
194
228
    public void notifyCacheMissInMemory() {
195
 
        incrementIfStatsEnabled(cacheMissCount, cacheMissInMemoryCount);
 
229
        incrementIfStatsEnabled(cacheMissInMemoryCount);
196
230
    }
197
231
 
198
232
    /**
199
233
     * {@inheritDoc}
200
234
     */
201
235
    public void notifyCacheMissOffHeap() {
202
 
        incrementIfStatsEnabled(cacheMissCount, cacheMissOffHeapCount);
 
236
        incrementIfStatsEnabled(cacheMissOffHeapCount);
203
237
    }
204
238
 
205
239
    /**
206
240
     * {@inheritDoc}
207
241
     */
208
242
    public void notifyCacheMissOnDisk() {
209
 
        incrementIfStatsEnabled(cacheMissCount, cacheMissOnDiskCount);
 
243
        incrementIfStatsEnabled(cacheMissOnDiskCount);
210
244
    }
211
245
 
212
246
    /**
248
282
     * {@inheritDoc}
249
283
     */
250
284
    public void notifyTimeTakenForGet(long millis) {
 
285
        /**/
 
286
    }
 
287
 
 
288
    @Override
 
289
    public void notifyGetTimeNanos(long nanos) {
251
290
        if (!sampledStatisticsEnabled.get()) {
252
291
            return;
253
292
        }
254
 
        averageGetTime.increment(millis, 1);
 
293
        averageGetTimeNanos.increment(nanos, 1);
 
294
        averageGetTime.increment(nanos / NANOS_PER_MILLI, 1);
255
295
    }
256
296
 
257
297
    /**
304
344
        cacheMissOnDiskCount.getAndReset();
305
345
        cacheMissExpiredCount.getAndReset();
306
346
        cacheMissNotFoundCount.getAndReset();
 
347
        cacheHitRatio.getAndReset();
307
348
        cacheElementEvictedCount.getAndReset();
308
349
        cacheElementRemoved.getAndReset();
309
350
        cacheElementExpired.getAndReset();
312
353
        cacheXaCommitCount.getAndReset();
313
354
        cacheXaRollbackCount.getAndReset();
314
355
        averageGetTime.getAndReset();
 
356
        averageGetTimeNanos.getAndReset();
315
357
    }
316
358
 
317
359
    /**
331
373
    /**
332
374
     * {@inheritDoc}
333
375
     */
 
376
    public long getAverageGetTimeNanosMostRecentSample() {
 
377
        return averageGetTimeNanos.getMostRecentSample().getCounterValue();
 
378
    }
 
379
 
 
380
    /**
 
381
     * {@inheritDoc}
 
382
     */
334
383
    public long getCacheElementEvictedMostRecentSample() {
335
384
        return cacheElementEvictedCount.getMostRecentSample().getCounterValue();
336
385
    }
401
450
    /**
402
451
     * {@inheritDoc}
403
452
     */
 
453
    public int getCacheHitRatioMostRecentSample() {
 
454
        return (int) cacheHitRatio.getMostRecentSample().getCounterValue();
 
455
    }
 
456
 
 
457
    /**
 
458
     * {@inheritDoc}
 
459
     */
404
460
    public long getCacheElementExpiredMostRecentSample() {
405
461
        return cacheElementExpired.getMostRecentSample().getCounterValue();
406
462
    }
436
492
    /**
437
493
     * {@inheritDoc}
438
494
     */
 
495
    @Override
 
496
    public SampledCounter getCacheHitSample() {
 
497
        return cacheHitCount;
 
498
    }
 
499
 
 
500
    /**
 
501
     * {@inheritDoc}
 
502
     */
 
503
    @Override
 
504
    public SampledCounter getCacheHitInMemorySample() {
 
505
        return cacheHitInMemoryCount;
 
506
    }
 
507
 
 
508
    /**
 
509
     * {@inheritDoc}
 
510
     */
 
511
    @Override
 
512
    public SampledCounter getCacheHitOffHeapSample() {
 
513
        return cacheHitOffHeapCount;
 
514
    }
 
515
 
 
516
    /**
 
517
     * {@inheritDoc}
 
518
     */
 
519
    @Override
 
520
    public SampledCounter getCacheHitOnDiskSample() {
 
521
        return cacheHitOnDiskCount;
 
522
    }
 
523
 
 
524
    /**
 
525
     * {@inheritDoc}
 
526
     */
 
527
    @Override
 
528
    public SampledCounter getCacheMissSample() {
 
529
        return cacheMissCount;
 
530
    }
 
531
 
 
532
    /**
 
533
     * {@inheritDoc}
 
534
     */
 
535
    @Override
 
536
    public SampledCounter getCacheMissInMemorySample() {
 
537
        return cacheMissInMemoryCount;
 
538
    }
 
539
 
 
540
    /**
 
541
     * {@inheritDoc}
 
542
     */
 
543
    @Override
 
544
    public SampledCounter getCacheMissOffHeapSample() {
 
545
        return cacheMissOffHeapCount;
 
546
    }
 
547
 
 
548
    /**
 
549
     * {@inheritDoc}
 
550
     */
 
551
    @Override
 
552
    public SampledCounter getCacheMissOnDiskSample() {
 
553
        return cacheMissOnDiskCount;
 
554
    }
 
555
 
 
556
    /**
 
557
     * {@inheritDoc}
 
558
     */
 
559
    @Override
 
560
    public SampledCounter getCacheMissExpiredSample() {
 
561
        return cacheMissExpiredCount;
 
562
    }
 
563
 
 
564
    /**
 
565
     * {@inheritDoc}
 
566
     */
 
567
    @Override
 
568
    public SampledCounter getCacheMissNotFoundSample() {
 
569
        return cacheMissNotFoundCount;
 
570
    }
 
571
 
 
572
    /**
 
573
     * {@inheritDoc}
 
574
     */
 
575
    public SampledCounter getCacheHitRatioSample() {
 
576
        return cacheHitRatio;
 
577
    }
 
578
 
 
579
    /**
 
580
     * {@inheritDoc}
 
581
     */
 
582
    @Override
 
583
    public SampledCounter getCacheElementEvictedSample() {
 
584
        return cacheElementEvictedCount;
 
585
    }
 
586
 
 
587
    /**
 
588
     * {@inheritDoc}
 
589
     */
 
590
    @Override
 
591
    public SampledCounter getCacheElementRemovedSample() {
 
592
        return cacheElementRemoved;
 
593
    }
 
594
 
 
595
    /**
 
596
     * {@inheritDoc}
 
597
     */
 
598
    @Override
 
599
    public SampledCounter getCacheElementExpiredSample() {
 
600
        return cacheElementExpired;
 
601
    }
 
602
 
 
603
    /**
 
604
     * {@inheritDoc}
 
605
     */
 
606
    @Override
 
607
    public SampledCounter getCacheElementPutSample() {
 
608
        return cacheElementPut;
 
609
    }
 
610
 
 
611
    /**
 
612
     * {@inheritDoc}
 
613
     */
 
614
    @Override
 
615
    public SampledCounter getCacheElementUpdatedSample() {
 
616
        return cacheElementUpdated;
 
617
    }
 
618
 
 
619
    /**
 
620
     * {@inheritDoc}
 
621
     */
 
622
    @Override
 
623
    public SampledRateCounter getAverageGetTimeSample() {
 
624
        return averageGetTime;
 
625
    }
 
626
 
 
627
    @Override
 
628
    public SampledRateCounter getAverageGetTimeNanosSample() {
 
629
        return averageGetTimeNanos;
 
630
    }
 
631
 
 
632
    /**
 
633
     * {@inheritDoc}
 
634
     */
 
635
    @Override
 
636
    public SampledRateCounter getAverageSearchTimeSample() {
 
637
        return averageSearchTime;
 
638
    }
 
639
 
 
640
    /**
 
641
     * {@inheritDoc}
 
642
     */
 
643
    @Override
 
644
    public SampledCounter getSearchesPerSecondSample() {
 
645
        return cacheSearchCount;
 
646
    }
 
647
 
 
648
    /**
 
649
     * {@inheritDoc}
 
650
     */
439
651
    public String getStatisticsAccuracyDescription() {
440
652
        int value = statisticsAccuracy.get();
441
653
        if (value == 0) {
471
683
    /**
472
684
     * {@inheritDoc}
473
685
     */
 
686
    @Override
 
687
    public SampledCounter getCacheXaCommitsSample() {
 
688
        return cacheXaCommitCount;
 
689
    }
 
690
 
 
691
    /**
 
692
     * {@inheritDoc}
 
693
     */
 
694
    @Override
 
695
    public SampledCounter getCacheXaRollbacksSample() {
 
696
        return cacheXaRollbackCount;
 
697
    }
 
698
 
 
699
    /**
 
700
     * {@inheritDoc}
 
701
     */
474
702
    public void notifyCacheSearch(long executeTime) {
475
703
        this.cacheSearchCount.increment();
476
704
        this.averageSearchTime.increment(executeTime, 1);