-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Reading from /proc is slow for very large servers #708
Comments
You can pass a |
OK I took a look at this and tried some benchmarks.
...and this is the benchmark script I've been using:
...and this is the result:
Summary: on python 2 using |
This is specifically a problem of large servers with many open sockets. In my case, the servers that had problems had around 70000 open sockets. In this case, /proc/net/tcp will be several MB large when reading, and reading byte by byte is a significant performance problem. The change I describe (setting buffer to 8192) solves the problem for our servers. |
Have you tried my patch?
What kind of speedup do you have? |
OK, I verified that in case of many connections we can have a substantial speedup. Benchmark script:
Before the patch (a079e90):
After the patch (a079e90):
|
…s not have any effect so it's better to let python decide what to do
Note: the speedup only affects Python 2 (not 3). |
* giampaolo/master: (148 commits) update doc add DEVNOTES.rst Add import-time tests for psutil one import per line use with lock: set global cpu vars to None if they can't be determined at import time add more tests giampaolo#717: ignore everything after the first occurrence of '\x00' instead of replacing '\x00' for the whole string fix giampaolo#717: [Linux] Process.open_files fails if deleted files still visible. giampaolo#715: don't crash at import time if cpu_times() fail for some reason. safety measure for ZombieProcess exc giampaolo#718: process_iter() thread safety giampaolo#708: use buffering for open() only on Python 2; on Python 3 this does not have any effect so it's better to let python decide what to do little speedup for system connections fix giampaolo#708 [Linux]: speedup psutil.net_connections() and psutil.Process.connections() linux refactoring: use a wrapper around open() for binary files update doc raise no memory err if malloc() fails giampaolo#714: [OpenBSD] return shared virtual mem add test for vmem total on freebsd ...
When using Python's default open() on large servers, reading from e.g. /proc/net/tcp can be very slow. The reason is that Python tries to determine the buffer size based on the file system it is reading from. In case of /proc, this effectively turns off buffering, and as a consequence listing e.g. open connections can take a minute or more on large servers. A solution is to use io.open() and explicitly set a buffer size:
It might be worthwhile changing the linux implementation accordingly.
The text was updated successfully, but these errors were encountered: