30
27
import net.sf.ehcache.util.counter.sampled.SampledRateCounter;
31
28
import net.sf.ehcache.util.counter.sampled.SampledRateCounterConfig;
30
import java.util.concurrent.atomic.AtomicBoolean;
31
import java.util.concurrent.atomic.AtomicInteger;
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
36
37
* these to update the stats
39
40
* @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
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;
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);
52
56
private volatile CounterManager counterManager;
53
57
private final SampledCounter cacheHitCount;
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;
74
80
private final AtomicBoolean sampledStatisticsEnabled;
75
81
private final AtomicInteger statisticsAccuracy;
84
* The default constructor
88
public SampledCacheStatisticsImpl(FailSafeTimer timer) {
89
this(timer, DEFAULT_SAMPLED_COUNTER_CONFIG, DEFAULT_RATE_COUNTER_CONFIG, DEFAULT_AVG_SEARCH_COUNTER_CONFIG);
96
public SampledCacheStatisticsImpl(FailSafeTimer timer, SampledCounterConfig config) {
97
this(timer, config, DEFAULT_RATE_COUNTER_CONFIG, DEFAULT_AVG_SEARCH_COUNTER_CONFIG);
78
101
* Constructor that accepts a timer which will be used to schedule the
79
102
* sampled counters
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);
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);
106
133
this.sampledStatisticsEnabled = new AtomicBoolean(true);
107
134
this.statisticsAccuracy = new AtomicInteger(Statistics.STATISTICS_ACCURACY_BEST_EFFORT);
187
220
public void notifyCacheMissedWithNotFound() {
188
221
incrementIfStatsEnabled(cacheMissCount, cacheMissNotFoundCount);
222
cacheHitRatio.increment(0, 1);
194
228
public void notifyCacheMissInMemory() {
195
incrementIfStatsEnabled(cacheMissCount, cacheMissInMemoryCount);
229
incrementIfStatsEnabled(cacheMissInMemoryCount);
201
235
public void notifyCacheMissOffHeap() {
202
incrementIfStatsEnabled(cacheMissCount, cacheMissOffHeapCount);
236
incrementIfStatsEnabled(cacheMissOffHeapCount);
208
242
public void notifyCacheMissOnDisk() {
209
incrementIfStatsEnabled(cacheMissCount, cacheMissOnDiskCount);
243
incrementIfStatsEnabled(cacheMissOnDiskCount);
496
public SampledCounter getCacheHitSample() {
497
return cacheHitCount;
504
public SampledCounter getCacheHitInMemorySample() {
505
return cacheHitInMemoryCount;
512
public SampledCounter getCacheHitOffHeapSample() {
513
return cacheHitOffHeapCount;
520
public SampledCounter getCacheHitOnDiskSample() {
521
return cacheHitOnDiskCount;
528
public SampledCounter getCacheMissSample() {
529
return cacheMissCount;
536
public SampledCounter getCacheMissInMemorySample() {
537
return cacheMissInMemoryCount;
544
public SampledCounter getCacheMissOffHeapSample() {
545
return cacheMissOffHeapCount;
552
public SampledCounter getCacheMissOnDiskSample() {
553
return cacheMissOnDiskCount;
560
public SampledCounter getCacheMissExpiredSample() {
561
return cacheMissExpiredCount;
568
public SampledCounter getCacheMissNotFoundSample() {
569
return cacheMissNotFoundCount;
575
public SampledCounter getCacheHitRatioSample() {
576
return cacheHitRatio;
583
public SampledCounter getCacheElementEvictedSample() {
584
return cacheElementEvictedCount;
591
public SampledCounter getCacheElementRemovedSample() {
592
return cacheElementRemoved;
599
public SampledCounter getCacheElementExpiredSample() {
600
return cacheElementExpired;
607
public SampledCounter getCacheElementPutSample() {
608
return cacheElementPut;
615
public SampledCounter getCacheElementUpdatedSample() {
616
return cacheElementUpdated;
623
public SampledRateCounter getAverageGetTimeSample() {
624
return averageGetTime;
628
public SampledRateCounter getAverageGetTimeNanosSample() {
629
return averageGetTimeNanos;
636
public SampledRateCounter getAverageSearchTimeSample() {
637
return averageSearchTime;
644
public SampledCounter getSearchesPerSecondSample() {
645
return cacheSearchCount;
439
651
public String getStatisticsAccuracyDescription() {
440
652
int value = statisticsAccuracy.get();
441
653
if (value == 0) {