-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[GR-51851] Add initial support for native memory tracking (NMT mallocs) #7883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… memory allocated outside java code.
… set up. add missing files
|
Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application. When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated. If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public. |
100c1c7 to
d50a711
Compare
wip handle preinit clean up and add some manual labels minor cleanup and fixes Label call sites and clean up mtThread Remove virtual memory tracking code wip remove more vmem tracking code comments and refactor more small fixes, refactore, comments comment unchecked cast label Unsafe callsite. Clean up. javadoc
d50a711 to
58cfa1d
Compare
|
@fniephaus do you know why the OCA check is failing for me now? |
|
@roberttoyonaga sorry, there has been an issue on our side. It's now resolved, the check is passing. thank you for your contribution! |
|
|
||
| private final UninterruptibleEntry[] table; | ||
| private int size; | ||
| protected int size; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this protected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it protected so that com.oracle.svm.core.nmt.PreInitTable can decrement the size when it frees nodes using raw LibC https://github.com/oracle/graal/pull/7883/files#diff-87344ca5d4dbcfcc86a41d2c665f97dba67181f1a0ac4c0428b4a9799bc23b2dR77
I've added @Override and a brief javadoc on some of the methods here for more clarity.
|
@roberttoyonaga : thanks, I started integrating this PR. |
Thank you @christianhaeubl. What do you think about allowing enabling NMT at runtime vs buildtime? Also, I think it might be a good idea to pad the malloc header with 4B so it's a total of 16B for alignment reasons. I haven't done this yet, but should be easy and not have any side effects other than increase memory usage. |
|
I think that we should only have a hosted option for NMT (i.e., if the option is enable at image build-time, NMT is used at run-time). As far as I can see, the malloc header is already 16 bytes (is rounded to wordSize). |
Ok, in that case, we can remove much of the code in NativeMemoryTracking.java
Right, I forgot about that |
Summary
As a first step to supporting native memory tracking (NMT), this PR adds support for tracking mallocs/calloc/realloc. Virtual memory tracking support will be added in a separate PR as it is mostly independent of this code.
Related issue: #7631
This already includes the cleanup from: #7669
Implementation
This PR adopts a similar approach to NMT in Hotspot. It uses "malloc headers". A "malloc header" is used to cache metadata about the native allocation. To do this, a small amount of additional space is requested contiguous to the user allocation. Malloc headers are always the same size. This metadata is later used to update the memory tracking once the block is freed.
The usage of malloc headers requires certain precautions. Please see the javadoc on class
NativeMemoryTrackingfor more info.Alternative Approaches
One alternative approach to malloc headers is using a big look-up table to cache metadata. This approach has the advantage of being more robust. It does not have the disadvantages of malloc headers described in the
NativeMemoryTrackingjavadoc.However, it has a few big disadvantages of its own:
An alternative to enabling NMT at runtime is to decide whether to enable NMT at build-time. This would allow us to bypass a lot of complexity by eliminating the special handling done to accommodate a pre-init phase. This is a possibility not available in Hotspot. In this PR, I've decided to implement the handling required to postpone this decision until runtime, not because I firmly believe it's the best choice, but because it might help facilitate discussion on whether its worth the added flexibility. The code that handles pre-init is relatively independent and can be easily removed.
The advantages of enabling at runtime are:
The disadvantages are:
Limitations
mtTest,mtNMT,mtTracing,mtThread,mtOther). Everything else is aggregated into the defaultmtNonefor now. Manual labeling of other call sites can be done in future PRs.