Files
Reims-Go/internal/app/stats_flow.go
T
Nwflower c1b33c9c4f refactor(app): isolate native macOS adapters
Route generic App and CLI code through lifecycle and driver models. Move native macOS detail, create, settings and legacy flows into explicit files, share progress and redaction through host, and enforce the boundary with a file-scoped architecture ratchet.
2026-11-30 11:18:09 -07:00

96 lines
3.3 KiB
Go

package app
import (
"runtime"
"time"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/host"
)
// VMStats provides lightweight real-time resource telemetry for the VM detail header.
type VMStats struct {
Name string `json:"name"`
Running bool `json:"running"`
WindowFullscreen bool `json:"window_fullscreen,omitempty"`
PID int `json:"pid,omitempty"`
CPUPercent float64 `json:"cpu_percent"`
MemoryRSSBytes uint64 `json:"memory_rss_bytes"`
MemoryPrivateBytes uint64 `json:"memory_private_bytes"`
DiskActualBytes uint64 `json:"disk_actual_bytes"`
DiskVirtualBytes uint64 `json:"disk_virtual_bytes"`
DiskFormat string `json:"disk_format,omitempty"`
DiskBackingDepth int `json:"disk_backing_depth,omitempty"`
DiskBackingBroken bool `json:"disk_backing_broken,omitempty"`
DiskBackingDepthWarn bool `json:"disk_backing_depth_warn,omitempty"`
UptimeSeconds int64 `json:"uptime_seconds"`
GuestToolsConnected bool `json:"guest_tools_connected"`
GuestToolsMountMode string `json:"guest_tools_mount_mode,omitempty"`
GuestToolsSleepGuard bool `json:"guest_tools_sleep_guard"`
}
// GetVMStats samples host-side process and disk telemetry through the owning driver.
func (a *App) GetVMStats(name string) (VMStats, error) { return a.GetVMStatsWithDriver("", name) }
func (a *App) GetVMStatsWithDriver(driverID, name string) (VMStats, error) {
vm, err := a.resolveVMInfo(driverID, name)
if err != nil {
return VMStats{Name: name}, err
}
ref := vm.Ref()
stats := VMStats{Name: vm.Name, Running: vm.State == "running"}
pid := 0
if vm.PID != nil {
pid = *vm.PID
}
if listed, ok := a.polledVMForDriver(ref.DriverID, vm.Name); ok {
stats.Running = stats.Running || listed.State == "running"
if listed.PID != nil {
pid = *listed.PID
}
}
stats.PID = pid
if vm.DiskPath != "" {
if du, err := host.InspectDiskUsage(vm.DiskPath); err == nil {
stats.DiskActualBytes = du.ActualBytes
stats.DiskVirtualBytes = du.VirtualBytes
stats.DiskFormat = du.Format
stats.DiskBackingDepth = du.BackingDepth
stats.DiskBackingBroken = du.BackingBroken
stats.DiskBackingDepthWarn = du.BackingDepth >= host.BackingChainWarnDepth || du.BackingBroken
}
}
if stats.Running && pid > 0 {
stats.WindowFullscreen = host.IsProcessWindowFullscreen(uint32(pid))
if sample, err := host.SampleProcessStats(pid); err == nil {
a.mu.Lock()
prev := a.procSamples[ref.Key()]
a.procSamples[ref.Key()] = sample
a.mu.Unlock()
stats.CPUPercent = host.ComputeCPUPercent(prev, sample, runtime.NumCPU())
stats.MemoryRSSBytes = sample.WorkingSet
stats.MemoryPrivateBytes = sample.PrivateBytes
if !sample.CreatedAt.IsZero() {
if sec := int64(time.Since(sample.CreatedAt).Seconds()); sec > 0 {
stats.UptimeSeconds = sec
}
}
}
} else {
a.mu.Lock()
delete(a.procSamples, ref.Key())
a.mu.Unlock()
}
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
a.mu.Lock()
srv := a.guestTools
a.mu.Unlock()
if srv != nil {
st := srv.Status(vm.Name, 15*time.Second)
stats.GuestToolsConnected = st.Connected
stats.GuestToolsMountMode = st.MountMode
stats.GuestToolsSleepGuard = st.SleepActive
}
}
return stats, nil
}