Explorar el Código

Merge pull request #45861 from thaJeztah/fileutils_deprecate_GetTotalUsedFds

pkg/fileutils: GetTotalUsedFds(): slight optimization for macOS
Sebastiaan van Stijn hace 2 años
padre
commit
6fee7c8f8b
Se han modificado 1 ficheros con 11 adiciones y 13 borrados
  1. 11 13
      pkg/fileutils/fileutils_darwin.go

+ 11 - 13
pkg/fileutils/fileutils_darwin.go

@@ -1,27 +1,25 @@
 package fileutils // import "github.com/docker/docker/pkg/fileutils"
 package fileutils // import "github.com/docker/docker/pkg/fileutils"
 
 
 import (
 import (
+	"bytes"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
 	"strconv"
 	"strconv"
-	"strings"
 )
 )
 
 
-// GetTotalUsedFds returns the number of used File Descriptors by
-// executing `lsof -p PID`
+// GetTotalUsedFds returns the number of used File Descriptors by executing
+// "lsof -lnP -Ff -p PID".
+//
+// It uses the "-F" option to only print file-descriptors (f), and the "-l",
+// "-n", and "-P" options to omit looking up user-names, host-names, and port-
+// names. See [LSOF(8)].
+//
+// [LSOF(8)]: https://opensource.apple.com/source/lsof/lsof-49/lsof/lsof.man.auto.html
 func GetTotalUsedFds() int {
 func GetTotalUsedFds() int {
-	pid := os.Getpid()
-
-	cmd := exec.Command("lsof", "-p", strconv.Itoa(pid))
-
-	output, err := cmd.CombinedOutput()
+	output, err := exec.Command("lsof", "-lnP", "-Ff", "-p", strconv.Itoa(os.Getpid())).CombinedOutput()
 	if err != nil {
 	if err != nil {
 		return -1
 		return -1
 	}
 	}
 
 
-	outputStr := strings.TrimSpace(string(output))
-
-	fds := strings.Split(outputStr, "\n")
-
-	return len(fds) - 1
+	return bytes.Count(output, []byte("\nf")) // Count number of file descriptor fields in output.
 }
 }