2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-09-02 19:14:14 +01:00
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
2021-09-02 19:49:32 +01:00
|
|
|
import (
|
2022-11-05 21:26:29 +00:00
|
|
|
"io/fs"
|
|
|
|
"sync"
|
2021-09-02 23:28:19 +01:00
|
|
|
|
2022-11-05 21:26:29 +00:00
|
|
|
"go4.org/mem"
|
|
|
|
"tailscale.com/util/dirwalk"
|
2021-09-02 19:49:32 +01:00
|
|
|
)
|
2021-09-02 19:14:14 +01:00
|
|
|
|
2022-11-05 21:26:29 +00:00
|
|
|
// counter is a reusable counter for counting file descriptors.
|
|
|
|
type counter struct {
|
|
|
|
n int
|
2021-09-02 19:49:32 +01:00
|
|
|
|
2022-11-05 21:26:29 +00:00
|
|
|
// cb is the (*counter).count method value. Creating it allocates,
|
|
|
|
// so we have to save it away and use a sync.Pool to keep currentFDs
|
|
|
|
// amortized alloc-free.
|
|
|
|
cb func(name mem.RO, de fs.DirEntry) error
|
2021-09-02 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2022-11-05 21:26:29 +00:00
|
|
|
var counterPool = &sync.Pool{New: func() any {
|
|
|
|
c := new(counter)
|
|
|
|
c.cb = c.count
|
|
|
|
return c
|
|
|
|
}}
|
2021-09-02 19:49:32 +01:00
|
|
|
|
2022-11-05 21:26:29 +00:00
|
|
|
func (c *counter) count(name mem.RO, de fs.DirEntry) error {
|
|
|
|
c.n++
|
|
|
|
return nil
|
2021-09-02 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2022-11-05 21:26:29 +00:00
|
|
|
func currentFDs() int {
|
|
|
|
c := counterPool.Get().(*counter)
|
|
|
|
defer counterPool.Put(c)
|
|
|
|
c.n = 0
|
|
|
|
dirwalk.WalkShallow(mem.S("/proc/self/fd"), c.cb)
|
|
|
|
return c.n
|
2021-09-02 19:14:14 +01:00
|
|
|
}
|