- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在/proc/stat 中有许多关于如何使用统计信息获取 CPU 利用率的帖子和引用资料。然而,它们中的大多数只使用 7+ CPU 统计信息中的四个(用户、nice、系统和空闲),忽略了 Linux 2.6 中剩余的 jiffie CPU 计数(iowait、irq、softirq)。
例如,参见 Determining CPU utilization .
我的问题是:iowait/irq/softirq 数是否也算在前四个数字之一(user/nice/system/idle)中?换句话说,jiffie 总数是否等于前四个统计数据的总和?或者,jiffie 总数是否等于所有 7 个统计数据的总和?如果后者为真,则 CPU 利用率公式应考虑所有数字,如下所示:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long double a[7],b[7],loadavg;
FILE *fp;
for(;;)
{
fp = fopen("/proc/stat","r");
fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]);
fclose(fp);
sleep(1);
fp = fopen("/proc/stat","r");
fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3],&b[4],&b[5],&b[6]);
fclose(fp);
loadavg = ((b[0]+b[1]+b[2]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[4]+a[5]+a[6]))
/ ((b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
printf("The current CPU utilization is : %Lf\n",loadavg);
}
return(0);
}
最佳答案
我认为 iowait/irq/softirq 没有算在前 4 个数字中。更详细的可以看内核代码中irqtime_account_process_tick的注释:
(对于 Linux kernel 4.1.1 )
2815 * Tick demultiplexing follows the order
2816 * - pending hardirq update <-- this is irq
2817 * - pending softirq update <-- this is softirq
2818 * - user_time
2819 * - idle_time <-- iowait is included in here, discuss below
2820 * - system time
2821 * - check for guest_time
2822 * - else account as system_time
闲置时间处理见account_idle_time函数:
2772 /*
2773 * Account for idle time.
2774 * @cputime: the cpu time spent in idle wait
2775 */
2776 void account_idle_time(cputime_t cputime)
2777 {
2778 u64 *cpustat = kcpustat_this_cpu->cpustat;
2779 struct rq *rq = this_rq();
2780
2781 if (atomic_read(&rq->nr_iowait) > 0)
2782 cpustat[CPUTIME_IOWAIT] += (__force u64) cputime;
2783 else
2784 cpustat[CPUTIME_IDLE] += (__force u64) cputime;
2785 }
如果 cpu 空闲并且有一些 IO 挂起,它将计算 CPUTIME_IOWAIT 中的时间。否则,它在 CPUTIME_IDLE 中计数。
总而言之,我认为 irq/softirq 中的 jiffies 应该算作 cpu 的“忙”,因为它实际上正在处理一些 IRQ 或软 IRQ。另一方面,“iowait”中的 jiffies 应该算作 cpu 的“空闲”,因为它没有做任何事情,而是在等待挂起的 IO 发生。
关于linux - 使用/proc/stat 准确计算 Linux 中的 CPU 利用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5514119/
所以,我正在尝试创建一种 ls 函数。这是我对每个文件的描述的代码 struct stat fileStat; struct dirent **files; num_entries = scandir
我最近一直在尝试实现我自己的 linux ls 命令版本。一切都很好,但是当我尝试使用 ls -l 功能时,struct stat 的某些字段未初始化 - 我得到 NULL 指针或垃圾值,尽管它似乎只
我在 Yii 中遇到 STAT 关系问题。我不确定我正在寻找的东西是否可以通过本地 Yii 关系实现。我会尽力描述我的问题,如果不清楚,请询问任何具体细节。 我有三个表,因此有三个模型 | table
我正在为一个严重依赖 scipy.stats.stats(scipy 版本 0.9.0)的包创建一个 django-powered (1.3) 接口(interface),称为 ovl 。在早期开发阶
为了安全起见,我喜欢显式初始化我的变量(当您编写大量代码时,它通常会使它更安全,因为您的代码最终不会崩溃那么多。) 对于大多数类型,无论是结构还是整数等基本 C++ 类型,我都可以编写以下内容: ti
我一直在使用 stat() 检查文件是否存在,据我所知,这比尝试打开文件更好。但是,stat() 不适用于包含其他语言的 unicode 字符的文件名。是否有 stat() 的宽字符版本或我可以使用的
错误: File "/usr/lib/python2.7/dist-packages/statsmodels/regression/linear_model.py", line 36, in
下面是我要运行的脚本。我不能在 awk 中使用 stat。 cat /etc/passwd | awk 'BEGIN{FS=":"}{print $6 }' | (stat $6 | sed -n '
我正在尝试拟合 xlog 线性回归。我使用 Seaborn regplot 来绘制拟合,看起来很合适(绿线)。然后,因为 regplot 不提供系数。我使用 stats.linregress 来查找系
我正在尝试使用共享库 (libscplugin.so) 中包含的方法。 我已经满足了库的所有要求: libc.so 带有指向 libc.so.6 的符号链接(symbolic link) libz.s
嘿,感谢阅读。 我正在制作一个程序,它接受 1 个参数(目录)并使用 opendir()/readdir() 读取目录中的所有文件,并使用 stat 显示文件类型(reg、链接、目录等)。当我在 sh
简单问题:在 Linux 中,我 stat() 一个不是设备的文件。 st_rdev 字段的期望值是多少?我可以运行 major(stat.st_rdev) 和 minor(stat.st_rdev)
我正在尝试为我的 Angular 6 应用程序生成 stats.json 文件。下面的事情我已经尝试过,但根本没有生成文件。我的系统需要有 “npm 运行”在每个 angular cli 命令之前。
我正在尝试使用返回的 stat 结构中的 st_mode,该结构是我通过以下方式从 stat() 调用获得的; char *fn = "test.c" struct s
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我有一个程序,是我通过修改原始暗网(深度学习图像识别,Yolov2)的许多地方而制作的。几个月前我一直在使用它,但是今天当我编译它时,它给了我一个错误: gcc -DSAVE_LAYER_INPUT
我预计 scipy.stats.mstats.pearsonr 对于屏蔽数组输入的结果将与 scipy.stats.pearsonr 对于输入数据的 unmasked 值给出相同的结果,但它不会't:
给定 tmp.c: #include #include #include int main(int argc, const char *argv[]) { struct stat st;
In [15]: a = np.array([0.5, 0.5, 0, 0, 0]) In [16]: b = np.array([1, 0, 0, 0, 0]) In [17]: entropy(a
当我们运行 stat filename我们得到 Access: 2021-06-25 15:40:18.532621916 +0530 Modify: 2020-08-13 15:57:30.0000
我是一名优秀的程序员,十分优秀!