tdbPCache.c 7.2 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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 已提交
15 16
#include "tdbInt.h"

H
more  
Hongze Cheng 已提交
17
struct SPCache {
H
Hongze Cheng 已提交
18 19 20
  int         pageSize;
  int         cacheSize;
  tdb_mutex_t mutex;
H
Hongze Cheng 已提交
21
  SPage      *pList;
H
Hongze Cheng 已提交
22 23 24 25 26 27 28
  int         nFree;
  SPage      *pFree;
  int         nPage;
  int         nHash;
  SPage     **pgHash;
  int         nRecyclable;
  SPage       lru;
H
Hongze Cheng 已提交
29 30
};

H
more  
Hongze Cheng 已提交
31 32 33 34 35 36
#define PCACHE_PAGE_HASH(pPgid)                              \
  ({                                                         \
    u32 *t = (u32 *)((pPgid)->fileid);                       \
    t[0] + t[1] + t[2] + t[3] + t[4] + t[5] + (pPgid)->pgno; \
  })
#define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL)
H
Hongze Cheng 已提交
37

H
refact  
Hongze Cheng 已提交
38
static int    tdbPCacheOpenImpl(SPCache *pCache);
H
Hongze Cheng 已提交
39
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn);
H
Hongze Cheng 已提交
40 41 42 43
static void   tdbPCachePinPage(SPCache *pCache, SPage *pPage);
static void   tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage);
static void   tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage);
static void   tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage);
H
Hongze Cheng 已提交
44 45 46 47 48 49
static int    tdbPCacheCloseImpl(SPCache *pCache);

static void tdbPCacheInitLock(SPCache *pCache) { tdbMutexInit(&(pCache->mutex), NULL); }
static void tdbPCacheDestroyLock(SPCache *pCache) { tdbMutexDestroy(&(pCache->mutex)); }
static void tdbPCacheLock(SPCache *pCache) { tdbMutexLock(&(pCache->mutex)); }
static void tdbPCacheUnlock(SPCache *pCache) { tdbMutexUnlock(&(pCache->mutex)); }
H
Hongze Cheng 已提交
50

H
Hongze Cheng 已提交
51
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
H
more  
Hongze Cheng 已提交
52
  SPCache *pCache;
H
Hongze Cheng 已提交
53 54
  void    *pPtr;
  SPage   *pPgHdr;
H
Hongze Cheng 已提交
55

H
Hongze Cheng 已提交
56
  pCache = (SPCache *)tdbOsCalloc(1, sizeof(*pCache));
H
more  
Hongze Cheng 已提交
57
  if (pCache == NULL) {
H
Hongze Cheng 已提交
58 59 60
    return -1;
  }

H
more  
Hongze Cheng 已提交
61 62
  pCache->pageSize = pageSize;
  pCache->cacheSize = cacheSize;
H
Hongze Cheng 已提交
63

H
more  
Hongze Cheng 已提交
64
  if (tdbPCacheOpenImpl(pCache) < 0) {
H
Hongze Cheng 已提交
65
    tdbOsFree(pCache);
H
more  
Hongze Cheng 已提交
66
    return -1;
H
Hongze Cheng 已提交
67 68
  }

H
more  
Hongze Cheng 已提交
69
  *ppCache = pCache;
H
Hongze Cheng 已提交
70 71 72
  return 0;
}

H
Hongze Cheng 已提交
73
int tdbPCacheClose(SPCache *pCache) {
H
Hongze Cheng 已提交
74 75 76 77
  if (pCache) {
    tdbPCacheCloseImpl(pCache);
    tdbOsFree(pCache);
  }
H
Hongze Cheng 已提交
78
  return 0;
H
Hongze Cheng 已提交
79 80
}

H
Hongze Cheng 已提交
81
SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) {
H
refact  
Hongze Cheng 已提交
82
  SPage *pPage;
H
more  
Hongze Cheng 已提交
83

H
Hongze Cheng 已提交
84
  tdbPCacheLock(pCache);
H
Hongze Cheng 已提交
85

H
Hongze Cheng 已提交
86
  pPage = tdbPCacheFetchImpl(pCache, pPgid, pTxn);
H
Hongze Cheng 已提交
87 88 89 90
  if (pPage) {
    TDB_REF_PAGE(pPage);
  }

H
Hongze Cheng 已提交
91
  tdbPCacheUnlock(pCache);
H
more  
Hongze Cheng 已提交
92 93

  return pPage;
H
Hongze Cheng 已提交
94 95
}

H
Hongze Cheng 已提交
96
void tdbPCacheRelease(SPCache *pCache, SPage *pPage) {
H
Hongze Cheng 已提交
97 98 99 100 101 102
  i32 nRef;

  nRef = TDB_UNREF_PAGE(pPage);
  ASSERT(nRef >= 0);

  if (nRef == 0) {
H
Hongze Cheng 已提交
103
    tdbPCacheUnpinPage(pCache, pPage);
H
more  
Hongze Cheng 已提交
104
  }
H
Hongze Cheng 已提交
105 106
}

H
Hongze Cheng 已提交
107
int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; }
H
more  
Hongze Cheng 已提交
108

H
Hongze Cheng 已提交
109 110
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) {
  int    ret;
H
refact  
Hongze Cheng 已提交
111
  SPage *pPage;
H
more  
Hongze Cheng 已提交
112 113 114 115

  // 1. Search the hash table
  pPage = pCache->pgHash[PCACHE_PAGE_HASH(pPgid) % pCache->nHash];
  while (pPage) {
H
more  
Hongze Cheng 已提交
116
    if (TDB_IS_SAME_PAGE(&(pPage->pgid), pPgid)) break;
H
more  
Hongze Cheng 已提交
117 118 119
    pPage = pPage->pHashNext;
  }

H
refact  
Hongze Cheng 已提交
120
  if (pPage) {
H
Hongze Cheng 已提交
121 122
    // TODO: the page need to be copied and
    // replaced the page in hash table
H
refact  
Hongze Cheng 已提交
123
    tdbPCachePinPage(pCache, pPage);
H
Hongze Cheng 已提交
124
    return pPage;
H
more  
Hongze Cheng 已提交
125 126
  }

H
more  
Hongze Cheng 已提交
127 128 129 130 131 132 133 134 135 136 137
  // 2. Try to allocate a new page from the free list
  if (pCache->pFree) {
    pPage = pCache->pFree;
    pCache->pFree = pPage->pFreeNext;
    pCache->nFree--;
    pPage->pLruNext = NULL;
  }

  // 3. Try to Recycle a page
  if (!pPage && !pCache->lru.pLruPrev->isAnchor) {
    pPage = pCache->lru.pLruPrev;
H
Hongze Cheng 已提交
138 139
    tdbPCacheRemovePageFromHash(pCache, pPage);
    tdbPCachePinPage(pCache, pPage);
H
more  
Hongze Cheng 已提交
140 141
  }

H
Hongze Cheng 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155
  // 4. Try a create new page
  if (pTxn && pTxn->xMalloc) {
    ret = tdbPageCreate(pCache->pageSize, &pPage, pTxn->xMalloc, pTxn->xArg);
    if (ret < 0) {
      // TODO
      ASSERT(0);
      return NULL;
    }

    // init the page fields
    pPage->isAnchor = 0;
    pPage->isLocal = 0;
    TDB_INIT_PAGE_REF(pPage);
  }
H
more  
Hongze Cheng 已提交
156 157 158 159 160

  // 5. Page here are just created from a free list
  // or by recycling or allocated streesly,
  // need to initialize it
  if (pPage) {
H
more  
Hongze Cheng 已提交
161
    memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
H
more  
Hongze Cheng 已提交
162
    pPage->pLruNext = NULL;
H
more  
Hongze Cheng 已提交
163
    pPage->pPager = NULL;
H
Hongze Cheng 已提交
164 165

    // TODO: allocated page may not add to hash
H
Hongze Cheng 已提交
166
    tdbPCacheAddPageToHash(pCache, pPage);
H
more  
Hongze Cheng 已提交
167 168
  }

H
more  
Hongze Cheng 已提交
169
  return pPage;
H
more  
Hongze Cheng 已提交
170 171
}

H
Hongze Cheng 已提交
172
static void tdbPCachePinPage(SPCache *pCache, SPage *pPage) {
H
more  
Hongze Cheng 已提交
173 174 175 176 177 178
  if (!PAGE_IS_PINNED(pPage)) {
    pPage->pLruPrev->pLruNext = pPage->pLruNext;
    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
    pPage->pLruNext = NULL;

    pCache->nRecyclable--;
H
more  
Hongze Cheng 已提交
179
  }
H
more  
Hongze Cheng 已提交
180 181
}

H
Hongze Cheng 已提交
182 183
static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) {
  i32 nRef;
H
Hongze Cheng 已提交
184 185 186

  tdbPCacheLock(pCache);

H
Hongze Cheng 已提交
187 188
  ASSERT(!pPage->isDirty);

H
Hongze Cheng 已提交
189
  nRef = TDB_GET_PAGE_REF(pPage);
H
Hongze Cheng 已提交
190 191
  ASSERT(nRef >= 0);
  if (nRef == 0) {
H
Hongze Cheng 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
    if (1) {
      // Add the page to LRU list
      ASSERT(pPage->pLruNext == NULL);

      pPage->pLruPrev = &(pCache->lru);
      pPage->pLruNext = pCache->lru.pLruNext;
      pCache->lru.pLruNext->pLruPrev = pPage;
      pCache->lru.pLruNext = pPage;

      pCache->nRecyclable++;
    } else {
      // TODO: may need to free the page
    }
H
Hongze Cheng 已提交
205
  }
H
more  
Hongze Cheng 已提交
206

H
Hongze Cheng 已提交
207
  tdbPCacheUnlock(pCache);
H
more  
Hongze Cheng 已提交
208 209
}

H
Hongze Cheng 已提交
210 211 212
static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) {
  SPage **ppPage;
  int     h;
H
more  
Hongze Cheng 已提交
213 214 215 216 217 218 219 220 221 222

  h = PCACHE_PAGE_HASH(&(pPage->pgid));
  for (ppPage = &(pCache->pgHash[h % pCache->nHash]); *ppPage != pPage; ppPage = &((*ppPage)->pHashNext))
    ;
  ASSERT(*ppPage == pPage);
  *ppPage = pPage->pHashNext;

  pCache->nPage--;
}

H
Hongze Cheng 已提交
223 224
static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) {
  int h;
H
more  
Hongze Cheng 已提交
225

H
more  
Hongze Cheng 已提交
226
  h = PCACHE_PAGE_HASH(&(pPage->pgid)) % pCache->nHash;
H
more  
Hongze Cheng 已提交
227 228 229 230 231

  pPage->pHashNext = pCache->pgHash[h];
  pCache->pgHash[h] = pPage;

  pCache->nPage++;
H
more  
Hongze Cheng 已提交
232 233 234
}

static int tdbPCacheOpenImpl(SPCache *pCache) {
H
refact  
Hongze Cheng 已提交
235
  SPage *pPage;
H
Hongze Cheng 已提交
236
  u8    *pPtr;
H
refact  
Hongze Cheng 已提交
237
  int    tsize;
H
Hongze Cheng 已提交
238
  int    ret;
H
more  
Hongze Cheng 已提交
239 240 241 242 243 244 245

  tdbPCacheInitLock(pCache);

  // Open the free list
  pCache->nFree = 0;
  pCache->pFree = NULL;
  for (int i = 0; i < pCache->cacheSize; i++) {
H
Hongze Cheng 已提交
246
    ret = tdbPageCreate(pCache->pageSize, &pPage, NULL, NULL);
H
Hongze Cheng 已提交
247 248
    if (ret < 0) {
      // TODO: handle error
H
more  
Hongze Cheng 已提交
249 250 251 252 253
      return -1;
    }

    // pPage->pgid = 0;
    pPage->isAnchor = 0;
H
Hongze Cheng 已提交
254
    pPage->isLocal = 1;
H
Hongze Cheng 已提交
255
    TDB_INIT_PAGE_REF(pPage);
H
more  
Hongze Cheng 已提交
256 257 258
    pPage->pHashNext = NULL;
    pPage->pLruNext = NULL;
    pPage->pLruPrev = NULL;
H
more  
Hongze Cheng 已提交
259
    pPage->pDirtyNext = NULL;
H
more  
Hongze Cheng 已提交
260

H
Hongze Cheng 已提交
261
    // add page to free list
H
more  
Hongze Cheng 已提交
262 263 264 265 266 267 268 269
    pPage->pFreeNext = pCache->pFree;
    pCache->pFree = pPage;
    pCache->nFree++;
  }

  // Open the hash table
  pCache->nPage = 0;
  pCache->nHash = pCache->cacheSize;
H
Hongze Cheng 已提交
270
  pCache->pgHash = (SPage **)tdbOsCalloc(pCache->nHash, sizeof(SPage *));
H
more  
Hongze Cheng 已提交
271 272 273 274 275 276 277 278 279 280 281 282
  if (pCache->pgHash == NULL) {
    // TODO
    return -1;
  }

  // Open LRU list
  pCache->nRecyclable = 0;
  pCache->lru.isAnchor = 1;
  pCache->lru.pLruNext = &(pCache->lru);
  pCache->lru.pLruPrev = &(pCache->lru);

  return 0;
H
Hongze Cheng 已提交
283 284
}

H
Hongze Cheng 已提交
285 286 287 288 289
static int tdbPCacheCloseImpl(SPCache *pCache) {
  SPage *pPage;

  for (pPage = pCache->pList; pPage; pPage = pCache->pList) {
    pCache->pList = pPage->pCacheNext;
H
refact  
Hongze Cheng 已提交
290
    tdbPageDestroy(pPage, NULL, NULL);
H
Hongze Cheng 已提交
291 292 293 294
  }

  tdbPCacheDestroyLock(pCache);
}