From 86c271caba071ef087037d924e579f88873d357e Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 19 Oct 2020 07:56:23 -0700 Subject: [PATCH] types/logger: move RusagePrefixLog to logger package, disable by default The RusagePrefixLog is rarely useful, hasn't been useful in a long time, is rarely the measurement we need, and is pretty spammy (and syscall-heavy). Disable it by default. We can enable it when we're debugging memory. --- cmd/tailscaled/tailscaled.go | 6 +++++- {wgengine => types/logger}/rusage.go | 6 ++---- {wgengine => types/logger}/rusage_nowindows.go | 2 +- {wgengine => types/logger}/rusage_windows.go | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) rename {wgengine => types/logger}/rusage.go (86%) rename {wgengine => types/logger}/rusage_nowindows.go (97%) rename {wgengine => types/logger}/rusage_windows.go (94%) diff --git a/cmd/tailscaled/tailscaled.go b/cmd/tailscaled/tailscaled.go index 995277cd4..e09f59c90 100644 --- a/cmd/tailscaled/tailscaled.go +++ b/cmd/tailscaled/tailscaled.go @@ -19,6 +19,7 @@ import ( "os/signal" "runtime" "runtime/debug" + "strconv" "syscall" "time" @@ -115,7 +116,10 @@ func run() error { pol.Shutdown(ctx) }() - logf := wgengine.RusagePrefixLog(log.Printf) + var logf logger.Logf = log.Printf + if v, _ := strconv.ParseBool(os.Getenv("TS_DEBUG_MEMORY")); v { + logf = logger.RusagePrefixLog(logf) + } logf = logger.RateLimitedFn(logf, 5*time.Second, 5, 100) if args.cleanup { diff --git a/wgengine/rusage.go b/types/logger/rusage.go similarity index 86% rename from wgengine/rusage.go rename to types/logger/rusage.go index b39667f6f..feb6fd19c 100644 --- a/wgengine/rusage.go +++ b/types/logger/rusage.go @@ -2,19 +2,17 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package wgengine +package logger import ( "fmt" "runtime" - - "tailscale.com/types/logger" ) // RusagePrefixLog returns a Logf func wrapping the provided logf func that adds // a prefixed log message to each line with the current binary memory usage // and max RSS. -func RusagePrefixLog(logf logger.Logf) logger.Logf { +func RusagePrefixLog(logf Logf) Logf { return func(f string, argv ...interface{}) { var m runtime.MemStats runtime.ReadMemStats(&m) diff --git a/wgengine/rusage_nowindows.go b/types/logger/rusage_nowindows.go similarity index 97% rename from wgengine/rusage_nowindows.go rename to types/logger/rusage_nowindows.go index 54f17e889..c5d0e706d 100644 --- a/wgengine/rusage_nowindows.go +++ b/types/logger/rusage_nowindows.go @@ -4,7 +4,7 @@ // +build !windows -package wgengine +package logger import ( "runtime" diff --git a/wgengine/rusage_windows.go b/types/logger/rusage_windows.go similarity index 94% rename from wgengine/rusage_windows.go rename to types/logger/rusage_windows.go index f4bf15119..ec5cf5513 100644 --- a/wgengine/rusage_windows.go +++ b/types/logger/rusage_windows.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package wgengine +package logger func rusageMaxRSS() float64 { // TODO(apenwarr): Substitute Windows equivalent of Getrusage() here.