Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
298c4d3e
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
298c4d3e
编写于
6月 30, 2022
作者:
M
Minglei Jin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
tlrucache: fix cap calculation
上级
0f922533
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
108 addition
and
88 deletion
+108
-88
source/util/src/tlrucache.c
source/util/src/tlrucache.c
+108
-88
未找到文件。
source/util/src/tlrucache.c
浏览文件 @
298c4d3e
...
...
@@ -16,10 +16,10 @@
#define _DEFAULT_SOURCE
#include "tlrucache.h"
#include "os.h"
#include "tdef.h"
#include "taoserror.h"
#include "tlog.h"
#include "tarray.h"
#include "tdef.h"
#include "tlog.h"
typedef
struct
SLRUEntry
SLRUEntry
;
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_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_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_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_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_HAS_REFS(h) ((h)->refs > 0)
...
...
@@ -90,7 +111,7 @@ struct SLRUEntryTable {
static
int
taosLRUEntryTableInit
(
SLRUEntryTable
*
table
,
int
maxUpperHashBits
)
{
table
->
lengthBits
=
4
;
table
->
list
=
taosMemoryCalloc
(
1
<<
table
->
lengthBits
,
sizeof
(
SLRUEntry
*
));
table
->
list
=
taosMemoryCalloc
(
1
<<
table
->
lengthBits
,
sizeof
(
SLRUEntry
*
));
if
(
!
table
->
list
)
{
return
-
1
;
}
...
...
@@ -125,7 +146,7 @@ static void taosLRUEntryTableCleanup(SLRUEntryTable *table) {
taosMemoryFree
(
table
->
list
);
}
static
SLRUEntry
**
taosLRUEntryTableFindPtr
(
SLRUEntryTable
*
table
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
)
{
static
SLRUEntry
**
taosLRUEntryTableFindPtr
(
SLRUEntryTable
*
table
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
)
{
SLRUEntry
**
entry
=
&
table
->
list
[
hash
>>
(
32
-
table
->
lengthBits
)];
while
(
*
entry
&&
((
*
entry
)
->
hash
!=
hash
||
memcmp
(
key
,
(
*
entry
)
->
keyData
,
keyLen
)
!=
0
))
{
entry
=
&
(
*
entry
)
->
nextHash
;
...
...
@@ -134,7 +155,7 @@ static SLRUEntry **taosLRUEntryTableFindPtr(SLRUEntryTable * table, const void *
return
entry
;
}
static
void
taosLRUEntryTableResize
(
SLRUEntryTable
*
table
)
{
static
void
taosLRUEntryTableResize
(
SLRUEntryTable
*
table
)
{
int
lengthBits
=
table
->
lengthBits
;
if
(
lengthBits
>=
table
->
maxLengthBits
)
{
return
;
...
...
@@ -146,7 +167,7 @@ static void taosLRUEntryTableResize(SLRUEntryTable * table) {
uint32_t
oldLength
=
1
<<
lengthBits
;
int
newLengthBits
=
lengthBits
+
1
;
SLRUEntry
**
newList
=
taosMemoryCalloc
(
1
<<
newLengthBits
,
sizeof
(
SLRUEntry
*
));
SLRUEntry
**
newList
=
taosMemoryCalloc
(
1
<<
newLengthBits
,
sizeof
(
SLRUEntry
*
));
if
(
!
newList
)
{
return
;
}
...
...
@@ -170,11 +191,11 @@ static void taosLRUEntryTableResize(SLRUEntryTable * table) {
table
->
lengthBits
=
newLengthBits
;
}
static
SLRUEntry
*
taosLRUEntryTableLookup
(
SLRUEntryTable
*
table
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
)
{
static
SLRUEntry
*
taosLRUEntryTableLookup
(
SLRUEntryTable
*
table
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
)
{
return
*
taosLRUEntryTableFindPtr
(
table
,
key
,
keyLen
,
hash
);
}
static
SLRUEntry
*
taosLRUEntryTableInsert
(
SLRUEntryTable
*
table
,
SLRUEntry
*
entry
)
{
static
SLRUEntry
*
taosLRUEntryTableInsert
(
SLRUEntryTable
*
table
,
SLRUEntry
*
entry
)
{
SLRUEntry
**
ptr
=
taosLRUEntryTableFindPtr
(
table
,
entry
->
keyData
,
entry
->
keyLength
,
entry
->
hash
);
SLRUEntry
*
old
=
*
ptr
;
entry
->
nextHash
=
(
old
==
NULL
)
?
NULL
:
old
->
nextHash
;
...
...
@@ -189,7 +210,7 @@ static SLRUEntry *taosLRUEntryTableInsert(SLRUEntryTable * table, SLRUEntry *ent
return
old
;
}
static
SLRUEntry
*
taosLRUEntryTableRemove
(
SLRUEntryTable
*
table
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
)
{
static
SLRUEntry
*
taosLRUEntryTableRemove
(
SLRUEntryTable
*
table
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
)
{
SLRUEntry
**
entry
=
taosLRUEntryTableFindPtr
(
table
,
key
,
keyLen
,
hash
);
SLRUEntry
*
result
=
*
entry
;
if
(
result
)
{
...
...
@@ -231,8 +252,7 @@ static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
assert
(
e
->
next
==
NULL
);
assert
(
e
->
prev
==
NULL
);
if
(
shard
->
highPriPoolRatio
>
0
&&
(
TAOS_LRU_ENTRY_IS_HIGH_PRI
(
e
)
||
TAOS_LRU_ENTRY_HAS_HIT
(
e
)))
{
if
(
shard
->
highPriPoolRatio
>
0
&&
(
TAOS_LRU_ENTRY_IS_HIGH_PRI
(
e
)
||
TAOS_LRU_ENTRY_HAS_HIT
(
e
)))
{
e
->
next
=
&
shard
->
lru
;
e
->
prev
=
shard
->
lru
.
prev
;
...
...
@@ -309,8 +329,8 @@ static void taosLRUCacheShardSetCapacity(SLRUCacheShard *shard, size_t capacity)
taosArrayDestroy
(
lastReferenceList
);
}
static
int
taosLRUCacheShardInit
(
SLRUCacheShard
*
shard
,
size_t
capacity
,
bool
strict
,
double
highPriPoolRatio
,
int
maxUpperHashBits
)
{
static
int
taosLRUCacheShardInit
(
SLRUCacheShard
*
shard
,
size_t
capacity
,
bool
strict
,
double
highPriPoolRatio
,
int
maxUpperHashBits
)
{
if
(
taosLRUEntryTableInit
(
&
shard
->
table
,
maxUpperHashBits
)
<
0
)
{
return
-
1
;
}
...
...
@@ -341,7 +361,8 @@ static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
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
;
SArray
*
lastReferenceList
=
taosArrayInit
(
16
,
POINTER_BYTES
);
...
...
@@ -385,7 +406,7 @@ static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *
TAOS_LRU_ENTRY_REF
(
e
);
}
*
handle
=
(
LRUHandle
*
)
e
;
*
handle
=
(
LRUHandle
*
)
e
;
}
}
...
...
@@ -402,8 +423,8 @@ static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *
}
static
LRUStatus
taosLRUCacheShardInsert
(
SLRUCacheShard
*
shard
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
,
void
*
value
,
size_t
charge
,
_taos_lru_deleter_t
deleter
,
LRUHandle
**
handle
,
LRUPriority
priority
)
{
void
*
value
,
size_t
charge
,
_taos_lru_deleter_t
deleter
,
LRUHandle
**
handle
,
LRUPriority
priority
)
{
SLRUEntry
*
e
=
taosMemoryCalloc
(
1
,
sizeof
(
SLRUEntry
)
-
1
+
keyLen
);
if
(
!
e
)
{
return
TAOS_LRU_STATUS_FAIL
;
...
...
@@ -442,7 +463,7 @@ static LRUHandle *taosLRUCacheShardLookup(SLRUCacheShard *shard, const void *key
taosThreadMutexUnlock
(
&
shard
->
mutex
);
return
(
LRUHandle
*
)
e
;
return
(
LRUHandle
*
)
e
;
}
static
void
taosLRUCacheShardErase
(
SLRUCacheShard
*
shard
,
const
void
*
key
,
size_t
keyLen
,
uint32_t
hash
)
{
...
...
@@ -498,7 +519,7 @@ static void taosLRUCacheShardEraseUnrefEntries(SLRUCacheShard *shard) {
}
static
bool
taosLRUCacheShardRef
(
SLRUCacheShard
*
shard
,
LRUHandle
*
handle
)
{
SLRUEntry
*
e
=
(
SLRUEntry
*
)
handle
;
SLRUEntry
*
e
=
(
SLRUEntry
*
)
handle
;
taosThreadMutexLock
(
&
shard
->
mutex
);
assert
(
TAOS_LRU_ENTRY_HAS_REFS
(
e
));
...
...
@@ -514,7 +535,7 @@ static bool taosLRUCacheShardRelease(SLRUCacheShard *shard, LRUHandle *handle, b
return
false
;
}
SLRUEntry
*
e
=
(
SLRUEntry
*
)
handle
;
SLRUEntry
*
e
=
(
SLRUEntry
*
)
handle
;
bool
lastReference
=
false
;
taosThreadMutexLock
(
&
shard
->
mutex
);
...
...
@@ -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
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
)
{
...
...
@@ -699,7 +721,7 @@ bool taosLRUCacheRef(SLRUCache *cache, LRUHandle *handle) {
return
false
;
}
uint32_t
hash
=
((
SLRUEntry
*
)
handle
)
->
hash
;
uint32_t
hash
=
((
SLRUEntry
*
)
handle
)
->
hash
;
uint32_t
shardIndex
=
hash
&
cache
->
shardedCache
.
shardMask
;
return
taosLRUCacheShardRef
(
&
cache
->
shards
[
shardIndex
],
handle
);
...
...
@@ -710,15 +732,13 @@ bool taosLRUCacheRelease(SLRUCache *cache, LRUHandle *handle, bool eraseIfLastRe
return
false
;
}
uint32_t
hash
=
((
SLRUEntry
*
)
handle
)
->
hash
;
uint32_t
hash
=
((
SLRUEntry
*
)
handle
)
->
hash
;
uint32_t
shardIndex
=
hash
&
cache
->
shardedCache
.
shardMask
;
return
taosLRUCacheShardRelease
(
&
cache
->
shards
[
shardIndex
],
handle
,
eraseIfLastRef
);
}
void
*
taosLRUCacheValue
(
SLRUCache
*
cache
,
LRUHandle
*
handle
)
{
return
((
SLRUEntry
*
)
handle
)
->
value
;
}
void
*
taosLRUCacheValue
(
SLRUCache
*
cache
,
LRUHandle
*
handle
)
{
return
((
SLRUEntry
*
)
handle
)
->
value
;
}
size_t
taosLRUCacheGetUsage
(
SLRUCache
*
cache
)
{
size_t
usage
=
0
;
...
...
@@ -742,7 +762,7 @@ size_t taosLRUCacheGetPinnedUsage(SLRUCache *cache) {
void
taosLRUCacheSetCapacity
(
SLRUCache
*
cache
,
size_t
capacity
)
{
uint32_t
numShards
=
cache
->
numShards
;
size_t
perShard
=
(
capacity
+
(
numShards
=
1
))
/
numShards
;
size_t
perShard
=
(
capacity
+
(
numShards
-
1
))
/
numShards
;
taosThreadMutexLock
(
&
cache
->
shardedCache
.
capacityMutex
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录