Memory alignment for a MappedBuffer: how to find out the off-heap memory pointer to align?

283 views
Skip to first unread message

bob.tw...@gmail.com

unread,
Apr 29, 2015, 8:21:56 AM4/29/15
to mechanica...@googlegroups.com
I read this great article by Nitsan Wakart where he talks about the importance of memory alignment for concurrency: 


Basically he concluded: "Aligned access means writing data types to addresses which divide by their size."

My question is:

After creating a MappedByffer through a memory-mapped file, how can I find out the off-heap address pointer of my MappedBuffer so I can proceed to do proper alignment? Can Unsafe help me here somehow?

My goal is to write a long to the MappedBuffer so it can be read concurrently and atomically by another process accessing the same memory-mapped file from another JVM.


rick.ow...@gmail.com

unread,
Apr 29, 2015, 9:55:51 AM4/29/15
to mechanica...@googlegroups.com

The Buffer class has a field called "address". Grab this field through reflection the same way you get "theUnsafe" for off-heap access.

Martin Thompson

unread,
Apr 29, 2015, 9:58:32 AM4/29/15
to mechanica...@googlegroups.com
https://github.com/real-logic/Agrona/blob/master/src/main/java/uk/co/real_logic/agrona/concurrent/UnsafeBuffer.java

Memory map a file and then pass in the MappedByteBuffer to the AtomicBuffer and off you go.

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vitaly Davidovich

unread,
Apr 29, 2015, 10:12:12 AM4/29/15
to mechanica...@googlegroups.com
I cast to DirectBuffer and call address() -- I prefer that option to reflection.  Although with java 8 compiler there're warnings now emitted about this type being internal and subject to removal.

Also, some processors (e.g. Intel Sandy Bridge and up) don't penalize you, performance wise, for unaligned reads/writes.  For atomicity, as long as the unaligned read/write is within a cacheline, it will be carried out atomically (at hw level).

ymo

unread,
Apr 29, 2015, 10:21:47 AM4/29/15
to mechanica...@googlegroups.com
If you look at the code from the article that is exactly what he is doing.  On line 5 you have :

long alignedAddress = UnsafeDirectByteBuffer.getAddress(buffy);

The author has the actual class mentioned in the article on github:



On Wednesday, April 29, 2015 at 8:21:56 AM UTC-4, bob.tw...@gmail.com wrote:

Richard Warburton

unread,
Apr 29, 2015, 3:58:48 PM4/29/15
to mechanica...@googlegroups.com
Hi,

After creating a MappedByffer through a memory-mapped file, how can I find out the off-heap address pointer of my MappedBuffer so I can proceed to do proper alignment? Can Unsafe help me here somehow?

One thing worth noting is that you've got a buffer from a memory mapped file then the memory will be allocated in terms of pages. This means that index 0 is already page aligned, and since page sizes are a multiple of cache and word sizes it means that its already cache and word aligned.

So you don't necessarily need to resort to reflection or casting to private APIs in order to get the address. You just need to pad out the objects that you're writing into this space to make sure that the alignment that you want is retained at the end of each object.

regards,

  Richard Warburton

Martin Thompson

unread,
Apr 29, 2015, 4:01:55 PM4/29/15
to mechanica...@googlegroups.com
If you look at this implementation you'll see it has a implementation of AtomicBuffer.verifyAlignment() which throws an IllegalStateException if the buffer is not aligned.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-sympathy+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages