MemorySwapMeter.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. htop - MemorySwapMeter.c
  3. (C) 2021 htop dev team
  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 "MemorySwapMeter.h"
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include "Macros.h"
  14. #include "MemoryMeter.h"
  15. #include "Object.h"
  16. #include "SwapMeter.h"
  17. #include "XUtils.h"
  18. typedef struct MemorySwapMeterData_ {
  19. Meter* memoryMeter;
  20. Meter* swapMeter;
  21. } MemorySwapMeterData;
  22. static void MemorySwapMeter_updateValues(Meter* this) {
  23. MemorySwapMeterData* data = this->meterData;
  24. Meter_updateValues(data->memoryMeter);
  25. Meter_updateValues(data->swapMeter);
  26. }
  27. static void MemorySwapMeter_draw(Meter* this, int x, int y, int w) {
  28. MemorySwapMeterData* data = this->meterData;
  29. /* Use the same width for each sub meter to align with CPU meter */
  30. const int colwidth = w / 2;
  31. const int diff = w - colwidth * 2;
  32. assert(data->memoryMeter->draw);
  33. data->memoryMeter->draw(data->memoryMeter, x, y, colwidth);
  34. assert(data->swapMeter->draw);
  35. data->swapMeter->draw(data->swapMeter, x + colwidth + diff, y, colwidth);
  36. }
  37. static void MemorySwapMeter_init(Meter* this) {
  38. MemorySwapMeterData* data = this->meterData;
  39. if (!data) {
  40. data = this->meterData = xCalloc(1, sizeof(MemorySwapMeterData));
  41. }
  42. if (!data->memoryMeter)
  43. data->memoryMeter = Meter_new(this->host, 0, (const MeterClass*) Class(MemoryMeter));
  44. if (!data->swapMeter)
  45. data->swapMeter = Meter_new(this->host, 0, (const MeterClass*) Class(SwapMeter));
  46. if (Meter_initFn(data->memoryMeter)) {
  47. Meter_init(data->memoryMeter);
  48. }
  49. if (Meter_initFn(data->swapMeter)) {
  50. Meter_init(data->swapMeter);
  51. }
  52. }
  53. static void MemorySwapMeter_updateMode(Meter* this, MeterModeId mode) {
  54. MemorySwapMeterData* data = this->meterData;
  55. this->mode = mode;
  56. Meter_setMode(data->memoryMeter, mode);
  57. Meter_setMode(data->swapMeter, mode);
  58. this->h = MAXIMUM(data->memoryMeter->h, data->swapMeter->h);
  59. }
  60. static void MemorySwapMeter_done(Meter* this) {
  61. MemorySwapMeterData* data = this->meterData;
  62. Meter_delete((Object*)data->swapMeter);
  63. Meter_delete((Object*)data->memoryMeter);
  64. free(data);
  65. }
  66. const MeterClass MemorySwapMeter_class = {
  67. .super = {
  68. .extends = Class(Meter),
  69. .delete = Meter_delete,
  70. },
  71. .updateValues = MemorySwapMeter_updateValues,
  72. .defaultMode = BAR_METERMODE,
  73. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  74. .isMultiColumn = true,
  75. .name = "MemorySwap",
  76. .uiName = "Memory & Swap",
  77. .description = "Combined memory and swap usage",
  78. .caption = "M&S",
  79. .draw = MemorySwapMeter_draw,
  80. .init = MemorySwapMeter_init,
  81. .updateMode = MemorySwapMeter_updateMode,
  82. .done = MemorySwapMeter_done
  83. };