About Ashkrit Sharma

Pragmatic software developer who loves practice that makes software development fun and likes to develop high performance & low latency system.

Off Heap concurrent counter

Concurrent counter are part of almost every system, it is used to collect data, thread synchronization etc. Java has good support of heap based counter.

There could be use case when you need counter that can be shared between processor.
 
 
 
 
 

How to build inter process counters

Database

This is first option that comes to mind, database sequence is counter that can be used by multiple process. All concurrency is handled by database. It is good option for starter but we know types of overhead(Network,Locks etc) you get with database. Only Larry Elision will be happy about it, not you!

Some Server

You could develop some server/middleware that provides such type of service. This option will still have network latency,marshal/unmarshal overhead.

Memory Mapped file

You could use memory mapped file to do this. I got idea from looking at thread-safe-interprocess-shared-memory-in-java presentation from PeterLawrey.

Challenges involved in multi process counter.

Data visibility

Changes done by one process should be visible to all the process. This problem can be solved by using memory mapped file. Operating System gives guarantee about it and java memory model supports it to make is possible.

Thread safety

Counters is all about multiple writers , so thread safety becomes big problem. Compare-and-swap is one option to handles multiple writers. Is it possible to use CAS for off heap operation ? yes it is possible to do that , welcome to Unsafe. By using using Memorymapped & Unsafe it is possible to use CAS for Off heap operation.

In this blog i will share my experiment of Memory mapped using CAS.

How?

How to get memory address

MappedByteBuffer is using DirectByteBuffer, which is off heap memory. So it is possible to get virtual address of memory and use unsafe to perform CAS operation. Lets look at the code.

FileChannel fc = new RandomAccessFile(fileName, "rw").getChannel();
// Map 8 bytes for long value.
mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, 8);
startAddress = ((DirectBuffer) mem).address();

Above code create memory mapped file of 8 bytes and get the virtual address. This address can be be used to read/write content of memory mapped file.

How to Write/read in thread safe manner

public boolean increment() {
		long orignalValue = readVolatile(startAddress);
		long value = convert(orignalValue);
		return UnsafeUtils.unsafe.compareAndSwapLong(null, 
				startAddress,orignalValue, convert(value + 1));
	}

	public long get() {
		long orignalValue = readVolatile(startAddress);
		return convert(orignalValue);
	}

	// Only unaligned is implemented
	private static long readVolatile(long position) {
		if (UnsafeUtils.unaligned()) {
			return UnsafeUtils.unsafe.getLongVolatile(null, position);
		}
		throw new UnsupportedOperationException();
	}

	private static long convert(long a) {
		if (UnsafeUtils.unaligned()) {
			return (UnsafeUtils.nativeByteOrder ? a : Long.reverseBytes(a));
		}
		throw new UnsupportedOperationException();
	}

Important function to look are readVolatile and increment readVolatile reads directly from memory and increment is using unsafe to perform CAS on the address obtained from MemoryByteBuffer.

Performance

Some performance numbers from my system. Each thread increments counter 1 Million times.

Counter+Stats

Performance of counter is decent , as number of threads are increased CAS failures starts to happen and performance starts to degrade. Performance of these counter can be improved by having multiple segment to reduce write contention. I will write about it in next blog.

Conclusion

  • Memory mapped file is very powerful, it can be used to developed lot of things like off heap collections, IPC, Off heap thread coordination etc.
  • Memory mapped file opens gates for GC less programming.

All the code used in this blog is available on github.

Reference: Off Heap concurrent counter from our JCG partner Ashkrit Sharma at the Are you ready blog.
Related Whitepaper:

Software Architecture

This guide will introduce you to the world of Software Architecture!

This 162 page guide will cover topics within the field of software architecture including: software architecture as a solution balancing the concerns of different stakeholders, quality assurance, methods to describe and evaluate architectures, the influence of architecture on reuse, and the life cycle of a system and its architecture. This guide concludes with a comparison between the professions of software architect and software engineer.

Get it Now!  

Leave a Reply


− 4 = two



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close