Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
b17b4932
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b17b4932
编写于
2月 10, 2022
作者:
H
Haojun Liao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[td-11818]Refactor.
上级
4dbbf286
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
228 addition
and
159 deletion
+228
-159
include/util/tpagedbuf.h
include/util/tpagedbuf.h
+29
-23
source/libs/executor/src/executorimpl.c
source/libs/executor/src/executorimpl.c
+2
-0
source/libs/executor/test/executorTests.cpp
source/libs/executor/test/executorTests.cpp
+2
-0
source/util/src/tpagedbuf.c
source/util/src/tpagedbuf.c
+195
-136
未找到文件。
include/util/tpagedbuf.h
浏览文件 @
b17b4932
...
...
@@ -39,73 +39,73 @@ typedef struct SFilePage {
/**
* create disk-based result buffer
* @param p
Result
Buf
* @param pBuf
* @param rowSize
* @param pagesize
* @param inMemPages
* @param handle
* @return
*/
int32_t
createDiskbasedBuffer
(
SDiskbasedBuf
**
p
Result
Buf
,
int32_t
pagesize
,
int32_t
inMemBufSize
,
uint64_t
qId
,
const
char
*
dir
);
int32_t
createDiskbasedBuffer
(
SDiskbasedBuf
**
pBuf
,
int32_t
pagesize
,
int32_t
inMemBufSize
,
uint64_t
qId
,
const
char
*
dir
);
/**
*
* @param p
Result
Buf
* @param pBuf
* @param groupId
* @param pageId
* @return
*/
SFilePage
*
getNewDataBuf
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
groupId
,
int32_t
*
pageId
);
SFilePage
*
getNewDataBuf
(
SDiskbasedBuf
*
pBuf
,
int32_t
groupId
,
int32_t
*
pageId
);
/**
*
* @param p
Result
Buf
* @param pBuf
* @param groupId
* @return
*/
SIDList
getDataBufPagesIdList
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
groupId
);
SIDList
getDataBufPagesIdList
(
SDiskbasedBuf
*
pBuf
,
int32_t
groupId
);
/**
* get the specified buffer page by id
* @param p
Result
Buf
* @param pBuf
* @param id
* @return
*/
SFilePage
*
getBufPage
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
id
);
SFilePage
*
getBufPage
(
SDiskbasedBuf
*
pBuf
,
int32_t
id
);
/**
* release the referenced buf pages
* @param p
Result
Buf
* @param pBuf
* @param page
*/
void
releaseBufPage
(
SDiskbasedBuf
*
p
Result
Buf
,
void
*
page
);
void
releaseBufPage
(
SDiskbasedBuf
*
pBuf
,
void
*
page
);
/**
*
* @param p
Result
Buf
* @param pBuf
* @param pi
*/
void
releaseBufPageInfo
(
SDiskbasedBuf
*
p
Result
Buf
,
struct
SPageInfo
*
pi
);
void
releaseBufPageInfo
(
SDiskbasedBuf
*
pBuf
,
struct
SPageInfo
*
pi
);
/**
* get the total buffer size in the format of disk file
* @param p
Result
Buf
* @param pBuf
* @return
*/
size_t
getTotalBufSize
(
const
SDiskbasedBuf
*
p
Result
Buf
);
size_t
getTotalBufSize
(
const
SDiskbasedBuf
*
pBuf
);
/**
* get the number of groups in the result buffer
* @param p
Result
Buf
* @param pBuf
* @return
*/
size_t
getNumOfResultBufGroupId
(
const
SDiskbasedBuf
*
p
Result
Buf
);
size_t
getNumOfResultBufGroupId
(
const
SDiskbasedBuf
*
pBuf
);
/**
* destroy result buffer
* @param p
Result
Buf
* @param pBuf
*/
void
destroyResultBuf
(
SDiskbasedBuf
*
p
Result
Buf
);
void
destroyResultBuf
(
SDiskbasedBuf
*
pBuf
);
/**
*
...
...
@@ -123,24 +123,30 @@ int32_t getPageId(const SPageInfo* pPgInfo);
/**
* Return the buffer page size.
* @param p
Result
Buf
* @param pBuf
* @return
*/
int32_t
getBufPageSize
(
const
SDiskbasedBuf
*
p
Result
Buf
);
int32_t
getBufPageSize
(
const
SDiskbasedBuf
*
pBuf
);
/**
*
* @param p
Result
Buf
* @param pBuf
* @return
*/
bool
isAllDataInMemBuf
(
const
SDiskbasedBuf
*
p
Result
Buf
);
bool
isAllDataInMemBuf
(
const
SDiskbasedBuf
*
pBuf
);
/**
* Set the buffer page is dirty, and needs to be flushed to disk when swap out.
* @param pPageInfo
* @param dirty
*/
void
setBufPageDirty
(
SPageInfo
*
pPageInfo
,
bool
dirty
);
void
setBufPageDirty
(
SFilePage
*
pPageInfo
,
bool
dirty
);
/**
* Print the statistics when closing this buffer
* @param pBuf
*/
void
printStatisBeforeClose
(
SDiskbasedBuf
*
pBuf
);
#ifdef __cplusplus
}
...
...
source/libs/executor/src/executorimpl.c
浏览文件 @
b17b4932
...
...
@@ -5729,6 +5729,8 @@ void addToDiskbasedBuf(SOrderOperatorInfo* pInfo, jmp_buf env) {
assert
(
size
<=
getBufPageSize
(
pInfo
->
pSortInternalBuf
));
blockDataToBuf
(
pPage
->
data
,
p
);
setBufPageDirty
(
pPage
,
true
);
releaseBufPage
(
pInfo
->
pSortInternalBuf
,
pPage
);
blockDataDestroy
(
p
);
...
...
source/libs/executor/test/executorTests.cpp
浏览文件 @
b17b4932
...
...
@@ -293,6 +293,8 @@ TEST(testCase, external_sort_Test) {
// }
}
printStatisBeforeClose
(((
SOrderOperatorInfo
*
)
pOperator
->
info
)
->
pSortInternalBuf
);
int64_t
s2
=
taosGetTimestampUs
();
printf
(
"total:%ld
\n
"
,
s2
-
s1
);
...
...
source/util/src/tpagedbuf.c
浏览文件 @
b17b4932
#include "tpagedbuf.h"
#include <ulog.h>
#include <zconf.h>
#include "stddef.h"
#include "taoserror.h"
#include "tcompression.h"
...
...
@@ -28,8 +30,9 @@ typedef struct SPageInfo {
}
SPageInfo
;
typedef
struct
SDiskbasedBufStatis
{
int32_t
flushBytes
;
int32_t
loadBytes
;
int64_t
flushBytes
;
int64_t
loadBytes
;
int32_t
loadPages
;
int32_t
getPages
;
int32_t
releasePages
;
int32_t
flushPages
;
...
...
@@ -55,12 +58,15 @@ typedef struct SDiskbasedBuf {
uint64_t
qId
;
// for debug purpose
SDiskbasedBufStatis
statis
;
bool
printStatis
;
// Print statistics info when closing this buffer.
}
SDiskbasedBuf
;
int32_t
createDiskbasedBuffer
(
SDiskbasedBuf
**
pResultBuf
,
int32_t
pagesize
,
int32_t
inMemBufSize
,
uint64_t
qId
,
const
char
*
dir
)
{
*
pResultBuf
=
calloc
(
1
,
sizeof
(
SDiskbasedBuf
));
static
void
printStatisData
(
const
SDiskbasedBuf
*
pBuf
);
SDiskbasedBuf
*
pResBuf
=
*
pResultBuf
;
int32_t
createDiskbasedBuffer
(
SDiskbasedBuf
**
pBuf
,
int32_t
pagesize
,
int32_t
inMemBufSize
,
uint64_t
qId
,
const
char
*
dir
)
{
*
pBuf
=
calloc
(
1
,
sizeof
(
SDiskbasedBuf
));
SDiskbasedBuf
*
pResBuf
=
*
pBuf
;
if
(
pResBuf
==
NULL
)
{
return
TSDB_CODE_OUT_OF_MEMORY
;
}
...
...
@@ -97,50 +103,50 @@ int32_t createDiskbasedBuffer(SDiskbasedBuf** pResultBuf, int32_t pagesize, int3
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
createDiskFile
(
SDiskbasedBuf
*
p
Result
Buf
)
{
p
ResultBuf
->
file
=
fopen
(
pResult
Buf
->
path
,
"wb+"
);
if
(
p
Result
Buf
->
file
==
NULL
)
{
// qError("failed to create tmp file: %s on disk. %s", p
Result
Buf->path, strerror(errno));
static
int32_t
createDiskFile
(
SDiskbasedBuf
*
pBuf
)
{
p
Buf
->
file
=
fopen
(
p
Buf
->
path
,
"wb+"
);
if
(
pBuf
->
file
==
NULL
)
{
// qError("failed to create tmp file: %s on disk. %s", pBuf->path, strerror(errno));
return
TAOS_SYSTEM_ERROR
(
errno
);
}
return
TSDB_CODE_SUCCESS
;
}
static
char
*
doCompressData
(
void
*
data
,
int32_t
srcSize
,
int32_t
*
dst
,
SDiskbasedBuf
*
p
Result
Buf
)
{
// do nothing
if
(
!
p
Result
Buf
->
comp
)
{
static
char
*
doCompressData
(
void
*
data
,
int32_t
srcSize
,
int32_t
*
dst
,
SDiskbasedBuf
*
pBuf
)
{
// do nothing
if
(
!
pBuf
->
comp
)
{
*
dst
=
srcSize
;
return
data
;
}
*
dst
=
tsCompressString
(
data
,
srcSize
,
1
,
p
Result
Buf
->
assistBuf
,
srcSize
,
ONE_STAGE_COMP
,
NULL
,
0
);
*
dst
=
tsCompressString
(
data
,
srcSize
,
1
,
pBuf
->
assistBuf
,
srcSize
,
ONE_STAGE_COMP
,
NULL
,
0
);
memcpy
(
data
,
p
Result
Buf
->
assistBuf
,
*
dst
);
memcpy
(
data
,
pBuf
->
assistBuf
,
*
dst
);
return
data
;
}
static
char
*
doDecompressData
(
void
*
data
,
int32_t
srcSize
,
int32_t
*
dst
,
SDiskbasedBuf
*
p
Result
Buf
)
{
// do nothing
if
(
!
p
Result
Buf
->
comp
)
{
static
char
*
doDecompressData
(
void
*
data
,
int32_t
srcSize
,
int32_t
*
dst
,
SDiskbasedBuf
*
pBuf
)
{
// do nothing
if
(
!
pBuf
->
comp
)
{
*
dst
=
srcSize
;
return
data
;
}
*
dst
=
tsDecompressString
(
data
,
srcSize
,
1
,
p
ResultBuf
->
assistBuf
,
pResult
Buf
->
pageSize
,
ONE_STAGE_COMP
,
NULL
,
0
);
*
dst
=
tsDecompressString
(
data
,
srcSize
,
1
,
p
Buf
->
assistBuf
,
p
Buf
->
pageSize
,
ONE_STAGE_COMP
,
NULL
,
0
);
if
(
*
dst
>
0
)
{
memcpy
(
data
,
p
Result
Buf
->
assistBuf
,
*
dst
);
memcpy
(
data
,
pBuf
->
assistBuf
,
*
dst
);
}
return
data
;
}
static
uint64_t
allocatePositionInFile
(
SDiskbasedBuf
*
p
Result
Buf
,
size_t
size
)
{
if
(
p
Result
Buf
->
pFree
==
NULL
)
{
return
p
Result
Buf
->
nextPos
;
static
uint64_t
allocatePositionInFile
(
SDiskbasedBuf
*
pBuf
,
size_t
size
)
{
if
(
pBuf
->
pFree
==
NULL
)
{
return
pBuf
->
nextPos
;
}
else
{
int32_t
offset
=
-
1
;
size_t
num
=
taosArrayGetSize
(
p
Result
Buf
->
pFree
);
size_t
num
=
taosArrayGetSize
(
pBuf
->
pFree
);
for
(
int32_t
i
=
0
;
i
<
num
;
++
i
)
{
SFreeListItem
*
pi
=
taosArrayGet
(
p
Result
Buf
->
pFree
,
i
);
SFreeListItem
*
pi
=
taosArrayGet
(
pBuf
->
pFree
,
i
);
if
(
pi
->
len
>=
size
)
{
offset
=
pi
->
offset
;
pi
->
offset
+=
(
int32_t
)
size
;
...
...
@@ -151,128 +157,141 @@ static uint64_t allocatePositionInFile(SDiskbasedBuf* pResultBuf, size_t size) {
}
// no available recycle space, allocate new area in file
return
p
Result
Buf
->
nextPos
;
return
pBuf
->
nextPos
;
}
}
static
char
*
doFlushPageToDisk
(
SDiskbasedBuf
*
p
Result
Buf
,
SPageInfo
*
pg
)
{
static
char
*
doFlushPageToDisk
(
SDiskbasedBuf
*
pBuf
,
SPageInfo
*
pg
)
{
assert
(
!
pg
->
used
&&
pg
->
pData
!=
NULL
);
int32_t
size
=
-
1
;
char
*
t
=
doCompressData
(
GET_DATA_PAYLOAD
(
pg
),
p
ResultBuf
->
pageSize
,
&
size
,
pResult
Buf
);
char
*
t
=
doCompressData
(
GET_DATA_PAYLOAD
(
pg
),
p
Buf
->
pageSize
,
&
size
,
p
Buf
);
// this page is flushed to disk for the first time
if
(
pg
->
offset
==
-
1
)
{
pg
->
offset
=
allocatePositionInFile
(
pResultBuf
,
size
);
pResultBuf
->
nextPos
+=
size
;
assert
(
pg
->
dirty
==
true
);
pg
->
offset
=
allocatePositionInFile
(
pBuf
,
size
);
pBuf
->
nextPos
+=
size
;
int32_t
ret
=
fseek
(
p
Result
Buf
->
file
,
pg
->
offset
,
SEEK_SET
);
int32_t
ret
=
fseek
(
pBuf
->
file
,
pg
->
offset
,
SEEK_SET
);
if
(
ret
!=
0
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
NULL
;
}
ret
=
(
int32_t
)
fwrite
(
t
,
1
,
size
,
p
Result
Buf
->
file
);
ret
=
(
int32_t
)
fwrite
(
t
,
1
,
size
,
pBuf
->
file
);
if
(
ret
!=
size
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
NULL
;
}
if
(
p
Result
Buf
->
fileSize
<
pg
->
offset
+
size
)
{
p
Result
Buf
->
fileSize
=
pg
->
offset
+
size
;
if
(
pBuf
->
fileSize
<
pg
->
offset
+
size
)
{
pBuf
->
fileSize
=
pg
->
offset
+
size
;
}
}
else
{
pBuf
->
statis
.
flushBytes
+=
size
;
pBuf
->
statis
.
flushPages
+=
1
;
}
else
if
(
pg
->
dirty
)
{
// length becomes greater, current space is not enough, allocate new place, otherwise, do nothing
if
(
pg
->
length
<
size
)
{
// 1. add current space to free list
SPageDiskInfo
dinfo
=
{.
length
=
pg
->
length
,
.
offset
=
pg
->
offset
};
taosArrayPush
(
p
Result
Buf
->
pFree
,
&
dinfo
);
taosArrayPush
(
pBuf
->
pFree
,
&
dinfo
);
// 2. allocate new position, and update the info
pg
->
offset
=
allocatePositionInFile
(
p
Result
Buf
,
size
);
p
Result
Buf
->
nextPos
+=
size
;
pg
->
offset
=
allocatePositionInFile
(
pBuf
,
size
);
pBuf
->
nextPos
+=
size
;
}
//3. write to disk.
int32_t
ret
=
fseek
(
p
Result
Buf
->
file
,
pg
->
offset
,
SEEK_SET
);
//
3. write to disk.
int32_t
ret
=
fseek
(
pBuf
->
file
,
pg
->
offset
,
SEEK_SET
);
if
(
ret
!=
0
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
NULL
;
}
ret
=
(
int32_t
)
fwrite
(
t
,
1
,
size
,
p
Result
Buf
->
file
);
ret
=
(
int32_t
)
fwrite
(
t
,
1
,
size
,
pBuf
->
file
);
if
(
ret
!=
size
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
NULL
;
}
if
(
p
Result
Buf
->
fileSize
<
pg
->
offset
+
size
)
{
p
Result
Buf
->
fileSize
=
pg
->
offset
+
size
;
if
(
pBuf
->
fileSize
<
pg
->
offset
+
size
)
{
pBuf
->
fileSize
=
pg
->
offset
+
size
;
}
pBuf
->
statis
.
flushBytes
+=
size
;
pBuf
->
statis
.
flushPages
+=
1
;
}
char
*
ret
=
pg
->
pData
;
memset
(
ret
,
0
,
pResult
Buf
->
pageSize
);
char
*
pDataBuf
=
pg
->
pData
;
memset
(
pDataBuf
,
0
,
p
Buf
->
pageSize
);
pg
->
pData
=
NULL
;
pg
->
pData
=
NULL
;
// this means the data is not in buffer
pg
->
length
=
size
;
pg
->
dirty
=
false
;
pResultBuf
->
statis
.
flushBytes
+=
pg
->
length
;
return
ret
;
return
pDataBuf
;
}
static
char
*
flushPageToDisk
(
SDiskbasedBuf
*
p
Result
Buf
,
SPageInfo
*
pg
)
{
static
char
*
flushPageToDisk
(
SDiskbasedBuf
*
pBuf
,
SPageInfo
*
pg
)
{
int32_t
ret
=
TSDB_CODE_SUCCESS
;
assert
(((
int64_t
)
p
ResultBuf
->
numOfPages
*
pResultBuf
->
pageSize
)
==
pResultBuf
->
totalBufSize
&&
pResultBuf
->
numOfPages
>=
pResult
Buf
->
inMemPages
);
assert
(((
int64_t
)
p
Buf
->
numOfPages
*
pBuf
->
pageSize
)
==
pBuf
->
totalBufSize
&&
pBuf
->
numOfPages
>=
p
Buf
->
inMemPages
);
if
(
p
Result
Buf
->
file
==
NULL
)
{
if
((
ret
=
createDiskFile
(
p
Result
Buf
))
!=
TSDB_CODE_SUCCESS
)
{
if
(
pBuf
->
file
==
NULL
)
{
if
((
ret
=
createDiskFile
(
pBuf
))
!=
TSDB_CODE_SUCCESS
)
{
terrno
=
ret
;
return
NULL
;
}
}
return
doFlushPageToDisk
(
p
Result
Buf
,
pg
);
return
doFlushPageToDisk
(
pBuf
,
pg
);
}
// load file block data in disk
static
char
*
loadPageFromDisk
(
SDiskbasedBuf
*
pResultBuf
,
SPageInfo
*
pg
)
{
int32_t
ret
=
fseek
(
pResultBuf
->
file
,
pg
->
offset
,
SEEK_SET
);
ret
=
(
int32_t
)
fread
(
GET_DATA_PAYLOAD
(
pg
),
1
,
pg
->
length
,
pResultBuf
->
file
);
static
int32_t
loadPageFromDisk
(
SDiskbasedBuf
*
pBuf
,
SPageInfo
*
pg
)
{
int32_t
ret
=
fseek
(
pBuf
->
file
,
pg
->
offset
,
SEEK_SET
);
if
(
ret
!=
0
)
{
ret
=
TAOS_SYSTEM_ERROR
(
errno
);
return
ret
;
}
ret
=
(
int32_t
)
fread
(
GET_DATA_PAYLOAD
(
pg
),
1
,
pg
->
length
,
pBuf
->
file
);
if
(
ret
!=
pg
->
length
)
{
terrno
=
errno
;
return
NULL
;
ret
=
TAOS_SYSTEM_ERROR
(
errno
)
;
return
ret
;
}
pResultBuf
->
statis
.
loadBytes
+=
pg
->
length
;
pBuf
->
statis
.
loadBytes
+=
pg
->
length
;
pBuf
->
statis
.
loadPages
+=
1
;
int32_t
fullSize
=
0
;
doDecompressData
(
GET_DATA_PAYLOAD
(
pg
),
pg
->
length
,
&
fullSize
,
pResultBuf
);
return
(
char
*
)
GET_DATA_PAYLOAD
(
pg
);
doDecompressData
(
GET_DATA_PAYLOAD
(
pg
),
pg
->
length
,
&
fullSize
,
pBuf
);
return
0
;
}
static
SIDList
addNewGroup
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
groupId
)
{
assert
(
taosHashGet
(
p
Result
Buf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
))
==
NULL
);
static
SIDList
addNewGroup
(
SDiskbasedBuf
*
pBuf
,
int32_t
groupId
)
{
assert
(
taosHashGet
(
pBuf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
))
==
NULL
);
SArray
*
pa
=
taosArrayInit
(
1
,
POINTER_BYTES
);
int32_t
ret
=
taosHashPut
(
p
Result
Buf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
),
&
pa
,
POINTER_BYTES
);
int32_t
ret
=
taosHashPut
(
pBuf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
),
&
pa
,
POINTER_BYTES
);
assert
(
ret
==
0
);
return
pa
;
}
static
SPageInfo
*
registerPage
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
groupId
,
int32_t
pageId
)
{
static
SPageInfo
*
registerPage
(
SDiskbasedBuf
*
pBuf
,
int32_t
groupId
,
int32_t
pageId
)
{
SIDList
list
=
NULL
;
char
**
p
=
taosHashGet
(
p
Result
Buf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
));
char
**
p
=
taosHashGet
(
pBuf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
));
if
(
p
==
NULL
)
{
// it is a new group id
list
=
addNewGroup
(
p
Result
Buf
,
groupId
);
list
=
addNewGroup
(
pBuf
,
groupId
);
}
else
{
list
=
(
SIDList
)
(
*
p
);
}
p
Result
Buf
->
numOfPages
+=
1
;
pBuf
->
numOfPages
+=
1
;
SPageInfo
*
ppi
=
malloc
(
sizeof
(
SPageInfo
));
//{ .info = PAGE_INFO_INITIALIZER, .pageId = pageId, .pn = NULL};
...
...
@@ -286,9 +305,9 @@ static SPageInfo* registerPage(SDiskbasedBuf* pResultBuf, int32_t groupId, int32
return
*
(
SPageInfo
**
)
taosArrayPush
(
list
,
&
ppi
);
}
static
SListNode
*
getEldestUnrefedPage
(
SDiskbasedBuf
*
p
Result
Buf
)
{
static
SListNode
*
getEldestUnrefedPage
(
SDiskbasedBuf
*
pBuf
)
{
SListIter
iter
=
{
0
};
tdListInitIter
(
p
Result
Buf
->
lruList
,
&
iter
,
TD_LIST_BACKWARD
);
tdListInitIter
(
pBuf
->
lruList
,
&
iter
,
TD_LIST_BACKWARD
);
SListNode
*
pn
=
NULL
;
while
((
pn
=
tdListNext
(
&
iter
))
!=
NULL
)
{
...
...
@@ -305,23 +324,22 @@ static SListNode* getEldestUnrefedPage(SDiskbasedBuf* pResultBuf) {
return
pn
;
}
static
char
*
evacOneDataPage
(
SDiskbasedBuf
*
p
Result
Buf
)
{
static
char
*
evacOneDataPage
(
SDiskbasedBuf
*
pBuf
)
{
char
*
bufPage
=
NULL
;
SListNode
*
pn
=
getEldestUnrefedPage
(
p
Result
Buf
);
SListNode
*
pn
=
getEldestUnrefedPage
(
pBuf
);
// all pages are referenced by user, try to allocate new space
if
(
pn
==
NULL
)
{
assert
(
0
);
int32_t
prev
=
p
Result
Buf
->
inMemPages
;
int32_t
prev
=
pBuf
->
inMemPages
;
// increase by 50% of previous mem pages
p
ResultBuf
->
inMemPages
=
(
int32_t
)(
pResult
Buf
->
inMemPages
*
1
.
5
f
);
p
Buf
->
inMemPages
=
(
int32_t
)(
p
Buf
->
inMemPages
*
1
.
5
f
);
// qWarn("%p in memory buf page not sufficient, expand from %d to %d, page size:%d", p
Result
Buf, prev,
// p
ResultBuf->inMemPages, pResult
Buf->pageSize);
// qWarn("%p in memory buf page not sufficient, expand from %d to %d, page size:%d", pBuf, prev,
// p
Buf->inMemPages, p
Buf->pageSize);
}
else
{
pResultBuf
->
statis
.
flushPages
+=
1
;
tdListPopNode
(
pResultBuf
->
lruList
,
pn
);
tdListPopNode
(
pBuf
->
lruList
,
pn
);
SPageInfo
*
d
=
*
(
SPageInfo
**
)
pn
->
data
;
assert
(
d
->
pn
==
pn
);
...
...
@@ -329,7 +347,7 @@ static char* evacOneDataPage(SDiskbasedBuf* pResultBuf) {
d
->
pn
=
NULL
;
tfree
(
pn
);
bufPage
=
flushPageToDisk
(
p
Result
Buf
,
d
);
bufPage
=
flushPageToDisk
(
pBuf
,
d
);
}
return
bufPage
;
...
...
@@ -350,12 +368,12 @@ static FORCE_INLINE size_t getAllocPageSize(int32_t pageSize) {
return
pageSize
+
POINTER_BYTES
+
2
+
sizeof
(
SFilePage
);
}
SFilePage
*
getNewDataBuf
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
groupId
,
int32_t
*
pageId
)
{
p
Result
Buf
->
statis
.
getPages
+=
1
;
SFilePage
*
getNewDataBuf
(
SDiskbasedBuf
*
pBuf
,
int32_t
groupId
,
int32_t
*
pageId
)
{
pBuf
->
statis
.
getPages
+=
1
;
char
*
availablePage
=
NULL
;
if
(
NO_IN_MEM_AVAILABLE_PAGES
(
p
Result
Buf
))
{
availablePage
=
evacOneDataPage
(
p
Result
Buf
);
if
(
NO_IN_MEM_AVAILABLE_PAGES
(
pBuf
))
{
availablePage
=
evacOneDataPage
(
pBuf
);
// Failed to allocate a new buffer page, and there is an error occurs.
if
(
availablePage
==
NULL
)
{
...
...
@@ -364,26 +382,26 @@ SFilePage* getNewDataBuf(SDiskbasedBuf* pResultBuf, int32_t groupId, int32_t* pa
}
// register new id in this group
*
pageId
=
(
++
p
Result
Buf
->
allocateId
);
*
pageId
=
(
++
pBuf
->
allocateId
);
// register page id info
SPageInfo
*
pi
=
registerPage
(
p
Result
Buf
,
groupId
,
*
pageId
);
SPageInfo
*
pi
=
registerPage
(
pBuf
,
groupId
,
*
pageId
);
// add to LRU list
assert
(
listNEles
(
p
ResultBuf
->
lruList
)
<
pResultBuf
->
inMemPages
&&
pResult
Buf
->
inMemPages
>
0
);
lruListPushFront
(
p
Result
Buf
->
lruList
,
pi
);
assert
(
listNEles
(
p
Buf
->
lruList
)
<
pBuf
->
inMemPages
&&
p
Buf
->
inMemPages
>
0
);
lruListPushFront
(
pBuf
->
lruList
,
pi
);
// add to hash map
taosHashPut
(
p
Result
Buf
->
all
,
pageId
,
sizeof
(
int32_t
),
&
pi
,
POINTER_BYTES
);
taosHashPut
(
pBuf
->
all
,
pageId
,
sizeof
(
int32_t
),
&
pi
,
POINTER_BYTES
);
// allocate buf
if
(
availablePage
==
NULL
)
{
pi
->
pData
=
calloc
(
1
,
getAllocPageSize
(
p
Result
Buf
->
pageSize
));
// add extract bytes in case of zipped buffer increased.
pi
->
pData
=
calloc
(
1
,
getAllocPageSize
(
pBuf
->
pageSize
));
// add extract bytes in case of zipped buffer increased.
}
else
{
pi
->
pData
=
availablePage
;
}
p
ResultBuf
->
totalBufSize
+=
pResult
Buf
->
pageSize
;
p
Buf
->
totalBufSize
+=
p
Buf
->
pageSize
;
((
void
**
)
pi
->
pData
)[
0
]
=
pi
;
pi
->
used
=
true
;
...
...
@@ -391,16 +409,16 @@ SFilePage* getNewDataBuf(SDiskbasedBuf* pResultBuf, int32_t groupId, int32_t* pa
return
(
void
*
)(
GET_DATA_PAYLOAD
(
pi
));
}
SFilePage
*
getBufPage
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
id
)
{
assert
(
p
Result
Buf
!=
NULL
&&
id
>=
0
);
p
Result
Buf
->
statis
.
getPages
+=
1
;
SFilePage
*
getBufPage
(
SDiskbasedBuf
*
pBuf
,
int32_t
id
)
{
assert
(
pBuf
!=
NULL
&&
id
>=
0
);
pBuf
->
statis
.
getPages
+=
1
;
SPageInfo
**
pi
=
taosHashGet
(
p
Result
Buf
->
all
,
&
id
,
sizeof
(
int32_t
));
SPageInfo
**
pi
=
taosHashGet
(
pBuf
->
all
,
&
id
,
sizeof
(
int32_t
));
assert
(
pi
!=
NULL
&&
*
pi
!=
NULL
);
if
((
*
pi
)
->
pData
!=
NULL
)
{
// it is in memory
// no need to update the LRU list if only one page exists
if
(
p
Result
Buf
->
numOfPages
==
1
)
{
if
(
pBuf
->
numOfPages
==
1
)
{
(
*
pi
)
->
used
=
true
;
return
(
void
*
)(
GET_DATA_PAYLOAD
(
*
pi
));
}
...
...
@@ -408,7 +426,7 @@ SFilePage* getBufPage(SDiskbasedBuf* pResultBuf, int32_t id) {
SPageInfo
**
pInfo
=
(
SPageInfo
**
)
((
*
pi
)
->
pn
->
data
);
assert
(
*
pInfo
==
*
pi
);
lruListMoveToFront
(
p
Result
Buf
->
lruList
,
(
*
pi
));
lruListMoveToFront
(
pBuf
->
lruList
,
(
*
pi
));
(
*
pi
)
->
used
=
true
;
return
(
void
*
)(
GET_DATA_PAYLOAD
(
*
pi
));
...
...
@@ -417,79 +435,93 @@ SFilePage* getBufPage(SDiskbasedBuf* pResultBuf, int32_t id) {
assert
((
*
pi
)
->
pData
==
NULL
&&
(
*
pi
)
->
pn
==
NULL
&&
(
*
pi
)
->
length
>=
0
&&
(
*
pi
)
->
offset
>=
0
);
char
*
availablePage
=
NULL
;
if
(
NO_IN_MEM_AVAILABLE_PAGES
(
p
Result
Buf
))
{
availablePage
=
evacOneDataPage
(
p
Result
Buf
);
if
(
NO_IN_MEM_AVAILABLE_PAGES
(
pBuf
))
{
availablePage
=
evacOneDataPage
(
pBuf
);
if
(
availablePage
==
NULL
)
{
return
NULL
;
}
}
if
(
availablePage
==
NULL
)
{
(
*
pi
)
->
pData
=
calloc
(
1
,
getAllocPageSize
(
p
Result
Buf
->
pageSize
));
(
*
pi
)
->
pData
=
calloc
(
1
,
getAllocPageSize
(
pBuf
->
pageSize
));
}
else
{
(
*
pi
)
->
pData
=
availablePage
;
}
((
void
**
)((
*
pi
)
->
pData
))[
0
]
=
(
*
pi
);
lruListPushFront
(
p
Result
Buf
->
lruList
,
*
pi
);
lruListPushFront
(
pBuf
->
lruList
,
*
pi
);
(
*
pi
)
->
used
=
true
;
loadPageFromDisk
(
pResultBuf
,
*
pi
);
int32_t
code
=
loadPageFromDisk
(
pBuf
,
*
pi
);
if
(
code
!=
0
)
{
return
NULL
;
}
return
(
void
*
)(
GET_DATA_PAYLOAD
(
*
pi
));
}
}
void
releaseBufPage
(
SDiskbasedBuf
*
pResultBuf
,
void
*
page
)
{
assert
(
pResultBuf
!=
NULL
&&
page
!=
NULL
);
char
*
p
=
(
char
*
)
page
-
POINTER_BYTES
;
void
releaseBufPage
(
SDiskbasedBuf
*
pBuf
,
void
*
page
)
{
assert
(
pBuf
!=
NULL
&&
page
!=
NULL
);
int32_t
offset
=
offsetof
(
SPageInfo
,
pData
);
char
*
p
=
page
-
offset
;
SPageInfo
*
ppi
=
((
SPageInfo
**
)
p
)[
0
];
releaseBufPageInfo
(
p
Result
Buf
,
ppi
);
releaseBufPageInfo
(
pBuf
,
ppi
);
}
void
releaseBufPageInfo
(
SDiskbasedBuf
*
p
Result
Buf
,
SPageInfo
*
pi
)
{
void
releaseBufPageInfo
(
SDiskbasedBuf
*
pBuf
,
SPageInfo
*
pi
)
{
assert
(
pi
->
pData
!=
NULL
&&
pi
->
used
);
pi
->
used
=
false
;
p
Result
Buf
->
statis
.
releasePages
+=
1
;
pBuf
->
statis
.
releasePages
+=
1
;
}
size_t
getNumOfResultBufGroupId
(
const
SDiskbasedBuf
*
p
ResultBuf
)
{
return
taosHashGetSize
(
pResult
Buf
->
groupSet
);
}
size_t
getNumOfResultBufGroupId
(
const
SDiskbasedBuf
*
p
Buf
)
{
return
taosHashGetSize
(
p
Buf
->
groupSet
);
}
size_t
getTotalBufSize
(
const
SDiskbasedBuf
*
p
ResultBuf
)
{
return
(
size_t
)
pResult
Buf
->
totalBufSize
;
}
size_t
getTotalBufSize
(
const
SDiskbasedBuf
*
p
Buf
)
{
return
(
size_t
)
p
Buf
->
totalBufSize
;
}
SIDList
getDataBufPagesIdList
(
SDiskbasedBuf
*
p
Result
Buf
,
int32_t
groupId
)
{
assert
(
p
Result
Buf
!=
NULL
);
SIDList
getDataBufPagesIdList
(
SDiskbasedBuf
*
pBuf
,
int32_t
groupId
)
{
assert
(
pBuf
!=
NULL
);
char
**
p
=
taosHashGet
(
p
Result
Buf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
));
char
**
p
=
taosHashGet
(
pBuf
->
groupSet
,
(
const
char
*
)
&
groupId
,
sizeof
(
int32_t
));
if
(
p
==
NULL
)
{
// it is a new group id
return
p
Result
Buf
->
emptyDummyIdList
;
return
pBuf
->
emptyDummyIdList
;
}
else
{
return
(
SArray
*
)
(
*
p
);
}
}
void
destroyResultBuf
(
SDiskbasedBuf
*
p
Result
Buf
)
{
if
(
p
Result
Buf
==
NULL
)
{
void
destroyResultBuf
(
SDiskbasedBuf
*
pBuf
)
{
if
(
pBuf
==
NULL
)
{
return
;
}
if
(
pResultBuf
->
file
!=
NULL
)
{
// qDebug("QInfo:0x%"PRIx64" res output buffer closed, total:%.2f Kb, inmem size:%.2f Kb, file size:%.2f Kb",
// pResultBuf->qId, pResultBuf->totalBufSize/1024.0, listNEles(pResultBuf->lruList) * pResultBuf->pageSize / 1024.0,
// pResultBuf->fileSize/1024.0);
printStatisData
(
pBuf
);
if
(
pBuf
->
file
!=
NULL
)
{
uDebug
(
"Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page size:%.2f Kb, %"
PRIx64
"
\n
"
,
pBuf
->
totalBufSize
/
1024
.
0
,
pBuf
->
numOfPages
,
listNEles
(
pBuf
->
lruList
)
*
pBuf
->
pageSize
/
1024
.
0
,
listNEles
(
pBuf
->
lruList
),
pBuf
->
fileSize
/
1024
.
0
,
pBuf
->
pageSize
/
1024
.
0
f
,
pBuf
->
qId
);
fclose
(
pResult
Buf
->
file
);
fclose
(
p
Buf
->
file
);
}
else
{
// qDebug("QInfo:0x%"PRIx64" res output buffer closed, total:%.2f Kb, no file created", pResultBuf->qId,
// pResultBuf->totalBufSize/1024.0);
uDebug
(
"Paged buffer closed, total:%.2f Kb, no file created, %"
PRIx64
,
pBuf
->
totalBufSize
/
1024
.
0
,
pBuf
->
qId
);
}
remove
(
pResultBuf
->
path
);
tfree
(
pResultBuf
->
path
);
// print the statistics information
{
SDiskbasedBufStatis
*
ps
=
&
pBuf
->
statis
;
uDebug
(
"Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f Kb
\n
"
,
ps
->
getPages
,
ps
->
releasePages
,
ps
->
flushBytes
/
1024
.
0
f
,
ps
->
flushPages
,
ps
->
loadBytes
/
1024
.
0
f
,
ps
->
loadPages
,
ps
->
loadBytes
/
(
1024
.
0
*
ps
->
loadPages
));
}
SArray
**
p
=
taosHashIterate
(
pResultBuf
->
groupSet
,
NULL
);
remove
(
pBuf
->
path
);
tfree
(
pBuf
->
path
);
SArray
**
p
=
taosHashIterate
(
pBuf
->
groupSet
,
NULL
);
while
(
p
)
{
size_t
n
=
taosArrayGetSize
(
*
p
);
for
(
int32_t
i
=
0
;
i
<
n
;
++
i
)
{
...
...
@@ -499,16 +531,16 @@ void destroyResultBuf(SDiskbasedBuf* pResultBuf) {
}
taosArrayDestroy
(
*
p
);
p
=
taosHashIterate
(
p
Result
Buf
->
groupSet
,
p
);
p
=
taosHashIterate
(
pBuf
->
groupSet
,
p
);
}
tdListFree
(
p
Result
Buf
->
lruList
);
taosArrayDestroy
(
p
Result
Buf
->
emptyDummyIdList
);
taosHashCleanup
(
p
Result
Buf
->
groupSet
);
taosHashCleanup
(
p
Result
Buf
->
all
);
tdListFree
(
pBuf
->
lruList
);
taosArrayDestroy
(
pBuf
->
emptyDummyIdList
);
taosHashCleanup
(
pBuf
->
groupSet
);
taosHashCleanup
(
pBuf
->
all
);
tfree
(
p
Result
Buf
->
assistBuf
);
tfree
(
p
Result
Buf
);
tfree
(
pBuf
->
assistBuf
);
tfree
(
pBuf
);
}
SPageInfo
*
getLastPageInfo
(
SIDList
pList
)
{
...
...
@@ -522,14 +554,41 @@ int32_t getPageId(const SPageInfo* pPgInfo) {
return
pPgInfo
->
pageId
;
}
int32_t
getBufPageSize
(
const
SDiskbasedBuf
*
pResultBuf
)
{
return
pResultBuf
->
pageSize
;
int32_t
getBufPageSize
(
const
SDiskbasedBuf
*
pBuf
)
{
return
pBuf
->
pageSize
;
}
bool
isAllDataInMemBuf
(
const
SDiskbasedBuf
*
pBuf
)
{
return
pBuf
->
fileSize
==
0
;
}
void
setBufPageDirty
(
SFilePage
*
pPage
,
bool
dirty
)
{
int32_t
offset
=
offsetof
(
SPageInfo
,
pData
);
// todo extract method
char
*
p
=
(
char
*
)
pPage
-
offset
;
SPageInfo
*
ppi
=
((
SPageInfo
**
)
p
)[
0
];
ppi
->
dirty
=
dirty
;
}
bool
isAllDataInMemBuf
(
const
SDiskbasedBuf
*
pResult
Buf
)
{
return
pResultBuf
->
fileSize
==
0
;
void
printStatisBeforeClose
(
SDiskbasedBuf
*
p
Buf
)
{
pBuf
->
printStatis
=
true
;
}
void
setBufPageDirty
(
SPageInfo
*
pPageInfo
,
bool
dirty
)
{
pPageInfo
->
dirty
=
dirty
;
void
printStatisData
(
const
SDiskbasedBuf
*
pBuf
)
{
if
(
!
pBuf
->
printStatis
)
{
return
;
}
const
SDiskbasedBufStatis
*
ps
=
&
pBuf
->
statis
;
printf
(
"Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page size:%.2f "
"Kb, %"
PRIx64
"
\n
"
,
pBuf
->
totalBufSize
/
1024
.
0
,
pBuf
->
numOfPages
,
listNEles
(
pBuf
->
lruList
)
*
pBuf
->
pageSize
/
1024
.
0
,
listNEles
(
pBuf
->
lruList
),
pBuf
->
fileSize
/
1024
.
0
,
pBuf
->
pageSize
/
1024
.
0
f
,
pBuf
->
qId
);
printf
(
"Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f Kb
\n
"
,
ps
->
getPages
,
ps
->
releasePages
,
ps
->
flushBytes
/
1024
.
0
f
,
ps
->
flushPages
,
ps
->
loadBytes
/
1024
.
0
f
,
ps
->
loadPages
,
ps
->
loadBytes
/
(
1024
.
0
*
ps
->
loadPages
));
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录