MemoryMeter.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Settings *settings = this->host->settings;
  29. /* shared, compressed and available memory are not supported on all platforms */
  30. this->values[MEMORY_METER_SHARED] = NAN;
  31. this->values[MEMORY_METER_COMPRESSED] = NAN;
  32. this->values[MEMORY_METER_AVAILABLE] = NAN;
  33. Platform_setMemoryValues(this);
  34. if ((this->mode == GRAPH_METERMODE || this->mode == BAR_METERMODE) && !settings->showCachedMemory) {
  35. this->values[MEMORY_METER_BUFFERS] = 0;
  36. this->values[MEMORY_METER_CACHE] = 0;
  37. }
  38. /* Do not print available memory in bar mode */
  39. static_assert(MEMORY_METER_AVAILABLE + 1 == MEMORY_METER_ITEMCOUNT,
  40. "MEMORY_METER_AVAILABLE is not the last item in MemoryMeterValues");
  41. this->curItems = MEMORY_METER_AVAILABLE;
  42. /* we actually want to show "used + shared + compressed" */
  43. double used = this->values[MEMORY_METER_USED];
  44. if (isPositive(this->values[MEMORY_METER_SHARED]))
  45. used += this->values[MEMORY_METER_SHARED];
  46. if (isPositive(this->values[MEMORY_METER_COMPRESSED]))
  47. used += this->values[MEMORY_METER_COMPRESSED];
  48. written = Meter_humanUnit(buffer, used, size);
  49. METER_BUFFER_CHECK(buffer, size, written);
  50. METER_BUFFER_APPEND_CHR(buffer, size, '/');
  51. Meter_humanUnit(buffer, this->total, size);
  52. }
  53. static void MemoryMeter_display(const Object* cast, RichString* out) {
  54. char buffer[50];
  55. const Meter* this = (const Meter*)cast;
  56. RichString_writeAscii(out, CRT_colors[METER_TEXT], ":");
  57. Meter_humanUnit(buffer, this->total, sizeof(buffer));
  58. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  59. Meter_humanUnit(buffer, this->values[MEMORY_METER_USED], sizeof(buffer));
  60. RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:");
  61. RichString_appendAscii(out, CRT_colors[MEMORY_USED], buffer);
  62. /* shared memory is not supported on all platforms */
  63. if (isNonnegative(this->values[MEMORY_METER_SHARED])) {
  64. Meter_humanUnit(buffer, this->values[MEMORY_METER_SHARED], sizeof(buffer));
  65. RichString_appendAscii(out, CRT_colors[METER_TEXT], " shared:");
  66. RichString_appendAscii(out, CRT_colors[MEMORY_SHARED], buffer);
  67. }
  68. /* compressed memory is not supported on all platforms */
  69. if (isNonnegative(this->values[MEMORY_METER_COMPRESSED])) {
  70. Meter_humanUnit(buffer, this->values[MEMORY_METER_COMPRESSED], sizeof(buffer));
  71. RichString_appendAscii(out, CRT_colors[METER_TEXT], " compressed:");
  72. RichString_appendAscii(out, CRT_colors[MEMORY_COMPRESSED], buffer);
  73. }
  74. Meter_humanUnit(buffer, this->values[MEMORY_METER_BUFFERS], sizeof(buffer));
  75. RichString_appendAscii(out, CRT_colors[METER_TEXT], " buffers:");
  76. RichString_appendAscii(out, CRT_colors[MEMORY_BUFFERS_TEXT], buffer);
  77. Meter_humanUnit(buffer, this->values[MEMORY_METER_CACHE], sizeof(buffer));
  78. RichString_appendAscii(out, CRT_colors[METER_TEXT], " cache:");
  79. RichString_appendAscii(out, CRT_colors[MEMORY_CACHE], buffer);
  80. /* available memory is not supported on all platforms */
  81. if (isNonnegative(this->values[MEMORY_METER_AVAILABLE])) {
  82. Meter_humanUnit(buffer, this->values[MEMORY_METER_AVAILABLE], sizeof(buffer));
  83. RichString_appendAscii(out, CRT_colors[METER_TEXT], " available:");
  84. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  85. }
  86. }
  87. const MeterClass MemoryMeter_class = {
  88. .super = {
  89. .extends = Class(Meter),
  90. .delete = Meter_delete,
  91. .display = MemoryMeter_display,
  92. },
  93. .updateValues = MemoryMeter_updateValues,
  94. .defaultMode = BAR_METERMODE,
  95. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  96. .maxItems = MEMORY_METER_ITEMCOUNT,
  97. .total = 100.0,
  98. .attributes = MemoryMeter_attributes,
  99. .name = "Memory",
  100. .uiName = "Memory",
  101. .caption = "Mem"
  102. };