more
This commit is contained in:
		
							parent
							
								
									a1a677f650
								
							
						
					
					
						commit
						9dd59b41e0
					
				| 
						 | 
					@ -28,6 +28,7 @@ extern "C" {
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct SVnodeOptions {
 | 
					struct SVnodeOptions {
 | 
				
			||||||
 | 
					  size_t       wsize;
 | 
				
			||||||
  STsdbOptions tsdbOptions;
 | 
					  STsdbOptions tsdbOptions;
 | 
				
			||||||
  SMetaOptions metaOptions;
 | 
					  SMetaOptions metaOptions;
 | 
				
			||||||
  // STqOptions   tqOptions; // TODO
 | 
					  // STqOptions   tqOptions; // TODO
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,35 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * 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 <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _TD_VNODE_ALLOCATOR_POOL_H_
 | 
				
			||||||
 | 
					#define _TD_VNODE_ALLOCATOR_POOL_H_
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "vnode.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					extern "C" {
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct {
 | 
				
			||||||
 | 
					} SVAllocatorPool;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int  vnodeOpenAllocatorPool(SVnode *pVnode);
 | 
				
			||||||
 | 
					void vnodeCloseAllocatorPool(SVnode *pVnode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif /*_TD_VNODE_ALLOCATOR_POOL_H_*/
 | 
				
			||||||
| 
						 | 
					@ -17,6 +17,7 @@
 | 
				
			||||||
#define _TD_VNODE_DEF_H_
 | 
					#define _TD_VNODE_DEF_H_
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "vnode.h"
 | 
					#include "vnode.h"
 | 
				
			||||||
 | 
					#include "vnodeAllocatorPool.h"
 | 
				
			||||||
#include "vnodeOptions.h"
 | 
					#include "vnodeOptions.h"
 | 
				
			||||||
#include "vnodeStateMgr.h"
 | 
					#include "vnodeStateMgr.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,6 +29,7 @@ struct SVnode {
 | 
				
			||||||
  char*           path;
 | 
					  char*           path;
 | 
				
			||||||
  SVnodeOptions   options;
 | 
					  SVnodeOptions   options;
 | 
				
			||||||
  SVState         state;
 | 
					  SVState         state;
 | 
				
			||||||
 | 
					  SVAllocatorPool pool;
 | 
				
			||||||
  SMeta*          pMeta;
 | 
					  SMeta*          pMeta;
 | 
				
			||||||
  STsdb*          pTsdb;
 | 
					  STsdb*          pTsdb;
 | 
				
			||||||
  STQ*            pTq;
 | 
					  STQ*            pTq;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,55 +0,0 @@
 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#ifndef _TD_VNODE_MEM_ALLOCATOR_H_
 | 
					 | 
				
			||||||
#define _TD_VNODE_MEM_ALLOCATOR_H_
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include "vnodeInt.h"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#ifdef __cplusplus
 | 
					 | 
				
			||||||
extern "C" {
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
typedef struct SVnodeMemAllocator SVnodeMemAllocator;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SVnodeMemAllocator *VMACreate(size_t size /* base size */, size_t ssize /* step size */,
 | 
					 | 
				
			||||||
                              size_t threshold /* threshold size when full*/);
 | 
					 | 
				
			||||||
void                VMADestroy(SVnodeMemAllocator *pvma);
 | 
					 | 
				
			||||||
void                VMAReset(SVnodeMemAllocator *pvma);
 | 
					 | 
				
			||||||
void *              VMAMalloc(SVnodeMemAllocator *pvma, size_t size);
 | 
					 | 
				
			||||||
void                VMAFree(SVnodeMemAllocator *pvma, void *ptr);
 | 
					 | 
				
			||||||
bool                VMAIsFull(SVnodeMemAllocator *pvma);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ------------------ FOR TEST ONLY ------------------
 | 
					 | 
				
			||||||
typedef struct SVMANode {
 | 
					 | 
				
			||||||
  struct SVMANode *prev;
 | 
					 | 
				
			||||||
  size_t           tsize;
 | 
					 | 
				
			||||||
  size_t           used;
 | 
					 | 
				
			||||||
  char             data[];
 | 
					 | 
				
			||||||
} SVMANode;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
struct SVnodeMemAllocator {
 | 
					 | 
				
			||||||
  bool      full;       // if allocator is full
 | 
					 | 
				
			||||||
  size_t    threshold;  // threshold;
 | 
					 | 
				
			||||||
  size_t    ssize;      // step size to allocate
 | 
					 | 
				
			||||||
  SVMANode *inuse;      // inuse node to allocate
 | 
					 | 
				
			||||||
  SVMANode  node;       // basic node to use
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#ifdef __cplusplus
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#endif /*_TD_VNODE_MEM_ALLOCATOR_H_*/
 | 
					 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,25 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * 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 <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "vnodeDef.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int vnodeOpenAllocatorPool(SVnode *pVnode) {
 | 
				
			||||||
 | 
					  // TODO
 | 
				
			||||||
 | 
					  return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void vnodeCloseAllocatorPool(SVnode *pVnode) {
 | 
				
			||||||
 | 
					  // TODO
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -87,6 +87,12 @@ static void vnodeFree(SVnode *pVnode) {
 | 
				
			||||||
static int vnodeOpenImpl(SVnode *pVnode) {
 | 
					static int vnodeOpenImpl(SVnode *pVnode) {
 | 
				
			||||||
  char dir[TSDB_FILENAME_LEN];
 | 
					  char dir[TSDB_FILENAME_LEN];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Open allocator pool
 | 
				
			||||||
 | 
					  if (vnodeOpenAllocatorPool(pVnode) < 0) {
 | 
				
			||||||
 | 
					    // TODO: handle error
 | 
				
			||||||
 | 
					    return -1;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Open meta
 | 
					  // Open meta
 | 
				
			||||||
  sprintf(dir, "%s/meta", pVnode->path);
 | 
					  sprintf(dir, "%s/meta", pVnode->path);
 | 
				
			||||||
  pVnode->pMeta = metaOpen(dir, &(pVnode->options.metaOptions));
 | 
					  pVnode->pMeta = metaOpen(dir, &(pVnode->options.metaOptions));
 | 
				
			||||||
| 
						 | 
					@ -111,6 +117,7 @@ static int vnodeOpenImpl(SVnode *pVnode) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void vnodeCloseImpl(SVnode *pVnode) {
 | 
					static void vnodeCloseImpl(SVnode *pVnode) {
 | 
				
			||||||
  if (pVnode) {
 | 
					  if (pVnode) {
 | 
				
			||||||
 | 
					    vnodeCloseAllocatorPool(pVnode);
 | 
				
			||||||
    // TODO: Close TQ
 | 
					    // TODO: Close TQ
 | 
				
			||||||
    tsdbClose(pVnode->pTsdb);
 | 
					    tsdbClose(pVnode->pTsdb);
 | 
				
			||||||
    metaClose(pVnode->pMeta);
 | 
					    metaClose(pVnode->pMeta);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,124 +0,0 @@
 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include "vnodeMemAllocator.h"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#define VMA_IS_FULL(pvma) \
 | 
					 | 
				
			||||||
  (((pvma)->inuse != &((pvma)->node)) || ((pvma)->inuse->tsize - (pvma)->inuse->used < (pvma)->threshold))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static SVMANode *VMANodeNew(size_t size);
 | 
					 | 
				
			||||||
static void      VMANodeFree(SVMANode *node);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SVnodeMemAllocator *VMACreate(size_t size, size_t ssize, size_t threshold) {
 | 
					 | 
				
			||||||
  SVnodeMemAllocator *pvma = NULL;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (size < threshold) {
 | 
					 | 
				
			||||||
    return NULL;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  pvma = (SVnodeMemAllocator *)malloc(sizeof(*pvma) + size);
 | 
					 | 
				
			||||||
  if (pvma) {
 | 
					 | 
				
			||||||
    pvma->full = false;
 | 
					 | 
				
			||||||
    pvma->threshold = threshold;
 | 
					 | 
				
			||||||
    pvma->ssize = ssize;
 | 
					 | 
				
			||||||
    pvma->inuse = &(pvma->node);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pvma->inuse->prev = NULL;
 | 
					 | 
				
			||||||
    pvma->inuse->tsize = size;
 | 
					 | 
				
			||||||
    pvma->inuse->used = 0;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return pvma;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void VMADestroy(SVnodeMemAllocator *pvma) {
 | 
					 | 
				
			||||||
  if (pvma) {
 | 
					 | 
				
			||||||
    VMAReset(pvma);
 | 
					 | 
				
			||||||
    free(pvma);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void VMAReset(SVnodeMemAllocator *pvma) {
 | 
					 | 
				
			||||||
  while (pvma->inuse != &(pvma->node)) {
 | 
					 | 
				
			||||||
    SVMANode *node = pvma->inuse;
 | 
					 | 
				
			||||||
    pvma->inuse = node->prev;
 | 
					 | 
				
			||||||
    VMANodeFree(node);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  pvma->inuse->used = 0;
 | 
					 | 
				
			||||||
  pvma->full = false;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void *VMAMalloc(SVnodeMemAllocator *pvma, size_t size) {
 | 
					 | 
				
			||||||
  void * ptr = NULL;
 | 
					 | 
				
			||||||
  size_t tsize = size + sizeof(size_t);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (pvma->inuse->tsize - pvma->inuse->used < tsize) {
 | 
					 | 
				
			||||||
    SVMANode *pNode = VMANodeNew(MAX(pvma->ssize, tsize));
 | 
					 | 
				
			||||||
    if (pNode == NULL) {
 | 
					 | 
				
			||||||
      return NULL;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pNode->prev = pvma->inuse;
 | 
					 | 
				
			||||||
    pvma->inuse = pNode;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  ptr = pvma->inuse->data + pvma->inuse->used;
 | 
					 | 
				
			||||||
  pvma->inuse->used += tsize;
 | 
					 | 
				
			||||||
  *(size_t *)ptr = size;
 | 
					 | 
				
			||||||
  ptr = POINTER_SHIFT(ptr, sizeof(size_t));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  pvma->full = VMA_IS_FULL(pvma);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return ptr;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void VMAFree(SVnodeMemAllocator *pvma, void *ptr) {
 | 
					 | 
				
			||||||
  if (ptr) {
 | 
					 | 
				
			||||||
    size_t size = *(size_t *)POINTER_SHIFT(ptr, -sizeof(size_t));
 | 
					 | 
				
			||||||
    if (POINTER_SHIFT(ptr, size) == pvma->inuse->data + pvma->inuse->used) {
 | 
					 | 
				
			||||||
      pvma->inuse->used -= (size + sizeof(size_t));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if ((pvma->inuse->used == 0) && (pvma->inuse != &(pvma->node))) {
 | 
					 | 
				
			||||||
        SVMANode *node = pvma->inuse;
 | 
					 | 
				
			||||||
        pvma->inuse = node->prev;
 | 
					 | 
				
			||||||
        VMANodeFree(node);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      pvma->full = VMA_IS_FULL(pvma);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
bool VMAIsFull(SVnodeMemAllocator *pvma) { return pvma->full; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static SVMANode *VMANodeNew(size_t size) {
 | 
					 | 
				
			||||||
  SVMANode *node = NULL;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  node = (SVMANode *)malloc(sizeof(*node) + size);
 | 
					 | 
				
			||||||
  if (node) {
 | 
					 | 
				
			||||||
    node->prev = NULL;
 | 
					 | 
				
			||||||
    node->tsize = size;
 | 
					 | 
				
			||||||
    node->used = 0;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return node;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static void VMANodeFree(SVMANode *node) {
 | 
					 | 
				
			||||||
  if (node) {
 | 
					 | 
				
			||||||
    free(node);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Loading…
	
		Reference in New Issue