free.c 5.0 KB

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