Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/native-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ from `main` either drowns in merges or freezes the web app for months.
`task gallery` builds it into `bin/` and opens it.
- **`LICH_NATIVE_PPROF=127.0.0.1:6060`**, set where `lich native` starts, has `lich-native` serve Go's
runtime profiles (`net/http/pprof`) on that address, for a heap or CPU profile of a real window:
`go tool pprof http://127.0.0.1:6060/debug/pprof/heap`. Only a loopback address is accepted, and any
process on the machine can read what the profiles carry.
`go tool pprof http://127.0.0.1:6060/debug/pprof/heap`. `/debug/vars` on the same address carries `frames`,
the frames drawn since the window opened: two reads a few seconds apart give the window's frame rate. Only a
loopback address is accepted, and any process on the machine can read what the profiles carry.
- **`GOGC`** left unset, `lich-native` collects at 50 instead of Go's 100: a window with many sessions keeps a
live heap of hundreds of MB, and at 100 half the resident size is slack the system swaps out. Set `GOGC` to
choose another value.
Expand Down
1 change: 1 addition & 0 deletions native/cmd/lich-native/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func run(w *app.Window, rt backend.Runtime) error {
}
}
e.Frame(gtx.Ops)
frames.Add(1)
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion native/cmd/lich-native/pprof.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"expvar"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -29,9 +30,16 @@ func pprofListener(addr string) (net.Listener, error) {
return ln, nil
}

// servePprof answers net/http/pprof's endpoints on ln until it closes.
// frames counts the frames the window drew, served at /debug/vars beside
// the profiles: two reads a few seconds apart give the window's frame rate,
// which a profile alone does not show.
var frames = expvar.NewInt("frames")

// servePprof answers net/http/pprof's endpoints and /debug/vars on ln until
// it closes.
func servePprof(ln net.Listener) {
mux := http.NewServeMux()
mux.Handle("/debug/vars", expvar.Handler())
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
Expand Down
23 changes: 23 additions & 0 deletions native/cmd/lich-native/pprof_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -36,3 +37,25 @@ func TestPprofServesTheHeapOnLoopback(t *testing.T) {
t.Fatalf("status %d, body starts %.80q", resp.StatusCode, body)
}
}

func TestPprofServesTheFrameCount(t *testing.T) {
ln, err := pprofListener("127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
go servePprof(ln)
frames.Add(3)
resp, err := http.Get("http://" + ln.Addr().String() + "/debug/vars")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
var vars struct{ Frames int64 }
if err := json.NewDecoder(resp.Body).Decode(&vars); err != nil {
t.Fatal(err)
}
if vars.Frames < 3 {
t.Fatalf("frames = %d, want the 3 drawn", vars.Frames)
}
}
Loading