go运行时状态信息

news/2024/6/1 22:04:19 标签: golang, 运行信息, 性能, pporf

go运行时状态信息

内存

runtime.ReadMemStats(&ms) 获取内存分配信息,可以定时收集,发送给时序数据库做监控统计

gc 数据

ReadGCStats 获取最近的GC统计信息,可以定时收集,发送给时序数据库做监控统计

协程数

runtime.NumGoroutine 获取当前协程数,可以定时收集,发送给时序数据库做监控统计

panic 堆栈信息

日志查询

pporf

实现


    flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to this file")
    flag.StringVar(&memprofile, "memprofile", "", "Write mem profile to this file")

    if cpuprofile != "" {
        f, err := os.Create(cpuprofile)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Could not create cpuprofile file: %s\n", err)
            return 1
        }
        if err := pprof.StartCPUProfile(f); err != nil {
            panic(err)
        }
        defer pprof.StopCPUProfile()
        f.Close()
    }
    if memprofile != "" {
        f1, err := os.Create(cpuprofile)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Could not create cpuprofile file: %s\n", err)
            return 1
        }
        runtime.GC() // get up-to-date statistics
        if pprof.WriteHeapProfile(f1); err != nil {
            panic(err)
        }
        f1.Close()
    }
一些可访问的/debug/pprof/目录下的路由:
cpu(CPU Profiling): `$HOST/debug/pprof/profile`,默认进行 30s 的 CPU Profiling,得到一个分析用的 profile 文件

block(Block Profiling):`$HOST/debug/pprof/block`,查看导致阻塞同步的堆栈跟踪

goroutine:`$HOST/debug/pprof/goroutine`,查看当前所有运行的 goroutines 堆栈跟踪

heap(Memory Profiling): `$HOST/debug/pprof/heap`,查看活动对象的内存分配情况

mutex(Mutex Profiling):`$HOST/debug/pprof/mutex`,查看导致互斥锁的竞争持有者的堆栈跟踪

threadcreate:`$HOST/debug/pprof/threadcreate`,查看创建新OS线程的堆栈跟踪

cmdline
allocs

ui交互
go tool pprof -http=:9000 http://localhost:7878/debug/pprof/profile?seconds=20
导出文件
go tool pprof http://localhost:7878/debug/pprof/profile?seconds=20 > cpu.out
go tool pprof -http=:9000 cpu.prof 或者 go-torch http://localhost:7878/debug/pprof/heap --colors mem  -f mem.svg

trace
go tool pprof http://localhost:7878/debug/pprof/trace?seconds=20 > trace.out
go tool trace -http='127.0.0.1:9001' trace.out

使用浏览器访问http://localhost:7878/debug/pprof/trace?seconds=20 下载trace日志文件
然后 go tool trace -http='127.0.0.1:9001' trace.out 分析

pprof也可以比较两个时间点的分配的内存的差值

导出时间点1的堆的profile: curl -s http://127.0.0.1:8080/debug/pprof/heap > base.heap
导出时间点2的堆的profile: curl -s http://127.0.0.1:8080/debug/pprof/heap > current.heap
比较这两个时间点的堆的差异: go tool pprof --base base.heap current.heap

gops

(stack,gc,setgc,memstats,version,stats,trace,pprof-heap,pprof-cpu)

race (协程竞争检测,开发环境加上-race编译选项进行竞态检查,需要消耗更多cpu和内存资源,影响性能

godebug 使用 gctrace (非侵入式查看 gc 信息,排查内存泄漏)


http://www.niftyadmin.cn/n/757471.html

相关文章

easyui数据表格显示复选框_关于数据透视表的介绍和使用

基础认知和操作:一、数据透视表的概念 数据透视表是用来从Excel数据列表、关系数据库文件等数据集的字段中总结信息的分析工具。所谓“透视”,可以理解为对原始数据具有透视功能,也就是可以从数据背后找到联系,从而将看似杂乱的数…

Linux下动态库(so)的搜索路径

程序编译时,通过-Wl, -rpath参数指定的路径;环境变量LD_LIBRARY_PATH指定的路径/etc/ld.so.conf或/etc/ld.so.conf.d/目录下任何一个文件中指定的路径/lib路径/usr/lib路径 加入动态链接库的路径 su - root echo "/usr/local/lib" >>…

依赖倒转原则

依赖倒转原则 抽象不应该依赖细节,细节应该依赖抽象 通俗的解释,以计算机系统为例,就是要针对接口编程,不要对实现编程,无论主板、CPU、内存、硬件都是在针对接口设计的,如果针对实现来设计,内存…

客户说发货慢怎么回复_智云通CRM:客户说你的产品太贵了,怎么办?

我们在做销售时,客户对我们说得最多的一句话就是:“你的产品太贵了!”我们一般都是怎么回答的?有的说:“我们的产品质量好,我们的服务好啊!”“用料好,价格自然高!”“请…

wrk 压测工具

wrk 压测工具 安装 git clone https://github.com/wg/wrk.git cd wrk make 常用参数类型 Usage: wrk <options> <url>Options:-c, --connections <N> Connections to keep open-d, --duration <T> Duration of test, e.g. 2s, 2m, 2h-t, …

jquery.chained与jquery.chained.remote使用以及区别

---恢复内容开始--- jquery.chained与jquery.chained.remote总体来说一个是静态的一个是动态的&#xff0c; jquery.chained是静态的&#xff0c;他可以显示和隐藏页面上select的选项&#xff0c;他是根据类来显示select的数据项 例如&#xff1a; <select id"mark&quo…

管道爬行机器人内部陀螺仪_CCTV管道检测机器人,小小机器本领大

“道路千万条&#xff0c;安全第一条。”道路上每天车来车往、川流不息&#xff0c;地面安全对于我们而言&#xff0c;已经很熟悉了。但是你知道“地下安全”吗&#xff1f;地下的各种管道就好比人类的内脏器官&#xff0c;需要时刻检查、保养。地下安全与地面安全一样&#xf…

gcc 版本管理工具 scl

1 安装 scl devtoolset对应gcc的版本 devtoolset-3对应gcc4.x.x版本 devtoolset-4对应gcc5.x.x版本 devtoolset-6对应gcc6.x.x版本 devtoolset-7对应gcc7.x.x版本 devtoolset-8对应gcc8.x.x版本 devtoolset-9对应gcc9.x.x版本 devtoolset-10对应gcc10.x.x版本# centos instal…