|
| 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 | +} |
0 commit comments