geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sai_boorlaga...@apache.org
Subject [1/2] incubator-geode git commit: Fixed review comments
Date Tue, 01 Mar 2016 01:45:26 GMT
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 <sboorlagadda@pivotal.io>
Authored: Mon Feb 29 13:09:40 2016 -0800
Committer: Sai Boorlagadda <sboorlagadda@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.


Mime
View raw message