You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directmemory.apache.org by bp...@apache.org on 2012/03/01 12:41:36 UTC
svn commit: r1295522 [2/2] - in /incubator/directmemory/trunk:
directmemory-cache/src/main/java/org/apache/directmemory/cache/
directmemory-cache/src/main/java/org/apache/directmemory/memory/
directmemory-cache/src/main/java/org/apache/directmemory/mem...
Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java?rev=1295522&r1=1295521&r2=1295522&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java Thu Mar 1 11:41:34 2012
@@ -23,10 +23,12 @@ import static org.junit.Assert.assertEqu
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import java.io.IOException;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
-import java.util.Date;
import java.util.List;
+import org.apache.directmemory.memory.allocator.ByteBufferAllocator;
import org.junit.Before;
import org.junit.Test;
@@ -41,52 +43,52 @@ public class RoundRobinAllocationPolicyT
private static final int NUMBER_OF_BUFFERS = 4;
- List<OffHeapMemoryBuffer<Object>> buffers;
+ List<ByteBufferAllocator> allocators;
- RoundRobinAllocationPolicy<Object> allocationPolicy;
+ RoundRobinAllocationPolicy allocationPolicy;
@Before
public void initAllocationPolicy()
{
- buffers = new ArrayList<OffHeapMemoryBuffer<Object>>();
+ allocators = new ArrayList<ByteBufferAllocator>();
for ( int i = 0; i < NUMBER_OF_BUFFERS; i++ )
{
- buffers.add( new DummyOffHeapMemoryBufferImpl() );
+ allocators.add( new DummyByteBufferAllocator() );
}
- allocationPolicy = new RoundRobinAllocationPolicy<Object>();
- allocationPolicy.init( buffers );
+ allocationPolicy = new RoundRobinAllocationPolicy();
+ allocationPolicy.init( allocators );
}
@Test
public void testSequence()
{
- assertEquals( buffers.get( 0 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 1 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 2 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 3 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 0 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 1 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 2 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 3 ), allocationPolicy.getActiveBuffer( null, 1 ) );
-
- assertNotNull( allocationPolicy.getActiveBuffer( null, 1 ) );
- assertNotNull( allocationPolicy.getActiveBuffer( null, 2 ) );
- assertNull( allocationPolicy.getActiveBuffer( null, 3 ) );
+ assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+
+ assertNotNull( allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertNotNull( allocationPolicy.getActiveAllocator( null, 2 ) );
+ assertNull( allocationPolicy.getActiveAllocator( null, 3 ) );
allocationPolicy.reset();
- assertEquals( buffers.get( 0 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 1 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 2 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 3 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 0 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 1 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 2 ), allocationPolicy.getActiveBuffer( null, 1 ) );
- assertEquals( buffers.get( 3 ), allocationPolicy.getActiveBuffer( null, 1 ) );
+ assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
}
@@ -97,9 +99,9 @@ public class RoundRobinAllocationPolicyT
allocationPolicy.setMaxAllocations( 1 );
- assertNotNull( allocationPolicy.getActiveBuffer( null, 1 ) );
- assertNull( allocationPolicy.getActiveBuffer( null, 2 ) );
- assertNull( allocationPolicy.getActiveBuffer( null, 3 ) );
+ assertNotNull( allocationPolicy.getActiveAllocator( null, 1 ) );
+ assertNull( allocationPolicy.getActiveAllocator( null, 2 ) );
+ assertNull( allocationPolicy.getActiveAllocator( null, 3 ) );
}
@@ -107,96 +109,44 @@ public class RoundRobinAllocationPolicyT
/**
* Dummy {@link OffHeapMemoryBuffer} that do nothing.
*/
- private static class DummyOffHeapMemoryBufferImpl
- implements OffHeapMemoryBuffer<Object>
+ private static class DummyByteBufferAllocator
+ implements ByteBufferAllocator
{
@Override
- public int used()
- {
- return 0;
- }
-
- @Override
- public int capacity()
- {
- return 0;
- }
-
- @Override
- public int getBufferNumber()
- {
- return 0;
+ public void free( ByteBuffer buffer )
+ {
}
@Override
- public Pointer<Object> store( byte[] payload )
+ public ByteBuffer allocate( int size )
{
return null;
}
@Override
- public Pointer<Object> store( byte[] payload, Date expires )
- {
- return null;
- }
-
- @Override
- public Pointer<Object> store( byte[] payload, long expiresIn )
- {
- return null;
- }
-
- @Override
- public byte[] retrieve( Pointer<Object> pointer )
- {
- return null;
- }
-
- @Override
- public int free( Pointer<Object> pointer2free )
- {
- return 0;
- }
-
- @Override
public void clear()
- {
+ {
}
@Override
- public void disposeExpiredRelative()
- {
- }
-
- @Override
- public void disposeExpiredAbsolute()
- {
- }
-
- @Override
- public long collectExpired()
+ public int getCapacity()
{
return 0;
}
@Override
- public long collectLFU( int limit )
+ public int getNumber()
{
return 0;
}
@Override
- public Pointer<Object> update( Pointer<Object> pointer, byte[] payload )
+ public void close()
+ throws IOException
{
- return null;
- }
-
- @Override
- public <V> Pointer<Object> allocate( Class<V> type, int size, long expiresIn, long expires )
- {
- // TODO Auto-generated method stub
- return null;
+
}
+
}
}
Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java?rev=1295522&r1=1295521&r2=1295522&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java Thu Mar 1 11:41:34 2012
@@ -70,6 +70,16 @@ public class Starter
logger.info( "************************************************" );
}
+ private static void dump( MemoryManagerService<Object> mms )
+ {
+ logger.info( "off-heap - allocated: " + Ram.inMb( mms.capacity() ) );
+ logger.info( "off-heap - used: " + Ram.inMb( mms.used() ) );
+ logger.info( "heap - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
+ logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
+ logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
+ logger.info( "************************************************" );
+ }
+
public void rawInsert( int megabytes, int howMany )
{
OffHeapMemoryBuffer<Object> mem = OffHeapMemoryBufferImpl.createNew( megabytes * 1024 * 1024 );
@@ -116,10 +126,7 @@ public class Starter
logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
logger.info( "---------------------------------" );
- for ( OffHeapMemoryBuffer<Object> buf : MemoryManager.getBuffers() )
- {
- dump( buf );
- }
+ dump( MemoryManager.getMemoryManager() );
}
Added: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java?rev=1295522&view=auto
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java (added)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java Thu Mar 1 11:41:34 2012
@@ -0,0 +1,111 @@
+package org.apache.directmemory.memory.allocator;
+
+import java.nio.ByteBuffer;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class FixedSizeByteBufferAllocatorImplTest
+{
+ @Test
+ public void allocationTest()
+ {
+
+ ByteBufferAllocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 5000, 256, 1 );
+
+ ByteBuffer bf1 = allocator.allocate( 250 );
+ Assert.assertEquals( 256, bf1.capacity() );
+ Assert.assertEquals( 250, bf1.limit() );
+
+ ByteBuffer bf2 = allocator.allocate( 251 );
+ Assert.assertEquals( 256, bf2.capacity() );
+ Assert.assertEquals( 251, bf2.limit() );
+
+ ByteBuffer bf3 = allocator.allocate( 200 );
+ Assert.assertEquals( 256, bf3.capacity() );
+ Assert.assertEquals( 200, bf3.limit() );
+
+ ByteBuffer bf4 = allocator.allocate( 2000 );
+ Assert.assertNull( bf4 );
+
+ ByteBuffer bf5 = allocator.allocate( 298 );
+ Assert.assertNull( bf5 );
+
+ ByteBuffer bf6 = allocator.allocate( 128 );
+ Assert.assertEquals( 256, bf6.capacity() );
+ Assert.assertEquals( 128, bf6.limit() );
+
+ }
+
+
+ @Test
+ public void releaseTest()
+ {
+
+ ByteBufferAllocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );
+
+ ByteBuffer bf1 = allocator.allocate( 250 );
+ Assert.assertEquals( 256, bf1.capacity() );
+ Assert.assertEquals( 250, bf1.limit() );
+
+ ByteBuffer bf2 = allocator.allocate( 251 );
+ Assert.assertEquals( 256, bf2.capacity() );
+ Assert.assertEquals( 251, bf2.limit() );
+
+ ByteBuffer bf3 = allocator.allocate( 252 );
+ Assert.assertEquals( 256, bf3.capacity() );
+ Assert.assertEquals( 252, bf3.limit() );
+
+ ByteBuffer bf4 = allocator.allocate( 500 );
+ Assert.assertNull( bf4 );
+
+ allocator.free( bf1 );
+ allocator.free( bf2 );
+
+ ByteBuffer bf5 = allocator.allocate( 500 );
+ Assert.assertNull( bf5 );
+
+ ByteBuffer bf6 = allocator.allocate( 249 );
+ Assert.assertEquals( 256, bf6.capacity() );
+ Assert.assertEquals( 249, bf6.limit() );
+
+ ByteBuffer bf7 = allocator.allocate( 248 );
+ Assert.assertEquals( 256, bf7.capacity() );
+ Assert.assertEquals( 248, bf7.limit() );
+
+ }
+
+ @Test
+ public void allocateAndFreeTest()
+ {
+
+ ByteBufferAllocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );
+
+ for (int i = 0; i < 1000; i++)
+ {
+ ByteBuffer bf1 = allocator.allocate( 250 );
+ Assert.assertEquals( 256, bf1.capacity() );
+ Assert.assertEquals( 250, bf1.limit() );
+
+ allocator.free( bf1 );
+ }
+
+
+ ByteBuffer bf2 = allocator.allocate( 1000 );
+ Assert.assertNull( bf2 );
+
+ for (int i = 0; i < 3; i++)
+ {
+ ByteBuffer bf3 = allocator.allocate( 250 );
+ Assert.assertEquals( 256, bf3.capacity() );
+ Assert.assertEquals( 250, bf3.limit() );
+
+ }
+
+ ByteBuffer bf4 = allocator.allocate( 238 );
+ Assert.assertNull( bf4 );
+
+ }
+
+}
Added: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java?rev=1295522&view=auto
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java (added)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java Thu Mar 1 11:41:34 2012
@@ -0,0 +1,169 @@
+package org.apache.directmemory.memory.allocator;
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class MergingByteBufferAllocatorImplTest
+{
+ @Test
+ public void allocationTest()
+ {
+
+ ByteBufferAllocator allocator = new MergingByteBufferAllocatorImpl( 0, 5000 );
+
+ ByteBuffer bf1 = allocator.allocate( 250 );
+ Assert.assertEquals( 250, bf1.capacity() );
+ Assert.assertEquals( 250, bf1.limit() );
+
+ ByteBuffer bf2 = allocator.allocate( 251 );
+ Assert.assertEquals( 251, bf2.capacity() );
+ Assert.assertEquals( 251, bf2.limit() );
+
+ ByteBuffer bf3 = allocator.allocate( 200 );
+ Assert.assertEquals( 200, bf3.capacity() );
+ Assert.assertEquals( 200, bf3.limit() );
+
+ ByteBuffer bf4 = allocator.allocate( 2000 );
+ Assert.assertEquals( 2000, bf4.capacity() );
+ Assert.assertEquals( 2000, bf4.limit() );
+
+ ByteBuffer bf5 = allocator.allocate( 2001 );
+ Assert.assertEquals( 2001, bf5.capacity() );
+ Assert.assertEquals( 2001, bf5.limit() );
+
+ ByteBuffer bf6 = allocator.allocate( 298 );
+ Assert.assertEquals( 298, bf6.capacity() );
+ Assert.assertEquals( 298, bf6.limit() );
+
+ ByteBuffer bf7 = allocator.allocate( 128 );
+ Assert.assertNull( bf7 );
+
+ }
+
+
+ @Test
+ public void releaseTest()
+ {
+
+ ByteBufferAllocator allocator = new MergingByteBufferAllocatorImpl( 0, 1000 );
+
+ ByteBuffer bf1 = allocator.allocate( 250 );
+ Assert.assertEquals( 250, bf1.capacity() );
+ Assert.assertEquals( 250, bf1.limit() );
+
+ ByteBuffer bf2 = allocator.allocate( 251 );
+ Assert.assertEquals( 251, bf2.capacity() );
+ Assert.assertEquals( 251, bf2.limit() );
+
+ ByteBuffer bf3 = allocator.allocate( 252 );
+ Assert.assertEquals( 252, bf3.capacity() );
+ Assert.assertEquals( 252, bf3.limit() );
+
+ ByteBuffer bf4 = allocator.allocate( 500 );
+ Assert.assertNull( bf4 );
+
+ allocator.free( bf1 );
+ allocator.free( bf2 );
+
+ ByteBuffer bf5 = allocator.allocate( 500 );
+ Assert.assertEquals( 501, bf5.capacity() );
+ Assert.assertEquals( 500, bf5.limit() );
+
+ }
+
+ @Test
+ public void allocateAndFreeTest()
+ {
+
+ ByteBufferAllocator allocator = new MergingByteBufferAllocatorImpl( 0, 1000 );
+
+ for (int i = 0; i < 1000; i++)
+ {
+ ByteBuffer bf1 = allocator.allocate( 250 );
+ Assert.assertEquals( 250, bf1.capacity() );
+ Assert.assertEquals( 250, bf1.limit() );
+
+ allocator.free( bf1 );
+ }
+
+
+ ByteBuffer bf2 = allocator.allocate( 1000 );
+ Assert.assertEquals( 1000, bf2.capacity() );
+ Assert.assertEquals( 1000, bf2.limit() );
+
+ }
+
+ @Test
+ public void allocationWithoutSplittingPointerTest()
+ {
+
+ ByteBufferAllocator allocator = new MergingByteBufferAllocatorImpl( 0, 200 );
+
+ ByteBuffer bf1 = allocator.allocate( 180 );
+ Assert.assertEquals( 200, bf1.capacity() );
+ Assert.assertEquals( 180, bf1.limit() );
+
+ ByteBuffer bf2 = allocator.allocate( 5 );
+ Assert.assertNull( bf2 );
+
+ allocator.free( bf1 );
+
+
+ ByteBuffer bf3 = allocator.allocate( 10 );
+ Assert.assertEquals( 10, bf3.capacity() );
+ Assert.assertEquals( 10, bf3.limit() );
+
+ ByteBuffer bf4 = allocator.allocate( 20 );
+ Assert.assertEquals( 20, bf4.capacity() );
+ Assert.assertEquals( 20, bf4.limit() );
+
+ ByteBuffer bf5 = allocator.allocate( 30 );
+ Assert.assertEquals( 30, bf5.capacity() );
+ Assert.assertEquals( 30, bf5.limit() );
+
+ allocator.free( bf4 );
+ allocator.free( bf3 );
+
+ ByteBuffer bf6 = allocator.allocate( 25 );
+ Assert.assertEquals( 30, bf6.capacity() );
+ Assert.assertEquals( 25, bf6.limit() );
+
+ }
+
+ @Test
+ public void allocationWithDifferentRatioTest()
+ {
+
+ MergingByteBufferAllocatorImpl allocator = new MergingByteBufferAllocatorImpl( 0, 200 );
+ allocator.setSizeRatioThreshold( 0.95 );
+
+ allocator.setSizeRatioThreshold( 10 );
+
+ ByteBuffer bf1 = allocator.allocate( 180 );
+ Assert.assertEquals( 180, bf1.capacity() );
+ Assert.assertEquals( 180, bf1.limit() );
+
+ ByteBuffer bf2 = allocator.allocate( 10 );
+ Assert.assertEquals( 20, bf2.capacity() );
+ Assert.assertEquals( 10, bf2.limit() );
+
+ }
+
+
+ @Test(expected = BufferOverflowException.class)
+ public void allocationThrowingBOExceptionTest()
+ {
+
+ MergingByteBufferAllocatorImpl allocator = new MergingByteBufferAllocatorImpl( 0, 200 );
+ allocator.setReturnNullWhenBufferIsFull( false );
+
+ allocator.allocate( 210 );
+ Assert.fail();
+ }
+
+
+}
Added: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java?rev=1295522&view=auto
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java (added)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java Thu Mar 1 11:41:34 2012
@@ -0,0 +1,57 @@
+package org.apache.directmemory.memory.allocator;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class SlabByteBufferAllocatorImplTest
+{
+ @Test
+ public void allocationTest()
+ {
+
+ List<FixedSizeByteBufferAllocatorImpl> slabs = new ArrayList<FixedSizeByteBufferAllocatorImpl>();
+ slabs.add( new FixedSizeByteBufferAllocatorImpl( 0, 1024, 128, 1 ) );
+ slabs.add( new FixedSizeByteBufferAllocatorImpl( 1, 1024, 256, 1 ) );
+ slabs.add( new FixedSizeByteBufferAllocatorImpl( 2, 1024, 512, 1 ) );
+ slabs.add( new FixedSizeByteBufferAllocatorImpl( 3, 1024, 1024, 1 ) );
+
+
+ ByteBufferAllocator allocator = new SlabByteBufferAllocatorImpl( 0, slabs, false );
+
+ ByteBuffer bf1 = allocator.allocate( 250 );
+ Assert.assertEquals( 256, bf1.capacity() );
+ Assert.assertEquals( 250, bf1.limit() );
+
+ ByteBuffer bf2 = allocator.allocate( 251 );
+ Assert.assertEquals( 256, bf2.capacity() );
+ Assert.assertEquals( 251, bf2.limit() );
+
+ ByteBuffer bf3 = allocator.allocate( 200 );
+ Assert.assertEquals( 256, bf3.capacity() );
+ Assert.assertEquals( 200, bf3.limit() );
+
+ ByteBuffer bf4 = allocator.allocate( 100 );
+ Assert.assertEquals( 128, bf4.capacity() );
+ Assert.assertEquals( 100, bf4.limit() );
+
+ ByteBuffer bf5 = allocator.allocate( 550 );
+ Assert.assertEquals( 1024, bf5.capacity() );
+ Assert.assertEquals( 550, bf5.limit() );
+
+ ByteBuffer bf6 = allocator.allocate( 800 );
+ Assert.assertNull( bf6 );
+
+ allocator.free( bf5 );
+
+ ByteBuffer bf7 = allocator.allocate( 800 );
+ Assert.assertEquals( 1024, bf7.capacity() );
+ Assert.assertEquals( 800, bf7.limit() );
+
+ }
+
+}
Modified: incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java?rev=1295522&r1=1295521&r2=1295522&view=diff
==============================================================================
--- incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java (original)
+++ incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java Thu Mar 1 11:41:34 2012
@@ -46,14 +46,14 @@ public class DirectMemoryCache<K, V>
public DirectMemoryCache( int numberOfBuffers, int size, int initialCapacity, int concurrencyLevel )
{
MemoryManagerService<V> memoryManager =
- new MemoryManagerServiceWithAllocationPolicyImpl<V>( new RoundRobinAllocationPolicy<V>(), false );
+ new MemoryManagerServiceWithAllocationPolicyImpl<V>( new RoundRobinAllocationPolicy(), false );
cacheService = new DirectMemory<K, V>().setMemoryManager( memoryManager )
.setNumberOfBuffers( numberOfBuffers )
.setSize( size )
.setInitialCapacity( initialCapacity )
.setConcurrencyLevel( concurrencyLevel )
- .newCacheService();;
+ .newCacheService();
}
public DirectMemoryCache( int numberOfBuffers, int size )
@@ -138,22 +138,12 @@ public class DirectMemoryCache<K, V>
public long sizeInBytes()
{
- long i = 0;
- for ( OffHeapMemoryBuffer<V> buffer : getMemoryManager().getBuffers() )
- {
- i += buffer.used();
- }
- return i;
+ return getMemoryManager().used();
}
public long capacityInBytes()
{
- long i = 0;
- for ( OffHeapMemoryBuffer<V> buffer : getMemoryManager().getBuffers() )
- {
- i += buffer.capacity();
- }
- return i;
+ return getMemoryManager().capacity();
}
public void dump( OffHeapMemoryBuffer<V> mem )
Modified: incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryStore.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryStore.java?rev=1295522&r1=1295521&r2=1295522&view=diff
==============================================================================
--- incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryStore.java (original)
+++ incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryStore.java Thu Mar 1 11:41:34 2012
@@ -34,7 +34,6 @@ import net.sf.ehcache.store.disk.StoreUp
import net.sf.ehcache.writer.CacheWriterManager;
import org.apache.directmemory.cache.CacheServiceImpl;
import org.apache.directmemory.measures.Ram;
-import org.apache.directmemory.memory.OffHeapMemoryBuffer;
import org.apache.directmemory.memory.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -98,7 +97,7 @@ public class DirectMemoryStore
logger.info( "default buffer size = " + DEFAULT_BUFFER_SIZE );
logger.info( "off heap size = " + offHeapSizeBytes );
int numberOfBuffers = (int) ( offHeapSizeBytes / DEFAULT_BUFFER_SIZE );
- numberOfBuffers = DEFAULT_NUMBER_BYTE_BUFFERS;
+// numberOfBuffers = DEFAULT_NUMBER_BYTE_BUFFERS;
logger.info( "no of buffers = " + numberOfBuffers );
this.bufferLocks = new ArrayList<ReentrantLock>( numberOfBuffers );
@@ -109,6 +108,7 @@ public class DirectMemoryStore
directMemoryCache =
new DirectMemoryCache<Object, Element>( numberOfBuffers, (int) ( offHeapSizeBytes / numberOfBuffers ) );
+
}
@Override
@@ -586,16 +586,10 @@ public class DirectMemoryStore
public void dumpTotal()
{
- long capacity = 0;
- long used = 0;
- for ( OffHeapMemoryBuffer<Element> buffer : directMemoryCache.getMemoryManager().getBuffers() )
- {
- capacity += buffer.capacity();
- used += buffer.used();
- }
+ long capacity = directMemoryCache.getMemoryManager().capacity();
+ long used = directMemoryCache.getMemoryManager().used();
+
logger.info( "***Totals***************************************" );
- logger.info(
- format( "off-heap - # buffers: \t%1d", directMemoryCache.getMemoryManager().getBuffers().size() ) );
logger.info( format( "off-heap - allocated: \t%1s", Ram.inMb( capacity ) ) );
logger.info( format( "off-heap - used: \t%1s", Ram.inMb( used ) ) );
logger.info( format( "heap - max: \t%1s", Ram.inMb( Runtime.getRuntime().maxMemory() ) ) );
Modified: incubator/directmemory/trunk/integrations/ehcache/src/test/java/org/apache/directmemory/ehcache/EHCacheTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/integrations/ehcache/src/test/java/org/apache/directmemory/ehcache/EHCacheTest.java?rev=1295522&r1=1295521&r2=1295522&view=diff
==============================================================================
--- incubator/directmemory/trunk/integrations/ehcache/src/test/java/org/apache/directmemory/ehcache/EHCacheTest.java (original)
+++ incubator/directmemory/trunk/integrations/ehcache/src/test/java/org/apache/directmemory/ehcache/EHCacheTest.java Thu Mar 1 11:41:34 2012
@@ -74,7 +74,7 @@ public class EHCacheTest
{
if ( ( i % 1000 ) == 0 )
{
- System.out.println( "heatbeat " + i );
+ System.out.println( "heatbeat 2 " + i );
stats( ehcache );
}
element = new Element( i, new byte[1024] );
Modified: incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java?rev=1295522&r1=1295521&r2=1295522&view=diff
==============================================================================
--- incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java (original)
+++ incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java Thu Mar 1 11:41:34 2012
@@ -43,7 +43,7 @@ public class CacheServiceExportingActiva
public void start( BundleContext context )
throws Exception
{
- AllocationPolicy<SimpleObject> allocationPolicy = new RoundRobinAllocationPolicy<SimpleObject>();
+ AllocationPolicy allocationPolicy = new RoundRobinAllocationPolicy();
MemoryManagerService<SimpleObject> memoryManager =
new MemoryManagerServiceWithAllocationPolicyImpl<SimpleObject>( allocationPolicy, true );
this.cacheService =