Skip to content

Commit

Permalink
Extract a few constants in PageList.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed May 7, 2018
1 parent c715dec commit 3ed9e88
Showing 1 changed file with 17 additions and 11 deletions.
Expand Up @@ -66,6 +66,12 @@ class PageList
private static final int OFFSET_SWAPPER_ID = 29; // 2 bytes, plus the 5 high-bits from usage counter
@SuppressWarnings( "unused" )
private static final int OFFSET_USAGE_COUNTER = 31; // 1 byte, but only the 3 low bits.
private static final int MASK_USAGE_COUNT = 0x07;
private static final long MASK_NOT_USAGE_COUNT = 0xFFFFFFFF_FFFFFFF8L;
private static final int SHIFT_FILE_PAGE_ID = 24;
private static final int MASK_NOT_FILE_PAGE_ID = 0xFFFFFF;
private static final int SHIFT_SWAPPER_ID = 3;
private static final int MASK_SHIFTED_SWAPPER_ID = 0b1_11111_11111_11111_11111;

// todo we can alternatively also make use of the lower 12 bits of the address field, because
// todo the addresses are page aligned, and we can assume them to be at least 4096 bytes in size.
Expand Down Expand Up @@ -313,7 +319,7 @@ public void initBuffer( long pageRef )

private byte getUsageCounter( long pageRef )
{
return (byte) (UnsafeUtil.getLongVolatile( offFilePageId( pageRef ) ) & 0x07);
return (byte) (UnsafeUtil.getLongVolatile( offFilePageId( pageRef ) ) & MASK_USAGE_COUNT);
}

/**
Expand All @@ -324,7 +330,7 @@ public void incrementUsage( long pageRef )
// This is intentionally left benignly racy for performance.
long address = offFilePageId( pageRef );
long v = UnsafeUtil.getLongVolatile( address );
long usage = v & 0x07;
long usage = v & MASK_USAGE_COUNT;
if ( usage < 4 ) // avoid cache sloshing by not doing a write if counter is already maxed out
{
usage++;
Expand All @@ -333,7 +339,7 @@ public void incrementUsage( long pageRef )
// Those fields are updated under guard of the exclusive lock, but we *might* race with
// that here, and in that case we would never want a usage counter update to clobber a page
// binding update.
UnsafeUtil.compareAndSwapLong( null, address, v, (v & 0xFFFFFFFF_FFFFFFF8L) + usage );
UnsafeUtil.compareAndSwapLong( null, address, v, (v & MASK_NOT_USAGE_COUNT) + usage );
}
}

Expand All @@ -345,19 +351,19 @@ public boolean decrementUsage( long pageRef )
// This is intentionally left benignly racy for performance.
long address = offFilePageId( pageRef );
long v = UnsafeUtil.getLongVolatile( address );
long usage = v & 0x07;
long usage = v & MASK_USAGE_COUNT;
if ( usage > 0 )
{
usage--;
// See `incrementUsage` about why we use `compareAndSwapLong`.
UnsafeUtil.compareAndSwapLong( null, address, v, (v & 0xFFFFFFFF_FFFFFFF8L) + usage );
UnsafeUtil.compareAndSwapLong( null, address, v, (v & MASK_NOT_USAGE_COUNT) + usage );
}
return usage == 0;
}

public long getFilePageId( long pageRef )
{
long filePageId = UnsafeUtil.getLong( offFilePageId( pageRef ) ) >>> 24;
long filePageId = UnsafeUtil.getLong( offFilePageId( pageRef ) ) >>> SHIFT_FILE_PAGE_ID;
return filePageId == MAX_FILE_PAGE_ID ? PageCursor.UNBOUND_PAGE_ID : filePageId;
}

Expand All @@ -370,7 +376,7 @@ private void setFilePageId( long pageRef, long filePageId )
}
long address = offFilePageId( pageRef );
long v = UnsafeUtil.getLong( address );
filePageId = (filePageId << 24) + (v & 0xFFFFFF);
filePageId = (filePageId << SHIFT_FILE_PAGE_ID) + (v & MASK_NOT_FILE_PAGE_ID);
UnsafeUtil.putLong( address, filePageId );
}

Expand All @@ -394,15 +400,15 @@ void setLastModifiedTxId( long pageRef, long modifierTxId )

public int getSwapperId( long pageRef )
{
long v = UnsafeUtil.getLong( offFilePageId( pageRef ) ) >>> 3;
return (int) (v & 0b1_11111_11111_11111_11111); // 21 bits.
long v = UnsafeUtil.getLong( offFilePageId( pageRef ) ) >>> SHIFT_SWAPPER_ID;
return (int) (v & MASK_SHIFTED_SWAPPER_ID); // 21 bits.
}

private void setSwapperId( long pageRef, int swapperId )
{
swapperId = swapperId << 3;
swapperId = swapperId << SHIFT_SWAPPER_ID;
long address = offFilePageId( pageRef );
long v = UnsafeUtil.getLong( address ) & (~(0b1_11111_11111_11111_11111 << 3));
long v = UnsafeUtil.getLong( address ) & (~(MASK_SHIFTED_SWAPPER_ID << SHIFT_SWAPPER_ID));
UnsafeUtil.putLong( address, v + swapperId );
}

Expand Down

0 comments on commit 3ed9e88

Please sign in to comment.