From: Meng Zhuo Date: Fri, 17 Dec 2021 06:15:42 +0000 (+0800) Subject: runtime: invalid negative frequency while tracing X-Git-Tag: go1.18beta2~180 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a78532a4121d26c33ee3ce69b3dda3a608f5a077;p=gostls13.git runtime: invalid negative frequency while tracing The riscv64 Hifive Unmatched is the only platform that failed on testcase TestAnalyzeAnnotations occasionally after CL 332954 had merged. The failure happens when ticks per second (freq) is over 1e12 which causing the timestamps of two events are same. There are 2 reasons causing big frequency: 1. RDCYCLE is HART based according to the riscv manual which makes negative ticks delta 2. negative float64 -> uint64 is undefined and "lucky" negative float is too big to handle for trace For #46737 Change-Id: I1f3c1ac31aae249969000c719c32aaf5a66d29a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/373034 Trust: Zhuo Meng Run-TryBot: Zhuo Meng TryBot-Result: Gopher Robot Reviewed-by: Cherry Mui Reviewed-by: Michael Knyszek --- diff --git a/src/runtime/asm_riscv64.s b/src/runtime/asm_riscv64.s index 0e813189d4..2a4837b399 100644 --- a/src/runtime/asm_riscv64.s +++ b/src/runtime/asm_riscv64.s @@ -81,7 +81,10 @@ TEXT setg_gcc<>(SB),NOSPLIT,$0-0 // func cputicks() int64 TEXT runtime·cputicks(SB),NOSPLIT,$0-8 - RDCYCLE A0 + // RDTIME to emulate cpu ticks + // RDCYCLE reads counter that is per HART(core) based + // according to the riscv manual, see issue 46737 + RDTIME A0 MOV A0, ret+0(FP) RET diff --git a/src/runtime/trace.go b/src/runtime/trace.go index 5b14a5f553..71a29d4316 100644 --- a/src/runtime/trace.go +++ b/src/runtime/trace.go @@ -426,6 +426,9 @@ func ReadTrace() []byte { trace.footerWritten = true // Use float64 because (trace.ticksEnd - trace.ticksStart) * 1e9 can overflow int64. freq := float64(trace.ticksEnd-trace.ticksStart) * 1e9 / float64(trace.timeEnd-trace.timeStart) / traceTickDiv + if freq <= 0 { + throw("trace: ReadTrace got invalid frequency") + } trace.lockOwner = nil unlock(&trace.lock) var data []byte