aix-disk-stats/test.c

98 lines
3.1 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;
/* 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);
}
long history[32][8];
while(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++) {
unsigned long bsize = statp[i].bsize; // Disk block size (in bytes).
unsigned long rblks = statp[i].rblks; // Number of blocks read from disk.
unsigned long wblks = statp[i].wblks; // Number of blocks written to disk.
unsigned long rbytes = 0;
if(rblks > 0) {
rbytes = rblks * bsize;
}
unsigned long rdiff = 0;
if(history[i][0] > 0) {
rdiff = rbytes - history[i][0];
}
unsigned long wbytes = 0;
if(wblks > 0) {
wbytes = wblks * bsize;
}
unsigned long wdiff = 0;
if(history[i][1] > 0) {
wdiff = wbytes - history[i][1];
}
printf("%s => reads [ blocks: %10u, bytes %10u, diff %8u ], writes [ blocks: %10u, bytes: %10u, diff: %8u ]\n",
statp[i].name,
rblks,
rbytes,
rdiff,
wblks,
wbytes,
wdiff
);
history[i][0] = rbytes;
history[i][1] = wbytes;
}
sleep(10);
}
}