MCPcopy Create free account
hub / github.com/facebook/CacheLib / work

Method work

cachelib/object_cache/ObjectCacheSizeController.h:124–263  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

122
123template <typename AllocatorT>
124void ObjectCacheSizeController<AllocatorT>::work() {
125 if (mode_ == FreeMemoryOnly) {
126 auto freeMemBytes = objCache_.config_.getFreeMemBytes();
127 if (freeMemBytes < objCache_.config_.lowerLimitBytes) {
128 // Shrink cache when free memory is below the lower limit
129 ensureFreeMemoryAboveLowerLimit();
130 } else if (freeMemBytes > objCache_.config_.upperLimitBytes) {
131 // Only expand cache when free memory is above the upper limit
132 expandCacheByEntriesNum(objCache_.config_.expandCacheBy,
133 getCurrentEntriesLimit());
134 }
135 // When free memory is between lower and upper limits, do nothing (stable
136 // zone)
137 return;
138 }
139
140 // This function adjusts number of entries allowed in the object cache
141 // based on internal metrics (average object size, total object size, max
142 // entries) and system metrics (free memory and RSS).
143 auto totalObjSize = getTotalObjSizeBytes();
144 auto currentNumEntries = objCache_.getNumEntries();
145 if (currentNumEntries == 0) {
146 return;
147 }
148 auto minEntriesWarmCacheThreshold =
149 kSizeControllerThresholdPct * objCache_.config_.l1EntriesLimit / 100;
150 auto totalObjectSizeLimit = getTotalObjectSizeLimit();
151 auto totalObjectSizeWarmCacheThreshold =
152 kSizeControllerThresholdPct * totalObjectSizeLimit / 100;
153
154 // Check if the cache is warming up.
155 // A cache is considered to be warming up if:
156 // 1. The total size of objects in the cache is less than the warm cache
157 // threshold.
158 // 2. The current number of entries in the cache is less than the minimum
159 // entries warm cache threshold.
160 // 3. There is space to grow, i.e., the current number of entries limit is
161 // greater than the current number of entries.
162 // If 1 and 2 are true but not 3, meaning very little items and bytes are
163 // consumed, yet the cache cannot grow since (currentEntriesLimit_ ==
164 // currentNumEntries), this could be because the cache shrunk to adjust for
165 // large items and then the item size suddenly dropped. Without checking for
166 // 3, we would not be stuck in an infinite loop where we always think the
167 // cache is warming up.
168 if ((totalObjSize < totalObjectSizeWarmCacheThreshold) &&
169 (currentNumEntries < minEntriesWarmCacheThreshold) &&
170 (currentEntriesLimit_ > currentNumEntries)) {
171 // Cache is warming up.
172 return;
173 }
174
175 // First thing, we want to take care of is to make sure that the total
176 // object size and number of objects are within the limit.
177 auto averageObjSize = totalObjSize / currentNumEntries;
178 XDCHECK_NE(0u, averageObjSize);
179 if (averageObjSize == 0) {
180 return;
181 }

Callers

nothing calls this directly

Calls 1

getNumEntriesMethod · 0.45

Tested by

no test coverage detected