-
Notifications
You must be signed in to change notification settings - Fork 104
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
Introducing class and methods to log memory usage in scope #2640
Introducing class and methods to log memory usage in scope #2640
Conversation
@dotfloat your input on the approach would be appreciated in particular about how to test this in an effivient way. |
Codecov Report
@@ Coverage Diff @@
## main #2640 +/- ##
==========================================
- Coverage 64.90% 64.80% -0.10%
==========================================
Files 649 651 +2
Lines 53841 53897 +56
Branches 4540 4609 +69
==========================================
- Hits 34944 34927 -17
- Misses 17443 17507 +64
- Partials 1454 1463 +9
Continue to review full report at Codecov.
|
#ifndef ERT_MEMORY_H | ||
#define ERT_MEMORY_H | ||
|
||
#ifdef __cplusplus |
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.
The purpose of this extern "C"
is to avoid name mangling - i.e. to get "simple" symbol names which can be accessed from the cwrap
machinery. Unless you intend to instantiate this class from Python I would suggest removing the extern "C"
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.
Good point
|
||
private: | ||
std::shared_ptr<ert::ILogger> m_logger; | ||
std::string m_message; |
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.
Suggestion: I would create a small struct:
struct meminfo {
long current;
long max;
meminfo();
}
with the implementation of the constructor in the .cpp
file. The benefits from this:
- You codify the fact that you are interested in a snapshot of the memory situation - not individual independent values.
- You avoid exporting the
SystemRamFree(),....
functions (export a struct instead through). - You can test the OS call functions by creating
memino
instance - without offering public access into the logger class.
For the member variable in the logger I would avoid the use of cur
or similar abbreviations for current, when that information is used in the destructor it represents the initial memory information.
My previous experience with memory logging like this is that it is difficult to get conclusive results, unless you are doing something really extreme (e.g. spikes of 98% memory usage) the OS will be in there and do it's thing. I think memory reporting by the kernel is a topic which has been worked on - so hopefully you will have more success.
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.
Mmmm... guess we have slightly different views on some aspects here... 😄
The SystemRawFree()...
etc are meant to be exposed as utilities. Assuming they produce correct results, they may be useful in developing code which adapts to memory-requirements, not only for logging.
The class utilizes the RAII-promise of C++ to easily "checkpoint" mem-usage and gain insight into how a code-block (scope) uses memory (note logging of max-mem). I.e. we log current and max mem-usage on entering a scope and the same on exiting the scope. Thus, if max-mem changed significally we know the the scope allocated (and hopefully released) a lot of memory, providing insight without the need for analyzing code in detail.
I'll have a look at the prefix of member-vars - names may be clearer, yes...
Finally, I completely agree on the complexity of determining memory usage in general. This is an initlal approach which I believe we can get some useful info from it. It's a shame this will only work in Linux, but I guess that is fair enough for real life.
39b6fb5
to
924f375
Compare
Verified manually by |
I am honored to be requested for review; I have some suggestions/opinions. I do not expect you to follow them, and in particular I do not intend to discuss these discussion further - unless explicitly encouraged to do so. If you think the ideas have some merit - that is good; otherwise no harm done. |
I am a bit uncertain if you understood what I meant - so just to be certain we are discussing the same suggestion I'll spell it out in some detail: //memory.hpp
struct meminfo {
long process_memory;
long max_memory;
meminfo();
}
class ScopedMemoryLogger {
public:
ScopedMemoryLogger(logger_type& logger, const std::string& msg)
: m_logger(logger)
, m_init_mem(meminfo{})
{}
~ScopedMemoryLogger();
private:
logger_type& logger;
meminfo m_init_mem;
};
// memory.cpp
meminfo::meminfo()
: max_memory( ProcessMaxMemory() )
, process_memory( ProcessMemory() )
{} The advantage of this is:
|
@joakim-hove I appreciate your input as much as as your pragmatism. Upcoming patch will definitely contain some of your suggestions! |
d8c8af5
to
2f0d665
Compare
Test ideas:
Note: Allocation is very complex so these tests may give false negatives. Linux only sees the pages that the |
2f0d665
to
6291765
Compare
test ert please |
test libres please |
test ctest please |
Thanks for ideas @pinkwah! I agree that this will be hard to test deterministically... Have we established some way to mock C/C++ functions using Catch2? We could mock Or can we venture into (ab)using |
044b63e
to
3575647
Compare
The test in this latest patch is deterministic and I think it is sufficient. |
890e8f3
to
7531d37
Compare
test ert please |
1 similar comment
test ert please |
test this please |
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.
Minor revisiony stuff. Looks pretty good otherwise!
7531d37
to
4aa1e23
Compare
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.
💯 good job!
Issue
Resolves #2353
Approach
Introduced some methods to report different aspects of memory usage. Wrapped methods by RAII to log mem-usage in process on entering and exiting scope. Example of usage in
smoother_update