sebhildebrandt / systeminformation

System Information Library for Node.JS
MIT License
2.74k stars 311 forks source link

processes() return wrong CPU usage information #771

Closed emmkimme closed 1 year ago

emmkimme commented 1 year ago

Describe the bug when calling sti.processes, I see CPU values switching from 0 to an high value alternately at each call.

Actual behavior For instance, looking at my powerpoint.exe which has, according to taskmanager, cpu ~ 0%,, here are the results of a series of processes calls powerpnt.exe { "cpu": 0.004018299432017538 "cpu": 0 "cpu": 0.15141261641974887 "cpu": 0 "cpu": 36.95049504950495 "cpu": 0 "cpu": 37.270550678371904 } and CPU usage when > 0 is increasing.

Expected behavior I would expect a consistent series of values and closed to the taskmanager information.

Environment (please complete the following information):

Additional context According to the code

function calcProcStatWin(procStat, all, _cpu_old) {
  // calc
  let cpuu = 0;
  let cpus = 0;
  if (_cpu_old.all > 0 && _cpu_old.list[procStat.pid]) {
    cpuu = (procStat.utime - _cpu_old.list[procStat.pid].utime) / (all - _cpu_old.all) * 100; // user
    cpus = (procStat.stime - _cpu_old.list[procStat.pid].stime) / (all - _cpu_old.all) * 100; // system
  } else {
    cpuu = (procStat.utime) / (all) * 100; // user
    cpus = (procStat.stime) / (all) * 100; // system
  }
  return {
    pid: procStat.pid,
    utime: cpuu > 0 ? procStat.utime : 0,
    stime: cpus > 0 ? procStat.stime : 0,
    cpuu: cpuu > 0 ? cpuu : 0,
    cpus: cpus > 0 ? cpus : 0
  };
}

for powerpoint .exe my procstat utime/stime are always the same at each call call #1 cpuu = (procStat.utime (=n)- _cpu_old.list[procStat.pid].utime (=n)) === 0 so we store utime: cpuu > 0 ? procStat.utime : 0, === 0

call #2 cpuu = (procStat.utime (=n)- _cpu_old.list[procStat.pid].utime (=0)) === n so we store utime: cpuu > 0 ? procStat.utime : 0, === procStat.utime (=n)

call #3 cpuu = (procStat.utime (=n)- _cpu_old.list[procStat.pid].utime (=n)) === 0 so we store utime: cpuu > 0 ? procStat.utime : 0, === 0

etc....

to be fair, I do not understand why we are not always saving the utime / stime without any condition related to the cpu. I modified the code this way


  return {
    pid: procStat.pid,
    utime: procStat.utime,
    stime: procStat.stime,
    cpuu: cpuu > 0 ? cpuu : 0,
    cpus: cpus > 0 ? cpus : 0
  };

and it works

sebhildebrandt commented 1 year ago

@emmkimme I fixed it like suggested. Sorry, you are right, my fault! Version 5.17.7 just released. Can you test it on your side?

emmkimme commented 1 year ago

it works as expected, thanks for the prompt fix :-)