tlrucache: fix cap calculation
This commit is contained in:
parent
0f92253390
commit
298c4d3e44
|
@ -16,10 +16,10 @@
|
||||||
#define _DEFAULT_SOURCE
|
#define _DEFAULT_SOURCE
|
||||||
#include "tlrucache.h"
|
#include "tlrucache.h"
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
#include "tdef.h"
|
|
||||||
#include "taoserror.h"
|
#include "taoserror.h"
|
||||||
#include "tlog.h"
|
|
||||||
#include "tarray.h"
|
#include "tarray.h"
|
||||||
|
#include "tdef.h"
|
||||||
|
#include "tlog.h"
|
||||||
|
|
||||||
typedef struct SLRUEntry SLRUEntry;
|
typedef struct SLRUEntry SLRUEntry;
|
||||||
typedef struct SLRUEntryTable SLRUEntryTable;
|
typedef struct SLRUEntryTable SLRUEntryTable;
|
||||||
|
@ -55,9 +55,30 @@ struct SLRUEntry {
|
||||||
#define TAOS_LRU_ENTRY_IS_HIGH_PRI(h) ((h)->flags & TAOS_LRU_IS_HIGH_PRI)
|
#define TAOS_LRU_ENTRY_IS_HIGH_PRI(h) ((h)->flags & TAOS_LRU_IS_HIGH_PRI)
|
||||||
#define TAOS_LRU_ENTRY_HAS_HIT(h) ((h)->flags & TAOS_LRU_HAS_HIT)
|
#define TAOS_LRU_ENTRY_HAS_HIT(h) ((h)->flags & TAOS_LRU_HAS_HIT)
|
||||||
|
|
||||||
#define TAOS_LRU_ENTRY_SET_IN_CACHE(h, inCache) do { if(inCache) {(h)->flags |= TAOS_LRU_IN_CACHE;} else {(h)->flags &= ~TAOS_LRU_IN_CACHE;} } while(0)
|
#define TAOS_LRU_ENTRY_SET_IN_CACHE(h, inCache) \
|
||||||
#define TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(h, inHigh) do { if(inHigh) {(h)->flags |= TAOS_LRU_IN_HIGH_PRI_POOL;} else {(h)->flags &= ~TAOS_LRU_IN_HIGH_PRI_POOL;} } while(0)
|
do { \
|
||||||
#define TAOS_LRU_ENTRY_SET_PRIORITY(h, priority) do { if(priority == TAOS_LRU_PRIORITY_HIGH) {(h)->flags |= TAOS_LRU_IS_HIGH_PRI;} else {(h)->flags &= ~TAOS_LRU_IS_HIGH_PRI;} } while(0)
|
if (inCache) { \
|
||||||
|
(h)->flags |= TAOS_LRU_IN_CACHE; \
|
||||||
|
} else { \
|
||||||
|
(h)->flags &= ~TAOS_LRU_IN_CACHE; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(h, inHigh) \
|
||||||
|
do { \
|
||||||
|
if (inHigh) { \
|
||||||
|
(h)->flags |= TAOS_LRU_IN_HIGH_PRI_POOL; \
|
||||||
|
} else { \
|
||||||
|
(h)->flags &= ~TAOS_LRU_IN_HIGH_PRI_POOL; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define TAOS_LRU_ENTRY_SET_PRIORITY(h, priority) \
|
||||||
|
do { \
|
||||||
|
if (priority == TAOS_LRU_PRIORITY_HIGH) { \
|
||||||
|
(h)->flags |= TAOS_LRU_IS_HIGH_PRI; \
|
||||||
|
} else { \
|
||||||
|
(h)->flags &= ~TAOS_LRU_IS_HIGH_PRI; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
#define TAOS_LRU_ENTRY_SET_HIT(h) ((h)->flags |= TAOS_LRU_HAS_HIT)
|
#define TAOS_LRU_ENTRY_SET_HIT(h) ((h)->flags |= TAOS_LRU_HAS_HIT)
|
||||||
|
|
||||||
#define TAOS_LRU_ENTRY_HAS_REFS(h) ((h)->refs > 0)
|
#define TAOS_LRU_ENTRY_HAS_REFS(h) ((h)->refs > 0)
|
||||||
|
@ -231,8 +252,7 @@ static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
|
||||||
assert(e->next == NULL);
|
assert(e->next == NULL);
|
||||||
assert(e->prev == NULL);
|
assert(e->prev == NULL);
|
||||||
|
|
||||||
if (shard->highPriPoolRatio > 0
|
if (shard->highPriPoolRatio > 0 && (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
|
||||||
&& (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
|
|
||||||
e->next = &shard->lru;
|
e->next = &shard->lru;
|
||||||
e->prev = shard->lru.prev;
|
e->prev = shard->lru.prev;
|
||||||
|
|
||||||
|
@ -309,8 +329,8 @@ static void taosLRUCacheShardSetCapacity(SLRUCacheShard *shard, size_t capacity)
|
||||||
taosArrayDestroy(lastReferenceList);
|
taosArrayDestroy(lastReferenceList);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int taosLRUCacheShardInit(SLRUCacheShard *shard, size_t capacity, bool strict,
|
static int taosLRUCacheShardInit(SLRUCacheShard *shard, size_t capacity, bool strict, double highPriPoolRatio,
|
||||||
double highPriPoolRatio, int maxUpperHashBits) {
|
int maxUpperHashBits) {
|
||||||
if (taosLRUEntryTableInit(&shard->table, maxUpperHashBits) < 0) {
|
if (taosLRUEntryTableInit(&shard->table, maxUpperHashBits) < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -341,7 +361,8 @@ static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
|
||||||
taosLRUEntryTableCleanup(&shard->table);
|
taosLRUEntryTableCleanup(&shard->table);
|
||||||
}
|
}
|
||||||
|
|
||||||
static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *e, LRUHandle **handle, bool freeOnFail) {
|
static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *e, LRUHandle **handle,
|
||||||
|
bool freeOnFail) {
|
||||||
LRUStatus status = TAOS_LRU_STATUS_OK;
|
LRUStatus status = TAOS_LRU_STATUS_OK;
|
||||||
SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
|
SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
|
||||||
|
|
||||||
|
@ -402,8 +423,8 @@ static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *
|
||||||
}
|
}
|
||||||
|
|
||||||
static LRUStatus taosLRUCacheShardInsert(SLRUCacheShard *shard, const void *key, size_t keyLen, uint32_t hash,
|
static LRUStatus taosLRUCacheShardInsert(SLRUCacheShard *shard, const void *key, size_t keyLen, uint32_t hash,
|
||||||
void *value, size_t charge, _taos_lru_deleter_t deleter,
|
void *value, size_t charge, _taos_lru_deleter_t deleter, LRUHandle **handle,
|
||||||
LRUHandle **handle, LRUPriority priority) {
|
LRUPriority priority) {
|
||||||
SLRUEntry *e = taosMemoryCalloc(1, sizeof(SLRUEntry) - 1 + keyLen);
|
SLRUEntry *e = taosMemoryCalloc(1, sizeof(SLRUEntry) - 1 + keyLen);
|
||||||
if (!e) {
|
if (!e) {
|
||||||
return TAOS_LRU_STATUS_FAIL;
|
return TAOS_LRU_STATUS_FAIL;
|
||||||
|
@ -670,7 +691,8 @@ LRUStatus taosLRUCacheInsert(SLRUCache *cache, const void *key, size_t keyLen, v
|
||||||
uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
|
uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
|
||||||
uint32_t shardIndex = hash & cache->shardedCache.shardMask;
|
uint32_t shardIndex = hash & cache->shardedCache.shardMask;
|
||||||
|
|
||||||
return taosLRUCacheShardInsert(&cache->shards[shardIndex], key, keyLen, hash, value, charge, deleter, handle, priority);
|
return taosLRUCacheShardInsert(&cache->shards[shardIndex], key, keyLen, hash, value, charge, deleter, handle,
|
||||||
|
priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
|
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
|
||||||
|
@ -716,9 +738,7 @@ bool taosLRUCacheRelease(SLRUCache *cache, LRUHandle *handle, bool eraseIfLastRe
|
||||||
return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
|
return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) {
|
void *taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) { return ((SLRUEntry *)handle)->value; }
|
||||||
return ((SLRUEntry*) handle)->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
|
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
|
||||||
size_t usage = 0;
|
size_t usage = 0;
|
||||||
|
@ -742,7 +762,7 @@ size_t taosLRUCacheGetPinnedUsage(SLRUCache *cache) {
|
||||||
|
|
||||||
void taosLRUCacheSetCapacity(SLRUCache *cache, size_t capacity) {
|
void taosLRUCacheSetCapacity(SLRUCache *cache, size_t capacity) {
|
||||||
uint32_t numShards = cache->numShards;
|
uint32_t numShards = cache->numShards;
|
||||||
size_t perShard = (capacity + (numShards = 1)) / numShards;
|
size_t perShard = (capacity + (numShards - 1)) / numShards;
|
||||||
|
|
||||||
taosThreadMutexLock(&cache->shardedCache.capacityMutex);
|
taosThreadMutexLock(&cache->shardedCache.capacityMutex);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue