]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix SetBlockProfileRate
authorDmitriy Vyukov <dvyukov@google.com>
Wed, 14 Aug 2013 20:20:36 +0000 (00:20 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Wed, 14 Aug 2013 20:20:36 +0000 (00:20 +0400)
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

index 6e51ef3eb161050dd0e8289a1b834453e268c4bf..473e6e11cfdc329c6f744d0d1ecf64aee33bdcdc 100644 (file)
@@ -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