The VM page made the shell spawn about 84 reims-ctl processes a minute and pull a full 4K screendump every 5 s while a VM ran. - Scale reads *image.RGBA Pix directly: a 4K frame drops from ~80 ms and 8.3M allocations to ~10 ms and 2. - Thumbnails refresh every 15 s and skip the screendump while the VM's own window is in the foreground. - pollVMs polls every 2 s only while a VM starts or stops, else every 5 s; start, stop and delete wake it at once. - Stats, thumbnails and the heartbeat reuse the polled list instead of running their own `reims-ctl list`; the disk path from `show` is kept for a minute. Measured with two windows on the VM page: 67 -> 13 reims-ctl spawns a minute. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
3.3 KiB
Go
122 lines
3.3 KiB
Go
package main
|
|
|
|
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,
|
|
})
|
|
}
|