From 0bed5fffd4f16db33b882fc421954e8c09ef735f Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Wed, 12 Aug 2020 10:22:56 -0700 Subject: [PATCH] pkg/runtime: optimize FDUsage by removing sort No need sort when we just want the counts. Signed-off-by: Gyuho Lee --- pkg/runtime/fds_linux.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/runtime/fds_linux.go b/pkg/runtime/fds_linux.go index 8e9359db2..4906d678f 100644 --- a/pkg/runtime/fds_linux.go +++ b/pkg/runtime/fds_linux.go @@ -16,7 +16,7 @@ package runtime import ( - "io/ioutil" + "os" "syscall" ) @@ -29,9 +29,20 @@ func FDLimit() (uint64, error) { } func FDUsage() (uint64, error) { - fds, err := ioutil.ReadDir("/proc/self/fd") + return countFiles("/proc/self/fd") +} + +// countFiles reads the directory named by dirname and returns the count. +// This is same as stdlib "io/ioutil.ReadDir" but without sorting. +func countFiles(dirname string) (uint64, error) { + f, err := os.Open(dirname) if err != nil { return 0, err } - return uint64(len(fds)), nil + list, err := f.Readdir(-1) + f.Close() + if err != nil { + return 0, err + } + return uint64(len(list)), nil }