allmende-sim: record runs as JSON (samples, phases, arrival times); sweep script and graph data; scaling results
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package main
|
||||
|
||||
// Recording of a run for graphs: a sample of the whole process every second,
|
||||
// where each phase begins and ends, and how long each thing took to arrive.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type sample struct {
|
||||
T float64 `json:"t"` // seconds since the run began
|
||||
HeapMB float64 `json:"heap_mb"`
|
||||
Goroutines int `json:"goroutines"`
|
||||
CPUSeconds float64 `json:"cpu_s"` // all nodes together, cumulative
|
||||
SentMB float64 `json:"sent_mb"` // all nodes together, cumulative
|
||||
Nodes int `json:"nodes_live"` // nodes that are up
|
||||
}
|
||||
|
||||
type phaseSpan struct {
|
||||
Name string `json:"name"`
|
||||
From float64 `json:"from"`
|
||||
To float64 `json:"to"`
|
||||
}
|
||||
|
||||
// series is how long each of a kind of thing took to arrive; things that never
|
||||
// arrived are counted in Sent only.
|
||||
type series struct {
|
||||
What string `json:"what"`
|
||||
Sent int `json:"sent"`
|
||||
Ms []float64 `json:"ms"` // sorted
|
||||
At float64 `json:"at"` // seconds into the run when they were sent (first)
|
||||
}
|
||||
|
||||
type recorder struct {
|
||||
mu sync.Mutex
|
||||
t0 time.Time
|
||||
phases []phaseSpan
|
||||
samples []sample
|
||||
series map[string]*series
|
||||
rows map[string]string
|
||||
}
|
||||
|
||||
func newRecorder() *recorder {
|
||||
return &recorder{t0: time.Now(), series: map[string]*series{}, rows: map[string]string{}}
|
||||
}
|
||||
|
||||
func (r *recorder) now() float64 { return time.Since(r.t0).Seconds() }
|
||||
|
||||
// phase ends the running phase and begins a new one.
|
||||
func (r *recorder) phase(name string) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
t := r.now()
|
||||
if n := len(r.phases); n > 0 {
|
||||
r.phases[n-1].To = t
|
||||
}
|
||||
r.phases = append(r.phases, phaseSpan{Name: name, From: t, To: t})
|
||||
}
|
||||
|
||||
func (r *recorder) addSeries(key, what string, ps []*pair) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
sr := &series{What: what, Sent: len(ps), At: r.now()}
|
||||
for _, p := range ps {
|
||||
if p.done {
|
||||
sr.Ms = append(sr.Ms, float64(p.took.Microseconds())/1000)
|
||||
}
|
||||
}
|
||||
sort.Float64s(sr.Ms)
|
||||
r.series[key] = sr
|
||||
}
|
||||
|
||||
// run samples the process every second until stop is closed.
|
||||
func (r *recorder) run(s *sim, stop <-chan struct{}) {
|
||||
tick := time.NewTicker(time.Second)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case <-tick.C:
|
||||
}
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
var ru syscall.Rusage
|
||||
_ = syscall.Getrusage(syscall.RUSAGE_SELF, &ru)
|
||||
var sent int64
|
||||
live := 0
|
||||
s.mu.Lock()
|
||||
for _, n := range s.nodes {
|
||||
sent += n.bw.GetBandwidthTotals().TotalOut
|
||||
live++
|
||||
}
|
||||
s.mu.Unlock()
|
||||
r.mu.Lock()
|
||||
r.samples = append(r.samples, sample{
|
||||
T: r.now(), HeapMB: float64(m.HeapAlloc) / (1 << 20), Goroutines: runtime.NumGoroutine(),
|
||||
CPUSeconds: float64(ru.Utime.Nano()+ru.Stime.Nano()) / 1e9, SentMB: float64(sent) / (1 << 20), Nodes: live,
|
||||
})
|
||||
r.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// write saves the run as JSON.
|
||||
func (r *recorder) write(path string, meta map[string]any) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if n := len(r.phases); n > 0 {
|
||||
r.phases[n-1].To = r.now()
|
||||
}
|
||||
out := map[string]any{"meta": meta, "phases": r.phases, "samples": r.samples, "series": r.series, "rows": r.rows}
|
||||
b, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, b, 0o644)
|
||||
}
|
||||
Reference in New Issue
Block a user