summaryrefslogtreecommitdiffstats
path: root/src/runtime/testdata/testprogcgo/gprof.go
blob: d453b4d0ce330ae1b2ee9c888f24125c30ebe69a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

// Test taking a goroutine profile with C traceback.

/*
// Defined in gprof_c.c.
void CallGoSleep(void);
void gprofCgoTraceback(void* parg);
void gprofCgoContext(void* parg);
*/
import "C"

import (
	"fmt"
	"io"
	"runtime"
	"runtime/pprof"
	"time"
	"unsafe"
)

func init() {
	register("GoroutineProfile", GoroutineProfile)
}

func GoroutineProfile() {
	runtime.SetCgoTraceback(0, unsafe.Pointer(C.gprofCgoTraceback), unsafe.Pointer(C.gprofCgoContext), nil)

	go C.CallGoSleep()
	go C.CallGoSleep()
	go C.CallGoSleep()
	time.Sleep(1 * time.Second)

	prof := pprof.Lookup("goroutine")
	prof.WriteTo(io.Discard, 1)
	fmt.Println("OK")
}

//export GoSleep
func GoSleep() {
	time.Sleep(time.Hour)
}