free.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 (2.4 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. };
  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(unsigned long *cached_kb, unsigned long *available_kb)
  50. {
  51. char buf[60]; /* actual lines we expect are ~30 chars or less */
  52. FILE *fp;
  53. int seen_cached_and_available;
  54. fp = xfopen_for_read("/proc/meminfo");
  55. *cached_kb = *available_kb = 0;
  56. seen_cached_and_available = 2;
  57. while (fgets(buf, sizeof(buf), fp)) {
  58. if (sscanf(buf, "Cached: %lu %*s\n", cached_kb) == 1)
  59. if (--seen_cached_and_available == 0)
  60. break;
  61. if (sscanf(buf, "MemAvailable: %lu %*s\n", available_kb) == 1)
  62. if (--seen_cached_and_available == 0)
  63. break;
  64. }
  65. /* Have to close because of NOFORK */
  66. fclose(fp);
  67. return seen_cached_and_available == 0;
  68. }
  69. int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  70. int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
  71. {
  72. struct globals G;
  73. struct sysinfo info;
  74. unsigned long long cached, cached_plus_free, available;
  75. unsigned long cached_kb, available_kb;
  76. int seen_available;
  77. #if ENABLE_DESKTOP
  78. G.unit_steps = 10;
  79. if (argv[1] && argv[1][0] == '-') {
  80. switch (argv[1][1]) {
  81. case 'b':
  82. G.unit_steps = 0;
  83. break;
  84. case 'k': /* 2^10 */
  85. /* G.unit_steps = 10; - already is */
  86. break;
  87. case 'm': /* 2^(2*10) */
  88. G.unit_steps = 20;
  89. break;
  90. case 'g': /* 2^(3*10) */
  91. G.unit_steps = 30;
  92. break;
  93. default:
  94. bb_show_usage();
  95. }
  96. }
  97. #endif
  98. printf(" %12s%12s%12s%12s%12s%12s\n"
  99. "Mem: ",
  100. "total",
  101. "used",
  102. "free",
  103. "shared", "buff/cache", "available" /* swap and total don't have these columns */
  104. );
  105. sysinfo(&info);
  106. /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
  107. G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
  108. /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
  109. seen_available = parse_meminfo(&cached_kb, &available_kb);
  110. available = ((unsigned long long) available_kb * 1024) / G.mem_unit;
  111. cached = ((unsigned long long) cached_kb * 1024) / G.mem_unit;
  112. cached += info.bufferram;
  113. cached_plus_free = cached + info.freeram;
  114. #define FIELDS_6 "%12llu%12llu%12llu%12llu%12llu%12llu\n"
  115. #define FIELDS_3 (FIELDS_6 + 3*6)
  116. #define FIELDS_2 (FIELDS_6 + 4*6)
  117. printf(FIELDS_6,
  118. scale(&G, info.totalram), //total
  119. scale(&G, info.totalram - cached_plus_free), //used
  120. scale(&G, info.freeram), //free
  121. scale(&G, info.sharedram), //shared
  122. scale(&G, cached), //buff/cache
  123. scale(&G, available) //available
  124. );
  125. /* On kernels < 3.14, MemAvailable is not provided.
  126. * Show alternate, more meaningful busy/free numbers by counting
  127. * buffer cache as free memory. */
  128. if (!seen_available) {
  129. printf("-/+ buffers/cache: ");
  130. printf(FIELDS_2,
  131. scale(&G, info.totalram - cached_plus_free), //used
  132. scale(&G, cached_plus_free) //free
  133. );
  134. }
  135. #if BB_MMU
  136. printf("Swap: ");
  137. printf(FIELDS_3,
  138. scale(&G, info.totalswap), //total
  139. scale(&G, info.totalswap - info.freeswap), //used
  140. scale(&G, info.freeswap) //free
  141. );
  142. #endif
  143. return EXIT_SUCCESS;
  144. }