1 Star 2 Fork 2

半斗米/RVBoards D1 CPU 使用率指示器

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
proc_stat.go 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
半斗米 提交于 2021-05-31 17:25 . 实现Go版本。
package main
import "os"
import "fmt"
type proc_stat_t struct {
user uint64
system uint64
nice uint64
idle uint64
iowait uint64
irq uint64
softirq uint64
total uint64
used uint64
}
func proc_stat_read(stat *proc_stat_t) {
file, err := os.Open("/proc/stat")
if err != nil {
panic(err)
}
fmt.Fscanf(file, "cpu %d %d %d %d %d %d %d",
&stat.user, &stat.system, &stat.nice, &stat.idle,
&stat.iowait, &stat.irq, &stat.softirq)
err = file.Close()
if err != nil {
panic(err)
}
}
func proc_stat_calc(stat *proc_stat_t) {
stat.total = stat.user;
stat.total += stat.nice;
stat.total += stat.system;
stat.total += stat.idle;
stat.total += stat.iowait;
stat.total += stat.irq;
stat.total += stat.softirq;
stat.used = stat.total - stat.idle;
}
func proc_stat_init(stat *proc_stat_t) {
proc_stat_read(stat)
proc_stat_calc(stat)
}
func proc_stat_get_cpu_usage(stat *proc_stat_t) uint8 {
var stat_latest proc_stat_t
proc_stat_read(&stat_latest)
proc_stat_calc(&stat_latest)
usage := (stat_latest.used - stat.used) * 100 / (stat_latest.total - stat.total)
stat.used = stat_latest.used;
stat.total = stat_latest.total;
return uint8(usage)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zoomdy/rvboards_d1_cpu_usage_indicator.git
git@gitee.com:zoomdy/rvboards_d1_cpu_usage_indicator.git
zoomdy
rvboards_d1_cpu_usage_indicator
RVBoards D1 CPU 使用率指示器
master

搜索帮助