...

Source file src/runtime/runtime_unix_test.go

Documentation: runtime

		 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  // Only works on systems with syscall.Close.
		 6  // We need a fast system call to provoke the race,
		 7  // and Close(-1) is nearly universally fast.
		 8  
		 9  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || plan9
		10  // +build aix darwin dragonfly freebsd linux netbsd openbsd plan9
		11  
		12  package runtime_test
		13  
		14  import (
		15  	"runtime"
		16  	"sync"
		17  	"sync/atomic"
		18  	"syscall"
		19  	"testing"
		20  )
		21  
		22  func TestGoroutineProfile(t *testing.T) {
		23  	// GoroutineProfile used to use the wrong starting sp for
		24  	// goroutines coming out of system calls, causing possible
		25  	// crashes.
		26  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(100))
		27  
		28  	var stop uint32
		29  	defer atomic.StoreUint32(&stop, 1) // in case of panic
		30  
		31  	var wg sync.WaitGroup
		32  	for i := 0; i < 4; i++ {
		33  		wg.Add(1)
		34  		go func() {
		35  			for atomic.LoadUint32(&stop) == 0 {
		36  				syscall.Close(-1)
		37  			}
		38  			wg.Done()
		39  		}()
		40  	}
		41  
		42  	max := 10000
		43  	if testing.Short() {
		44  		max = 100
		45  	}
		46  	stk := make([]runtime.StackRecord, 128)
		47  	for n := 0; n < max; n++ {
		48  		_, ok := runtime.GoroutineProfile(stk)
		49  		if !ok {
		50  			t.Fatalf("GoroutineProfile failed")
		51  		}
		52  	}
		53  
		54  	// If the program didn't crash, we passed.
		55  	atomic.StoreUint32(&stop, 1)
		56  	wg.Wait()
		57  }
		58  

View as plain text