shirou / gopsutil

psutil for golang
Other
10.47k stars 1.58k forks source link

cpu.parseStatLine bug #1557

Closed heixiaoma closed 8 months ago

heixiaoma commented 9 months ago

device:iphone 14 pro

IMG_3081

shirou commented 8 months ago

What tool are you using ? iSH Shell or else? And could you share a reproducible source code?

heixiaoma commented 8 months ago

I think we can close this issue now. I am using version v3 and this problem no longer occurs. Thank you for your reply, buddy

heixiaoma commented 8 months ago
func SysInfo() string {
    defer func() {
        if err := recover(); err != nil {
        }
    }()
    info := bean.SysInfo{}
    v, err := mem.VirtualMemory()
    if err == nil {
        info.TotalMem = v.Total
        info.UseMem = v.Used
    }
    start, err := cpu.Times(false)
    if err == nil {
        var totalUsed float64
        // 计算每个 CPU 核心的使用率并相加
        for i := range start {
            totalUsed += (start[i].Total() - start[i].Idle) / start[i].Total()
        }
        info.CpuRate = (totalUsed / float64(len(start))) * 100.0
    }
    var m runtime.MemStats
    runtime.ReadMemStats(&m)
    // 程序占用的总内存
    totalMemory := m.Sys
    // 程序当前使用的内存
    usedMemory := m.Alloc

    info.HpTotalMem = float64(totalMemory)
    info.HpUseMem = float64(usedMemory)
    marshal, _ := json.Marshal(info)
    return string(marshal)
}

I will encounter this issue with the v2 version of the same code, but not with v3

heixiaoma commented 8 months ago

ac7c4fc22b1a14e81faf0b06ec2cea4

app logo

shirou commented 8 months ago

I think we can close this issue now. I am using version v3 and this problem no longer occurs.

Thank you for the information and code. v2 is not maintained anymore.