Skip to content

Commit

Permalink
Add guardrail logging of when loops take too long.
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed Jul 29, 2024
1 parent 367750e commit 786f39e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion field/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (

const (
arenaLoopPeriodMs = 10
arenaLoopWarningUs = 3000
dsPacketPeriodMs = 500
dsPacketWarningMs = 550
periodicTaskPeriodSec = 30
matchEndScoreDwellSec = 3
postTimeoutSec = 4
Expand Down Expand Up @@ -644,7 +646,11 @@ func (arena *Arena) Update() {
}

// Send a packet if at a period transition point or if it's been long enough since the last one.
if sendDsPacket || time.Since(arena.lastDsPacketTime).Seconds()*1000 >= dsPacketPeriodMs {
msSinceLastDsPacket := int(time.Since(arena.lastDsPacketTime).Seconds() * 1000)
if sendDsPacket || msSinceLastDsPacket >= dsPacketPeriodMs {
if msSinceLastDsPacket >= dsPacketWarningMs && arena.lastDsPacketTime.After(time.Time{}) {
log.Printf("Too long since last driver station packet: %dms", msSinceLastDsPacket)
}
arena.sendDsPacket(auto, enabled)
arena.ArenaStatusNotifier.Notify()
}
Expand All @@ -670,11 +676,16 @@ func (arena *Arena) Run() {
go arena.Plc.Run()

for {
loopStartTime := time.Now()
arena.Update()
if time.Since(arena.lastPeriodicTaskTime).Seconds() >= periodicTaskPeriodSec {
arena.lastPeriodicTaskTime = time.Now()
go arena.runPeriodicTasks()
}
if time.Since(loopStartTime).Microseconds() > arenaLoopWarningUs {
log.Printf("Arena loop iteration took too long: %dus", time.Since(loopStartTime).Microseconds())
}

time.Sleep(time.Millisecond * arenaLoopPeriodMs)
}
}
Expand Down

0 comments on commit 786f39e

Please sign in to comment.