protected void statAdd(int asked, int missed) { this.asked += asked; this.missed += missed; if(log.isDebugEnabled()) { String total = StringUtil.convertMemory(Runtime.getRuntime().totalMemory()); String free = StringUtil.convertMemory(Runtime.getRuntime().freeMemory()); String msg = String.format("cache stat (missed/asked): %s/%s (memory %s/%s [total/free])", this.missed, this.asked, total, free); log.debug(msg); } }
@Override public String getStat() { long mean = totalCached / Math.max(1, numberCached); long stepMean = totalStepCached / Math.max(1, numberStepCached); double variance = M2/Math.max(1, numberCached - 1); double stepVariance = stepM2/Math.max(1, numberStepCached - 1); double standardDeviation = Math.sqrt(variance); double stepStandardDeviation = Math.sqrt(stepVariance); String trick = ""; if (sizeCache < biggestStepCached) { trick = String.format( "TRICK: for better performance you must set memory at least to %s\n", StringUtil.convertMemory(biggestStepCached + 500 * 1024 *1024)); } String result = String.format("%s - \n%s" + "\tmaxMemory: %s, cacheMemory: %s, totalCached: %s, numberStep: %s, numberEntries: %s\n" + "\tStep min: %s, max: %s, mean: %s, standard deviation: %s\n" + "\tEntry min: %s, max: %s, mean: %s, standard deviation: %s\n", trick, getClass().getSimpleName(), StringUtil.convertMemory(maxMemory), StringUtil.convertMemory(sizeCache), StringUtil.convertMemory(totalCached), numberStepCached, numberCached, StringUtil.convertMemory(smallestStepCached), StringUtil.convertMemory(biggestStepCached), StringUtil.convertMemory(stepMean), StringUtil.convertMemory((long)stepStandardDeviation), StringUtil.convertMemory(smallestCached), StringUtil.convertMemory(biggestCached), StringUtil.convertMemory(mean), StringUtil.convertMemory((long)standardDeviation) ); return result; }
/** * resizes the cache if amount is not greater than autorized maxCache * @param amount new size of cache in byte */ public void adjustedCache(long amount){ if (amount == 0) { log.info("Cache size not ajusted because 0 is not acceptable amount"); } else { // on estime le nombre d'entree necessaire pour ce volume de donnees long bytePerStep = totalCached / Math.max(1, numberStepCached); long entriesPerStep = numberCached / Math.max(1, numberStepCached); long initialCapacity = amount * entriesPerStep / Math.max(1, bytePerStep); long stepEquivalent = initialCapacity / Math.max(1, entriesPerStep); // use amount as new size for cache // if this new size is less than maxCache, use it // to keep more memory for JVM sizeCache = Math.min(this.maxCache, amount); CacheBuilder builder = CacheBuilder.newBuilder(); builder.maximumWeight(sizeCache); builder.weigher(isisWeigher); if (initialCapacity > 0) { builder.initialCapacity((int)initialCapacity); } cache = builder.build(); log.info(String.format("Cache size ajusted to %s (equivalent to %s step need)", StringUtil.convertMemory(sizeCache), stepEquivalent)); } }