removed static variables from bufferPool.c; renamed methods, moved Page typedef to common.h
This commit is contained in:
parent
4b07b538a6
commit
7db35ecd39
6 changed files with 167 additions and 162 deletions
|
@ -6,7 +6,6 @@
|
||||||
#include <stasis/doubleLinkedList.h>
|
#include <stasis/doubleLinkedList.h>
|
||||||
#include <stasis/lhtable.h>
|
#include <stasis/lhtable.h>
|
||||||
|
|
||||||
#include <stasis/bufferPool.h>
|
|
||||||
#include <stasis/pageHandle.h>
|
#include <stasis/pageHandle.h>
|
||||||
|
|
||||||
#include <stasis/replacementPolicy.h>
|
#include <stasis/replacementPolicy.h>
|
||||||
|
@ -33,6 +32,8 @@ static Page ** freeList;
|
||||||
// A page is in LRU iff !pending, !pinned
|
// A page is in LRU iff !pending, !pinned
|
||||||
static replacementPolicy * lru;
|
static replacementPolicy * lru;
|
||||||
|
|
||||||
|
static stasis_buffer_pool_t * stasis_buffer_pool;
|
||||||
|
|
||||||
static int running;
|
static int running;
|
||||||
|
|
||||||
typedef struct LL_ENTRY(node_t) node_t;
|
typedef struct LL_ENTRY(node_t) node_t;
|
||||||
|
@ -128,8 +129,8 @@ inline static Page * writeBackOnePage() {
|
||||||
inline static Page * getFreePage() {
|
inline static Page * getFreePage() {
|
||||||
Page * ret;
|
Page * ret;
|
||||||
if(pageCount < MAX_BUFFER_SIZE) {
|
if(pageCount < MAX_BUFFER_SIZE) {
|
||||||
ret = pageMalloc();
|
ret = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
|
||||||
pageFree(ret,-1);
|
stasis_buffer_pool_free_page(stasis_buffer_pool, ret,-1);
|
||||||
(*pagePinCountPtr(ret)) = 0;
|
(*pagePinCountPtr(ret)) = 0;
|
||||||
(*pagePendingPtr(ret)) = 0;
|
(*pagePendingPtr(ret)) = 0;
|
||||||
pageSetNode(ret,0,0);
|
pageSetNode(ret,0,0);
|
||||||
|
@ -348,7 +349,7 @@ static void bhBufDeinit() {
|
||||||
free(freeList);
|
free(freeList);
|
||||||
|
|
||||||
lru->deinit(lru);
|
lru->deinit(lru);
|
||||||
bufferPoolDeInit();
|
stasis_buffer_pool_deinit(stasis_buffer_pool);
|
||||||
}
|
}
|
||||||
static void bhSimulateBufferManagerCrash() {
|
static void bhSimulateBufferManagerCrash() {
|
||||||
running = 0;
|
running = 0;
|
||||||
|
@ -372,7 +373,7 @@ static void bhSimulateBufferManagerCrash() {
|
||||||
free(freeList);
|
free(freeList);
|
||||||
|
|
||||||
lru->deinit(lru);
|
lru->deinit(lru);
|
||||||
bufferPoolDeInit();
|
stasis_buffer_pool_deinit(stasis_buffer_pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bhBufInit() {
|
void bhBufInit() {
|
||||||
|
@ -392,7 +393,7 @@ void bhBufInit() {
|
||||||
bufDeinit = bhBufDeinit;
|
bufDeinit = bhBufDeinit;
|
||||||
simulateBufferManagerCrash = bhSimulateBufferManagerCrash;
|
simulateBufferManagerCrash = bhSimulateBufferManagerCrash;
|
||||||
|
|
||||||
bufferPoolInit();
|
stasis_buffer_pool = stasis_buffer_pool_init();
|
||||||
|
|
||||||
lru = lruFastInit(pageGetNode, pageSetNode, 0);
|
lru = lruFastInit(pageGetNode, pageSetNode, 0);
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ static compensated_function Page *bufManLoadUninitPage(int xid, pageid_t pageid)
|
||||||
static void bufManReleasePage (Page * p);
|
static void bufManReleasePage (Page * p);
|
||||||
static void bufManSimulateBufferManagerCrash();
|
static void bufManSimulateBufferManagerCrash();
|
||||||
|
|
||||||
|
static stasis_buffer_pool_t * stasis_buffer_pool;
|
||||||
|
|
||||||
int bufManBufInit() {
|
int bufManBufInit() {
|
||||||
|
|
||||||
releasePageImpl = bufManReleasePage;
|
releasePageImpl = bufManReleasePage;
|
||||||
|
@ -36,17 +38,17 @@ int bufManBufInit() {
|
||||||
bufDeinit = bufManBufDeinit;
|
bufDeinit = bufManBufDeinit;
|
||||||
simulateBufferManagerCrash = bufManSimulateBufferManagerCrash;
|
simulateBufferManagerCrash = bufManSimulateBufferManagerCrash;
|
||||||
|
|
||||||
bufferPoolInit();
|
stasis_buffer_pool = stasis_buffer_pool_init();
|
||||||
|
|
||||||
pthread_mutex_init(&loadPagePtr_mutex, NULL);
|
pthread_mutex_init(&loadPagePtr_mutex, NULL);
|
||||||
|
|
||||||
activePages = LH_ENTRY(create)(16);
|
activePages = LH_ENTRY(create)(16);
|
||||||
|
|
||||||
dummy_page = pageMalloc();
|
dummy_page = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
|
||||||
pageFree(dummy_page, -1);
|
stasis_buffer_pool_free_page(stasis_buffer_pool, dummy_page, -1);
|
||||||
Page *first;
|
Page *first;
|
||||||
first = pageMalloc();
|
first = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
|
||||||
pageFree(first, 0);
|
stasis_buffer_pool_free_page(stasis_buffer_pool, first, 0);
|
||||||
LH_ENTRY(insert)(activePages, &first->id, sizeof(first->id), first);
|
LH_ENTRY(insert)(activePages, &first->id, sizeof(first->id), first);
|
||||||
pageRead(first);
|
pageRead(first);
|
||||||
pageCacheInit(first);
|
pageCacheInit(first);
|
||||||
|
@ -83,7 +85,7 @@ static void bufManBufDeinit() {
|
||||||
|
|
||||||
//closePageFile();
|
//closePageFile();
|
||||||
|
|
||||||
bufferPoolDeInit();
|
stasis_buffer_pool_deinit(stasis_buffer_pool);
|
||||||
|
|
||||||
#ifdef PIN_COUNT
|
#ifdef PIN_COUNT
|
||||||
if(pinCount != 0) {
|
if(pinCount != 0) {
|
||||||
|
@ -218,7 +220,7 @@ static Page* bufManGetPage(pageid_t pageid, int locktype, int uninitialized) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
ret = pageMalloc();
|
ret = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
|
||||||
ret->id = -1;
|
ret->id = -1;
|
||||||
ret->inCache = 0;
|
ret->inCache = 0;
|
||||||
}
|
}
|
||||||
|
@ -260,7 +262,7 @@ static Page* bufManGetPage(pageid_t pageid, int locktype, int uninitialized) {
|
||||||
pageWrite(ret);
|
pageWrite(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
pageFree(ret, pageid);
|
stasis_buffer_pool_free_page(stasis_buffer_pool, ret, pageid);
|
||||||
if(!uninitialized) {
|
if(!uninitialized) {
|
||||||
pageRead(ret);
|
pageRead(ret);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -48,83 +48,77 @@ terms specified in this license.
|
||||||
* $Id$
|
* $Id$
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
#include <assert.h>
|
||||||
/* _XOPEN_SOURCE is needed for posix_memalign */
|
|
||||||
#define _XOPEN_SOURCE 600
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include <stasis/bufferPool.h>
|
#include <stasis/bufferPool.h>
|
||||||
#include <assert.h>
|
|
||||||
#include <stasis/truncation.h>
|
|
||||||
#include <stasis/page.h>
|
#include <stasis/page.h>
|
||||||
/* TODO: Combine with buffer size... */
|
|
||||||
static pageid_t nextPage = 0;
|
|
||||||
static pthread_mutex_t pageMallocMutex;
|
|
||||||
|
|
||||||
static void * addressFromMalloc = 0;
|
struct stasis_buffer_pool_t {
|
||||||
|
pageid_t nextPage;
|
||||||
|
Page* pool;
|
||||||
|
pthread_mutex_t mut;
|
||||||
|
void * addr_to_free;
|
||||||
|
};
|
||||||
|
|
||||||
/** We need one dummy page for locking purposes, so this array has one extra page in it. */
|
stasis_buffer_pool_t* stasis_buffer_pool_init() {
|
||||||
Page pool[MAX_BUFFER_SIZE+1];
|
|
||||||
|
|
||||||
|
stasis_buffer_pool_t * ret = malloc(sizeof(*ret));
|
||||||
|
|
||||||
void bufferPoolInit() {
|
ret->nextPage = 0;
|
||||||
|
|
||||||
nextPage = 0;
|
pthread_mutex_init(&(ret->mut), NULL);
|
||||||
|
|
||||||
pthread_mutex_init(&pageMallocMutex, NULL);
|
byte * bufferSpace = calloc((MAX_BUFFER_SIZE + 2), PAGE_SIZE);
|
||||||
|
|
||||||
byte * bufferSpace ;
|
|
||||||
|
|
||||||
bufferSpace = calloc((MAX_BUFFER_SIZE + 2), PAGE_SIZE);
|
|
||||||
assert(bufferSpace);
|
assert(bufferSpace);
|
||||||
addressFromMalloc = bufferSpace;
|
ret->addr_to_free = bufferSpace;
|
||||||
|
|
||||||
bufferSpace = (byte*)(((long)bufferSpace) +
|
bufferSpace = (byte*)(((long)bufferSpace) +
|
||||||
PAGE_SIZE -
|
PAGE_SIZE -
|
||||||
(((long)bufferSpace) % PAGE_SIZE));
|
(((long)bufferSpace) % PAGE_SIZE));
|
||||||
|
|
||||||
|
// We need one dummy page for locking purposes,
|
||||||
|
// so this array has one extra page in it.
|
||||||
|
ret->pool = malloc(sizeof(ret->pool[0])*(MAX_BUFFER_SIZE+1));
|
||||||
|
|
||||||
for(pageid_t i = 0; i < MAX_BUFFER_SIZE+1; i++) {
|
for(pageid_t i = 0; i < MAX_BUFFER_SIZE+1; i++) {
|
||||||
pool[i].rwlatch = initlock();
|
ret->pool[i].rwlatch = initlock();
|
||||||
pool[i].loadlatch = initlock();
|
ret->pool[i].loadlatch = initlock();
|
||||||
pool[i].memAddr = &(bufferSpace[i*PAGE_SIZE]);
|
ret->pool[i].memAddr = &(bufferSpace[i*PAGE_SIZE]);
|
||||||
pool[i].dirty = 0;
|
ret->pool[i].dirty = 0;
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bufferPoolDeInit() {
|
void stasis_buffer_pool_deinit(stasis_buffer_pool_t * ret) {
|
||||||
for(pageid_t i = 0; i < MAX_BUFFER_SIZE+1; i++) {
|
for(pageid_t i = 0; i < MAX_BUFFER_SIZE+1; i++) {
|
||||||
deletelock(pool[i].rwlatch);
|
deletelock(ret->pool[i].rwlatch);
|
||||||
deletelock(pool[i].loadlatch);
|
deletelock(ret->pool[i].loadlatch);
|
||||||
}
|
}
|
||||||
free(addressFromMalloc); // breaks efence
|
free(ret->addr_to_free); // breaks efence
|
||||||
pthread_mutex_destroy(&pageMallocMutex);
|
pthread_mutex_destroy(&ret->mut);
|
||||||
}
|
}
|
||||||
|
|
||||||
Page* pageMalloc() {
|
Page* stasis_buffer_pool_malloc_page(stasis_buffer_pool_t * ret) {
|
||||||
Page *page;
|
Page *page;
|
||||||
|
|
||||||
pthread_mutex_lock(&pageMallocMutex);
|
pthread_mutex_lock(&ret->mut);
|
||||||
|
|
||||||
page = &(pool[nextPage]);
|
page = &(ret->pool[ret->nextPage]);
|
||||||
|
|
||||||
nextPage++;
|
(ret->nextPage)++;
|
||||||
/* There's a dummy page that we need to keep around, thus the +1 */
|
/* There's a dummy page that we need to keep around, thus the +1 */
|
||||||
assert(nextPage <= MAX_BUFFER_SIZE + 1);
|
assert(ret->nextPage <= MAX_BUFFER_SIZE + 1);
|
||||||
|
|
||||||
pthread_mutex_unlock(&pageMallocMutex);
|
pthread_mutex_unlock(&ret->mut);
|
||||||
|
|
||||||
return page;
|
return page;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void stasis_buffer_pool_free_page(stasis_buffer_pool_t * ret, Page *p, pageid_t id) {
|
||||||
static void pageFreeNoLock(Page *p, pageid_t id) {
|
writelock(p->rwlatch, 10);
|
||||||
p->id = id;
|
p->id = id;
|
||||||
p->LSN = 0;
|
p->LSN = 0;
|
||||||
p->dirty = 0;
|
p->dirty = 0;
|
||||||
}
|
|
||||||
|
|
||||||
void pageFree(Page *p, pageid_t id) {
|
|
||||||
writelock(p->rwlatch, 10);
|
|
||||||
pageFreeNoLock(p,id);
|
|
||||||
writeunlock(p->rwlatch);
|
writeunlock(p->rwlatch);
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,13 +84,6 @@ terms specified in this license.
|
||||||
|
|
||||||
BEGIN_C_DECLS
|
BEGIN_C_DECLS
|
||||||
|
|
||||||
typedef struct Page_s Page_s;
|
|
||||||
/**
|
|
||||||
Page is defined in bufferManager.h as an incomplete type to enforce
|
|
||||||
an abstraction barrier between page.h and the rest of the system.
|
|
||||||
*/
|
|
||||||
typedef struct Page_s Page;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtain a pointer to a page from the buffer manager. The page will
|
* Obtain a pointer to a page from the buffer manager. The page will
|
||||||
* be pinned, and the pointer valid until releasePage is called.
|
* be pinned, and the pointer valid until releasePage is called.
|
||||||
|
|
|
@ -40,6 +40,9 @@ permission to use and distribute the software in accordance with the
|
||||||
terms specified in this license.
|
terms specified in this license.
|
||||||
---*/
|
---*/
|
||||||
|
|
||||||
|
#ifndef STASIS_BUFFER_POOL_H
|
||||||
|
#define STASIS_BUFFER_POOL_H
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
*
|
*
|
||||||
|
@ -47,16 +50,19 @@ terms specified in this license.
|
||||||
* $Id$
|
* $Id$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "bufferManager.h"
|
#include <stasis/common.h>
|
||||||
|
|
||||||
void bufferPoolInit();
|
typedef struct stasis_buffer_pool_t stasis_buffer_pool_t;
|
||||||
|
|
||||||
void bufferPoolDeInit();
|
stasis_buffer_pool_t* stasis_buffer_pool_init();
|
||||||
|
|
||||||
Page* pageMalloc();
|
void stasis_buffer_pool_deinit(stasis_buffer_pool_t* pool);
|
||||||
|
|
||||||
|
Page* stasis_buffer_pool_malloc_page(stasis_buffer_pool_t* pool);
|
||||||
/**
|
/**
|
||||||
Return a page to the in memory pool of free pages.
|
Return a page to the in memory pool of free pages.
|
||||||
|
|
||||||
@see pageMalloc()
|
@see stasis_buffer_pool_malloc_page()
|
||||||
*/
|
*/
|
||||||
void pageFree(Page * p, pageid_t id);
|
void stasis_buffer_pool_free_page(stasis_buffer_pool_t* pool, Page * p, pageid_t id);
|
||||||
|
#endif // STASIS_BUFFER_POOL_H
|
||||||
|
|
|
@ -134,6 +134,15 @@ typedef struct {
|
||||||
} recordid;
|
} recordid;
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
/*
|
||||||
|
Define Page as an incomplete type to hide its implementation from clients.
|
||||||
|
|
||||||
|
Include stasis/page.h for the complete definition.
|
||||||
|
*/
|
||||||
|
typedef struct Page_s Page;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "compensations.h"
|
#include "compensations.h"
|
||||||
|
|
||||||
#endif /* __stasis_common_h */
|
#endif /* __stasis_common_h */
|
||||||
|
|
Loading…
Reference in a new issue