tdbPCache.c 6.7 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 21 22 23 24 25 26 27
  int         pageSize;
  int         cacheSize;
  tdb_mutex_t mutex;
  int         nFree;
  SPage      *pFree;
  int         nPage;
  int         nHash;
  SPage     **pgHash;
  int         nRecyclable;
  SPage       lru;
H
Hongze Cheng 已提交
28 29
};

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

H
refact  
Hongze Cheng 已提交
37 38 39 40 41 42 43
static int    tdbPCacheOpenImpl(SPCache *pCache);
static void   tdbPCacheInitLock(SPCache *pCache);
static void   tdbPCacheClearLock(SPCache *pCache);
static void   tdbPCacheLock(SPCache *pCache);
static void   tdbPCacheUnlock(SPCache *pCache);
static bool   tdbPCacheLocked(SPCache *pCache);
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
H
Hongze Cheng 已提交
44 45 46 47
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 已提交
48

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

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

H
more  
Hongze Cheng 已提交
59 60
  pCache->pageSize = pageSize;
  pCache->cacheSize = cacheSize;
H
Hongze Cheng 已提交
61

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

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

H
Hongze Cheng 已提交
71
int tdbPCacheClose(SPCache *pCache) {
H
Hongze Cheng 已提交
72
  /* TODO */
H
Hongze Cheng 已提交
73
  return 0;
H
Hongze Cheng 已提交
74 75
}

H
refact  
Hongze Cheng 已提交
76 77
SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
  SPage *pPage;
H
more  
Hongze Cheng 已提交
78

H
Hongze Cheng 已提交
79
  tdbPCacheLock(pCache);
H
Hongze Cheng 已提交
80

H
more  
Hongze Cheng 已提交
81
  pPage = tdbPCacheFetchImpl(pCache, pPgid, alcNewPage);
H
Hongze Cheng 已提交
82 83 84 85
  if (pPage) {
    TDB_REF_PAGE(pPage);
  }

H
Hongze Cheng 已提交
86
  tdbPCacheUnlock(pCache);
H
more  
Hongze Cheng 已提交
87 88

  return pPage;
H
Hongze Cheng 已提交
89 90
}

H
Hongze Cheng 已提交
91
void tdbPCacheRelease(SPCache *pCache, SPage *pPage) {
H
Hongze Cheng 已提交
92 93 94 95 96 97
  i32 nRef;

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

  if (nRef == 0) {
H
Hongze Cheng 已提交
98
    tdbPCacheUnpinPage(pCache, pPage);
H
more  
Hongze Cheng 已提交
99
  }
H
Hongze Cheng 已提交
100 101
}

H
Hongze Cheng 已提交
102
static void tdbPCacheInitLock(SPCache *pCache) { tdbMutexInit(&(pCache->mutex), NULL); }
H
more  
Hongze Cheng 已提交
103

H
Hongze Cheng 已提交
104
static void tdbPCacheClearLock(SPCache *pCache) { tdbMutexDestroy(&(pCache->mutex)); }
H
more  
Hongze Cheng 已提交
105

H
Hongze Cheng 已提交
106
static void tdbPCacheLock(SPCache *pCache) { tdbMutexLock(&(pCache->mutex)); }
H
Hongze Cheng 已提交
107

H
Hongze Cheng 已提交
108
static void tdbPCacheUnlock(SPCache *pCache) { tdbMutexUnlock(&(pCache->mutex)); }
H
Hongze Cheng 已提交
109 110 111 112 113

static bool tdbPCacheLocked(SPCache *pCache) {
  assert(0);
  // TODO
  return true;
H
more  
Hongze Cheng 已提交
114 115
}

H
refact  
Hongze Cheng 已提交
116 117
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
  SPage *pPage;
H
more  
Hongze Cheng 已提交
118 119 120 121

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

H
more  
Hongze Cheng 已提交
126
  if (pPage || !alcNewPage) {
H
more  
Hongze Cheng 已提交
127
    if (pPage) {
H
Hongze Cheng 已提交
128
      tdbPCachePinPage(pCache, pPage);
H
more  
Hongze Cheng 已提交
129
    }
H
more  
Hongze Cheng 已提交
130 131 132
    return pPage;
  }

H
more  
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141 142 143
  // 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 已提交
144 145
    tdbPCacheRemovePageFromHash(pCache, pPage);
    tdbPCachePinPage(pCache, pPage);
H
more  
Hongze Cheng 已提交
146 147
  }

H
more  
Hongze Cheng 已提交
148
  // 4. Try a stress allocation (TODO)
H
more  
Hongze Cheng 已提交
149 150 151 152 153

  // 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 已提交
154
    memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
H
more  
Hongze Cheng 已提交
155
    pPage->pLruNext = NULL;
H
more  
Hongze Cheng 已提交
156
    pPage->pPager = NULL;
H
Hongze Cheng 已提交
157
    tdbPCacheAddPageToHash(pCache, pPage);
H
more  
Hongze Cheng 已提交
158 159
  }

H
more  
Hongze Cheng 已提交
160
  return pPage;
H
more  
Hongze Cheng 已提交
161 162
}

H
Hongze Cheng 已提交
163
static void tdbPCachePinPage(SPCache *pCache, SPage *pPage) {
H
more  
Hongze Cheng 已提交
164 165 166 167 168 169
  if (!PAGE_IS_PINNED(pPage)) {
    pPage->pLruPrev->pLruNext = pPage->pLruNext;
    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
    pPage->pLruNext = NULL;

    pCache->nRecyclable--;
H
more  
Hongze Cheng 已提交
170
  }
H
more  
Hongze Cheng 已提交
171 172
}

H
Hongze Cheng 已提交
173 174
static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) {
  i32 nRef;
H
Hongze Cheng 已提交
175 176 177

  tdbPCacheLock(pCache);

H
Hongze Cheng 已提交
178 179
  ASSERT(!pPage->isDirty);

H
Hongze Cheng 已提交
180
  nRef = TDB_GET_PAGE_REF(pPage);
H
Hongze Cheng 已提交
181 182 183 184
  ASSERT(nRef >= 0);
  if (nRef == 0) {
    // Add the page to LRU list
    ASSERT(pPage->pLruNext == NULL);
H
more  
Hongze Cheng 已提交
185

H
Hongze Cheng 已提交
186 187 188 189 190
    pPage->pLruPrev = &(pCache->lru);
    pPage->pLruNext = pCache->lru.pLruNext;
    pCache->lru.pLruNext->pLruPrev = pPage;
    pCache->lru.pLruNext = pPage;
  }
H
more  
Hongze Cheng 已提交
191 192

  pCache->nRecyclable++;
H
Hongze Cheng 已提交
193 194

  tdbPCacheUnlock(pCache);
H
more  
Hongze Cheng 已提交
195 196
}

H
Hongze Cheng 已提交
197 198 199
static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) {
  SPage **ppPage;
  int     h;
H
more  
Hongze Cheng 已提交
200 201 202 203 204 205 206 207 208 209

  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 已提交
210 211
static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) {
  int h;
H
more  
Hongze Cheng 已提交
212

H
more  
Hongze Cheng 已提交
213
  h = PCACHE_PAGE_HASH(&(pPage->pgid)) % pCache->nHash;
H
more  
Hongze Cheng 已提交
214 215 216 217 218

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

  pCache->nPage++;
H
more  
Hongze Cheng 已提交
219 220 221
}

static int tdbPCacheOpenImpl(SPCache *pCache) {
H
refact  
Hongze Cheng 已提交
222
  SPage *pPage;
H
Hongze Cheng 已提交
223
  u8    *pPtr;
H
refact  
Hongze Cheng 已提交
224
  int    tsize;
H
Hongze Cheng 已提交
225
  int    ret;
H
more  
Hongze Cheng 已提交
226 227 228 229 230 231 232

  tdbPCacheInitLock(pCache);

  // Open the free list
  pCache->nFree = 0;
  pCache->pFree = NULL;
  for (int i = 0; i < pCache->cacheSize; i++) {
H
Hongze Cheng 已提交
233
    ret = tdbPageCreate(pCache->pageSize, &pPage, NULL, NULL);
H
Hongze Cheng 已提交
234 235
    if (ret < 0) {
      // TODO: handle error
H
more  
Hongze Cheng 已提交
236 237 238 239 240 241
      return -1;
    }

    // pPage->pgid = 0;
    pPage->isAnchor = 0;
    pPage->isLocalPage = 1;
H
Hongze Cheng 已提交
242
    TDB_INIT_PAGE_REF(pPage);
H
more  
Hongze Cheng 已提交
243 244 245
    pPage->pHashNext = NULL;
    pPage->pLruNext = NULL;
    pPage->pLruPrev = NULL;
H
more  
Hongze Cheng 已提交
246
    pPage->pDirtyNext = NULL;
H
more  
Hongze Cheng 已提交
247 248 249 250 251 252 253 254 255

    pPage->pFreeNext = pCache->pFree;
    pCache->pFree = pPage;
    pCache->nFree++;
  }

  // Open the hash table
  pCache->nPage = 0;
  pCache->nHash = pCache->cacheSize;
H
Hongze Cheng 已提交
256
  pCache->pgHash = (SPage **)tdbOsCalloc(pCache->nHash, sizeof(SPage *));
H
more  
Hongze Cheng 已提交
257 258 259 260 261 262 263 264 265 266 267 268
  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 已提交
269 270
}

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