free.c 3.6 KB

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