Merge pull request #22472 from allencloud/make-pkg-fileutils-support-darwin

add fileutils_darwin.go in pkg/fileutils to support darwin platform
This commit is contained in:
Sebastiaan van Stijn 2016-07-02 14:15:22 -07:00 committed by GitHub
commit 9344f05303

View file

@ -0,0 +1,27 @@
package fileutils
import (
"os"
"os/exec"
"strconv"
"strings"
)
// GetTotalUsedFds returns the number of used File Descriptors by
// executing `lsof -p PID`
func GetTotalUsedFds() int {
pid := os.Getpid()
cmd := exec.Command("lsof", "-p", strconv.Itoa(pid))
output, err := cmd.CombinedOutput()
if err != nil {
return -1
}
outputStr := strings.TrimSpace(string(output))
fds := strings.Split(outputStr, "\n")
return len(fds) - 1
}