Repository navigation
Ensure that all Bigarray memory managed by the GC is tracked by Memprof - #13675
Conversation
|
I agree with your criterion:
In this PR you sample a number of blocks equivalent to the size of the mapping, so you are seeing the memory of the file as being managed by the GC. However, neither RAM nor even disk memory is being freed by the finalizer (only virtual memory). Is this what you intend? Maybe I am missing the details from a specific case you have in mind. |
| } | ||
| asize = SIZEOF_BA_ARRAY + num_dims * sizeof(intnat); | ||
| res = caml_alloc_custom(&caml_ba_mapped_ops, asize, 0, 1); | ||
| mem_bytes = num_elts * caml_ba_element_size[flags & CAML_BA_KIND_MASK]; |
There was a problem hiding this comment.
If you did this after the initialization of b (so before the return resbelow), I think this could reuse caml_ba_byte_size:
mem_bytes = caml_ba_byte_size(b);
mem_words = (mem_bytes + sizeof(value) - 1) / sizeof(value);
caml_memprof_sample_block(res, mem_words, mem_words, CAML_MEMPROF_SRC_CUSTOM);There was a problem hiding this comment.
That's much simpler, thanks.
This is not really true. In a normal allocation (e.g. when the heap expands), typical OS behaviour is:
For a memory-mapped file, the situation is exactly the same, except that it begins life in the "swapped out" stage. If
The immediate use-case is a couple of programs at Jane Street whose memory consumption consists in a large part of calls to I can imagine that there are programs where you might want to profile memory use and not count calls to |
|
Thank you @stedolan for providing me with these helpful details. How about introducing a specific allocation source for file-backed memory? If I can try to rephrase your point (at least on Linux), it seems that the crucial details are that memory pressure causes both filesystem paging and swapping (in proportions that actually depend on the OS configuration), and that the page cache reclamation is done with a higher priority for pages that underwent the msync+munmap calls (in case this was the last mapping). In this sense the finaliser indeed has an effect on available memory, even if it primarily concerns cached memory. It also seems important to track the memory usage for caching files in this way for the proper functioning of systems. Given the different nature of the memory usage, a different allocation source seems justified and it would actually make filtering easy and robust. (It still seems an important difference that with mmapped files, the cached memory can intentionally be less than the total file size, given that the program might not need to access the whole file (unlike your example of extending the heap, where all the pages are touched immediately by the runtime). I note that with Lastly, the Memprof interface is still described as experimental, but it might be good to make |
This is a good idea, done.
I thought about this but I don't think an extensible variant type ( |
ad4aba0 to
09e303c
Compare
|
I would just like to mention that there is a case (which may correspond to case 3) where a bigarray exists to correspond to a physical memory address corresponding to a device (hardware) and which should not be count by memprof. |
This does correspond to case 3 (it's |
|
Thanks for the separate kind. |
|
And to be clear my question was about |
gadmm
left a comment
There was a problem hiding this comment.
There is something missing the adaptation to the new Map_file memprof source (see below). Otherwise this looks fine to me.
| custom_words := !custom_words + info.size * Sys.word_size/8; | ||
| Some info.size | ||
| | _ -> | ||
| None |
There was a problem hiding this comment.
The test has not yet been adapted to the Map_file source
Specifically, this adds Gc.Memprof calls to Unix.map_file, and adds some tests (but does not otherwise change behaviour)
|
I merged @gadmm's review suggestions, and updated the test. This should be good to merge if the CI agrees. |
|
Some failures from the AppVeyor CI: |
|
|
407e7a7 to
07a8956
Compare
|
Is this allowed for 5.4, @Octachron? (given that the alpha isn't yet tagged, I would push for "yes") |
|
This feels really at the frontier between a bug fix and a new feature. |
Ensure that all Bigarray memory managed by the GC is tracked by Memprof (cherry picked from commit 820bc29)
There are a number of memory-management options available when using Bigarrays to represent off-heap memory:
Bigarray.createand friendsCAML_BA_MANAGEDfrom a C stub passing non-NULLdatatocaml_ba_createCAML_BA_EXTERNALfrom a C stubUnix.map_file, GC-freed: usingCAML_BA_MAPPED_FILEand a custom finaliser.It's not obvious exactly which of these should result in
Gc.Memprofevents.I think the right criterion that is that we should have
Gc.Memprofevents when the GC manages the lifetime of an allocation - that is, cases 1, 2 and 4 above. (In case 3, the user is managing the lifetime of the off-heap memory, and it may well extend beyond the point where the bigarray is finalised).(If you're unfamiliar with the API it might be surprising that
Unix.map_fileis GC-freed: there is noUnix.unmap_file, but instead the freeing is done by a GC finaliser as with normal Bigarrays).The behaviour of the runtime is already very close to the criterion identified above. The only difference is the lack of Memprof calls in
Unix.map_file, which this patch adds. That is the only functional change in this PR, but there is also a new test for this logic.