MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / performEvictions

Function performEvictions

src/evict.cpp:667–965  ·  view source on GitHub ↗

Check that memory usage is within the current "maxmemory" limit. If over * "maxmemory", attempt to free memory by evicting data (if it's safe to do so). * * It's possible for Redis to suddenly be significantly over the "maxmemory" * setting. This can happen if there is a large allocation (like a hash table * resize) or even if the "maxmemory" setting is manually adjusted. Because of * thi

Source from the content-addressed store, hash-verified

665 * EVICT_FAIL - memory is over the limit, and there's nothing to evict
666 * */
667int performEvictions(bool fPreSnapshot) {
668 if (!isSafeToPerformEvictions()) return EVICT_OK;
669 serverAssert(GlobalLocksAcquired());
670
671 int keys_freed = 0;
672 size_t mem_reported, mem_tofree;
673 long long mem_freed; /* May be negative */
674 mstime_t latency;
675 long long delta;
676 int slaves = listLength(g_pserver->slaves);
677 const bool fEvictToStorage = !cserver.delete_on_evict && g_pserver->db[0]->FStorageProvider();
678 int result = EVICT_FAIL;
679 int ckeysFailed = 0;
680 EvictReason evictReason;
681
682 std::unique_ptr<FreeMemoryLazyFree> splazy = std::make_unique<FreeMemoryLazyFree>();
683
684 if (getMaxmemoryState(&mem_reported,NULL,&mem_tofree,NULL,&evictReason,false,fPreSnapshot) == C_OK)
685 return EVICT_OK;
686
687 if (g_pserver->maxmemory_policy == MAXMEMORY_NO_EVICTION)
688 return EVICT_FAIL; /* We need to free memory, but policy forbids. */
689
690 unsigned long eviction_time_limit_us = evictionTimeLimitUs();
691
692 mem_freed = 0;
693
694 latencyStartMonitor(latency);
695
696 monotime evictionTimer;
697 elapsedStart(&evictionTimer);
698
699 if (g_pserver->maxstorage && g_pserver->m_pstorageFactory != nullptr) {
700 while (g_pserver->m_pstorageFactory->totalDiskspaceUsed() >= g_pserver->maxstorage && elapsedUs(evictionTimer) < eviction_time_limit_us) {
701 redisDb *db;
702 std::vector<std::string> evictionPool;
703 robj *bestkey = nullptr;
704 redisDb *bestdb = nullptr;
705 unsigned long long bestidle = 0;
706 for (int i = 0; i < cserver.dbnum; i++) {
707 db = g_pserver->db[i];
708 evictionPool = db->getStorageCache()->getEvictionCandidates(g_pserver->maxmemory_samples);
709 for (std::string key : evictionPool) {
710 robj *keyobj = createStringObject(key.c_str(), key.size());
711 robj *obj = db->find(szFromObj(keyobj));
712 if (obj != nullptr) {
713 expireEntry *e = db->getExpire(keyobj);
714 unsigned long long idle = getIdle(obj, e);
715
716 if (bestkey == nullptr || bestidle < idle) {
717 if (bestkey != nullptr)
718 decrRefCount(bestkey);
719 incrRefCount(keyobj);
720 bestkey = keyobj;
721 bestidle = idle;
722 bestdb = db;
723 }
724 }

Callers 10

evictionTimeProcFunction · 0.85
processJobMethod · 0.85
createSnapshotMethod · 0.85
endSnapshotMethod · 0.85
serverCronFunction · 0.85
serverCronLiteFunction · 0.85
processCommandFunction · 0.85
updateMaxmemoryFunction · 0.85
updateFlashMaxmemoryFunction · 0.85
evalGenericCommandFunction · 0.85

Calls 15

isSafeToPerformEvictionsFunction · 0.85
GlobalLocksAcquiredFunction · 0.85
getMaxmemoryStateFunction · 0.85
evictionTimeLimitUsFunction · 0.85
elapsedStartFunction · 0.85
elapsedUsFunction · 0.85
szFromObjFunction · 0.85
getIdleFunction · 0.85
decrRefCountFunction · 0.85
incrRefCountFunction · 0.85
evictFunction · 0.85
evictionPoolPopulateFunction · 0.85

Tested by

no test coverage detected