Skip to content
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

[Linux] Performance optimization for Process.memory_full_info() #2107

Closed
itamarst opened this issue May 13, 2022 · 2 comments · Fixed by #2108
Closed

[Linux] Performance optimization for Process.memory_full_info() #2107

itamarst opened this issue May 13, 2022 · 2 comments · Fixed by #2108

Comments

@itamarst
Copy link

Summary

  • OS: Linux
  • Type: performance

Description

Current Process().memory_full_info() reads /proc/<pid>/smaps, and then sums across maps. This can be slow if there are many maps.

Linux provides a /proc/<pid>/smaps_rollup that is the information in smaps summarized across the whole process: exactly what this function is calculating. For processes with large number of mmaps, using this file directly would reduce the cost of memory_full_info() significantly.

This was apparently added to Linux sometime in 2017 or 2018.

It's the same format, so you could the exact same code, even, just open that file instead if it exists.

@giampaolo
Copy link
Owner

Well, this is awesome.

@giampaolo
Copy link
Owner

giampaolo commented May 18, 2022

I've just implemented this in #2108.

Without patch:

~/svn/psutil {linux-smaps-rollup}$ python3 -m timeit -s "import psutil; p = psutil.Process()" "p.memory_full_info()"
500 loops, best of 5: 518 usec per loop

With patch (5 times faster):

~/svn/psutil {linux-smaps-rollup}$ python3 -m timeit -s "import psutil; p = psutil.Process()" "p.memory_full_info()"
2000 loops, best of 5: 111 usec per loop

make test-memleaks suite, who heavily rely on Process.memory_full_info(), also received a nice speedup:

Before patch:

$ make test-memleaks
----------------------------------------------------------------------
Ran 99 tests in 1.646s

OK (skipped=9)
SUCCESS

After patch:

$ make test-memleaks
----------------------------------------------------------------------
Ran 99 tests in 1.195s

OK (skipped=9)
SUCCESS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants