-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
container/common: Replace readUInt64 with optimized version #3406
base: master
Are you sure you want to change the base?
Conversation
This patch replaces the `readUint64` function with one that optimizes for reading uint64s from files only. It prevents unnecessary calls to `.Stat` because a valid uint64 can be 20 characters at most anyway. It also avoids a few allocations. Overall, benchmarking has shown to reduce the total CPU time by ~15%, which given a total usage of roughly 3% in the entire kubelet, this means we're reducing the kubelet baseline CPU by about ~0.45%.
Hi @brancz. Thanks for your PR. I'm waiting for a google member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
func readNBytesFromFile(file string, i int) []byte { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
// Ignore non-existent files | ||
if !os.IsNotExist(err) { | ||
klog.Warningf("readNBytesFromFile: Failed to open %q: %s", file, err) | ||
} | ||
return nil | ||
} | ||
defer f.Close() | ||
|
||
buf := make([]byte, i) | ||
n, err := io.ReadFull(f, buf) | ||
if err != nil { | ||
if err != io.EOF && err != io.ErrUnexpectedEOF { | ||
klog.Warningf("readNBytesFromFile: Failed to read %q: %s", file, err) | ||
} | ||
} | ||
|
||
return bytes.TrimSpace(buf[:n]) | ||
} |
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.
Can I have uni tests for this function, please?
/ok-to-test |
This patch replaces the
readUint64
function with one that optimizesfor reading uint64s from files only. It prevents unnecessary calls to
.Stat
because a valid uint64 can be 20 characters at most anyway. Italso avoids a few allocations.
Overall, benchmarking has shown to reduce the total CPU time by ~15%,
which given a total usage of roughly 3% in the entire kubelet, this
means we're reducing the kubelet baseline CPU by about ~0.45%.
Original profiling data from the Kubelet filtered down to only show
readUInt64
: https://pprof.me/5dfadaf/?filter_by_function=readuint64&search_string=readuint64