Skip to content

Commit ee484b2

Browse files
committed
tests: add /proc/pressure/* virtualization tests
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
1 parent ea6f5b0 commit ee484b2

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

tests/meson.build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,10 @@ test_programs += executable(
145145
include_directories: config_include,
146146
install: false,
147147
build_by_default: want_tests != false)
148+
149+
test_programs += executable(
150+
'test-psi-triggers-poll',
151+
'test-psi-triggers-poll.c',
152+
include_directories: config_include,
153+
install: false,
154+
build_by_default: want_tests != false)

tests/test-psi-triggers-poll.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* Borrowed from https://www.kernel.org/doc/Documentation/accounting/psi.rst */
2+
#include <errno.h>
3+
#include <fcntl.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <poll.h>
7+
#include <string.h>
8+
#include <time.h>
9+
#include <unistd.h>
10+
11+
#define WAIT_EVENTS_NUM 4
12+
13+
/*
14+
* Monitor cpu partial stall with 2s tracking window size
15+
* and 20ms threshold.
16+
*/
17+
int main(int argc, char **argv) {
18+
const char trig[] = "some 20000 2000000";
19+
struct pollfd fds;
20+
int n;
21+
char *path;
22+
size_t len;
23+
int events_cnt = 0;
24+
25+
if (geteuid() != 0) {
26+
fprintf(stderr, "Run me as root\n");
27+
exit(1);
28+
}
29+
30+
if (argc != 2) {
31+
fprintf(stderr, "Usage: %s [lxcfs_mount_path]\n", argv[0]);
32+
exit(1);
33+
}
34+
35+
len = strlen(argv[1]) + strlen("/proc/pressure/cpu") + 1;
36+
path = alloca(len);
37+
snprintf(path, len, "%s/proc/pressure/cpu", argv[1]);
38+
fds.fd = open(path, O_RDWR | O_NONBLOCK);
39+
if (fds.fd < 0) {
40+
printf("%s open error: %s\n", path, strerror(errno));
41+
return 1;
42+
}
43+
fds.events = POLLPRI;
44+
45+
if (write(fds.fd, trig, strlen(trig) + 1) < 0) {
46+
printf("/proc/pressure/cpu write error: %s\n",
47+
strerror(errno));
48+
return 1;
49+
}
50+
51+
printf("waiting for events...\n");
52+
time_t t1 = time(NULL);
53+
while (1) {
54+
/* test code in proc_fuse.c poll_thread() generates 1 event per second */
55+
n = poll(&fds, 1, 3 * 1000);
56+
if (n < 0) {
57+
printf("poll error: %s\n", strerror(errno));
58+
return 1;
59+
}
60+
if (n == 0) {
61+
printf("timeout\n");
62+
return 1;
63+
}
64+
if (fds.revents & POLLERR) {
65+
printf("got POLLERR, event source is gone\n");
66+
return 1;
67+
}
68+
if (fds.revents & POLLPRI) {
69+
printf("event triggered!\n");
70+
events_cnt++;
71+
if (events_cnt == WAIT_EVENTS_NUM)
72+
break;
73+
} else {
74+
printf("unknown event received: 0x%x\n", fds.revents);
75+
return 1;
76+
}
77+
}
78+
time_t t2 = time(NULL);
79+
80+
printf("events_cnt = %d time diff = %ld\n", events_cnt, (long)(t2 - t1));
81+
/* events frequency is 1 HZ, so we can expect that difference <= 1 */
82+
if (labs((long)(t2 - t1) - events_cnt) > 1) {
83+
printf("| (t2 - t1) - events_cnt | > 1 while should be <= 1\n");
84+
return 1;
85+
}
86+
87+
return 0;
88+
}

tests/test_proc.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,7 @@ echo "==> Testing /proc/stat"
9292
echo "==> Testing /proc/meminfo"
9393
grep -q "^MemTotal.*65536 kB$" ${LXCFSDIR}/proc/meminfo
9494

95+
echo "==> Testing /proc/pressure/{cpu, io, memory}"
96+
{{LXCFS_BUILD_ROOT}}/tests/test-psi-triggers-poll ${LXCFSDIR}
97+
9598
PASS=1

0 commit comments

Comments
 (0)