• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* free.c - Display amount of free and used memory in the system.
2  *
3  * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
4 
5 USE_FREE(NEWTOY(free, "tgmkb[!tgmkb]", TOYFLAG_USR|TOYFLAG_BIN))
6 
7 config FREE
8   bool "free"
9   default y
10   help
11     usage: free [-bkmgt]
12 
13     Display the total, free and used amount of physical memory and swap space.
14 
15     -bkmgt	Output units (default is bytes)
16 */
17 
18 #define FOR_free
19 #include "toys.h"
20 
GLOBALS(unsigned bits;unsigned long long units;)21 GLOBALS(
22   unsigned bits;
23   unsigned long long units;
24 )
25 
26 static unsigned long long convert(unsigned long d)
27 {
28   return (d*TT.units)>>TT.bits;
29 }
30 
free_main(void)31 void free_main(void)
32 {
33   struct sysinfo in;
34 
35   sysinfo(&in);
36   TT.units = in.mem_unit ? in.mem_unit : 1;
37   for (TT.bits = 0; toys.optflags && !(toys.optflags&(1<<TT.bits)); TT.bits++);
38   TT.bits *= 10;
39 
40   xprintf("\t\ttotal        used        free      shared     buffers\n"
41     "Mem:%17llu%12llu%12llu%12llu%12llu\n-/+ buffers/cache:%15llu%12llu\n"
42     "Swap:%16llu%12llu%12llu\n", convert(in.totalram),
43     convert(in.totalram-in.freeram), convert(in.freeram), convert(in.sharedram),
44     convert(in.bufferram), convert(in.totalram - in.freeram - in.bufferram),
45     convert(in.freeram + in.bufferram), convert(in.totalswap),
46     convert(in.totalswap - in.freeswap), convert(in.freeswap));
47 }
48