-1

我想计算空闲的 CPU 周期。我试图在互联网上找到这个问题的答案。但答案并不令人满意。我询问计算空闲 CPU 周期,但答案是 CPU 利用率/CPU 使用率。

请告诉我如何用c语言计算给定时间间隔的空闲CPU周期?我正在研究减少 CPU 能量的速度设置算法调度

idle_cycles = hard_idle + soft_idle;
run_cycles += excess_cycles;
run_percent = run_cycles /
(idle_cycles + run_cycles);
next_excess = run_cycles -
speed * (run_cycles + soft_idle)
IF excess_cycles < 0. THEN
excess_cycles = 0.
energy = (run_cycles - excess_cycles) *
speed * speed;
IF excess_cycles > idle_cycles THEN
newspeed = 1.0;
ELSEIF run_percent > 0.7 THEN
newspeed = speed + 0.2;
ELSEIF run_percent < 0.5 THEN
newspeed = speed -
(0.6 - run_percent);
IF newspeed > 1.0 THEN
newspeed = 1.0;
IF newspeed < min_speed THEN
newspeed = min_speed;
speed = newspeed;
excess_cycles = next_excess;

在这个算法中,我遇到了我想用 c 计算的术语 idle_cycles。

4

1 回答 1

1

/proc/uptime变量文件

有一个变量文件,位于/proc/uptime,仅包含两个值:

  1. 以秒为单位的正常运行时间

  2. 以秒为单位的空闲时间

注意:如果您使用多个核心,则第二个值是 针对所有核心的空闲jiffies计数器。

我在那里使用无尽的 html编写了一个演示:

或简单的监视器:

请,rtfm:man proc

/proc/stat变量文件

第一行/proc/stat保存 cpu 和每个内核的计数器。

head -n3 /proc/stat
cpu  1500160 13226 337809 16064648 1475420 34 16142 0 0 0
cpu0 747501 6569 168513 8022626 742061 25 14478 0 0 0
cpu1 752659 6656 169296 8042022 733359 9 1664 0 0 0

第 4 个柜台在哪里idle time counter

man proc

man proc | sed  '/proc\/stat/,+19p;d'
   /proc/stat
          kernel/system statistics.   Varies  with  architecture.   Common
          entries include:

          cpu  3357 0 4313 1362393
                 The   amount  of  time,  measured  in  units  of  USER_HZ
                 (1/100ths  of  a  second  on  most   architectures,   use
                 sysconf(_SC_CLK_TCK) to obtain the right value), that the
                 system spent in various states:

                 user   (1) Time spent in user mode.

                 nice   (2) Time spent in  user  mode  with  low  priority
                        (nice).

                 system (3) Time spent in system mode.

                 idle   (4)  Time  spent  in  the  idle  task.  This value
                        should be USER_HZ times the second  entry  in  the
                        /proc/uptime pseudo-file.

为此,还有另一个无穷无尽的 html脚本:

于 2015-12-06T21:01:06.643 回答