Files
Reims-Go/internal/app/stats_flow.go
T

122 lines
3.3 KiB
Go

package app
import (
"context"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/Nwflower/reims-go/internal/driver/reims"
"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"`
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"`
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"`
}
func resolveVMDiskPath(d reims.VMDetail) string {
raw := strings.TrimSpace(d.Disk)
// `reims-ctl show` formats disk like "disk.qcow2 (nvme, 128G)" or an absolute path.
if idx := strings.Index(raw, " ("); idx > 0 {
raw = strings.TrimSpace(raw[:idx])
}
if raw != "" {
if filepath.IsAbs(raw) {
return raw
}
if d.Directory != "" {
return filepath.Join(d.Directory, raw)
}
}
if d.Directory != "" {
return filepath.Join(d.Directory, "disk.qcow2")
}
return ""
}
// GetVMStats samples QEMU CPU/memory usage (when running), disk actual vs
// virtual capacity, and live Guest Tools 2.0 connection state.
func (a *App) GetVMStats(name string) (VMStats, error) {
stats := VMStats{Name: name}
c, err := a.client()
if err != nil {
return stats, err
}
ctx := context.Background()
vms, _ := a.polledVMs(ctx)
var pid int
for _, vm := range vms {
if vm.Name == name {
stats.Running = vm.State == "running"
if vm.PID != nil {
pid = *vm.PID
}
break
}
}
stats.PID = pid
if diskPath := a.vmDiskPath(ctx, c, name); diskPath != "" {
if du, err := host.InspectDiskUsage(diskPath); err == nil {
stats.DiskActualBytes = du.ActualBytes
stats.DiskVirtualBytes = du.VirtualBytes
stats.DiskFormat = du.Format
}
}
if stats.Running && pid > 0 {
if sample, err := host.SampleProcessStats(pid); err == nil {
a.mu.Lock()
prev := a.procSamples[name]
a.procSamples[name] = 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, name)
a.mu.Unlock()
}
a.mu.Lock()
srv := a.guestTools
a.mu.Unlock()
if srv != nil {
st := srv.Status(name, 15*time.Second)
stats.GuestToolsConnected = st.Connected
stats.GuestToolsMountMode = st.MountMode
stats.GuestToolsSleepGuard = st.SleepActive
}
return stats, nil
}
// ToggleBorderless switches borderless fullscreen startup via `reims-ctl set --borderless`.
func (a *App) ToggleBorderless(name string, enabled bool) error {
v := enabled
return a.ApplyVMSettings(reims.SetOpts{
Name: name,
Borderless: &v,
})
}