aboutsummaryrefslogtreecommitdiff
path: root/weed/util/grace/pprof.go
blob: 620184c9ba6354807077601b045db7e912f2fa95 (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
package grace

import (
	"os"
	"runtime"
	"runtime/pprof"

	"github.com/seaweedfs/seaweedfs/weed/glog"
)

func SetupProfiling(cpuProfile, memProfile string) {
	if cpuProfile != "" {
		f, err := os.Create(cpuProfile)
		if err != nil {
			glog.Fatal(err)
		}
		runtime.SetBlockProfileRate(1)
		runtime.SetMutexProfileFraction(1)
		pprof.StartCPUProfile(f)
		OnInterrupt(func() {
			pprof.StopCPUProfile()

			// write block pprof
			blockF, err := os.Create(cpuProfile + ".block")
			if err != nil {
				return
			}
			p := pprof.Lookup("block")
			p.WriteTo(blockF, 0)
			blockF.Close()

			// write mutex pprof
			mutexF, err := os.Create(cpuProfile + ".mutex")
			if err != nil {
				return
			}
			p = pprof.Lookup("mutex")
			p.WriteTo(mutexF, 0)
			mutexF.Close()

		})
	}
	if memProfile != "" {
		runtime.MemProfileRate = 1
		f, err := os.Create(memProfile)
		if err != nil {
			glog.Fatal(err)
		}
		OnInterrupt(func() {
			pprof.WriteHeapProfile(f)
			f.Close()
		})
	}

}