| 1 |
// Copyright 2021 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 taking a goroutine profile with C traceback. |
| 8 |
|
| 9 |
/* |
| 10 |
// Defined in gprof_c.c. |
| 11 |
void CallGoSleep(void); |
| 12 |
void gprofCgoTraceback(void* parg); |
| 13 |
void gprofCgoContext(void* parg); |
| 14 |
*/ |
| 15 |
import "C" |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"io" |
| 20 |
"runtime" |
| 21 |
"runtime/pprof" |
| 22 |
"time" |
| 23 |
"unsafe" |
| 24 |
) |
| 25 |
|
| 26 |
func init() { |
| 27 |
register("GoroutineProfile", GoroutineProfile) |
| 28 |
} |
| 29 |
|
| 30 |
func GoroutineProfile() { |
| 31 |
runtime.SetCgoTraceback(0, unsafe.Pointer(C.gprofCgoTraceback), unsafe.Pointer(C.gprofCgoContext), nil) |
| 32 |
|
| 33 |
go C.CallGoSleep() |
| 34 |
go C.CallGoSleep() |
| 35 |
go C.CallGoSleep() |
| 36 |
time.Sleep(1 * time.Second) |
| 37 |
|
| 38 |
prof := pprof.Lookup("goroutine") |
| 39 |
prof.WriteTo(io.Discard, 1) |
| 40 |
fmt.Println("OK") |
| 41 |
} |
| 42 |
|
| 43 |
//export GoSleep |
| 44 |
func GoSleep() { |
| 45 |
time.Sleep(time.Hour) |
| 46 |
} |