Added memory PSI info, also flush stdount for printf

This commit is contained in:
Trey Blancher 2023-08-07 21:36:51 -04:00
parent 2c85cc2019
commit 1fa3473911

View File

@ -8,14 +8,18 @@
#define CPU_TRACKING_WINDOW_SECS 1 #define CPU_TRACKING_WINDOW_SECS 1
#define IO_TRACKING_WINDOW_SECS 1 #define IO_TRACKING_WINDOW_SECS 1
#define MEM_TRACKING_WINDOW_SECS 1
#define CPU_TRIGGER_THRESHOLD_MS 100 #define CPU_TRIGGER_THRESHOLD_MS 100
#define IO_TRIGGER_THRESHOLD_MS 100 #define IO_TRIGGER_THRESHOLD_MS 100
#define MEM_TRIGGER_THRESHOLD_MS 100
#define CPU_PRESSURE_FILE "/proc/pressure/cpu" #define CPU_PRESSURE_FILE "/proc/pressure/cpu"
#define IO_PRESSURE_FILE "/proc/pressure/io" #define IO_PRESSURE_FILE "/proc/pressure/io"
#define MEM_PRESSURE_FILE "/proc/pressure/memory"
#define FD_CPU_IDX 0 #define FD_CPU_IDX 0
#define FD_IO_IDX 1 #define FD_IO_IDX 1
#define FD_MEM_IDX 2
struct pollfd fds[2]; struct pollfd fds[3];
/* /*
One function that prints the system call and the error details One function that prints the system call and the error details
@ -47,38 +51,53 @@ void setup_polling() {
fds[FD_IO_IDX].fd = open(IO_PRESSURE_FILE, O_RDWR | O_NONBLOCK); fds[FD_IO_IDX].fd = open(IO_PRESSURE_FILE, O_RDWR | O_NONBLOCK);
if (fds[FD_IO_IDX].fd < 0) if (fds[FD_IO_IDX].fd < 0)
fatal_error("open(): " IO_PRESSURE_FILE); fatal_error("open(): " IO_PRESSURE_FILE);
/* Let's setup our MEM PSI trigger */
fds[FD_MEM_IDX].fd = open(MEM_PRESSURE_FILE, O_RDWR | O_NONBLOCK);
if (fds[FD_MEM_IDX].fd < 0)
fatal_error("open(): " MEM_PRESSURE_FILE);
fds[FD_CPU_IDX].events = fds[FD_IO_IDX].events = POLLPRI; fds[FD_CPU_IDX].events = fds[FD_MEM_IDX].events = fds[FD_IO_IDX].events = POLLPRI;
char trigger[128]; char trigger[128];
snprintf(trigger, 128, "some %d %d", CPU_TRIGGER_THRESHOLD_MS * 1000, CPU_TRACKING_WINDOW_SECS * 1000000); snprintf(trigger, 128, "some %d %d", CPU_TRIGGER_THRESHOLD_MS * 1000, CPU_TRACKING_WINDOW_SECS * 1000000);
printf("Trigger: %s\n", trigger); printf("Trigger: %s\n", trigger);
fflush(stdout);
if (write(fds[FD_CPU_IDX].fd, trigger, strlen(trigger) + 1) < 0) if (write(fds[FD_CPU_IDX].fd, trigger, strlen(trigger) + 1) < 0)
fatal_error("write(): " CPU_PRESSURE_FILE); fatal_error("write(): " CPU_PRESSURE_FILE);
snprintf(trigger, 128, "some %d %d", MEM_TRIGGER_THRESHOLD_MS * 1000, MEM_TRACKING_WINDOW_SECS * 1000000);
printf("Trigger: %s\n", trigger);
fflush(stdout);
if (write(fds[FD_MEM_IDX].fd, trigger, strlen(trigger) + 1) < 0)
fatal_error("write(): " MEM_PRESSURE_FILE);
snprintf(trigger, 128, "some %d %d", IO_TRIGGER_THRESHOLD_MS * 1000, IO_TRACKING_WINDOW_SECS * 1000000); snprintf(trigger, 128, "some %d %d", IO_TRIGGER_THRESHOLD_MS * 1000, IO_TRACKING_WINDOW_SECS * 1000000);
printf("Trigger: %s\n", trigger); printf("Trigger: %s\n", trigger);
fflush(stdout);
if (write(fds[FD_IO_IDX].fd, trigger, strlen(trigger) + 1) < 0) if (write(fds[FD_IO_IDX].fd, trigger, strlen(trigger) + 1) < 0)
fatal_error("write(): " IO_PRESSURE_FILE); fatal_error("write(): " IO_PRESSURE_FILE);
} }
/* /*
* This is the main function where we wait for notifications from * This is the main function where we wait for notifications from PSI. We
* PSI. We increment 2 separate variables that track CPU and I/O * increment 2 separate variables that track CPU, MEM and I/O notification
* notification counts separately and print them. * counts separately and print them.
* */ */
void wait_for_notification() { void wait_for_notification() {
int cpu_event_counter = 1; int cpu_event_counter = 1;
int mem_event_counter = 1;
int io_event_counter = 1; int io_event_counter = 1;
while (1) { while (1) {
int n = poll(fds, 2, -1); int n = poll(fds, 3, -1);
if (n < 0) { if (n < 0) {
fatal_error("poll()"); fatal_error("poll()");
} }
for (int i = 0; i < 2; i++) { for (int i = 0; i < 3; i++) {
/* If the fd of the current iteration does not have any /* If the fd of the current iteration does not have any
* events, move on to the next fd. * events, move on to the next fd.
@ -91,10 +110,18 @@ void wait_for_notification() {
exit(1); exit(1);
} }
if (fds[i].revents & POLLPRI) { if (fds[i].revents & POLLPRI) {
if (i == FD_CPU_IDX) if (i == FD_CPU_IDX) {
printf("CPU PSI event %d triggered.\n", cpu_event_counter++); printf("CPU PSI event %d triggered.\n", cpu_event_counter++);
else fflush(stdout);
printf("I/O PSI event %d triggered.\n", io_event_counter++); }
else if (i == FD_MEM_IDX) {
printf("MEM PSI event %d triggered.\n", mem_event_counter++);
fflush(stdout);
}
else {
printf("IO PSI event %d triggered.\n", io_event_counter++);
fflush(stdout);
}
} else { } else {
fprintf(stderr, "Unrecognized event: 0x%x.\n", fds[i].revents); fprintf(stderr, "Unrecognized event: 0x%x.\n", fds[i].revents);
exit(1); exit(1);
@ -123,4 +150,4 @@ int main() {
setup_polling(); setup_polling();
wait_for_notification(); wait_for_notification();
return 0; return 0;
} }