From d2dd1a1f7d9e7e522a9e0b3c3aec52c23abbdd46 Mon Sep 17 00:00:00 2001 From: allencloud Date: Tue, 3 May 2016 23:08:51 +0800 Subject: [PATCH] add fileutils_darwin.go in pkg/fileutils to support darwin platform Signed-off-by: allencloud --- pkg/fileutils/fileutils_darwin.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 pkg/fileutils/fileutils_darwin.go diff --git a/pkg/fileutils/fileutils_darwin.go b/pkg/fileutils/fileutils_darwin.go new file mode 100644 index 0000000000..ccd648fac3 --- /dev/null +++ b/pkg/fileutils/fileutils_darwin.go @@ -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 +}