2023-01-27 13:37:20 -08:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 14:16:58 -08:00
|
|
|
|
2020-10-19 07:56:23 -07:00
|
|
|
package logger
|
2020-02-05 14:16:58 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2020-02-11 15:21:24 -08:00
|
|
|
// 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.
|
2020-10-19 07:56:23 -07:00
|
|
|
func RusagePrefixLog(logf Logf) Logf {
|
2022-03-16 16:27:57 -07:00
|
|
|
return func(f string, argv ...any) {
|
2020-02-05 14:16:58 -08:00
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
goMem := float64(m.HeapInuse+m.StackInuse) / (1 << 20)
|
|
|
|
maxRSS := rusageMaxRSS()
|
|
|
|
pf := fmt.Sprintf("%.1fM/%.1fM %s", goMem, maxRSS, f)
|
|
|
|
logf(pf, argv...)
|
|
|
|
}
|
|
|
|
}
|