pkg/system: IsProcessZombie() skip conversion to string, use bytes instead

bytes.SplitN() is more performant, and skips having to do the conversion.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-15 14:55:40 +02:00
parent 970ad4e3c7
commit 0040fb93d6
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -4,9 +4,9 @@
package system // import "github.com/docker/docker/pkg/system"
import (
"bytes"
"fmt"
"os"
"strings"
"golang.org/x/sys/unix"
)
@ -29,18 +29,15 @@ func KillProcess(pid int) {
// IsProcessZombie return true if process has a state with "Z"
// http://man7.org/linux/man-pages/man5/proc.5.html
func IsProcessZombie(pid int) (bool, error) {
dataBytes, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
data := string(dataBytes)
sdata := strings.SplitN(data, " ", 4)
if len(sdata) >= 3 && sdata[2] == "Z" {
if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" {
return true, nil
}
return false, nil
}