1

I'm trying to code an IPC solution through /dev/shm.

@SK-logic gave me some pointers in the comments here: Chronicle: How to optimize memory-mapped files for low-latency?

My doubt is: Should I use a MappedByteBuffer or just a plain FileChannel?

With a MappedByteBuffer I can use sun.misc.Unsafe and have direct access to memory. That's fantastic because Unsafe gives me methods like getLongVolatile (in addition to getLong) and putLongVolatile (in addition to putLong). Is that even possible if I use a plain FileChannel? How would I avoid reading cached data from the CPU cache with a plain FileChannel reading from /dev/shm/? Do I have to configure something in the operating system for volatile reads and writes from /dev/shm? What? Where? How? :)

What is the correct way of doing Java IPC through /dev/shm? Plain FileChannel? MappedByteBuffer?

Below how I get the pointer to memory through sun.misc.Unsafe:

    try {
        this.raf = new RandomAccessFile(file, "rw");
        this.fileChannel = raf.getChannel();
        this.mappedBuffer = this.fileChannel.map(MapMode.READ_WRITE, 0, size);
    } catch (Exception e) {
        throw new RuntimeException("Could not mmap file to memory: " + filename + " " + size, e);
    }
    
    try {
        addressField = Buffer.class.getDeclaredField("address");
        addressField.setAccessible(true);
        this.pointer = (long) addressField.get(this.mappedBuffer);
    } catch(Exception e) {
        throw new RuntimeException("Could not get off-heap pointer!", e);
    }

1 Answers1

0

Chronicle Queue uses Unsafe for thread-safe memory access for heap and direct memory.

While this works, the JVM doesn't provide any guarantees as to how your system will behave. We test on Intel X64, AMD, and ARM processors.

Rather than writing this all yourself, why not try Chronicle Map or Queue to do this for you?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I'm using Chronicle already, but I'm curious to know how it is done. Do you use a `FileChannel` or a `MappedByteBuffer` with `Unsafe`? According to what I've heard from @SK-logic, `MappedByteBuffer` has overhead, even inside `/dev/shm`. – Roger Kasinsky Feb 10 '22 at 15:28
  • Also, when you said `heap memory` above, you probably meant `direct memory`. – Roger Kasinsky Feb 12 '22 at 03:38
  • @RogerKasinsky MappedByteBuffer has a small overhead but lacks thread safe operations or 64 bit offsets. Bytes supports 63 bit offsets, thread safe and low/zero overhead depending on your needs. – Peter Lawrey Feb 14 '22 at 18:54
  • @rogerkasinsky we support accessing Bytes for byte[], portions of objects, direct anonymous memory, abd memory mapped files. – Peter Lawrey Feb 14 '22 at 18:56