blob: 484b7f9d06cf54d44c3f5bbb1108ffbfa6882089 (
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
// Copyright 2018 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.
// This program outputs a CPU profile that includes
// both Go and Cgo stacks. This is used by the mapping info
// tests in runtime/pprof.
//
// If SETCGOTRACEBACK=1 is set, the CPU profile will includes
// PCs from C side but they will not be symbolized.
package main
/*
#include <stdint.h>
#include <stdlib.h>
int cpuHogCSalt1 = 0;
int cpuHogCSalt2 = 0;
void CPUHogCFunction0(int foo) {
int i;
for (i = 0; i < 100000; i++) {
if (foo > 0) {
foo *= foo;
} else {
foo *= foo + 1;
}
cpuHogCSalt2 = foo;
}
}
void CPUHogCFunction() {
CPUHogCFunction0(cpuHogCSalt1);
}
struct CgoTracebackArg {
uintptr_t context;
uintptr_t sigContext;
uintptr_t *buf;
uintptr_t max;
};
void CollectCgoTraceback(void* parg) {
struct CgoTracebackArg* arg = (struct CgoTracebackArg*)(parg);
arg->buf[0] = (uintptr_t)(CPUHogCFunction0);
arg->buf[1] = (uintptr_t)(CPUHogCFunction);
arg->buf[2] = 0;
};
*/
import "C"
import (
"log"
"os"
"runtime"
"runtime/pprof"
"time"
"unsafe"
)
func init() {
if v := os.Getenv("SETCGOTRACEBACK"); v == "1" {
// Collect some PCs from C-side, but don't symbolize.
runtime.SetCgoTraceback(0, unsafe.Pointer(C.CollectCgoTraceback), nil, nil)
}
}
func main() {
go cpuHogGoFunction()
go cpuHogCFunction()
runtime.Gosched()
if err := pprof.StartCPUProfile(os.Stdout); err != nil {
log.Fatal("can't start CPU profile: ", err)
}
time.Sleep(200 * time.Millisecond)
pprof.StopCPUProfile()
if err := os.Stdout.Close(); err != nil {
log.Fatal("can't write CPU profile: ", err)
}
}
var salt1 int
var salt2 int
func cpuHogGoFunction() {
for {
foo := salt1
for i := 0; i < 1e5; i++ {
if foo > 0 {
foo *= foo
} else {
foo *= foo + 1
}
salt2 = foo
}
runtime.Gosched()
}
}
func cpuHogCFunction() {
// Generates CPU profile samples including a Cgo call path.
for {
C.CPUHogCFunction()
runtime.Gosched()
}
}
|