From d2241c6db5ca6342018ec00a919342e1f301f264 Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 4 May 2023 16:08:34 +0200 Subject: [PATCH] cmd,mipsevm: improve stats reporting --- cmd/run.go | 7 +++++++ mipsevm/memory.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/cmd/run.go b/cmd/run.go index beff1f8f..5fdca4c4 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -244,15 +244,22 @@ func Run(ctx *cli.Context) error { stepFn = Guard(po.cmd.ProcessState, stepFn) } + start := time.Now() + startStep := state.Step + for !state.Exited { step := state.Step name := meta.LookupSymbol(state.PC) if infoAt(state) { + delta := time.Since(start) l.Info("processing", "step", step, "pc", mipsevm.HexU32(state.PC), "insn", mipsevm.HexU32(state.Memory.GetMemory(state.PC)), + "ips", float64(step-startStep)/(float64(delta)/float64(time.Second)), + "pages", len(state.Memory.Pages), + "mem", state.Memory.Usage(), "name", name, ) } diff --git a/mipsevm/memory.go b/mipsevm/memory.go index f127da41..d5c5930d 100644 --- a/mipsevm/memory.go +++ b/mipsevm/memory.go @@ -276,3 +276,18 @@ func (r *memReader) Read(dest []byte) (n int, err error) { func (m *Memory) ReadMemoryRange(addr uint32, count uint32) io.Reader { return &memReader{m: m, addr: addr, count: count} } + +func (m *Memory) Usage() string { + total := uint64(len(m.Pages)) * PageSize + const unit = 1024 + if total < unit { + return fmt.Sprintf("%d B", total) + } + div, exp := uint64(unit), 0 + for n := total / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + // KiB, MiB, GiB, TiB, ... + return fmt.Sprintf("%.1f %ciB", float64(total)/float64(div), "KMGTPE"[exp]) +}