fileutils_darwin.go 450 B

123456789101112131415161718192021222324252627
  1. package fileutils
  2. import (
  3. "os"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. )
  8. // GetTotalUsedFds returns the number of used File Descriptors by
  9. // executing `lsof -p PID`
  10. func GetTotalUsedFds() int {
  11. pid := os.Getpid()
  12. cmd := exec.Command("lsof", "-p", strconv.Itoa(pid))
  13. output, err := cmd.CombinedOutput()
  14. if err != nil {
  15. return -1
  16. }
  17. outputStr := strings.TrimSpace(string(output))
  18. fds := strings.Split(outputStr, "\n")
  19. return len(fds) - 1
  20. }