pii: pf-serve starts with ggml's Metal residency heartbeat off (#3185)

macOS's Energy Impact puts magpie at the top while CPU stays at ~6%
(#3185). Energy Impact counts wakeups, and an idle pf-serve (the
privacy filter's llama.cpp child) woke 134-146 times a second: ggml's
Metal backend keeps a residency set alive with a heartbeat. pf-serve
stays up for 10 minutes after each masked request, so any masking user
pays that most of the day. GGML_METAL_NO_RESIDENCY=1 turns the residency
set off.

Measured with proc_pid_rusage (ri_interrupt_wkups, ri_energy_nj) on an
M4 Pro, idle pf-serve, 20s windows:
- before, pf-serve pf-v1 as magpie starts it today: 140.0 and 138.5
  wakeups/s, 2.67 mW; the user's live v0.1.1370 pf-serve child read
  134-142/s
- after, pf-serve started through pii.Find at this commit: 0.4, 0.3,
  0.3 wakeups/s, 0.02 mW
- GGML_METAL_RESIDENCY_KEEP_ALIVE_S=0 or 1 instead: 143-146/s, no help
- latency: hot requests 51-78ms either way; the first request after a
  5-60s idle spell 57-77ms before, 105-518ms after; 90s idle through
  pii.Find at this commit 56-183ms

Run at this commit:
- go test -count=1 ./internal/pii/ ok;
  TestServeStartsWithoutResidencyHeartbeat red with the env line put
  back to os.Environ() ("residency " on both starts), green with it
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/pii/ ok,
  gofmt -l clean

Also measured: current main in a sandbox HOME with clones of the real
Claude/Codex sessions and magpie cache (no logins), window open, after
warm-up: main process 1.1-2.2 wakeups/s, 0.9-1.9 mW; its WebContent
0.5/s and GPU 0.3/s. The app.js intervals and the Go tickers were read
and are already held while the document is hidden or cheap.

Not tried: the full suite (an env-only change to one package); a
logged-in instance. The user's live v0.1.1370 main process reads 69-217
wakeups/s and 25-120 mW, which the sandbox at main (no logins) doesn't
reproduce, so its source is still unknown; Vulkan/Linux pf-serve (the
variable is Metal's only and ignored there).
This commit is contained in:
yetone
2026-10-25 18:40:25 +08:00
parent 0cba7fa680
commit d2e0ea393d
3 changed files with 27 additions and 1 deletions
+1
View File
@@ -56,6 +56,7 @@ func fakeServe(mode string) {
}
}
logf("start %s %s", name, device)
logf("residency %s", os.Getenv("GGML_METAL_NO_RESIDENCY"))
if mode == "noload" || mode == "novulkan" && name == "pf-serve-vulkan" {
json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "can't load " + mode})
os.Exit(1)
+7 -1
View File
@@ -93,7 +93,13 @@ func start(cpu bool) (*helper, error) {
func startOne(path string, cpu bool) (*helper, error) {
cmd := proc.Command(path, modelPath())
cmd.Env = os.Environ()
// Without GGML_METAL_NO_RESIDENCY, ggml's Metal residency set keeps a
// heartbeat that woke an idle pf-serve ~146 times a second for the 10
// minutes it stays up after a request, and macOS's Energy Impact
// counted magpie among the top for it (#3185). With it set: 0.3 a
// second, and the first request after an idle spell takes 0.1-0.45s
// more (51-78ms hot either way).
cmd.Env = append(os.Environ(), "GGML_METAL_NO_RESIDENCY=1")
if cpu {
cmd.Env = append(cmd.Env, "PF_DEVICE=cpu")
}
+19
View File
@@ -114,6 +114,25 @@ func TestVulkanThatWontStartFallsBack(t *testing.T) {
}
}
// Every pf-serve starts with ggml's Metal residency heartbeat off: it
// woke an idle one ~146 times a second (#3185). The CPU one after a GPU
// death too.
func TestServeStartsWithoutResidencyHeartbeat(t *testing.T) {
log := fakeInstall(t, "gpudies", false)
if _, err := Find([]string{"hi Alice"}); err != nil {
t.Fatal(err)
}
got := fakeLines(t, log, "residency")
if len(got) != 2 {
t.Fatalf("residency lines = %q, want a GPU and a CPU start", got)
}
for _, l := range got {
if l != "residency 1" {
t.Fatalf("residency lines = %q, want GGML_METAL_NO_RESIDENCY=1 on each", got)
}
}
}
// A pf-serve that doesn't answer is killed, and the request fails rather
// than wait for good.
func TestHungServeIsKilled(t *testing.T) {