geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject [01/11] incubator-geode git commit: initial FreeListManager test
Date Wed, 27 Jan 2016 00:20:00 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-831 [created] 3dd04f2f4


initial FreeListManager test


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/be9373ee
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/be9373ee
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/be9373ee

Branch: refs/heads/feature/GEODE-831
Commit: be9373ee81df5430a8f294bd1fbd0ca5064f24c1
Parents: b370a70
Author: Darrel Schneider <dschneider@pivotal.io>
Authored: Fri Jan 22 11:42:35 2016 -0800
Committer: Darrel Schneider <dschneider@pivotal.io>
Committed: Fri Jan 22 11:42:35 2016 -0800

----------------------------------------------------------------------
 .../internal/offheap/FreeListManager.java       | 33 ++++----
 .../offheap/SimpleMemoryAllocatorImpl.java      | 12 ++-
 .../internal/offheap/FreeListManagerTest.java   | 82 ++++++++++++++++++++
 3 files changed, 109 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/be9373ee/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
index 10e4148..4aa667f 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
@@ -56,6 +56,7 @@ public class FreeListManager {
   }
   private void getLiveChunks(UnsafeMemoryChunk slab, List<Chunk> result) {
     long addr = slab.getMemoryAddress();
+    ChunkFactory cf = this.ma.getChunkFactory();
     while (addr <= (slab.getMemoryAddress() + slab.getSize() - Chunk.MIN_CHUNK_SIZE))
{
       Fragment f = isAddrInFragmentFreeSpace(addr);
       if (f != null) {
@@ -64,7 +65,7 @@ public class FreeListManager {
         int curChunkSize = Chunk.getSize(addr);
         int refCount = Chunk.getRefCount(addr);
         if (refCount > 0) {
-          result.add(this.ma.chunkFactory.newChunk(addr));
+          result.add(cf.newChunk(addr));
         }
         addr += curChunkSize;
       }
@@ -172,11 +173,12 @@ public class FreeListManager {
       result = basicAllocate(size, true, chunkType);
       result.setDataSize(size);
     }
-    this.ma.stats.incObjects(1);
+    OffHeapMemoryStats stats = this.ma.getStats();
+    stats.incObjects(1);
     int resultSize = result.getSize();
     this.allocatedSize.addAndGet(resultSize);
-    this.ma.stats.incUsedMemory(resultSize);
-    this.ma.stats.incFreeMemory(-resultSize);
+    stats.incUsedMemory(resultSize);
+    stats.incFreeMemory(-resultSize);
     result.initializeUseCount();
     this.ma.notifyListeners();
 
@@ -220,14 +222,15 @@ public class FreeListManager {
     try {
       throw failure;
     } finally {
-      this.ma.ooohml.outOfOffHeapMemory(failure);
+      this.ma.getOutOfOffHeapMemoryListener().outOfOffHeapMemory(failure);
     }
   }
 
   private void logOffHeapState(int chunkSize) {
     if (InternalDistributedSystem.getAnyInstance() != null) {
       LogWriter lw = InternalDistributedSystem.getAnyInstance().getLogWriter();
-      lw.info("OutOfOffHeapMemory allocating size of " + chunkSize + ". allocated=" + this.allocatedSize.get()
+ " compactions=" + this.compactCount.get() + " objects=" + this.ma.stats.getObjects() + "
free=" + this.ma.stats.getFreeMemory() + " fragments=" + this.ma.stats.getFragments() + "
largestFragment=" + this.ma.stats.getLargestFragment() + " fragmentation=" + this.ma.stats.getFragmentation());
+      OffHeapMemoryStats stats = this.ma.getStats();
+      lw.info("OutOfOffHeapMemory allocating size of " + chunkSize + ". allocated=" + this.allocatedSize.get()
+ " compactions=" + this.compactCount.get() + " objects=" + stats.getObjects() + " free="
+ stats.getFreeMemory() + " fragments=" + stats.getFragments() + " largestFragment=" + stats.getLargestFragment()
+ " fragmentation=" + stats.getFragmentation());
       logFragmentState(lw);
       logTinyState(lw);
       logHugeState(lw);
@@ -523,7 +526,8 @@ public class FreeListManager {
         if (fragment.allocate(oldOffset, newOffset)) {
           // We did the allocate!
           this.lastFragmentAllocation.set(fragIdx);
-          Chunk result = this.ma.chunkFactory.newChunk(fragment.getMemoryAddress()+oldOffset,
chunkSize+extraSize, chunkType);
+          ChunkFactory cf = this.ma.getChunkFactory();
+          Chunk result = cf.newChunk(fragment.getMemoryAddress()+oldOffset, chunkSize+extraSize,
chunkType);
           allocSize -= chunkSize+extraSize;
           oldOffset += extraSize;
           while (allocSize > 0) {
@@ -531,7 +535,7 @@ public class FreeListManager {
             // we add the batch ones immediately to the freelist
             result.readyForFree();
             free(result.getMemoryAddress(), false);
-            result = this.ma.chunkFactory.newChunk(fragment.getMemoryAddress()+oldOffset,
chunkSize, chunkType);
+            result = cf.newChunk(fragment.getMemoryAddress()+oldOffset, chunkSize, chunkType);
             allocSize -= chunkSize;
           }
 
@@ -566,7 +570,7 @@ public class FreeListManager {
     if (clq != null) {
       long memAddr = clq.poll();
       if (memAddr != 0) {
-        Chunk result = this.ma.chunkFactory.newChunk(memAddr, chunkType);
+        Chunk result = this.ma.getChunkFactory().newChunk(memAddr, chunkType);
 
         // Data integrity check.
         if(this.ma.validateMemoryWithFill) {          
@@ -599,7 +603,7 @@ public class FreeListManager {
         if (chunkType.getSrcType() != Chunk.getSrcType(result.getMemoryAddress())) {
           // The java wrapper class that was cached in the huge chunk list is the wrong type.
           // So allocate a new one and garbage collect the old one.
-          result = this.ma.chunkFactory.newChunk(result.getMemoryAddress(), chunkType);
+          result = this.ma.getChunkFactory().newChunk(result.getMemoryAddress(), chunkType);
         }
         result.readyForAllocation(chunkType);
         return result;
@@ -641,10 +645,11 @@ public class FreeListManager {
   private void free(long addr, boolean updateStats) {
     int cSize = Chunk.getSize(addr);
     if (updateStats) {
-      this.ma.stats.incObjects(-1);
+      OffHeapMemoryStats stats = this.ma.getStats();
+      stats.incObjects(-1);
       this.allocatedSize.addAndGet(-cSize);
-      this.ma.stats.incUsedMemory(-cSize);
-      this.ma.stats.incFreeMemory(cSize);
+      stats.incUsedMemory(-cSize);
+      stats.incFreeMemory(cSize);
       this.ma.notifyListeners();
     }
     if (cSize <= SimpleMemoryAllocatorImpl.MAX_TINY) {
@@ -671,7 +676,7 @@ public class FreeListManager {
 
   }
   private void freeHuge(long addr, int cSize) {
-    this.hugeChunkSet.add(this.ma.chunkFactory.newChunk(addr)); // TODO make this a collection
of longs
+    this.hugeChunkSet.add(this.ma.getChunkFactory().newChunk(addr)); // TODO make this a
collection of longs
   }
 
   List<MemoryBlock> getOrderedBlocks() {

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/be9373ee/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
index 12d297b..425d057 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
@@ -55,7 +55,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
  * @author Kirk Lund
  * @since 9.0
  */
-public final class SimpleMemoryAllocatorImpl implements MemoryAllocator {
+public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
 
   static final Logger logger = LogService.getLogger();
   
@@ -85,10 +85,14 @@ public final class SimpleMemoryAllocatorImpl implements MemoryAllocator
{
    */
   public final static int HUGE_MULTIPLE = 256;
   
-  volatile OffHeapMemoryStats stats;
+  private volatile OffHeapMemoryStats stats;
   
-  volatile OutOfOffHeapMemoryListener ooohml;
+  private volatile OutOfOffHeapMemoryListener ooohml;
   
+  OutOfOffHeapMemoryListener getOutOfOffHeapMemoryListener() {
+    return this.ooohml;
+  }
+
   /** The MemoryChunks that this allocator is managing by allocating smaller chunks of them.
    * The contents of this array never change.
    */
@@ -103,7 +107,7 @@ public final class SimpleMemoryAllocatorImpl implements MemoryAllocator
{
   private volatile MemoryUsageListener[] memoryUsageListeners = new MemoryUsageListener[0];
   
   private static SimpleMemoryAllocatorImpl singleton = null;
-  final ChunkFactory chunkFactory;
+  private final ChunkFactory chunkFactory;
   
   public static SimpleMemoryAllocatorImpl getAllocator() {
     SimpleMemoryAllocatorImpl result = singleton;

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/be9373ee/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
new file mode 100644
index 0000000..4b87375
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
@@ -0,0 +1,82 @@
+package com.gemstone.gemfire.internal.offheap;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class FreeListManagerTest {
+  static {
+    ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
+  }
+
+  private final SimpleMemoryAllocatorImpl ma = mock(SimpleMemoryAllocatorImpl.class);
+  private final UnsafeMemoryChunk slab = new UnsafeMemoryChunk(1024*1024*5);
+  private final OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
+  private final ChunkFactory cf = new GemFireChunkFactory();
+  private FreeListManager freeListManager;
+  
+
+  @BeforeClass
+  public static void setUpBeforeClass() throws Exception {
+  }
+
+  @AfterClass
+  public static void tearDownAfterClass() throws Exception {
+  }
+
+  @Before
+  public void setUp() throws Exception {
+    when(ma.getSlabs()).thenReturn(new UnsafeMemoryChunk[] {slab});
+    when(ma.getTotalMemory()).thenReturn((long) slab.getSize());
+    when(ma.getStats()).thenReturn(stats);
+    when(ma.getChunkFactory()).thenReturn(cf);
+    
+    this.freeListManager = new FreeListManager(ma);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    slab.release();
+  }
+
+  @Test
+  public void usedMemoryIsZeroOnDefault() {
+    assertEquals(0, this.freeListManager.getUsedMemory());
+  }
+
+  @Test
+  public void freeMemoryIsAllocatorTotalOnDefault() {
+    assertEquals(ma.getTotalMemory(), this.freeListManager.getFreeMemory());
+  }
+  
+  @Test
+  public void allocateTinyChunkHasCorrectSize() {
+    Chunk c = this.freeListManager.allocate(10, null);
+    assertNotNull(c);
+    assertEquals(10, c.getDataSize());
+    assertEquals(24, c.getSize());
+  }
+
+  @Test
+  public void allocateTinyChunkWithExplicitTypeHasCorrectSize() {
+    Chunk c = this.freeListManager.allocate(10, GemFireChunk.TYPE);
+    assertNotNull(c);
+    assertEquals(10, c.getDataSize());
+    assertEquals(24, c.getSize());
+  }
+  
+  @Test(expected = AssertionError.class)
+  public void allocateZeroThrowsAssertion() {
+    this.freeListManager.allocate(0, null);
+  }
+  
+  @Test(expected = AssertionError.class)
+  public void allocateNegativeThrowsAssertion() {
+    this.freeListManager.allocate(-123, null);
+  }
+}


Mime
View raw message