Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
cmd,mipsevm: improve stats reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed May 4, 2023
1 parent 05097d3 commit d2241c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down
15 changes: 15 additions & 0 deletions mipsevm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

0 comments on commit d2241c6

Please sign in to comment.