github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/runtime/testdata/testprogcgo/aprof.go (about)

     1  // Copyright 2016 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  package main
     6  
     7  // Test that SIGPROF received in C code does not crash the process
     8  // looking for the C code's func pointer.
     9  
    10  // The test fails when the function is the first C function.
    11  // The exported functions are the first C functions, so we use that.
    12  
    13  // extern void GoNop();
    14  import "C"
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  	"runtime/pprof"
    20  	"time"
    21  )
    22  
    23  func init() {
    24  	register("CgoCCodeSIGPROF", CgoCCodeSIGPROF)
    25  }
    26  
    27  //export GoNop
    28  func GoNop() {}
    29  
    30  func CgoCCodeSIGPROF() {
    31  	c := make(chan bool)
    32  	go func() {
    33  		for {
    34  			<-c
    35  			start := time.Now()
    36  			for i := 0; i < 1e7; i++ {
    37  				if i%1000 == 0 {
    38  					if time.Since(start) > time.Second {
    39  						break
    40  					}
    41  				}
    42  				C.GoNop()
    43  			}
    44  			c <- true
    45  		}
    46  	}()
    47  
    48  	var buf bytes.Buffer
    49  	pprof.StartCPUProfile(&buf)
    50  	c <- true
    51  	<-c
    52  	pprof.StopCPUProfile()
    53  
    54  	fmt.Println("OK")
    55  }