aix-disk-stats/main.c

132 lines
3.8 KiB
C
Raw Normal View History

2022-11-11 07:51:27 +00:00
/*
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
*/
2022-11-11 13:51:33 +00:00
#include <time.h>
2022-11-11 07:51:27 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libperfstat.h>
2022-11-11 13:51:33 +00:00
int startsWith(const char *a, const char *b) {
if(strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
}
2022-11-11 07:51:27 +00:00
int main(int argc, char* argv[]) {
2022-11-11 10:44:28 +00:00
int i, ret, tot;
perfstat_disk_t *statp;
perfstat_id_t first;
2022-11-11 13:51:33 +00:00
time_t now;
2022-11-11 07:51:27 +00:00
/* check how many perfstat_disk_t structures are available */
tot = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0);
2022-11-11 10:44:28 +00:00
2022-11-11 07:51:27 +00:00
/* check for error */
if (tot < 0) {
perror("perfstat_disk");
exit(-1);
}
if (tot == 0) {
printf("No disks found in the system\n");
exit(-1);
}
2022-11-11 11:21:46 +00:00
long history[32][8] = { 0 };
2022-11-11 07:51:27 +00:00
2022-11-11 10:44:28 +00:00
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);
2022-11-11 07:51:27 +00:00
2022-11-11 10:44:28 +00:00
/* check for error */
if (ret <= 0) {
perror("perfstat_disk");
exit(-1);
}
2022-11-11 13:51:33 +00:00
// Obtain current time
// `time()` returns the current time of the system as a `time_t` value
time(&now);
2022-11-11 10:44:28 +00:00
/* print statistics for each of the disks */
for (i = 0; i < ret; i++) {
2022-11-11 13:51:33 +00:00
if(!startsWith(statp[i].name, "hdisk")) {
continue;
}
2022-11-11 10:44:28 +00:00
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 wbytes = 0;
2022-11-11 11:21:46 +00:00
if (wblks > 0) {
2022-11-11 10:44:28 +00:00
wbytes = wblks * bsize;
}
2022-11-11 11:21:46 +00:00
unsigned long drblbks = 0;
if (history[i][0] > 0) {
drblbks = rblks - history[i][0];
}
unsigned long drbytes = 0;
2022-11-11 10:44:28 +00:00
if(history[i][1] > 0) {
2022-11-11 11:21:46 +00:00
drbytes = rbytes - history[i][1];
2022-11-11 10:44:28 +00:00
}
2022-11-11 11:21:46 +00:00
unsigned long dwblbks = 0;
if (history[i][2] > 0) {
dwblbks = wblks - history[i][2];
}
unsigned long dwbytes = 0;
if(history[i][3] > 0) {
dwbytes = wbytes - history[i][3];
}
2022-11-11 13:51:33 +00:00
printf("%s - %s => reads: [ blocks: %10u (%8u ), bytes: %10u (%8u ) ], writes: [ blocks: %10u (%8u ), bytes: %10u (%8u ) ]\n",
strtok(ctime(&now), "\n"),
statp[i].name,
rblks,
drblbks,
rbytes,
drbytes,
wblks,
dwblbks,
wbytes,
dwbytes);
2022-11-11 11:21:46 +00:00
history[i][0] = rblks;
history[i][1] = rbytes;
history[i][2] = wblks;
history[i][3] = wbytes;
2022-11-11 10:44:28 +00:00
}
sleep(10);
}
2022-11-11 07:51:27 +00:00
}
2022-11-11 13:51:33 +00:00