一种有效的方法来获取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?

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.