Provide argument for sleep-time

This commit is contained in:
Mark Nellemann 2022-11-12 14:32:31 +01:00
parent 243f0062d9
commit 66bac0e120
2 changed files with 107 additions and 90 deletions

View File

@ -5,25 +5,29 @@ Test code for [investigating](https://github.com/oshi/oshi/issues/2239) *wierd*
Example from PowerVS AIX 7.3
```
# ./test
hdisk0 => reads: [ blocks: 1995768 ( 0 ), bytes: 1021833216 ( 0 ) ], writes: [ blocks: 126169 ( 0 ), bytes: 64598528 ( 0 ) ]
hdisk0 => reads: [ blocks: 1999986 ( 4218 ), bytes: 1023992832 ( 2159616 ) ], writes: [ blocks: 126241 ( 72 ), bytes: 64635392 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2004196 ( 4210 ), bytes: 1026148352 ( 2155520 ) ], writes: [ blocks: 126321 ( 80 ), bytes: 64676352 ( 40960 ) ]
hdisk0 => reads: [ blocks: 2008406 ( 4210 ), bytes: 1028303872 ( 2155520 ) ], writes: [ blocks: 126393 ( 72 ), bytes: 64713216 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2012616 ( 4210 ), bytes: 1030459392 ( 2155520 ) ], writes: [ blocks: 127321 ( 928 ), bytes: 65188352 ( 475136 ) ]
hdisk0 => reads: [ blocks: 2016826 ( 4210 ), bytes: 1032614912 ( 2155520 ) ], writes: [ blocks: 127393 ( 72 ), bytes: 65225216 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2021036 ( 4210 ), bytes: 1034770432 ( 2155520 ) ], writes: [ blocks: 127465 ( 72 ), bytes: 65262080 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2025246 ( 4210 ), bytes: 1036925952 ( 2155520 ) ], writes: [ blocks: 127537 ( 72 ), bytes: 65298944 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2029456 ( 4210 ), bytes: 1039081472 ( 2155520 ) ], writes: [ blocks: 127617 ( 80 ), bytes: 65339904 ( 40960 ) ]
hdisk0 => reads: [ blocks: 2033666 ( 4210 ), bytes: 1041236992 ( 2155520 ) ], writes: [ blocks: 127689 ( 72 ), bytes: 65376768 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2037876 ( 4210 ), bytes: 1043392512 ( 2155520 ) ], writes: [ blocks: 128033 ( 344 ), bytes: 65552896 ( 176128 ) ]
hdisk0 => reads: [ blocks: 2042094 ( 4218 ), bytes: 1045552128 ( 2159616 ) ], writes: [ blocks: 128113 ( 80 ), bytes: 65593856 ( 40960 ) ]
hdisk0 => reads: [ blocks: 2046304 ( 4210 ), bytes: 1047707648 ( 2155520 ) ], writes: [ blocks: 128185 ( 72 ), bytes: 65630720 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2050514 ( 4210 ), bytes: 1049863168 ( 2155520 ) ], writes: [ blocks: 128257 ( 72 ), bytes: 65667584 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2054724 ( 4210 ), bytes: 1052018688 ( 2155520 ) ], writes: [ blocks: 128329 ( 72 ), bytes: 65704448 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2058934 ( 4210 ), bytes: 1054174208 ( 2155520 ) ], writes: [ blocks: 128441 ( 112 ), bytes: 65761792 ( 57344 ) ]
hdisk0 => reads: [ blocks: 2063152 ( 4218 ), bytes: 1056333824 ( 2159616 ) ], writes: [ blocks: 129297 ( 856 ), bytes: 66200064 ( 438272 ) ]
hdisk0 => reads: [ blocks: 2067362 ( 4210 ), bytes: 1058489344 ( 2155520 ) ], writes: [ blocks: 129369 ( 72 ), bytes: 66236928 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2071572 ( 4210 ), bytes: 1060644864 ( 2155520 ) ], writes: [ blocks: 129441 ( 72 ), bytes: 66273792 ( 36864 ) ]
hdisk0 => reads: [ blocks: 2075782 ( 4210 ), bytes: 1062800384 ( 2155520 ) ], writes: [ blocks: 129521 ( 80 ), bytes: 66314752 ( 40960 ) ]
# ./aix-disk-perfstat 2
Sat Nov 12 14:29:28 2022 - hdisk0 => reads: [ blocks: 41710376 ( 0 ), bytes: 4175843328 ( 0 ) ], writes: [ blocks: 52579577 ( 0 ), bytes: 1150939648 ( 0 ) ]
Sat Nov 12 14:29:30 2022 - hdisk0 => reads: [ blocks: 41710376 ( 0 ), bytes: 4175843328 ( 0 ) ], writes: [ blocks: 52579577 ( 0 ), bytes: 1150939648 ( 0 ) ]
Sat Nov 12 14:29:32 2022 - hdisk0 => reads: [ blocks: 41710376 ( 0 ), bytes: 4175843328 ( 0 ) ], writes: [ blocks: 52580401 ( 824 ), bytes: 1151361536 ( 421888 ) ]
Sat Nov 12 14:29:34 2022 - hdisk0 => reads: [ blocks: 41714586 ( 4210 ), bytes: 4177998848 ( 2155520 ) ], writes: [ blocks: 52580473 ( 72 ), bytes: 1151398400 ( 36864 ) ]
Sat Nov 12 14:29:36 2022 - hdisk0 => reads: [ blocks: 41714586 ( 0 ), bytes: 4177998848 ( 0 ) ], writes: [ blocks: 52580473 ( 0 ), bytes: 1151398400 ( 0 ) ]
Sat Nov 12 14:29:38 2022 - hdisk0 => reads: [ blocks: 41714586 ( 0 ), bytes: 4177998848 ( 0 ) ], writes: [ blocks: 52580473 ( 0 ), bytes: 1151398400 ( 0 ) ]
Sat Nov 12 14:29:40 2022 - hdisk0 => reads: [ blocks: 41714586 ( 0 ), bytes: 4177998848 ( 0 ) ], writes: [ blocks: 52580473 ( 0 ), bytes: 1151398400 ( 0 ) ]
Sat Nov 12 14:29:42 2022 - hdisk0 => reads: [ blocks: 41714586 ( 0 ), bytes: 4177998848 ( 0 ) ], writes: [ blocks: 52580473 ( 0 ), bytes: 1151398400 ( 0 ) ]
Sat Nov 12 14:29:44 2022 - hdisk0 => reads: [ blocks: 41718796 ( 4210 ), bytes: 4180154368 ( 2155520 ) ], writes: [ blocks: 52580553 ( 80 ), bytes: 1151439360 ( 40960 ) ]
Sat Nov 12 14:29:46 2022 - hdisk0 => reads: [ blocks: 41718796 ( 0 ), bytes: 4180154368 ( 0 ) ], writes: [ blocks: 52580553 ( 0 ), bytes: 1151439360 ( 0 ) ]
Sat Nov 12 14:29:48 2022 - hdisk0 => reads: [ blocks: 41718796 ( 0 ), bytes: 4180154368 ( 0 ) ], writes: [ blocks: 52580553 ( 0 ), bytes: 1151439360 ( 0 ) ]
Sat Nov 12 14:29:50 2022 - hdisk0 => reads: [ blocks: 41718796 ( 0 ), bytes: 4180154368 ( 0 ) ], writes: [ blocks: 52580553 ( 0 ), bytes: 1151439360 ( 0 ) ]
Sat Nov 12 14:29:52 2022 - hdisk0 => reads: [ blocks: 41718796 ( 0 ), bytes: 4180154368 ( 0 ) ], writes: [ blocks: 52580553 ( 0 ), bytes: 1151439360 ( 0 ) ]
Sat Nov 12 14:29:54 2022 - hdisk0 => reads: [ blocks: 41723006 ( 4210 ), bytes: 4182309888 ( 2155520 ) ], writes: [ blocks: 52580625 ( 72 ), bytes: 1151476224 ( 36864 ) ]
Sat Nov 12 14:29:56 2022 - hdisk0 => reads: [ blocks: 41723006 ( 0 ), bytes: 4182309888 ( 0 ) ], writes: [ blocks: 52580625 ( 0 ), bytes: 1151476224 ( 0 ) ]
Sat Nov 12 14:29:58 2022 - hdisk0 => reads: [ blocks: 41723006 ( 0 ), bytes: 4182309888 ( 0 ) ], writes: [ blocks: 52580625 ( 0 ), bytes: 1151476224 ( 0 ) ]
Sat Nov 12 14:30:00 2022 - hdisk0 => reads: [ blocks: 41723006 ( 0 ), bytes: 4182309888 ( 0 ) ], writes: [ blocks: 52580625 ( 0 ), bytes: 1151476224 ( 0 ) ]
Sat Nov 12 14:30:02 2022 - hdisk0 => reads: [ blocks: 41723006 ( 0 ), bytes: 4182309888 ( 0 ) ], writes: [ blocks: 52580657 ( 32 ), bytes: 1151492608 ( 16384 ) ]
Sat Nov 12 14:30:04 2022 - hdisk0 => reads: [ blocks: 41727216 ( 4210 ), bytes: 4184465408 ( 2155520 ) ], writes: [ blocks: 52580737 ( 80 ), bytes: 1151533568 ( 40960 ) ]
Sat Nov 12 14:30:06 2022 - hdisk0 => reads: [ blocks: 41727216 ( 0 ), bytes: 4184465408 ( 0 ) ], writes: [ blocks: 52580737 ( 0 ), bytes: 1151533568 ( 0 ) ]
Sat Nov 12 14:30:08 2022 - hdisk0 => reads: [ blocks: 41727216 ( 0 ), bytes: 4184465408 ( 0 ) ], writes: [ blocks: 52580737 ( 0 ), bytes: 1151533568 ( 0 ) ]
Sat Nov 12 14:30:10 2022 - hdisk0 => reads: [ blocks: 41727216 ( 0 ), bytes: 4184465408 ( 0 ) ], writes: [ blocks: 52580737 ( 0 ), bytes: 1151533568 ( 0 ) ]
Sat Nov 12 14:30:12 2022 - hdisk0 => reads: [ blocks: 41727216 ( 0 ), bytes: 4184465408 ( 0 ) ], writes: [ blocks: 52580737 ( 0 ), bytes: 1151533568 ( 0 ) ]
Sat Nov 12 14:30:14 2022 - hdisk0 => reads: [ blocks: 41731426 ( 4210 ), bytes: 4186620928 ( 2155520 ) ], writes: [ blocks: 52580849 ( 112 ), bytes: 1151590912 ( 57344 ) ]
```

151
main.c
View File

@ -10,103 +10,117 @@ Modified example from: https://www.ibm.com/docs/en/aix/7.3?topic=interfaces-perf
#include <unistd.h>
#include <libperfstat.h>
const int true = 1;
const int false = 0;
int startsWith(const char *a, const char *b) {
if(strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
if(strncmp(a, b, strlen(b)) == 0) return true;
return false;
}
int main(int argc, char* argv[]) {
int i, ret, tot;
perfstat_disk_t *statp;
perfstat_id_t first;
int i, ret, tot, sleepSecs;
perfstat_disk_t *statp;
perfstat_id_t first;
time_t now;
/* check how many perfstat_disk_t structures are available */
tot = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0);
if(argc == 2) {
char *a = argv[1];
sleepSecs = atoi(a);
} else {
sleepSecs = 10;
}
/* 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);
}
/* keep previous values to calculate difference */
long history[32][8] = { 0 };
/* allocate enough memory for all the structures */
statp = calloc(tot, sizeof(perfstat_disk_t));
/* set name to first interface */
strcpy(first.name, FIRST_DISK);
while (1)
{
/* 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 (tot < 0) {
if (ret <= 0) {
perror("perfstat_disk");
exit(-1);
}
if (tot == 0) {
printf("No disks found in the system\n");
exit(-1);
}
long history[32][8] = { 0 };
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);
}
// Obtain current time
// `time()` returns the current time of the system as a `time_t` value
time(&now);
/* print statistics for each of the disks */
for (i = 0; i < ret; i++) {
/* print statistics for each of the disks */
for (i = 0; i < ret; i++) {
if(!startsWith(statp[i].name, "hdisk")) {
continue;
}
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 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 rbytes = 0;
if(rblks > 0) {
rbytes = rblks * bsize;
}
unsigned long wbytes = 0;
if (wblks > 0) {
wbytes = wblks * bsize;
}
unsigned long wbytes = 0;
if (wblks > 0) {
wbytes = wblks * bsize;
}
unsigned long drblbks = 0;
if (history[i][0] > 0) {
drblbks = rblks - history[i][0];
}
unsigned long drblbks = 0;
if (history[i][0] > 0) {
drblbks = rblks - history[i][0];
}
unsigned long drbytes = 0;
if(history[i][0] > 0) {
drbytes = rbytes - (history[i][0] * bsize);
}
unsigned long drbytes = 0;
if(history[i][0] > 0) {
drbytes = rbytes - (history[i][0] * bsize);
}
unsigned long dwblbks = 0;
if (history[i][1] > 0) {
dwblbks = wblks - history[i][1];
}
unsigned long dwblbks = 0;
if (history[i][1] > 0) {
dwblbks = wblks - history[i][1];
}
unsigned long dwbytes = 0;
if(history[i][1] > 0) {
dwbytes = wbytes - (history[i][1] * bsize);
}
unsigned long dwbytes = 0;
if(history[i][1] > 0) {
dwbytes = wbytes - (history[i][1] * bsize);
}
printf("%s - %s => reads: [ blocks: %10u (%8u ), bytes: %10u (%8u ) ], writes: [ blocks: %10u (%8u ), bytes: %10u (%8u ) ]\n",
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,
@ -118,12 +132,11 @@ int main(int argc, char* argv[]) {
wbytes,
dwbytes);
history[i][0] = rblks;
history[i][1] = wblks;
}
sleep(10);
history[i][0] = rblks;
history[i][1] = wblks;
}
sleep(sleepSecs);
}
}