2004-06-24 21:10:31 +00:00
|
|
|
/*---
|
|
|
|
This software is copyrighted by the Regents of the University of
|
|
|
|
California, and other parties. The following terms apply to all files
|
|
|
|
associated with the software unless explicitly disclaimed in
|
|
|
|
individual files.
|
|
|
|
|
|
|
|
The authors hereby grant permission to use, copy, modify, distribute,
|
|
|
|
and license this software and its documentation for any purpose,
|
|
|
|
provided that existing copyright notices are retained in all copies
|
|
|
|
and that this notice is included verbatim in any distributions. No
|
|
|
|
written agreement, license, or royalty fee is required for any of the
|
|
|
|
authorized uses. Modifications to this software may be copyrighted by
|
|
|
|
their authors and need not follow the licensing terms described here,
|
|
|
|
provided that the new terms are clearly indicated on the first page of
|
|
|
|
each file where they apply.
|
|
|
|
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
|
|
|
|
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
|
|
|
|
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
|
|
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
|
|
NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND
|
|
|
|
THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
|
|
|
|
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
|
|
|
|
GOVERNMENT USE: If you are acquiring this software on behalf of the
|
|
|
|
U.S. government, the Government shall have only "Restricted Rights" in
|
|
|
|
the software and related documentation as defined in the Federal
|
|
|
|
Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you are
|
|
|
|
acquiring the software on behalf of the Department of Defense, the
|
|
|
|
software shall be classified as "Commercial Computer Software" and the
|
|
|
|
Government shall have only "Restricted Rights" as defined in Clause
|
|
|
|
252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
|
|
|
|
authors grant the U.S. Government and others acting in its behalf
|
|
|
|
permission to use and distribute the software in accordance with the
|
|
|
|
terms specified in this license.
|
|
|
|
---*/
|
|
|
|
/*******************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* implementation of the page buffer
|
|
|
|
* *************************************************/
|
2004-07-06 01:22:18 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <lladd/common.h>
|
2004-07-20 00:15:17 +00:00
|
|
|
#include <latches.h>
|
2004-06-24 21:10:31 +00:00
|
|
|
#include <assert.h>
|
2004-07-23 20:21:44 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
#include <lladd/bufferManager.h>
|
2004-07-30 01:28:39 +00:00
|
|
|
|
|
|
|
#include "page.h"
|
2004-06-25 18:56:43 +00:00
|
|
|
#include "blobManager.h"
|
2004-06-30 01:09:57 +00:00
|
|
|
#include <lladd/pageCache.h>
|
2004-07-15 00:42:36 +00:00
|
|
|
#include "pageFile.h"
|
2004-07-30 01:28:39 +00:00
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
#include <pbl/pbl.h>
|
2004-07-14 20:49:18 +00:00
|
|
|
|
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
static pblHashTable_t *activePages; /* page lookup */
|
2004-10-19 04:45:42 +00:00
|
|
|
/*static Page * activePagePtrs[MAX_BUFFER_SIZE];*/
|
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
|
|
|
|
static pthread_mutex_t loadPagePtr_mutex;
|
|
|
|
|
|
|
|
static Page * dummy_page;
|
2004-06-30 01:09:57 +00:00
|
|
|
|
|
|
|
int bufInit() {
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
pageInit();
|
2004-06-30 01:09:57 +00:00
|
|
|
openPageFile();
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
|
|
|
|
pthread_mutex_init(&loadPagePtr_mutex, NULL);
|
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
activePages = pblHtCreate();
|
2004-07-27 01:04:35 +00:00
|
|
|
|
2004-08-17 01:46:17 +00:00
|
|
|
dummy_page = pageMalloc();
|
2004-07-27 01:04:35 +00:00
|
|
|
pageRealloc(dummy_page, -1);
|
|
|
|
Page *first;
|
2004-08-17 01:46:17 +00:00
|
|
|
first = pageMalloc();
|
2004-07-27 01:04:35 +00:00
|
|
|
pageRealloc(first, 0);
|
2004-10-19 04:45:42 +00:00
|
|
|
pblHtInsert(activePages, &first->id, sizeof(int), first);
|
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
openBlobStore();
|
|
|
|
|
|
|
|
pageCacheInit(first);
|
|
|
|
|
|
|
|
assert(activePages);
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
void bufDeinit() {
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
closeBlobStore();
|
2004-07-27 01:04:35 +00:00
|
|
|
|
|
|
|
Page *p;
|
|
|
|
DEBUG("pageCacheDeinit()");
|
2004-10-19 04:45:42 +00:00
|
|
|
|
|
|
|
for( p = (Page*)pblHtFirst( activePages ); p; p = (Page*)pblHtNext(activePages)) {
|
2004-07-27 01:04:35 +00:00
|
|
|
|
2005-02-02 02:12:40 +00:00
|
|
|
pblHtRemove( activePages, 0, 0 );
|
2004-07-27 01:04:35 +00:00
|
|
|
DEBUG("+");
|
|
|
|
pageWrite(p);
|
2004-10-19 04:45:42 +00:00
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_destroy(&loadPagePtr_mutex);
|
|
|
|
|
|
|
|
pblHtDelete(activePages);
|
2004-06-30 01:09:57 +00:00
|
|
|
pageCacheDeinit();
|
|
|
|
closePageFile();
|
2004-07-27 01:04:35 +00:00
|
|
|
|
|
|
|
pageDeInit();
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
return;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-06-30 01:09:57 +00:00
|
|
|
/**
|
|
|
|
Just close file descriptors, don't do any other clean up. (For
|
|
|
|
testing.)
|
2004-06-28 22:48:02 +00:00
|
|
|
*/
|
2004-06-30 01:09:57 +00:00
|
|
|
void simulateBufferManagerCrash() {
|
|
|
|
closeBlobStore();
|
|
|
|
closePageFile();
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
void releasePage (Page * p) {
|
|
|
|
unlock(p->loadlatch);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
int bufTransCommit(int xid, lsn_t lsn) {
|
|
|
|
|
|
|
|
commitBlobs(xid);
|
|
|
|
pageCommit(xid);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
return 0;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
int bufTransAbort(int xid, lsn_t lsn) {
|
2004-06-30 01:09:57 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
abortBlobs(xid); /* abortBlobs doesn't write any log entries, so it doesn't need the lsn. */
|
|
|
|
pageAbort(xid);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
return 0;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-07-27 01:04:35 +00:00
|
|
|
|
|
|
|
Page * getPage(int pageid, int locktype) {
|
|
|
|
Page * ret;
|
|
|
|
int spin = 0;
|
|
|
|
pthread_mutex_lock(&loadPagePtr_mutex);
|
|
|
|
ret = pblHtLookup(activePages, &pageid, sizeof(int));
|
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
if(locktype == RW) {
|
|
|
|
writelock(ret->loadlatch, 217);
|
|
|
|
} else {
|
|
|
|
readlock(ret->loadlatch, 217);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ret && (ret->id != pageid)) {
|
|
|
|
unlock(ret->loadlatch);
|
|
|
|
pthread_mutex_unlock(&loadPagePtr_mutex);
|
|
|
|
sched_yield();
|
|
|
|
pthread_mutex_lock(&loadPagePtr_mutex);
|
|
|
|
ret = pblHtLookup(activePages, &pageid, sizeof(int));
|
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
if(locktype == RW) {
|
|
|
|
writelock(ret->loadlatch, 217);
|
|
|
|
} else {
|
|
|
|
readlock(ret->loadlatch, 217);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin++;
|
|
|
|
if(spin > 10000) {
|
|
|
|
printf("GetPage is stuck!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
cacheHitOnPage(ret);
|
|
|
|
assert(ret->id == pageid);
|
|
|
|
pthread_mutex_unlock(&loadPagePtr_mutex);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* If ret is null, then we know that:
|
|
|
|
|
|
|
|
a) there is no cache entry for pageid
|
|
|
|
b) this is the only thread that has gotten this far,
|
|
|
|
and that will try to add an entry for pageid
|
|
|
|
c) the most recent version of this page has been
|
|
|
|
written to the OS's file cache. */
|
|
|
|
int oldid = -1;
|
|
|
|
|
|
|
|
if( cache_state == FULL ) {
|
|
|
|
|
|
|
|
/* Select an item from cache, and remove it atomicly. (So it's
|
|
|
|
only reclaimed once) */
|
|
|
|
|
|
|
|
ret = cacheStalePage();
|
|
|
|
cacheRemovePage(ret);
|
|
|
|
|
|
|
|
oldid = ret->id;
|
|
|
|
|
|
|
|
assert(oldid != pageid);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2004-08-17 01:46:17 +00:00
|
|
|
ret = pageMalloc();
|
2004-07-27 01:04:35 +00:00
|
|
|
ret->id = -1;
|
|
|
|
ret->inCache = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
writelock(ret->loadlatch, 217);
|
|
|
|
|
|
|
|
/* Inserting this into the cache before releasing the mutex
|
|
|
|
ensures that constraint (b) above holds. */
|
|
|
|
pblHtInsert(activePages, &pageid, sizeof(int), ret);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&loadPagePtr_mutex);
|
|
|
|
|
|
|
|
/* Could writelock(ret) go here? */
|
|
|
|
|
|
|
|
assert(ret != dummy_page);
|
|
|
|
if(ret->id != -1) {
|
|
|
|
pageWrite(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
pageRealloc(ret, pageid);
|
|
|
|
|
|
|
|
pageRead(ret);
|
|
|
|
|
|
|
|
writeunlock(ret->loadlatch);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&loadPagePtr_mutex);
|
|
|
|
|
|
|
|
pblHtRemove(activePages, &(oldid), sizeof(int));
|
|
|
|
|
2004-10-18 18:24:54 +00:00
|
|
|
/* @todo Put off putting this back into cache until we're done with
|
2004-07-27 01:04:35 +00:00
|
|
|
it. -- This could cause the cache to empty out if the ratio of
|
|
|
|
threads to buffer slots is above ~ 1/3, but it decreases the
|
|
|
|
liklihood of thrashing. */
|
|
|
|
cacheInsertPage(ret);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&loadPagePtr_mutex);
|
|
|
|
|
|
|
|
if(locktype == RW) {
|
|
|
|
writelock(ret->loadlatch, 217);
|
|
|
|
} else {
|
|
|
|
readlock(ret->loadlatch, 217);
|
|
|
|
}
|
|
|
|
if(ret->id != pageid) {
|
|
|
|
unlock(ret->loadlatch);
|
|
|
|
printf("pageCache.c: Thrashing detected. Strongly consider increasing LLADD's buffer pool size!\n");
|
|
|
|
fflush(NULL);
|
|
|
|
return getPage(pageid, locktype);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Page *loadPage(int pageid) {
|
2004-07-30 01:28:39 +00:00
|
|
|
Page * ret = getPage(pageid, RO);
|
2004-07-27 01:04:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|