From 0df28b9294c60904529548a34741d3c2f9c79f18 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 --- fileutils/fileutils_darwin.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 fileutils/fileutils_darwin.go diff --git a/fileutils/fileutils_darwin.go b/fileutils/fileutils_darwin.go new file mode 100644 index 0000000..ccd648f --- /dev/null +++ b/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 +}