Home | History | Annotate | Download | only in other
      1 /* free.c - Display amount of free and used memory in the system.
      2  *
      3  * Copyright 2012 Elie De Brauwer <eliedebrauwer (at) gmail.com>
      4 
      5 // Flag order is signifcant: b-t are units in order, FLAG_h-1 is unit mask
      6 USE_FREE(NEWTOY(free, "htgmkb[!htgmkb]", TOYFLAG_USR|TOYFLAG_BIN))
      7 
      8 config FREE
      9   bool "free"
     10   default y
     11   help
     12     usage: free [-bkmgt]
     13 
     14     Display the total, free and used amount of physical memory and swap space.
     15 
     16     -bkmgt	Output units (default is bytes)
     17     -h	Human readable (K=1024)
     18 */
     19 
     20 #define FOR_free
     21 #include "toys.h"
     22 
     23 GLOBALS(
     24   unsigned bits;
     25   unsigned long long units;
     26   char *buf;
     27 )
     28 
     29 static char *convert(unsigned long d)
     30 {
     31   long long ll = d*TT.units;
     32   char *s = TT.buf;
     33 
     34   if (toys.optflags & FLAG_h) human_readable(s, ll, 0);
     35   else sprintf(s, "%llu",ll>>TT.bits);
     36   TT.buf += strlen(TT.buf)+1;
     37 
     38   return s;
     39 }
     40 
     41 void free_main(void)
     42 {
     43   struct sysinfo in;
     44 
     45   sysinfo(&in);
     46   TT.units = in.mem_unit ? in.mem_unit : 1;
     47   while ((toys.optflags&(FLAG_h-1)) && !(toys.optflags&(1<<TT.bits))) TT.bits++;
     48   TT.bits *= 10;
     49   TT.buf = toybuf;
     50 
     51   xprintf("\t\ttotal        used        free      shared     buffers\n"
     52     "Mem:%17s%12s%12s%12s%12s\n-/+ buffers/cache:%15s%12s\n"
     53     "Swap:%16s%12s%12s\n", convert(in.totalram),
     54     convert(in.totalram-in.freeram), convert(in.freeram), convert(in.sharedram),
     55     convert(in.bufferram), convert(in.totalram - in.freeram - in.bufferram),
     56     convert(in.freeram + in.bufferram), convert(in.totalswap),
     57     convert(in.totalswap - in.freeswap), convert(in.freeswap));
     58 }
     59