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.
|
|
|
|
---*/
|
|
|
|
|
|
|
|
/************************************************************************
|
2004-07-13 23:48:20 +00:00
|
|
|
* implementation of pages
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
STRUCTURE OF A PAGE
|
|
|
|
|
|
|
|
+-------------------------------------------+-----------------------+--+
|
|
|
|
| DATA SECTION +--------->| RID: (PAGE, 0) | |
|
|
|
|
| +-----------------+ | +-----------------------+ |
|
|
|
|
| +-->| RID: (PAGE, 1) | | |
|
|
|
|
| | +-----------------+ | |
|
|
|
|
| | | |
|
|
|
|
| +-----------------+ | +----------------------------+
|
|
|
|
| | | +--->| RID: (PAGE, n) |
|
|
|
|
| | | | +----------------------------+
|
|
|
|
|======================================================================|
|
|
|
|
|^ FREE SPACE | | | |
|
|
|
|
|+-----------------------|-------|---|--------------------+ |
|
|
|
|
| | | | | |
|
|
|
|
| +-------------|-------|---+ | |
|
|
|
|
| | | | | |
|
|
|
|
| +---|---+-----+---|---+---|---+--------------+-----|------+-----+
|
|
|
|
| | slotn | ... | slot1 | slot0 | num of slots | free space | LSN |
|
|
|
|
+------+-------+-----+-------+-------+--------------+------------+-----+
|
|
|
|
|
|
|
|
NOTE:
|
|
|
|
- slots are zero indexed.
|
|
|
|
- slots are of implemented as (offset, length)
|
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
Latching summary:
|
|
|
|
|
|
|
|
Each page has an associated read/write lock. This lock only
|
|
|
|
protects the internal layout of the page, and the members of the
|
|
|
|
page struct. Here is how it is held in various circumstances:
|
|
|
|
|
|
|
|
Record allocation: Write lock
|
|
|
|
Record read: Read lock
|
|
|
|
Read LSN Read lock
|
|
|
|
Record write *READ LOCK*
|
|
|
|
Write LSN Write lock
|
|
|
|
|
|
|
|
Any circumstance where these locks are held during an I/O operation
|
|
|
|
is a bug.
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
************************************************************************/
|
2004-07-27 22:04:59 +00:00
|
|
|
/* _XOPEN_SOURCE is needed for posix_memalign */
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#include <stdlib.h>
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
#include <lladd/common.h>
|
2004-07-20 00:15:17 +00:00
|
|
|
#include "latches.h"
|
2004-07-14 21:25:59 +00:00
|
|
|
#include "page.h"
|
2004-07-13 23:48:20 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <lladd/constants.h>
|
2004-07-21 02:13:28 +00:00
|
|
|
#include <assert.h>
|
2004-07-26 22:01:09 +00:00
|
|
|
#include "blobManager.h"
|
|
|
|
#include "pageFile.h"
|
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
/* TODO: Combine with buffer size... */
|
|
|
|
static int nextPage = 0;
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
static const byte *slotMemAddr(const byte *memAddr, int slotNum) ;
|
|
|
|
|
|
|
|
/** @todo: Why does only one of the get/set First/Second HalfOfWord take an unsigned int? */
|
|
|
|
static int getFirstHalfOfWord(unsigned int *memAddr);
|
|
|
|
static int getSecondHalfOfWord(int *memAddr);
|
|
|
|
static void setFirstHalfOfWord(int *memAddr, int value);
|
|
|
|
static void setSecondHalfOfWord(int *memAddr, int value);
|
|
|
|
|
|
|
|
static int readFreeSpace(byte *memAddr);
|
|
|
|
static void writeFreeSpace(byte *memAddr, int newOffset);
|
|
|
|
static int readNumSlots(byte *memAddr);
|
|
|
|
static void writeNumSlots(byte *memAddr, int numSlots);
|
|
|
|
|
|
|
|
static int getSlotOffset(byte *memAddr, int slot) ;
|
|
|
|
static int getSlotLength(byte *memAddr, int slot) ;
|
|
|
|
static void setSlotOffset(byte *memAddr, int slot, int offset) ;
|
|
|
|
static void setSlotLength(byte *memAddr, int slot, int length) ;
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Invariant: This lock should be held while updating lastFreepage, or
|
|
|
|
while performing any operation that may decrease the amount of
|
|
|
|
freespace in the page that lastFreepage refers to.
|
|
|
|
|
|
|
|
Since pageCompact and pageDeRalloc may only increase this value,
|
|
|
|
they do not need to hold this lock. Since bufferManager is the
|
|
|
|
only place where pageRalloc is called, pageRalloc does not obtain
|
|
|
|
this lock.
|
|
|
|
*/
|
|
|
|
static pthread_mutex_t lastFreepage_mutex;
|
|
|
|
static unsigned int lastFreepage = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
/** @todo replace static ints in page.c with #defines. */
|
|
|
|
|
|
|
|
/* ------ */
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
static int SLOT_OFFSET_SIZE;
|
|
|
|
static int SLOT_LENGTH_SIZE;
|
|
|
|
static int SLOT_SIZE;
|
|
|
|
|
|
|
|
static int LSN_SIZE;
|
|
|
|
static int FREE_SPACE_SIZE;
|
|
|
|
static int NUMSLOTS_SIZE;
|
|
|
|
|
|
|
|
static int START_OF_LSN;
|
|
|
|
static int START_OF_FREE_SPACE;
|
|
|
|
static int START_OF_NUMSLOTS;
|
|
|
|
|
|
|
|
static int MASK_0000FFFF;
|
|
|
|
static int MASK_FFFF0000;
|
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
/* ------ */
|
|
|
|
|
|
|
|
static pthread_mutex_t pageAllocMutex;
|
2004-07-20 00:15:17 +00:00
|
|
|
/** We need one dummy page for locking purposes, so this array has one extra page in it. */
|
|
|
|
Page pool[MAX_BUFFER_SIZE+1];
|
2004-07-13 23:48:20 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
/* ------------------ STATIC FUNCTIONS. NONE OF THESE ACQUIRE LOCKS
|
|
|
|
ON THE MEMORY THAT IS PASSED INTO THEM -------------*/
|
|
|
|
|
2004-07-21 02:13:28 +00:00
|
|
|
static int isValidSlot(byte *memAddr, int slot);
|
|
|
|
static void invalidateSlot(byte *memAddr, int slot);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
/**
|
|
|
|
The caller of this function must already have a writelock on the
|
|
|
|
page.
|
|
|
|
*/
|
2004-07-14 20:49:18 +00:00
|
|
|
static void pageCompact(Page * page);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
static int getFirstHalfOfWord(unsigned int *memAddr) {
|
|
|
|
unsigned int word = *memAddr;
|
|
|
|
word = (word >> (2*BITS_PER_BYTE)); /* & MASK_0000FFFF; */
|
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int getSecondHalfOfWord(int *memAddr) {
|
|
|
|
int word = *memAddr;
|
|
|
|
word = word & MASK_0000FFFF;
|
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
static void setFirstHalfOfWord(int *memAddr, int value){
|
2004-06-24 21:10:31 +00:00
|
|
|
int word = *memAddr;
|
|
|
|
word = word & MASK_0000FFFF;
|
|
|
|
word = word | (value << (2*BITS_PER_BYTE));
|
|
|
|
*memAddr = word;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
static void setSecondHalfOfWord(int *memAddr, int value) {
|
2004-06-24 21:10:31 +00:00
|
|
|
int word = *memAddr;;
|
|
|
|
word = word & MASK_FFFF0000;
|
|
|
|
word = word | (value & MASK_0000FFFF);
|
|
|
|
*memAddr = word;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* slotMemAddr() calculates the memory address of the given slot. It does this
|
|
|
|
* by going to the end of the page, then walking backwards, past the LSN field
|
|
|
|
* (LSN_SIZE), past the 'free space' and 'num of slots' fields (NUMSLOTS_SIZE),
|
|
|
|
* and then past a slotNum slots (slotNum * SLOT_SIZE).
|
|
|
|
*/
|
|
|
|
static const byte *slotMemAddr(const byte *memAddr, int slotNum) {
|
|
|
|
return (memAddr + PAGE_SIZE) - (LSN_SIZE + FREE_SPACE_SIZE + NUMSLOTS_SIZE + ((slotNum+1) * SLOT_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pageWriteLSN() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as a parameter a Page. The Page struct contains the new LSN and the page
|
|
|
|
* number to which the new LSN must be written to.
|
2004-07-13 23:48:20 +00:00
|
|
|
*
|
|
|
|
* @param page You must have a writelock on page before calling this function.
|
2004-06-24 21:10:31 +00:00
|
|
|
*/
|
2004-07-14 20:49:18 +00:00
|
|
|
static void pageWriteLSN(Page * page) {
|
2004-07-21 02:13:28 +00:00
|
|
|
/* unlocked since we're only called by a function that holds the writelock. */
|
2004-07-14 20:49:18 +00:00
|
|
|
*(long *)(page->memAddr + START_OF_LSN) = page->LSN;
|
2004-07-21 02:13:28 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
static int unlocked_freespace(Page * page);
|
2004-07-13 23:48:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Just like freespace(), but doesn't obtain a lock. (So that other methods in this file can use it.)
|
|
|
|
*/
|
2004-07-14 20:49:18 +00:00
|
|
|
static int unlocked_freespace(Page * page) {
|
2004-07-13 23:48:20 +00:00
|
|
|
int space;
|
2004-07-14 20:49:18 +00:00
|
|
|
space= (slotMemAddr(page->memAddr, readNumSlots(page->memAddr)) - (page->memAddr + readFreeSpace(page->memAddr)));
|
2004-07-13 23:48:20 +00:00
|
|
|
return (space < 0) ? 0 : space;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* readFreeSpace() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as a parameter the memory address of the loaded page in memory and returns
|
|
|
|
* the offset at which the free space section of this page begins.
|
|
|
|
*/
|
|
|
|
static int readFreeSpace(byte *memAddr) {
|
|
|
|
return getSecondHalfOfWord((int*)(memAddr + START_OF_NUMSLOTS));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* writeFreeSpace() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as parameters the memory address of the loaded page in memory and a new offset
|
|
|
|
* in the page that will denote the point at which free space begins.
|
|
|
|
*/
|
|
|
|
static void writeFreeSpace(byte *memAddr, int newOffset) {
|
|
|
|
setSecondHalfOfWord((int*)(memAddr + START_OF_NUMSLOTS), newOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* readNumSlots() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as a parameter the memory address of the loaded page in memory, and returns
|
|
|
|
* the memory address at which the free space section of this page begins.
|
|
|
|
*/
|
|
|
|
static int readNumSlots(byte *memAddr) {
|
|
|
|
return getFirstHalfOfWord((unsigned int*)(memAddr + START_OF_NUMSLOTS));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* writeNumSlots() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as parameters the memory address of the loaded page in memory and an int
|
|
|
|
* to which the value of the numSlots field in the page will be set to.
|
|
|
|
*/
|
|
|
|
static void writeNumSlots(byte *memAddr, int numSlots) {
|
|
|
|
setFirstHalfOfWord((int*)(unsigned int*)(memAddr + START_OF_NUMSLOTS), numSlots);
|
|
|
|
}
|
|
|
|
|
2004-07-21 02:13:28 +00:00
|
|
|
static int isValidSlot(byte *memAddr, int slot) {
|
2004-07-13 23:48:20 +00:00
|
|
|
return (getSlotOffset(memAddr, slot) != INVALID_SLOT) ? 1 : 0;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-07-21 02:13:28 +00:00
|
|
|
static void invalidateSlot(byte *memAddr, int slot) {
|
|
|
|
setSlotOffset(memAddr, slot, INVALID_SLOT);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
Move all of the records to the beginning of the page in order to
|
|
|
|
increase the available free space.
|
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
@todo If we were supporting multithreaded operation, this routine
|
2004-06-24 21:10:31 +00:00
|
|
|
would need to pin the pages that it works on.
|
|
|
|
*/
|
2004-07-14 20:49:18 +00:00
|
|
|
static void pageCompact(Page * page) {
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
byte buffer[PAGE_SIZE];
|
|
|
|
int freeSpace = 0;
|
2004-07-13 23:48:20 +00:00
|
|
|
int numSlots;
|
|
|
|
int meta_size;
|
2004-06-24 21:10:31 +00:00
|
|
|
int slot_length;
|
2004-07-13 23:48:20 +00:00
|
|
|
int last_used_slot = -1;
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
numSlots = readNumSlots(page->memAddr);
|
2004-07-13 23:48:20 +00:00
|
|
|
|
|
|
|
/* DEBUG("Compact: numSlots=%d\n", numSlots); */
|
|
|
|
meta_size = LSN_SIZE + FREE_SPACE_SIZE + NUMSLOTS_SIZE + (SLOT_SIZE*numSlots);
|
2004-06-28 22:48:02 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
/* Can't compact in place, slot numbers can come in different orders than
|
|
|
|
the physical space allocated to them. */
|
2004-07-14 20:49:18 +00:00
|
|
|
memcpy(buffer + PAGE_SIZE - meta_size, page->memAddr + PAGE_SIZE - meta_size, meta_size);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < numSlots; i++) {
|
2004-07-13 23:48:20 +00:00
|
|
|
/* DEBUG("i = %d\n", i); */
|
2004-07-14 20:49:18 +00:00
|
|
|
if (isValidSlot(page->memAddr, i)) {
|
2004-07-13 23:48:20 +00:00
|
|
|
/* DEBUG("Buffer offset: %d\n", freeSpace); */
|
2004-07-14 20:49:18 +00:00
|
|
|
slot_length = getSlotLength(page->memAddr, i);
|
|
|
|
memcpy(buffer + freeSpace, page->memAddr + getSlotOffset(page->memAddr, i), slot_length);
|
2004-06-24 21:10:31 +00:00
|
|
|
setSlotOffset(buffer, i, freeSpace);
|
|
|
|
freeSpace += slot_length;
|
|
|
|
last_used_slot = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
|
|
|
|
/* if (last_used_slot < numSlots) { */
|
|
|
|
writeNumSlots(buffer, last_used_slot + 1);
|
|
|
|
/*} */
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
/* DEBUG("freeSpace = %d, num slots = %d\n", freeSpace, last_used_slot + 1); */
|
|
|
|
|
|
|
|
writeFreeSpace(buffer, freeSpace);
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
memcpy(page->memAddr, buffer, PAGE_SIZE);
|
2004-07-13 23:48:20 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getSlotOffset() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as parameters the memory address of the page loaded in memory, and a slot
|
|
|
|
* number. It returns the offset corresponding to that slot.
|
|
|
|
*/
|
|
|
|
static int getSlotOffset(byte *memAddr, int slot) {
|
|
|
|
return getFirstHalfOfWord((unsigned int*)slotMemAddr(memAddr, slot));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getSlotLength() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as parameters the memory address of the page loaded in memory, and a slot
|
|
|
|
* number. It returns the length corresponding to that slot.
|
|
|
|
*/
|
|
|
|
static int getSlotLength(byte *memAddr, int slot) {
|
|
|
|
return getSecondHalfOfWord((int*)(unsigned int*)slotMemAddr(memAddr, slot));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setSlotOffset() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as parameters the memory address of the page loaded in memory, a slot number,
|
|
|
|
* and an offset. It sets the offset of the given slot to the offset passed in
|
|
|
|
* as a parameter.
|
|
|
|
*/
|
|
|
|
static void setSlotOffset(byte *memAddr, int slot, int offset) {
|
|
|
|
setFirstHalfOfWord((int*)slotMemAddr(memAddr, slot), offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setSlotLength() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as parameters the memory address of the page loaded in memory, a slot number,
|
|
|
|
* and a length. It sets the length of the given slot to the length passed in
|
|
|
|
* as a parameter.
|
|
|
|
*/
|
|
|
|
static void setSlotLength(byte *memAddr, int slot, int length) {
|
|
|
|
setSecondHalfOfWord((int*)(unsigned int*)slotMemAddr(memAddr, slot), length);
|
|
|
|
}
|
2004-07-23 20:21:44 +00:00
|
|
|
|
|
|
|
static void pageReallocNoLock(Page *p, int id) {
|
|
|
|
p->id = id;
|
|
|
|
p->LSN = 0;
|
|
|
|
p->dirty = 0;
|
|
|
|
/* assert(p->pending == 0);
|
|
|
|
assert(p->waiting == 1);
|
|
|
|
p->waiting = 0;*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----- end static functions ----- */
|
|
|
|
|
|
|
|
/* ----- (de)initialization functions. Do not need to support multithreading. -----*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pageInit() initializes all the important variables needed in
|
|
|
|
* all the functions dealing with pages.
|
|
|
|
*/
|
|
|
|
void pageInit() {
|
|
|
|
|
|
|
|
nextPage = 0;
|
|
|
|
/**
|
|
|
|
* For now, we will assume that slots are 4 bytes long, and that the
|
|
|
|
* first two bytes are the offset, and the second two bytes are the
|
|
|
|
* the length. There are some functions at the bottom of this file
|
|
|
|
* that may be useful later if we decide to dynamically choose
|
|
|
|
* sizes for offset and length.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the largest a slot length can be is the size of the page,
|
|
|
|
* and the greatest offset at which a record could possibly
|
|
|
|
* start is at the end of the page
|
|
|
|
*/
|
|
|
|
SLOT_LENGTH_SIZE = SLOT_OFFSET_SIZE = 2; /* in bytes */
|
|
|
|
SLOT_SIZE = SLOT_OFFSET_SIZE + SLOT_LENGTH_SIZE;
|
|
|
|
|
|
|
|
LSN_SIZE = sizeof(long);
|
|
|
|
FREE_SPACE_SIZE = NUMSLOTS_SIZE = 2;
|
|
|
|
|
|
|
|
/* START_OF_LSN is the offset in the page to the lsn */
|
|
|
|
START_OF_LSN = PAGE_SIZE - LSN_SIZE;
|
|
|
|
START_OF_FREE_SPACE = START_OF_LSN - FREE_SPACE_SIZE;
|
|
|
|
START_OF_NUMSLOTS = START_OF_FREE_SPACE - NUMSLOTS_SIZE;
|
|
|
|
|
|
|
|
MASK_0000FFFF = (1 << (2*BITS_PER_BYTE)) - 1;
|
|
|
|
MASK_FFFF0000 = ~MASK_0000FFFF;
|
|
|
|
|
|
|
|
|
|
|
|
pthread_mutex_init(&pageAllocMutex, NULL);
|
2004-07-27 01:04:35 +00:00
|
|
|
for(int i = 0; i < MAX_BUFFER_SIZE+1; i++) {
|
|
|
|
pool[i].rwlatch = initlock();
|
|
|
|
pool[i].loadlatch = initlock();
|
2004-07-27 22:04:59 +00:00
|
|
|
assert(!posix_memalign((void*)(&(pool[i].memAddr)), PAGE_SIZE, PAGE_SIZE));
|
2004-07-27 01:04:35 +00:00
|
|
|
}
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_init(&lastFreepage_mutex , NULL);
|
|
|
|
lastFreepage = 0;
|
|
|
|
|
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pageDeInit() {
|
|
|
|
for(int i = 0; i < MAX_BUFFER_SIZE+1; i++) {
|
|
|
|
|
|
|
|
deletelock(pool[i].rwlatch);
|
|
|
|
deletelock(pool[i].loadlatch);
|
|
|
|
free(pool[i].memAddr);
|
|
|
|
}
|
2004-07-23 20:21:44 +00:00
|
|
|
}
|
|
|
|
|
2004-07-26 22:01:09 +00:00
|
|
|
typedef struct {
|
|
|
|
int page;
|
|
|
|
int slot;
|
|
|
|
/** If pageptr is not null, then it is used by the iterator methods.
|
|
|
|
Otherwise, they re-load the pages and obtain short latches for
|
|
|
|
each call. */
|
|
|
|
Page * pageptr;
|
|
|
|
} page_iterator_t;
|
|
|
|
|
|
|
|
void pageIteratorInit(recordid rid, page_iterator_t * pit, Page * p) {
|
|
|
|
pit->page = rid.page;
|
|
|
|
pit->slot = rid.slot;
|
|
|
|
pit->pageptr = p;
|
|
|
|
assert((!p) || (p->id == pit->page));
|
|
|
|
}
|
|
|
|
|
|
|
|
int nextSlot(page_iterator_t * pit, recordid * rid) {
|
|
|
|
Page * p;
|
|
|
|
int numSlots;
|
|
|
|
int done = 0;
|
|
|
|
int ret;
|
|
|
|
if(pit->pageptr) {
|
|
|
|
p = pit->pageptr;
|
|
|
|
} else {
|
|
|
|
p = loadPage(pit->page);
|
|
|
|
}
|
|
|
|
|
|
|
|
numSlots = readNumSlots(p->memAddr);
|
|
|
|
while(pit->slot < numSlots && !done) {
|
|
|
|
|
|
|
|
if(isValidSlot(p->memAddr, pit->slot)) {
|
|
|
|
done = 1;
|
|
|
|
} else {
|
|
|
|
pit->slot ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if(!done) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
rid->page = pit->page;
|
|
|
|
rid->slot = pit->slot;
|
|
|
|
rid->size = getSlotLength(p->memAddr, rid->slot);
|
|
|
|
if(rid->size >= PAGE_SIZE) {
|
|
|
|
|
|
|
|
if(rid->size == BLOB_SLOT) {
|
|
|
|
blob_record_t br;
|
|
|
|
pageReadRecord(-1, p, *rid, (byte*)&br);
|
|
|
|
rid->size = br.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!pit->pageptr) {
|
|
|
|
releasePage(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
void pageCommit(int xid) {
|
|
|
|
/* rmTouch(xid); */
|
|
|
|
}
|
|
|
|
|
|
|
|
void pageAbort(int xid) {
|
|
|
|
/* rmTouch(xid); */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pageReadLSN() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as a parameter a Page and returns the LSN that is currently written on that
|
|
|
|
* page in memory.
|
|
|
|
*/
|
|
|
|
lsn_t pageReadLSN(const Page * page) {
|
|
|
|
lsn_t ret;
|
|
|
|
|
|
|
|
readlock(page->rwlatch, 259);
|
|
|
|
ret = *(long *)(page->memAddr + START_OF_LSN);
|
|
|
|
readunlock(page->rwlatch);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* freeSpace() assumes that the page is already loaded in memory. It takes
|
|
|
|
* as a parameter a Page, and returns an estimate of the amount of free space
|
|
|
|
* available to a new slot on this page. (This is the amount of unused space
|
|
|
|
* in the page, minus the size of a new slot entry.) This is either exact,
|
|
|
|
* or an underestimate.
|
|
|
|
*
|
|
|
|
* @todo is it ever safe to call freespace without a lock on the page?
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int freespace(Page * page) {
|
|
|
|
int ret;
|
|
|
|
readlock(page->rwlatch, 292);
|
|
|
|
ret = unlocked_freespace(page);
|
|
|
|
readunlock(page->rwlatch);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
/** @todo ralloc ignores it's xid parameter; change the interface? */
|
|
|
|
recordid ralloc(int xid, long size) {
|
|
|
|
|
|
|
|
recordid ret;
|
|
|
|
Page * p;
|
|
|
|
|
|
|
|
/* DEBUG("Rallocing record of size %ld\n", (long int)size); */
|
|
|
|
|
|
|
|
assert(size < BLOB_THRESHOLD_SIZE || size == BLOB_SLOT);
|
|
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lastFreepage_mutex);
|
|
|
|
while(freespace(p = loadPage(lastFreepage)) < size ) {
|
|
|
|
releasePage(p);
|
|
|
|
lastFreepage++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pageRalloc(p, size);
|
|
|
|
|
|
|
|
releasePage(p);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&lastFreepage_mutex);
|
|
|
|
|
|
|
|
/* DEBUG("alloced rid = {%d, %d, %ld}\n", ret.page, ret.slot, ret.size); */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
recordid pageRalloc(Page * page, int size) {
|
|
|
|
int freeSpace;
|
|
|
|
int numSlots;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
writelock(page->rwlatch, 342);
|
|
|
|
if(unlocked_freespace(page) < size) {
|
|
|
|
|
|
|
|
pageCompact(page);
|
|
|
|
|
|
|
|
/* Make sure there's enough free space... */
|
|
|
|
|
|
|
|
/*#ifdef DEBUGGING*/
|
|
|
|
assert (unlocked_freespace(page) >= (int)size); /*Expensive, so skip it when debugging is off. */
|
|
|
|
/*#endif */
|
|
|
|
|
|
|
|
}
|
|
|
|
freeSpace = readFreeSpace(page->memAddr);
|
|
|
|
numSlots = readNumSlots(page->memAddr);
|
|
|
|
recordid rid;
|
|
|
|
|
|
|
|
|
|
|
|
rid.page = page->id;
|
|
|
|
rid.slot = numSlots;
|
|
|
|
rid.size = size;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Reuse an old (invalid) slot entry. Why was this here?
|
|
|
|
|
|
|
|
@todo is slot reuse in page.c a performance bottleneck?
|
|
|
|
|
|
|
|
*/
|
|
|
|
for (i = 0; i < numSlots; i++) {
|
|
|
|
if (!isValidSlot(page->memAddr, i)) {
|
|
|
|
rid.slot = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rid.slot == numSlots) {
|
|
|
|
writeNumSlots(page->memAddr, numSlots+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
setSlotOffset(page->memAddr, rid.slot, freeSpace);
|
|
|
|
setSlotLength(page->memAddr, rid.slot, rid.size);
|
|
|
|
writeFreeSpace(page->memAddr, freeSpace + rid.size);
|
|
|
|
|
|
|
|
writeunlock(page->rwlatch);
|
|
|
|
|
|
|
|
/* DEBUG("slot: %d freespace: %d\n", rid.slot, freeSpace); */
|
|
|
|
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Only used for recovery, to make sure that consistent RID's are created
|
|
|
|
* on log playback. */
|
|
|
|
recordid pageSlotRalloc(Page * page, lsn_t lsn, recordid rid) {
|
|
|
|
int freeSpace;
|
|
|
|
int numSlots;
|
|
|
|
|
|
|
|
writelock(page->rwlatch, 376);
|
|
|
|
|
|
|
|
freeSpace = readFreeSpace(page->memAddr);
|
|
|
|
numSlots= readNumSlots(page->memAddr);
|
|
|
|
|
|
|
|
/* printf("!"); fflush(NULL); */
|
|
|
|
|
|
|
|
/* if(rid.size > BLOB_THRESHOLD_SIZE) {
|
|
|
|
return blobSlotAlloc(page, lsn_t lsn, recordid rid);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
/* assert(rid.slot >= numSlots); */
|
|
|
|
|
|
|
|
/** @todo for recovery, pageSlotRalloc assumes no other thread added a slot
|
|
|
|
between when ralloc and it were called. (This may be a
|
|
|
|
safe assumption..) */
|
|
|
|
|
|
|
|
if(getSlotLength(page->memAddr, rid.slot) == 0) {
|
|
|
|
|
|
|
|
/* if(rid.slot >= numSlots) { */
|
|
|
|
|
|
|
|
if (unlocked_freespace(page) < rid.size) { /*freeSpace < rid.size) { */
|
|
|
|
pageCompact(page);
|
|
|
|
freeSpace = readFreeSpace(page->memAddr);
|
|
|
|
assert (freeSpace < rid.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
setSlotOffset(page->memAddr, rid.slot, freeSpace);
|
|
|
|
setSlotLength(page->memAddr, rid.slot, rid.size);
|
|
|
|
writeFreeSpace(page->memAddr, freeSpace + rid.size);
|
|
|
|
/* printf("?"); fflush(NULL);*/
|
|
|
|
} else {
|
|
|
|
assert((rid.size == getSlotLength(page->memAddr, rid.slot)) ||
|
|
|
|
(getSlotLength(page->memAddr, rid.slot) >= PAGE_SIZE)); /* Fails. Why? */
|
|
|
|
}
|
|
|
|
writeunlock(page->rwlatch);
|
|
|
|
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void pageDeRalloc(Page * page, recordid rid) {
|
|
|
|
|
|
|
|
readlock(page->rwlatch, 443);
|
|
|
|
invalidateSlot(page->memAddr, rid.slot);
|
|
|
|
unlock(page->rwlatch);
|
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-28 22:48:02 +00:00
|
|
|
/*
|
2004-07-23 20:21:44 +00:00
|
|
|
This should trust the rid (since the caller needs to
|
2004-07-04 00:46:49 +00:00
|
|
|
override the size in special circumstances)
|
|
|
|
|
|
|
|
@todo If the rid size has been overridden, we should check to make
|
|
|
|
sure that this really is a special record.
|
2004-06-24 21:10:31 +00:00
|
|
|
*/
|
2004-07-14 20:49:18 +00:00
|
|
|
void pageReadRecord(int xid, Page * page, recordid rid, byte *buff) {
|
2004-07-13 23:48:20 +00:00
|
|
|
byte *recAddress;
|
2004-07-23 20:21:44 +00:00
|
|
|
int slot_length;
|
2004-07-14 20:49:18 +00:00
|
|
|
readlock(page->rwlatch, 519);
|
2004-07-20 00:15:17 +00:00
|
|
|
|
|
|
|
assert(page->id == rid.page);
|
2004-07-14 20:49:18 +00:00
|
|
|
recAddress = page->memAddr + getSlotOffset(page->memAddr, rid.slot);
|
2004-07-23 20:21:44 +00:00
|
|
|
|
|
|
|
slot_length = getSlotLength(page->memAddr, rid.slot);
|
|
|
|
|
|
|
|
assert((rid.size == slot_length) || (slot_length >= PAGE_SIZE));
|
2004-07-26 22:01:09 +00:00
|
|
|
|
2004-06-28 22:48:02 +00:00
|
|
|
memcpy(buff, recAddress, rid.size);
|
2004-07-23 20:21:44 +00:00
|
|
|
unlock(page->rwlatch);
|
2004-07-13 23:48:20 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
void pageWriteRecord(int xid, Page * page, recordid rid, lsn_t lsn, const byte *data) {
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-28 22:48:02 +00:00
|
|
|
byte *rec;
|
2004-07-23 20:21:44 +00:00
|
|
|
int len;
|
|
|
|
readlock(page->rwlatch, 529);
|
2004-06-28 22:48:02 +00:00
|
|
|
assert(rid.size < PAGE_SIZE);
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
rec = page->memAddr + getSlotOffset(page->memAddr, rid.slot);
|
2004-07-23 20:21:44 +00:00
|
|
|
len = getSlotLength(page->memAddr, rid.slot);
|
|
|
|
assert(rid.size == len || len >= PAGE_SIZE);
|
2004-06-28 22:48:02 +00:00
|
|
|
if(memcpy(rec, data, rid.size) == NULL ) {
|
|
|
|
printf("ERROR: MEM_WRITE_ERROR on %s line %d", __FILE__, __LINE__);
|
|
|
|
exit(MEM_WRITE_ERROR);
|
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
page->LSN = lsn;
|
2004-07-09 22:05:33 +00:00
|
|
|
pageWriteLSN(page);
|
2004-07-23 20:21:44 +00:00
|
|
|
unlock(page->rwlatch);
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-06-28 22:48:02 +00:00
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
void pageRealloc(Page *p, int id) {
|
|
|
|
writelock(p->rwlatch, 10);
|
|
|
|
pageReallocNoLock(p,id);
|
2004-07-13 23:48:20 +00:00
|
|
|
writeunlock(p->rwlatch);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Allocate a new page.
|
|
|
|
@param id The id of the new page.
|
|
|
|
@return A pointer to the new page. This memory is part of a pool,
|
2004-07-13 23:48:20 +00:00
|
|
|
and should never be freed manually.
|
2004-06-24 21:10:31 +00:00
|
|
|
*/
|
|
|
|
Page *pageAlloc(int id) {
|
2004-07-14 20:49:18 +00:00
|
|
|
Page *page;
|
2004-06-28 22:48:02 +00:00
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
pthread_mutex_lock(&pageAllocMutex);
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
page = &(pool[nextPage]);
|
2004-07-13 23:48:20 +00:00
|
|
|
|
|
|
|
/* We have an implicit lock on rwlatch, since we allocated it, but
|
|
|
|
haven't returned yet. */
|
2004-07-27 01:04:35 +00:00
|
|
|
/* page->rwlatch = initlock();
|
2004-07-20 00:15:17 +00:00
|
|
|
page->loadlatch = initlock();
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-27 01:04:35 +00:00
|
|
|
page->memAddr = malloc(PAGE_SIZE); */
|
2004-07-20 00:15:17 +00:00
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
nextPage++;
|
2004-07-20 00:15:17 +00:00
|
|
|
assert(nextPage <= MAX_BUFFER_SIZE + 1); /* There's a dummy page that we need to keep around, thus the +1 */
|
2004-07-13 23:48:20 +00:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&pageAllocMutex);
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
return page;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void printPage(byte *memAddr) {
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < PAGE_SIZE; i++) {
|
|
|
|
if((*(char *)(memAddr+i)) == 0) {
|
|
|
|
printf("#");
|
|
|
|
}else {
|
|
|
|
printf("%c", *(char *)(memAddr+i));
|
|
|
|
}
|
|
|
|
if((i+1)%4 == 0)
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define num 20
|
|
|
|
int pageTest() {
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
Page * page = malloc(sizeof(Page));
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
recordid rid[num];
|
|
|
|
char *str[num] = {"one",
|
|
|
|
"two",
|
|
|
|
"three",
|
|
|
|
"four",
|
|
|
|
"five",
|
|
|
|
"six",
|
|
|
|
"seven",
|
|
|
|
"eight",
|
|
|
|
"nine",
|
|
|
|
"ten",
|
|
|
|
"eleven",
|
|
|
|
"twelve",
|
|
|
|
"thirteen",
|
|
|
|
"fourteen",
|
|
|
|
"fifteen",
|
|
|
|
"sixteen",
|
|
|
|
"seventeen",
|
|
|
|
"eighteen",
|
|
|
|
"nineteen",
|
|
|
|
"twenty"};
|
|
|
|
int i;
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
page->memAddr = (byte *)malloc(PAGE_SIZE);
|
|
|
|
memset(page->memAddr, 0, PAGE_SIZE);
|
2004-06-24 21:10:31 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
rid[i] = pageRalloc(page, strlen(str[i]) + 1);
|
2004-07-09 22:05:33 +00:00
|
|
|
pageWriteRecord(0, page, rid[i], 1, (byte*)str[i]);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-07-14 20:49:18 +00:00
|
|
|
printPage(page->memAddr);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num; i+= 2)
|
|
|
|
pageDeRalloc(page, rid[i]);
|
|
|
|
|
|
|
|
pageCompact(page);
|
|
|
|
printf("\n\n\n");
|
2004-07-14 20:49:18 +00:00
|
|
|
printPage(page->memAddr);
|
2004-06-24 21:10:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-07-21 02:13:28 +00:00
|
|
|
/** @todo: Should the caller need to obtain the writelock when calling pageSetSlotType? */
|
2004-07-14 20:49:18 +00:00
|
|
|
void pageSetSlotType(Page * p, int slot, int type) {
|
2004-06-25 18:56:43 +00:00
|
|
|
assert(type > PAGE_SIZE);
|
2004-07-21 02:13:28 +00:00
|
|
|
writelock(p->rwlatch, 686);
|
2004-07-14 20:49:18 +00:00
|
|
|
setSlotLength(p->memAddr, slot, type);
|
2004-07-21 02:13:28 +00:00
|
|
|
unlock(p->rwlatch);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
int pageGetSlotType(Page * p, int slot, int type) {
|
2004-07-21 02:13:28 +00:00
|
|
|
int ret;
|
|
|
|
readlock(p->rwlatch, 693);
|
|
|
|
ret = getSlotLength(p->memAddr, slot);
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
|
2004-07-13 23:48:20 +00:00
|
|
|
/* getSlotType does the locking for us. */
|
2004-06-25 18:56:43 +00:00
|
|
|
return ret > PAGE_SIZE ? ret : NORMAL_SLOT;
|
|
|
|
}
|