114 lines
3.9 KiB
Go
114 lines
3.9 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"`
|
|
DiskExtendedL2 bool `json:"disk_extended_l2,omitempty"`
|
|
DiskSnapshotCount uint32 `json:"disk_snapshot_count,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"`
|
|
GuestToolsAgentVersion int `json:"guest_tools_agent_version,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.DiskExtendedL2 = du.ExtendedL2
|
|
stats.DiskSnapshotCount = du.SnapshotCount
|
|
stats.DiskBackingDepth = du.BackingDepth
|
|
stats.DiskBackingBroken = du.BackingBroken
|
|
stats.DiskBackingDepthWarn = du.BackingDepth >= host.BackingChainWarnDepth || du.BackingBroken
|
|
}
|
|
}
|
|
hosted := driver.IsHosted(vm.Capabilities)
|
|
var pids []uint32
|
|
if !hosted {
|
|
pids = a.vmProcessPIDs(ref, pid)
|
|
}
|
|
if stats.Running && !hosted && (pid > 0 || len(pids) > 0) {
|
|
if len(pids) > 0 {
|
|
stats.WindowFullscreen = host.IsProcessTreeWindowFullscreen(pids)
|
|
}
|
|
samplePID := pid
|
|
if samplePID <= 0 {
|
|
samplePID = int(pids[0])
|
|
}
|
|
if sample, err := host.SampleProcessStats(samplePID); 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.GuestToolsAgentVersion = st.AgentVersion
|
|
stats.GuestToolsSleepGuard = st.SleepActive
|
|
}
|
|
}
|
|
return stats, nil
|
|
}
|