From 6303b0c5369532c398b23d810d803499a2522fb7 Mon Sep 17 00:00:00 2001 From: Christopher Young Date: Thu, 7 Jan 2016 11:08:54 -0500 Subject: [PATCH] Use monotonic timer for uptime. Fixes #69. --- main/gen_gdl90.go | 7 +++++-- main/monotonic.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 main/monotonic.go diff --git a/main/gen_gdl90.go b/main/gen_gdl90.go index 9bc2c24f..c71f7695 100644 --- a/main/gen_gdl90.go +++ b/main/gen_gdl90.go @@ -717,7 +717,7 @@ func updateStatus() { } // Update Uptime value - globalStatus.Uptime = time.Since(timeStarted).Nanoseconds() / 1000000 + globalStatus.Uptime = int64(stratuxClock.Seconds) * 1000 } type ReplayWriter struct { @@ -1109,7 +1109,7 @@ func printStats() { <-statTimer.C var memstats runtime.MemStats runtime.ReadMemStats(&memstats) - log.Printf("stats [up since: %s]\n", humanize.Time(timeStarted)) + log.Printf("stats [started: %s]\n", humanize.RelTime(time.Time{}, stratuxClock.Time, "ago", "from now")) log.Printf(" - CPUTemp=%.02f deg C, MemStats.Alloc=%s, MemStats.Sys=%s, totalNetworkMessagesSent=%s\n", globalStatus.CPUTemp, humanize.Bytes(uint64(memstats.Alloc)), humanize.Bytes(uint64(memstats.Sys)), humanize.Comma(int64(totalNetworkMessagesSent))) log.Printf(" - UAT/min %s/%s [maxSS=%.02f%%], ES/min %s/%s\n, Total traffic targets tracked=%s", humanize.Comma(int64(globalStatus.UAT_messages_last_minute)), humanize.Comma(int64(globalStatus.UAT_messages_max)), float64(maxSignalStrength)/10.0, humanize.Comma(int64(globalStatus.ES_messages_last_minute)), humanize.Comma(int64(globalStatus.ES_messages_max)), humanize.Comma(int64(len(seenTraffic)))) if globalSettings.GPS_Enabled { @@ -1186,7 +1186,10 @@ func openReplayFile(fn string) ReadCloser { return ret } +var stratuxClock *monotonic + func main() { + stratuxClock = NewMonotonic() // Start our "stratux clock". // replayESFilename := flag.String("eslog", "none", "ES Log filename") replayUATFilename := flag.String("uatlog", "none", "UAT Log filename") diff --git a/main/monotonic.go b/main/monotonic.go new file mode 100644 index 00000000..026523e0 --- /dev/null +++ b/main/monotonic.go @@ -0,0 +1,27 @@ +package main + +import ( + "time" +) + +// Timer (since start). + +type monotonic struct { + Seconds uint64 + Time time.Time + ticker *time.Ticker +} + +func (m *monotonic) Watcher() { + for { + <-m.ticker.C + m.Seconds++ + m.Time = m.Time.Add(1 * time.Second) + } +} + +func NewMonotonic() *monotonic { + t := &monotonic{Seconds: 0, Time: time.Time{}, ticker: time.NewTicker(1 * time.Second)} + go t.Watcher() + return t +}