free.c 6.2 KB

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