pkg/fileutils: move GetTotalUsedFds internal in daemon

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-01-03 17:09:05 +01:00
parent ca1cd48ad8
commit a51baca00d
8 changed files with 28 additions and 56 deletions

View File

@@ -1,27 +0,0 @@
package fileutils // import "github.com/docker/docker/pkg/fileutils"
import (
"bytes"
"os"
"os/exec"
"strconv"
)
// GetTotalUsedFds returns the number of used File Descriptors by executing
// "lsof -lnP -Ff -p PID".
//
// It uses the "-F" option to only print file-descriptors (f), and the "-l",
// "-n", and "-P" options to omit looking up user-names, host-names, and port-
// names. See [LSOF(8)].
//
// Deprecated: this function is only used internally, and will be removed in the next release.
//
// [LSOF(8)]: https://opensource.apple.com/source/lsof/lsof-49/lsof/lsof.man.auto.html
func GetTotalUsedFds() int {
output, err := exec.Command("lsof", "-lnP", "-Ff", "-p", strconv.Itoa(os.Getpid())).CombinedOutput()
if err != nil {
return -1
}
return bytes.Count(output, []byte("\nf")) // Count number of file descriptor fields in output.
}

View File

@@ -1,65 +0,0 @@
package fileutils
import (
"context"
"fmt"
"io"
"os"
"github.com/containerd/containerd/tracing"
"github.com/containerd/log"
"golang.org/x/sys/unix"
)
// GetTotalUsedFds Returns the number of used File Descriptors by
// reading it via /proc filesystem.
//
// Deprecated: this function is only used internally, and will be removed in the next release.
func GetTotalUsedFds(ctx context.Context) int {
ctx, span := tracing.StartSpan(ctx, "GetTotalUsedFds")
defer span.End()
name := fmt.Sprintf("/proc/%d/fd", os.Getpid())
// Fast-path for Linux 6.2 (since [f1f1f2569901ec5b9d425f2e91c09a0e320768f3]).
// From the [Linux docs]:
//
// "The number of open files for the process is stored in 'size' member of
// stat() output for /proc/<pid>/fd for fast access."
//
// [Linux docs]: https://docs.kernel.org/filesystems/proc.html#proc-pid-fd-list-of-symlinks-to-open-files:
// [f1f1f2569901ec5b9d425f2e91c09a0e320768f3]: https://github.com/torvalds/linux/commit/f1f1f2569901ec5b9d425f2e91c09a0e320768f3
var stat unix.Stat_t
if err := unix.Stat(name, &stat); err == nil && stat.Size > 0 {
return int(stat.Size)
}
f, err := os.Open(name)
if err != nil {
log.G(ctx).WithError(err).Error("Error listing file descriptors")
return -1
}
defer f.Close()
var fdCount int
for {
select {
case <-ctx.Done():
log.G(ctx).WithError(ctx.Err()).Error("Context cancelled while counting file descriptors")
return -1
default:
}
names, err := f.Readdirnames(100)
fdCount += len(names)
if err == io.EOF {
break
} else if err != nil {
log.G(ctx).WithError(err).Error("Error listing file descriptors")
return -1
}
}
// Note that the slow path has 1 more file-descriptor, due to the open
// file-handle for /proc/<pid>/fd during the calculation.
return fdCount
}

View File

@@ -1,7 +1,6 @@
package fileutils // import "github.com/docker/docker/pkg/fileutils"
import (
"context"
"errors"
"os"
"path"
@@ -241,11 +240,3 @@ func TestCreateIfNotExistsFile(t *testing.T) {
t.Errorf("Should have been a file, seems it's not")
}
}
func BenchmarkGetTotalUsedFds(b *testing.B) {
ctx := context.Background()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = GetTotalUsedFds(ctx)
}
}

View File

@@ -1,11 +0,0 @@
package fileutils // import "github.com/docker/docker/pkg/fileutils"
import "context"
// GetTotalUsedFds Returns the number of used File Descriptors. Not supported
// on Windows.
//
// Deprecated: this function is only used internally, and will be removed in the next release.
func GetTotalUsedFds(ctx context.Context) int {
return -1
}