You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2013/05/13 23:30:16 UTC

[3/4] git commit: fix memorySize bugs and cleanup patch by Carl Yeksigian and jbellis for CASSANDRA-5564

fix memorySize bugs and cleanup
patch by Carl Yeksigian and jbellis for CASSANDRA-5564


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

Branch: refs/heads/cassandra-1.2
Commit: fe10ba7df361c80d17a11aeed68d8b1c39ec1ae9
Parents: 3b41d21
Author: Jonathan Ellis <jb...@apache.org>
Authored: Mon May 13 16:28:48 2013 -0500
Committer: Jonathan Ellis <jb...@apache.org>
Committed: Mon May 13 16:28:48 2013 -0500

----------------------------------------------------------------------
 .../org/apache/cassandra/cache/KeyCacheKey.java    |    7 ++-
 .../org/apache/cassandra/cache/RowCacheKey.java    |    7 ++-
 .../apache/cassandra/cache/RowCacheSentinel.java   |    3 +-
 .../org/apache/cassandra/db/RowIndexEntry.java     |   18 +++++--
 .../apache/cassandra/io/sstable/IndexHelper.java   |    9 +++-
 .../org/apache/cassandra/utils/ObjectSizes.java    |   38 ++++++++++++---
 6 files changed, 62 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/fe10ba7d/src/java/org/apache/cassandra/cache/KeyCacheKey.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cache/KeyCacheKey.java b/src/java/org/apache/cassandra/cache/KeyCacheKey.java
index 9741c7f..3b2077c 100644
--- a/src/java/org/apache/cassandra/cache/KeyCacheKey.java
+++ b/src/java/org/apache/cassandra/cache/KeyCacheKey.java
@@ -52,8 +52,11 @@ public class KeyCacheKey implements CacheKey
 
     public long memorySize()
     {
-        long fields = ObjectSizes.getReferenceSize() + ObjectSizes.getSizeWithRef(key);
-        return ObjectSizes.getFieldSize(fields);
+        return ObjectSizes.getFieldSize(// desc
+                                        ObjectSizes.getReferenceSize() +
+                                        // key
+                                        ObjectSizes.getReferenceSize())
+               + ObjectSizes.getArraySize(key);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cassandra/blob/fe10ba7d/src/java/org/apache/cassandra/cache/RowCacheKey.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cache/RowCacheKey.java b/src/java/org/apache/cassandra/cache/RowCacheKey.java
index d7dab35..33e2065 100644
--- a/src/java/org/apache/cassandra/cache/RowCacheKey.java
+++ b/src/java/org/apache/cassandra/cache/RowCacheKey.java
@@ -52,8 +52,11 @@ public class RowCacheKey implements CacheKey, Comparable<RowCacheKey>
 
     public long memorySize()
     {
-        long fields = ObjectSizes.getReferenceSize() + ObjectSizes.getSizeWithRef(key);
-        return ObjectSizes.getFieldSize(fields);
+        return ObjectSizes.getFieldSize(// cfId
+                                        ObjectSizes.getReferenceSize() +
+                                        // key
+                                        ObjectSizes.getReferenceSize())
+               + ObjectSizes.getArraySize(key);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cassandra/blob/fe10ba7d/src/java/org/apache/cassandra/cache/RowCacheSentinel.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cache/RowCacheSentinel.java b/src/java/org/apache/cassandra/cache/RowCacheSentinel.java
index d7da6ce..9a014dc 100644
--- a/src/java/org/apache/cassandra/cache/RowCacheSentinel.java
+++ b/src/java/org/apache/cassandra/cache/RowCacheSentinel.java
@@ -19,6 +19,7 @@ package org.apache.cassandra.cache;
 
 import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.cassandra.db.TypeSizes;
 import org.apache.cassandra.utils.ObjectSizes;
 
 import com.google.common.base.Objects;
@@ -60,6 +61,6 @@ public class RowCacheSentinel implements IRowCacheEntry
     public long memorySize()
     {
         // Only long reference.
-        return ObjectSizes.getFieldSize(8);
+        return ObjectSizes.getFieldSize(TypeSizes.NATIVE.sizeof(sentinelId));
     }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/fe10ba7d/src/java/org/apache/cassandra/db/RowIndexEntry.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/RowIndexEntry.java b/src/java/org/apache/cassandra/db/RowIndexEntry.java
index c53e52f..b3904d4 100644
--- a/src/java/org/apache/cassandra/db/RowIndexEntry.java
+++ b/src/java/org/apache/cassandra/db/RowIndexEntry.java
@@ -88,8 +88,7 @@ public class RowIndexEntry implements IMeasurableMemory
 
     public long memorySize()
     {
-        long fields = TypeSizes.NATIVE.sizeof(position) + ObjectSizes.getReferenceSize(); 
-        return ObjectSizes.getFieldSize(fields);
+        return ObjectSizes.getFieldSize(TypeSizes.NATIVE.sizeof(position));
     }
 
     public static class Serializer
@@ -200,13 +199,20 @@ public class RowIndexEntry implements IMeasurableMemory
             return (int)size;
         }
 
+        @Override
         public long memorySize()
         {
-            long internal = 0;
+            long entrySize = 0;
             for (IndexHelper.IndexInfo idx : columnsIndex)
-                internal += idx.memorySize();
-            long listSize = ObjectSizes.getFieldSize(ObjectSizes.getArraySize(columnsIndex.size(), internal) + 4);
-            return ObjectSizes.getFieldSize(deletionTime.memorySize() + listSize);
+                entrySize += idx.memorySize();
+
+            return ObjectSizes.getSuperClassFieldSize(TypeSizes.NATIVE.sizeof(position))
+                   + ObjectSizes.getFieldSize(// deletionTime
+                                              ObjectSizes.getReferenceSize() +
+                                              // columnsIndex
+                                              ObjectSizes.getReferenceSize())
+                   + deletionTime.memorySize()
+                   + ObjectSizes.getArraySize(columnsIndex.size(), ObjectSizes.getReferenceSize()) + entrySize + 4;
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/fe10ba7d/src/java/org/apache/cassandra/io/sstable/IndexHelper.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/sstable/IndexHelper.java b/src/java/org/apache/cassandra/io/sstable/IndexHelper.java
index b93c019..b74e48b 100644
--- a/src/java/org/apache/cassandra/io/sstable/IndexHelper.java
+++ b/src/java/org/apache/cassandra/io/sstable/IndexHelper.java
@@ -189,8 +189,13 @@ public class IndexHelper
 
         public long memorySize()
         {
-            long fields = ObjectSizes.getSize(firstName) + ObjectSizes.getSize(lastName) + 8 + 8; 
-            return ObjectSizes.getFieldSize(fields);
+            return ObjectSizes.getFieldSize(// firstName
+                                            ObjectSizes.getReferenceSize() +
+                                            // lastName
+                                            ObjectSizes.getReferenceSize() +
+                                            TypeSizes.NATIVE.sizeof(offset) +
+                                            TypeSizes.NATIVE.sizeof(width))
+                   + ObjectSizes.getSize(firstName) + ObjectSizes.getSize(lastName);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/fe10ba7d/src/java/org/apache/cassandra/utils/ObjectSizes.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/ObjectSizes.java b/src/java/org/apache/cassandra/utils/ObjectSizes.java
index 66ec501..acee919 100644
--- a/src/java/org/apache/cassandra/utils/ObjectSizes.java
+++ b/src/java/org/apache/cassandra/utils/ObjectSizes.java
@@ -56,26 +56,52 @@ public class ObjectSizes
         int getSuperclassFieldPadding();
     }
 
+    /**
+     * Memory a class consumes, including the object header and the size of the fields.
+     * @param fieldsSize Total size of the primitive fields of a class
+     * @return Total in-memory size of the class
+     */
     public static long getFieldSize(long fieldsSize)
     {
         return roundTo(SPEC.getObjectHeaderSize() + fieldsSize, SPEC.getObjectPadding());
     }
 
+    /**
+     * Memory a super class consumes, given the primitive field sizes
+     * @param fieldsSize Total size of the primitive fields of the super class
+     * @return Total additional in-memory that the super class takes up
+     */
     public static long getSuperClassFieldSize(long fieldsSize)
     {
         return roundTo(fieldsSize, SPEC.getSuperclassFieldPadding());
     }
 
+    /**
+     * Memory an array will consume
+     * @param length Number of elements in the array
+     * @param elementSize In-memory size of each element's primitive stored
+     * @return In-memory size of the array
+     */
     public static long getArraySize(int length, long elementSize)
     {
         return roundTo(SPEC.getArrayHeaderSize() + length * elementSize, SPEC.getObjectPadding());
     }
 
-    public static long getSizeWithRef(byte[] bytes)
+    /**
+     * Memory a byte array consumes
+     * @param bytes byte array to get memory size
+     * @return In-memory size of the array
+     */
+    public static long getArraySize(byte[] bytes)
     {
-        return SPEC.getReferenceSize() + getArraySize(bytes.length, 1);
+        return getArraySize(bytes.length, 1);
     }
 
+    /**
+     * Memory a byte buffer consumes
+     * @param buffer ByteBuffer to calculate in memory size
+     * @return Total in-memory size of the byte buffer
+     */
     public static long getSize(ByteBuffer buffer)
     {
         long size = 0;
@@ -93,16 +119,14 @@ public class ObjectSizes
         return size;
     }
 
-    public static long getSizeWithRef(ByteBuffer buffer)
-    {
-        return SPEC.getReferenceSize() + getSize(buffer);
-    }
-
     public static long roundTo(long x, int multiple)
     {
         return ((x + multiple - 1) / multiple) * multiple;
     }
 
+    /**
+     * @return Memory a reference consumes on the current architecture.
+     */
     public static int getReferenceSize()
     {
         return SPEC.getReferenceSize();