Files
Reims-Go/thumbnail_flow.go
T
NwflowerandClaude Opus 5.5 69803355bf perf(shell): cut thumbnail cost and reims-ctl polling
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>
2026-11-28 22:51:40 -07:00

124 lines
3.4 KiB
Go

package main
// VM screen thumbnails for the VM page (docs/ui-audit.md): while a VM runs,
// grab one frame over its QMP socket (`screendump`), shrink it to a small
// JPEG and keep it under <data root>\cache\thumbs, so a stopped VM can still
// show its last screen. Nothing here touches vm.toml or the guest.
import (
"context"
"encoding/base64"
"os"
"path/filepath"
"sync"
"time"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/qmp"
"github.com/Nwflower/reims-go/internal/thumb"
)
// VMThumbnail is one frame of a VM's screen as a data URL.
type VMThumbnail struct {
DataURL string `json:"data_url"`
CapturedAt int64 `json:"captured_at"` // Unix milliseconds
Live bool `json:"live"` // captured just now from the running VM
}
const thumbWidth = 640
// thumbLocks keeps one capture per VM in flight: QMP serves a single client
// and the page polls on a timer.
var thumbLocks sync.Map // name → *sync.Mutex
func (a *App) thumbPath(name string) string {
a.mu.Lock()
dir := a.cfg.XDGCacheDir()
a.mu.Unlock()
return filepath.Join(dir, "thumbs", name+".jpg")
}
// GetVMThumbnail returns the VM's screen: a fresh frame while it runs, else
// the last one saved. An empty DataURL means there is no frame to show.
func (a *App) GetVMThumbnail(name string) (VMThumbnail, error) {
path := a.thumbPath(name)
l, _ := thumbLocks.LoadOrStore(name, &sync.Mutex{})
mu := l.(*sync.Mutex)
if mu.TryLock() {
defer mu.Unlock()
if t, ok := a.captureThumb(name, path); ok {
return t, nil
}
}
return readThumb(path, false), nil
}
func (a *App) captureThumb(name, path string) (VMThumbnail, bool) {
typing.mu.Lock()
busy := typing.busy[name]
typing.mu.Unlock()
if busy {
return VMThumbnail{}, false
}
vm, ok := a.polledVM(name)
if !ok || vm.State != "running" || vm.Dir == "" {
return VMThumbnail{}, false
}
// The user is looking at the VM window itself, not this page: skip the
// screendump, which runs on QEMU's main loop and writes a full-size frame
// (about 25 MB at 4K) to disk.
if vm.PID != nil && *vm.PID > 0 && host.ForegroundPID() == uint32(*vm.PID) {
return VMThumbnail{}, false
}
a.mu.Lock()
tmp := a.cfg.TempDir()
a.mu.Unlock()
if err := os.MkdirAll(tmp, 0o755); err != nil {
return VMThumbnail{}, false
}
raw := filepath.Join(tmp, "screendump-"+name)
defer os.Remove(raw)
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
format, err := qmp.ScreendumpVM(ctx, vm.Dir, raw)
if err != nil {
return VMThumbnail{}, false
}
img, err := thumb.Load(raw, format)
if err != nil || thumb.Blank(img) {
return VMThumbnail{}, false
}
jpg, err := thumb.JPEG(thumb.Scale(img, thumbWidth), 80)
if err != nil {
return VMThumbnail{}, false
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err == nil {
part := path + ".part"
if os.WriteFile(part, jpg, 0o644) == nil {
_ = os.Rename(part, path)
}
}
return VMThumbnail{
DataURL: "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(jpg),
CapturedAt: time.Now().UnixMilli(),
Live: true,
}, true
}
func readThumb(path string, live bool) VMThumbnail {
b, err := os.ReadFile(path)
if err != nil || len(b) == 0 {
return VMThumbnail{}
}
var at int64
if st, err := os.Stat(path); err == nil {
at = st.ModTime().UnixMilli()
}
return VMThumbnail{
DataURL: "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(b),
CapturedAt: at,
Live: live,
}
}