-
Notifications
You must be signed in to change notification settings - Fork 259
/
profiles.go
122 lines (110 loc) · 3.55 KB
/
profiles.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2018, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package common
import (
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
)
// WriteRuntimeProfiles writes Go runtime profile information to a set of
// files in the specified output directory. The profiles include "heap",
// "goroutine", and other selected profiles from:
// https://golang.org/pkg/runtime/pprof/#Profile.
//
// The SampleDurationSeconds inputs determine how long to wait and sample
// profiles that require active sampling. When set to 0, these profiles are
// skipped.
func WriteRuntimeProfiles(
logger Logger,
outputDirectory string,
filenameSuffix string,
blockSampleDurationSeconds int,
cpuSampleDurationSeconds int) {
openProfileFile := func(profileName string) *os.File {
filename := filepath.Join(outputDirectory, profileName+".profile")
if filenameSuffix != "" {
filename += "." + filenameSuffix
}
file, err := os.OpenFile(
filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
logger.WithContextFields(
LogFields{
"error": err,
"fileName": filename}).Error("open profile file failed")
return nil
}
return file
}
writeProfile := func(profileName string) {
file := openProfileFile(profileName)
if file == nil {
return
}
err := pprof.Lookup(profileName).WriteTo(file, 1)
file.Close()
if err != nil {
logger.WithContextFields(
LogFields{
"error": err,
"profileName": profileName}).Error("write profile failed")
}
}
// TODO: capture https://golang.org/pkg/runtime/debug/#WriteHeapDump?
// May not be useful in its current state, as per:
// https://groups.google.com/forum/#!topic/golang-dev/cYAkuU45Qyw
// Write goroutine, heap, and threadcreate profiles
// https://golang.org/pkg/runtime/pprof/#Profile
writeProfile("goroutine")
writeProfile("heap")
writeProfile("threadcreate")
// Write CPU profile (after sampling)
// https://golang.org/pkg/runtime/pprof/#StartCPUProfile
if cpuSampleDurationSeconds > 0 {
file := openProfileFile("cpu")
if file != nil {
logger.WithContext().Info("start cpu profiling")
err := pprof.StartCPUProfile(file)
if err != nil {
logger.WithContextFields(
LogFields{"error": err}).Error("StartCPUProfile failed")
} else {
time.Sleep(time.Duration(cpuSampleDurationSeconds) * time.Second)
pprof.StopCPUProfile()
logger.WithContext().Info("end cpu profiling")
}
file.Close()
}
}
// Write block profile (after sampling)
// https://golang.org/pkg/runtime/pprof/#Profile
if blockSampleDurationSeconds > 0 {
logger.WithContext().Info("start block/mutex profiling")
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
time.Sleep(time.Duration(blockSampleDurationSeconds) * time.Second)
runtime.SetBlockProfileRate(0)
runtime.SetMutexProfileFraction(0)
logger.WithContext().Info("end block/mutex profiling")
writeProfile("block")
writeProfile("mutex")
}
}