fileutils_linux.go 693 B

1234567891011121314151617181920212223242526272829303132333435
  1. package fileutils
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/containerd/containerd/log"
  8. )
  9. // GetTotalUsedFds Returns the number of used File Descriptors by
  10. // reading it via /proc filesystem.
  11. func GetTotalUsedFds() int {
  12. name := fmt.Sprintf("/proc/%d/fd", os.Getpid())
  13. f, err := os.Open(name)
  14. if err != nil {
  15. log.G(context.TODO()).WithError(err).Error("Error listing file descriptors")
  16. return -1
  17. }
  18. defer f.Close()
  19. var fdCount int
  20. for {
  21. names, err := f.Readdirnames(100)
  22. fdCount += len(names)
  23. if err == io.EOF {
  24. break
  25. } else if err != nil {
  26. log.G(context.TODO()).WithError(err).Error("Error listing file descriptors")
  27. return -1
  28. }
  29. }
  30. return fdCount
  31. }