Skip to content

Commit

Permalink
MemoryAllocators can now say how much memory they have available
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Dec 11, 2017
1 parent a68a942 commit da2136f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
14 changes: 13 additions & 1 deletion community/io/src/main/java/org/neo4j/io/mem/GrabAllocator.java
Expand Up @@ -60,7 +60,7 @@ public final class GrabAllocator implements MemoryAllocator
} }


@Override @Override
public synchronized long sumUsedMemory() public synchronized long usedMemory()
{ {
long sum = 0; long sum = 0;
Grab grab = grabs; Grab grab = grabs;
Expand All @@ -72,6 +72,18 @@ public synchronized long sumUsedMemory()
return sum; return sum;
} }


@Override
public synchronized long availableMemory()
{
Grab grab = grabs;
long availableInCurrentGrab = 0;
if ( grab != null )
{
availableInCurrentGrab = grab.limit - grab.nextAlignedPointer;
}
return Math.max( memoryReserve, 0L ) + availableInCurrentGrab;
}

@Override @Override
public synchronized long allocateAligned( long bytes ) public synchronized long allocateAligned( long bytes )
{ {
Expand Down
Expand Up @@ -32,7 +32,12 @@ static MemoryAllocator createAllocator( long expectedMaxMemory, long alignment )
/** /**
* @return The sum, in bytes, of all the memory currently allocating through this allocator. * @return The sum, in bytes, of all the memory currently allocating through this allocator.
*/ */
long sumUsedMemory(); long usedMemory();

/**
* @return The amount of available memory, in bytes.
*/
long availableMemory();


/** /**
* Allocate a contiguous, aligned region of memory of the given size in bytes. * Allocate a contiguous, aligned region of memory of the given size in bytes.
Expand Down
Expand Up @@ -23,7 +23,9 @@


import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil; import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil;


import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;


Expand Down Expand Up @@ -78,4 +80,23 @@ public void mustBeAbleToAllocateSlabsLargerThanGrabSize() throws Exception
assertThat( largeBlock, is( not( 0L ) ) ); assertThat( largeBlock, is( not( 0L ) ) );
assertThat( page2, is( not( 0L ) ) ); assertThat( page2, is( not( 0L ) ) );
} }

@Test
public void allocatingMustIncreaseMemoryUsedAndDecreaseAvailableMemory() throws Exception
{
MemoryAllocator mman = createAllocator( 8192, 1 );
assertThat( mman.usedMemory(), is( 0L ) );
assertThat( mman.availableMemory(), is( 8192L ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( 8192L ) );

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

mman.allocateAligned( 32 );
assertThat( mman.usedMemory(), is( greaterThanOrEqualTo( 64L ) ) );
assertThat( mman.availableMemory(), is( lessThanOrEqualTo( 8192L - 32 - 32 ) ) );
assertThat( mman.usedMemory() + mman.availableMemory(), is( 8192L ) );
}
} }
Expand Up @@ -1206,9 +1206,9 @@ public void addressesMustBeZeroBeforeInitialisation() throws Exception
@Test @Test
public void initialisingBufferMustConsumeMemoryFromMemoryManager() throws Exception public void initialisingBufferMustConsumeMemoryFromMemoryManager() throws Exception
{ {
long initialUsedMemory = mman.sumUsedMemory(); long initialUsedMemory = mman.usedMemory();
pageList.initBuffer( pageRef ); pageList.initBuffer( pageRef );
long resultingUsedMemory = mman.sumUsedMemory(); long resultingUsedMemory = mman.usedMemory();
int allocatedMemory = (int) (resultingUsedMemory - initialUsedMemory); int allocatedMemory = (int) (resultingUsedMemory - initialUsedMemory);
assertThat( allocatedMemory, greaterThanOrEqualTo( pageSize ) ); assertThat( allocatedMemory, greaterThanOrEqualTo( pageSize ) );
assertThat( allocatedMemory, lessThanOrEqualTo( pageSize + ALIGNMENT ) ); assertThat( allocatedMemory, lessThanOrEqualTo( pageSize + ALIGNMENT ) );
Expand Down

0 comments on commit da2136f

Please sign in to comment.