-
Notifications
You must be signed in to change notification settings - Fork 17
/
profile.go
61 lines (54 loc) · 1.6 KB
/
profile.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
/*************************************************************************
* Copyright 2017 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package utils
import (
"log"
"os"
"runtime"
"runtime/pprof"
)
const (
envCpuProfileName = `CPU_PROFILE`
envMemProfileName = `MEM_PROFILE`
)
var (
cpuProfFile *os.File
memProfFile *os.File
)
func StartProfile() {
if pth := os.Getenv(envCpuProfileName); pth != `` {
var err error
if cpuProfFile, err = os.Create(pth); err != nil {
log.Fatalf("failed to create CPU profile at %s - %v\n", pth, err)
} else if err = pprof.StartCPUProfile(cpuProfFile); err != nil {
log.Fatalf("failed to start CPU profile at %s - %v\n", pth, err)
}
}
}
func StopProfile() {
if cpuProfFile != nil {
pprof.StopCPUProfile()
if err := cpuProfFile.Close(); err != nil {
log.Printf("Failed to close CPU profile file - %v\n", err)
}
}
if pth := os.Getenv(envMemProfileName); pth != `` {
var err error
if memProfFile, err = os.Create(pth); err != nil {
log.Printf("failed to create MEM profile at %s - %v\n", pth, err)
} else {
runtime.GC()
if err = pprof.WriteHeapProfile(memProfFile); err != nil {
memProfFile.Close()
log.Printf("failed to start Memory profile at %s - %v\n", pth, err)
} else if err = memProfFile.Close(); err != nil {
log.Printf("failed to close Memory profile file %s - %v\n", pth, err)
}
}
}
}