removed static variables from bufferPool.c; renamed methods, moved Page typedef to common.h

This commit is contained in:
Sears Russell 2009-04-13 21:55:08 +00:00
parent 4b07b538a6
commit 7db35ecd39
6 changed files with 167 additions and 162 deletions

View file

@ -6,7 +6,6 @@
#include <stasis/doubleLinkedList.h>
#include <stasis/lhtable.h>
#include <stasis/bufferPool.h>
#include <stasis/pageHandle.h>
#include <stasis/replacementPolicy.h>
@ -33,6 +32,8 @@ static Page ** freeList;
// A page is in LRU iff !pending, !pinned
static replacementPolicy * lru;
static stasis_buffer_pool_t * stasis_buffer_pool;
static int running;
typedef struct LL_ENTRY(node_t) node_t;
@ -128,8 +129,8 @@ inline static Page * writeBackOnePage() {
inline static Page * getFreePage() {
Page * ret;
if(pageCount < MAX_BUFFER_SIZE) {
ret = pageMalloc();
pageFree(ret,-1);
ret = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
stasis_buffer_pool_free_page(stasis_buffer_pool, ret,-1);
(*pagePinCountPtr(ret)) = 0;
(*pagePendingPtr(ret)) = 0;
pageSetNode(ret,0,0);
@ -348,7 +349,7 @@ static void bhBufDeinit() {
free(freeList);
lru->deinit(lru);
bufferPoolDeInit();
stasis_buffer_pool_deinit(stasis_buffer_pool);
}
static void bhSimulateBufferManagerCrash() {
running = 0;
@ -372,7 +373,7 @@ static void bhSimulateBufferManagerCrash() {
free(freeList);
lru->deinit(lru);
bufferPoolDeInit();
stasis_buffer_pool_deinit(stasis_buffer_pool);
}
void bhBufInit() {
@ -392,7 +393,7 @@ void bhBufInit() {
bufDeinit = bhBufDeinit;
simulateBufferManagerCrash = bhSimulateBufferManagerCrash;
bufferPoolInit();
stasis_buffer_pool = stasis_buffer_pool_init();
lru = lruFastInit(pageGetNode, pageSetNode, 0);

View file

@ -25,6 +25,8 @@ static compensated_function Page *bufManLoadUninitPage(int xid, pageid_t pageid)
static void bufManReleasePage (Page * p);
static void bufManSimulateBufferManagerCrash();
static stasis_buffer_pool_t * stasis_buffer_pool;
int bufManBufInit() {
releasePageImpl = bufManReleasePage;
@ -36,17 +38,17 @@ int bufManBufInit() {
bufDeinit = bufManBufDeinit;
simulateBufferManagerCrash = bufManSimulateBufferManagerCrash;
bufferPoolInit();
stasis_buffer_pool = stasis_buffer_pool_init();
pthread_mutex_init(&loadPagePtr_mutex, NULL);
activePages = LH_ENTRY(create)(16);
dummy_page = pageMalloc();
pageFree(dummy_page, -1);
dummy_page = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
stasis_buffer_pool_free_page(stasis_buffer_pool, dummy_page, -1);
Page *first;
first = pageMalloc();
pageFree(first, 0);
first = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
stasis_buffer_pool_free_page(stasis_buffer_pool, first, 0);
LH_ENTRY(insert)(activePages, &first->id, sizeof(first->id), first);
pageRead(first);
pageCacheInit(first);
@ -83,7 +85,7 @@ static void bufManBufDeinit() {
//closePageFile();
bufferPoolDeInit();
stasis_buffer_pool_deinit(stasis_buffer_pool);
#ifdef PIN_COUNT
if(pinCount != 0) {
@ -218,7 +220,7 @@ static Page* bufManGetPage(pageid_t pageid, int locktype, int uninitialized) {
} else {
ret = pageMalloc();
ret = stasis_buffer_pool_malloc_page(stasis_buffer_pool);
ret->id = -1;
ret->inCache = 0;
}
@ -260,7 +262,7 @@ static Page* bufManGetPage(pageid_t pageid, int locktype, int uninitialized) {
pageWrite(ret);
}
pageFree(ret, pageid);
stasis_buffer_pool_free_page(stasis_buffer_pool, ret, pageid);
if(!uninitialized) {
pageRead(ret);
} else {

View file

@ -48,83 +48,77 @@ terms specified in this license.
* $Id$
*
*/
/* _XOPEN_SOURCE is needed for posix_memalign */
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <assert.h>
#include <stasis/bufferPool.h>
#include <assert.h>
#include <stasis/truncation.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. */
Page pool[MAX_BUFFER_SIZE+1];
stasis_buffer_pool_t* stasis_buffer_pool_init() {
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 ;
bufferSpace = calloc((MAX_BUFFER_SIZE + 2), PAGE_SIZE);
byte * bufferSpace = calloc((MAX_BUFFER_SIZE + 2), PAGE_SIZE);
assert(bufferSpace);
addressFromMalloc = bufferSpace;
ret->addr_to_free = bufferSpace;
bufferSpace = (byte*)(((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++) {
pool[i].rwlatch = initlock();
pool[i].loadlatch = initlock();
pool[i].memAddr = &(bufferSpace[i*PAGE_SIZE]);
pool[i].dirty = 0;
ret->pool[i].rwlatch = initlock();
ret->pool[i].loadlatch = initlock();
ret->pool[i].memAddr = &(bufferSpace[i*PAGE_SIZE]);
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++) {
deletelock(pool[i].rwlatch);
deletelock(pool[i].loadlatch);
deletelock(ret->pool[i].rwlatch);
deletelock(ret->pool[i].loadlatch);
}
free(addressFromMalloc); // breaks efence
pthread_mutex_destroy(&pageMallocMutex);
free(ret->addr_to_free); // breaks efence
pthread_mutex_destroy(&ret->mut);
}
Page* pageMalloc() {
Page* stasis_buffer_pool_malloc_page(stasis_buffer_pool_t * ret) {
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 */
assert(nextPage <= MAX_BUFFER_SIZE + 1);
assert(ret->nextPage <= MAX_BUFFER_SIZE + 1);
pthread_mutex_unlock(&pageMallocMutex);
pthread_mutex_unlock(&ret->mut);
return page;
}
static void pageFreeNoLock(Page *p, pageid_t id) {
void stasis_buffer_pool_free_page(stasis_buffer_pool_t * ret, Page *p, pageid_t id) {
writelock(p->rwlatch, 10);
p->id = id;
p->LSN = 0;
p->dirty = 0;
}
void pageFree(Page *p, pageid_t id) {
writelock(p->rwlatch, 10);
pageFreeNoLock(p,id);
writeunlock(p->rwlatch);
}

View file

@ -84,13 +84,6 @@ terms specified in this license.
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
* be pinned, and the pointer valid until releasePage is called.

View file

@ -40,6 +40,9 @@ permission to use and distribute the software in accordance with the
terms specified in this license.
---*/
#ifndef STASIS_BUFFER_POOL_H
#define STASIS_BUFFER_POOL_H
/**
* @file
*
@ -47,16 +50,19 @@ terms specified in this license.
* $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.
@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

View file

@ -134,6 +134,15 @@ typedef struct {
} recordid;
#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"
#endif /* __stasis_common_h */