File tree Expand file tree Collapse file tree 1 file changed +4
-5
lines changed Expand file tree Collapse file tree 1 file changed +4
-5
lines changed Original file line number Diff line number Diff line change @@ -381,16 +381,15 @@ static NSUInteger const kMaxPercent = 100;
381381 return -1;
382382 }
383383
384- natural_t user = info.cpu_ticks[CPU_STATE_USER] + info .cpu_ticks[CPU_STATE_NICE]
385- - previous_info .cpu_ticks[CPU_STATE_USER ] - previous_info.cpu_ticks[CPU_STATE_NICE];
384+ natural_t user = info.cpu_ticks[CPU_STATE_USER] - previous_info .cpu_ticks[CPU_STATE_USER];
385+ natural_t nice = info .cpu_ticks[CPU_STATE_NICE ] - previous_info.cpu_ticks[CPU_STATE_NICE];
386386 natural_t system = info.cpu_ticks[CPU_STATE_SYSTEM] - previous_info.cpu_ticks[CPU_STATE_SYSTEM];
387387 natural_t idle = info.cpu_ticks[CPU_STATE_IDLE] - previous_info.cpu_ticks[CPU_STATE_IDLE];
388- natural_t total = user + system + idle;
388+ natural_t total = user + nice + system + idle;
389389 previous_info = info;
390390
391- return (user + system) * 100.0 / total;
391+ return (user + nice + system) * 100.0 / total;
392392}
393-
394393```
395394
396395上面代码通过计算 ` info ` 和 ` previous_info ` 的差值,分别得到在这几个模式下的 ` cpu_ticks ` ,除 ` idle ` 以外都属于 CPU 被占用的情况,最后就能求出 CPU 的占用率。
You can’t perform that action at this time.
0 commit comments