busybox/procps/free.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Mini free implementation for busybox
   4 *
   5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
   6 *
   7 * Licensed under GPLv2, see file LICENSE in this source tree.
   8 */
   9//config:config FREE
  10//config:       bool "free (3.1 kb)"
  11//config:       default y
  12//config:       help
  13//config:       free displays the total amount of free and used physical and swap
  14//config:       memory in the system, as well as the buffers used by the kernel.
  15//config:       The shared memory column should be ignored; it is obsolete.
  16
  17//applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
  18
  19//kbuild:lib-$(CONFIG_FREE) += free.o
  20
  21//usage:#define free_trivial_usage
  22//usage:       "" IF_DESKTOP("[-bkmgh]")
  23//usage:#define free_full_usage "\n\n"
  24//usage:       "Display free and used memory"
  25//usage:
  26//usage:#define free_example_usage
  27//usage:       "$ free\n"
  28//usage:       "              total         used         free       shared      buffers\n"
  29//usage:       "  Mem:       257628       248724         8904        59644        93124\n"
  30//usage:       " Swap:       128516         8404       120112\n"
  31//usage:       "Total:       386144       257128       129016\n"
  32//procps-ng 3.3.15:
  33// -b, --bytes         show output in bytes
  34//     --kilo          show output in kilobytes
  35//     --mega          show output in megabytes
  36//     --giga          show output in gigabytes
  37//     --tera          show output in terabytes
  38//     --peta          show output in petabytes
  39// -k, --kibi          show output in kibibytes
  40// -m, --mebi          show output in mebibytes
  41// -g, --gibi          show output in gibibytes
  42//     --tebi          show output in tebibytes
  43//     --pebi          show output in pebibytes
  44// -h, --human         show human-readable output
  45//     --si            use powers of 1000 not 1024
  46// -l, --lohi          show detailed low and high memory statistics
  47// -t, --total         show total for RAM + swap
  48// -s N, --seconds N   repeat printing every N seconds
  49// -c N, --count N     repeat printing N times, then exit
  50// -w, --wide          wide output
  51//
  52//NB: if we implement -s or -c, need to stop being NOFORK!
  53
  54#include "libbb.h"
  55#ifdef __linux__
  56# include <sys/sysinfo.h>
  57#endif
  58
  59struct globals {
  60        unsigned mem_unit;
  61#if ENABLE_DESKTOP
  62        unsigned unit;
  63# define G_unit g->unit
  64#else
  65# define G_unit (1 << 10)
  66#endif
  67        unsigned long cached_kb, available_kb, reclaimable_kb;
  68};
  69/* Because of NOFORK, "globals" are not in global data */
  70
  71static const char *scale(struct globals *g, unsigned long d)
  72{
  73        /* Display (size * block_size) with one decimal digit.
  74         * If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...),
  75         * else divide by display_unit and do not use suffix.
  76         * Returns "auto pointer" */
  77        return make_human_readable_str(d, g->mem_unit, G_unit);
  78}
  79
  80/* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
  81static NOINLINE unsigned int parse_meminfo(struct globals *g)
  82{
  83        char buf[60]; /* actual lines we expect are ~30 chars or less */
  84        FILE *fp;
  85        int seen_cached_and_available_and_reclaimable;
  86
  87        fp = xfopen_for_read("/proc/meminfo");
  88        g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
  89        seen_cached_and_available_and_reclaimable = 3;
  90        while (fgets(buf, sizeof(buf), fp)) {
  91                if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
  92                        if (--seen_cached_and_available_and_reclaimable == 0)
  93                                break;
  94                if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
  95                        if (--seen_cached_and_available_and_reclaimable == 0)
  96                                break;
  97                if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
  98                        if (--seen_cached_and_available_and_reclaimable == 0)
  99                                break;
 100        }
 101        /* Have to close because of NOFORK */
 102        fclose(fp);
 103
 104        return seen_cached_and_available_and_reclaimable == 0;
 105}
 106
 107int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 108int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
 109{
 110        struct globals G;
 111        struct sysinfo info;
 112        unsigned long long cached, cached_plus_free, available;
 113        int seen_available;
 114
 115#if ENABLE_DESKTOP
 116        G.unit = 1 << 10;
 117        if (argv[1] && argv[1][0] == '-') {
 118                switch (argv[1][1]) {
 119                case 'b':
 120                        G.unit = 1;
 121                        break;
 122                case 'k': /* 2^10 */
 123                        /* G.unit = 1 << 10; - already is */
 124                        break;
 125                case 'm': /* 2^20 */
 126                        G.unit = 1 << 20;
 127                        break;
 128                case 'g': /* 2^30 */
 129                        G.unit = 1 << 30;
 130                        break;
 131//              case 't':
 132// -- WRONG, -t is not "terabytes" in procps-ng, it's --total
 133//                      G.unit = 1 << 40;
 134//                      break;
 135                case 'h':
 136                        G.unit = 0; /* human readable */
 137                        break;
 138                default:
 139                        bb_show_usage();
 140                }
 141        }
 142#endif
 143        printf("       %12s%12s%12s%12s%12s%12s\n"
 144        "Mem:   ",
 145                "total",
 146                "used",
 147                "free",
 148                "shared", "buff/cache", "available" /* swap and total don't have these columns */
 149        );
 150
 151        sysinfo(&info);
 152        /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
 153        seen_available = parse_meminfo(&G);
 154        G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* kernels < 2.4.x return mem_unit==0, so cope */
 155        available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
 156        cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
 157        cached += info.bufferram;
 158        cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
 159        cached_plus_free = cached + info.freeram;
 160
 161        printf("%12s%12s%12s",
 162                scale(&G, info.totalram),                //total
 163                scale(&G, info.totalram - cached_plus_free), //used
 164                scale(&G, info.freeram)                  //free
 165        );
 166        /* using two printf's: only 4 auto strings are supported, we need 6 */
 167        printf("%12s%12s%12s\n",
 168                scale(&G, info.sharedram),               //shared
 169                scale(&G, cached),                       //buff/cache
 170                scale(&G, available)                     //available
 171        );
 172        /* On kernels < 3.14, MemAvailable is not provided.
 173         * Show alternate, more meaningful busy/free numbers by counting
 174         * buffer cache as free memory. */
 175        if (!seen_available) {
 176                printf("-/+ buffers/cache: ");
 177                printf("%12s%12s%12s\n" + 4,
 178                        scale(&G, info.totalram - cached_plus_free), //used
 179                        scale(&G, cached_plus_free)                  //free
 180                );
 181        }
 182#if BB_MMU
 183        printf("Swap:  ");
 184        printf("%12s%12s%12s\n",
 185                scale(&G, info.totalswap),                 //total
 186                scale(&G, info.totalswap - info.freeswap), //used
 187                scale(&G, info.freeswap)                   //free
 188        );
 189#endif
 190        return EXIT_SUCCESS;
 191}
 192