-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
101 lines (91 loc) · 2.64 KB
/
init.go
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
package prof
import (
"code.google.com/p/mx3/util"
"flag"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime/pprof"
)
var (
Flag_cpuprof = flag.Bool("cpuprof", false, "Record gopprof CPU profile")
Flag_memprof = flag.Bool("memprof", false, "Recored gopprof memory profile")
Flag_gpuprof = flag.Bool("gpuprof", false, "Recored GPU profile")
)
func Init(OD string) {
initGPUProf(OD) // should be before initGPU
initCpuProf(OD)
initMemProf(OD)
}
func initCpuProf(OD string) {
if *Flag_cpuprof {
// start CPU profile to file
fname := OD + "cpu.pprof"
f, err := os.Create(fname)
util.FatalErr(err, "CPU profile")
err = pprof.StartCPUProfile(f)
util.FatalErr(err, "CPU profile")
log.Println("writing CPU profile to", fname)
// at exit: exec go tool pprof to generate SVG output
AtExit(func() {
pprof.StopCPUProfile()
me := procSelfExe()
outfile := fname + ".svg"
saveCmdOutput(outfile, "go", "tool", "pprof", "-svg", me, fname)
})
}
}
func initMemProf(OD string) {
if *Flag_memprof {
log.Println("memory profile enabled")
AtExit(func() {
fname := OD + "mem.pprof"
f, err := os.Create(fname)
defer f.Close()
util.LogErr(err, "memory profile") // during cleanup, should not panic/exit
log.Println("writing memory profile to", fname)
util.LogErr(pprof.WriteHeapProfile(f), "memory profile")
me := procSelfExe()
outfile := fname + ".svg"
saveCmdOutput(outfile, "go", "tool", "pprof", "-svg", "--inuse_objects", me, fname)
})
}
}
// Configuration for GPU profile output.
const CUDA_PROFILE_CONFIG = `
gpustarttimestamp
instructions
streamid
`
// called by init()
func initGPUProf(OD string) {
if *Flag_gpuprof {
util.PanicErr(os.Setenv("CUDA_PROFILE", "1"))
util.PanicErr(os.Setenv("CUDA_PROFILE_CSV", "1"))
out := OD + "gpuprofile.csv"
log.Println("writing GPU profile to", out)
util.PanicErr(os.Setenv("CUDA_PROFILE_LOG", out))
cfgfile := OD + "cudaprof.cfg"
util.PanicErr(os.Setenv("CUDA_PROFILE_CONFIG", cfgfile))
util.FatalErr(ioutil.WriteFile(cfgfile, []byte(CUDA_PROFILE_CONFIG), 0666), "gpuprof")
//AtExit(cuda.DeviceReset)
}
}
// Exec command and write output to outfile.
func saveCmdOutput(outfile string, cmd string, args ...string) {
log.Println("exec:", cmd, args, ">", outfile)
out, err := exec.Command(cmd, args...).Output() // TODO: stderr is ignored
if err != nil {
log.Printf("exec %v %v: %v: %v", cmd, args, err, string(out))
}
// on error: write anyway, clobbers output file.
e := ioutil.WriteFile(outfile, out, 0666)
util.LogErr(e, "writing", outfile)
}
// path to the executable.
func procSelfExe() string {
me, err := os.Readlink("/proc/self/exe")
util.PanicErr(err)
return me
}