ipn/ipnlocal/c2n: add logallocs endpoint

This allows C2N clients to obtain sampled information about
memory allocations since the program started.

We now also force GC on logheap.

Updates tailscale/corp#18514

Signed-off-by: Percy Wegmann <percy@tailscale.com>
This commit is contained in:
Percy Wegmann 2024-03-21 06:58:10 -05:00
parent c1029de875
commit dc02d49bf1
No known key found for this signature in database
GPG Key ID: 29D8CDEB4C13D48B
2 changed files with 18 additions and 0 deletions

View File

@ -48,6 +48,7 @@
req("/debug/metrics"): handleC2NDebugMetrics,
req("/debug/component-logging"): handleC2NDebugComponentLogging,
req("/debug/logheap"): handleC2NDebugLogHeap,
req("/debug/logallocs"): handleC2NDebugLogAllocs,
req("POST /logtail/flush"): handleC2NLogtailFlush,
req("POST /sockstats"): handleC2NSockStats,
@ -175,9 +176,22 @@ func handleC2NDebugLogHeap(b *LocalBackend, w http.ResponseWriter, r *http.Reque
http.Error(w, "not implemented", http.StatusNotImplemented)
return
}
runtime.GC()
c2nLogHeap(w, r)
}
var c2nLogAllocs func(http.ResponseWriter, *http.Request) // non-nil on most platforms (c2n_pprof.go)
func handleC2NDebugLogAllocs(b *LocalBackend, w http.ResponseWriter, r *http.Request) {
if c2nLogAllocs == nil {
// Not implemented on platforms trying to optimize for binary size or
// reduced memory usage.
http.Error(w, "not implemented", http.StatusNotImplemented)
return
}
c2nLogAllocs(w, r)
}
func handleC2NSSHUsernames(b *LocalBackend, w http.ResponseWriter, r *http.Request) {
var req tailcfg.C2NSSHUsernamesRequest
if r.Method == "POST" {

View File

@ -14,4 +14,8 @@ func init() {
c2nLogHeap = func(w http.ResponseWriter, r *http.Request) {
pprof.WriteHeapProfile(w)
}
c2nLogAllocs = func(w http.ResponseWriter, r *http.Request) {
pprof.Lookup("allocs").WriteTo(w, 0)
}
}