fileutils_linux.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package fileutils
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/containerd/containerd/log"
  8. "golang.org/x/sys/unix"
  9. )
  10. // GetTotalUsedFds Returns the number of used File Descriptors by
  11. // reading it via /proc filesystem.
  12. func GetTotalUsedFds() int {
  13. name := fmt.Sprintf("/proc/%d/fd", os.Getpid())
  14. // Fast-path for Linux 6.2 (since [f1f1f2569901ec5b9d425f2e91c09a0e320768f3]).
  15. // From the [Linux docs]:
  16. //
  17. // "The number of open files for the process is stored in 'size' member of
  18. // stat() output for /proc/<pid>/fd for fast access."
  19. //
  20. // [Linux docs]: https://docs.kernel.org/filesystems/proc.html#proc-pid-fd-list-of-symlinks-to-open-files:
  21. // [f1f1f2569901ec5b9d425f2e91c09a0e320768f3]: https://github.com/torvalds/linux/commit/f1f1f2569901ec5b9d425f2e91c09a0e320768f3
  22. var stat unix.Stat_t
  23. if err := unix.Stat(name, &stat); err == nil && stat.Size > 0 {
  24. return int(stat.Size)
  25. }
  26. f, err := os.Open(name)
  27. if err != nil {
  28. log.G(context.TODO()).WithError(err).Error("Error listing file descriptors")
  29. return -1
  30. }
  31. defer f.Close()
  32. var fdCount int
  33. for {
  34. names, err := f.Readdirnames(100)
  35. fdCount += len(names)
  36. if err == io.EOF {
  37. break
  38. } else if err != nil {
  39. log.G(context.TODO()).WithError(err).Error("Error listing file descriptors")
  40. return -1
  41. }
  42. }
  43. // Note that the slow path has 1 more file-descriptor, due to the open
  44. // file-handle for /proc/<pid>/fd during the calculation.
  45. return fdCount
  46. }