-
转载请注明出处哈:http://carlosfu.iteye.com/blog/2237511
- Ehcache默认启动jmx,对Ehcache的使用状态、配置和统计信息进行监控。
一、使用状态:
-
Name: 缓存名
-
Status: 状态
-
TerracottaClustered: 是否是bigmemory集群模式(bigmemory max)
二、配置信息:
(对应到ehcache.xml中的自定义配置和默认配置)
-
DiskExpiryThreadIntervalSeconds: 每xx秒检查硬盘的数据是否有过期的
-
DiskPersistent: 是否做硬盘持久化
-
DiskSpoolBufferSizedMB: 硬盘缓存区尺寸(单位:MB)
-
Eternal: 对象是否永久,如果是true的话,TimeToIdleSeconds和TimeToLiveSeconds的配置都会无效。
-
LoggingEnabled: 是否启动日志
-
MaxBytesLocalDisk: 硬盘最大存储量(单位是字节)
-
MaxBytesLocalHeap: 堆内最大存储量(单位是字节)
-
MaxMemoryOffHeapInBytes: 设置对外内存的最大尺寸(单位是字节)
-
MaxBytesLocalOffHeap: 堆外最大存储量(单位是字节)(8和9貌似是一样的)
-
MaxElementsInMemory: 内存最大Elements个数
-
MaxElementsOnDisk: 硬盘最大Elements个数
-
MaxEntriesLocalDisk: 硬盘最大Entries个数
-
MaxEntriesLocalHeap: 堆内最大Entries个数
-
TimeToIdleSeconds: 对象最大空闲时间(超过空闲时间且超过设置的Max,可能参与到对象的删除策略中)
-
TimeToLiveSeconds: 对象的最大存活时间,也就是对象的过期时间。
-
OverflowToDisk: OffHeap如果满了,是否flow到硬盘。(默认是false, 如果为false,所有和硬盘相关的配置都不生效)
-
OverflowToOffHeap: Heap如果满了,是否flow到OffHeap。
-
Name: 缓存名
-
TerracottaClustered: 是否是bigmemory集群模式(bigmemory max)
-
MemoryStoreEvictionPolicy: 堆内Eviction策略(默认是LRU)
三、统计信息:
1. AssociatedCachedName: cache的名称,对应ehcache.xml中的<cache name="mobilServiceOffHeap".../>
2. CacheHitPercentage: 总缓存命中率
3. CacheHits: 总缓存命中数
4. CacheMissPercentage: 总缓存丢失率
5. CacheMisses: 总缓存丢失数
6. DiskStoreObjectCount: 硬盘中存储的对象个数 (配置中DiskPersistent必须为true)
7. InMemoryHitPercentage: 堆内命中率
8. InMemoryHits: 堆内命中数
9. InMemoryMisses: 堆内丢失数
11. MemoryStoreObjectCount: 堆内存储对象个数
11. OffHeapStoreObjectCount: 堆外存储对象个数
12. OffHeapHitPercentage: 堆外命中率
13. OffHeapHits: 堆外命中数
14. OffHeapMisses: 堆外丢失数
15. OnDiskHitPercentage: 堆外命中率
16. OnDiskHits: 堆外命中数
17. OnDiskMisses: 堆外丢失数
18. ObjectCount: 总对象个数
如果当前cache是offheapCache,那么总对象数 = OffHeapStoreObjectCount + DiskStoreObjectCount (可能InMemory是Offheap的热点数据,认为是一个对象)
如果当前cache是heapCache,那么总对象数 = InMemoryStoreObjectCount + DiskStoreObjectCount
19. WriterMaxQueueSize: the maximum size of the write-behind queue (应该是写硬盘的线程,用到的队列)
20. WriteQueueLength: the size of the write-behind queue(应该是写硬盘的线程,用到的队列)
四、自定义统计信息
1. 除了默认的统计信息,可以调用ehcache api获取更加全面的统计信息,实现自己来定制统计。
package com.sohu.tv.mobil.common.jmx; import java.util.List; public interface EhcacheExtendWatcherMBean { /** * 获取延迟结果 * * @return */ List<String> getGlobalResult(); /** * 获取剔除数量 * * @return */ long getEvictedCount(); /** * 获取超时数量 * * @return */ long getExpiredCount(); /** * 获取未命中统计 * * @return */ List<String> getMissStatisticsMap(); }
package com.sohu.tv.mobil.common.jmx.impl; import com.sohu.tv.mobil.common.jmx.EhcacheExtendWatcherMBean; import net.sf.ehcache.Ehcache; import net.sf.ehcache.statistics.StatisticsGateway; import net.sf.ehcache.statistics.extended.ExtendedStatistics; import java.util.ArrayList; import java.util.List; public class EhcacheExtendWatcher implements EhcacheExtendWatcherMBean { private Ehcache ehcache; @Override public List<String> getGlobalResult() { ExtendedStatistics extendedStatistics = ehcache.getStatistics().getExtended(); ExtendedStatistics.Result getResult = extendedStatistics.allGet(); ExtendedStatistics.Result putResult = extendedStatistics.allPut(); ExtendedStatistics.Result missResult = extendedStatistics.allMiss(); List<String> resultList = new ArrayList<String>(); String getStr = "allGet:count=" + getResult.count().value() + ";rate=" + getResult.rate().value() + ";latency:average=" + getResult.latency().average().value() + ";minimum=" + getResult.latency().minimum().value() + ";maximum=" + getResult.latency() .maximum().value(); String putStr = "allPut:count=" + putResult.count().value() + ";rate=" + putResult.rate().value() + ";latency:average=" + putResult.latency().average().value() + ";minimum=" + putResult.latency().minimum().value() + ";maximum=" + putResult.latency() .maximum().value(); String missStr = "allMiss:count=" + missResult.count().value() + ";rate=" + missResult.rate().value() + ";latency:average=" + missResult.latency().average().value() + ";minimum=" + missResult.latency().minimum().value() + ";maximum=" + missResult.latency() .maximum().value(); resultList.add(getStr); resultList.add(putStr); resultList.add(missStr); return resultList; } @Override public long getEvictedCount() { StatisticsGateway statisticsGateway = ehcache.getStatistics(); return statisticsGateway.cacheEvictedCount(); } @Override public long getExpiredCount() { StatisticsGateway statisticsGateway = ehcache.getStatistics(); return statisticsGateway.cacheExpiredCount(); } @Override public List<String> getMissStatisticsMap() { StatisticsGateway statisticsGateway = ehcache.getStatistics(); ExtendedStatistics.Result missResult = statisticsGateway.cacheMissOperation(); ExtendedStatistics.Result missExpiredResult = statisticsGateway.cacheMissExpiredOperation(); ExtendedStatistics.Result missMissNotFoundResult = statisticsGateway.cacheMissNotFoundOperation(); String missResultStr = "missResult:count=" + missResult.count().value() + ";rate=" + missResult.rate().value() + ";latency:average=" + missResult.latency().average().value() + ";minimum=" + missResult.latency().minimum().value() + ";maximum=" + missResult.latency() .maximum().value(); String missExpiredResultStr = "missExpiredResult:count=" + missExpiredResult.count().value() + ";rate=" + missExpiredResult.rate().value() + ";latency:average=" + missExpiredResult .latency().average().value() + ";minimum=" + missExpiredResult.latency().minimum() .value() + ";maximum=" + missExpiredResult.latency().maximum().value(); String missMissNotFoundResultStr = "missMissNotFoundResult:count=" + missMissNotFoundResult.count().value() + ";rate=" + missMissNotFoundResult.rate().value() + ";latency:average=" + missMissNotFoundResult.latency().average().value() + ";minimum=" + missMissNotFoundResult.latency().minimum().value() + ";maximum=" + missMissNotFoundResult.latency().maximum().value(); List<String> resultList = new ArrayList<String>(); resultList.add(missResultStr); resultList.add(missExpiredResultStr); resultList.add(missMissNotFoundResultStr); return resultList; } public void setEhcache(Ehcache ehcache) { this.ehcache = ehcache; } }
2. 实现效果:
3. 重要统计说明:
EvictedCount: 剔除个数(内存满之后)
ExpiredCount: 过期个数 (如果设置了单个KEY的过期时间或者全局的TimeToLive就会出现)
GlobalResult: 包含各个命令的调用次数,各种响应时间,QPS(rate)