pkg/runtime: optimize FDUsage by removing sort
No need sort when we just want the counts.
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
+ }
+ list, err := f.Readdir(-1)
+ f.Close()
if err != nil {
return 0, err
}
- return uint64(len(fds)), nil
+ return uint64(len(list)), nil
}
