提交 442f0165 编写于 作者: H Hongze Cheng

tdb debug

上级 e95e8f45
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
#include "tdbInt.h" #include "tdbInt.h"
struct SPCache { struct SPCache {
int pageSize; int szPage;
int cacheSize; int nPages;
SPage **aPage;
tdb_mutex_t mutex; tdb_mutex_t mutex;
SPage *pList;
int nFree; int nFree;
SPage *pFree; SPage *pFree;
int nPage; int nPage;
...@@ -52,13 +52,14 @@ int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) { ...@@ -52,13 +52,14 @@ int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
void *pPtr; void *pPtr;
SPage *pPgHdr; SPage *pPgHdr;
pCache = (SPCache *)tdbOsCalloc(1, sizeof(*pCache)); pCache = (SPCache *)tdbOsCalloc(1, sizeof(*pCache) + sizeof(SPage *) * cacheSize);
if (pCache == NULL) { if (pCache == NULL) {
return -1; return -1;
} }
pCache->pageSize = pageSize; pCache->szPage = pageSize;
pCache->cacheSize = cacheSize; pCache->nPages = cacheSize;
pCache->aPage = (SPage **)&pCache[1];
if (tdbPCacheOpenImpl(pCache) < 0) { if (tdbPCacheOpenImpl(pCache) < 0) {
tdbOsFree(pCache); tdbOsFree(pCache);
...@@ -123,7 +124,7 @@ void tdbPCacheRelease(SPCache *pCache, SPage *pPage, TXN *pTxn) { ...@@ -123,7 +124,7 @@ void tdbPCacheRelease(SPCache *pCache, SPage *pPage, TXN *pTxn) {
} }
} }
int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; } int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->szPage; }
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) { static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) {
int ret = 0; int ret = 0;
...@@ -168,7 +169,7 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) ...@@ -168,7 +169,7 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn)
// 4. Try a create new page // 4. Try a create new page
if (!pPage) { if (!pPage) {
ret = tdbPageCreate(pCache->pageSize, &pPage, pTxn->xMalloc, pTxn->xArg); ret = tdbPageCreate(pCache->szPage, &pPage, pTxn->xMalloc, pTxn->xArg);
if (ret < 0) { if (ret < 0) {
// TODO // TODO
ASSERT(0); ASSERT(0);
...@@ -218,12 +219,15 @@ static void tdbPCachePinPage(SPCache *pCache, SPage *pPage) { ...@@ -218,12 +219,15 @@ static void tdbPCachePinPage(SPCache *pCache, SPage *pPage) {
pPage->pLruNext = NULL; pPage->pLruNext = NULL;
pCache->nRecyclable--; pCache->nRecyclable--;
tdbDebug("pin page %d", pPage->id);
} }
} }
static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) { static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) {
i32 nRef; i32 nRef;
ASSERT(pPage->isLocal);
ASSERT(!pPage->isDirty); ASSERT(!pPage->isDirty);
ASSERT(TDB_GET_PAGE_REF(pPage) == 0); ASSERT(TDB_GET_PAGE_REF(pPage) == 0);
...@@ -235,6 +239,8 @@ static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) { ...@@ -235,6 +239,8 @@ static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) {
pCache->lru.pLruNext = pPage; pCache->lru.pLruNext = pPage;
pCache->nRecyclable++; pCache->nRecyclable++;
tdbDebug("unpin page %d", pPage->id);
} }
static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) { static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) {
...@@ -248,6 +254,8 @@ static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) { ...@@ -248,6 +254,8 @@ static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) {
*ppPage = pPage->pHashNext; *ppPage = pPage->pHashNext;
pCache->nPage--; pCache->nPage--;
tdbDebug("remove page %d to hash", pPage->id);
} }
static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) { static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) {
...@@ -259,6 +267,8 @@ static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) { ...@@ -259,6 +267,8 @@ static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) {
pCache->pgHash[h] = pPage; pCache->pgHash[h] = pPage;
pCache->nPage++; pCache->nPage++;
tdbDebug("add page %d to hash", pPage->id);
} }
static int tdbPCacheOpenImpl(SPCache *pCache) { static int tdbPCacheOpenImpl(SPCache *pCache) {
...@@ -272,8 +282,8 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { ...@@ -272,8 +282,8 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
// Open the free list // Open the free list
pCache->nFree = 0; pCache->nFree = 0;
pCache->pFree = NULL; pCache->pFree = NULL;
for (int i = 0; i < pCache->cacheSize; i++) { for (int i = 0; i < pCache->nPages; i++) {
ret = tdbPageCreate(pCache->pageSize, &pPage, tdbDefaultMalloc, NULL); ret = tdbPageCreate(pCache->szPage, &pPage, tdbDefaultMalloc, NULL);
if (ret < 0) { if (ret < 0) {
// TODO: handle error // TODO: handle error
return -1; return -1;
...@@ -294,13 +304,13 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { ...@@ -294,13 +304,13 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
pCache->nFree++; pCache->nFree++;
// add to local list // add to local list
pPage->pCacheNext = pCache->pList; pPage->id = i;
pCache->pList = pPage; pCache->aPage[i] = pPage;
} }
// Open the hash table // Open the hash table
pCache->nPage = 0; pCache->nPage = 0;
pCache->nHash = pCache->cacheSize < 8 ? 8 : pCache->cacheSize; pCache->nHash = pCache->nPages < 8 ? 8 : pCache->nPages;
pCache->pgHash = (SPage **)tdbOsCalloc(pCache->nHash, sizeof(SPage *)); pCache->pgHash = (SPage **)tdbOsCalloc(pCache->nHash, sizeof(SPage *));
if (pCache->pgHash == NULL) { if (pCache->pgHash == NULL) {
// TODO // TODO
...@@ -319,9 +329,11 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { ...@@ -319,9 +329,11 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
static int tdbPCacheCloseImpl(SPCache *pCache) { static int tdbPCacheCloseImpl(SPCache *pCache) {
SPage *pPage; SPage *pPage;
for (pPage = pCache->pList; pPage; pPage = pCache->pList) { for (i32 iPage = 0; iPage < pCache->nPages; iPage++) {
pCache->pList = pPage->pCacheNext; if (pCache->aPage[iPage]) {
tdbPageDestroy(pPage, tdbDefaultFree, NULL); tdbPageDestroy(pPage, tdbDefaultFree, NULL);
pCache->aPage[iPage] = NULL;
}
} }
tdbOsFree(pCache->pgHash); tdbOsFree(pCache->pgHash);
......
...@@ -18,10 +18,23 @@ ...@@ -18,10 +18,23 @@
#include "tdb.h" #include "tdb.h"
#include "tlog.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// clang-format off
extern int32_t tdbDebugFlag;
#define tdbFatal(...) do { if (tdbDebugFlag & DEBUG_FATAL) { taosPrintLog("TDB FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }} while(0)
#define tdbError(...) do { if (tdbDebugFlag & DEBUG_ERROR) { taosPrintLog("TDB ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }} while(0)
#define tdbWarn(...) do { if (tdbDebugFlag & DEBUG_WARN) { taosPrintLog("TDB WARN ", DEBUG_WARN, 255, __VA_ARGS__); }} while(0)
#define tdbInfo(...) do { if (tdbDebugFlag & DEBUG_INFO) { taosPrintLog("TDB ", DEBUG_INFO, 255, __VA_ARGS__); }} while(0)
#define tdbDebug(...) do { if (tdbDebugFlag & DEBUG_DEBUG) { taosPrintLog("TDB ", DEBUG_DEBUG, tdbDebugFlag, __VA_ARGS__); }} while(0)
#define tdbTrace(...) do { if (tdbDebugFlag & DEBUG_TRACE) { taosPrintLog("TDB ", DEBUG_TRACE, tdbDebugFlag, __VA_ARGS__); }} while(0)
// clang-format on
typedef int8_t i8; typedef int8_t i8;
typedef int16_t i16; typedef int16_t i16;
typedef int32_t i32; typedef int32_t i32;
...@@ -166,7 +179,7 @@ int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno); ...@@ -166,7 +179,7 @@ int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno);
u8 isLocal; \ u8 isLocal; \
u8 isDirty; \ u8 isDirty; \
i32 nRef; \ i32 nRef; \
SPage *pCacheNext; \ i32 id; \
SPage *pFreeNext; \ SPage *pFreeNext; \
SPage *pHashNext; \ SPage *pHashNext; \
SPage *pLruNext; \ SPage *pLruNext; \
......
...@@ -14,8 +14,8 @@ ...@@ -14,8 +14,8 @@
*/ */
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "os.h"
#include "tlog.h" #include "tlog.h"
#include "os.h"
#include "tutil.h" #include "tutil.h"
#define LOG_MAX_LINE_SIZE (1024) #define LOG_MAX_LINE_SIZE (1024)
...@@ -90,6 +90,7 @@ int32_t qDebugFlag = 131; ...@@ -90,6 +90,7 @@ int32_t qDebugFlag = 131;
int32_t wDebugFlag = 135; int32_t wDebugFlag = 135;
int32_t sDebugFlag = 135; int32_t sDebugFlag = 135;
int32_t tsdbDebugFlag = 131; int32_t tsdbDebugFlag = 131;
int32_t tdbDebugFlag = 131;
int32_t tqDebugFlag = 135; int32_t tqDebugFlag = 135;
int32_t fsDebugFlag = 135; int32_t fsDebugFlag = 135;
int32_t metaDebugFlag = 135; int32_t metaDebugFlag = 135;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册