From 3bd0b0a80dd78bacf814cbe51e427dac0fd231c3 Mon Sep 17 00:00:00 2001 From: Dmitriy Vyukov Date: Thu, 15 Aug 2013 00:20:36 +0400 Subject: [PATCH] runtime: fix SetBlockProfileRate It doughtily misses all possible corner cases. In particular on machines with <1GHz processors, SetBlockProfileRate(1) disables profiling. Fixes #6114. R=golang-dev, bradfitz, rsc CC=golang-dev https://golang.org/cl/12936043 --- src/pkg/runtime/mprof.goc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pkg/runtime/mprof.goc b/src/pkg/runtime/mprof.goc index 6e51ef3eb1..473e6e11cf 100644 --- a/src/pkg/runtime/mprof.goc +++ b/src/pkg/runtime/mprof.goc @@ -295,7 +295,17 @@ int64 runtime·blockprofilerate; // in CPU ticks void runtime·SetBlockProfileRate(intgo rate) { - runtime·atomicstore64((uint64*)&runtime·blockprofilerate, rate * runtime·tickspersecond() / (1000*1000*1000)); + int64 r; + + if(rate <= 0) + r = 0; // disable profiling + else { + // convert ns to cycles, use float64 to prevent overflow during multiplication + r = (float64)rate*runtime·tickspersecond()/(1000*1000*1000); + if(r == 0) + r = 1; + } + runtime·atomicstore64((uint64*)&runtime·blockprofilerate, r); } void -- 2.48.1