Source file src/runtime/testdata/testprogcgo/aprof.go

     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  // This is a regression test for issue 14599, where profiling fails when the
    11  // function is the first C function. Exported functions are the first C
    12  // functions, so we use an exported function. Exported functions are created in
    13  // lexicographical order of source files, so this file is named aprof.go to
    14  // ensure its function is first.
    15  
    16  // extern void CallGoNop();
    17  import "C"
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"runtime/pprof"
    23  	"time"
    24  )
    25  
    26  func init() {
    27  	register("CgoCCodeSIGPROF", CgoCCodeSIGPROF)
    28  }
    29  
    30  //export GoNop
    31  func GoNop() {}
    32  
    33  func CgoCCodeSIGPROF() {
    34  	c := make(chan bool)
    35  	go func() {
    36  		<-c
    37  		start := time.Now()
    38  		for i := 0; i < 1e7; i++ {
    39  			if i%1000 == 0 {
    40  				if time.Since(start) > time.Second {
    41  					break
    42  				}
    43  			}
    44  			C.CallGoNop()
    45  		}
    46  		c <- true
    47  	}()
    48  
    49  	var buf bytes.Buffer
    50  	pprof.StartCPUProfile(&buf)
    51  	c <- true
    52  	<-c
    53  	pprof.StopCPUProfile()
    54  
    55  	fmt.Println("OK")
    56  }
    57  

View as plain text