OpenHFT Java Lang project

Overview

OpenHFT/Java Lang started as an Apache 2.0 library to provide the low level functionality used by Java Chronicle without the need to persist to a file.

This allows serializable and deserialization of data and random access to memory in native space (off heap)  It supports writing and reading enumerable types with object pooling. e.g. writing and reading String without creating an object (if it has been pooled).  It also supports writing and read primitive types in binary and text without creating any garbage. Small messages can be serialized and deserialized in under a micro-second.

Recent additions

Java Lang supports a DirectStore which is like a ByteBuffer but can be any size (up to 40 to 48-bit on most systems)  It support 64-bit sizes and offset. It support compacted types, and object serialization. It also supports thread safety features such as volatile reads, ordered (lazy) writes, CAS operations and using an int (4 bytes) as a lock in native memory.

Testing a native memory lock in Java

This test has one lock and a value which is toggled.  One thread changes the value from 0 to 1 and the other switches it from 1 to 0.  This goes around 20 million times, but has been run for longer

        final DirectStore store1 = DirectStore.allocate(1L << 12);
        final int lockCount = 20 * 1000 * 1000;
        new Thread(new Runnable() {
            @Override
            public void run() {
                manyToggles(store1, lockCount, 1, 0);
            }
        }).start();

        manyToggles(store1, lockCount, 0, 1);

        store1.free();

The manyToggles method is more interesting. Note is using the 4 bytes at offset 0 as a lock.  You can arrange any number of locks in native space this way.  E.g. you might have fixed length records and want to be able to lock them before updating or access them.  You can place a lock at the "head" of the record.

    private void manyToggles(DirectStore store1, int lockCount, int from, int to) {
        long id = Thread.currentThread().getId();
        assertEquals(0, id >>> 24);
        System.out.println("Thread " + id);

        DirectBytes slice1 = store1.createSlice();

        for (int i = 0; i < lockCount; i++) {
            assertTrue(
                slice1.tryLockNanosInt(0L, 10 * 1000 * 1000));
            int toggle1 = slice1.readInt(4);
            if (toggle1 == from) {
                slice1.writeInt(4L, to);
            } else {
                i--;
            }
            slice1.unlockInt(0L);
        }
    }

The size of the DataStore and the offsets within it are long, allowing you to allocate a continuous block of native memory into the many GB, and access it as you required.

On my 2.6 GHz i5 laptop I get the following output for this test

Contended lock rate was 9,096,824 per second

This looks great but under heavy contention, one thread can be staved out.  This is more useful for lots of locks and lower contention.  Note: if I drop the timeout from 10 ms to 1 ms, it eventually fails meaning sometimes it takes more then 1 ms to get a lock !

Conclusion

The Java Lang library is taking the step of making it easier to use native memory with the same functionality available on the heap.  The language support is not as good, but if you need to store say 128 GB of data you will get a much better GC behaviour using off heap memory.


Comments

  1. Hi Peter! (big fan of your work)

    I was reading your DirectStore on github, and saw that you register a sun.misc.Cleaner on the store, a cleaner that can be called explicitly.

    But actually if the cleaner is not called explicitly I think it will never get called at all (even if the direct store is not referenced in the application anymore). Because the runnable cleanup code in your cleaner is an anonymous class, that will maintain a strong reference on the store itself, preventing it from ever becoming "phantom reachable".


    cleaner = Cleaner.create(this, new Runnable() {
    @Override
    public void run() {
    if (address > 0)
    NativeBytes.UNSAFE.freeMemory(address);
    address = DirectStore.this.size = 0;
    }
    });



    Cheers,
    -Antoine

    ReplyDelete

Post a Comment

Popular posts from this blog

Low Latency Microservices, A Retrospective

Unusual Java: StackTrace Extends Throwable

System wide unique nanosecond timestamps