gpt4 book ai didi

c - 如何计算空闲CPU周期?

转载 作者:行者123 更新时间:2023-11-30 19:14:25 24 4
gpt4 key购买 nike

我想计算空闲 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 来计算它。

最佳答案

/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

其中第四个计数器是空闲时间计数器

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脚本:

关于c - 如何计算空闲CPU周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com