-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Cache common/common_linux.Virtualization() #942
Conversation
By assuming virtualization environment won't change during a the program's runtime, we can cache common/common_linux.Virtualization() with a simple map to reduce amount of system calls. I first mentioned this issue at #890 (comment)
I wanted to compare profiling results of this optimization. Before optimization of Virtualization(), there is a distinct compute weight on fillFromStatWithContext which calls Virtualization() After optimization, we can clearly see that the compute weight is reduced and spread to the other functions. |
I think these kind of optimization should be implemented in Application side, not library side. |
@shirou could you elaborate on that? Most of the compute is spent with ReadLines on Virtualization which is in internal/common package. How could we optimize it from application side? |
or whatever, in your application. |
@shirou thank you for your reply. However your answer would be the case if it was my app who was calling Virtualization information, but it isn't. The function that calls common/common_linux.Virtualization() is fillFromStatWithContext() which is called by many process.Process methods in process_linux. We can't change private functions from the application side. |
In fact the call to I fear caching this will make it harder to run unit tests for this function (we would have to hook to this new variable |
ah, sorry for my misunderstanding. But as @Lomanic said, I also cache is not good for library. I will try to open a PR about using |
@Lomanic #857 mention a device can start with wrong date but the question we must ask for this case is can a device start with wrong virtualization data? I don't think it can but of course we must be certain. I'm not going to comment on unit tests as I'm not sure on potential effects of caching on unit tests. |
I think maybe we should cache this value in fact, for unit tests we can work around this by resetting this internal variable to nil if we want to call Virtualization() more than once. This would really improve performances for process.Processes() (see #842 (comment)) |
Here's a proper benchmark git -C "$(go env GOPATH)/src/github.com/shirou/gopsutil" checkout master
git -C "$(go env GOPATH)/src/github.com/shirou/gopsutil" reset --hard origin/master
git -C "$(go env GOPATH)/src/github.com/shirou/gopsutil" clean -f -d
cat <<EOF >> "$(go env GOPATH)/src/github.com/shirou/gopsutil/process/process_test.go"
func BenchmarkNewProcess(b *testing.B) {
checkPid := os.Getpid()
for i := 0; i < b.N; i++ {
NewProcess(int32(checkPid))
}
}
EOF
printf "\nBefore change:\n"
go test -bench=. github.com/shirou/gopsutil/process
curl -sL https://github.com/shirou/gopsutil/pull/942.diff | git apply
printf "\nAfter change:\n"
go test -bench=. github.com/shirou/gopsutil/process Results are telling:
|
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.
removed explicit nil
@Lomanic removed explicit nil as requested. Thank you for benchmarking results. Looking forward for the merge. |
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.
As I said, I don't like cache in a library, but I don't have any other good solution, so I approve this PR. Thank you for your contribution!
I tested this fix in telegraf and found that it makes performance similar to before #837 was merged. Will this PR make it into a release soon? We'd like to include it in telegraf 1.16.0 which is planned for next week. |
@Lomanic sorry for interrupting your comment, but I think your request is resolved. So I merge this PR. Thank you for your review and contribution! |
gopsutil is monthly release. After this PR merged, I have released |
By assuming virtualization environment won't change during a the program's runtime, we can cache common/common_linux.Virtualization() with a simple map to reduce amount of system calls. I first mentioned this issue at 890