Debian Codegraph @

libgo/go/runtime/testdata/testprogcgo/gprof.go

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