From 29993af4082c151c2c334a936c72b8e035e267d4 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 31 May 2019 14:41:05 +0200 Subject: [PATCH] apps/gophers: add per-frame mallocs to profiling status line Signed-off-by: Elias Naur --- apps/gophers/main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/gophers/main.go b/apps/gophers/main.go index a9b6572b..bd8339a7 100644 --- a/apps/gophers/main.go +++ b/apps/gophers/main.go @@ -13,6 +13,7 @@ import ( "log" "net/http" "os" + "runtime" "golang.org/x/image/draw" "golang.org/x/oauth2" @@ -166,6 +167,7 @@ func init() { func (a *App) run() error { a.w.Profiling = *stats ops := new(ui.Ops) + var lastMallocs uint64 for a.w.IsAlive() { select { case users := <-a.updateUsers: @@ -217,11 +219,16 @@ func (a *App) run() error { cs := layout.ExactConstraints(a.w.Size()) a.Layout(ops, cs) if a.w.Profiling { + var mstats runtime.MemStats + runtime.ReadMemStats(&mstats) + mallocs := mstats.Mallocs - lastMallocs + lastMallocs = mstats.Mallocs al := layout.Align{Alignment: layout.NE} cs := al.Begin(ops, cs) in := layout.Insets{Top: a.cfg.Dp(16)} cs = in.Begin(ops, cs) - dims := text.Label{Src: textColor, Face: a.face(fonts.mono, 8), Text: a.w.Timings()}.Layout(ops, cs) + txt := fmt.Sprintf("m: %d %s", mallocs, a.w.Timings()) + dims := text.Label{Src: textColor, Face: a.face(fonts.mono, 8), Text: txt}.Layout(ops, cs) dims = in.End(ops, dims) al.End(ops, dims) }