forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
107 lines (92 loc) · 2.85 KB
/
dump.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
// +build !windows
// Copyright (c) 2016-2018 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logutils
import (
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/felix/config"
)
func DumpHeapMemoryProfile(fileName string) {
logCxt := log.WithField("file", fileName)
logCxt.Info("Asked to create a memory profile.")
fileName = renderFileName(fileName)
// Open a file with that name.
f, err := os.Create(fileName)
if err != nil {
logCxt.WithError(err).Error("Could not create memory profile file")
return
}
defer f.Close()
logCxt.Info("Writing memory profile...")
if err := pprof.WriteHeapProfile(f); err != nil {
logCxt.WithError(err).Error("Could not write memory profile")
}
logCxt.Info("Finished writing memory profile")
}
func DumpCPUProfile(fileName string) {
logCxt := log.WithField("file", fileName)
logCxt.Info("Asked to create a CPU profile.")
fileName = renderFileName(fileName)
// Open a file with that name.
f, err := os.Create(fileName)
if err != nil {
logCxt.WithError(err).Error("Could not create CPU profile file")
return
}
defer f.Close()
logCxt.Info("Writing CPU profile...")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
time.Sleep(10 * time.Second)
logCxt.Info("Finished writing CPU profile")
}
func renderFileName(template string) string {
// If the configured file name includes "<timestamp>", replace that with the current
// time.
if strings.Contains(template, "<timestamp>") {
timestamp := time.Now().Format("2006-01-02-15:04:05")
return strings.Replace(template, "<timestamp>", timestamp, 1)
}
return template
}
func RegisterProfilingSignalHandlers(configParams *config.Config) {
if configParams.DebugMemoryProfilePath != "" {
// On receipt of SIGUSR1, write out heap profile.
usr1SignalChan := make(chan os.Signal, 1)
signal.Notify(usr1SignalChan, syscall.SIGUSR1)
go func() {
for {
<-usr1SignalChan
DumpHeapMemoryProfile(configParams.DebugMemoryProfilePath)
}
}()
}
if configParams.DebugCPUProfilePath != "" {
// On receipt of SIGUSR2, write out CPU profile.
usr2SignalChan := make(chan os.Signal, 10)
signal.Notify(usr2SignalChan, syscall.SIGUSR2)
go func() {
for {
<-usr2SignalChan
DumpCPUProfile(configParams.DebugCPUProfilePath)
}
}()
}
}