From a09ea59198faf85d41da6699220319089c98d86b Mon Sep 17 00:00:00 2001 From: qmuntal Date: Mon, 31 Jul 2023 17:33:40 +0200 Subject: [PATCH] runtime: cpu profiler to use high resolution timers on Windows The CPU profiler skip samples if the sampling rate is too high for the system timer resolution. This CL uses high resolution timers on Windows when available, to avoid this problem. Note that the default sampling rate (100Hz) is already too high for the Windows timer resolution (15.6ms), so this CL also improves the default Windows sampling coverage. Not adding regression tests, as they would be too flaky. Fixes #61665 Change-Id: Ifdadabc9ebaf56f397eac517bd0e5f1502b956b3 Reviewed-on: https://go-review.googlesource.com/c/go/+/514375 TryBot-Result: Gopher Robot Reviewed-by: Cherry Mui Run-TryBot: Quim Muntal Reviewed-by: David Chase --- src/runtime/os_windows.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go index f5c2429a05d14..6686a9053476d 100644 --- a/src/runtime/os_windows.go +++ b/src/runtime/os_windows.go @@ -1266,7 +1266,12 @@ func profileLoop() { func setProcessCPUProfiler(hz int32) { if profiletimer == 0 { - timer := stdcall3(_CreateWaitableTimerA, 0, 0, 0) + var timer uintptr + if haveHighResTimer { + timer = createHighResTimer() + } else { + timer = stdcall3(_CreateWaitableTimerA, 0, 0, 0) + } atomic.Storeuintptr(&profiletimer, timer) newm(profileLoop, nil, -1) }