vnodeBufferPool.c 8.0 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 23
  TD_DLIST(SVMemAllocator) free;
  TD_DLIST(SVMemAllocator) incycle;
H
more  
Hongze Cheng 已提交
24
  SVMemAllocator *inuse;
H
more  
Hongze Cheng 已提交
25
  // MAF for submodules
H
more  
Hongze Cheng 已提交
26
  // SMemAllocatorFactory maf;
H
more  
Hongze Cheng 已提交
27 28
};

H
more  
Hongze Cheng 已提交
29 30 31 32 33 34 35 36 37
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;
  }

H
more  
Hongze Cheng 已提交
38 39
  tDListInit(&(pVnode->pBufPool->free));
  tDListInit(&(pVnode->pBufPool->incycle));
H
more  
Hongze Cheng 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52

  pVnode->pBufPool->inuse = NULL;

  // TODO
  capacity = pVnode->config.wsize / VNODE_BUF_POOL_SHARDS;

  for (int i = 0; i < VNODE_BUF_POOL_SHARDS; i++) {
    SVMemAllocator *pVMA = vmaCreate(capacity, pVnode->config.ssize, pVnode->config.lsize);
    if (pVMA == NULL) {
      // TODO: handle error
      return -1;
    }

H
more  
Hongze Cheng 已提交
53
    tDListAppend(&(pVnode->pBufPool->free), pVMA);
H
more  
Hongze Cheng 已提交
54 55 56 57 58 59 60 61 62 63
  }

  return 0;
}

void vnodeCloseBufPool(SVnode *pVnode) {
  if (pVnode->pBufPool) {
    vmaDestroy(pVnode->pBufPool->inuse);

    while (true) {
H
more  
Hongze Cheng 已提交
64
      SVMemAllocator *pVMA = TD_DLIST_HEAD(&(pVnode->pBufPool->incycle));
H
more  
Hongze Cheng 已提交
65
      if (pVMA == NULL) break;
H
more  
Hongze Cheng 已提交
66
      tDListPop(&(pVnode->pBufPool->incycle), pVMA);
H
more  
Hongze Cheng 已提交
67 68 69 70
      vmaDestroy(pVMA);
    }

    while (true) {
H
more  
Hongze Cheng 已提交
71
      SVMemAllocator *pVMA = TD_DLIST_HEAD(&(pVnode->pBufPool->free));
H
more  
Hongze Cheng 已提交
72
      if (pVMA == NULL) break;
H
more  
Hongze Cheng 已提交
73
      tDListPop(&(pVnode->pBufPool->free), pVMA);
H
more  
Hongze Cheng 已提交
74 75 76 77 78 79 80 81
      vmaDestroy(pVMA);
    }

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

H
Hongze Cheng 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
int vnodeBufPoolSwitch(SVnode *pVnode) {
  SVMemAllocator *pvma = pVnode->pBufPool->inuse;

  pVnode->pBufPool->inuse = NULL;

  tDListAppend(&(pVnode->pBufPool->incycle), pvma);
  return 0;
}

int vnodeBufPoolRecycle(SVnode *pVnode) {
  SVBufPool *     pBufPool = pVnode->pBufPool;
  SVMemAllocator *pvma = TD_DLIST_HEAD(&(pBufPool->incycle));
  ASSERT(pvma != NULL);

  tDListPop(&(pBufPool->incycle), pvma);
  vmaReset(pvma);
  tDListAppend(&(pBufPool->free), pvma);

  return 0;
}

H
more  
Hongze Cheng 已提交
103
void *vnodeMalloc(SVnode *pVnode, uint64_t size) {
H
more  
Hongze Cheng 已提交
104 105 106 107 108
  SVBufPool *pBufPool = pVnode->pBufPool;

  if (pBufPool->inuse == NULL) {
    while (true) {
      // TODO: add sem_wait and sem_post
H
more  
Hongze Cheng 已提交
109
      pBufPool->inuse = TD_DLIST_HEAD(&(pBufPool->free));
H
more  
Hongze Cheng 已提交
110
      if (pBufPool->inuse) {
H
more  
Hongze Cheng 已提交
111
        tDListPop(&(pBufPool->free), pBufPool->inuse);
H
more  
Hongze Cheng 已提交
112 113 114 115 116 117
        break;
      }
    }
  }

  return vmaMalloc(pBufPool->inuse, size);
H
more  
Hongze Cheng 已提交
118 119 120
}

bool vnodeBufPoolIsFull(SVnode *pVnode) {
H
more  
Hongze Cheng 已提交
121 122
  if (pVnode->pBufPool->inuse == NULL) return false;
  return vmaIsFull(pVnode->pBufPool->inuse);
H
more  
Hongze Cheng 已提交
123 124 125 126
}

#if 0

H
more  
Hongze Cheng 已提交
127 128 129 130 131 132
typedef enum {
  // Heap allocator
  E_V_HEAP_ALLOCATOR = 0,
  // Arena allocator
  E_V_ARENA_ALLOCATOR
} EVMemAllocatorT;
H
more  
Hongze Cheng 已提交
133 134

typedef struct {
H
more  
Hongze Cheng 已提交
135
  /* TODO */
H
more  
Hongze Cheng 已提交
136 137 138 139 140 141 142 143 144 145
} SVHeapAllocator;

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

typedef struct {
H
more  
Hongze Cheng 已提交
146 147
  uint64_t     ssize;  // step size
  uint64_t     lsize;  // limit size
H
more  
Hongze Cheng 已提交
148 149 150 151
  SVArenaNode *inuse;
  SVArenaNode  node;
} SVArenaAllocator;

H
more  
Hongze Cheng 已提交
152 153 154 155 156
typedef struct {
  SVnode *   pVnode;
  SListNode *pNode;
} SVMAWrapper;

H
more  
Hongze Cheng 已提交
157

H
more  
Hongze Cheng 已提交
158 159 160 161
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 已提交
162
static void *         vBufPoolMalloc(SVMemAllocator *pvma, uint64_t size);
H
more  
Hongze Cheng 已提交
163

H
more  
Hongze Cheng 已提交
164
/* ------------------------ STATIC METHODS ------------------------ */
H
more  
Hongze Cheng 已提交
165 166 167
static SListNode *vBufPoolNewNode(uint64_t capacity, EVMemAllocatorT type) {
  SListNode *     pNode;
  SVMemAllocator *pvma;
H
more  
Hongze Cheng 已提交
168
  uint64_t        msize;
H
Hongze Cheng 已提交
169 170
  uint64_t        ssize = 4096;  // TODO
  uint64_t        lsize = 1024;  // TODO
H
more  
Hongze Cheng 已提交
171

H
more  
Hongze Cheng 已提交
172 173 174 175 176 177
  msize = sizeof(SListNode) + sizeof(SVMemAllocator);
  if (type == E_V_ARENA_ALLOCATOR) {
    msize += capacity;
  }

  pNode = (SListNode *)calloc(1, msize);
H
more  
Hongze Cheng 已提交
178
  if (pNode == NULL) {
H
more  
Hongze Cheng 已提交
179
    // TODO: handle error
H
more  
Hongze Cheng 已提交
180 181 182 183 184 185 186 187 188
    return NULL;
  }

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

  switch (type) {
    case E_V_ARENA_ALLOCATOR:
H
more  
Hongze Cheng 已提交
189 190 191 192
      vArenaAllocatorInit(&(pvma->vaa), capacity, ssize, lsize);
      break;
    case E_V_HEAP_ALLOCATOR:
      // vHeapAllocatorInit(&(pvma->vha));
H
more  
Hongze Cheng 已提交
193 194 195 196 197 198 199 200 201
      break;
    default:
      ASSERT(0);
  }

  return pNode;
}

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

H
more  
Hongze Cheng 已提交
204 205 206 207 208 209 210 211 212 213
  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 已提交
214

H
more  
Hongze Cheng 已提交
215
  free(pNode);
H
more  
Hongze Cheng 已提交
216 217
}

H
more  
Hongze Cheng 已提交
218 219
static void *vBufPoolMalloc(SVMemAllocator *pvma, uint64_t size) {
  void *ptr = NULL;
H
more  
Hongze Cheng 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

  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 */
  }

H
more  
Hongze Cheng 已提交
244
  return ptr;
H
more  
Hongze Cheng 已提交
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 280 281
}

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;
H
more  
Hongze Cheng 已提交
282
  pma->malloc = NULL;
H
more  
Hongze Cheng 已提交
283 284 285 286 287 288 289 290 291
  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 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  SVnode *        pVnode = (SVnode *)(pmaf->impl);
  SListNode *     pNode = ((SVMAWrapper *)(pma->impl))->pNode;
  SVMemAllocator *pvma = (SVMemAllocator *)(pNode->data);

  if (T_REF_DEC(pvma) == 0) {
    if (pvma->type == E_V_ARENA_ALLOCATOR) {
      SVArenaAllocator *pvaa = &(pvma->vaa);
      while (pvaa->inuse != &(pvaa->node)) {
        SVArenaNode *pNode = pvaa->inuse;
        pvaa->inuse = pNode->prev;
        /* code */
      }

      pvaa->inuse->ptr = pvaa->inuse->data;
    } else if (pvma->type == E_V_HEAP_ALLOCATOR) {
    } else {
      ASSERT(0);
    }

    // Move node from incycle to free
    tdListAppendNode(&(pVnode->pBufPool->free), tdListPopNode(&(pVnode->pBufPool->incycle), pNode));
    // tsem_post(); todo: sem_post
  }
H
more  
Hongze Cheng 已提交
315 316
}
#endif