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

container/common: Replace readUInt64 with optimized version #3406

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

brancz
Copy link
Contributor

@brancz brancz commented Oct 6, 2023

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%.

Original profiling data from the Kubelet filtered down to only show readUInt64: https://pprof.me/5dfadaf/?filter_by_function=readuint64&search_string=readuint64

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%.
@k8s-ci-robot
Copy link
Collaborator

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@brancz brancz changed the title Readuint64 container/common: Replace readUInt64 with optimized version Oct 6, 2023
Comment on lines +313 to +333
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])
}
Copy link
Collaborator

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?

@iwankgb
Copy link
Collaborator

iwankgb commented Oct 7, 2023

/ok-to-test

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

Successfully merging this pull request may close these issues.

3 participants