From 15d9a46600db7362be4e2cd8486226ad9e0de7d2 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 12 Nov 2021 10:53:52 +0800 Subject: [PATCH] more --- include/server/vnode/meta/meta.h | 20 +- include/server/vnode/vnode.h | 1 + include/util/mallocator.h | 8 +- include/util/tlist.h | 3 +- ...vnodeAllocatorPool.h => vnodeBufferPool.h} | 22 +- source/dnode/vnode/impl/inc/vnodeDef.h | 23 +- source/dnode/vnode/impl/inc/vnodeRequest.h | 14 ++ .../dnode/vnode/impl/src/vnodeAllocatorPool.c | 37 --- source/dnode/vnode/impl/src/vnodeBufferPool.c | 211 ++++++++++++++++++ source/dnode/vnode/impl/src/vnodeMain.c | 7 - source/dnode/vnode/impl/src/vnodeWrite.c | 5 + source/util/src/tlist.c | 12 +- 12 files changed, 274 insertions(+), 89 deletions(-) rename source/dnode/vnode/impl/inc/{vnodeAllocatorPool.h => vnodeBufferPool.h} (59%) delete mode 100644 source/dnode/vnode/impl/src/vnodeAllocatorPool.c create mode 100644 source/dnode/vnode/impl/src/vnodeBufferPool.c diff --git a/include/server/vnode/meta/meta.h b/include/server/vnode/meta/meta.h index b94ffc7a5d..421f96ef5f 100644 --- a/include/server/vnode/meta/meta.h +++ b/include/server/vnode/meta/meta.h @@ -28,24 +28,24 @@ typedef struct SMetaOptions SMetaOptions; typedef struct STbOptions STbOptions; // SMeta operations -SMeta *metaOpen(const char *path, const SMetaOptions *); -void metaClose(SMeta *); +SMeta *metaOpen(const char *path, const SMetaOptions *pOptions); +void metaClose(SMeta *pMeta); void metaRemove(const char *path); -int metaCreateTable(SMeta *pMeta, const STbOptions *); +int metaCreateTable(SMeta *pMeta, const STbOptions *pTbOptions); int metaDropTable(SMeta *pMeta, tb_uid_t uid); -int metaCommit(SMeta *); +int metaCommit(SMeta *pMeta); // Options -void metaOptionsInit(SMetaOptions *); -void metaOptionsClear(SMetaOptions *); +void metaOptionsInit(SMetaOptions *pOptions); +void metaOptionsClear(SMetaOptions *pOptions); // STableOpts #define META_TABLE_OPTS_DECLARE(name) STableOpts name = {0} -void metaNormalTableOptsInit(STbOptions *, const char *name, const STSchema *pSchema); -void metaSuperTableOptsInit(STbOptions *, const char *name, tb_uid_t uid, const STSchema *pSchema, +void metaNormalTableOptsInit(STbOptions *pTbOptions, const char *name, const STSchema *pSchema); +void metaSuperTableOptsInit(STbOptions *pTbOptions, const char *name, tb_uid_t uid, const STSchema *pSchema, const STSchema *pTagSchema); -void metaChildTableOptsInit(STbOptions *, const char *name, tb_uid_t suid, const SKVRow tags); -void metaTableOptsClear(STbOptions *); +void metaChildTableOptsInit(STbOptions *pTbOptions, const char *name, tb_uid_t suid, const SKVRow tags); +void metaTableOptsClear(STbOptions *pTbOptions); #ifdef __cplusplus } diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index 1b1e1c1b0f..51cd643caf 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -70,6 +70,7 @@ int vnodeProcessWMsgs(SVnode *pVnode, SArray *pMsgs); * * @param pVnode The vnode object. * @param pMsg The request message + * @param pRsp The response message * @return int 0 for success, -1 for failure */ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp); diff --git a/include/util/mallocator.h b/include/util/mallocator.h index a4705bdd2c..204f344cb7 100644 --- a/include/util/mallocator.h +++ b/include/util/mallocator.h @@ -25,20 +25,14 @@ extern "C" { typedef struct SMemAllocator SMemAllocator; #define MALLOCATOR_APIS \ + void *impl; \ void *(*malloc)(SMemAllocator *, size_t size); \ void *(*calloc)(SMemAllocator *, size_t nmemb, size_t size); \ void *(*realloc)(SMemAllocator *, void *ptr, size_t size); \ void (*free)(SMemAllocator *, void *ptr); \ size_t (*usage)(SMemAllocator *); -// Interfaces to implement -typedef struct { - MALLOCATOR_APIS -} SMemAllocatorIf; - struct SMemAllocator { - void * impl; - size_t usize; MALLOCATOR_APIS }; diff --git a/include/util/tlist.h b/include/util/tlist.h index 8246205e9b..e803e96605 100644 --- a/include/util/tlist.h +++ b/include/util/tlist.h @@ -46,9 +46,10 @@ typedef struct { #define isListEmpty(l) ((l)->numOfEles == 0) #define listNodeFree(n) free(n) +void tdListInit(SList *list, int eleSize); +void tdListEmpty(SList *list); SList * tdListNew(int eleSize); void * tdListFree(SList *list); -void tdListEmpty(SList *list); void tdListPrependNode(SList *list, SListNode *node); void tdListAppendNode(SList *list, SListNode *node); int tdListPrepend(SList *list, void *data); diff --git a/source/dnode/vnode/impl/inc/vnodeAllocatorPool.h b/source/dnode/vnode/impl/inc/vnodeBufferPool.h similarity index 59% rename from source/dnode/vnode/impl/inc/vnodeAllocatorPool.h rename to source/dnode/vnode/impl/inc/vnodeBufferPool.h index f02329fa87..5c65b4f614 100644 --- a/source/dnode/vnode/impl/inc/vnodeAllocatorPool.h +++ b/source/dnode/vnode/impl/inc/vnodeBufferPool.h @@ -13,27 +13,27 @@ * along with this program. If not, see . */ -#ifndef _TD_VNODE_ALLOCATOR_POOL_H_ -#define _TD_VNODE_ALLOCATOR_POOL_H_ +#ifndef _TD_VNODE_BUFFER_POOL_H_ +#define _TD_VNODE_BUFFER_POOL_H_ +#include "tlist.h" #include "vnode.h" #ifdef __cplusplus extern "C" { #endif -typedef struct { - int nexta; - int enda; - SMemAllocator *free[3]; - SMemAllocator *used[3]; -} SVAllocatorPool; +typedef struct SVBufPool SVBufPool; -int vnodeOpenAllocatorPool(SVnode *pVnode); -void vnodeCloseAllocatorPool(SVnode *pVnode); +int vnodeOpenBufPool(SVnode *pVnode); +void vnodeCloseBufPool(SVnode *pVnode); +SMemAllocator *vnodeCreateMemAllocator(SVnode *pVnode); +void vnodeDestroyMemAllocator(SMemAllocator *pma); +void vnodeRefMemAllocator(SMemAllocator *pma); +void vnodeUnrefMemAllocator(SMemAllocator *pma); #ifdef __cplusplus } #endif -#endif /*_TD_VNODE_ALLOCATOR_POOL_H_*/ \ No newline at end of file +#endif /*_TD_VNODE_BUFFER_POOL_H_*/ \ No newline at end of file diff --git a/source/dnode/vnode/impl/inc/vnodeDef.h b/source/dnode/vnode/impl/inc/vnodeDef.h index 5ffbd8dcff..9cf9210cf3 100644 --- a/source/dnode/vnode/impl/inc/vnodeDef.h +++ b/source/dnode/vnode/impl/inc/vnodeDef.h @@ -22,7 +22,7 @@ #include "wal.h" #include "vnode.h" -#include "vnodeAllocatorPool.h" +#include "vnodeBufferPool.h" #include "vnodeCommit.h" #include "vnodeFileSystem.h" #include "vnodeOptions.h" @@ -34,17 +34,16 @@ extern "C" { #endif struct SVnode { - char* path; - SVnodeOptions options; - SVState state; - SVAllocatorPool* pool; - SMemAllocator* inuse; - SMeta* pMeta; - STsdb* pTsdb; - STQ* pTq; - SWal* pWal; - SVnodeSync* pSync; - SVnodeFS* pFs; + char* path; + SVnodeOptions options; + SVState state; + SVBufPool* pBufPool; + SMeta* pMeta; + STsdb* pTsdb; + STQ* pTq; + SWal* pWal; + SVnodeSync* pSync; + SVnodeFS* pFs; }; #ifdef __cplusplus diff --git a/source/dnode/vnode/impl/inc/vnodeRequest.h b/source/dnode/vnode/impl/inc/vnodeRequest.h index af909fb636..788918f105 100644 --- a/source/dnode/vnode/impl/inc/vnodeRequest.h +++ b/source/dnode/vnode/impl/inc/vnodeRequest.h @@ -20,6 +20,20 @@ extern "C" { #endif +typedef struct SVnodeReq SVnodeReq; +typedef struct SVnodeRsp SVnodeRsp; + +typedef enum { +} EVReqT; + +struct SVnodeReq { + /* TODO */ +}; + +struct SVnodeRsp { + /* TODO */ +}; + #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/impl/src/vnodeAllocatorPool.c b/source/dnode/vnode/impl/src/vnodeAllocatorPool.c deleted file mode 100644 index b5617210a0..0000000000 --- a/source/dnode/vnode/impl/src/vnodeAllocatorPool.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#include "vnodeDef.h" - -int vnodeOpenAllocatorPool(SVnode *pVnode) { - // TODO - return 0; -} - -void vnodeCloseAllocatorPool(SVnode *pVnode) { - if (pVnode->pool) { - } -} - -/* ------------------------ STATIC METHODS ------------------------ */ -static SVAllocatorPool *vapCreate() { - SVAllocatorPool *pPool = NULL; - /* TODO */ - return pPool; -} - -static void vapDestroy() { - // TODO -} \ No newline at end of file diff --git a/source/dnode/vnode/impl/src/vnodeBufferPool.c b/source/dnode/vnode/impl/src/vnodeBufferPool.c new file mode 100644 index 0000000000..d5005cf98c --- /dev/null +++ b/source/dnode/vnode/impl/src/vnodeBufferPool.c @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "vnodeDef.h" + +/* ------------------------ STRUCTURES ------------------------ */ +struct SVBufPool { + SList free; + SList incycle; + SListNode *inuse; +}; + +typedef enum { E_V_HEAP_ALLOCATOR = 0, E_V_ARENA_ALLOCATOR } EVMemAllocatorT; + +typedef struct { +} SVHeapAllocator; + +typedef struct SVArenaNode { + struct SVArenaNode *prev; + uint64_t size; + void * ptr; + char data[]; +} SVArenaNode; + +typedef struct { + SVArenaNode *inuse; + SVArenaNode node; +} SVArenaAllocator; + +typedef struct { + uint64_t capacity; + EVMemAllocatorT type; + T_REF_DECLARE() + union { + SVHeapAllocator vha; + SVArenaAllocator vaa; + }; +} SVMemAllocator; + +static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type); +static void vBufPoolFreeNode(SListNode *pNode); +static int vArenaAllocatorInit(SVArenaAllocator *pvaa); +static void vArenaAllocatorClear(SVArenaAllocator *pvaa); +static int vHeapAllocatorInit(SVHeapAllocator *pvha); +static void vHeapAllocatorClear(SVHeapAllocator *pvha); + +int vnodeOpenBufPool(SVnode *pVnode) { + uint64_t capacity; + EVMemAllocatorT type = E_V_ARENA_ALLOCATOR; + + if ((pVnode->pBufPool = (SVBufPool *)calloc(1, sizeof(SVBufPool))) == NULL) { + /* TODO */ + return -1; + } + + tdListInit(&(pVnode->pBufPool->free), 0); + tdListInit(&(pVnode->pBufPool->incycle), 0); + + capacity = pVnode->options.wsize / 3; + if (pVnode->options.isHeapAllocator) { + type = E_V_HEAP_ALLOCATOR; + } + + for (int i = 0; i < 3; i++) { + SListNode *pNode = vBufPoolNewNode(capacity, type); + if (pNode == NULL) { + vnodeCloseBufPool(pVnode); + return -1; + } + + tdListAppendNode(&(pVnode->pBufPool->free), pNode); + } + + pVnode->pBufPool->inuse = tdListPopHead(&(pVnode->pBufPool->free)); + + return 0; +} + +void vnodeCloseBufPool(SVnode *pVnode) { + SListNode *pNode; + if (pVnode->pBufPool) { + // Clear free list + while ((pNode = tdListPopHead(&(pVnode->pBufPool->free))) != NULL) { + vBufPoolFreeNode(pNode); + } + + // Clear incycle list + while ((pNode = tdListPopHead(&(pVnode->pBufPool->incycle))) != NULL) { + vBufPoolFreeNode(pNode); + } + + // Free inuse node + vBufPoolFreeNode(pVnode->pBufPool->inuse); + + free(pVnode->pBufPool); + pVnode->pBufPool = NULL; + } +} + +SMemAllocator *vnodeCreateMemAllocator(SVnode *pVnode) { + SMemAllocator *pma; + + pma = (SMemAllocator *)calloc(1, sizeof(*pma)); + if (pma == NULL) { + /* TODO */ + return NULL; + } + + pma->impl = pVnode; + if (pVnode->options.isHeapAllocator) { + /* TODO */ + pma->malloc = NULL; + pma->calloc = NULL; + pma->realloc = NULL; + pma->free = NULL; + pma->usage = NULL; + } else { + /* TODO */ + pma->malloc = NULL; + pma->calloc = NULL; + pma->realloc = NULL; + pma->free = NULL; + pma->usage = NULL; + } + + return pma; +} + +void vnodeDestroyMemAllocator(SMemAllocator *pma) { tfree(pma); } + +void vnodeRefMemAllocator(SMemAllocator *pma) { + SVnode * pVnode = (SVnode *)pma->impl; + SVMemAllocator *pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data); + + T_REF_INC(pvma); +} + +void vnodeUnrefMemAllocator(SMemAllocator *pma) { + SVnode * pVnode = (SVnode *)pma->impl; + SVMemAllocator *pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data); + + if (T_REF_DEC(pvma) == 0) { + /* TODO */ + } +} + +/* ------------------------ STATIC METHODS ------------------------ */ +static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) { + SListNode * pNode; + SVMemAllocator *pvma; + + pNode = (SListNode *)calloc(1, sizeof(*pNode) + sizeof(SVMemAllocator)); + if (pNode == NULL) { + return NULL; + } + + pvma = (SVMemAllocator *)(pNode->data); + pvma->capacity = capacity; + pvma->type = type; + + switch (type) { + case E_V_HEAP_ALLOCATOR: + vHeapAllocatorInit(&(pvma->vha)); + break; + case E_V_ARENA_ALLOCATOR: + vArenaAllocatorInit(&(pvma->vaa)); + break; + default: + ASSERT(0); + } + + return pNode; +} + +static void vBufPoolFreeNode(SListNode *pNode) { + if (pNode) { + free(pNode); + } +} + +// --------------- For arena allocator +static int vArenaAllocatorInit(SVArenaAllocator *pvaa) { + // TODO + return 0; +} + +static void vArenaAllocatorClear(SVArenaAllocator *pvaa) { + // TODO +} + +// --------------- For heap allocator +static int vHeapAllocatorInit(SVHeapAllocator *pvha) { + // TODO + return 0; +} + +static void vHeapAllocatorClear(SVHeapAllocator *pvha) { + // TODO +} \ No newline at end of file diff --git a/source/dnode/vnode/impl/src/vnodeMain.c b/source/dnode/vnode/impl/src/vnodeMain.c index 6c0fe9c974..a0c1d38ea9 100644 --- a/source/dnode/vnode/impl/src/vnodeMain.c +++ b/source/dnode/vnode/impl/src/vnodeMain.c @@ -87,12 +87,6 @@ static void vnodeFree(SVnode *pVnode) { static int vnodeOpenImpl(SVnode *pVnode) { char dir[TSDB_FILENAME_LEN]; - // Open allocator pool - if (vnodeOpenAllocatorPool(pVnode) < 0) { - // TODO: handle error - return -1; - } - // Open meta sprintf(dir, "%s/meta", pVnode->path); pVnode->pMeta = metaOpen(dir, &(pVnode->options.metaOptions)); @@ -117,7 +111,6 @@ static int vnodeOpenImpl(SVnode *pVnode) { static void vnodeCloseImpl(SVnode *pVnode) { if (pVnode) { - vnodeCloseAllocatorPool(pVnode); // TODO: Close TQ tsdbClose(pVnode->pTsdb); metaClose(pVnode->pMeta); diff --git a/source/dnode/vnode/impl/src/vnodeWrite.c b/source/dnode/vnode/impl/src/vnodeWrite.c index 3d200b28d5..810ac570bc 100644 --- a/source/dnode/vnode/impl/src/vnodeWrite.c +++ b/source/dnode/vnode/impl/src/vnodeWrite.c @@ -21,6 +21,7 @@ int vnodeProcessWMsgs(SVnode *pVnode, SArray *pMsgs) { } int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { +#if 0 int reqType; /* TODO */ size_t reqSize; /* TODO */ uint64_t reqVersion = 0; /* TODO */ @@ -45,7 +46,9 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { case TSDB_MSG_TYPE_DROP_TABLE: code = metaDropTable(pVnode->pMeta, 0 /* TODO */); break; + case TSDB_MSG_TYPE_SUBMIT: /* TODO */ + break; default: break; } @@ -57,6 +60,8 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { } return code; +#endif + return 0; } /* ------------------------ STATIC METHODS ------------------------ */ \ No newline at end of file diff --git a/source/util/src/tlist.c b/source/util/src/tlist.c index c5b4dbad10..6756af226f 100644 --- a/source/util/src/tlist.c +++ b/source/util/src/tlist.c @@ -13,16 +13,20 @@ * along with this program. If not, see . */ -#include "os.h" #include "tlist.h" +#include "os.h" + +void tdListInit(SList *list, int eleSize) { + list->eleSize = eleSize; + list->numOfEles = 0; + list->head = list->tail = NULL; +} SList *tdListNew(int eleSize) { SList *list = (SList *)malloc(sizeof(SList)); if (list == NULL) return NULL; - list->eleSize = eleSize; - list->numOfEles = 0; - list->head = list->tail = NULL; + tdListInit(list, eleSize); return list; }