Add a primitive to compute the "transitive" heap size of a value#560
Add a primitive to compute the "transitive" heap size of a value#560alainfrisch merged 3 commits intoocaml:trunkfrom
Conversation
|
Any comment on the proposal? Is |
I think it's better than the |
|
I only had a very quick look (and am jetlagged) but it looks like caml_stat_alloc should probably be used instead of malloc, no? I'm a bit torn about Gc versus Obj. In some sense, if this is going to be a safe operation, there's no need for it to be in Obj. However it doesn't seem to quite fit into Gc either. |
I got inspired by extern.c which uses malloc. A quick grep-fight in byterun gives an inconclusive: Does anyone know how to choose?
|
byterun/obj.c
Outdated
| The real color is stored in the last 2 bits of the pointer in the | ||
| queue. (Same technique as in extern.c.) | ||
|
|
||
| The queue is stored as a linked list of blocks. Initially, |
There was a problem hiding this comment.
This line is confusing because there are two kinds of blocks.
|
I think |
|
@damiendoligez Can you comment about malloc vs caml_stat_alloc? |
2dc899f to
8f6556b
Compare
…ocks accessible from a given value.
|
I am really surprised by this merge. I was thinking that we were waiting for Damien's feedback, and that the technical discussion was not over, as I think the primitive you described is far from being as useful as you said, in its current state (at least, for a cache or whatever, I would expect the primitive to come with a threshold argument, that would stop the traversal if the size becomes greater than the threshold). |
|
Damien posted some (minor) comments, which I addressed. I guess that he would have said so if he was against the proposal. The threshold is indeed a good idea for use in caches, but the current version is already very useful for debugging purposes. |
|
@alainfrisch I think that one big difference is that if the runtime becomes unloadable, then |
|
CI fails with flambda. |
|
@drbo This means we should be using @alainfrisch minor comments don't really mean that I approve of everything else, especially when I don't add the "approved" label... |
|
@damiendoligez I would imagine so (not part of the core team). |
|
FWIW, we (the Frama-C team) have been using @backtracking's size library for a long time. This is more or less what is proposed here. Our experience is that more functions are needed very rapidly. In particular, we often need to compute "the size of this value except what is also reachable from that value". |
This would be quite easy to implement, but costly if "that" value itself is big. A variant is to "cut" only one (or several) specific values (but not blocks accessible from them). |
Not always. Only if you're happy with caml_stat_alloc aborting the current function if it runs out of memory. However, this can lead to memory leaks. Consider two out-of-heap allocations in sequence: If the second allocation runs out of memory (but not the first), an exception is raised and the space for |
|
OK, so don't use |
64235a3 flambda-backend: Change Float.nan from sNaN to qNaN (ocaml#466) 14a8e27 flambda-backend: Track GC work for all managed bigarray allocations (upstream 11022) (ocaml#569) c3cda96 flambda-backend: Add two new methods to targetint for dwarf (ocaml#560) e6f1fed flambda-backend: Handle arithmetic overflow in select_addr (ocaml#570) dab7209 flambda-backend: Add Target_system to ocaml/utils (ocaml#542) 82d5044 flambda-backend: Enhance numbers.ml with more primitive types (ocaml#544) 216be99 flambda-backend: Fix flambda_o3 and flambda_oclassic attributes (ocaml#536) 4b56e07 flambda-backend: Test naked pointer root handling (ocaml#550) 40d69ce flambda-backend: Stop local function optimisation from moving code into function bodies; opaque_identity fixes for class compilation (ocaml#537) f08ae58 flambda-backend: Implemented inlining history and use it inside inlining reports (ocaml#365) ac496bf flambda-backend: Disable the local keyword in typing (ocaml#540) 7d46712 flambda-backend: Bugfix for Typedtree generation of arrow types (ocaml#539) 61a7b47 flambda-backend: Insert missing page table check in roots_nat.c (ocaml#541) 323bd36 flambda-backend: Compiler error when -disable-all-extensions and -extension are used (ocaml#534) d8956b0 flambda-backend: Persistent environment and reproducibility (ocaml#533) 4a0c89f flambda-backend: Revert "Revert bswap PRs (480 and 482)" (ocaml#506) 7803705 flambda-backend: Cause a C warning when CAMLreturn is missing in C stubs. (ocaml#376) 6199db5 flambda-backend: Improve unboxing during cmm for Flambda (ocaml#295) 96b9e1b flambda-backend: Print diagnostics at runtime for Invalid (ocaml#530) 42ab88e flambda-backend: Disable bytecode compilers in ocamltest (ocaml#504) 58c72d5 flambda-backend: Backport ocaml#10595 from upstream/trunk (ocaml#471) 1010539 flambda-backend: Use C++ name mangling convention (ocaml#483) 81881bb flambda-backend: Local allocation test no longer relies on lifting (ocaml#525) f5c4719 flambda-backend: Fix an assertion in Closure that breaks probes (ocaml#505) c2cf2b2 flambda-backend: Add some missing command line arguments to ocamlnat (ocaml#499) git-subtree-dir: ocaml git-subtree-split: 64235a3
It is sometimes useful to know how "big" values are. This can serve as a debugging tool (to track the cause of excessive memory usage), but also in production to adjust cache heuristics (e.g. eject "big-but-cheap-to-compute" values sooner). It can also be useful to help people get a good feeling of how to design compact data structures and how not to break sharing of inner nodes.
So I propose to add such a function to the Obj module (to make it a bit scary to use). It computes the total size (in words) of blocks accessible from a given value. Statically allocated blocks are excluded (this can also serve to implement non-regression tests that check that a value is statically allocated).
The implementation was inspired by the technique used in extern.c: when a block is first traversed, its color is turned to "blue". We maintain a queue of pointers which serves as a worklist (blocks yet to inspect) and also to restore original colors. One word of internal storage is required per visited block. (I had a first implementation reusing data structures from extern.c, but they use at least two words per block.)
If this proposal is well received, I'll write some unit tests. [EDIT: done]