Lesson-40 Go 的 pprof 的一些使用

Pprof

获取 pprof 状态

runtime_pprof "runtime/pprof"
profiles := runtime_pprof.Profiles() //获取所有  pprof
runtime_pprof.Lookup(string(name)) //直接查询指定 pprof

读取 CPU 状态

//fd 是一个 满足 io.Writer 协议的对象

if err = runtime_pprof.StartCPUProfile(fd); err != nil {
    return err
}

sec, _ := strconv.ParseInt(c.Request.FormValue("seconds"), 10, 64)
if sec == 0 {
    sec = 30
}

time.Sleep(time.Duration(sec) * time.Second)

runtime_pprof.StopCPUProfile()

查看状态

内存状态

    ms := &runtime.MemStats{}  // 这两句话获取内存状态
    runtime.ReadMemStats(ms)

    bufwrite := func(name string, value interface{}, remark string) {
        buf.WriteString(fmt.Sprintf("  %s:\t%v\t\t%s\n", name, value, remark))
    }
    mb := uint64(1024 * 1024)
    bufwrite("Alloc", ms.Alloc/mb, "Mbytes allocated and still in use")
    bufwrite("Sys  ", ms.Sys/mb, "Mbytes obtained from system (sum of XxxSys below)")
    bufwrite("Lookups", ms.Lookups/mb, "number of pointer lookups")
    bufwrite("Mallocs", ms.Mallocs/mb, "number of mallocs")
    bufwrite("Frees", ms.Frees/mb, "number of frees")
    bufwrite("Total", ms.TotalAlloc/mb, "Mbytes allocated (even if freed)")

    buf.WriteString("\n")
    bufwrite("HeapAlloc", ms.HeapAlloc/mb, "Mbytes allocated and still in use")
    bufwrite("HeapSys", ms.HeapSys/mb, "Mbytes obtained from system")
    bufwrite("HeapIdle", ms.HeapIdle/mb, "Mbytes in idle spans")
    bufwrite("HeapInuse", ms.HeapInuse/mb, "Mbytes in non-idle span")
    bufwrite("HeapReleased", ms.HeapReleased/mb, "Mbytes released to the OS")
    bufwrite("HeapObjects", ms.HeapObjects, "total number of allocated objects")

GC 状态

"runtime/debug"

count := 5

st := new(debug.GCStats)  //读取 GC 状态
st.Pause = make([]time.Duration, count, count)
st.PauseQuantiles = make([]time.Duration, count, count)
debug.ReadGCStats(st)

buf.WriteString(fmt.Sprintf("  %10v: \t%v\n", "Last GC", time.Now().Sub(st.LastGC).String()))
buf.WriteString(fmt.Sprintf("  %10v: \t(%v) %v\n", "Total", st.NumGC, st.PauseTotal.String()))
buf.WriteString(fmt.Sprintf("  %10v:", "Recent"))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容