有一些问题我下面提了一下 总的来说,这个小工具有点粗糙,建议好好改一下。最好是能动态显示各CPU的负载,哪怕是固定间隔也是好的。






Xie Xiuqi
Email: xiexiuqi@huawei.com
发件人:Hongyu Li <543306408@qq.com>
收件人:kernel <kernel@openeuler.org>
抄 送:Xiexiuqi <xiexiuqi@huawei.com>;chengjian (D) <cj.chengjian@huawei.com>;Zhengzengkai <zhengzengkai@huawei.com>
时 间:2021-09-30 20:56:47
主 题:[PATCH openEuler-21.09 2/2] tools: add a tool to calculate the idle rate

openEuler inclusion
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I4CIJQ
CVE: NA






This tool can help calculate the CPU idle rate in higher precision.

Signed-off-by: Hongyu Li <543306408@qq.com>
---
 tools/accounting/Makefile   | 2 +-
 tools/accounting/idle_cal.c | 91 +++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+), 1 deletion(-)
 create mode 100644 tools/accounting/idle_cal.c

diff --git a/tools/accounting/Makefile b/tools/accounting/Makefile
index 03687f19cbb1..d14151e28173 100644
--- a/tools/accounting/Makefile
+++ b/tools/accounting/Makefile
@@ -2,7 +2,7 @@
 CC := $(CROSS_COMPILE)gcc
 CFLAGS := -I../../usr/include
 
-PROGS := getdelays
+PROGS := getdelays idle_cal
 
 all: $(PROGS)
 
diff --git a/tools/accounting/idle_cal.c b/tools/accounting/idle_cal.c
new file mode 100644
index 000000000000..397b0fc1c69c
--- /dev/null
+++ b/tools/accounting/idle_cal.c
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * idle_cal.c
+ *
+ * Copyright (C) 2007


为啥是2007 ?

+ *
+ * cpu idle time accouting
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include <limits.h>
+#include <sys/time.h>
+
+#define CPUS 4

为啥定义成4?系统CPU个数不能这么假设。

+#define BUFFSIZE 4096
+#define HZ 100
+#define FILE_NAME "/proc/stat2"
+
+struct cpu_info {
+       char name[BUFFSIZE];
+       long long value[1];
+};
+
+int main(void)
+{
+       char buf[BUFFSIZE];
+       struct cpu_info cpus[CPUS+1];
+       struct cpu_info cpus_2[CPUS+1];
+       long long sub;
+       double value;
+
+       FILE *fp = fopen(FILE_NAME, "r");
+       int i = 0;
+       struct timeval start, end;
+
+
+       while (i < CPUS+1) {
+               int n = fscanf(fp, "%s %lld\n", cpus[i].name, &cpus[i].value[0]);
+
+               if (n < 0) {
+                       printf("wrong");
+                       return -1;
+               }
+               i += 1;
+       }
+
+       for (int i = 0; i < CPUS+1; i++)
+               printf("%s %lld\n", cpus[i].name, cpus[i].value[0]);

每次把所有CPU的值打出来,太啰嗦,对使用者没什么意义。下边那个也是。执行一下看一下输出效果就知道了,干扰输出太多 会影响易用性。


+       gettimeofday(&start, NULL);
+       fflush(fp);
+       fclose(fp);
+       i = 0;
+
+       sleep(2);
+
+       FILE *fp_2 = fopen(FILE_NAME, "r");
+
+       while (i < CPUS+1) {
+               int n = fscanf(fp_2, "%s %lld\n", cpus_2[i].name, &cpus_2[i].value[0]);
+
+               if (n < 0) {
+                       printf("wrong");
+                       return -1;
+               }
+               i += 1;
+       }
+
+       for (int i = 0; i < CPUS+1; i++)
+               printf("%s %lld\n", cpus_2[i].name, cpus_2[i].value[0]);
+       gettimeofday(&end, NULL);
+       fclose(fp_2);
+
+       sub = end.tv_sec-start.tv_sec;
+       value = sub*1000000.0+end.tv_usec-start.tv_usec;
+
+       printf("CPU idle rate %f\n", 1000000/HZ*(cpus_2[0].value[0]-cpus[0].value[0])/value);
+
+       for (int i = 1; i < CPUS+1; i++) {
+               printf("CPU%d idle rate %f\n", i-1, 1000000/HZ

 目的是算系统负载,即 CPU占用率,idle rate 是中间过程,没必要把这个指标作为最后结果输出。


+                               *(cpus_2[i].value[0]-cpus[i].value[0])/value);
+       }
+
+       return 0;
+}
+
--
2.17.1