]> granicus.if.org Git - procps-ng/commitdiff
free: support for MemAvailable
authorJaromir Capik <jcapik@redhat.com>
Fri, 11 Jul 2014 20:34:06 +0000 (22:34 +0200)
committerJaromir Capik <jcapik@redhat.com>
Fri, 11 Jul 2014 20:34:06 +0000 (22:34 +0200)
This commit adds a new switch -a/--available that
appends a new column called 'available' to the
output. The column displays an estimation
of how much memory is available for starting
new applications, without swapping. Unlike the data
provided by the 'cached' or 'free' fields, this
field takes into account page cache and also that
not all reclaimable memory slabs will be reclaimed
due to items being in use.

free.1
free.c
proc/libprocps.sym
proc/sysinfo.c
proc/sysinfo.h

diff --git a/free.1 b/free.1
index 85efef88e2a0884ca96dfd73fd4474ad4bb2be84..a4665ea9c59c96ba8692e7e078eb238dfcfc6b2b 100644 (file)
--- a/free.1
+++ b/free.1
@@ -33,6 +33,14 @@ Memory used by kernel buffers (Buffers in /proc/meminfo)
 .TP
 \fBcached\fR
 Memory used by the page cache (Cached in /proc/meminfo)
+.TP
+\fBavailable\fR
+Estimation of how much memory is available for starting
+new applications, without swapping. Unlike the data
+provided by the \fBcached\fR or \fBfree\fR fields,
+this field takes into account page cache and also that
+not all reclaimable memory slabs will be reclaimed
+due to items being in use (MemAvailable in /proc/meminfo)
 .SH OPTIONS
 .TP
 \fB\-b\fR, \fB\-\-bytes\fR
@@ -65,6 +73,10 @@ display the units of print out.  Following units are used.
 If unit is missing, and you have petabyte of RAM or swap, the number is in
 terabytes and columns might not be aligned with header.
 .TP
+\fB\-a\fR, \fB\-\-available\fR
+Display the estimation of memory available for starting new applications,
+without swapping. This switch makes the output wider than 80 characters.
+.TP
 \fB\-c\fR, \fB\-\-count\fR \fIcount\fR
 Display the result
 .I count
diff --git a/free.c b/free.c
index d95feaa81144309a2cce3dadbea21214a424c03c..e6ea043d2719d59a48ef4be304ca0e84dadbb72f 100644 (file)
--- a/free.c
+++ b/free.c
@@ -54,6 +54,7 @@
 #define FREE_SI                        (1 << 5)
 #define FREE_REPEAT            (1 << 6)
 #define FREE_REPEATCOUNT       (1 << 7)
+#define FREE_AVAILABLE         (1 << 8)
 
 struct commandline_arguments {
        int exponent;           /* demanded in kilos, magas... */
@@ -85,6 +86,7 @@ static void __attribute__ ((__noreturn__))
        fputs(_(" -t, --total         show total for RAM + swap\n"), out);
        fputs(_(" -s N, --seconds N   repeat printing every N seconds\n"), out);
        fputs(_(" -c N, --count N     repeat printing N times, then exit\n"), out);
+       fputs(_(" -a, --available     show available memory\n"), out);
        fputs(USAGE_SEPARATOR, out);
        fputs(_("     --help     display this help and exit\n"), out);
        fputs(USAGE_VERSION, out);
@@ -213,6 +215,7 @@ int main(int argc, char **argv)
                {  "total",     no_argument,        NULL,  't'          },
                {  "seconds",   required_argument,  NULL,  's'          },
                {  "count",     required_argument,  NULL,  'c'          },
+               {  "available", no_argument,        NULL,  'a'          },
                {  "help",      no_argument,        NULL,  HELP_OPTION  },
                {  "version",   no_argument,        NULL,  'V'          },
                {  NULL,        0,                  NULL,  0            }
@@ -231,7 +234,7 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
        atexit(close_stdout);
 
-       while ((c = getopt_long(argc, argv, "bkmghlotc:s:V", longopts, NULL)) != -1)
+       while ((c = getopt_long(argc, argv, "bkmghlotc:as:V", longopts, NULL)) != -1)
                switch (c) {
                case 'b':
                        args.exponent = 1;
@@ -281,6 +284,9 @@ int main(int argc, char **argv)
                          error(EXIT_FAILURE, ERANGE,
                                  _("failed to parse count argument: '%s'"), optarg);
                        break;
+               case 'a':
+                       flags |= FREE_AVAILABLE;
+                       break;
                case HELP_OPTION:
                        usage(stdout);
                case 'V':
@@ -296,7 +302,9 @@ int main(int argc, char **argv)
                /* Translation Hint: You can use 9 character words in
                 * the header, and the words need to be right align to
                 * beginning of a number. */
-               printf("%s\n", _("             total       used       free     shared    buffers     cached"));
+               printf(_("             total       used       free     shared    buffers     cached"));
+               if (flags & FREE_AVAILABLE) printf(_("  available"));
+               printf("\n");
                printf("%-7s", _("Mem:"));
                printf(" %10s", scale_size(kb_main_total, flags, args));
                printf(" %10s", scale_size(kb_main_used, flags, args));
@@ -304,6 +312,7 @@ int main(int argc, char **argv)
                printf(" %10s", scale_size(kb_main_shared, flags, args));
                printf(" %10s", scale_size(kb_main_buffers, flags, args));
                printf(" %10s", scale_size(kb_main_cached, flags, args));
+               if (flags & FREE_AVAILABLE) printf(" %10s", scale_size(kb_main_available, flags, args));
                printf("\n");
                /*
                 * Print low vs. high information, if the user requested it.
index d4f692eebc4ff3cba22177e81bebc4b6e0b123a6..b86175ff93713ae303b2ab2e4e207a4f91868121 100644 (file)
@@ -26,6 +26,7 @@ global:
        kb_inactive;
        kb_low_free;
        kb_low_total;
+       kb_main_available;
        kb_main_buffers;
        kb_main_cached;
        kb_main_free;
index 85fb0a430945c67764f461e66ffe0a8de8443517..e3e2ca5b45b3fc96104c2e9ba56f9bb495d7ffdf 100644 (file)
@@ -573,6 +573,7 @@ unsigned long kb_high_free;
 unsigned long kb_high_total;
 unsigned long kb_low_free;
 unsigned long kb_low_total;
+unsigned long kb_main_available;
 /* 2.4.xx era */
 unsigned long kb_active;
 unsigned long kb_inact_laundry;
@@ -629,6 +630,7 @@ void meminfo(void){
   {"LowFree",      &kb_low_free},
   {"LowTotal",     &kb_low_total},
   {"Mapped",       &kb_mapped},       // kB version of vmstat nr_mapped
+  {"MemAvailable", &kb_main_available}, // important
   {"MemFree",      &kb_main_free},    // important
   {"MemTotal",     &kb_main_total},   // important
   {"NFS_Unstable", &kb_nfs_unstable},
index 22916319d8712ceba49dfc43ebebd400eac05157..2ec7954ac1e280a01485a27da8f798caf8d504b6 100644 (file)
@@ -34,6 +34,7 @@ extern unsigned long kb_high_free;
 extern unsigned long kb_high_total;
 extern unsigned long kb_low_free;
 extern unsigned long kb_low_total;
+extern unsigned long kb_main_available;
 /* 2.4.xx era */
 extern unsigned long kb_active;
 extern unsigned long kb_inact_laundry;  // grrr...