1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 04:20:55 +00:00
Beego/toolbox/profile.go

185 lines
4.8 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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.
2013-11-20 13:17:49 +00:00
package toolbox
import (
"fmt"
2013-11-15 13:45:51 +00:00
"io"
"log"
"os"
2014-08-15 07:17:11 +00:00
"path"
"runtime"
"runtime/debug"
"runtime/pprof"
"strconv"
"time"
)
var startTime = time.Now()
var pid int
func init() {
pid = os.Getpid()
}
2015-09-12 15:28:24 +00:00
// ProcessInput parse input command string
2013-11-15 13:45:51 +00:00
func ProcessInput(input string, w io.Writer) {
switch input {
case "lookup goroutine":
p := pprof.Lookup("goroutine")
2013-11-15 13:45:51 +00:00
p.WriteTo(w, 2)
case "lookup heap":
p := pprof.Lookup("heap")
2013-11-15 13:45:51 +00:00
p.WriteTo(w, 2)
case "lookup threadcreate":
p := pprof.Lookup("threadcreate")
2013-11-15 13:45:51 +00:00
p.WriteTo(w, 2)
case "lookup block":
p := pprof.Lookup("block")
2013-11-15 13:45:51 +00:00
p.WriteTo(w, 2)
2014-08-15 07:09:59 +00:00
case "get cpuprof":
2014-08-15 07:24:46 +00:00
GetCPUProfile(w)
case "get memprof":
2014-08-15 07:09:59 +00:00
MemProf(w)
case "gc summary":
2013-11-15 13:45:51 +00:00
PrintGCSummary(w)
}
}
2015-09-12 15:28:24 +00:00
// MemProf record memory profile in pprof
2014-08-15 07:09:59 +00:00
func MemProf(w io.Writer) {
filename := "mem-" + strconv.Itoa(pid) + ".memprof"
if f, err := os.Create(filename); err != nil {
fmt.Fprintf(w, "create file %s error %s\n", filename, err.Error())
log.Fatal("record heap profile failed: ", err)
} else {
runtime.GC()
pprof.WriteHeapProfile(f)
f.Close()
2014-08-15 07:09:59 +00:00
fmt.Fprintf(w, "create heap profile %s \n", filename)
2014-08-15 07:17:11 +00:00
_, fl := path.Split(os.Args[0])
fmt.Fprintf(w, "Now you can use this to check it: go tool pprof %s %s\n", fl, filename)
}
}
2015-09-12 15:28:24 +00:00
// GetCPUProfile start cpu profile monitor
2014-08-15 07:24:46 +00:00
func GetCPUProfile(w io.Writer) {
2014-08-15 07:09:59 +00:00
sec := 30
filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
f, err := os.Create(filename)
if err != nil {
2014-08-15 07:24:46 +00:00
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
2014-08-15 07:09:59 +00:00
log.Fatal("record cpu profile failed: ", err)
}
pprof.StartCPUProfile(f)
2014-08-15 07:09:59 +00:00
time.Sleep(time.Duration(sec) * time.Second)
pprof.StopCPUProfile()
2014-08-15 07:24:46 +00:00
fmt.Fprintf(w, "create cpu profile %s \n", filename)
2014-08-15 07:17:11 +00:00
_, fl := path.Split(os.Args[0])
2014-08-15 07:24:46 +00:00
fmt.Fprintf(w, "Now you can use this to check it: go tool pprof %s %s\n", fl, filename)
}
2015-09-12 15:28:24 +00:00
// PrintGCSummary print gc information to io.Writer
2013-11-15 13:45:51 +00:00
func PrintGCSummary(w io.Writer) {
memStats := &runtime.MemStats{}
runtime.ReadMemStats(memStats)
gcstats := &debug.GCStats{PauseQuantiles: make([]time.Duration, 100)}
debug.ReadGCStats(gcstats)
2013-11-15 13:45:51 +00:00
printGC(memStats, gcstats, w)
}
2013-11-15 13:45:51 +00:00
func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
if gcstats.NumGC > 0 {
lastPause := gcstats.Pause[0]
elapsed := time.Now().Sub(startTime)
overhead := float64(gcstats.PauseTotal) / float64(elapsed) * 100
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
2013-11-15 13:45:51 +00:00
fmt.Fprintf(w, "NumGC:%d Pause:%s Pause(Avg):%s Overhead:%3.2f%% Alloc:%s Sys:%s Alloc(Rate):%s/s Histogram:%s %s %s \n",
gcstats.NumGC,
toS(lastPause),
toS(avg(gcstats.Pause)),
overhead,
toH(memStats.Alloc),
toH(memStats.Sys),
toH(uint64(allocatedRate)),
toS(gcstats.PauseQuantiles[94]),
toS(gcstats.PauseQuantiles[98]),
toS(gcstats.PauseQuantiles[99]))
} else {
// while GC has disabled
elapsed := time.Now().Sub(startTime)
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
2013-11-15 13:45:51 +00:00
fmt.Fprintf(w, "Alloc:%s Sys:%s Alloc(Rate):%s/s\n",
toH(memStats.Alloc),
toH(memStats.Sys),
toH(uint64(allocatedRate)))
}
}
func avg(items []time.Duration) time.Duration {
var sum time.Duration
for _, item := range items {
sum += item
}
return time.Duration(int64(sum) / int64(len(items)))
}
2014-01-29 11:12:00 +00:00
// format bytes number friendly
func toH(bytes uint64) string {
switch {
case bytes < 1024:
return fmt.Sprintf("%dB", bytes)
case bytes < 1024*1024:
return fmt.Sprintf("%.2fK", float64(bytes)/1024)
case bytes < 1024*1024*1024:
return fmt.Sprintf("%.2fM", float64(bytes)/1024/1024)
default:
return fmt.Sprintf("%.2fG", float64(bytes)/1024/1024/1024)
}
}
// short string format
func toS(d time.Duration) string {
u := uint64(d)
if u < uint64(time.Second) {
switch {
case u == 0:
return "0"
case u < uint64(time.Microsecond):
return fmt.Sprintf("%.2fns", float64(u))
case u < uint64(time.Millisecond):
return fmt.Sprintf("%.2fus", float64(u)/1000)
default:
return fmt.Sprintf("%.2fms", float64(u)/1000/1000)
}
} else {
switch {
case u < uint64(time.Minute):
return fmt.Sprintf("%.2fs", float64(u)/1000/1000/1000)
case u < uint64(time.Hour):
return fmt.Sprintf("%.2fm", float64(u)/1000/1000/1000/60)
default:
return fmt.Sprintf("%.2fh", float64(u)/1000/1000/1000/60/60)
}
}
}