-
Notifications
You must be signed in to change notification settings - Fork 253
/
profiling.go
95 lines (90 loc) · 2.2 KB
/
profiling.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
package main
import (
"os"
"runtime/pprof"
"runtime/trace"
"time"
"github.com/rfjakob/gocryptfs/v2/internal/exitcodes"
"github.com/rfjakob/gocryptfs/v2/internal/tlog"
)
// setupCpuprofile is called to handle a non-empty "-cpuprofile" cli argument
func setupCpuprofile(cpuprofileArg string) func() {
tlog.Info.Printf("Writing CPU profile to %s", cpuprofileArg)
f, err := os.Create(cpuprofileArg)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(exitcodes.Profiler)
}
err = pprof.StartCPUProfile(f)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(exitcodes.Profiler)
}
return func() {
pprof.StopCPUProfile()
}
}
// setupTrace is called to handle a non-empty "-memprofile" cli argument
func setupMemprofile(memprofileArg string) func() {
tlog.Info.Printf("Will write memory profile to %q", memprofileArg)
f, err := os.Create(memprofileArg)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(exitcodes.Profiler)
}
exiting := false
// Write the memory profile to disk every 60 seconds to get the in-use
// memory stats.
go func() {
for {
time.Sleep(60 * time.Second)
if exiting {
return
}
_, err = f.Seek(0, 0)
if err != nil {
tlog.Warn.Printf("memprofile: Seek failed: %v", err)
return
}
err = f.Truncate(0)
if err != nil {
tlog.Warn.Printf("memprofile: Truncate failed: %v", err)
return
}
err = pprof.WriteHeapProfile(f)
if err == nil {
tlog.Info.Printf("memprofile: periodic write to %q succeeded",
memprofileArg)
} else {
tlog.Warn.Printf("memprofile: periodic WriteHeapProfile failed: %v", err)
return
}
}
}()
// Final write on exit.
return func() {
exiting = true
err = pprof.WriteHeapProfile(f)
if err != nil {
tlog.Warn.Printf("memprofile: on-exit WriteHeapProfile failed: %v", err)
}
f.Close()
}
}
// setupTrace is called to handle a non-empty "-trace" cli argument
func setupTrace(traceArg string) func() {
tlog.Info.Printf("Writing execution trace to %s", traceArg)
f, err := os.Create(traceArg)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(exitcodes.Profiler)
}
err = trace.Start(f)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(exitcodes.Profiler)
}
return func() {
trace.Stop()
}
}