...
  
  
     1  
     2  
     3  
     4  
     5  
     6  
     7  package main
     8  
     9  
    10  
    11  
    12  
    35  import "C"
    36  
    37  import (
    38  	"bytes"
    39  	"fmt"
    40  	"internal/profile"
    41  	"os"
    42  	"runtime/pprof"
    43  	"time"
    44  )
    45  
    46  func init() {
    47  	register("CgoCallbackPprof", CgoCallbackPprof)
    48  }
    49  
    50  func CgoCallbackPprof() {
    51  	C.start_callback_pprof_thread()
    52  
    53  	var buf bytes.Buffer
    54  	if err := pprof.StartCPUProfile(&buf); err != nil {
    55  		fmt.Printf("Error starting CPU profile: %v\n", err)
    56  		os.Exit(1)
    57  	}
    58  	time.Sleep(1 * time.Second)
    59  	pprof.StopCPUProfile()
    60  
    61  	p, err := profile.Parse(&buf)
    62  	if err != nil {
    63  		fmt.Printf("Error parsing profile: %v\n", err)
    64  		os.Exit(1)
    65  	}
    66  
    67  	foundCallee := false
    68  	for _, s := range p.Sample {
    69  		funcs := flattenFrames(s)
    70  		if len(funcs) == 0 {
    71  			continue
    72  		}
    73  
    74  		leaf := funcs[0]
    75  		if leaf.Name != "main.go_callback1_callee" {
    76  			continue
    77  		}
    78  		foundCallee = true
    79  
    80  		if len(funcs) < 2 {
    81  			fmt.Printf("Profile: %s\n", p)
    82  			frames := make([]string, len(funcs))
    83  			for i := range funcs {
    84  				frames[i] = funcs[i].Name
    85  			}
    86  			fmt.Printf("FAIL: main.go_callback1_callee sample missing caller in frames %v\n", frames)
    87  			os.Exit(1)
    88  		}
    89  
    90  		if funcs[1].Name != "main.go_callback1" {
    91  			
    92  			fmt.Printf("Profile: %s\n", p)
    93  			frames := make([]string, len(funcs))
    94  			for i := range funcs {
    95  				frames[i] = funcs[i].Name
    96  			}
    97  			fmt.Printf("FAIL: main.go_callback1_callee sample caller got %s want main.go_callback1 in frames %v\n", funcs[1].Name, frames)
    98  			os.Exit(1)
    99  		}
   100  	}
   101  
   102  	if !foundCallee {
   103  		fmt.Printf("Missing main.go_callback1_callee sample in profile %s\n", p)
   104  		os.Exit(1)
   105  	}
   106  
   107  	fmt.Printf("OK\n")
   108  }
   109  
   110  
   111  func flattenFrames(s *profile.Sample) []*profile.Function {
   112  	ret := make([]*profile.Function, 0, len(s.Location))
   113  	for _, loc := range s.Location {
   114  		for _, line := range loc.Line {
   115  			ret = append(ret, line.Function)
   116  		}
   117  	}
   118  	return ret
   119  }
   120  
   121  
   122  func go_callback1() {
   123  	
   124  	
   125  	go_callback1_callee()
   126  }
   127  
   128  func go_callback1_callee() {
   129  	C.c_callback()
   130  
   131  	
   132  	for {
   133  	}
   134  }
   135  
   136  
   137  func go_callback2() {
   138  }
   139  
View as plain text