SwapMeter.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. htop - SwapMeter.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 "SwapMeter.h"
  9. #include <math.h>
  10. #include <stddef.h>
  11. #include "CRT.h"
  12. #include "Macros.h"
  13. #include "Object.h"
  14. #include "Platform.h"
  15. #include "RichString.h"
  16. static const int SwapMeter_attributes[] = {
  17. SWAP,
  18. SWAP_CACHE,
  19. SWAP_FRONTSWAP,
  20. };
  21. static void SwapMeter_updateValues(Meter* this) {
  22. char* buffer = this->txtBuffer;
  23. size_t size = sizeof(this->txtBuffer);
  24. int written;
  25. this->values[SWAP_METER_CACHE] = NAN; /* 'cached' not present on all platforms */
  26. this->values[SWAP_METER_FRONTSWAP] = NAN; /* 'frontswap' not present on all platforms */
  27. Platform_setSwapValues(this);
  28. written = Meter_humanUnit(buffer, this->values[SWAP_METER_USED], size);
  29. METER_BUFFER_CHECK(buffer, size, written);
  30. METER_BUFFER_APPEND_CHR(buffer, size, '/');
  31. Meter_humanUnit(buffer, this->total, size);
  32. }
  33. static void SwapMeter_display(const Object* cast, RichString* out) {
  34. char buffer[50];
  35. const Meter* this = (const Meter*)cast;
  36. RichString_writeAscii(out, CRT_colors[METER_TEXT], ":");
  37. Meter_humanUnit(buffer, this->total, sizeof(buffer));
  38. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  39. Meter_humanUnit(buffer, this->values[SWAP_METER_USED], sizeof(buffer));
  40. RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:");
  41. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  42. if (isNonnegative(this->values[SWAP_METER_CACHE])) {
  43. Meter_humanUnit(buffer, this->values[SWAP_METER_CACHE], sizeof(buffer));
  44. RichString_appendAscii(out, CRT_colors[METER_TEXT], " cache:");
  45. RichString_appendAscii(out, CRT_colors[SWAP_CACHE], buffer);
  46. }
  47. if (isNonnegative(this->values[SWAP_METER_FRONTSWAP])) {
  48. Meter_humanUnit(buffer, this->values[SWAP_METER_FRONTSWAP], sizeof(buffer));
  49. RichString_appendAscii(out, CRT_colors[METER_TEXT], " frontswap:");
  50. RichString_appendAscii(out, CRT_colors[SWAP_FRONTSWAP], buffer);
  51. }
  52. }
  53. const MeterClass SwapMeter_class = {
  54. .super = {
  55. .extends = Class(Meter),
  56. .delete = Meter_delete,
  57. .display = SwapMeter_display,
  58. },
  59. .updateValues = SwapMeter_updateValues,
  60. .defaultMode = BAR_METERMODE,
  61. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  62. .maxItems = SWAP_METER_ITEMCOUNT,
  63. .total = 100.0,
  64. .attributes = SwapMeter_attributes,
  65. .name = "Swap",
  66. .uiName = "Swap",
  67. .caption = "Swp"
  68. };