free.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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("[-b/k/m/g]")
  20. //usage:#define free_full_usage "\n\n"
  21. //usage: "Display the amount of free and used system 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. #include "libbb.h"
  30. #ifdef __linux__
  31. # include <sys/sysinfo.h>
  32. #endif
  33. struct globals {
  34. unsigned mem_unit;
  35. #if ENABLE_DESKTOP
  36. uint8_t unit_steps;
  37. # define G_unit_steps g->unit_steps
  38. #else
  39. # define G_unit_steps 10
  40. #endif
  41. unsigned long cached_kb, available_kb, reclaimable_kb;
  42. };
  43. /* Because of NOFORK, "globals" are not in global data */
  44. static unsigned long long scale(struct globals *g, unsigned long d)
  45. {
  46. return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
  47. }
  48. /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
  49. static NOINLINE unsigned int parse_meminfo(struct globals *g)
  50. {
  51. char buf[60]; /* actual lines we expect are ~30 chars or less */
  52. FILE *fp;
  53. int seen_cached_and_available_and_reclaimable;
  54. fp = xfopen_for_read("/proc/meminfo");
  55. g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
  56. seen_cached_and_available_and_reclaimable = 3;
  57. while (fgets(buf, sizeof(buf), fp)) {
  58. if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
  59. if (--seen_cached_and_available_and_reclaimable == 0)
  60. break;
  61. if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
  62. if (--seen_cached_and_available_and_reclaimable == 0)
  63. break;
  64. if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
  65. if (--seen_cached_and_available_and_reclaimable == 0)
  66. break;
  67. }
  68. /* Have to close because of NOFORK */
  69. fclose(fp);
  70. return seen_cached_and_available_and_reclaimable == 0;
  71. }
  72. int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  73. int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
  74. {
  75. struct globals G;
  76. struct sysinfo info;
  77. unsigned long long cached, cached_plus_free, available;
  78. int seen_available;
  79. #if ENABLE_DESKTOP
  80. G.unit_steps = 10;
  81. if (argv[1] && argv[1][0] == '-') {
  82. switch (argv[1][1]) {
  83. case 'b':
  84. G.unit_steps = 0;
  85. break;
  86. case 'k': /* 2^10 */
  87. /* G.unit_steps = 10; - already is */
  88. break;
  89. case 'm': /* 2^20 */
  90. G.unit_steps = 20;
  91. break;
  92. case 'g': /* 2^30 */
  93. G.unit_steps = 30;
  94. break;
  95. default:
  96. bb_show_usage();
  97. }
  98. }
  99. #endif
  100. printf(" %12s%12s%12s%12s%12s%12s\n"
  101. "Mem: ",
  102. "total",
  103. "used",
  104. "free",
  105. "shared", "buff/cache", "available" /* swap and total don't have these columns */
  106. );
  107. sysinfo(&info);
  108. /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
  109. seen_available = parse_meminfo(&G);
  110. G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* kernels < 2.4.x return mem_unit==0, so cope */
  111. available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
  112. cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
  113. cached += info.bufferram;
  114. cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
  115. cached_plus_free = cached + info.freeram;
  116. /* In case (long long * G.mem_unit) can overflow, this can be used to reduce the chances */
  117. #if 0 //ENABLE_DESKTOP
  118. while (!(G.mem_unit & 1) && G.unit_steps != 0) {
  119. G.mem_unit >>= 1;
  120. G.unit_steps--;
  121. //bb_error_msg("mem_unit:%d unit_steps:%d", G.mem_unit, G.unit_steps);
  122. }
  123. #endif
  124. #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"
  125. #define FIELDS_3 (FIELDS_6 + 6 + 7 + 7)
  126. #define FIELDS_2 (FIELDS_6 + 6 + 7 + 7 + 7)
  127. printf(FIELDS_6,
  128. scale(&G, info.totalram), //total
  129. scale(&G, info.totalram - cached_plus_free), //used
  130. scale(&G, info.freeram), //free
  131. scale(&G, info.sharedram), //shared
  132. scale(&G, cached), //buff/cache
  133. scale(&G, available) //available
  134. );
  135. /* On kernels < 3.14, MemAvailable is not provided.
  136. * Show alternate, more meaningful busy/free numbers by counting
  137. * buffer cache as free memory. */
  138. if (!seen_available) {
  139. printf("-/+ buffers/cache: ");
  140. printf(FIELDS_2,
  141. scale(&G, info.totalram - cached_plus_free), //used
  142. scale(&G, cached_plus_free) //free
  143. );
  144. }
  145. #if BB_MMU
  146. printf("Swap: ");
  147. printf(FIELDS_3,
  148. scale(&G, info.totalswap), //total
  149. scale(&G, info.totalswap - info.freeswap), //used
  150. scale(&G, info.freeswap) //free
  151. );
  152. #endif
  153. return EXIT_SUCCESS;
  154. }