Render output as colored tables with pass/fail verdict

This commit is contained in:
flamingcow
2026-07-25 18:13:58 -07:00
parent 7f671a262e
commit 27f89d9916
3 changed files with 437 additions and 71 deletions
+184 -60
View File
@@ -26,14 +26,22 @@ const (
const wireOverhead = 24
type endpoint struct {
name string
idx int
mac [6]byte
mtu int
name string
tag string
idx int
mac [6]byte
mtu int
speed float64
}
func (e endpoint) macString() string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
e.mac[0], e.mac[1], e.mac[2], e.mac[3], e.mac[4], e.mac[5])
}
type direction struct {
label string
short string
tx endpoint
rx endpoint
spec *frameSpec
@@ -44,10 +52,12 @@ type direction struct {
rxFDs []int
reports chan string
prev sample
drops uint64
nicTX nicCounters
nicRX nicCounters
prev sample
drops uint64
nicTX nicCounters
nicRX nicCounters
nicTX0 nicCounters
nicRX0 nicCounters
}
type sample struct {
@@ -69,7 +79,11 @@ func lookupEndpoint(name string) (endpoint, error) {
}
var mac [6]byte
copy(mac[:], ifi.HardwareAddr)
return endpoint{name: name, idx: ifi.Index, mac: mac, mtu: ifi.MTU}, nil
e := endpoint{name: name, idx: ifi.Index, mac: mac, mtu: ifi.MTU}
if v, ok := readUint("/sys/class/net/" + name + "/speed"); ok {
e.speed = float64(v) / 1000
}
return e, nil
}
func parseSizes(s string) ([]int, error) {
@@ -151,12 +165,13 @@ func (d *direction) snapshot() sample {
s.badLen += r.badLen.Load()
}
for i := range d.streams {
expected := d.streams[i].maxSeq.Load() + 1
c := d.streams[i].count.Load()
if c == 0 {
continue
}
s.count += c
s.expected += d.streams[i].maxSeq.Load() + 1
s.expected += expected
}
return s
}
@@ -171,7 +186,20 @@ func gbps(bytes, frames uint64, secs float64) float64 {
return float64((bytes+frames*wireOverhead)*8) / secs / 1e9
}
func (d *direction) reportInterval(secs float64) string {
var intervalCols = []colSpec{
{title: "TIME", width: 6, right: true},
{title: "DIR", width: 5},
{title: "TX pps", width: 9, right: true},
{title: "TX Gb/s", width: 7, right: true},
{title: "RX pps", width: 9, right: true},
{title: "RX Gb/s", width: 7, right: true},
{title: "GAP", width: 9, right: true},
{title: "CRC", width: 5, right: true},
{title: "BADMAG", width: 6, right: true},
{title: "KDROP", width: 7, right: true},
}
func (d *direction) intervalRow(elapsed, secs, target float64) []string {
now := d.snapshot()
p := d.prev
d.prev = now
@@ -181,43 +209,53 @@ func (d *direction) reportInterval(secs float64) string {
rxF := now.rxFrames - p.rxFrames
rxB := now.rxBytes - p.rxBytes
lost := int64(now.expected) - int64(now.count)
before := d.drops
d.sampleDrops()
drops := d.drops - before
return fmt.Sprintf("%s tx %8.0f pps %6.2f Gb/s | rx %8.0f pps %6.2f Gb/s | lost(cum) %6d crc %d badmagic %d kdrop %d txshort %d txerr %d",
d.label,
float64(txF)/secs, gbps(txB, txF, secs),
float64(rxF)/secs, gbps(rxB, rxF, secs),
lost,
now.crcErr-p.crcErr,
now.badMagic-p.badMagic,
drops,
now.txShort-p.txShort,
now.txErrs-p.txErrs,
)
return []string{
fmt.Sprintf("%.0fs", elapsed),
paint(d.short, cCyan),
commas(uint64(float64(txF) / secs)),
rateCell(gbps(txB, txF, secs), target),
commas(uint64(float64(rxF) / secs)),
rateCell(gbps(rxB, rxF, secs), target),
gapCell(int64(now.expected) - int64(now.count)),
statusCell(now.crcErr - p.crcErr),
statusCell(now.badMagic - p.badMagic),
statusCell(d.drops - before),
}
}
func (d *direction) reportNIC() string {
func (d *direction) reportNIC() []string {
tx := readNIC(d.tx.name)
rx := readNIC(d.rx.name)
var parts []string
if s := tx.diff(d.nicTX); s != "" {
parts = append(parts, " "+d.tx.name+"(tx): "+s)
parts = append(parts, paint(" "+d.tx.name+" tx-side: "+s, cYellow))
}
if s := rx.diff(d.nicRX); s != "" {
parts = append(parts, " "+d.rx.name+"(rx): "+s)
parts = append(parts, paint(" "+d.rx.name+" rx-side: "+s, cYellow))
}
d.nicTX = tx
d.nicRX = rx
return strings.Join(parts, "\n")
return parts
}
func (d *direction) nicRunTotals() string {
var parts []string
if s := readNIC(d.tx.name).diff(d.nicTX0); s != "" {
parts = append(parts, d.tx.name+" tx-side: "+s)
}
if s := readNIC(d.rx.name).diff(d.nicRX0); s != "" {
parts = append(parts, d.rx.name+" rx-side: "+s)
}
return strings.Join(parts, "; ")
}
func buildDirection(label string, tx, rx endpoint, patIdx int, sizes []int, cfg config) (*direction, error) {
d := &direction{
label: label,
short: tx.tag + "→" + rx.tag,
tx: tx,
rx: rx,
spec: newFrameSpec(patIdx, rx.mac, tx.mac, sizes),
@@ -226,6 +264,8 @@ func buildDirection(label string, tx, rx endpoint, patIdx int, sizes []int, cfg
}
d.nicTX = readNIC(tx.name)
d.nicRX = readNIC(rx.name)
d.nicTX0 = d.nicTX
d.nicRX0 = d.nicRX
fm, err := fanoutMode(cfg.fanout, cfg.rxWorkers)
if err != nil {
@@ -335,9 +375,15 @@ func main() {
rxRing = flag.Uint("rx-ring", 8160, "required rx ring size, clamped to hardware maximum")
txRing = flag.Uint("tx-ring", 4096, "required tx ring size, clamped to hardware maximum")
setRings = flag.Bool("set-rings", true, "allow ring resizing at startup, which resets the link")
color = flag.String("color", "auto", "colored output: auto, always, never")
)
flag.Parse()
if err := initColor(*color); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
tcfg := tuneConfig{
enabled: *tune,
governor: *governor,
@@ -394,15 +440,20 @@ func run(aName, bName, sizesArg, patArg, fanout, txCPUsArg, rxCPUsArg string,
}
}
a.tag, b.tag = "A", "B"
ifnames := []string{a.name, b.name}
fmt.Println("host settings:")
var fatal []string
var tuneRows [][]string
for _, r := range applyTuning(tcfg, ifnames) {
fmt.Println(r)
tuneRows = append(tuneRows, []string{r.item, r.status(), r.detail()})
if r.fatal {
fatal = append(fatal, r.item)
}
}
fmt.Println(renderBox("HOST SETTINGS",
[]string{"CHECK", "STATUS", "DETAIL"},
[]bool{false, false, false}, tuneRows))
if len(fatal) > 0 {
return fmt.Errorf("cannot test with %s in this state", strings.Join(fatal, ", "))
}
@@ -442,18 +493,43 @@ func run(aName, bName, sizesArg, patArg, fanout, txCPUsArg, rxCPUsArg string,
}
}()
fmt.Printf("pattern=%s sizes=%v tx=%d rx=%d batch=%d verify=%v fanout=%s duplex=%v\n",
patterns[patIdx].name, sizes, txN, rxN, batch, verify, fanout, duplex)
for _, d := range dirs {
fmt.Printf(" %s %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x ethertype 0x%04x\n",
d.label,
d.tx.mac[0], d.tx.mac[1], d.tx.mac[2], d.tx.mac[3], d.tx.mac[4], d.tx.mac[5],
d.rx.mac[0], d.rx.mac[1], d.rx.mac[2], d.rx.mac[3], d.rx.mac[4], d.rx.mac[5],
etherType)
var linkRows [][]string
for _, e := range []endpoint{a, b} {
linkRows = append(linkRows, []string{
paint(e.tag, cCyan), e.name, e.macString(),
fmt.Sprintf("%.0f Gb/s", e.speed), fmt.Sprintf("%d", e.mtu),
})
}
fmt.Printf(" sndbuf %d rcvbuf %d (as granted by kernel)\n",
sockBufSize(dirs[0].txFDs[0], unix.SO_SNDBUF),
sockBufSize(dirs[0].rxFDs[0], unix.SO_RCVBUF))
fmt.Println(renderBox("LINKS",
[]string{"TAG", "INTERFACE", "MAC", "SPEED", "MTU"},
[]bool{false, false, false, true, true}, linkRows))
target := a.speed
if target <= 0 {
target = 10
}
sizeStrs := make([]string, len(sizes))
for i, s := range sizes {
sizeStrs[i] = fmt.Sprintf("%d", s)
}
fmt.Println(renderBox("TEST",
[]string{"SETTING", "VALUE"},
[]bool{false, false}, [][]string{
{"pattern", patterns[patIdx].name},
{"frame sizes", strings.Join(sizeStrs, " ")},
{"ethertype", fmt.Sprintf("0x%04x", etherType)},
{"workers", fmt.Sprintf("%d tx, %d rx per direction", txN, rxN)},
{"batch", fmt.Sprintf("%d frames per syscall", batch)},
{"payload verify", fmt.Sprintf("%v", verify)},
{"rx fanout", fanout},
{"duplex", fmt.Sprintf("%v", duplex)},
{"socket buffers", fmt.Sprintf("sndbuf %s, rcvbuf %s (granted)",
humanBytes(uint64(sockBufSize(dirs[0].txFDs[0], unix.SO_SNDBUF))),
humanBytes(uint64(sockBufSize(dirs[0].rxFDs[0], unix.SO_RCVBUF))))},
}))
fmt.Println(paint("GAP counts frames below the high-water mark not yet drained from the rx queues;", cDim))
fmt.Println(paint("it is provisional and settles at the drain. RESULTS at the end is authoritative.", cDim))
fmt.Println()
var doneTx, doneRx atomic.Bool
var wg sync.WaitGroup
@@ -483,6 +559,7 @@ func run(aName, bName, sizesArg, patArg, fanout, txCPUsArg, rxCPUsArg string,
}
last := time.Now()
stats := &streamTable{cols: intervalCols, headerEvery: 20}
loop:
for {
select {
@@ -493,18 +570,22 @@ loop:
case now := <-tick.C:
secs := now.Sub(last).Seconds()
last = now
for _, w := range verifyTuning(tcfg, ifnames) {
fmt.Println(w)
elapsed := now.Sub(start).Seconds()
for _, r := range verifyTuning(tcfg, ifnames) {
fmt.Printf(" %s host config drifted: %s — %s\n",
paint("!", cYellow), r.item, r.detail())
}
for _, d := range dirs {
fmt.Println(d.reportInterval(secs))
if s := d.reportNIC(); s != "" {
fmt.Println(s)
for _, line := range stats.emit(d.intervalRow(elapsed, secs, target)) {
fmt.Println(line)
}
for _, line := range d.reportNIC() {
fmt.Println(line)
}
for {
select {
case msg := <-d.reports:
fmt.Println(" " + d.label + ": " + msg)
fmt.Println(paint(" ✗ "+d.short+": "+msg, cRed))
continue
default:
}
@@ -520,22 +601,65 @@ loop:
doneRx.Store(true)
wg.Wait()
fmt.Println("---")
fmt.Println()
var resultRows, nicRows [][]string
var problems []string
for _, d := range dirs {
d.sampleDrops()
s := d.snapshot()
lost := int64(s.expected) - int64(s.count)
fmt.Printf("%s total tx %d frames %.2f GB | rx %d frames %.2f GB | lost %d (%.3g%%) crc %d badmagic %d badlen %d kdrop %d\n",
d.label,
s.txFrames, float64(s.txBytes)/1e9,
s.rxFrames, float64(s.rxBytes)/1e9,
lost, 100*float64(lost)/float64(max(s.expected, 1)),
s.crcErr, s.badMagic, s.badLen, d.drops)
fmt.Printf("%s avg tx %.2f Gb/s rx %.2f Gb/s over %.1fs\n",
d.label,
gbps(s.txBytes, s.txFrames, elapsed),
gbps(s.rxBytes, s.rxFrames, elapsed),
elapsed)
lostPct := 100 * float64(lost) / float64(max(s.expected, 1))
resultRows = append(resultRows, []string{
paint(d.short, cCyan),
commas(s.txFrames),
commas(s.rxFrames),
humanBytes(s.rxBytes),
rateCell(gbps(s.txBytes, s.txFrames, elapsed), target),
rateCell(gbps(s.rxBytes, s.rxFrames, elapsed), target),
lostCell(lost),
fmt.Sprintf("%.4f", lostPct),
statusCell(s.crcErr),
statusCell(s.badMagic),
statusCell(s.badLen),
statusCell(d.drops),
})
if n := d.nicRunTotals(); n != "" {
nicRows = append(nicRows, []string{paint(d.short, cCyan), n})
}
if lost != 0 {
problems = append(problems, fmt.Sprintf("%s lost %d frames (%.4f%%)", d.short, lost, lostPct))
}
if s.crcErr != 0 {
problems = append(problems, fmt.Sprintf("%s had %d payload CRC failures", d.short, s.crcErr))
}
if d.drops != 0 {
problems = append(problems, fmt.Sprintf("%s dropped %d frames in the kernel (host too slow, not the cable)", d.short, d.drops))
}
}
fmt.Println(renderBox(fmt.Sprintf("RESULTS after %.1fs", elapsed),
[]string{"DIR", "TX FRAMES", "RX FRAMES", "RX DATA", "TX Gb/s", "RX Gb/s", "LOST", "LOST %", "CRC", "BAD", "BADLEN", "KDROP"},
[]bool{false, true, true, true, true, true, true, true, true, true, true, true},
resultRows))
if len(nicRows) > 0 {
fmt.Println(renderBox("NIC COUNTER CHANGES DURING RUN",
[]string{"DIR", "COUNTERS"}, []bool{false, false}, nicRows))
problems = append(problems, "NIC error counters moved during the run")
}
fmt.Println()
if len(problems) == 0 {
fmt.Println(paint(" PASS ", cBold+"\x1b[42m\x1b[30m") + " " +
paint(fmt.Sprintf("every frame arrived, no errors, %s each way at %.2f Gb/s",
humanBytes(dirs[0].snapshot().rxBytes), target), cGreen))
} else {
fmt.Println(paint(" FAIL ", cBold+"\x1b[41m\x1b[37m"))
for _, p := range problems {
fmt.Println(paint(" ✗ "+p, cRed))
}
}
return nil
}