Kyle Brandt
Kyle Brandt

Reputation: 28407

Efficient Way to get the number of File Handles Open By a Process in Go?

I have a monitoring agent called scollector that is using more cpu on our loadbalancer. Perf says the CPU is mostly due to __d_lookup. One of the things I monitor is the number of open file handles - I do this via the following code:

    fds, e := ioutil.ReadDir("/proc/" + pid + "/fd")
    if e != nil {
        w.Remove(pid)
        continue
    }
    ...
    Add(md, "linux.proc.num_fds", len(fds), tags, metadata.Gauge, metadata.Files, descLinuxProcFd)

When I strace the process, I see it calling lstat on every file in the /fd directory (which is going to be a lot for our active load balancer (at least .5 million fds) - so I hypothesizing that this is the source of the high dentry cache cpu usage for the process.

Anyone have a suggestion on a better way to do this?

Upvotes: 3

Views: 2067

Answers (1)

cnicutar
cnicutar

Reputation: 182639

The trouble with ioutil.Readdir is that it does file.Readdir which says that it does an lstat for every file.

It seems Readdirnames doesn't do this, returning only the names. And since you only want the count, that should be enough.

Upvotes: 4

Related Questions