tdbPCache.c 7.6 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 18 19 20
struct SPCache {
  int             pageSize;
  int             cacheSize;
  pthread_mutex_t mutex;
H
more  
Hongze Cheng 已提交
21
  int             nFree;
H
Hongze Cheng 已提交
22
  SPage          *pFree;
H
more  
Hongze Cheng 已提交
23 24
  int             nPage;
  int             nHash;
H
Hongze Cheng 已提交
25
  SPage         **pgHash;
H
more  
Hongze Cheng 已提交
26
  int             nRecyclable;
H
refact  
Hongze Cheng 已提交
27
  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
Hongze Cheng 已提交
37 38 39 40 41 42 43 44 45 46 47 48
// For page ref
#define TDB_INIT_PAGE_REF(pPage) ((pPage)->nRef = 0)
#if 0
#define TDB_REF_PAGE(pPage)     (++(pPage)->nRef)
#define TDB_UNREF_PAGE(pPage)   (--(pPage)->nRef)
#define TDB_GET_PAGE_REF(pPage) ((pPage)->nRef)
#else
#define TDB_REF_PAGE(pPage)     atomic_add_fetch_32(&((pPage)->nRef), 1)
#define TDB_UNREF_PAGE(pPage)   atomic_sub_fetch_32(&((pPage)->nRef), 1)
#define TDB_GET_PAGE_REF(pPage) atomic_load_32(&((pPage)->nRef))
#endif

H
refact  
Hongze Cheng 已提交
49 50 51 52 53 54 55 56 57 58
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);
static void   tdbPCachePinPage(SPage *pPage);
static void   tdbPCacheRemovePageFromHash(SPage *pPage);
static void   tdbPCacheAddPageToHash(SPage *pPage);
H
more  
Hongze Cheng 已提交
59
static void   tdbPCacheUnpinPage(SPage *pPage);
H
Hongze Cheng 已提交
60 61
static void  *tdbOsMalloc(void *arg, size_t size);
static void   tdbOsFree(void *arg, void *ptr);
H
Hongze Cheng 已提交
62

H
Hongze Cheng 已提交
63
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
H
more  
Hongze Cheng 已提交
64
  SPCache *pCache;
H
Hongze Cheng 已提交
65 66
  void    *pPtr;
  SPage   *pPgHdr;
H
Hongze Cheng 已提交
67

H
more  
Hongze Cheng 已提交
68 69
  pCache = (SPCache *)calloc(1, sizeof(*pCache));
  if (pCache == NULL) {
H
Hongze Cheng 已提交
70 71 72
    return -1;
  }

H
more  
Hongze Cheng 已提交
73 74
  pCache->pageSize = pageSize;
  pCache->cacheSize = cacheSize;
H
Hongze Cheng 已提交
75

H
more  
Hongze Cheng 已提交
76 77 78
  if (tdbPCacheOpenImpl(pCache) < 0) {
    free(pCache);
    return -1;
H
Hongze Cheng 已提交
79 80
  }

H
more  
Hongze Cheng 已提交
81
  *ppCache = pCache;
H
Hongze Cheng 已提交
82 83 84
  return 0;
}

H
Hongze Cheng 已提交
85
int tdbPCacheClose(SPCache *pCache) {
H
Hongze Cheng 已提交
86
  /* TODO */
H
Hongze Cheng 已提交
87
  return 0;
H
Hongze Cheng 已提交
88 89
}

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

H
Hongze Cheng 已提交
93
  tdbPCacheLock(pCache);
H
Hongze Cheng 已提交
94

H
more  
Hongze Cheng 已提交
95
  pPage = tdbPCacheFetchImpl(pCache, pPgid, alcNewPage);
H
Hongze Cheng 已提交
96 97 98 99
  if (pPage) {
    TDB_REF_PAGE(pPage);
  }

H
Hongze Cheng 已提交
100
  tdbPCacheUnlock(pCache);
H
more  
Hongze Cheng 已提交
101 102

  return pPage;
H
Hongze Cheng 已提交
103 104
}

H
more  
Hongze Cheng 已提交
105
void tdbPCacheRelease(SPage *pPage) {
H
Hongze Cheng 已提交
106 107 108 109 110 111
  i32 nRef;

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

  if (nRef == 0) {
H
more  
Hongze Cheng 已提交
112 113 114 115 116 117 118
    if (1 /*TODO: page still clean*/) {
      tdbPCacheUnpinPage(pPage);
    } else {
      // TODO
      ASSERT(0);
    }
  }
H
Hongze Cheng 已提交
119 120
}

H
more  
Hongze Cheng 已提交
121 122 123 124
static void tdbPCacheInitLock(SPCache *pCache) { pthread_mutex_init(&(pCache->mutex), NULL); }

static void tdbPCacheClearLock(SPCache *pCache) { pthread_mutex_destroy(&(pCache->mutex)); }

H
Hongze Cheng 已提交
125 126 127 128 129 130 131 132
static void tdbPCacheLock(SPCache *pCache) { pthread_mutex_lock(&(pCache->mutex)); }

static void tdbPCacheUnlock(SPCache *pCache) { pthread_mutex_unlock(&(pCache->mutex)); }

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

H
refact  
Hongze Cheng 已提交
135 136
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
  SPage *pPage;
H
more  
Hongze Cheng 已提交
137 138 139 140

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

H
more  
Hongze Cheng 已提交
145
  if (pPage || !alcNewPage) {
H
more  
Hongze Cheng 已提交
146 147 148
    if (pPage) {
      tdbPCachePinPage(pPage);
    }
H
more  
Hongze Cheng 已提交
149 150 151
    return pPage;
  }

H
more  
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  // 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;
    tdbPCacheRemovePageFromHash(pPage);
    tdbPCachePinPage(pPage);
  }

H
more  
Hongze Cheng 已提交
167
  // 4. Try a stress allocation (TODO)
H
more  
Hongze Cheng 已提交
168 169 170 171 172

  // 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 已提交
173
    memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
H
more  
Hongze Cheng 已提交
174
    pPage->pLruNext = NULL;
H
more  
Hongze Cheng 已提交
175
    pPage->pPager = NULL;
H
more  
Hongze Cheng 已提交
176 177 178
    tdbPCacheAddPageToHash(pPage);
  }

H
more  
Hongze Cheng 已提交
179
  return pPage;
H
more  
Hongze Cheng 已提交
180 181
}

H
refact  
Hongze Cheng 已提交
182
static void tdbPCachePinPage(SPage *pPage) {
H
more  
Hongze Cheng 已提交
183 184 185 186 187 188 189 190 191
  SPCache *pCache;

  pCache = pPage->pCache;
  if (!PAGE_IS_PINNED(pPage)) {
    pPage->pLruPrev->pLruNext = pPage->pLruNext;
    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
    pPage->pLruNext = NULL;

    pCache->nRecyclable--;
H
more  
Hongze Cheng 已提交
192
  }
H
more  
Hongze Cheng 已提交
193 194
}

H
more  
Hongze Cheng 已提交
195 196
static void tdbPCacheUnpinPage(SPage *pPage) {
  SPCache *pCache;
H
Hongze Cheng 已提交
197 198 199 200 201 202
  i32      nRef;

  pCache = pPage->pCache;

  tdbPCacheLock(pCache);

H
Hongze Cheng 已提交
203
  nRef = TDB_GET_PAGE_REF(pPage);
H
Hongze Cheng 已提交
204 205 206 207
  ASSERT(nRef >= 0);
  if (nRef == 0) {
    // Add the page to LRU list
    ASSERT(pPage->pLruNext == NULL);
H
more  
Hongze Cheng 已提交
208

H
Hongze Cheng 已提交
209 210 211 212 213
    pPage->pLruPrev = &(pCache->lru);
    pPage->pLruNext = pCache->lru.pLruNext;
    pCache->lru.pLruNext->pLruPrev = pPage;
    pCache->lru.pLruNext = pPage;
  }
H
more  
Hongze Cheng 已提交
214 215

  pCache->nRecyclable++;
H
Hongze Cheng 已提交
216 217

  tdbPCacheUnlock(pCache);
H
more  
Hongze Cheng 已提交
218 219
}

H
refact  
Hongze Cheng 已提交
220
static void tdbPCacheRemovePageFromHash(SPage *pPage) {
H
more  
Hongze Cheng 已提交
221
  SPCache *pCache;
H
Hongze Cheng 已提交
222
  SPage  **ppPage;
H
more  
Hongze Cheng 已提交
223 224 225 226 227 228 229 230 231 232 233 234
  int      h;

  pCache = pPage->pCache;
  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
refact  
Hongze Cheng 已提交
235
static void tdbPCacheAddPageToHash(SPage *pPage) {
H
more  
Hongze Cheng 已提交
236 237 238 239
  SPCache *pCache;
  int      h;

  pCache = pPage->pCache;
H
more  
Hongze Cheng 已提交
240
  h = PCACHE_PAGE_HASH(&(pPage->pgid)) % pCache->nHash;
H
more  
Hongze Cheng 已提交
241 242 243 244 245

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

  pCache->nPage++;
H
more  
Hongze Cheng 已提交
246 247 248
}

static int tdbPCacheOpenImpl(SPCache *pCache) {
H
refact  
Hongze Cheng 已提交
249
  SPage *pPage;
H
Hongze Cheng 已提交
250
  u8    *pPtr;
H
refact  
Hongze Cheng 已提交
251
  int    tsize;
H
Hongze Cheng 已提交
252
  int    ret;
H
more  
Hongze Cheng 已提交
253 254 255 256 257 258 259

  tdbPCacheInitLock(pCache);

  // Open the free list
  pCache->nFree = 0;
  pCache->pFree = NULL;
  for (int i = 0; i < pCache->cacheSize; i++) {
H
Hongze Cheng 已提交
260 261 262
    ret = tdbPageCreate(pCache->pageSize, &pPage, tdbOsMalloc, NULL);
    if (ret < 0) {
      // TODO: handle error
H
more  
Hongze Cheng 已提交
263 264 265 266 267 268 269
      return -1;
    }

    // pPage->pgid = 0;
    pPage->isAnchor = 0;
    pPage->isLocalPage = 1;
    pPage->pCache = pCache;
H
Hongze Cheng 已提交
270
    TDB_INIT_PAGE_REF(pPage);
H
more  
Hongze Cheng 已提交
271 272 273
    pPage->pHashNext = NULL;
    pPage->pLruNext = NULL;
    pPage->pLruPrev = NULL;
H
more  
Hongze Cheng 已提交
274
    pPage->pDirtyNext = NULL;
H
more  
Hongze Cheng 已提交
275 276 277 278 279 280 281 282 283

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

  // Open the hash table
  pCache->nPage = 0;
  pCache->nHash = pCache->cacheSize;
H
refact  
Hongze Cheng 已提交
284
  pCache->pgHash = (SPage **)calloc(pCache->nHash, sizeof(SPage *));
H
more  
Hongze Cheng 已提交
285 286 287 288 289 290 291 292 293 294 295 296
  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 已提交
297 298
}

H
Hongze Cheng 已提交
299 300 301 302 303 304 305 306 307 308 309
int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; }

static void *tdbOsMalloc(void *arg, size_t size) {
  void *ptr;

  ptr = malloc(size);

  return ptr;
}

static void tdbOsFree(void *arg, void *ptr) { free(ptr); }