|
| 1 | +package mobile |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "math" |
| 7 | + "runtime" |
| 8 | + runtimeDebug "runtime/debug" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// memoryLogInterval matches the iOS PacketTunnelProvider Swift memory logger |
| 14 | +// cadence so the Go-side and Swift-side log lines interleave at the same rate. |
| 15 | +const memoryLogInterval = 10 * time.Second |
| 16 | + |
| 17 | +const bytesPerMB = 1024.0 * 1024.0 |
| 18 | + |
| 19 | +var ( |
| 20 | + memLogMu sync.Mutex |
| 21 | + memLogCancel context.CancelFunc |
| 22 | +) |
| 23 | + |
| 24 | +// startMemoryLogger begins periodic Go-runtime memory logging for the tunnel |
| 25 | +// (extension) process. It is the Go counterpart to the Swift memory logger in |
| 26 | +// PacketTunnelProvider: that one reports the whole extension process footprint |
| 27 | +// via Mach task_vm_info (phys_footprint), while this reports the Go runtime's |
| 28 | +// view from inside that same process. The Go heap is the largest mutable |
| 29 | +// contributor to phys_footprint, and iOS jetsams the entire Network Extension |
| 30 | +// when the footprint exceeds its tight memory cap — so correlating the two log |
| 31 | +// streams shows whether Go is what's pushing the process toward that limit. |
| 32 | +// |
| 33 | +// Started from StartIPCServer (tunnel start) and stopped from CloseIPCServer |
| 34 | +// (tunnel stop), mirroring the Swift logger's startTunnel/stopTunnel hooks. |
| 35 | +// Safe to call repeatedly: a start while already running is a no-op until the |
| 36 | +// matching stop. |
| 37 | +func startMemoryLogger() { |
| 38 | + memLogMu.Lock() |
| 39 | + defer memLogMu.Unlock() |
| 40 | + if memLogCancel != nil { |
| 41 | + return |
| 42 | + } |
| 43 | + ctx, cancel := context.WithCancel(context.Background()) |
| 44 | + memLogCancel = cancel |
| 45 | + go func() { |
| 46 | + ticker := time.NewTicker(memoryLogInterval) |
| 47 | + defer ticker.Stop() |
| 48 | + logMemStats() // log immediately, like the Swift timer's deadline: .now() |
| 49 | + for { |
| 50 | + select { |
| 51 | + case <-ctx.Done(): |
| 52 | + slog.Debug("Stopping tunnel memory logger") |
| 53 | + return |
| 54 | + case <-ticker.C: |
| 55 | + logMemStats() |
| 56 | + } |
| 57 | + } |
| 58 | + }() |
| 59 | +} |
| 60 | + |
| 61 | +// stopMemoryLogger stops the goroutine started by startMemoryLogger. Safe to |
| 62 | +// call when the logger isn't running. |
| 63 | +func stopMemoryLogger() { |
| 64 | + memLogMu.Lock() |
| 65 | + defer memLogMu.Unlock() |
| 66 | + if memLogCancel != nil { |
| 67 | + memLogCancel() |
| 68 | + memLogCancel = nil |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// mbFromBytes converts a byte count to megabytes rounded to 2 decimals, matching |
| 73 | +// the "%.2f MB" formatting on the Swift side. |
| 74 | +func mbFromBytes(b uint64) float64 { |
| 75 | + return math.Round(float64(b)/bytesPerMB*100) / 100 |
| 76 | +} |
| 77 | + |
| 78 | +// logMemStats emits one Go-runtime memory snapshot. Field meanings: |
| 79 | +// - heap_alloc: live heap objects (the working set Go can't release) |
| 80 | +// - heap_inuse/heap_idle: bytes in in-use vs idle spans |
| 81 | +// - heap_released: idle bytes already returned to the OS (lowers footprint) |
| 82 | +// - stack_inuse: goroutine stacks |
| 83 | +// - sys: total obtained from the OS (Go's contribution to process footprint) |
| 84 | +// - next_gc: heap size that will trigger the next GC |
| 85 | +// - mem_limit: the soft memory limit in effect (libbox.SetMemoryLimit sets |
| 86 | +// this on mobile); 0 means no limit |
| 87 | +func logMemStats() { |
| 88 | + var m runtime.MemStats |
| 89 | + runtime.ReadMemStats(&m) |
| 90 | + |
| 91 | + // SetMemoryLimit(-1) reads the current soft limit without changing it; |
| 92 | + // math.MaxInt64 is the sentinel for "no limit". |
| 93 | + var limitMB float64 |
| 94 | + if limit := runtimeDebug.SetMemoryLimit(-1); limit != math.MaxInt64 { |
| 95 | + limitMB = math.Round(float64(limit)/bytesPerMB*100) / 100 |
| 96 | + } |
| 97 | + |
| 98 | + slog.Info("[memory stats]", |
| 99 | + "heap_alloc_mb", mbFromBytes(m.HeapAlloc), |
| 100 | + "heap_inuse_mb", mbFromBytes(m.HeapInuse), |
| 101 | + "heap_idle_mb", mbFromBytes(m.HeapIdle), |
| 102 | + "heap_released_mb", mbFromBytes(m.HeapReleased), |
| 103 | + "stack_inuse_mb", mbFromBytes(m.StackInuse), |
| 104 | + "sys_mb", mbFromBytes(m.Sys), |
| 105 | + "next_gc_mb", mbFromBytes(m.NextGC), |
| 106 | + "mem_limit_mb", limitMB, |
| 107 | + "num_gc", m.NumGC, |
| 108 | + "num_goroutine", runtime.NumGoroutine(), |
| 109 | + ) |
| 110 | +} |
0 commit comments