duannao3819 2019-01-22 11:34
浏览 31

如何减少CPU使用率?

I am developing application which monitors system related things CPU, RAM etc. Even with little data while testing I found whenever I run my code there is a spike in CPU usage usually by 30% for a second whenever its polling data and then goes back to normal. So is there any way to reduce this spike.

Here is the code:

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/cpu"
    "github.com/shirou/gopsutil/mem"
    "github.com/shirou/gopsutil/process"
    "github.com/shirou/gopsutil/load"
    "github.com/shirou/gopsutil/disk"
    "github.com/shirou/gopsutil/host"
    "reflect"
    "time"
)

func main() {

    driveC := "C:"

    processor, _ := cpu.Times(false)
    memory,_ := mem.VirtualMemory()
    cpuPercent,_ := cpu.Percent(time.Second,false)



    fmt.Println(reflect.TypeOf(processor))
    fmt.Println(reflect.TypeOf(memory))
    fmt.Println(reflect.TypeOf(cpuPercent))


    fmt.Println("CPU")
    fmt.Println("CPU name",processor[0].CPU)
    fmt.Println("User time",processor[0].User)
    fmt.Println("Idle time",processor[0].Idle)
    fmt.Println("System time",processor[0].System)
    fmt.Println("Nice time",processor[0].Nice)
    fmt.Println("IOwait time",processor[0].Iowait)
    fmt.Println("Irq time",processor[0].Irq)
    fmt.Println("SoftIrq time",processor[0].Softirq)



    fmt.Println("CPU Percent",cpuPercent)
    //fmt.Println(memory.Total)


    infoStat, _ := cpu.Info()
    fmt.Printf("
")
    fmt.Println("--> Hardware Info with Context:  ", infoStat)

    procCount, _ := cpu.ProcInfo()
    fmt.Printf("
")
    fmt.Println("--> Process Count:  ", procCount)

    fmt.Println("-----------------DISK--------------------------------->")

    driveUsage, _ := disk.Usage(driveC)
    fmt.Printf("
")
    fmt.Println("--> Disk Usage for :", driveUsage)

    partitionInfo, _ := disk.Partitions(true)
    fmt.Printf("
")
    fmt.Println("--> Partitions :", partitionInfo)

    folderInfo, _ := disk.IOCounters("C:", "D:")
    //fmt.Printf("
Disk IO counter remaining
")
    fmt.Printf("
")
    fmt.Println("-->Disk IO counter :", folderInfo)

    fmt.Printf("Docker remaining
")
    fmt.Printf("
")

    fmt.Println("-----------------Host--------------------------------->")

    hostInfo, _ := host.Info()
    fmt.Printf("
")
    fmt.Println("--> Host Info :", hostInfo)

    /*userName, _ := host.Users()
    fmt.Printf("
")
    fmt.Println("--> Username :", userName)*/

    fmt.Println("-----------------Load--------------------------------->")

    avgLoad, _ := load.Avg()
    fmt.Printf("
")
    fmt.Println("--> Average Load :", avgLoad)

    avgMisc, _ := load.Misc()
    fmt.Printf("
")
    fmt.Println("--> Misc :", avgMisc)

    fmt.Println("-----------------RAM--------------------------------->")

    virtMemory, _ := mem.VirtualMemory()
    fmt.Printf("
")
    fmt.Println("--> Virtual Memory :", virtMemory)

    swapMemory, _ := mem.SwapMemory()
    fmt.Printf("
")
    fmt.Println("--> Swap Memory :", swapMemory)

    //fmt.Println("Process running list")
    //processList()

}

OS - Windows 10, Programming Language - Golang

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记