| 296 |
|
} |
| 297 |
|
|
| 298 |
|
/* VM */ |
| 299 |
< |
if (stats.mem_stats != NULL) { |
| 299 |
> |
if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { |
| 300 |
|
move(6, 54); |
| 301 |
|
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); |
| 302 |
|
} |
| 303 |
< |
if (stats.swap_stats != NULL) { |
| 303 |
> |
if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { |
| 304 |
|
move(7, 54); |
| 305 |
|
printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); |
| 306 |
|
} |
| 307 |
< |
if (stats.mem_stats != NULL && stats.swap_stats != NULL) { |
| 307 |
> |
if (stats.mem_stats != NULL && stats.swap_stats != NULL && |
| 308 |
> |
stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { |
| 309 |
|
move(8, 54); |
| 310 |
|
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total))); |
| 311 |
|
} |
| 428 |
|
extern int optind; |
| 429 |
|
int c; |
| 430 |
|
|
| 431 |
+ |
time_t last_update = 0; |
| 432 |
+ |
|
| 433 |
|
WINDOW *window; |
| 434 |
|
|
| 432 |
– |
int stdin_fileno; |
| 433 |
– |
fd_set infds; |
| 434 |
– |
struct timeval timeout; |
| 435 |
– |
|
| 435 |
|
extern int errno; |
| 437 |
– |
char ch; |
| 436 |
|
|
| 437 |
|
int delay=2; |
| 438 |
|
#ifdef ALLBSD |
| 446 |
|
} |
| 447 |
|
#endif |
| 448 |
|
|
| 449 |
< |
while ((c = getopt(argc, argv, "vhd:")) != EOF){ |
| 449 |
> |
while ((c = getopt(argc, argv, "vhd:")) != -1){ |
| 450 |
|
switch (c){ |
| 451 |
|
case 'd': |
| 452 |
|
delay = atoi(optarg); |
| 454 |
|
fprintf(stderr, "Time must be 1 second or greater\n"); |
| 455 |
|
exit(1); |
| 456 |
|
} |
| 459 |
– |
delay--; |
| 457 |
|
break; |
| 458 |
|
case 'v': |
| 459 |
|
version_num(argv[0]); |
| 463 |
|
usage(argv[0]); |
| 464 |
|
return 1; |
| 465 |
|
break; |
| 469 |
– |
|
| 466 |
|
} |
| 467 |
|
} |
| 468 |
|
|
| 471 |
|
nonl(); |
| 472 |
|
cbreak(); |
| 473 |
|
noecho(); |
| 474 |
+ |
timeout(delay * 1000); |
| 475 |
|
window=newwin(0, 0, 0, 0); |
| 476 |
|
clear(); |
| 477 |
|
|
| 482 |
|
} |
| 483 |
|
|
| 484 |
|
display_headings(); |
| 488 |
– |
stdin_fileno=fileno(stdin); |
| 485 |
|
|
| 486 |
|
for(;;){ |
| 487 |
+ |
time_t now; |
| 488 |
+ |
int ch = getch(); |
| 489 |
|
|
| 490 |
< |
FD_ZERO(&infds); |
| 491 |
< |
FD_SET(stdin_fileno, &infds); |
| 494 |
< |
timeout.tv_sec = delay; |
| 495 |
< |
timeout.tv_usec = 0; |
| 496 |
< |
|
| 497 |
< |
if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){ |
| 498 |
< |
if(errno!=EINTR){ |
| 499 |
< |
perror("select failed"); |
| 500 |
< |
exit(1); |
| 501 |
< |
} |
| 490 |
> |
if (ch == 'q'){ |
| 491 |
> |
break; |
| 492 |
|
} |
| 493 |
|
|
| 494 |
< |
if(FD_ISSET(stdin_fileno, &infds)){ |
| 495 |
< |
ch=getch(); |
| 496 |
< |
if (ch == 'q'){ |
| 497 |
< |
endwin(); |
| 498 |
< |
return 0; |
| 499 |
< |
} |
| 494 |
> |
/* To keep the numbers slightly accurate we do not want them |
| 495 |
> |
* updating more frequently than once a second. |
| 496 |
> |
*/ |
| 497 |
> |
now = time(NULL); |
| 498 |
> |
if ((now - last_update) >= 1) { |
| 499 |
> |
get_stats(); |
| 500 |
|
} |
| 501 |
+ |
last_update = now; |
| 502 |
|
|
| 512 |
– |
get_stats(); |
| 513 |
– |
|
| 503 |
|
display_data(); |
| 515 |
– |
|
| 516 |
– |
/* To keep the numbers slightly accurate we do not want them updating more |
| 517 |
– |
* frequently than once a second. |
| 518 |
– |
*/ |
| 519 |
– |
sleep(1); |
| 504 |
|
} |
| 505 |
|
|
| 506 |
|
endwin(); |