Check if cpu stats are read correctly from a sample
/proc/stat collected from a 24 CPU machine.
---
src/libvirt_linux.syms | 1 +
src/nodeinfo.c | 6 +-
tests/nodeinfodata/linux-cpustat-24cpu.out | 150 ++++++++++++++++++++++++++++
tests/nodeinfodata/linux-cpustat-24cpu.stat | 25 +++++
tests/nodeinfotest.c | 128 ++++++++++++++++++++++++
5 files changed, 309 insertions(+), 1 deletion(-)
create mode 100644 tests/nodeinfodata/linux-cpustat-24cpu.out
create mode 100644 tests/nodeinfodata/linux-cpustat-24cpu.stat
diff --git a/src/libvirt_linux.syms b/src/libvirt_linux.syms
index 3500898..b3b2384 100644
--- a/src/libvirt_linux.syms
+++ b/src/libvirt_linux.syms
@@ -3,6 +3,7 @@
#
# nodeinfo.h
+linuxNodeGetCPUStats;
linuxNodeInfoCPUPopulate;
# util/virstatslinux.h
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index 05bc038..671054e 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -193,6 +193,10 @@ freebsdNodeGetMemoryStats(virNodeMemoryStatsPtr params,
int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
const char *sysfs_dir,
virNodeInfoPtr nodeinfo);
+int linuxNodeGetCPUStats(FILE *procstat,
+ int cpuNum,
+ virNodeCPUStatsPtr params,
+ int *nparams);
/* Return the positive decimal contents of the given
* DIR/cpu%u/FILE, or -1 on error. If DEFAULT_VALUE is non-negative
@@ -681,7 +685,7 @@ cleanup:
# define TICK_TO_NSEC (1000ull * 1000ull * 1000ull / sysconf(_SC_CLK_TCK))
-static int
+int
linuxNodeGetCPUStats(FILE *procstat,
int cpuNum,
virNodeCPUStatsPtr params,
diff --git a/tests/nodeinfodata/linux-cpustat-24cpu.out
b/tests/nodeinfodata/linux-cpustat-24cpu.out
new file mode 100644
index 0000000..1ce6a97
--- /dev/null
+++ b/tests/nodeinfodata/linux-cpustat-24cpu.out
@@ -0,0 +1,150 @@
+cpu:
+kernel: 87511700000000
+user: 141280790000000
+idle: 18163445220000000
+iowait: 813230000000
+
+cpu0:
+kernel: 4476030000000
+user: 7490210000000
+idle: 753992420000000
+iowait: 52950000000
+
+cpu1:
+kernel: 1672150000000
+user: 3373260000000
+idle: 761786120000000
+iowait: 11210000000
+
+cpu2:
+kernel: 3089300000000
+user: 6668890000000
+idle: 756496960000000
+iowait: 42980000000
+
+cpu3:
+kernel: 2276740000000
+user: 3284640000000
+idle: 761316340000000
+iowait: 12190000000
+
+cpu4:
+kernel: 2995140000000
+user: 5839150000000
+idle: 757463830000000
+iowait: 39970000000
+
+cpu5:
+kernel: 1122870000000
+user: 2318670000000
+idle: 763363190000000
+iowait: 7980000000
+
+cpu6:
+kernel: 5465900000000
+user: 8962520000000
+idle: 751326650000000
+iowait: 72100000000
+
+cpu7:
+kernel: 1777150000000
+user: 3423370000000
+idle: 761548890000000
+iowait: 19330000000
+
+cpu8:
+kernel: 4527730000000
+user: 7724790000000
+idle: 753593270000000
+iowait: 58450000000
+
+cpu9:
+kernel: 10502300000000
+user: 10792580000000
+idle: 745327760000000
+iowait: 33400000000
+
+cpu10:
+kernel: 5354950000000
+user: 8472950000000
+idle: 752023620000000
+iowait: 40380000000
+
+cpu11:
+kernel: 1716350000000
+user: 3238910000000
+idle: 761816220000000
+iowait: 9930000000
+
+cpu12:
+kernel: 3310310000000
+user: 6832570000000
+idle: 755871760000000
+iowait: 51740000000
+
+cpu13:
+kernel: 1126860000000
+user: 2306330000000
+idle: 763452950000000
+iowait: 13670000000
+
+cpu14:
+kernel: 2513930000000
+user: 5475990000000
+idle: 758245540000000
+iowait: 51950000000
+
+cpu15:
+kernel: 1990440000000
+user: 2606730000000
+idle: 762305860000000
+iowait: 13790000000
+
+cpu16:
+kernel: 2441580000000
+user: 4633570000000
+idle: 759239930000000
+iowait: 62110000000
+
+cpu17:
+kernel: 885710000000
+user: 1892530000000
+idle: 764116100000000
+iowait: 13880000000
+
+cpu18:
+kernel: 5465390000000
+user: 8756550000000
+idle: 750968960000000
+iowait: 57560000000
+
+cpu19:
+kernel: 1863660000000
+user: 3487680000000
+idle: 761373230000000
+iowait: 12990000000
+
+cpu20:
+kernel: 4494600000000
+user: 7652020000000
+idle: 753489380000000
+iowait: 43890000000
+
+cpu21:
+kernel: 10450760000000
+user: 11160750000000
+idle: 745005570000000
+iowait: 24110000000
+
+cpu22:
+kernel: 5341250000000
+user: 8477790000000
+idle: 751781850000000
+iowait: 56320000000
+
+cpu23:
+kernel: 2650290000000
+user: 6408150000000
+idle: 757538720000000
+iowait: 10260000000
+
diff --git a/tests/nodeinfodata/linux-cpustat-24cpu.stat
b/tests/nodeinfodata/linux-cpustat-24cpu.stat
new file mode 100644
index 0000000..bc9d449
--- /dev/null
+++ b/tests/nodeinfodata/linux-cpustat-24cpu.stat
@@ -0,0 +1,25 @@
+cpu 14126233 1846 7764352 1816344522 81323 395581 591237 0 5880634 0
+cpu0 748997 24 320851 75399242 5295 22050 104702 0 331814 0
+cpu1 337325 1 140909 76178612 1121 8962 17344 0 166726 0
+cpu2 666860 29 269302 75649696 4298 18473 21155 0 272094 0
+cpu3 328387 77 211400 76131634 1219 9701 6573 0 115551 0
+cpu4 583896 19 265185 75746383 3997 17525 16804 0 253387 0
+cpu5 231867 0 100660 76336319 798 6856 4771 0 118465 0
+cpu6 896023 229 472933 75132665 7210 25811 47846 0 410328 0
+cpu7 342336 1 159567 76154889 1933 8675 9473 0 204523 0
+cpu8 772415 64 382065 75359327 5845 22810 47898 0 347169 0
+cpu9 1078771 487 1007467 74532776 3340 28419 14344 0 150374 0
+cpu10 847174 121 461786 75202362 4038 25206 48503 0 370309 0
+cpu11 323890 1 153521 76181622 993 9462 8652 0 199566 0
+cpu12 683237 20 290483 75587176 5174 19287 21261 0 293663 0
+cpu13 230633 0 100001 76345295 1367 7171 5514 0 103907 0
+cpu14 547593 6 220118 75824554 5195 14963 16312 0 207464 0
+cpu15 260648 25 185128 76230586 1379 8448 5468 0 76655 0
+cpu16 463328 29 214199 75923993 6211 14403 15556 0 184943 0
+cpu17 189247 6 79317 76411610 1388 5455 3799 0 85456 0
+cpu18 875552 103 470237 75096896 5756 25159 51143 0 408446 0
+cpu19 348767 1 167550 76137323 1299 8813 10003 0 208604 0
+cpu20 765169 33 380697 75348938 4389 21782 46981 0 353323 0
+cpu21 1115675 400 1003579 74500557 2411 28146 13351 0 162678 0
+cpu22 847629 150 463239 75178185 5632 24933 45953 0 376150 0
+cpu23 640804 11 244148 75753872 1026 13061 7820 0 479032 0
diff --git a/tests/nodeinfotest.c b/tests/nodeinfotest.c
index 74f6d4d..d0264fa 100644
--- a/tests/nodeinfotest.c
+++ b/tests/nodeinfotest.c
@@ -31,6 +31,13 @@ extern int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
char *sysfs_dir,
virNodeInfoPtr nodeinfo);
+extern int
+linuxNodeGetCPUStats(FILE *procstat,
+ int cpuNum,
+ virNodeCPUStatsPtr params,
+ int *nparams);
+
+
static int
linuxTestCompareFiles(const char *cpuinfofile,
char *sysfs_dir,
@@ -83,6 +90,90 @@ fail:
return ret;
}
+static int
+linuxCPUStatsToBuf(virBufferPtr buf,
+ int cpu,
+ virNodeCPUStatsPtr param,
+ size_t nparams)
+{
+ size_t i = 0;
+
+ if (cpu < 0)
+ virBufferAddLit(buf, "cpu:\n");
+ else
+ virBufferAsprintf(buf, "cpu%d:\n", cpu);
+
+ for (i = 0; i < nparams; i++)
+ virBufferAsprintf(buf, "%s: %llu\n", param[i].field, param[i].value);
+
+ virBufferAddChar(buf, '\n');
+ return 0;
+}
+
+static int
+linuxCPUStatsCompareFiles(const char *cpustatfile,
+ size_t ncpus,
+ const char *outfile)
+{
+ int ret = -1;
+ char *actualData = NULL;
+ char *expectData = NULL;
+ FILE *cpustat = NULL;
+ virNodeCPUStatsPtr params = NULL;
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+ size_t i;
+ int nparams = 0;
+
+ if (virtTestLoadFile(outfile, &expectData) < 0)
+ goto fail;
+
+ if (!(cpustat = fopen(cpustatfile, "r"))) {
+ virReportSystemError(errno, "failed to open '%s': ",
cpustatfile);
+ goto fail;
+ }
+
+ if (linuxNodeGetCPUStats(NULL, 0, NULL, &nparams) < 0)
+ goto fail;
+
+ if (VIR_ALLOC_N(params, nparams) < 0)
+ goto fail;
+
+ if (linuxNodeGetCPUStats(cpustat, VIR_NODE_CPU_STATS_ALL_CPUS, params,
+ &nparams) < 0)
+ goto fail;
+
+ if (linuxCPUStatsToBuf(&buf, VIR_NODE_CPU_STATS_ALL_CPUS,
+ params, nparams) < 0)
+ goto fail;
+
+ for (i = 0; i < ncpus; i++) {
+ if (linuxNodeGetCPUStats(cpustat, i, params, &nparams) < 0)
+ goto fail;
+ if (linuxCPUStatsToBuf(&buf, i, params, nparams) < 0)
+ goto fail;
+ }
+
+ if (!(actualData = virBufferContentAndReset(&buf))) {
+ virReportOOMError();
+ goto fail;
+ }
+
+ if (STRNEQ(actualData, expectData)) {
+ virtTestDifference(stderr, expectData, actualData);
+ goto fail;
+ }
+
+ ret = 0;
+
+fail:
+ virBufferFreeAndReset(&buf);
+ VIR_FORCE_FCLOSE(cpustat);
+ VIR_FREE(expectData);
+ VIR_FREE(actualData);
+ VIR_FREE(params);
+ return ret;
+}
+
static int
linuxTestNodeInfo(const void *data)
@@ -118,6 +209,34 @@ cleanup:
return result;
}
+struct nodeCPUStatsData {
+ const char *name;
+ int ncpus;
+};
+
+static int
+linuxTestNodeCPUStats(const void *data)
+{
+ const struct nodeCPUStatsData *testData = data;
+ int result = -1;
+ char *cpustatfile = NULL;
+ char *outfile = NULL;
+
+ if (virAsprintf(&cpustatfile, "%s/nodeinfodata/linux-cpustat-%s.stat",
+ abs_srcdir, testData->name) < 0 ||
+ virAsprintf(&outfile, "%s/nodeinfodata/linux-cpustat-%s.out",
+ abs_srcdir, testData->name) < 0)
+ goto fail;
+
+ result = linuxCPUStatsCompareFiles(cpustatfile,
+ testData->ncpus,
+ outfile);
+fail:
+ VIR_FREE(cpustatfile);
+ VIR_FREE(outfile);
+ return result;
+}
+
static int
mymain(void)
@@ -145,6 +264,15 @@ mymain(void)
if (virtTestRun(nodeData[i], linuxTestNodeInfo, nodeData[i]) != 0)
ret = -1;
+# define DO_TEST_CPU_STATS(name, ncpus) \
+ do { \
+ static struct nodeCPUStatsData data = { name, ncpus }; \
+ if (virtTestRun("CPU stats " name, linuxTestNodeCPUStats, &data)
< 0) \
+ ret = -1; \
+ } while (0)
+
+ DO_TEST_CPU_STATS("24cpu", 24);
+
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
--
1.8.3.2