tdb_mpool.c 8.4 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "tdb_mpool.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19 20 21
static int         tdbGnrtFileID(const char *fname, uint8_t *fileid);
static void        tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf);
static void        tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf);
static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid);
H
Hongze Cheng 已提交
22
static int         tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p);
H
more  
Hongze Cheng 已提交
23
static int         tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p);
H
Hongze Cheng 已提交
24
static void        tdbMPoolClockEvictPage(TDB_MPOOL *mp, pg_t **pagepp);
H
Hongze Cheng 已提交
25

H
Hongze Cheng 已提交
26
int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) {
H
Hongze Cheng 已提交
27
  TDB_MPOOL *mp = NULL;
H
Hongze Cheng 已提交
28
  size_t     tsize;
H
refact  
Hongze Cheng 已提交
29
  pg_t *     pagep;
H
Hongze Cheng 已提交
30 31 32 33 34 35 36 37 38 39 40

  // check parameters
  if (!TDB_IS_PGSIZE_VLD(pgsize)) {
    tdbError("invalid page size");
    return -1;
  }

  // allocate handle
  mp = (TDB_MPOOL *)calloc(1, sizeof(*mp));
  if (mp == NULL) {
    tdbError("failed to malloc memory pool handle");
H
Hongze Cheng 已提交
41
    goto _err;
H
Hongze Cheng 已提交
42 43 44 45 46 47
  }

  // initialize the handle
  mp->cachesize = cachesize;
  mp->pgsize = pgsize;
  mp->npages = cachesize / pgsize;
H
Hongze Cheng 已提交
48
  mp->clockHand = 0;
H
Hongze Cheng 已提交
49 50 51

  TD_DLIST_INIT(&mp->freeList);

H
refact  
Hongze Cheng 已提交
52
  mp->pages = (pg_t *)calloc(mp->npages, sizeof(pg_t));
H
Hongze Cheng 已提交
53 54
  if (mp->pages == NULL) {
    tdbError("failed to malloc memory pool pages");
H
Hongze Cheng 已提交
55
    goto _err;
H
Hongze Cheng 已提交
56 57
  }

H
Hongze Cheng 已提交
58
  for (frame_id_t i = 0; i < mp->npages; i++) {
H
refact  
Hongze Cheng 已提交
59 60
    mp->pages[i].p = malloc(pgsize);
    if (mp->pages[i].p == NULL) {
H
Hongze Cheng 已提交
61 62
      goto _err;
    }
H
Hongze Cheng 已提交
63

H
refact  
Hongze Cheng 已提交
64 65 66
    taosInitRWLatch(&mp->pages[i].rwLatch);
    mp->pages[i].frameid = i;
    mp->pages[i].pgid = TDB_IVLD_PGID;
H
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68
    // add new page to the free list
H
refact  
Hongze Cheng 已提交
69
    TD_DLIST_APPEND_WITH_FIELD(&(mp->freeList), &(mp->pages[i]), free);
H
Hongze Cheng 已提交
70 71
  }

H
Hongze Cheng 已提交
72 73 74 75 76 77
#define PGTAB_FACTOR 1.0
  mp->pgtab.nbucket = mp->npages / PGTAB_FACTOR;
  mp->pgtab.hashtab = (pg_list_t *)calloc(mp->pgtab.nbucket, sizeof(pg_list_t));
  if (mp->pgtab.hashtab == NULL) {
    tdbError("failed to malloc memory pool hash table");
    goto _err;
H
Hongze Cheng 已提交
78 79 80 81 82
  }

  // return
  *mpp = mp;
  return 0;
H
Hongze Cheng 已提交
83 84 85 86 87

_err:
  tdbMPoolClose(mp);
  *mpp = NULL;
  return -1;
H
Hongze Cheng 已提交
88 89
}

H
Hongze Cheng 已提交
90
int tdbMPoolClose(TDB_MPOOL *mp) {
H
Hongze Cheng 已提交
91 92 93 94
  if (mp) {
    tfree(mp->pgtab.hashtab);
    if (mp->pages) {
      for (int i = 0; i < mp->npages; i++) {
H
refact  
Hongze Cheng 已提交
95
        tfree(mp->pages[i].p);
H
Hongze Cheng 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      }

      free(mp->pages);
    }

    free(mp);
  }
  return 0;
}

int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) {
  TDB_MPFILE *mpf;

  if ((mpf = (TDB_MPFILE *)calloc(1, sizeof(*mpf))) == NULL) {
    return -1;
  }

  mpf->fd = -1;

  if ((mpf->fname = strdup(fname)) == NULL) {
    goto _err;
  }

  if ((mpf->fd = open(fname, O_CREAT | O_RDWR, 0755)) < 0) {
    goto _err;
  }

H
more  
Hongze Cheng 已提交
123 124 125 126
  if (tdbGnrtFileID(fname, mpf->fileid) < 0) {
    goto _err;
  }

H
Hongze Cheng 已提交
127
  // Register current MPF to MP
H
Hongze Cheng 已提交
128
  tdbMPoolRegFile(mp, mpf);
H
Hongze Cheng 已提交
129

H
Hongze Cheng 已提交
130 131 132 133 134 135 136 137 138 139
  *mpfp = mpf;
  return 0;

_err:
  tdbMPoolFileClose(mpf);
  *mpfp = NULL;
  return -1;
}

int tdbMPoolFileClose(TDB_MPFILE *mpf) {
H
more  
Hongze Cheng 已提交
140 141 142 143 144 145 146
  if (mpf) {
    if (mpf->fd > 0) {
      close(mpf->fd);
    }
    tfree(mpf->fname);
    free(mpf);
  }
H
Hongze Cheng 已提交
147 148 149
  return 0;
}

H
Hongze Cheng 已提交
150 151 152 153 154 155
#define MPF_GET_PAGE_BUCKETID(fileid, pgno, nbuckets) \
  ({                                                  \
    uint64_t *tmp = (uint64_t *)fileid;               \
    (tmp[0] + tmp[1] + tmp[2] + (pgno)) % (nbuckets); \
  })

H
Hongze Cheng 已提交
156 157 158 159 160 161 162 163 164 165 166
int tdbMPoolFileNewPage(TDB_MPFILE *mpf, pgno_t *pgno, void *addr) {
  // TODO
  return 0;
}

int tdbMPoolFileFreePage(TDB_MPOOL *mpf, pgno_t *pgno, void *addr) {
  // TODO
  return 0;
}

int tdbMPoolFileGetPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr) {
H
Hongze Cheng 已提交
167 168
  pg_t *     pagep;
  TDB_MPOOL *mp;
H
Hongze Cheng 已提交
169
  pg_list_t *pglist;
H
Hongze Cheng 已提交
170 171 172

  mp = mpf->mp;

H
Hongze Cheng 已提交
173
  // check if the page already in pool
H
Hongze Cheng 已提交
174 175 176 177 178 179 180 181 182 183
  pglist = mp->pgtab.hashtab + MPF_GET_PAGE_BUCKETID(mpf->fileid, pgno, mp->pgtab.nbucket);
  pagep = TD_DLIST_HEAD(pglist);
  while (pagep) {
    if (memcmp(mpf->fileid, pagep->pgid.fileid, TDB_FILE_ID_LEN) == 0 && pgno == pagep->pgid.pgno) {
      break;
    }

    pagep = TD_DLIST_NODE_NEXT_WITH_FIELD(pagep, hash);
  }

H
Hongze Cheng 已提交
184
  if (pagep) {
H
Hongze Cheng 已提交
185
    // page is found
H
Hongze Cheng 已提交
186
    // todo: pin the page and return
H
refact  
Hongze Cheng 已提交
187
    *(void **)addr = pagep->p;
H
Hongze Cheng 已提交
188
    return 0;
H
Hongze Cheng 已提交
189 190 191 192 193 194 195
  }

  // page not found
  pagep = TD_DLIST_HEAD(&mp->freeList);
  if (pagep) {
    // has free page
    TD_DLIST_POP_WITH_FIELD(&(mp->freeList), pagep, free);
H
Hongze Cheng 已提交
196
  } else {
H
Hongze Cheng 已提交
197
    // no free page available
H
Hongze Cheng 已提交
198
    tdbMPoolClockEvictPage(mp, &pagep);
H
Hongze Cheng 已提交
199
    if (pagep) {
H
Hongze Cheng 已提交
200 201 202
      if (pagep->dirty) {
        // TODO: Handle dirty page eviction
      }
H
Hongze Cheng 已提交
203 204 205
    }
  }

H
Hongze Cheng 已提交
206 207 208 209 210 211 212
  if (pagep == NULL) {
    // no available container page
    return -1;
  }

  // load page from the disk if a container page is available
  // TODO: load the page from the disk
H
refact  
Hongze Cheng 已提交
213
  if (tdbMPoolFileReadPage(mpf, pgno, pagep->p) < 0) {
H
Hongze Cheng 已提交
214 215 216 217 218 219 220
    return -1;
  }

  memcpy(pagep->pgid.fileid, mpf->fileid, TDB_FILE_ID_LEN);
  pagep->pgid.pgno = pgno;
  pagep->dirty = 0;
  pagep->pinRef = 1;
H
Hongze Cheng 已提交
221 222 223 224

  // add current page to page table
  TD_DLIST_APPEND_WITH_FIELD(pglist, pagep, hash);

H
Hongze Cheng 已提交
225 226 227
  return 0;
}

H
Hongze Cheng 已提交
228
int tdbMPoolFilePutPage(TDB_MPOOL *mpf, pgno_t pgno, void *addr) {
H
Hongze Cheng 已提交
229 230 231 232
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
233
static int tdbGnrtFileID(const char *fname, uint8_t *fileid) {
H
more  
Hongze Cheng 已提交
234 235 236 237 238 239 240 241 242 243 244 245
  struct stat statbuf;

  if (stat(fname, &statbuf) < 0) {
    return -1;
  }

  memset(fileid, 0, TDB_FILE_ID_LEN);

  ((uint64_t *)fileid)[0] = (uint64_t)statbuf.st_ino;
  ((uint64_t *)fileid)[1] = (uint64_t)statbuf.st_dev;
  ((uint64_t *)fileid)[2] = rand();

H
Hongze Cheng 已提交
246
  return 0;
H
Hongze Cheng 已提交
247 248
}

H
Hongze Cheng 已提交
249 250
#define MPF_GET_BUCKETID(fileid)                   \
  ({                                               \
H
refact  
Hongze Cheng 已提交
251
    uint64_t *tmp = (uint64_t *)fileid;            \
H
Hongze Cheng 已提交
252 253 254 255 256 257
    (tmp[0] + tmp[1] + tmp[2]) % MPF_HASH_BUCKETS; \
  })

static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) {
  mpf_bucket_t *bktp;

H
refact  
Hongze Cheng 已提交
258
  bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid);
H
Hongze Cheng 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272

  taosWLockLatch(&(bktp->latch));

  TD_DLIST_APPEND_WITH_FIELD(bktp, mpf, node);

  taosWUnLockLatch(&(bktp->latch));

  mpf->mp = mp;
}

static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid) {
  TDB_MPFILE *  mpf = NULL;
  mpf_bucket_t *bktp;

H
refact  
Hongze Cheng 已提交
273
  bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(fileid);
H
Hongze Cheng 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

  taosRLockLatch(&(bktp->latch));

  mpf = TD_DLIST_HEAD(bktp);
  while (mpf) {
    if (memcmp(fileid, mpf->fileid, TDB_FILE_ID_LEN) == 0) {
      break;
    }

    mpf = TD_DLIST_NODE_NEXT_WITH_FIELD(mpf, node);
  }

  taosRUnLockLatch(&(bktp->latch));

  return mpf;
}

static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) {
  mpf_bucket_t *bktp;
  TDB_MPFILE *  tmpf;

  if (mpf->mp == NULL) return;

  ASSERT(mpf->mp == mp);

  bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid);

  taosWLockLatch(&(bktp->latch));

  tmpf = TD_DLIST_HEAD(bktp);

  while (tmpf) {
    if (memcmp(mpf->fileid, tmpf->fileid, TDB_FILE_ID_LEN) == 0) {
      TD_DLIST_POP_WITH_FIELD(bktp, tmpf, node);
      break;
    }

    tmpf = TD_DLIST_NODE_NEXT_WITH_FIELD(tmpf, node);
  }

  taosWUnLockLatch(&(bktp->latch));

  ASSERT(tmpf == mpf);
H
Hongze Cheng 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
}

static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p) {
  pgsize_t   pgsize;
  TDB_MPOOL *mp;
  off_t      offset;
  size_t     rsize;

  mp = mpf->mp;
  pgsize = mp->pgsize;
  offset = pgno * pgsize;

  // TODO: use loop to read all data
  rsize = pread(mpf->fd, p, pgsize, offset);
  // TODO: error handle

H
more  
Hongze Cheng 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  return 0;
}

static int tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p) {
  pgsize_t   pgsize;
  TDB_MPOOL *mp;
  off_t      offset;

  mp = mpf->mp;
  pgsize = mp->pgsize;
  offset = pgno * pgsize;

  lseek(mpf->fd, offset, SEEK_SET);
  // TODO: handle error

  write(mpf->fd, p, pgsize);
  // TODO: handle error

H
Hongze Cheng 已提交
351
  return 0;
H
Hongze Cheng 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
}

static void tdbMPoolClockEvictPage(TDB_MPOOL *mp, pg_t **pagepp) {
  pg_t *     pagep;
  frame_id_t och;

  *pagepp = NULL;
  och = mp->clockHand;

  do {
    pagep = mp->pages + mp->clockHand;
    mp->clockHand = (mp->clockHand + 1) % mp->npages;

    if (pagep->pinRef == 0) {
      if (pagep->rbit == 1) {
        pagep->rbit = 0;
      } else {
        break;
      }
    }

    if (mp->clockHand == och) {
      return;
    }
  } while (1);

  *pagepp = pagep;
H
Hongze Cheng 已提交
379
}