-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
debug.go
56 lines (45 loc) · 1.07 KB
/
debug.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
// Copyright 2015 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package chihaya
import (
"flag"
"net/http"
"os"
"runtime/pprof"
_ "net/http/pprof"
"github.com/golang/glog"
)
var (
profile string
debugAddr string
profileFile *os.File
)
func init() {
flag.StringVar(&profile, "profile", "", "if non-empty, path to write CPU profiling data")
flag.StringVar(&debugAddr, "debug", "", "if non-empty, address to serve debug data")
}
func debugBoot() {
var err error
if debugAddr != "" {
go func() {
glog.Info("Starting debug HTTP on ", debugAddr)
glog.Fatal(http.ListenAndServe(debugAddr, nil))
}()
}
if profile != "" {
profileFile, err = os.Create(profile)
if err != nil {
glog.Fatalf("Failed to create profile file: %s\n", err)
}
pprof.StartCPUProfile(profileFile)
glog.Info("Started profiling")
}
}
func debugShutdown() {
if profileFile != nil {
profileFile.Close()
pprof.StopCPUProfile()
glog.Info("Stopped profiling")
}
}