HugePageMeter.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. htop - HugePageMeter.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 "linux/HugePageMeter.h"
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <stddef.h>
  12. #include "CRT.h"
  13. #include "Machine.h"
  14. #include "Macros.h"
  15. #include "Object.h"
  16. #include "RichString.h"
  17. #include "linux/LinuxMachine.h"
  18. static const char* HugePageMeter_active_labels[4] = { NULL, NULL, NULL, NULL };
  19. static const int HugePageMeter_attributes[] = {
  20. HUGEPAGE_1,
  21. HUGEPAGE_2,
  22. HUGEPAGE_3,
  23. HUGEPAGE_4,
  24. };
  25. static const char* const HugePageMeter_labels[] = {
  26. " 64K:", " 128K:", " 256K:", " 512K:",
  27. " 1M:", " 2M:", " 4M:", " 8M:", " 16M:", " 32M:", " 64M:", " 128M:", " 256M:", " 512M:",
  28. " 1G:", " 2G:", " 4G:", " 8G:", " 16G:", " 32G:", " 64G:", " 128G:", " 256G:", " 512G:",
  29. };
  30. static void HugePageMeter_updateValues(Meter* this) {
  31. assert(ARRAYSIZE(HugePageMeter_labels) == HTOP_HUGEPAGE_COUNT);
  32. char* buffer = this->txtBuffer;
  33. size_t size = sizeof(this->txtBuffer);
  34. int written;
  35. memory_t usedTotal = 0;
  36. unsigned nextUsed = 0;
  37. const LinuxMachine* host = (const LinuxMachine*) this->host;
  38. this->total = host->totalHugePageMem;
  39. this->values[0] = 0;
  40. HugePageMeter_active_labels[0] = " used:";
  41. for (unsigned i = 1; i < ARRAYSIZE(HugePageMeter_active_labels); i++) {
  42. this->values[i] = NAN;
  43. HugePageMeter_active_labels[i] = NULL;
  44. }
  45. for (unsigned i = 0; i < HTOP_HUGEPAGE_COUNT; i++) {
  46. memory_t value = host->usedHugePageMem[i];
  47. if (value != MEMORY_MAX) {
  48. this->values[nextUsed] = value;
  49. usedTotal += value;
  50. HugePageMeter_active_labels[nextUsed] = HugePageMeter_labels[i];
  51. if (++nextUsed == ARRAYSIZE(HugePageMeter_active_labels)) {
  52. break;
  53. }
  54. }
  55. }
  56. written = Meter_humanUnit(buffer, usedTotal, size);
  57. METER_BUFFER_CHECK(buffer, size, written);
  58. METER_BUFFER_APPEND_CHR(buffer, size, '/');
  59. Meter_humanUnit(buffer, this->total, size);
  60. }
  61. static void HugePageMeter_display(const Object* cast, RichString* out) {
  62. char buffer[50];
  63. const Meter* this = (const Meter*)cast;
  64. RichString_writeAscii(out, CRT_colors[METER_TEXT], ":");
  65. Meter_humanUnit(buffer, this->total, sizeof(buffer));
  66. RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
  67. for (unsigned i = 0; i < ARRAYSIZE(HugePageMeter_active_labels); i++) {
  68. if (!HugePageMeter_active_labels[i]) {
  69. break;
  70. }
  71. RichString_appendAscii(out, CRT_colors[METER_TEXT], HugePageMeter_active_labels[i]);
  72. Meter_humanUnit(buffer, this->values[i], sizeof(buffer));
  73. RichString_appendAscii(out, CRT_colors[HUGEPAGE_1 + i], buffer);
  74. }
  75. }
  76. const MeterClass HugePageMeter_class = {
  77. .super = {
  78. .extends = Class(Meter),
  79. .delete = Meter_delete,
  80. .display = HugePageMeter_display,
  81. },
  82. .updateValues = HugePageMeter_updateValues,
  83. .defaultMode = BAR_METERMODE,
  84. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  85. .maxItems = ARRAYSIZE(HugePageMeter_active_labels),
  86. .total = 100.0,
  87. .attributes = HugePageMeter_attributes,
  88. .name = "HugePages",
  89. .uiName = "HugePages",
  90. .caption = "HP"
  91. };