Server Environment
- Linux/RedHat
- 6 cores
- Java 7/8
About application :
- We are working on developing a low latency (7-8 ms) high speed trading platform using Java
- There are 2 modules A & B each running on its own JVM
- B gets data from A
Architecture:
- we have made use of MemoryMaps & Unsafe. In this case, Module A writes into a memory mapped file & Module B reads from the file (both are holding address location to the file)
- We went ahead & used an endless while-loop to continue reading till the desired value is obtained from the memory mapped file
Problem
- CPU utilization shoots up to 100% & remains the same till its life cycle
Question :
Is there a more sophisticated way to keep polling for a value in the memory mapped file which involves minimum overheads, minimum delay & minimum CPU utilization? Note that every microsecond delay will deteriorate the performance
Code Snippet
Code snippet for the Module B (endless while-loop which polls & reads from the memory mapped file) is below
FileChannel fc_pointer = new RandomAccessFile(file, "rw").getChannel();
MappedByteBuffer mem_file_pointer =fc_pointer.map(FileChannel.MapMode.READ_ONLY, 0, bufferSize);
long address_file_pointer = ((DirectBuffer) mem_file_pointer).address();
while(true)
{
int value_from_memory_mapped_file = unsafe.getInt(address_file_pointer);
if (value_from_memory_mapped_file .. is different from the last read value)
{
//do some operation....
//exit the routine;
}
else
{
continue;
}
}//end of while