How to get CPU usage

前端 未结 6 1801
刺人心
刺人心 2020-12-22 23:55

My Go program needs to know the current cpu usage percentage of all system and user processes.

How can I obtain that?

6条回答
  •  隐瞒了意图╮
    2020-12-23 00:27

    Check out this package http://github.com/c9s/goprocinfo, goprocinfo package does the parsing stuff for you.

    stat, err := linuxproc.ReadStat("/proc/stat")
    if err != nil {
        t.Fatal("stat read fail")
    }
    
    for _, s := range stat.CPUStats {
        // s.User
        // s.Nice
        // s.System
        // s.Idle
        // s.IOWait
    }
    

提交回复
热议问题