Repository: mnemonic Updated Branches: refs/heads/master d998b5ac8 -> c476763dc
MNEMONIC-460: Add helper code to verify the heap memory usage of data structures Project: http://git-wip-us.apache.org/repos/asf/mnemonic/repo Commit: http://git-wip-us.apache.org/repos/asf/mnemonic/commit/c476763d Tree: http://git-wip-us.apache.org/repos/asf/mnemonic/tree/c476763d Diff: http://git-wip-us.apache.org/repos/asf/mnemonic/diff/c476763d Branch: refs/heads/master Commit: c476763dcbff5a763f539124486d6a319c10e21a Parents: d998b5a Author: Wang, Gang(Gary) <ga...@apache.org> Authored: Fri Feb 2 23:22:16 2018 -0800 Committer: Wang, Gang(Gary) <ga...@apache.org> Committed: Fri Feb 2 23:22:16 2018 -0800 ---------------------------------------------------------------------- .../main/java/org/apache/mnemonic/Utils.java | 53 ++++++++++++++++++++ .../apache/mnemonic/examples/HelloWorld.java | 43 ++++++++++++++++ 2 files changed, 96 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mnemonic/blob/c476763d/mnemonic-core/src/main/java/org/apache/mnemonic/Utils.java ---------------------------------------------------------------------- diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/Utils.java b/mnemonic-core/src/main/java/org/apache/mnemonic/Utils.java index b1ed718..8ef27af 100644 --- a/mnemonic-core/src/main/java/org/apache/mnemonic/Utils.java +++ b/mnemonic-core/src/main/java/org/apache/mnemonic/Utils.java @@ -26,6 +26,7 @@ import sun.misc.Unsafe; import java.io.File; import java.lang.reflect.Field; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -536,4 +537,56 @@ public class Utils { } } } + + /** + * get the address of an object + * + * @param unsafe + * an unsafe object + * @param o + * an object to retrieve its address + * + * @return + * the address of this object + */ + public static long addressOf(Unsafe unsafe, Object o) { + Object[] array = new Object[] {o}; + + long baseOffset = unsafe.arrayBaseOffset(Object[].class); + int addressSize = unsafe.addressSize(); + long objectAddress; + switch (addressSize) { + case 4: + objectAddress = unsafe.getInt(array, baseOffset); + break; + case 8: + objectAddress = unsafe.getLong(array, baseOffset); + break; + default: + throw new Error("unsupported address size: " + addressSize); + } + return objectAddress; + } + + + + /** + * Gets the address value for the memory that backs a direct byte buffer. + * + * @param buffer + * A buffer to retrieve its address + * + * @return + * The system address for the buffers + */ + public static long getAddressFromDirectByteBuffer(ByteBuffer buffer) { + try { + Field addressField = Buffer.class.getDeclaredField("address"); + addressField.setAccessible(true); + return addressField.getLong(buffer); + } catch (Exception e) { + throw new RuntimeException("Unable to address field from ByteBuffer", e); + } + } + } http://git-wip-us.apache.org/repos/asf/mnemonic/blob/c476763d/mnemonic-examples/src/main/java/org/apache/mnemonic/examples/HelloWorld.java ---------------------------------------------------------------------- diff --git a/mnemonic-examples/src/main/java/org/apache/mnemonic/examples/HelloWorld.java b/mnemonic-examples/src/main/java/org/apache/mnemonic/examples/HelloWorld.java index 9ec4217..32ee9be 100644 --- a/mnemonic-examples/src/main/java/org/apache/mnemonic/examples/HelloWorld.java +++ b/mnemonic-examples/src/main/java/org/apache/mnemonic/examples/HelloWorld.java @@ -17,6 +17,7 @@ package org.apache.mnemonic.examples; +import org.apache.mnemonic.DurableBuffer; import org.apache.mnemonic.NonVolatileMemAllocator; import org.apache.mnemonic.Utils; import org.apache.mnemonic.collections.DurableString; @@ -39,5 +40,47 @@ public class HelloWorld { /* print out the string from this durable string object */ System.out.println(Utils.ANSI_GREEN + s.getStr() + Utils.ANSI_RESET); + /* print out the address of string */ + System.out.println(String.format("The address of this durable string object: 0x%016X", + Utils.addressOf(Utils.getUnsafe(), s.getStr()))); + + /* print out the hashcode of string */ + System.out.println(String.format("The hash code of this durable string object: 0x%08X", + s.getStr().hashCode())); + + /* create a durable buffer, it is able to store up to 10 characters */ + DurableBuffer<?> dbuf = act.createBuffer(20); + + /* store a string into this durable buffer*/ + dbuf.get().asCharBuffer().put("hello"); + + /* return a string containing the character sequence in this durable buffer */ + String dbufstr = dbuf.get().asCharBuffer().toString(); + + /* print out this string */ + System.out.println(String.format("A string returned from the durable buffer: %s", dbufstr)); + + /* store another string into the same durable buffer */ + dbuf.get().asCharBuffer().put("world"); + + /* print out the original string */ + System.out.println(String.format("The original string with the content of this durable buffer changed: %s", + dbufstr)); + + /* return a new string containing the character sequence in this durable buffer */ + String dbufstr2 = dbuf.get().asCharBuffer().toString(); + + /* print out the new string */ + System.out.println(String.format("A new string returned from the same durable buffer: %s", + dbufstr2)); + + /* print out the address of the content of this durable buffer */ + System.out.println(String.format("The address of the content of this durable buffer: 0x%016X", + Utils.getAddressFromDirectByteBuffer(dbuf.get()))); + + /* print out the base address of memory space */ + System.out.println(String.format("The base address of this memory space: 0x%016X", + act.getEffectiveAddress(0L))); + } }