be65f46f134cb1629465ad57b4fa63a132050d11
[platform/upstream/busybox.git] / procps / free.c
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
10 /* getopt not needed */
11
12 #include "libbb.h"
13
14 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
16 {
17         struct sysinfo info;
18         unsigned mem_unit;
19
20 #if ENABLE_DESKTOP
21         if (argv[1] && argv[1][0] == '-')
22                 bb_show_usage();
23 #endif
24
25         sysinfo(&info);
26
27         /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
28         mem_unit = 1;
29         if (info.mem_unit != 0) {
30                 mem_unit = info.mem_unit;
31         }
32
33         /* Convert values to kbytes */
34         if (mem_unit == 1) {
35                 info.totalram >>= 10;
36                 info.freeram >>= 10;
37 #if BB_MMU
38                 info.totalswap >>= 10;
39                 info.freeswap >>= 10;
40 #endif
41                 info.sharedram >>= 10;
42                 info.bufferram >>= 10;
43         } else {
44                 mem_unit >>= 10;
45                 /* TODO:  Make all this stuff not overflow when mem >= 4 Tb */
46                 info.totalram *= mem_unit;
47                 info.freeram *= mem_unit;
48 #if BB_MMU
49                 info.totalswap *= mem_unit;
50                 info.freeswap *= mem_unit;
51 #endif
52                 info.sharedram *= mem_unit;
53                 info.bufferram *= mem_unit;
54         }
55
56         printf("     %13s%13s%13s%13s%13s\n",
57                 "total",
58                 "used",
59                 "free",
60                 "shared", "buffers" /* swap and total don't have these columns */
61                 /* procps version 3.2.8 also shows "cached" column, but
62                  * sysinfo() does not provide this value, need to parse
63                  * /proc/meminfo instead and get "Cached: NNN kB" from there.
64                  */
65         );
66 #define FIELDS_5 "%13lu%13lu%13lu%13lu%13lu\n"
67 #define FIELDS_3 (FIELDS_5 + 2*5)
68 #define FIELDS_2 (FIELDS_5 + 3*5)
69         printf("Mem: ");
70         printf(FIELDS_5,
71                 info.totalram,
72                 info.totalram - info.freeram,
73                 info.freeram,
74                 info.sharedram, info.bufferram
75         );
76         /* Show alternate, more meaningful busy/free numbers by counting
77          * buffer cache as free memory (make it "-/+ buffers/cache"
78          * if/when we add support for "cached" column): */
79         printf("-/+ buffers:      ");
80         printf(FIELDS_2,
81                 info.totalram - info.freeram - info.bufferram,
82                 info.freeram + info.bufferram
83         );
84 #if BB_MMU
85         printf("Swap:");
86         printf(FIELDS_3,
87                 info.totalswap,
88                 info.totalswap - info.freeswap,
89                 info.freeswap
90         );
91 #endif
92         return EXIT_SUCCESS;
93 }