forked from Netflix/spectator-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfd_linux.go
50 lines (43 loc) · 1.05 KB
/
fd_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package spectator
import (
"os"
"syscall"
)
func getNumFiles(dir string) (n int, err error) {
var f *os.File
var entries []string
/* #nosec G304 */
f, err = os.Open(dir)
if err != nil {
return 0, err
}
defer func() {
if e := f.Close(); err != nil && e != nil {
err = e
}
}()
entries, err = f.Readdirnames(-1)
if err != nil {
return 0, err
}
return len(entries), err
}
func updateFdStats(s *sysStatsCollector, cur int, max uint64) {
s.curOpen.Set(float64(cur))
s.maxOpen.Set(float64(max))
}
func fdStats(s *sysStatsCollector) {
// do not include /proc/self/fd in the count, since it will be opened
// when we get the number of files under self/fd
currentFdCount, err := getNumFiles("/proc/self/fd")
currentFdCount--
if err != nil {
s.registry.config.Log.Errorf("Unable to get open files: %v", err)
}
var rl syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rl); err != nil {
s.registry.config.Log.Errorf("Unable to get max open files: %v", err)
}
maxFdCount := rl.Cur
updateFdStats(s, currentFdCount, maxFdCount)
}