github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/runtime/norace_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // The file contains tests that can not run under race detector for some reason.
     6  // +build !race
     7  
     8  package runtime_test
     9  
    10  import (
    11  	"runtime"
    12  	"sync/atomic"
    13  	"testing"
    14  )
    15  
    16  // Syscall tests split stack between Entersyscall and Exitsyscall under race detector.
    17  func BenchmarkSyscall(b *testing.B) {
    18  	benchmarkSyscall(b, 0, 1)
    19  }
    20  
    21  func BenchmarkSyscallWork(b *testing.B) {
    22  	benchmarkSyscall(b, 100, 1)
    23  }
    24  
    25  func BenchmarkSyscallExcess(b *testing.B) {
    26  	benchmarkSyscall(b, 0, 4)
    27  }
    28  
    29  func BenchmarkSyscallExcessWork(b *testing.B) {
    30  	benchmarkSyscall(b, 100, 4)
    31  }
    32  
    33  func benchmarkSyscall(b *testing.B, work, excess int) {
    34  	const CallsPerSched = 1000
    35  	procs := runtime.GOMAXPROCS(-1) * excess
    36  	N := int32(b.N / CallsPerSched)
    37  	c := make(chan bool, procs)
    38  	for p := 0; p < procs; p++ {
    39  		go func() {
    40  			foo := 42
    41  			for atomic.AddInt32(&N, -1) >= 0 {
    42  				runtime.Gosched()
    43  				for g := 0; g < CallsPerSched; g++ {
    44  					runtime.Entersyscall()
    45  					for i := 0; i < work; i++ {
    46  						foo *= 2
    47  						foo /= 2
    48  					}
    49  					runtime.Exitsyscall()
    50  				}
    51  			}
    52  			c <- foo == 42
    53  		}()
    54  	}
    55  	for p := 0; p < procs; p++ {
    56  		<-c
    57  	}
    58  }