/** Returns the combined weight of the values in the cache. */ long adjustedWeightedSize() { return Math.max(0, weightedSize()); }
/** * Sets the maximum weighted size of the cache. The caller may need to perform a maintenance cycle * to eagerly evicts entries until the cache shrinks to the appropriate size. */ @GuardedBy("evictionLock") void setMaximum(long maximum) { requireArgument(maximum >= 0); long max = Math.min(maximum, MAXIMUM_CAPACITY); long eden = max - (long) (max * PERCENT_MAIN); long mainProtected = (long) ((max - eden) * PERCENT_MAIN_PROTECTED); lazySetMaximum(max); lazySetEdenMaximum(eden); lazySetMainProtectedMaximum(mainProtected); if ((frequencySketch() != null) && !isWeighted() && (weightedSize() >= (max >>> 1))) { // Lazily initialize when close to the maximum size frequencySketch().ensureCapacity(max); } }
private void checkCache(BoundedLocalCache<K, V> cache) { try { if (cache.evictionLock.tryLock(5, TimeUnit.SECONDS)) { cache.evictionLock.unlock(); } else { desc.expected("Maintenance lock can be acquired"); } } catch (InterruptedException e) { desc.expected("Maintenance lock can be acquired: " + Throwables.getStackTraceAsString(e)); } desc.expectThat("Inconsistent size", cache.data.size(), is(cache.size())); if (cache.evicts()) { cache.evictionLock.lock(); try { long weightedSize = cache.weightedSize(); desc.expectThat("overflow", cache.maximum(), is(greaterThanOrEqualTo(weightedSize))); } finally { cache.evictionLock.unlock(); } } if (cache.isEmpty()) { desc.expectThat("empty map", cache, emptyMap()); } for (Node<K, V> node : cache.data.values()) { checkNode(cache, node, desc); } }
/** Returns the combined weight of the values in the cache. */ long adjustedWeightedSize() { return Math.max(0, weightedSize()); }
/** * Atomically transitions the node to the <tt>dead</tt> state and decrements the * <tt>weightedSize</tt>. * * @param node the entry in the page replacement policy */ @GuardedBy("evictionLock") void makeDead(Node<K, V> node) { synchronized (node) { if (node.isDead()) { return; } if (evicts()) { // The node's policy weight may be out of sync due to a pending update waiting to be // processed. At this point the node's weight is finalized, so the weight can be safely // taken from the node's perspective and the sizes will be adjusted correctly. if (node.inEden()) { lazySetEdenWeightedSize(edenWeightedSize() - node.getWeight()); } else if (node.inMainProtected()) { lazySetMainProtectedWeightedSize(mainProtectedWeightedSize() - node.getWeight()); } lazySetWeightedSize(weightedSize() - node.getWeight()); } node.die(); } }
Node<K, V> victim = accessOrderProbationDeque().peekFirst(); Node<K, V> candidate = accessOrderProbationDeque().peekLast(); while (weightedSize() > maximum()) {
/** * Sets the maximum weighted size of the cache. The caller may need to perform a maintenance cycle * to eagerly evicts entries until the cache shrinks to the appropriate size. */ @GuardedBy("evictionLock") void setMaximum(long maximum) { requireArgument(maximum >= 0); long max = Math.min(maximum, MAXIMUM_CAPACITY); long eden = max - (long) (max * PERCENT_MAIN); long mainProtected = (long) ((max - eden) * PERCENT_MAIN_PROTECTED); lazySetMaximum(max); lazySetEdenMaximum(eden); lazySetMainProtectedMaximum(mainProtected); if ((frequencySketch() != null) && !isWeighted() && (weightedSize() >= (max >>> 1))) { // Lazily initialize when close to the maximum size frequencySketch().ensureCapacity(max); } }
/** * Atomically transitions the node to the <tt>dead</tt> state and decrements the * <tt>weightedSize</tt>. * * @param node the entry in the page replacement policy */ @GuardedBy("evictionLock") void makeDead(Node<K, V> node) { synchronized (node) { if (node.isDead()) { return; } if (evicts()) { // The node's policy weight may be out of sync due to a pending update waiting to be // processed. At this point the node's weight is finalized, so the weight can be safely // taken from the node's perspective and the sizes will be adjusted correctly. if (node.inEden()) { lazySetEdenWeightedSize(edenWeightedSize() - node.getWeight()); } else if (node.inMainProtected()) { lazySetMainProtectedWeightedSize(mainProtectedWeightedSize() - node.getWeight()); } lazySetWeightedSize(weightedSize() - node.getWeight()); } node.die(); } }
Node<K, V> victim = accessOrderProbationDeque().peekFirst(); Node<K, V> candidate = accessOrderProbationDeque().peekLast(); while (weightedSize() > maximum()) {