Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Determine if an array is memory-mapped?

137 views
Skip to first unread message

Douglas Bates

unread,
May 13, 2015, 2:29:44 PM5/13/15
to julia...@googlegroups.com
Is there a way to determine if an array is memory-mapped? I know that if the file was opened read-only then trying to write to the array throws an error but that seems a rather heavy-handed approach.  Perhaps this is not possible because the memory-mapped file behaves just like a chunk of memory.

Isaiah Norton

unread,
May 14, 2015, 2:01:29 PM5/14/15
to julia...@googlegroups.com
As far as I can tell, we don't store this information [1] -- mmap_array simply calls pointer_to_array [2] on the address returned by mmap, and at that point the array is just like any other created from a pointer.

Your idea to to try writing to the array may actually be the least heavy-handed possibility I can think of! But for the sake of ...completeness... here are a few other ideas:

- mmap_array attaches a finalizer to the object. I don't think we have an API to look at finalizers right now, but we could potentially add a few lines of C code in gc.c to introspect whether a finalizer is attached to an object, then see whether that finalizer is calling Libdl.munmap.

- find an API that allows to check whether a pointer is mmap'd (unfortunately I didn't see an obvious candidate in a quick scan of the POSIX shm docs)

- relatedly: on Linux, you could look at /proc/PID/maps and see if the pointer comes from one of those regions (see e.g. [4])

Tim Holy

unread,
May 14, 2015, 3:23:38 PM5/14/15
to julia...@googlegroups.com
I've intermittently wondered whether one should wrap a mmapped array in a
type; certainly, you could bypass the high-level code and write your own
implementation of `mmap_array` that does so.

Best,
--Tim

On Thursday, May 14, 2015 02:01:26 PM Isaiah Norton wrote:
> As far as I can tell, we don't store this information [1] -- mmap_array
> simply calls pointer_to_array [2] on the address returned by mmap, and at
> that point the array is just like any other created from a pointer.
>
> Your idea to to try writing to the array may actually be the least
> heavy-handed possibility I can think of! But for the sake of
> ...completeness... here are a few other ideas:
>
> - mmap_array attaches a finalizer to the object. I don't think we have an
> API to look at finalizers right now, but we could potentially add a few
> lines of C code in gc.c to introspect whether a finalizer is attached to an
> object, then see whether that finalizer is calling Libdl.munmap.
>
> - find an API that allows to check whether a pointer is mmap'd
> (unfortunately I didn't see an obvious candidate in a quick scan of the
> POSIX shm docs)
>
> - relatedly: on Linux, you could look at /proc/PID/maps and see if the
> pointer comes from one of those regions (see e.g. [4])
>
> [1] https://github.com/JuliaLang/julia/blob/master/src/julia.h#L151-L162
> [2]
> https://github.com/JuliaLang/julia/blob/861f02712eb4b41c08fed3f21c5a4206b8d6
> 69bc/base/mmap.jl#L78 [3]
> https://github.com/JuliaLang/julia/blob/861f02712eb4b41c08fed3f21c5a4206b8d6
> 69bc/base/mmap.jl#L79 [4]
Reply all
Reply to author
Forward
0 new messages