free.c 4.1 KB

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