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

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/statistics/LiveCacheStatisticsImpl.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.
42
42
    private static final Logger LOG = LoggerFactory.getLogger(LiveCacheStatisticsImpl.class.getName());
43
43
 
44
44
    private static final int MIN_MAX_DEFAULT_VALUE = -1;
 
45
    private static final int MILLIS_PER_SECOND = 1000;
 
46
    private static final int NANOS_PER_MILLI = MILLIS_PER_SECOND * MILLIS_PER_SECOND;
 
47
    private static final int HIT_RATIO_MULTIPLIER = 100;
45
48
 
46
49
    private final AtomicBoolean statisticsEnabled = new AtomicBoolean(true);
47
50
    private final AtomicLong cacheHitInMemoryCount = new AtomicLong();
53
56
    private final AtomicLong cacheMissOnDiskCount = new AtomicLong();
54
57
    private final AtomicLong cacheMissExpired = new AtomicLong();
55
58
    private final AtomicLong cacheElementEvictedCount = new AtomicLong();
56
 
    private final AtomicLong totalGetTimeTakenMillis = new AtomicLong();
 
59
    private final AtomicLong totalGetTimeTakenNanos = new AtomicLong();
57
60
    private final AtomicLong cacheElementRemoved = new AtomicLong();
58
61
    private final AtomicLong cacheElementExpired = new AtomicLong();
59
62
    private final AtomicLong cacheElementPut = new AtomicLong();
60
63
    private final AtomicLong cacheElementUpdated = new AtomicLong();
61
64
    private final AtomicInteger statisticsAccuracy = new AtomicInteger();
62
 
    private final AtomicLong minGetTimeMillis = new AtomicLong(MIN_MAX_DEFAULT_VALUE);
63
 
    private final AtomicLong maxGetTimeMillis = new AtomicLong(MIN_MAX_DEFAULT_VALUE);
 
65
    private final AtomicLong minGetTimeNanos = new AtomicLong(MIN_MAX_DEFAULT_VALUE);
 
66
    private final AtomicLong maxGetTimeNanos = new AtomicLong(MIN_MAX_DEFAULT_VALUE);
64
67
    private final AtomicLong xaCommitCount = new AtomicLong();
65
68
    private final AtomicLong xaRollbackCount = new AtomicLong();
 
69
    private final AtomicLong xaRecoveredCount = new AtomicLong();
66
70
 
67
71
    private final List<CacheUsageListener> listeners = new CopyOnWriteArrayList<CacheUsageListener>();
68
72
 
90
94
        cacheMissOffHeapCount.set(0);
91
95
        cacheMissOnDiskCount.set(0);
92
96
        cacheElementEvictedCount.set(0);
93
 
        totalGetTimeTakenMillis.set(0);
 
97
        totalGetTimeTakenNanos.set(0);
94
98
        cacheElementRemoved.set(0);
95
99
        cacheElementExpired.set(0);
96
100
        cacheElementPut.set(0);
97
101
        cacheElementUpdated.set(0);
98
 
        minGetTimeMillis.set(MIN_MAX_DEFAULT_VALUE);
99
 
        maxGetTimeMillis.set(MIN_MAX_DEFAULT_VALUE);
 
102
        minGetTimeNanos.set(MIN_MAX_DEFAULT_VALUE);
 
103
        maxGetTimeNanos.set(MIN_MAX_DEFAULT_VALUE);
100
104
        xaCommitCount.set(0);
101
105
        xaRollbackCount.set(0);
102
106
        for (CacheUsageListener l : listeners) {
133
137
    /**
134
138
     * {@inheritDoc}
135
139
     */
 
140
    public void xaRecovered(int count) {
 
141
        if (!statisticsEnabled.get()) {
 
142
            return;
 
143
        }
 
144
        xaRecoveredCount.addAndGet(count);
 
145
    }
 
146
 
 
147
    /**
 
148
     * {@inheritDoc}
 
149
     */
136
150
    public boolean isStatisticsEnabled() {
137
151
        return statisticsEnabled.get();
138
152
    }
153
167
    /**
154
168
     * {@inheritDoc}
155
169
     */
 
170
    public void addGetTimeNanos(long nanos) {
 
171
        if (!statisticsEnabled.get()) {
 
172
            return;
 
173
        }
 
174
        totalGetTimeTakenNanos.addAndGet(nanos);
 
175
        for (CacheUsageListener l : listeners) {
 
176
            l.notifyGetTimeNanos(nanos);
 
177
            l.notifyTimeTakenForGet(nanos / NANOS_PER_MILLI);
 
178
        }
 
179
        if (minGetTimeNanos.get() == MIN_MAX_DEFAULT_VALUE || (nanos < minGetTimeNanos.get())) {
 
180
            minGetTimeNanos.set(nanos);
 
181
        }
 
182
        if (maxGetTimeNanos.get() == MIN_MAX_DEFAULT_VALUE || (nanos > maxGetTimeNanos.get() && nanos > 0)) {
 
183
            maxGetTimeNanos.set(nanos);
 
184
        }
 
185
    }
 
186
 
 
187
    /**
 
188
     * {@inheritDoc}
 
189
     */
156
190
    public void addGetTimeMillis(long millis) {
157
191
        if (!statisticsEnabled.get()) {
158
192
            return;
159
193
        }
160
 
        totalGetTimeTakenMillis.addAndGet(millis);
 
194
        totalGetTimeTakenNanos.addAndGet(millis);
161
195
        for (CacheUsageListener l : listeners) {
162
196
            l.notifyTimeTakenForGet(millis);
163
197
        }
164
 
        if (minGetTimeMillis.get() == MIN_MAX_DEFAULT_VALUE || (millis < minGetTimeMillis.get() /*&& millis > 0*/)) {
165
 
            minGetTimeMillis.set(millis);
 
198
        if (minGetTimeNanos.get() == MIN_MAX_DEFAULT_VALUE || (millis < minGetTimeNanos.get() /*&& millis > 0*/)) {
 
199
            minGetTimeNanos.set(millis);
166
200
        }
167
 
        if (maxGetTimeMillis.get() == MIN_MAX_DEFAULT_VALUE || (millis > maxGetTimeMillis.get() && millis > 0)) {
168
 
            maxGetTimeMillis.set(millis);
 
201
        if (maxGetTimeNanos.get() == MIN_MAX_DEFAULT_VALUE || (millis > maxGetTimeNanos.get() && millis > 0)) {
 
202
            maxGetTimeNanos.set(millis);
169
203
        }
170
204
    }
171
205
 
386
420
     * {@inheritDoc}
387
421
     */
388
422
    public float getAverageGetTimeMillis() {
 
423
        return getAverageGetTimeNanos() / (float) NANOS_PER_MILLI;
 
424
    }
 
425
 
 
426
    /**
 
427
     * {@inheritDoc}
 
428
     */
 
429
    public long getAverageGetTimeNanos() {
389
430
        long accessCount = getCacheHitCount() + getCacheMissCount();
390
431
        if (accessCount == 0) {
391
 
            return 0f;
 
432
            return 0;
392
433
        }
393
 
        return (float) totalGetTimeTakenMillis.get() / accessCount;
 
434
        return totalGetTimeTakenNanos.get() / accessCount;
394
435
    }
395
436
 
396
437
    /**
457
498
    /**
458
499
     * {@inheritDoc}
459
500
     */
 
501
    public int getCacheHitRatio() {
 
502
        long hits = getCacheHitCount();
 
503
        long accesses = hits + getCacheMissCount();
 
504
 
 
505
        return (int) (accesses == 0 ? 0 : ((hits / (double) accesses) * HIT_RATIO_MULTIPLIER));
 
506
    }
 
507
 
 
508
    /**
 
509
     * {@inheritDoc}
 
510
     */
460
511
    public long getEvictedCount() {
461
512
        return cacheElementEvictedCount.get();
462
513
    }
642
693
     * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMaxGetTimeMillis()
643
694
     */
644
695
    public long getMaxGetTimeMillis() {
645
 
        return maxGetTimeMillis.get();
 
696
        return maxGetTimeNanos.get() / NANOS_PER_MILLI;
 
697
    }
 
698
 
 
699
    /**
 
700
     * {@inheritDoc}
 
701
     *
 
702
     * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMinGetTimeMillis()
 
703
     */
 
704
    public long getMinGetTimeMillis() {
 
705
        return minGetTimeNanos.get() / NANOS_PER_MILLI;
 
706
    }
 
707
 
 
708
    /**
 
709
     * {@inheritDoc}
 
710
     *
 
711
     * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMaxGetTimeNanos()
 
712
     */
 
713
    public long getMaxGetTimeNanos() {
 
714
        return maxGetTimeNanos.get();
 
715
    }
 
716
 
 
717
    /**
 
718
     * {@inheritDoc}
 
719
     *
 
720
     * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMinGetTimeNanos()
 
721
     */
 
722
    public long getMinGetTimeNanos() {
 
723
        return minGetTimeNanos.get();
646
724
    }
647
725
 
648
726
    /**
665
743
 
666
744
    /**
667
745
     * {@inheritDoc}
 
746
     *
 
747
     * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaRecoveredCount()
 
748
     */
 
749
    public long getXaRecoveredCount() {
 
750
        return xaRecoveredCount.get();
 
751
    }
 
752
 
 
753
    /**
 
754
     * {@inheritDoc}
668
755
     */
669
756
    public long getWriterQueueLength() {
670
 
        long length = -1;
671
757
        CacheWriterManager writerManager = cache.getWriterManager();
672
758
        if (writerManager instanceof WriteBehindManager) {
673
 
            length = ((WriteBehindManager)writerManager).getQueueSize();
 
759
            return ((WriteBehindManager)writerManager).getQueueSize();
674
760
        }
675
 
        return length;
676
 
    }
677
 
 
678
 
    /**
679
 
     * {@inheritDoc}
680
 
     *
681
 
     * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMinGetTimeMillis()
682
 
     */
683
 
    public long getMinGetTimeMillis() {
684
 
        return minGetTimeMillis.get();
 
761
        return 0;
685
762
    }
686
763
}