forked from cpmech/gosl
/
profiling.go
102 lines (93 loc) · 2.99 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
96
97
98
99
100
101
102
// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package utl
import (
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"github.com/cpmech/gosl/chk"
"github.com/cpmech/gosl/io"
)
const (
KBSIZE = 1024.0
MBSIZE = 1048576.0
GBSIZE = 1073741824.0
)
// PrintMemStat prints memory statistics
func PrintMemStat(msg string) {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
io.PfYel("%s\n", msg)
io.Pfyel("Alloc = %v [KB] %v [MB] %v [GB]\n", mem.Alloc/KBSIZE, mem.Alloc/MBSIZE, mem.Alloc/GBSIZE)
io.Pfyel("HeapAlloc = %v [KB] %v [MB] %v [GB]\n", mem.HeapAlloc/KBSIZE, mem.HeapAlloc/MBSIZE, mem.HeapAlloc/GBSIZE)
io.Pfyel("Sys = %v [KB] %v [MB] %v [GB]\n", mem.Sys/KBSIZE, mem.Sys/MBSIZE, mem.Sys/GBSIZE)
io.Pfyel("HeapSys = %v [KB] %v [MB] %v [GB]\n", mem.HeapSys/KBSIZE, mem.HeapSys/MBSIZE, mem.HeapSys/GBSIZE)
io.Pfyel("TotalAlloc = %v [KB] %v [MB] %v [GB]\n", mem.TotalAlloc/KBSIZE, mem.TotalAlloc/MBSIZE, mem.TotalAlloc/GBSIZE)
io.Pfyel("Mallocs = %v\n", mem.Mallocs)
io.Pfyel("Frees = %v\n", mem.Frees)
}
// ProfCPU activates CPU profiling
// Note: returns a "stop()" function to be called before shutting down
func ProfCPU(dirout, filename string, silent bool) func() {
os.MkdirAll(dirout, 0777)
fn := filepath.Join(dirout, filename)
f, err := os.Create(fn)
if err != nil {
chk.Panic(_profiling_err1, "ProfCPU", err.Error())
}
if !silent {
io.Pfcyan("CPU profiling => %s\n", fn)
}
pprof.StartCPUProfile(f)
return func() {
pprof.StopCPUProfile()
f.Close()
if !silent {
io.Pfcyan("CPU profiling finished\n")
io.Pfcyan("run: go tool pprof <binary> %s/%s\n", dirout, filename)
}
}
}
// ProfMEM activates memory profiling
// Note: returns a "stop()" function to be called before shutting down
func ProfMEM(dirout, filename string, silent bool) func() {
os.MkdirAll(dirout, 0777)
fn := filepath.Join(dirout, filename)
f, err := os.Create(fn)
if err != nil {
chk.Panic(_profiling_err1, "ProfMEM", err.Error())
}
if !silent {
io.Pfcyan("MEM profiling => %s\n", fn)
}
return func() {
pprof.WriteHeapProfile(f)
f.Close()
if !silent {
io.Pfcyan("MEM profiling finished\n")
io.Pfcyan("run: go tool pprof <binary> %s/%s\n", dirout, filename)
}
}
}
// DoProf runs either CPU profiling or MEM profiling
// Input:
// silent -- show message
// option -- 1=CPU profiling, 2=memory profiling
// Output:
// 1) returns a "stop()" function to be called before shutting down
// 2) output files are saved to "/tmp/gosl/cpu.pprof"; or
// "/tmp/gosl/mem.pprof"
// 3) run analysis with (e.g.):
// go tool pprof binary /tmp/gosl/mem.pprof
func DoProf(silent bool, option int) func() {
if option == 2 {
return ProfMEM("/tmp/gosl", "mem.pprof", silent)
}
return ProfCPU("/tmp/gosl", "cpu.pprof", silent)
}
// error messages
var (
_profiling_err1 = "profiling.go: %s: cannot create file:\n%v"
)