Skip to content

Conversation

@kolyshkin
Copy link
Contributor

Please see the individual commits for details.

fmt.Sprintf is slow and is not needed here, string concatenation would
be sufficient. It is also redundant to convert []byte from string and
back, since `bytes` package now provides the same functions as `strings`.

Use Fields() instead of TrimSpace() and Split(), mainly for readability
(note Fields() is somewhat slower than Split() but here it doesn't
matter much).

Use Join() to prepend the plus signs.

Signed-off-by: Kir Kolyshkin <[email protected]>
1. Return earlier if there is an error.

2. Do not use filepath.Split on every entry, use info.Name() instead.

3. Make readProcsFile() accept file name as an argument, to avoid
   unnecessary file name and directory splitting and merging.

4. Skip on info.IsDir() -- this avoids an error when cgroup name is
   set to "cgroup.procs".

This is still not very good since filepath.Walk() performs an unnecessary
stat(2) on every entry, but better than before.

Signed-off-by: Kir Kolyshkin <[email protected]>
It it not needed as it does nothing here.

Signed-off-by: Kir Kolyshkin <[email protected]>
}
resByte := []byte(res)
ctrs := bytes.Fields(content)
res := append([]byte("+"), bytes.Join(ctrs, []byte(" +"))...)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@giuseppe PTAL

if info.IsDir() || info.Name() != CgroupProcesses {
return nil
}
cPids, err := readProcsFile(p)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previous change to this code was in #332

// GetPids returns all pids, that were added to cgroup at path.
func GetPids(path string) ([]int, error) {
return readProcsFile(path)
func GetPids(dir string) ([]int, error) {
Copy link
Member

Choose a reason for hiding this comment

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

breaks compatibility if there is non-runc consumer of this function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope (I just changed the name of the parameter to be clear it's a directory, otherwise it's doing the same thing).

@AkihiroSuda
Copy link
Member

AkihiroSuda commented Mar 21, 2020

LGTM

Approved with PullApprove

@mrunalp
Copy link
Contributor

mrunalp commented Mar 24, 2020

LGTM

Approved with PullApprove

@mrunalp mrunalp merged commit 7de5db3 into opencontainers:master Mar 24, 2020
@kolyshkin kolyshkin deleted the nits branch March 30, 2020 16:01
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