Class WindowCache
Pack in
memory for faster read access.
The WindowCache serves as a Java based "buffer cache", loading segments of a PackFile into the JVM heap prior to use. As JGit often wants to do reads of only tiny slices of a file, the WindowCache tries to smooth out these tiny reads into larger block-sized IO operations.
Whenever a cache miss occurs, load(Pack, long) is invoked by
exactly one thread for the given (PackFile,position) key tuple.
This is ensured by an array of locks, with the tuple hashed to a lock
instance.
During a miss, older entries are evicted from the cache so long as
isFull() returns true.
Its too expensive during object access to be 100% accurate with a least recently used (LRU) algorithm. Strictly ordering every read is a lot of overhead that typically doesn't yield a corresponding benefit to the application.
This cache implements a loose LRU policy by randomly picking a window comprised of roughly 10% of the cache, and evicting the oldest accessed entry within that window.
Entities created by the cache are held under SoftReferences if option
core.packedGitUseStrongRefs is set to false in the git config
(this is the default) or by calling
WindowCacheConfig.setPackedGitUseStrongRefs(boolean), permitting the
Java runtime's garbage collector to evict entries when heap memory gets low.
Most JREs implement a loose least recently used algorithm for this eviction.
When this option is set to true strong references are used which
means that Java gc cannot evict the WindowCache to reclaim memory. On the
other hand this provides more predictable performance since the cache isn't
flushed when used heap comes close to the maximum heap size.
The internal hash table does not expand at runtime, instead it is fixed in size at cache creation time. The internal lock table used to gate load invocations is also fixed in size.
The key tuple is passed through to methods as a pair of parameters rather than as a single Object, thus reducing the transient memory allocations of callers. It is more efficient to avoid the allocation, as we can't be 100% sure that a JIT would be able to stack-allocate a key tuple.
This cache has an implementation rule such that:
load(Pack, long)is invoked by at most one thread at a time for a given(PackFile,position)tuple.- For every
load()invocation there is exactly onecreateRef(Pack, long, ByteWindow)invocation to wrap a SoftReference or a StrongReference around the cached entity. - For every Reference created by
createRef()there will be exactly one call toclear(PageRef)to cleanup any resources associated with the (now expired) cached entity.
Therefore, it is safe to perform resource accounting increments during the
load(Pack, long) or
createRef(Pack, long, ByteWindow) methods, and matching
decrements during clear(PageRef). Implementors may need to override
createRef(Pack, long, ByteWindow) in order to embed additional
accounting information into an implementation specific
WindowCache.PageRef subclass, as
the cached entity may have already been evicted by the JRE's garbage
collector.
To maintain higher concurrency workloads, during eviction only one thread performs the eviction work, while other threads can continue to insert new objects in parallel. This means that the cache can be temporarily over limit, especially if the nominated eviction thread is being starved relative to the other threads.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static interfaceprivate static classprivate static final classprivate static interfaceprivate static classprivate static classA soft reference wrapped around a cached object.(package private) static interfaceRecord statistics for a cache(package private) static classprivate static classprivate static classA strong reference wrapped around a cached object. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static WindowCacheprivate final AtomicLongAccess clock for loose LRU.private final intNumber oftablebuckets to scan for an eviction window.private final ReentrantLockLock to elect the eviction thread after a load occurs.private final WindowCache.Lock[]Locks to prevent concurrent loads for same (PackFile,position).private final longprivate final intprivate final WindowCache.StatsRecorderImplprivate final booleanprivate final AtomicBooleanprivate final WindowCache.CleanupQueuecleanup released and/or garbage collected windows.private static final Randomprivate final WindowCache.StatsRecorderprivate static intprivate final AtomicReferenceArray<WindowCache.Entry>Hash bucket directory; entries are chained below.private final intNumber of entries intable.private booleanprivate final intprivate final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static final intbits(int newSize) private static WindowCache.Entryclean(WindowCache.Entry top) private voidprivate voidprivate WindowCache.PageRef<ByteWindow>createRef(Pack p, long o, ByteWindow v) private voidevict()private voidgc()(package private) static final ByteWindowstatic WindowCacheprivate ByteWindowLookup a cached object, creating and loading it if it doesn't exist.getStats()(package private) static intprivate inthash(int packHash, long off) private voidprivate booleanisFull()private ByteWindowprivate WindowCache.Lockprivate static intprivate WindowCache(package private) static final voidstatic voidDeprecated.private voidClear every entry from the cache.private voidClear all entries related to a single file.voidReset stats.private ByteWindowscan(WindowCache.Entry n, Pack pack, long position) private intprivate static intprivate longtoStart(long offset)
-
Field Details
-
rng
-
cache
-
streamFileThreshold
private static volatile int streamFileThreshold -
queue
cleanup released and/or garbage collected windows. -
tableSize
private final int tableSizeNumber of entries intable. -
clock
Access clock for loose LRU. -
table
Hash bucket directory; entries are chained below. -
locks
Locks to prevent concurrent loads for same (PackFile,position). -
evictLock
Lock to elect the eviction thread after a load occurs. -
evictBatch
private final int evictBatchNumber oftablebuckets to scan for an eviction window. -
maxFiles
private final int maxFiles -
maxBytes
private final long maxBytes -
mmap
private final boolean mmap -
windowSizeShift
private final int windowSizeShift -
windowSize
private final int windowSize -
statsRecorder
-
mbean
-
publishMBean
-
useStrongRefs
private boolean useStrongRefs
-
-
Constructor Details
-
WindowCache
-
-
Method Details
-
bits
private static final int bits(int newSize) -
reconfigure
Deprecated.usecfg.install()to avoid internal reference.Modify the configuration of the window cache.The new configuration is applied immediately. If the new limits are smaller than what is currently cached, older entries will be purged as soon as possible to allow the cache to meet the new limit.
- Parameters:
cfg- the new window cache configuration.- Throws:
IllegalArgumentException- the cache configuration contains one or more invalid settings, usually too low of a limit.
-
getStreamFileThreshold
static int getStreamFileThreshold() -
getInstance
- Returns:
- the cached instance.
-
get
- Throws:
IOException
-
purge
-
publishMBeanIfNeeded
-
getStats
- Returns:
- cache statistics for the WindowCache
-
resetStats
public void resetStats()Reset stats. Does not reset open bytes and open files stats. -
hash
private int hash(int packHash, long off) -
load
- Throws:
IOException
-
createRef
-
clear
-
close
-
isFull
private boolean isFull() -
toStart
private long toStart(long offset) -
tableSize
-
lockCount
-
getOrLoad
Lookup a cached object, creating and loading it if it doesn't exist.- Parameters:
pack- the pack that "contains" the cached object.position- offset withinpackof the object.- Returns:
- the object reference.
- Throws:
IOException- the object reference was not in the cache and could not be obtained byload(Pack, long).
-
scan
-
hit
-
evict
private void evict() -
removeAll
private void removeAll()Clear every entry from the cache.This is a last-ditch effort to clear out the cache, such as before it gets replaced by another cache that is configured differently. This method tries to force every cached entry through
clear(PageRef)to ensure that resources are correctly accounted for and cleaned up by the subclass. A concurrent reader loading entries while this method is running may cause resource accounting failures. -
removeAll
Clear all entries related to a single file.Typically this method is invoked during
Pack.close(), when we know the pack is never going to be useful to us again (for example, it no longer exists on disk). A concurrent reader loading an entry from this same pack may cause the pack to become stuck in the cache anyway.- Parameters:
pack- the file to purge all entries of.
-
gc
private void gc() -
slot
-
lock
-
clean
-
cfg.install()to avoid internal reference.