justinstenning/SharedMemory

Working on Linux?

Opened this issue · 6 comments

Do SharedBuffers work in Linux? Anyone successfully used it? As far as I understand it's based on named MemoryMaps, which are not supported in. NET Core under Linux.

Is there any alternative for shared memory with. NET Core under Linux?

Got SharedArray to work on Linux with the following patches:

  • in SharedBuffer.Open(), use MemoryMappedFile.CreateFromFile(), which is supported - and has the additional advantage of adding persistence to the buffer, something I needed
  • in BufferWithLocks, replace usage of EventWaitHandle (which depends on named mutexes, also unsupported on Linux, though perhaps that has changed - see https://github.com/dotnet/coreclr/issues/3422) with a custom InterLocked.Exchange / spinWait locking

I only use SharedArray on Linux, so did the latter on SharedArray level only (via a secondary MMF of ints, which had the added benefit of fine-grained, per-cell locking), but could probably be adapted to BufferWithLocks in general.

I'll try to pull this together as a patch if there is interest, need to it dig out. Overall great library, using parts of it in a demanding server app (market data feed handler) without any issues - SharedArray for in-memory cache on Linux and CircularBuffer on Windows consumers to isolate GC-intolerant high bandwidth multicast consumption from rest of the client apps.

May want to also check out https://github.com/AdaptiveConsulting/Aeron.NET - Martin Thomspon of LMAX Disruptor fame is involved.

@mkosina I would be interested in looking at integrating support for Linux.

I’m not sure how I feel about spin locks, but if there is no alternative for Linux then fair enough.

Would you be able to prepare a pull request or point to a repo that has these changes?

Thanks

@mkosina Can you share experiences on when Linux flushes those mappings to disc? Will it flush if I delete the file before closing the map? Will it behave like memory maps backed by the page file and not write dirty pages as long as there is enough memory?

I need more than 1GB/s of shared memory bandwith. Any IO from those mappings is a serious problem for me.

My short investigation into this so far has found that the .NET core implementation on Linux only supports shared memory backed by files. I believe there is a memory only option within Linux, however this will require a custom implementation to support it.

As a note: cross-process mutex's are supported on Linux/POSIX with "Global" prefix in name