LoadAverageMeter.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. htop - LoadAverageMeter.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 "LoadAverageMeter.h"
  9. #include "CRT.h"
  10. #include "Machine.h"
  11. #include "Object.h"
  12. #include "Platform.h"
  13. #include "RichString.h"
  14. #include "XUtils.h"
  15. static const int LoadAverageMeter_attributes[] = {
  16. LOAD_AVERAGE_ONE,
  17. LOAD_AVERAGE_FIVE,
  18. LOAD_AVERAGE_FIFTEEN
  19. };
  20. static const int LoadMeter_attributes[] = {
  21. LOAD
  22. };
  23. static const int OK_attributes[] = {
  24. METER_VALUE_OK
  25. };
  26. static const int Medium_attributes[] = {
  27. METER_VALUE_WARN
  28. };
  29. static const int High_attributes[] = {
  30. METER_VALUE_ERROR
  31. };
  32. static void LoadAverageMeter_updateValues(Meter* this) {
  33. Platform_getLoadAverage(&this->values[0], &this->values[1], &this->values[2]);
  34. // only show bar for 1min value
  35. this->curItems = 1;
  36. // change bar color and total based on value
  37. if (this->values[0] < 1.0) {
  38. this->curAttributes = OK_attributes;
  39. this->total = 1.0;
  40. } else if (this->values[0] < this->host->activeCPUs) {
  41. this->curAttributes = Medium_attributes;
  42. this->total = this->host->activeCPUs;
  43. } else {
  44. this->curAttributes = High_attributes;
  45. this->total = 2 * this->host->activeCPUs;
  46. }
  47. xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]);
  48. }
  49. static void LoadAverageMeter_display(const Object* cast, RichString* out) {
  50. const Meter* this = (const Meter*)cast;
  51. char buffer[20];
  52. int len;
  53. len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]);
  54. RichString_appendnAscii(out, CRT_colors[LOAD_AVERAGE_ONE], buffer, len);
  55. len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[1]);
  56. RichString_appendnAscii(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer, len);
  57. len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[2]);
  58. RichString_appendnAscii(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer, len);
  59. }
  60. static void LoadMeter_updateValues(Meter* this) {
  61. double five, fifteen;
  62. Platform_getLoadAverage(&this->values[0], &five, &fifteen);
  63. // change bar color and total based on value
  64. if (this->values[0] < 1.0) {
  65. this->curAttributes = OK_attributes;
  66. this->total = 1.0;
  67. } else if (this->values[0] < this->host->activeCPUs) {
  68. this->curAttributes = Medium_attributes;
  69. this->total = this->host->activeCPUs;
  70. } else {
  71. this->curAttributes = High_attributes;
  72. this->total = 2 * this->host->activeCPUs;
  73. }
  74. xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.2f", this->values[0]);
  75. }
  76. static void LoadMeter_display(const Object* cast, RichString* out) {
  77. const Meter* this = (const Meter*)cast;
  78. char buffer[20];
  79. int len;
  80. len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]);
  81. RichString_appendnAscii(out, CRT_colors[LOAD], buffer, len);
  82. }
  83. const MeterClass LoadAverageMeter_class = {
  84. .super = {
  85. .extends = Class(Meter),
  86. .delete = Meter_delete,
  87. .display = LoadAverageMeter_display,
  88. },
  89. .updateValues = LoadAverageMeter_updateValues,
  90. .defaultMode = TEXT_METERMODE,
  91. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  92. .maxItems = 3,
  93. .total = 100.0,
  94. .attributes = LoadAverageMeter_attributes,
  95. .name = "LoadAverage",
  96. .uiName = "Load average",
  97. .description = "Load averages: 1 minute, 5 minutes, 15 minutes",
  98. .caption = "Load average: "
  99. };
  100. const MeterClass LoadMeter_class = {
  101. .super = {
  102. .extends = Class(Meter),
  103. .delete = Meter_delete,
  104. .display = LoadMeter_display,
  105. },
  106. .updateValues = LoadMeter_updateValues,
  107. .defaultMode = TEXT_METERMODE,
  108. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  109. .maxItems = 1,
  110. .total = 100.0,
  111. .attributes = LoadMeter_attributes,
  112. .name = "Load",
  113. .uiName = "Load",
  114. .description = "Load: average of ready processes in the last minute",
  115. .caption = "Load: "
  116. };