本节重点介绍 :
- 分析主流程源码
- cpu使用时间采集源码查看
- cpu使用时间和cpu利用率的关系
分析源码流程
创建ProcessCollector对象
- 代码位置 D:\go_path\pkg\mod\github.com\ncabatoff\process-exporter@v0.7.5\cmd\process-exporter\main.go
- NewProcessCollector方法中 创建/proc文件系统对象,为采集最准备
- p.start开启采集主流程
- p.scrapeChan 会在Collect中接受来自 Describe中的待采集的指标
- 采集动作 p.scrape,通过p.Update拿到/proc文件系统中的结果
- 然后遍历赋值即可
cpu采集和利用率
- p.scrape函数中 namedprocess_namegroup_cpu_seconds_total user对应 CPUUserTime,system对应CPUSystemTime
- getStat通过读取 /proc/stat文件获取到对应的counter指标,代码位置D:\go_path\pkg\mod\github.com\ncabatoff\process-exporter@v0.7.5\proc\read.go
- GetCounts中对CPUUserTime计算方式为 /proc/stat文件中的第二列 cpu_user/userHZ ,userHZ=100
- 也就是cpu利用率可以用rate(namedprocess_namegroup_cpu_seconds_total)*100得到
- 100毫秒对应1个核
本节重点介绍 :
- 分析主流程源码
- cpu使用时间采集源码查看
- cpu使用时间和cpu利用率的关系