mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-09 08:01:31 +00:00
metrics: move currentFDs code to the metrics package
Updates #2784 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
13
metrics/fds_linux.go
Normal file
13
metrics/fds_linux.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package metrics
|
||||
|
||||
import "os"
|
||||
|
||||
func currentFDs() int {
|
||||
// TODO(bradfitz): do this without so many allocations on Linux.
|
||||
ents, _ := os.ReadDir("/proc/self/fd")
|
||||
return len(ents)
|
||||
}
|
10
metrics/fds_notlinux.go
Normal file
10
metrics/fds_notlinux.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package metrics
|
||||
|
||||
func currentFDs() int { return 0 }
|
@@ -47,3 +47,10 @@ func (m *LabelMap) GetFloat(key string) *expvar.Float {
|
||||
m.AddFloat(key, 0.0)
|
||||
return m.Map.Get(key).(*expvar.Float)
|
||||
}
|
||||
|
||||
// CurrentFDs reports how many file descriptors are currently open.
|
||||
//
|
||||
// It only works on Linux. It returns zero otherwise.
|
||||
func CurrentFDs() int {
|
||||
return currentFDs()
|
||||
}
|
||||
|
28
metrics/metrics_test.go
Normal file
28
metrics/metrics_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCurrentFileDescriptors(t *testing.T) {
|
||||
if runtime.GOOS != "linux" {
|
||||
t.Skipf("skipping on %v", runtime.GOOS)
|
||||
}
|
||||
if n := CurrentFDs(); n < 3 {
|
||||
t.Errorf("got %v; want >= 3", n)
|
||||
} else {
|
||||
t.Logf("got %v", n)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCurrentFileDescriptors(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = CurrentFDs()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user