Skip to content

Unsychronized MMapDataAccess - #2889

Merged
karussell merged 4 commits into
masterfrom
unsych_mmap
Nov 1, 2023
Merged

karussell merged 4 commits into
masterfrom
unsych_mmap

Conversation

@karussell

@karussell karussell commented Oct 18, 2023 •

Copy link
Copy Markdown
Member

Since #2827 we can now use getBytes with absolute indices and so the MMapDataAccess should be read-thread-safe even without synchronized. Note, that writing is still not thread-safe (but this is the same for RAMDataAccess) as e.g. a value change for an EncodedValue might still involve several method calls, i.e. the synchronization for write-thread-safety would be required on a level above the DataAccess.

@karussell karussell added this to the 9.0 milestone Oct 18, 2023
@karussell
karussell temporarily deployed to benchmarks October 18, 2023 13:25 — with GitHub Actions Inactive
@otbutz

otbutz commented Oct 18, 2023 •

Copy link
Copy Markdown
Contributor

We could also switch to a MethodHandle while we're at it:

    static final MethodHandle CLEANER;
    static {
        MethodHandle handle;
        try {
            Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
            MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(unsafeClass, MethodHandles.lookup());
            MethodType type = MethodType.methodType(void.class, ByteBuffer.class);
            handle = lookup.findVirtual(unsafeClass, "invokeCleaner", type);
            VarHandle unsafeField = lookup.findStaticVarHandle(unsafeClass, "theUnsafe", unsafeClass);
            Object unsafe = unsafeField.get();
            handle = handle.bindTo(unsafe);
        } catch (Exception e) {
            LOGGER.error("Failed to obtain method handle for ByteBuffer cleaner.", e);
            handle = null;
        }
        CLEANER = handle;
    }

    // ....

    public static void cleanMappedByteBuffer(final ByteBuffer buffer) {
        if (CLEANER == null) {
            throw new RuntimeException("Cleaner is not available");
        }

        try {
            CLEANER.invokeExact(buffer);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

@otbutz

otbutz commented Oct 19, 2023

Copy link
Copy Markdown
Contributor

#2893

@karussell

karussell commented Oct 31, 2023 •

Copy link
Copy Markdown
Member Author

I'm really unsure how to test MMAP properly as with synchronized the problem was concurrent users and for MMAP also the randomness of the routes might be a problem (as it has to load the data into RAM). The best or most realistic would be probably always the same N routes from our logs. But the following test with static 2 points shows at least a reproducible difference. I use graphhopper (with a coverage of sachsen, -Xmx6g, 32g physical RAM) and drop the caches everytime I start a new load test with 200 concurrent users, i.e.:

  • stop GH
  • sudo sysctl vm.drop_caches=3
  • start GH
  • ./run http://127.0.0.1:8989 -p route -d 30s -u 200 --point 'point=51.336192%2C14.08213&point=50.686456%2C12.478126'

The new MMAP config comes closer to RAM speed (avg response time in ms):

Type Avg Min Max
RAM 15 3 779
master MMAP 520 7 1080
new MMAP 50 3 1090

For real world setups, where the full RAM can be used the following config might be used:

  graph.dataaccess.default_type: MMAP
  graph.dataaccess.mmap.preload..*: 100

Which preloads everything into RAM. Then it is even a bit better:

Type Avg Min Max
master MMAP 280 6 780
new MMAP 35 3 1010

So we shouldn't draw quick conclusions as I probably did not test it correctly, but the new settings seems to do fine in general :)

@karussell
karussell merged commit aa00539 into master Nov 1, 2023
@karussell
karussell deleted the unsych_mmap branch November 1, 2023 10:07
@michaz

michaz commented Nov 3, 2023

Copy link
Copy Markdown
Member

Nice!

This branch was previously deployed

1 inactive deployment
benchmarks — 9eea7798 Deployed Oct 31, 2023 by karussell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants