mirror of https://github.com/tailscale/tailscale/
metrics: move currentFDs code to the metrics package
Updates #2784 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>pull/2586/head
parent
5d800152d9
commit
fc160f80ee
@ -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)
|
||||||
|
}
|
@ -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 }
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue