/** * Gets the size of heap consumed by items stored in the cache. * * @return Memory size. */ @Override public long getSize() { final StatisticsGateway statistics = cache.getStatistics(); // Store component sizes on each call to avoid recalculating // sizes in other methods that need them if (useBytes) { diskSize = statistics.getLocalDiskSizeInBytes(); heapSize = statistics.getLocalHeapSizeInBytes(); } else { diskSize = cache.getDiskStoreSize(); heapSize = cache.getMemoryStoreSize(); } offHeapSize = statistics.getLocalOffHeapSizeInBytes(); return heapSize; }
/** * Gets the size of heap consumed by items stored in the cache. * * @return Memory size. */ @Override public long getSize() { final StatisticsGateway statistics = this.cache.getStatistics(); // Store component sizes on each call to avoid recalculating // sizes in other methods that need them if (this.useBytes) { this.diskSize = statistics.getLocalDiskSizeInBytes(); this.heapSize = statistics.getLocalHeapSizeInBytes(); } else { this.diskSize = this.cache.getDiskStoreSize(); this.heapSize = this.cache.getMemoryStoreSize(); } this.offHeapSize = statistics.getLocalOffHeapSizeInBytes(); return this.heapSize; }
private void requestMetrics(MeterRegistry registry) { FunctionCounter.builder(name + ".requests", stats, StatisticsGateway::cacheMissExpiredCount) .tags(tags).tags("result", "miss", "reason", "expired") .description("The number of times cache lookup methods have not returned a value, due to expiry") .register(registry); FunctionCounter.builder(name + ".requests", stats, StatisticsGateway::cacheMissNotFoundCount) .tags(tags).tags("result", "miss", "reason", "notFound") .description("The number of times cache lookup methods have not returned a value, because the key was not found") .register(registry); FunctionCounter.builder(name + ".requests", stats, StatisticsGateway::cacheHitCount) .tags(tags).tags("result", "hit") .description("The number of times cache lookup methods have returned a cached value.") .register(registry); }
private void commitTransactionMetrics(MeterRegistry registry) { FunctionCounter.builder(name + ".xa.commits", stats, StatisticsGateway::xaCommitReadOnlyCount) .tags(tags).tags("result", "readOnly") .description("Transaction commits that had a read-only result") .register(registry); FunctionCounter.builder(name + ".xa.commits", stats, StatisticsGateway::xaCommitExceptionCount) .tags(tags).tags("result", "exception") .description("Transaction commits that failed") .register(registry); FunctionCounter.builder(name + ".xa.commits", stats, StatisticsGateway::xaCommitCommittedCount) .tags(tags).tags("result", "committed") .description("Transaction commits that failed") .register(registry); }
/** * Return the information of given cache. * * @param name * the cache's name to display. * * @return the cache's configuration. */ @GET @Path("{name:[\\w\\-]+}") public CacheStatistics getCache(@PathParam("name") final String name) { final CacheStatistics result = new CacheStatistics(); final Cache cache = CacheManager.getInstance().getCache(name); final StatisticsGateway statistics = cache.getStatistics(); result.setId(cache.getGuid()); result.setSize(cache.getKeys().size()); result.setName(cache.getName()); result.setHitCount(statistics.cacheHitCount()); result.setMissCount(statistics.cacheMissCount()); result.setBytes(statistics.getLocalHeapSizeInBytes()); result.setOffHeapBytes(statistics.getLocalOffHeapSizeInBytes()); return result; }
@Override public CacheStatistics getCacheStatistics(CacheManager cacheManager, EhCacheCache cache) { DefaultCacheStatistics statistics = new DefaultCacheStatistics(); StatisticsGateway ehCacheStatistics = cache.getNativeCache().getStatistics(); statistics.setSize(ehCacheStatistics.getSize()); double hitRatio = cacheHitRatio(ehCacheStatistics); if (!Double.isNaN(hitRatio)) { // ratio is calculated 'racily' and can drift marginally above unity, // so we cap it here double sanitizedHitRatio = (hitRatio > 1 ? 1 : hitRatio); statistics.setHitRatio(sanitizedHitRatio); statistics.setMissRatio(1 - sanitizedHitRatio); } return statistics; }
@Override public CacheStatistics getCacheStatistics(CacheManager cacheManager, EhCacheCache cache) { DefaultCacheStatistics statistics = new DefaultCacheStatistics(); StatisticsGateway ehCacheStatistics = cache.getNativeCache().getStatistics(); statistics.setSize(ehCacheStatistics.getSize()); Double hitRatio = ehCacheStatistics.cacheHitRatio(); if (!hitRatio.isNaN()) { // ratio is calculated 'racily' and can drift marginally above unity, // so we cap it here double sanitizedHitRatio = (hitRatio > 1 ? 1 : hitRatio); statistics.setHitRatio(sanitizedHitRatio); statistics.setMissRatio(1 - sanitizedHitRatio); } return statistics; }
private void rollbackTransactionMetrics(MeterRegistry registry) { FunctionCounter.builder(name + ".xa.rollbacks", stats, StatisticsGateway::xaRollbackExceptionCount) .tags(tags).tags("result", "exception") .description("Transaction rollbacks that failed") .register(registry); FunctionCounter.builder(name + ".xa.rollbacks", stats, StatisticsGateway::xaRollbackSuccessCount) .tags(tags).tags("result", "success") .description("Transaction rollbacks that failed") .register(registry); }
private void recoveryTransactionMetrics(MeterRegistry registry) { FunctionCounter.builder(name + ".xa.recoveries", stats, StatisticsGateway::xaRecoveryNothingCount) .tags(tags).tags("result", "nothing") .description("Recovery transactions that recovered nothing") .register(registry); FunctionCounter.builder(name + ".xa.recoveries", stats, StatisticsGateway::xaRecoveryRecoveredCount) .tags(tags).tags("result", "success") .description("Successful recovery transaction") .register(registry); }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] cacheNames = cacheManager.getCacheNames(); /* * Gather the stats */ JSONObject allStats = new JSONObject(); for (String name : cacheNames) { Cache cache = cacheManager.getCache(name); JSONObject cacheStats = new JSONObject(); StatisticsGateway statisticsGateway = cache.getStatistics(); cacheStats.put("CacheSize", statisticsGateway.getSize()); cacheStats.put("HitCount", statisticsGateway.cacheHitCount()); cacheStats.put("MissCount", statisticsGateway.cacheMissCount()); cacheStats.put("LocalHeapSize", statisticsGateway.getLocalHeapSize()); cacheStats.put("LocalHeapHitCount", statisticsGateway.localHeapHitCount()); cacheStats.put("LocalHeapMissCount", statisticsGateway.localHeapMissCount()); cacheStats.put("LocalDiskHitCount", statisticsGateway.localDiskHitCount()); cacheStats.put("LocalDiskMissCount", statisticsGateway.localDiskMissCount()); allStats.put(name, cacheStats); } response.setContentType("application/json"); PrintWriter out = response.getWriter(); out.print(allStats.toString()); out.flush(); }
private static String getStats(Ehcache c) { StatisticsGateway sg = c.getStatistics(); String s = "[" + " Size:" + sg.getSize() + " Expired:" + sg.cacheExpiredCount() + " Evicted:" + sg.cacheEvictedCount() + " Hits:" + sg.cacheHitCount() + " Hit-Ratio:" + sg.cacheHitRatio() + " Heap-Size:" + sg.getLocalHeapSizeInBytes()/(1024 * 1024) + "MB" + " ]"; return s; }
public void logStats() { for (String name: manager.getCacheNames()) { StatisticsGateway s = manager.getCache(name).getStatistics(); log.debug(String.format("Cache %s: hit/miss = %d/%d (%f), heap = %d (%d bytes), disk = %d (%d bytes)", name, s.cacheHitCount(), s.cacheMissCount(), s.cacheHitRatio(), s.getLocalHeapSize(), s.getLocalHeapSizeInBytes(), s.getLocalDiskSize(), s.getLocalDiskSizeInBytes())); } }
@Override public void bindTo(MeterRegistry registry) { Gauge.builder(name + ".size", stats, StatisticsGateway::getSize) .tags(tags).tags("where", "local") .description("The number of entries held locally in this cache") .register(registry); Gauge.builder(name + ".size", stats, StatisticsGateway::getRemoteSize) .tags(tags).tags("where", "remote") .description("The number of entries held remotely in this cache") .register(registry); FunctionCounter.builder(name + ".evictions", stats, StatisticsGateway::cacheEvictedCount) .tags(tags) .description("Cache evictions") .register(registry); FunctionCounter.builder(name + ".removals", stats, StatisticsGateway::cacheRemoveCount) .tags(tags) .description("Cache removals") .register(registry); FunctionCounter.builder(name + ".puts", stats, StatisticsGateway::cachePutAddedCount) .tags(tags).tags("result", "added") .description("Cache puts resulting in a new key/value pair") .register(registry); FunctionCounter.builder(name + ".puts", stats, StatisticsGateway::cachePutAddedCount) .tags(tags).tags("result", "updated") .description("Cache puts resulting in an updated value") .register(registry); requestMetrics(registry); commitTransactionMetrics(registry); rollbackTransactionMetrics(registry); recoveryTransactionMetrics(registry); Gauge.builder(name + ".local.offheap.size", stats, StatisticsGateway::getLocalOffHeapSize) .tags(tags) .description("Local off-heap size") .baseUnit("bytes") .register(registry); Gauge.builder(name + ".local.heap.size", stats, StatisticsGateway::getLocalHeapSizeInBytes) .tags(tags) .description("Local heap size") .baseUnit("bytes") .register(registry); Gauge.builder(name + ".local.disk.size", stats, StatisticsGateway::getLocalDiskSizeInBytes) .tags(tags) .description("Local disk size") .baseUnit("bytes") .register(registry); }
private double cacheHitRatio(StatisticsGateway stats) { long hitCount = stats.cacheHitCount(); long missCount = stats.cacheMissCount(); return ((double) hitCount) / (hitCount + missCount); }
@Override public StatisticsGateway getStatistics() throws IllegalStateException { init(); return super.getStatistics(); }
private String printStats(CacheManager cacheManager) { String[] cacheNames = cacheManager.getCacheNames(); Runtime runtime = Runtime.getRuntime(); String result="<HTML><head>\n" + "<style>\n" + "#stats\n" + "{\n" + "font-family:\"Trebuchet MS\", Arial, Helvetica, sans-serif;\n" + "width:100%;\n" + "border-collapse:collapse;\n" + "}\n" + "#stats td, #stats th \n" + "{\n" + "font-size:1em;\n" + "border:1px solid #98bf21;\n" + "padding:3px 7px 2px 7px;\n" + "}\n" + "#stats th \n" + "{\n" + "font-size:1.1em;\n" + "text-align:center;\n" + "padding-top:5px;\n" + "padding-bottom:4px;\n" + "background-color:#A7C942;\n" + "color:#ffffff;\n" + "}\n" + "#stats tr.alt td \n" + "{\n" + "color:#000000;\n" + "background-color:#EAF2D3;\n" + "}\n" + "#stats td.number\n" + "{\n" + " text-align:right;\n" + "}\n" + "</style>" + "</head>" + "<BODY><TABLE id=\"stats\"><tr><th>Cache Name</th><th>Size</th><th>Used Memory</th><th>Get Ops</th><th>Hits</th><th>Miss</th><th>Put</th><th>Evicted</th><th>Expired</th></tr>"; boolean alt=false; for (String cacheName : cacheNames) { StatisticsGateway c = cacheManager.getCache(cacheName).getStatistics(); result+=alt?"<tr><td>":"<tr><td class=\"alt\">"; result+=cacheName + "</td><td class=\"number\">" + c.getSize()+"</td><td class=\"number\">"+ ((runtime.totalMemory()-runtime.freeMemory())/(1024*1024))+" MB</td><td class=\"number\">"+ c.cacheGetOperation().count().value()+"</td><td class=\"number\">"+ c.cacheHitCount()+"</td><td class=\"number\">"+ c.cacheMissCount()+"</td><td class=\"number\">"+ c.cachePutCount()+"</td><td class=\"number\">"+ c.cacheEvictedCount()+"</td><td class=\"number\">"+ c.cacheExpiredCount()+"</td></tr>"; // for(Object Key:cacheManager.getCache(cacheName).getKeys()) { // result+=("|"+Key+"|\n"); // } } result+="</TABLE></BODY></HTML>"; return result; }
@Override public void displayStats() { StatisticsGateway s = cache.getStatistics(); LOGGER.info("Stats: size=" + s.getSize() + " localHeap=" + s.getLocalHeapSize() + " entries/" + s.getLocalHeapSizeInBytes() + " bytes localOffHeap=" + s.getLocalOffHeapSize() + " entries/" + s.getLocalOffHeapSizeInBytes() + " bytes"); }