free.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifdef __linux__
  23. # include <sys/sysinfo.h>
  24. #endif
  25. struct globals {
  26. unsigned mem_unit;
  27. #if ENABLE_DESKTOP
  28. unsigned unit_steps;
  29. # define G_unit_steps G.unit_steps
  30. #else
  31. # define G_unit_steps 10
  32. #endif
  33. } FIX_ALIASING;
  34. #define G (*(struct globals*)&bb_common_bufsiz1)
  35. #define INIT_G() do { } while (0)
  36. static unsigned long long scale(unsigned long d)
  37. {
  38. return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
  39. }
  40. int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  41. int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
  42. {
  43. struct sysinfo info;
  44. INIT_G();
  45. #if ENABLE_DESKTOP
  46. G.unit_steps = 10;
  47. if (argv[1] && argv[1][0] == '-') {
  48. switch (argv[1][1]) {
  49. case 'b':
  50. G.unit_steps = 0;
  51. break;
  52. case 'k': /* 2^10 */
  53. /* G.unit_steps = 10; - already is */
  54. break;
  55. case 'm': /* 2^(2*10) */
  56. G.unit_steps = 20;
  57. break;
  58. case 'g': /* 2^(3*10) */
  59. G.unit_steps = 30;
  60. break;
  61. default:
  62. bb_show_usage();
  63. }
  64. }
  65. #endif
  66. sysinfo(&info);
  67. /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
  68. G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
  69. printf(" %13s%13s%13s%13s%13s\n",
  70. "total",
  71. "used",
  72. "free",
  73. "shared", "buffers" /* swap and total don't have these columns */
  74. /* procps version 3.2.8 also shows "cached" column, but
  75. * sysinfo() does not provide this value, need to parse
  76. * /proc/meminfo instead and get "Cached: NNN kB" from there.
  77. */
  78. );
  79. #define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
  80. #define FIELDS_3 (FIELDS_5 + 2*6)
  81. #define FIELDS_2 (FIELDS_5 + 3*6)
  82. printf("Mem: ");
  83. printf(FIELDS_5,
  84. scale(info.totalram),
  85. scale(info.totalram - info.freeram),
  86. scale(info.freeram),
  87. scale(info.sharedram),
  88. scale(info.bufferram)
  89. );
  90. /* Show alternate, more meaningful busy/free numbers by counting
  91. * buffer cache as free memory (make it "-/+ buffers/cache"
  92. * if/when we add support for "cached" column): */
  93. printf("-/+ buffers: ");
  94. printf(FIELDS_2,
  95. scale(info.totalram - info.freeram - info.bufferram),
  96. scale(info.freeram + info.bufferram)
  97. );
  98. #if BB_MMU
  99. printf("Swap:");
  100. printf(FIELDS_3,
  101. scale(info.totalswap),
  102. scale(info.totalswap - info.freeswap),
  103. scale(info.freeswap)
  104. );
  105. #endif
  106. return EXIT_SUCCESS;
  107. }