3
0

free.c 4.0 KB

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