You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by sa...@apache.org on 2016/03/01 02:45:26 UTC

[1/2] incubator-geode git commit: Fixed review comments

Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-996 f15010cf6 -> 367011540


Fixed review comments


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

Branch: refs/heads/feature/GEODE-996
Commit: a2a84e750861c4bb5fcdc320aa6a62472e5a6c7e
Parents: f15010c
Author: Sai Boorlagadda <sb...@pivotal.io>
Authored: Mon Feb 29 13:09:40 2016 -0800
Committer: Sai Boorlagadda <sb...@pivotal.io>
Committed: Mon Feb 29 13:09:40 2016 -0800

----------------------------------------------------------------------
 .../internal/offheap/FreeListManager.java       | 27 ++++++------
 .../internal/offheap/FreeListManagerTest.java   | 46 ++++++++++++++++----
 2 files changed, 51 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/a2a84e75/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
index 5d5d688..4ecc4e4 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
@@ -473,30 +473,29 @@ public class FreeListManager {
     }
   }
   
-  protected int getNoOfFragments() {
+  protected int getFragmentsCount() {
     return this.fragmentList.size();
   }
   
   protected int getFragmentation() {
-
-    int availableFragments = getNoOfFragments();
-    
-    if(availableFragments < 2) {
-      //case when there are no fragments or only one fragment
-      //then freeMemory is either be 0 or as 1 fragment, so freeMemory is not fragmented.
+    if(getUsedMemory() == 0) {
+      //when no memory is used then there is no fragmentation
       return 0;
     } else {
-      long freeMemory = getFreeMemory();
-      long totalMemory = getTotalMemory();
-      if (freeMemory == totalMemory) {
-        //case when freeMemory is entirely totalMemory then freeMemory is not fragmented
+      int availableFragments = getFragmentsCount();
+      if (availableFragments == 0) {
+        //zero fragments means no free memory then no fragmentation
+        return 0;
+      } else if (availableFragments == 1) {
+        //free memory is available as one fragment, so no fragmentation
         return 0;
       } else {
-        //case when freeMemory is fragmented
+        //when more than 1 fragment is available
         //then compare the no. of available fragments with max no. of possible fragments
+        long freeMemory = getFreeMemory();
         long maxPossibleFragments = freeMemory / ObjectChunk.MIN_CHUNK_SIZE;
-        double fragmentation = (availableFragments / maxPossibleFragments) * 100.0;
-        return (int) fragmentation;
+        double fragmentation = ((double) availableFragments /(double) maxPossibleFragments) * 100d;
+        return (int) Math.rint(fragmentation);
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/a2a84e75/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
index c32b5ee..764944b 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
@@ -747,7 +747,7 @@ public class FreeListManagerTest {
     
     FreeListManager spy = spy(this.freeListManager);
     
-    when(spy.getNoOfFragments()).thenReturn(0);
+    when(spy.getFragmentsCount()).thenReturn(0);
     
     assertThat(spy.getFragmentation()).isZero();
   }
@@ -759,21 +759,19 @@ public class FreeListManagerTest {
     
     FreeListManager spy = spy(this.freeListManager);
     
-    when(spy.getNoOfFragments()).thenReturn(1);
+    when(spy.getFragmentsCount()).thenReturn(1);
     
     assertThat(spy.getFragmentation()).isZero();
   }
   
   @Test
-  public void fragmentationShouldBeZeroIfTotalMemoryIsFree() {
+  public void fragmentationShouldBeZeroIfUsedMemoryIsZero() {
     UnsafeMemoryChunk chunk = new UnsafeMemoryChunk(10);
     this.freeListManager = createFreeListManager(ma, new UnsafeMemoryChunk[] {chunk});
     
     FreeListManager spy = spy(this.freeListManager);
     
-    when(spy.getNoOfFragments()).thenReturn(2);
-    when(spy.getFreeMemory()).thenReturn(Long.MAX_VALUE);
-    when(spy.getTotalMemory()).thenReturn(Long.MAX_VALUE);
+    when(spy.getUsedMemory()).thenReturn(0L);
     
     assertThat(spy.getFragmentation()).isZero();
   }
@@ -785,13 +783,45 @@ public class FreeListManagerTest {
     
     FreeListManager spy = spy(this.freeListManager);
     
-    when(spy.getNoOfFragments()).thenReturn(2);
+    when(spy.getUsedMemory()).thenReturn(1L);
+    when(spy.getFragmentsCount()).thenReturn(2);
     when(spy.getFreeMemory()).thenReturn((long)ObjectChunk.MIN_CHUNK_SIZE * 2);
-    when(spy.getTotalMemory()).thenReturn(Long.MAX_VALUE);
     
     assertThat(spy.getFragmentation()).isEqualTo(100);
   }
   
+  @Test
+  public void fragmentationShouldBeRoundedToNearestInteger() {
+    UnsafeMemoryChunk chunk = new UnsafeMemoryChunk(10);
+    this.freeListManager = createFreeListManager(ma, new UnsafeMemoryChunk[] {chunk});
+    
+    FreeListManager spy = spy(this.freeListManager);
+    
+    when(spy.getUsedMemory()).thenReturn(1L);
+    when(spy.getFragmentsCount()).thenReturn(4);
+    when(spy.getFreeMemory()).thenReturn((long)ObjectChunk.MIN_CHUNK_SIZE * 8);
+    
+    assertThat(spy.getFragmentation()).isEqualTo(50); //Math.rint(50.0)
+    
+    when(spy.getUsedMemory()).thenReturn(1L);
+    when(spy.getFragmentsCount()).thenReturn(3);
+    when(spy.getFreeMemory()).thenReturn((long)ObjectChunk.MIN_CHUNK_SIZE * 8);
+    
+    assertThat(spy.getFragmentation()).isEqualTo(38); //Math.rint(37.5)
+    
+    when(spy.getUsedMemory()).thenReturn(1L);
+    when(spy.getFragmentsCount()).thenReturn(6);
+    when(spy.getFreeMemory()).thenReturn((long)ObjectChunk.MIN_CHUNK_SIZE * 17);
+    
+    assertThat(spy.getFragmentation()).isEqualTo(35); //Math.rint(35.29)
+    
+    when(spy.getUsedMemory()).thenReturn(1L);
+    when(spy.getFragmentsCount()).thenReturn(6);
+    when(spy.getFreeMemory()).thenReturn((long)ObjectChunk.MIN_CHUNK_SIZE * 9);
+    
+    assertThat(spy.getFragmentation()).isEqualTo(67); //Math.rint(66.66)
+  }
+  
   /**
    * Just like Fragment except that the first time allocate is called
    * it returns false indicating that the allocate failed.


[2/2] incubator-geode git commit: Fixed fragmentation DUnit

Posted by sa...@apache.org.
Fixed fragmentation DUnit


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

Branch: refs/heads/feature/GEODE-996
Commit: 3670115404c8de9ef0a8e0314d2cc1b5046a27a0
Parents: a2a84e7
Author: Sai Boorlagadda <sb...@pivotal.io>
Authored: Mon Feb 29 17:45:03 2016 -0800
Committer: Sai Boorlagadda <sb...@pivotal.io>
Committed: Mon Feb 29 17:45:03 2016 -0800

----------------------------------------------------------------------
 .../management/OffHeapManagementDUnitTest.java  | 54 +++++++++++++++-----
 1 file changed, 41 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/36701154/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java
index acafada..3d06e11 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java
@@ -36,6 +36,7 @@ import com.gemstone.gemfire.cache30.CacheTestCase;
 import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.internal.offheap.ObjectChunk;
 import com.gemstone.gemfire.internal.offheap.OffHeapMemoryStats;
 import com.gemstone.gemfire.internal.offheap.OffHeapStorage;
 import com.gemstone.gemfire.management.internal.MBeanJMXAdapter;
@@ -237,21 +238,51 @@ public class OffHeapManagementDUnitTest extends CacheTestCase {
       // Make sure our starting off heap stats are correct
       assertOffHeapMetricsOnVm(vm, TOTAL_MEMORY, 0, 0, 0);
       
-      // After allocating large chunck we should still have no fragmentation
-      doPutOnVm(vm, KEY, new byte[HALF_TOTAL_MEMORY], OFF_HEAP_REGION_NAME, false);
+      // After allocating large chunk (equal to total memory) 
+      // we should still have no fragmentation
+      int largeChunk = (int) TOTAL_MEMORY - ObjectChunk.OFF_HEAP_HEADER_SIZE;
+      doPutOnVm(vm, KEY, new byte[largeChunk], OFF_HEAP_REGION_NAME, false);
+      // No compaction has run, so fragmentation should be zero
+      assertFragmentationStatOnVm(vm,0,ASSERT_OP.EQUAL);
+      
+      // Allocate more memory to trigger compaction
+      doPutOnVm(vm, KEY, new byte[ALLOCATION_SIZE], OFF_HEAP_REGION_NAME, true);
+      // When total memory is used no fragmentation
       assertFragmentationStatOnVm(vm,0,ASSERT_OP.EQUAL);
       
       // After freeing all memory we should have no fragmentation
       doDestroyOnVm(vm, KEY, OFF_HEAP_REGION_NAME);
       assertFragmentationStatOnVm(vm,0,ASSERT_OP.EQUAL);
       
-      // Consume all off-heap memory using an allocation size
-      int numAllocations = doConsumeOffHeapMemoryOnVm(vm,ALLOCATION_SIZE);
-      assertTrue(numAllocations > 0);
+      // Allocate HALF_TOTAL_MEMORY twice and release one to create one fragment
+      int halfChunk = HALF_TOTAL_MEMORY - ObjectChunk.OFF_HEAP_HEADER_SIZE;
+      doPutOnVm(vm, KEY + "0", new byte[halfChunk], OFF_HEAP_REGION_NAME, false);
+      doPutOnVm(vm, KEY + "1", new byte[halfChunk], OFF_HEAP_REGION_NAME, false);
+      doDestroyOnVm(vm, KEY + "0", OFF_HEAP_REGION_NAME);
       
-      // Randomly free 3 allocations to produce off-heap gaps
-      doFreeOffHeapMemoryOnVm(vm, numAllocations, 3);
-
+      // Allocate largeChunk to trigger compaction and fragmentation should be zero 
+      // as all free memory is available as one fragment
+      doPutOnVm(vm, KEY + "1", new byte[largeChunk], OFF_HEAP_REGION_NAME, true);
+      assertFragmentationStatOnVm(vm,0,ASSERT_OP.EQUAL);
+      
+      // Consume the available fragment as below
+      // [16][262120][16][262120][16] = [524288] (HALF_TOTAL_MEMORY)
+      int smallChunk = ObjectChunk.MIN_CHUNK_SIZE - ObjectChunk.OFF_HEAP_HEADER_SIZE;
+      int mediumChunk = 262112; //(262120 - ObjectChunk.OFF_HEAP_HEADER_SIZE)
+      doPutOnVm(vm, KEY + "S1", new byte[smallChunk], OFF_HEAP_REGION_NAME, false);
+      doPutOnVm(vm, KEY + "M1", new byte[mediumChunk], OFF_HEAP_REGION_NAME, false);
+      doPutOnVm(vm, KEY + "S2", new byte[smallChunk], OFF_HEAP_REGION_NAME, false);
+      doPutOnVm(vm, KEY + "M2", new byte[mediumChunk], OFF_HEAP_REGION_NAME, false);
+      doPutOnVm(vm, KEY + "S3", new byte[smallChunk], OFF_HEAP_REGION_NAME, false);
+      
+      // free small chunks to create gaps
+      doDestroyOnVm(vm, KEY + "S1", OFF_HEAP_REGION_NAME);
+      doDestroyOnVm(vm, KEY + "S2", OFF_HEAP_REGION_NAME);
+      doDestroyOnVm(vm, KEY + "S3", OFF_HEAP_REGION_NAME);
+
+      // Now free memory should be 48 so allocate a 40 byte object
+      doPutOnVm(vm, KEY + "newKey", new byte[40], OFF_HEAP_REGION_NAME, true);
+     
       /*
        * Setup a fragmentation attribute monitor
        */
@@ -259,12 +290,9 @@ public class OffHeapManagementDUnitTest extends CacheTestCase {
         setupOffHeapMonitorOnVm(vm,"OffHeapFragmentation",0,0);      
         clearNotificationListenerOnVm(vm);
       }
-      
-      // Allocate enough memory to force compaction which will update fragmenation stat
-      doPutOnVm(vm,KEY, new byte[NEW_ALLOCATION_SIZE], OFF_HEAP_REGION_NAME, true);
-      
+
       // Make sure we have some fragmentation
-      assertFragmentationStatOnVm(vm, 0, ASSERT_OP.GREATER_THAN);
+      assertFragmentationStatOnVm(vm, 100, ASSERT_OP.EQUAL);
       
       // Make sure our fragmentation monitor was triggered
       waitForNotificationListenerOnVm(vm, 5000, 500, true);