Skip to content

Commit

Permalink
Update page cache tests to not rely on specifying page size
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Dec 11, 2017
1 parent c9f62ab commit 578fbbb
Show file tree
Hide file tree
Showing 27 changed files with 264 additions and 332 deletions.
Expand Up @@ -35,11 +35,10 @@
import org.neo4j.io.pagecache.tracing.cursor.PageCursorTracerSupplier;

import static java.lang.String.format;

import static org.neo4j.graphdb.config.Configuration.EMPTY;
import static org.neo4j.index.internal.gbptree.ConsistencyChecker.assertOnTreeNode;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.pointer;
import static org.neo4j.io.ByteUnit.kibiBytes;
import static org.neo4j.io.pagecache.PageCache.PAGE_SIZE;
import static org.neo4j.io.pagecache.tracing.PageCacheTracer.NULL;

/**
Expand Down Expand Up @@ -75,7 +74,8 @@ public static void printHeader( FileSystemAbstraction fs, File file, PrintStream
{
SingleFilePageSwapperFactory swapper = new SingleFilePageSwapperFactory();
swapper.open( fs, EMPTY );
try ( PageCache pageCache = new MuninnPageCache( swapper, 100, (int) kibiBytes( 8 ), NULL, PageCursorTracerSupplier.NULL ) )
PageCursorTracerSupplier cursorTracerSupplier = PageCursorTracerSupplier.NULL;
try ( PageCache pageCache = new MuninnPageCache( swapper, 100, PAGE_SIZE, NULL, cursorTracerSupplier ) )
{
printHeader( pageCache, file, out );
}
Expand Down
Expand Up @@ -21,21 +21,21 @@

import org.junit.Test;

import static org.junit.Assert.fail;
import org.neo4j.io.pagecache.PageCache;

import static org.junit.Assert.fail;
import static org.neo4j.index.internal.gbptree.GenerationSafePointer.MIN_GENERATION;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.MAX_GENERATION_OFFSET_MASK;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.read;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.write;
import static org.neo4j.io.ByteUnit.kibiBytes;

public class GenerationSafePointerPairAdditionalTest
{
@Test
public void shouldFailFastOnTooLargeGenerationOffset() throws Exception
{
// GIVEN
int pageSize = (int) kibiBytes( 8 );
int pageSize = PageCache.PAGE_SIZE;
PageAwareByteArrayCursor cursor = new PageAwareByteArrayCursor( pageSize );
cursor.next( 0 );
long firstGeneration = MIN_GENERATION;
Expand Down
Expand Up @@ -37,6 +37,11 @@
*/
public interface PageCache extends AutoCloseable
{
/**
* The default {@link #pageSize()}.
*/
int PAGE_SIZE = 8192;

/**
* Ask for a handle to a paged file, backed by this page cache.
* <p>
Expand Down
Expand Up @@ -21,8 +21,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.stream.Stream;

import org.neo4j.graphdb.config.Configuration;
import org.neo4j.io.fs.FileSystemAbstraction;
Expand Down Expand Up @@ -57,17 +55,6 @@ public interface PageSwapperFactory
*/
String implementationName();

/**
* Get the most optimal cache page size (in bytes) for these PageSwapper implementations.
*/
int getCachePageSizeHint();

/**
* Gives <code>true</code> if the {@link #getCachePageSizeHint()} is the only cache page size that is supported for
* these PageSwapper implementations, otherwise <code>false</code>.
*/
boolean isCachePageSizeHintStrict();

/**
* Get the unit of alignment that the swappers require of the memory buffers. For instance, if page alignment is
* required for doing direct IO, then {@link org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil#pageSize()} can be
Expand Down
Expand Up @@ -89,22 +89,9 @@ public String implementationName()
return "single";
}

@Override
public int getCachePageSizeHint()
{
return 8192;
}

@Override
public boolean isCachePageSizeHintStrict()
{
return false;
}

@Override
public long getRequiredBufferAlignment()
{
return 1;
}

}
Expand Up @@ -194,11 +194,25 @@ public class MuninnPageCache implements PageCache
* Create page cache
* @param swapperFactory page cache swapper factory
* @param maxPages maximum number of pages
* @param cachePageSize page cache size
* @param pageCacheTracer global page cache tracer
* @param pageCursorTracerSupplier supplier of thread local (transaction local) page cursor tracer that will provide
* thread local page cache statistics
*/
public MuninnPageCache(
PageSwapperFactory swapperFactory,
int maxPages,
PageCacheTracer pageCacheTracer,
PageCursorTracerSupplier pageCursorTracerSupplier )
{
this( swapperFactory, maxPages, PAGE_SIZE, pageCacheTracer, pageCursorTracerSupplier );
}

/**
* Constructor variant that allows setting a non-standard cache page size.
* Only ever use this for testing.
*/
@SuppressWarnings( "DeprecatedIsStillUsed" )
@Deprecated
public MuninnPageCache(
PageSwapperFactory swapperFactory,
int maxPages,
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.neo4j.io.pagecache.impl.SingleFilePageSwapperFactory;
import org.neo4j.io.pagecache.tracing.PageCacheTracer;
import org.neo4j.io.pagecache.tracing.cursor.DefaultPageCursorTracerSupplier;
import org.neo4j.io.pagecache.tracing.cursor.PageCursorTracerSupplier;

/*
* This class is an helper to allow to construct properly a page cache in the few places we need it without all
Expand All @@ -42,19 +41,14 @@ private StandalonePageCacheFactory()
}

public static PageCache createPageCache( FileSystemAbstraction fileSystem )
{
return createPageCache( fileSystem, PageCacheTracer.NULL, DefaultPageCursorTracerSupplier.INSTANCE );
}

public static PageCache createPageCache( FileSystemAbstraction fileSystem,
PageCacheTracer tracer, PageCursorTracerSupplier cursorTracerSupplier )
{
SingleFilePageSwapperFactory factory = new SingleFilePageSwapperFactory();
factory.open( fileSystem, Configuration.EMPTY );

int cachePageSize = factory.getCachePageSizeHint();
long pageCacheMemory = ByteUnit.mebiBytes( 8 );
long pageCount = pageCacheMemory / cachePageSize;
return new MuninnPageCache( factory, (int) pageCount, cachePageSize, tracer, cursorTracerSupplier );
int pageCount = (int) (pageCacheMemory / PageCache.PAGE_SIZE);
PageCacheTracer cacheTracer = PageCacheTracer.NULL;
DefaultPageCursorTracerSupplier cursorTracerSupplier = DefaultPageCursorTracerSupplier.INSTANCE;
return new MuninnPageCache( factory, pageCount, cacheTracer, cursorTracerSupplier );
}
}
Expand Up @@ -21,6 +21,7 @@

import org.junit.Test;

import org.neo4j.io.pagecache.PageCache;
import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil;

import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand All @@ -39,23 +40,23 @@ protected MemoryAllocator createAllocator( long expectedMaxMemory, long alignmen
@Test
public void allocatedPointerMustNotBeNull() throws Exception
{
MemoryAllocator mman = createAllocator( 16 * 4096, 8 );
long address = mman.allocateAligned( 8192 );
MemoryAllocator mman = createAllocator( 8 * PageCache.PAGE_SIZE, 8 );
long address = mman.allocateAligned( PageCache.PAGE_SIZE );
assertThat( address, is( not( 0L ) ) );
}

@Test
public void allocatedPointerMustBePageAligned() throws Exception
{
MemoryAllocator mman = createAllocator( 16 * 4096, UnsafeUtil.pageSize() );
long address = mman.allocateAligned( 8192 );
MemoryAllocator mman = createAllocator( 8 * PageCache.PAGE_SIZE, UnsafeUtil.pageSize() );
long address = mman.allocateAligned( PageCache.PAGE_SIZE );
assertThat( address % UnsafeUtil.pageSize(), is( 0L ) );
}

@Test
public void mustBeAbleToAllocatePastMemoryLimit() throws Exception
{
MemoryAllocator mman = createAllocator( 8192, 2 );
MemoryAllocator mman = createAllocator( PageCache.PAGE_SIZE, 2 );
for ( int i = 0; i < 4100; i++ )
{
assertThat( mman.allocateAligned( 1 ) % 2, is( 0L ) );
Expand All @@ -66,7 +67,7 @@ public void mustBeAbleToAllocatePastMemoryLimit() throws Exception
@Test( expected = IllegalArgumentException.class )
public void alignmentCannotBeZero() throws Exception
{
createAllocator( 8192, 0 );
createAllocator( PageCache.PAGE_SIZE, 0 );
}

@Test
Expand All @@ -84,19 +85,19 @@ public void mustBeAbleToAllocateSlabsLargerThanGrabSize() throws Exception
@Test
public void allocatingMustIncreaseMemoryUsedAndDecreaseAvailableMemory() throws Exception
{
MemoryAllocator mman = createAllocator( 8192, 1 );
MemoryAllocator mman = createAllocator( PageCache.PAGE_SIZE, 1 );
assertThat( mman.usedMemory(), is( 0L ) );
assertThat( mman.availableMemory(), is( 8192L ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( 8192L ) );
assertThat( mman.availableMemory(), is( (long) PageCache.PAGE_SIZE ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( (long) PageCache.PAGE_SIZE ) );

mman.allocateAligned( 32 );
assertThat( mman.usedMemory(), is( greaterThanOrEqualTo( 32L ) ) );
assertThat( mman.availableMemory(), is( lessThanOrEqualTo( 8192L - 32 ) ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( 8192L ) );
assertThat( mman.availableMemory(), is( lessThanOrEqualTo( PageCache.PAGE_SIZE - 32L ) ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( (long) PageCache.PAGE_SIZE ) );

mman.allocateAligned( 32 );
assertThat( mman.usedMemory(), is( greaterThanOrEqualTo( 64L ) ) );
assertThat( mman.availableMemory(), is( lessThanOrEqualTo( 8192L - 32 - 32 ) ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( 8192L ) );
assertThat( mman.availableMemory(), is( lessThanOrEqualTo( PageCache.PAGE_SIZE - 32 - 32L ) ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( (long) PageCache.PAGE_SIZE ) );
}
}

0 comments on commit 578fbbb

Please sign in to comment.