-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-150114: Use get_process_memory_usage() in memory watchdog #150402
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
Merged
+43
−33
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7a72e31
gh-150114: Use get_process_memory_usage() in memory watchdog
vstinner 26ef459
Fix Lint: remove now unused import (get_pagesize)
vstinner f523453
Fix memory_watchdog.py usage
vstinner 3a13b66
Add a comment to explain why sys.stdout.write() is used
vstinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,40 @@ | ||
| """Memory watchdog: periodically read the memory usage of the main test process | ||
| and print it out, until terminated.""" | ||
| # stdin should refer to the process' /proc/<PID>/statm: we don't pass the | ||
| # process' PID to avoid a race condition in case of - unlikely - PID recycling. | ||
| # If the process crashes, reading from the /proc entry will fail with ESRCH. | ||
|
|
||
|
|
||
| import sys | ||
| import time | ||
| from test.support import get_pagesize | ||
|
|
||
|
|
||
| while True: | ||
| page_size = get_pagesize() | ||
| sys.stdin.seek(0) | ||
| statm = sys.stdin.read() | ||
| data = int(statm.split()[5]) | ||
| sys.stdout.write(" ... process data size: {data:.1f}G\n" | ||
| .format(data=data * page_size / (1024 ** 3))) | ||
| sys.stdout.flush() | ||
| time.sleep(1) | ||
| from test.libregrtest.utils import get_process_memory_usage | ||
|
|
||
|
|
||
| ONE_GIB = (1024 ** 3) | ||
|
|
||
|
|
||
| def watchdog(pid): | ||
| while True: | ||
| mem = get_process_memory_usage(pid) | ||
| if mem is None: | ||
| # get_process_memory_usage() is not supported on the platform, | ||
| # or something went wrong. Exit since the next call is likely to | ||
| # fail the same way. | ||
| return | ||
|
|
||
| # Prefer sys.stdout.write() to print() to use a single write() syscall. | ||
| # print(msg) calls write(msg.encode()) and then write(b"\n"). | ||
| sys.stdout.write(f" ... process data size: {mem / ONE_GIB:.1f} GiB\n") | ||
|
cmaloney marked this conversation as resolved.
|
||
| sys.stdout.flush() | ||
| time.sleep(1) | ||
|
|
||
| def main(): | ||
| if len(sys.argv) != 2: | ||
| print(f"usage: python {sys.argv[0]} pid") | ||
| sys.exit(1) | ||
| pid = int(sys.argv[1]) | ||
|
|
||
| try: | ||
| watchdog(pid) | ||
| except KeyboardInterrupt: | ||
| pass | ||
|
|
||
| if __name__ == "__main__": | ||
|
cmaloney marked this conversation as resolved.
|
||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.