aix-disk-stats/test.c

64 lines
1.7 KiB
C

/*
Repeatable prints disk statistics and sleeps for 10 seconds
Modified example from: https://www.ibm.com/docs/en/aix/7.3?topic=interfaces-perfstat-disk-interface
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libperfstat.h>
int main(int argc, char* argv[]) {
int i, ret, tot;
perfstat_disk_t *statp;
perfstat_id_t first;
while(1) {
/* check how many perfstat_disk_t structures are available */
tot = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0);
/* check for error */
if (tot < 0) {
perror("perfstat_disk");
exit(-1);
}
if (tot == 0) {
printf("No disks found in the system\n");
exit(-1);
}
/* allocate enough memory for all the structures */
statp = calloc(tot, sizeof(perfstat_disk_t));
/* set name to first interface */
strcpy(first.name, FIRST_DISK);
/* ask to get all the structures available in one call */
/* return code is number of structures returned */
ret = perfstat_disk(&first, statp, sizeof(perfstat_disk_t), tot);
/* check for error */
if (ret <= 0) {
perror("perfstat_disk");
exit(-1);
}
/* print statistics for each of the disks */
for (i = 0; i < ret; i++) {
printf("%s: %11u blocks of %11u bytes read, %11u blocks of %11u bytes written\n",
statp[i].name,
statp[i].rblks,
statp[i].bsize,
statp[i].wblks,
statp[i].bsize
);
}
sleep(10);
}
}