mirror of
https://github.com/astaxie/beego.git
synced 2024-11-05 15:20:54 +00:00
158 lines
3.5 KiB
Go
158 lines
3.5 KiB
Go
|
package admin
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"runtime"
|
||
|
"runtime/debug"
|
||
|
"runtime/pprof"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var heapProfileCounter int32
|
||
|
var startTime = time.Now()
|
||
|
var pid int
|
||
|
|
||
|
func init() {
|
||
|
pid = os.Getpid()
|
||
|
}
|
||
|
|
||
|
func ProcessInput(input string) {
|
||
|
switch input {
|
||
|
case "lookup goroutine":
|
||
|
p := pprof.Lookup("goroutine")
|
||
|
p.WriteTo(os.Stdout, 2)
|
||
|
case "lookup heap":
|
||
|
p := pprof.Lookup("heap")
|
||
|
p.WriteTo(os.Stdout, 2)
|
||
|
case "lookup threadcreate":
|
||
|
p := pprof.Lookup("threadcreate")
|
||
|
p.WriteTo(os.Stdout, 2)
|
||
|
case "lookup block":
|
||
|
p := pprof.Lookup("block")
|
||
|
p.WriteTo(os.Stdout, 2)
|
||
|
case "start cpuprof":
|
||
|
StartCPUProfile()
|
||
|
case "stop cpuprof":
|
||
|
StopCPUProfile()
|
||
|
case "get memprof":
|
||
|
MemProf()
|
||
|
case "gc summary":
|
||
|
PrintGCSummary()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func MemProf() {
|
||
|
if f, err := os.Create("mem-" + strconv.Itoa(pid) + ".memprof"); err != nil {
|
||
|
log.Fatal("record memory profile failed: %v", err)
|
||
|
} else {
|
||
|
runtime.GC()
|
||
|
pprof.WriteHeapProfile(f)
|
||
|
f.Close()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func StartCPUProfile() {
|
||
|
f, err := os.Create("cpu-" + strconv.Itoa(pid) + ".pprof")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
pprof.StartCPUProfile(f)
|
||
|
}
|
||
|
|
||
|
func StopCPUProfile() {
|
||
|
pprof.StopCPUProfile()
|
||
|
}
|
||
|
|
||
|
func PrintGCSummary() {
|
||
|
memStats := &runtime.MemStats{}
|
||
|
runtime.ReadMemStats(memStats)
|
||
|
gcstats := &debug.GCStats{PauseQuantiles: make([]time.Duration, 100)}
|
||
|
debug.ReadGCStats(gcstats)
|
||
|
|
||
|
printGC(memStats, gcstats)
|
||
|
}
|
||
|
|
||
|
func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats) {
|
||
|
|
||
|
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()
|
||
|
|
||
|
fmt.Printf("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()
|
||
|
|
||
|
fmt.Printf("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)))
|
||
|
}
|
||
|
|
||
|
// human readable format
|
||
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|