Commit d33ee0c5 authored by Rob Pike's avatar Rob Pike

testing: read coverage counters atomically

For -mode=atomic, we need to read the counters
using an atomic load to avoid a race. Not worth worrying
about when -mode=atomic is set during generation
of the profile, so we use atomic loads always.

Fixes #8630.

LGTM=rsc
R=dvyukov, rsc
CC=golang-codereviews
https://golang.org/cl/141800043
parent eafa4fff
...@@ -9,6 +9,7 @@ package testing ...@@ -9,6 +9,7 @@ package testing
import ( import (
"fmt" "fmt"
"os" "os"
"sync/atomic"
) )
// CoverBlock records the coverage data for a single basic block. // CoverBlock records the coverage data for a single basic block.
...@@ -44,8 +45,8 @@ type Cover struct { ...@@ -44,8 +45,8 @@ type Cover struct {
func Coverage() float64 { func Coverage() float64 {
var n, d int64 var n, d int64
for _, counters := range cover.Counters { for _, counters := range cover.Counters {
for _, c := range counters { for i := range counters {
if c > 0 { if atomic.LoadUint32(&counters[i]) > 0 {
n++ n++
} }
d++ d++
...@@ -84,11 +85,13 @@ func coverReport() { ...@@ -84,11 +85,13 @@ func coverReport() {
} }
var active, total int64 var active, total int64
var count uint32
for name, counts := range cover.Counters { for name, counts := range cover.Counters {
blocks := cover.Blocks[name] blocks := cover.Blocks[name]
for i, count := range counts { for i := range counts {
stmts := int64(blocks[i].Stmts) stmts := int64(blocks[i].Stmts)
total += stmts total += stmts
count = atomic.LoadUint32(&counts[i]) // For -mode=atomic.
if count > 0 { if count > 0 {
active += stmts active += stmts
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment