free.c 2.4 KB

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