vnodeBufferPool.c 7.1 KB
Newer Older
H
more  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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"

/* ------------------------ STRUCTURES ------------------------ */
H
more  
Hongze Cheng 已提交
19 20
#define VNODE_BUF_POOL_SHARDS 3

H
more  
Hongze Cheng 已提交
21
struct SVBufPool {
H
more  
Hongze Cheng 已提交
22
  // buffer pool impl
H
more  
Hongze Cheng 已提交
23 24 25
  SList      free;
  SList      incycle;
  SListNode *inuse;
H
more  
Hongze Cheng 已提交
26 27
  // MAF for submodules
  SMemAllocatorFactory maf;
H
more  
Hongze Cheng 已提交
28 29
};

H
more  
Hongze Cheng 已提交
30 31 32 33 34 35
typedef enum {
  // Heap allocator
  E_V_HEAP_ALLOCATOR = 0,
  // Arena allocator
  E_V_ARENA_ALLOCATOR
} EVMemAllocatorT;
H
more  
Hongze Cheng 已提交
36 37

typedef struct {
H
more  
Hongze Cheng 已提交
38
  /* TODO */
H
more  
Hongze Cheng 已提交
39 40 41 42 43 44 45 46 47 48
} SVHeapAllocator;

typedef struct SVArenaNode {
  struct SVArenaNode *prev;
  uint64_t            size;
  void *              ptr;
  char                data[];
} SVArenaNode;

typedef struct {
H
more  
Hongze Cheng 已提交
49 50
  uint64_t     ssize;  // step size
  uint64_t     lsize;  // limit size
H
more  
Hongze Cheng 已提交
51 52 53 54
  SVArenaNode *inuse;
  SVArenaNode  node;
} SVArenaAllocator;

H
more  
Hongze Cheng 已提交
55 56 57 58 59
typedef struct {
  SVnode *   pVnode;
  SListNode *pNode;
} SVMAWrapper;

H
more  
Hongze Cheng 已提交
60
typedef struct {
H
more  
Hongze Cheng 已提交
61
  T_REF_DECLARE()
H
more  
Hongze Cheng 已提交
62 63 64 65 66 67 68 69
  uint64_t        capacity;
  EVMemAllocatorT type;
  union {
    SVHeapAllocator  vha;
    SVArenaAllocator vaa;
  };
} SVMemAllocator;

H
more  
Hongze Cheng 已提交
70 71 72 73
static SListNode *    vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type);
static void           vBufPoolFreeNode(SListNode *pNode);
static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pmaf);
static void           vBufPoolDestroyMA(SMemAllocatorFactory *pmaf, SMemAllocator *pma);
H
more  
Hongze Cheng 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86

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);

H
more  
Hongze Cheng 已提交
87
  capacity = pVnode->options.wsize / VNODE_BUF_POOL_SHARDS;
H
more  
Hongze Cheng 已提交
88 89 90 91
  if (pVnode->options.isHeapAllocator) {
    type = E_V_HEAP_ALLOCATOR;
  }

H
more  
Hongze Cheng 已提交
92
  for (int i = 0; i < VNODE_BUF_POOL_SHARDS; i++) {
H
more  
Hongze Cheng 已提交
93 94 95 96 97 98 99 100 101
    SListNode *pNode = vBufPoolNewNode(capacity, type);
    if (pNode == NULL) {
      vnodeCloseBufPool(pVnode);
      return -1;
    }

    tdListAppendNode(&(pVnode->pBufPool->free), pNode);
  }

H
more  
Hongze Cheng 已提交
102 103 104 105
  pVnode->pBufPool->maf.impl = pVnode;
  pVnode->pBufPool->maf.create = vBufPoolCreateMA;
  pVnode->pBufPool->maf.destroy = vBufPoolDestroyMA;

H
more  
Hongze Cheng 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  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
H
more  
Hongze Cheng 已提交
123 124 125
    if (pVnode->pBufPool->inuse) {
      vBufPoolFreeNode(pVnode->pBufPool->inuse);
    }
H
more  
Hongze Cheng 已提交
126 127 128 129 130 131

    free(pVnode->pBufPool);
    pVnode->pBufPool = NULL;
  }
}

H
more  
Hongze Cheng 已提交
132 133 134 135 136 137 138 139 140
/* ------------------------ STATIC METHODS ------------------------ */
static void vArenaAllocatorInit(SVArenaAllocator *pvaa, uint64_t capacity, uint64_t ssize, uint64_t lsize) { /* TODO */
  pvaa->ssize = ssize;
  pvaa->lsize = lsize;
  pvaa->inuse = &pvaa->node;

  pvaa->node.prev = NULL;
  pvaa->node.size = capacity;
  pvaa->node.ptr = pvaa->node.data;
H
more  
Hongze Cheng 已提交
141 142
}

H
more  
Hongze Cheng 已提交
143 144 145 146 147
static void vArenaAllocatorClear(SVArenaAllocator *pvaa) { /* TODO */
  while (pvaa->inuse != &(pvaa->node)) {
    SVArenaNode *pANode = pvaa->inuse;
    pvaa->inuse = pANode->prev;
    free(pANode);
H
more  
Hongze Cheng 已提交
148 149 150 151 152 153
  }
}

static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) {
  SListNode *     pNode;
  SVMemAllocator *pvma;
H
more  
Hongze Cheng 已提交
154 155 156
  uint64_t        msize;
  uint64_t        ssize = 0;  // TODO
  uint64_t        lsize = 0;  // TODO
H
more  
Hongze Cheng 已提交
157

H
more  
Hongze Cheng 已提交
158 159 160 161 162 163
  msize = sizeof(SListNode) + sizeof(SVMemAllocator);
  if (type == E_V_ARENA_ALLOCATOR) {
    msize += capacity;
  }

  pNode = (SListNode *)calloc(1, msize);
H
more  
Hongze Cheng 已提交
164
  if (pNode == NULL) {
H
more  
Hongze Cheng 已提交
165
    // TODO: handle error
H
more  
Hongze Cheng 已提交
166 167 168 169 170 171 172 173 174
    return NULL;
  }

  pvma = (SVMemAllocator *)(pNode->data);
  pvma->capacity = capacity;
  pvma->type = type;

  switch (type) {
    case E_V_ARENA_ALLOCATOR:
H
more  
Hongze Cheng 已提交
175 176 177 178
      vArenaAllocatorInit(&(pvma->vaa), capacity, ssize, lsize);
      break;
    case E_V_HEAP_ALLOCATOR:
      // vHeapAllocatorInit(&(pvma->vha));
H
more  
Hongze Cheng 已提交
179 180 181 182 183 184 185 186 187
      break;
    default:
      ASSERT(0);
  }

  return pNode;
}

static void vBufPoolFreeNode(SListNode *pNode) {
H
more  
Hongze Cheng 已提交
188
  SVMemAllocator *pvma = (SVMemAllocator *)(pNode->data);
H
more  
Hongze Cheng 已提交
189

H
more  
Hongze Cheng 已提交
190 191 192 193 194 195 196 197 198 199
  switch (pvma->type) {
    case E_V_ARENA_ALLOCATOR:
      vArenaAllocatorClear(&(pvma->vaa));
      break;
    case E_V_HEAP_ALLOCATOR:
      // vHeapAllocatorClear(&(pvma->vha));
      break;
    default:
      break;
  }
H
more  
Hongze Cheng 已提交
200

H
more  
Hongze Cheng 已提交
201
  free(pNode);
H
more  
Hongze Cheng 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

static void *vBufPoolMalloc(SMemAllocator *pma, uint64_t size) {
  SVMAWrapper *   pvmaw = (SVMAWrapper *)(pma->impl);
  SVMemAllocator *pvma = (SVMemAllocator *)(pvmaw->pNode->data);
  void *          ptr = NULL;

  if (pvma->type == E_V_ARENA_ALLOCATOR) {
    SVArenaAllocator *pvaa = &(pvma->vaa);

    if (POINTER_DISTANCE(pvaa->inuse->ptr, pvaa->inuse->data) + size > pvaa->inuse->size) {
      SVArenaNode *pNode = (SVArenaNode *)malloc(sizeof(*pNode) + MAX(size, pvaa->ssize));
      if (pNode == NULL) {
        // TODO: handle error
        return NULL;
      }

      pNode->prev = pvaa->inuse;
      pNode->size = MAX(size, pvaa->ssize);
      pNode->ptr = pNode->data;

      pvaa->inuse = pNode;
    }

    ptr = pvaa->inuse->ptr;
    pvaa->inuse->ptr = POINTER_SHIFT(ptr, size);
  } else if (pvma->type == E_V_HEAP_ALLOCATOR) {
    /* TODO */
  }

  return NULL;
}

static SMemAllocator *vBufPoolCreateMA(SMemAllocatorFactory *pmaf) {
  SVnode *        pVnode;
  SMemAllocator * pma;
  SVMemAllocator *pvma;
  SVMAWrapper *   pvmaw;

  pVnode = (SVnode *)(pmaf->impl);
  pma = (SMemAllocator *)calloc(1, sizeof(*pma) + sizeof(SVMAWrapper));
  if (pma == NULL) {
    // TODO: handle error
    return NULL;
  }
  pvmaw = (SVMAWrapper *)POINTER_SHIFT(pma, sizeof(*pma));

  // No allocator used currently
  if (pVnode->pBufPool->inuse == NULL) {
    while (listNEles(&(pVnode->pBufPool->free)) == 0) {
      // TODO: wait until all released ro kill query
      // tsem_wait();
      ASSERT(0);
    }

    pVnode->pBufPool->inuse = tdListPopHead(&(pVnode->pBufPool->free));
    pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data);
    T_REF_INIT_VAL(pvma, 1);
  } else {
    pvma = (SVMemAllocator *)(pVnode->pBufPool->inuse->data);
  }

  T_REF_INC(pvma);

  pvmaw->pVnode = pVnode;
  pvmaw->pNode = pVnode->pBufPool->inuse;

  pma->impl = pvmaw;
  pma->malloc = vBufPoolMalloc;
  pma->calloc = NULL;  /* TODO */
  pma->realloc = NULL; /* TODO */
  pma->free = NULL;    /* TODO */
  pma->usage = NULL;   /* TODO */

  return pma;
}

static void vBufPoolDestroyMA(SMemAllocatorFactory *pmaf, SMemAllocator *pma) { /* TODO */
H
more  
Hongze Cheng 已提交
280
}