ZramMeter.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. htop - linux/ZramMeter.c
  3. (C) 2020 Murloc Knight
  4. (C) 2020-2023 htop dev team
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "linux/ZramMeter.h"
  10. #include <stddef.h>
  11. #include "CRT.h"
  12. #include "Meter.h"
  13. #include "Object.h"
  14. #include "Platform.h"
  15. #include "RichString.h"
  16. #include "ZramMeter.h"
  17. static const int ZramMeter_attributes[ZRAM_METER_ITEMCOUNT] = {
  18. [ZRAM_METER_COMPRESSED] = ZRAM_COMPRESSED,
  19. [ZRAM_METER_UNCOMPRESSED] = ZRAM_UNCOMPRESSED,
  20. };
  21. static void ZramMeter_updateValues(Meter* this) {
  22. char* buffer = this->txtBuffer;
  23. size_t size = sizeof(this->txtBuffer);
  24. int written;
  25. Platform_setZramValues(this);
  26. written = Meter_humanUnit(buffer, this->values[ZRAM_METER_COMPRESSED], size);
  27. METER_BUFFER_CHECK(buffer, size, written);
  28. METER_BUFFER_APPEND_CHR(buffer, size, '(');
  29. double uncompressed = this->values[ZRAM_METER_COMPRESSED] + this->values[ZRAM_METER_UNCOMPRESSED];
  30. written = Meter_humanUnit(buffer, uncompressed, size);
  31. METER_BUFFER_CHECK(buffer, size, written);
  32. METER_BUFFER_APPEND_CHR(buffer, size, ')');
  33. METER_BUFFER_APPEND_CHR(buffer, size, '/');
  34. Meter_humanUnit(buffer, this->total, size);
  35. }
  36. static void ZramMeter_display(const Object* cast, RichString* out) {
  37. char buffer[50];
  38. const Meter* this = (const Meter*)cast;
  39. RichString_writeAscii(out, CRT_colors[METER_TEXT], ":");
  40. Meter_humanUnit(buffer, this->total, sizeof(buffer));
  41. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  42. Meter_humanUnit(buffer, this->values[ZRAM_METER_COMPRESSED], sizeof(buffer));
  43. RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:");
  44. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  45. double uncompressed = this->values[ZRAM_METER_COMPRESSED] + this->values[ZRAM_METER_UNCOMPRESSED];
  46. Meter_humanUnit(buffer, uncompressed, sizeof(buffer));
  47. RichString_appendAscii(out, CRT_colors[METER_TEXT], " uncompressed:");
  48. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  49. }
  50. const MeterClass ZramMeter_class = {
  51. .super = {
  52. .extends = Class(Meter),
  53. .delete = Meter_delete,
  54. .display = ZramMeter_display,
  55. },
  56. .updateValues = ZramMeter_updateValues,
  57. .defaultMode = BAR_METERMODE,
  58. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  59. .maxItems = ZRAM_METER_ITEMCOUNT,
  60. .total = 100.0,
  61. .attributes = ZramMeter_attributes,
  62. .name = "Zram",
  63. .uiName = "Zram",
  64. .caption = "zrm"
  65. };