Add test for linuxNodeGetCPUStats

Check if cpu stats are read correctly from a sample
/proc/stat collected from a 24 CPU machine.
This commit is contained in:
Ján Tomko 2014-01-16 12:37:27 +01:00
parent b3b44c572c
commit 5099f745e6
6 changed files with 306 additions and 1 deletions

View File

@ -3,6 +3,7 @@
#
# nodeinfo.h
linuxNodeGetCPUStats;
linuxNodeInfoCPUPopulate;
# util/virstatslinux.h

View File

@ -676,7 +676,7 @@ cleanup:
# define TICK_TO_NSEC (1000ull * 1000ull * 1000ull / sysconf(_SC_CLK_TCK))
static int
int
linuxNodeGetCPUStats(FILE *procstat,
int cpuNum,
virNodeCPUStatsPtr params,

View File

@ -28,6 +28,11 @@
int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
const char *sysfs_dir,
virNodeInfoPtr nodeinfo);
int linuxNodeGetCPUStats(FILE *procstat,
int cpuNum,
virNodeCPUStatsPtr params,
int *nparams);
# endif
#endif /* __NODEINFO_PRIV_H__ */

View File

@ -0,0 +1,150 @@
cpu:
kernel: 8751170
user: 14128079
idle: 1816344522
iowait: 81323
cpu0:
kernel: 447603
user: 749021
idle: 75399242
iowait: 5295
cpu1:
kernel: 167215
user: 337326
idle: 76178612
iowait: 1121
cpu2:
kernel: 308930
user: 666889
idle: 75649696
iowait: 4298
cpu3:
kernel: 227674
user: 328464
idle: 76131634
iowait: 1219
cpu4:
kernel: 299514
user: 583915
idle: 75746383
iowait: 3997
cpu5:
kernel: 112287
user: 231867
idle: 76336319
iowait: 798
cpu6:
kernel: 546590
user: 896252
idle: 75132665
iowait: 7210
cpu7:
kernel: 177715
user: 342337
idle: 76154889
iowait: 1933
cpu8:
kernel: 452773
user: 772479
idle: 75359327
iowait: 5845
cpu9:
kernel: 1050230
user: 1079258
idle: 74532776
iowait: 3340
cpu10:
kernel: 535495
user: 847295
idle: 75202362
iowait: 4038
cpu11:
kernel: 171635
user: 323891
idle: 76181622
iowait: 993
cpu12:
kernel: 331031
user: 683257
idle: 75587176
iowait: 5174
cpu13:
kernel: 112686
user: 230633
idle: 76345295
iowait: 1367
cpu14:
kernel: 251393
user: 547599
idle: 75824554
iowait: 5195
cpu15:
kernel: 199044
user: 260673
idle: 76230586
iowait: 1379
cpu16:
kernel: 244158
user: 463357
idle: 75923993
iowait: 6211
cpu17:
kernel: 88571
user: 189253
idle: 76411610
iowait: 1388
cpu18:
kernel: 546539
user: 875655
idle: 75096896
iowait: 5756
cpu19:
kernel: 186366
user: 348768
idle: 76137323
iowait: 1299
cpu20:
kernel: 449460
user: 765202
idle: 75348938
iowait: 4389
cpu21:
kernel: 1045076
user: 1116075
idle: 74500557
iowait: 2411
cpu22:
kernel: 534125
user: 847779
idle: 75178185
iowait: 5632
cpu23:
kernel: 265029
user: 640815
idle: 75753872
iowait: 1026

View File

@ -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

View File

@ -79,6 +79,93 @@ fail:
return ret;
}
# define TICK_TO_NSEC (1000ull * 1000ull * 1000ull / sysconf(_SC_CLK_TCK))
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 / TICK_TO_NSEC);
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)
@ -114,6 +201,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)
@ -141,6 +256,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;
}