procps/free.c

123 lines
3.8 KiB
C
Raw Normal View History

2004-01-30 10:17:14 +05:30
// free.c - free(1)
// procps utility to display free memory information
//
// All new, Robert Love <rml@tech9.net> 18 Nov 2002
// Original by Brian Edmonds and Rafal Maszkowski 14 Dec 1992
//
// This program is licensed under the GNU Library General Public License, v2
//
// Copyright 2003 Robert Love
// Copyright 2004 Albert Cahalan
2002-02-02 04:17:29 +05:30
#include "proc/sysinfo.h"
#include "proc/version.h"
2004-01-30 10:17:14 +05:30
//#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
2002-02-02 04:17:29 +05:30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2004-01-30 10:17:14 +05:30
#define S(X) ( ((unsigned long long)(X) << 10) >> shift)
2002-02-02 04:17:29 +05:30
2004-01-30 10:17:14 +05:30
const char help_message[] =
"usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]\n"
" -b,-k,-m,-g show output in bytes, KB, MB, or GB\n"
" -l show detailed low and high memory statistics\n"
" -o use old format (no -/+buffers/cache line)\n"
" -t display total for RAM + swap\n"
" -s update every [delay] seconds\n"
" -c update [count] times\n"
" -V display version information and exit\n"
;
2002-02-02 04:17:29 +05:30
int main(int argc, char *argv[]){
int i;
2004-01-30 10:17:14 +05:30
int count = 0;
int shift = 10;
int pause_length = 0;
int show_high = 0;
int show_total = 0;
2002-02-02 04:17:29 +05:30
int old_fmt = 0;
/* check startup flags */
2004-01-30 10:17:14 +05:30
while( (i = getopt(argc, argv, "bkmglotc:s:V") ) != -1 )
2002-02-02 04:17:29 +05:30
switch (i) {
2004-01-30 10:17:14 +05:30
case 'b': shift = 0; break;
case 'k': shift = 10; break;
case 'm': shift = 20; break;
case 'g': shift = 30; break;
case 'l': show_high = 1; break;
2002-02-02 04:17:29 +05:30
case 'o': old_fmt = 1; break;
2004-01-30 10:17:14 +05:30
case 't': show_total = 1; break;
case 's': pause_length = 1000000 * atof(optarg); break;
case 'c': count = strtoul(optarg, NULL, 10); break;
2002-02-02 04:17:29 +05:30
case 'V': display_version(); exit(0);
default:
2004-01-30 10:17:14 +05:30
fwrite(help_message,1,strlen(help_message),stderr);
return 1;
2002-02-02 04:17:29 +05:30
}
do {
meminfo();
printf(" total used free shared buffers cached\n");
printf(
2004-01-30 10:17:14 +05:30
"%-7s %10Lu %10Lu %10Lu %10Lu %10Lu %10Lu\n", "Mem:",
2002-02-02 04:17:29 +05:30
S(kb_main_total),
S(kb_main_used),
S(kb_main_free),
S(kb_main_shared),
S(kb_main_buffers),
S(kb_main_cached)
);
2004-01-30 10:17:14 +05:30
// Print low vs. high information, if the user requested it.
// Note we check if low_total==0: if so, then this kernel does
// not export the low and high stats. Note we still want to
// print the high info, even if it is zero.
if (show_high) {
printf(
"%-7s %10Lu %10Lu %10Lu\n", "Low:",
S(kb_low_total),
S(kb_low_total - kb_low_free),
S(kb_low_free)
);
printf(
"%-7s %10Lu %10Lu %10Lu\n", "High:",
S(kb_high_total),
S(kb_high_total - kb_high_free),
S(kb_high_free)
);
}
2002-02-02 04:17:29 +05:30
if(!old_fmt){
2004-01-30 10:17:14 +05:30
unsigned KLONG buffers_plus_cached = kb_main_buffers + kb_main_cached;
2002-02-02 04:17:29 +05:30
printf(
2004-01-30 10:17:14 +05:30
"-/+ buffers/cache: %10Lu %10Lu\n",
S(kb_main_used - buffers_plus_cached),
S(kb_main_free + buffers_plus_cached)
2002-02-02 04:17:29 +05:30
);
}
printf(
2004-01-30 10:17:14 +05:30
"%-7s %10Lu %10Lu %10Lu\n", "Swap:",
2002-02-02 04:17:29 +05:30
S(kb_swap_total),
S(kb_swap_used),
S(kb_swap_free)
);
2004-01-30 10:17:14 +05:30
if(show_total){
2002-02-02 04:17:29 +05:30
printf(
2004-01-30 10:17:14 +05:30
"%-7s %10Lu %10Lu %10Lu\n", "Total:",
2002-02-02 04:17:29 +05:30
S(kb_main_total + kb_swap_total),
S(kb_main_used + kb_swap_used),
S(kb_main_free + kb_swap_free)
);
}
2004-01-30 10:17:14 +05:30
if(pause_length){
2002-02-02 04:17:29 +05:30
fputc('\n', stdout);
fflush(stdout);
2004-01-30 10:17:14 +05:30
if (count != 1) usleep(pause_length);
2002-02-02 04:17:29 +05:30
}
2004-01-30 10:17:14 +05:30
} while(pause_length && --count);
2002-02-02 04:17:29 +05:30
return 0;
}