summaryrefslogtreecommitdiff
path: root/lib/textui/log_memstats.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-05 19:48:27 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-05 19:48:27 -0700
commitcd01485ec126c103fa9ab718685d184b70d0e1ff (patch)
tree0c003455bf0795a04e4fb198d2e85e2c7b63ffb3 /lib/textui/log_memstats.go
parentf416f777b2c2cac095e851e6799020f91e34aed1 (diff)
parent3b385f26973e45b4c2e2f3ebf9d52ab0131cff5e (diff)
Merge branch 'lukeshu/rebuild-nodes-take4'
Diffstat (limited to 'lib/textui/log_memstats.go')
-rw-r--r--lib/textui/log_memstats.go16
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/textui/log_memstats.go b/lib/textui/log_memstats.go
index 6e3c3a1..31d526f 100644
--- a/lib/textui/log_memstats.go
+++ b/lib/textui/log_memstats.go
@@ -11,6 +11,13 @@ import (
"time"
)
+// LiveMemUse is an object that stringifies as the live memory use of
+// the program.
+//
+// It is intended to be used with dlog by attaching it as a field, so
+// that all log lines include the current memory use:
+//
+// ctx = dlog.WithField(ctx, "mem", new(textui.LiveMemUse))
type LiveMemUse struct {
mu sync.Mutex
stats runtime.MemStats
@@ -19,14 +26,19 @@ type LiveMemUse struct {
var _ fmt.Stringer = (*LiveMemUse)(nil)
-var liveMemUseUpdateInterval = Tunable(1 * time.Second)
+// LiveMemUseUpdateInterval is the shortest interval on which
+// LiveMemUse is willing to update; we have this minimum interval
+// because it stops the world to collect memory statistics, so we
+// don't want to be updating the statistics too often.
+var LiveMemUseUpdateInterval = Tunable(1 * time.Second)
+// String implements fmt.Stringer.
func (o *LiveMemUse) String() string {
o.mu.Lock()
// runtime.ReadMemStats() calls stopTheWorld(), so we want to
// rate-limit how often we call it.
- if now := time.Now(); now.Sub(o.last) > liveMemUseUpdateInterval {
+ if now := time.Now(); now.Sub(o.last) > LiveMemUseUpdateInterval {
runtime.ReadMemStats(&o.stats)
o.last = now
}