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

getPids - don't recursively traverse every dir in /proc #66367

Merged
merged 1 commit into from
Oct 29, 2018

Conversation

cpuguy83
Copy link
Contributor

@cpuguy83 cpuguy83 commented Jul 19, 2018

What this PR does / why we need it:

filepath.Walk recursively traverses every dir, which is not what is
needed for getPids.
Instead only read the list of dirs in the top level of /proc.

benchmark              old ns/op     new ns/op     delta
BenchmarkGetPids-4     868684        195522        -77.49%
Speedup process lookup in /proc

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 19, 2018
@cpuguy83 cpuguy83 changed the title PidOf - don't recursively traverse every dir getPids - don't recursively traverse every dir Jul 19, 2018
@cpuguy83 cpuguy83 changed the title getPids - don't recursively traverse every dir getPids - don't recursively traverse every dir in /proc Jul 19, 2018
@k8s-ci-robot
Copy link
Contributor

@lanchongyizu: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/ok-to-test

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.

@xiaoxubeii
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 19, 2018
}

// The bytes we read have '\0' as a separator for the command line
parts := bytes.SplitN(cmdline, []byte{0}, 2)
if len(parts) == 0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

@@ -140,12 +142,10 @@ func getPids(re *regexp.Regexp) []int {
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`filepath.Walk` recursively traverses every dir, which is not what is
needed for getPids.
Instead only read the list of dirs in the top level of `/proc`.

```
benchmark              old ns/op     new ns/op     delta
BenchmarkGetPids-4     868684        195522        -77.49%
```
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 19, 2018
@cpuguy83
Copy link
Contributor Author

Updated, also optimized in case of many entries in /proc so that we don't allocate a huge slice.

Copy link

@fcrisciani fcrisciani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fcrisciani
Copy link

@xiaoxubeii PTAL

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jul 31, 2018
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 29, 2018
@fcrisciani
Copy link

still valid
/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 29, 2018
@dims
Copy link
Member

dims commented Oct 29, 2018

/test all
/assign @thockin

@thockin
Copy link
Member

thockin commented Oct 29, 2018

Would it be simpler to make a more surgical change? Something like:

    filepath.Walk("/proc", func(path string, info os.FileInfo, err error) error {
        if err != nil {
            // We should continue processing other directories/files
            return nil 
        }
        if path == "/proc" {
            // Skip the top-level dir, but recurse
            return nil 
        }
        fmt.Printf("path %v\n", path)
        if !info.IsDir() {
            // Skip files
            return nil 
        }
        // If the directory is not a number (i.e. not a PID), skip it
        base := filepath.Base(path)
        if _, err := strconv.Atoi(base); err != nil {
            fmt.Printf("skipping %v\n", path)
            return filepath.SkipDir
        }
        fmt.Printf("found pid %v => %v/cmdline\n", base, path)
        // ...process it...
        return filepath.SkipDir // done with this dir, don't recurse further
    })  

@thockin
Copy link
Member

thockin commented Oct 29, 2018

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 29, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cpuguy83, thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 29, 2018
@thockin
Copy link
Member

thockin commented Oct 29, 2018

Did some quick tests, this is better and complete

@k8s-ci-robot k8s-ci-robot merged commit 4c874db into kubernetes:master Oct 29, 2018
@cpuguy83 cpuguy83 deleted the speedup_pidof branch October 29, 2018 23:11
@cpuguy83
Copy link
Contributor Author

🎉 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants