2 // procps utility to display free memory information
4 // All new, Robert Love <rml@tech9.net> 18 Nov 2002
5 // Original by Brian Edmonds and Rafal Maszkowski 14 Dec 1992
7 // This program is licensed under the GNU Library General Public License, v2
9 // Copyright 2003 Robert Love
10 // Copyright 2004 Albert Cahalan
12 #include "proc/sysinfo.h"
13 #include "proc/version.h"
22 #define S(X) ( ((unsigned long long)(X) << 10) >> shift)
24 const char help_message[] =
25 "usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]\n"
26 " -b,-k,-m,-g show output in bytes, KB, MB, or GB\n"
27 " -l show detailed low and high memory statistics\n"
28 " -o use old format (no -/+buffers/cache line)\n"
29 " -t display total for RAM + swap\n"
30 " -s update every [delay] seconds\n"
31 " -c update [count] times\n"
32 " -V display version information and exit\n"
35 int main(int argc, char *argv[]){
44 /* check startup flags */
45 while( (i = getopt(argc, argv, "bkmglotc:s:V") ) != -1 )
47 case 'b': shift = 0; break;
48 case 'k': shift = 10; break;
49 case 'm': shift = 20; break;
50 case 'g': shift = 30; break;
51 case 'l': show_high = 1; break;
52 case 'o': old_fmt = 1; break;
53 case 't': show_total = 1; break;
54 case 's': pause_length = 1000000 * atof(optarg); break;
55 case 'c': count = strtoul(optarg, NULL, 10); break;
56 case 'V': display_version(); exit(0);
58 fwrite(help_message,1,strlen(help_message),stderr);
64 printf(" total used free shared buffers cached\n");
66 "%-7s %10Lu %10Lu %10Lu %10Lu %10Lu %10Lu\n", "Mem:",
74 // Print low vs. high information, if the user requested it.
75 // Note we check if low_total==0: if so, then this kernel does
76 // not export the low and high stats. Note we still want to
77 // print the high info, even if it is zero.
80 "%-7s %10Lu %10Lu %10Lu\n", "Low:",
82 S(kb_low_total - kb_low_free),
86 "%-7s %10Lu %10Lu %10Lu\n", "High:",
88 S(kb_high_total - kb_high_free),
93 unsigned KLONG buffers_plus_cached = kb_main_buffers + kb_main_cached;
95 "-/+ buffers/cache: %10Lu %10Lu\n",
96 S(kb_main_used - buffers_plus_cached),
97 S(kb_main_free + buffers_plus_cached)
101 "%-7s %10Lu %10Lu %10Lu\n", "Swap:",
108 "%-7s %10Lu %10Lu %10Lu\n", "Total:",
109 S(kb_main_total + kb_swap_total),
110 S(kb_main_used + kb_swap_used),
111 S(kb_main_free + kb_swap_free)
117 if (count != 1) usleep(pause_length);
119 } while(pause_length && --count);