如何计算空闲CPU周期?

时间:2015-12-06 16:04:53

标签: c linux cpu-cycles

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

请告诉我如何用c语言计算给定时间间隔内的空闲CPU周期? 我正在研究速度设定算法Scheduling for Reduced CPU Energy

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;

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

来计算

1 个答案:

答案 0 :(得分:1)

/proc/uptime变量文件

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

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

  2. 空闲时间(秒)

  3. 注意:如果您使用多个核心,则第二个值是针对所有核心的空闲 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
    

    第四个计数器为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 脚本: