MemoryMeter.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. htop - MemoryMeter.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "MemoryMeter.h"
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <stddef.h>
  12. #include "CRT.h"
  13. #include "Macros.h"
  14. #include "Object.h"
  15. #include "Platform.h"
  16. #include "RichString.h"
  17. static const int MemoryMeter_attributes[] = {
  18. MEMORY_USED,
  19. MEMORY_SHARED,
  20. MEMORY_COMPRESSED,
  21. MEMORY_BUFFERS,
  22. MEMORY_CACHE
  23. };
  24. static void MemoryMeter_updateValues(Meter* this) {
  25. char* buffer = this->txtBuffer;
  26. size_t size = sizeof(this->txtBuffer);
  27. int written;
  28. /* shared, compressed and available memory are not supported on all platforms */
  29. this->values[MEMORY_METER_SHARED] = NAN;
  30. this->values[MEMORY_METER_COMPRESSED] = NAN;
  31. this->values[MEMORY_METER_AVAILABLE] = NAN;
  32. Platform_setMemoryValues(this);
  33. /* Do not print available memory in bar mode */
  34. static_assert(MEMORY_METER_AVAILABLE + 1 == MEMORY_METER_ITEMCOUNT,
  35. "MEMORY_METER_AVAILABLE is not the last item in MemoryMeterValues");
  36. this->curItems = MEMORY_METER_AVAILABLE;
  37. /* we actually want to show "used + shared + compressed" */
  38. double used = this->values[MEMORY_METER_USED];
  39. if (isPositive(this->values[MEMORY_METER_SHARED]))
  40. used += this->values[MEMORY_METER_SHARED];
  41. if (isPositive(this->values[MEMORY_METER_COMPRESSED]))
  42. used += this->values[MEMORY_METER_COMPRESSED];
  43. written = Meter_humanUnit(buffer, used, size);
  44. METER_BUFFER_CHECK(buffer, size, written);
  45. METER_BUFFER_APPEND_CHR(buffer, size, '/');
  46. Meter_humanUnit(buffer, this->total, size);
  47. }
  48. static void MemoryMeter_display(const Object* cast, RichString* out) {
  49. char buffer[50];
  50. const Meter* this = (const Meter*)cast;
  51. RichString_writeAscii(out, CRT_colors[METER_TEXT], ":");
  52. Meter_humanUnit(buffer, this->total, sizeof(buffer));
  53. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  54. Meter_humanUnit(buffer, this->values[MEMORY_METER_USED], sizeof(buffer));
  55. RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:");
  56. RichString_appendAscii(out, CRT_colors[MEMORY_USED], buffer);
  57. /* shared memory is not supported on all platforms */
  58. if (isNonnegative(this->values[MEMORY_METER_SHARED])) {
  59. Meter_humanUnit(buffer, this->values[MEMORY_METER_SHARED], sizeof(buffer));
  60. RichString_appendAscii(out, CRT_colors[METER_TEXT], " shared:");
  61. RichString_appendAscii(out, CRT_colors[MEMORY_SHARED], buffer);
  62. }
  63. /* compressed memory is not supported on all platforms */
  64. if (isNonnegative(this->values[MEMORY_METER_COMPRESSED])) {
  65. Meter_humanUnit(buffer, this->values[MEMORY_METER_COMPRESSED], sizeof(buffer));
  66. RichString_appendAscii(out, CRT_colors[METER_TEXT], " compressed:");
  67. RichString_appendAscii(out, CRT_colors[MEMORY_COMPRESSED], buffer);
  68. }
  69. Meter_humanUnit(buffer, this->values[MEMORY_METER_BUFFERS], sizeof(buffer));
  70. RichString_appendAscii(out, CRT_colors[METER_TEXT], " buffers:");
  71. RichString_appendAscii(out, CRT_colors[MEMORY_BUFFERS_TEXT], buffer);
  72. Meter_humanUnit(buffer, this->values[MEMORY_METER_CACHE], sizeof(buffer));
  73. RichString_appendAscii(out, CRT_colors[METER_TEXT], " cache:");
  74. RichString_appendAscii(out, CRT_colors[MEMORY_CACHE], buffer);
  75. /* available memory is not supported on all platforms */
  76. if (isNonnegative(this->values[MEMORY_METER_AVAILABLE])) {
  77. Meter_humanUnit(buffer, this->values[MEMORY_METER_AVAILABLE], sizeof(buffer));
  78. RichString_appendAscii(out, CRT_colors[METER_TEXT], " available:");
  79. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  80. }
  81. }
  82. const MeterClass MemoryMeter_class = {
  83. .super = {
  84. .extends = Class(Meter),
  85. .delete = Meter_delete,
  86. .display = MemoryMeter_display,
  87. },
  88. .updateValues = MemoryMeter_updateValues,
  89. .defaultMode = BAR_METERMODE,
  90. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  91. .maxItems = MEMORY_METER_ITEMCOUNT,
  92. .total = 100.0,
  93. .attributes = MemoryMeter_attributes,
  94. .name = "Memory",
  95. .uiName = "Memory",
  96. .caption = "Mem"
  97. };