TasksMeter.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. htop - TasksMeter.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 "TasksMeter.h"
  9. #include "CRT.h"
  10. #include "Machine.h"
  11. #include "Macros.h"
  12. #include "Object.h"
  13. #include "ProcessTable.h"
  14. #include "RichString.h"
  15. #include "Settings.h"
  16. #include "XUtils.h"
  17. static const int TasksMeter_attributes[] = {
  18. CPU_SYSTEM,
  19. PROCESS_THREAD,
  20. PROCESS,
  21. TASKS_RUNNING
  22. };
  23. static void TasksMeter_updateValues(Meter* this) {
  24. const Machine* host = this->host;
  25. const ProcessTable* pt = (const ProcessTable*) host->processTable;
  26. this->values[0] = pt->kernelThreads;
  27. this->values[1] = pt->userlandThreads;
  28. this->values[2] = pt->totalTasks - pt->kernelThreads - pt->userlandThreads;
  29. this->values[3] = MINIMUM(pt->runningTasks, host->activeCPUs);
  30. this->total = pt->totalTasks;
  31. xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%u/%u", MINIMUM(pt->runningTasks, host->activeCPUs), pt->totalTasks);
  32. }
  33. static void TasksMeter_display(const Object* cast, RichString* out) {
  34. const Meter* this = (const Meter*)cast;
  35. const Settings* settings = this->host->settings;
  36. char buffer[20];
  37. int len;
  38. len = xSnprintf(buffer, sizeof(buffer), "%d", (int)this->values[2]);
  39. RichString_appendnAscii(out, CRT_colors[METER_VALUE], buffer, len);
  40. RichString_appendAscii(out, settings->hideUserlandThreads ? CRT_colors[METER_SHADOW] : CRT_colors[METER_TEXT], ", ");
  41. len = xSnprintf(buffer, sizeof(buffer), "%d", (int)this->values[1]);
  42. RichString_appendnAscii(out, settings->hideUserlandThreads ? CRT_colors[METER_SHADOW] : CRT_colors[TASKS_RUNNING], buffer, len);
  43. RichString_appendAscii(out, settings->hideUserlandThreads ? CRT_colors[METER_SHADOW] : CRT_colors[METER_TEXT], " thr");
  44. RichString_appendAscii(out, settings->hideKernelThreads ? CRT_colors[METER_SHADOW] : CRT_colors[METER_TEXT], ", ");
  45. len = xSnprintf(buffer, sizeof(buffer), "%d", (int)this->values[0]);
  46. RichString_appendnAscii(out, settings->hideKernelThreads ? CRT_colors[METER_SHADOW] : CRT_colors[TASKS_RUNNING], buffer, len);
  47. RichString_appendAscii(out, settings->hideKernelThreads ? CRT_colors[METER_SHADOW] : CRT_colors[METER_TEXT], " kthr");
  48. RichString_appendAscii(out, CRT_colors[METER_TEXT], "; ");
  49. len = xSnprintf(buffer, sizeof(buffer), "%d", (int)this->values[3]);
  50. RichString_appendnAscii(out, CRT_colors[TASKS_RUNNING], buffer, len);
  51. RichString_appendAscii(out, CRT_colors[METER_TEXT], " running");
  52. }
  53. const MeterClass TasksMeter_class = {
  54. .super = {
  55. .extends = Class(Meter),
  56. .delete = Meter_delete,
  57. .display = TasksMeter_display,
  58. },
  59. .updateValues = TasksMeter_updateValues,
  60. .defaultMode = TEXT_METERMODE,
  61. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  62. .maxItems = 4,
  63. .total = 100.0,
  64. .attributes = TasksMeter_attributes,
  65. .name = "Tasks",
  66. .uiName = "Task counter",
  67. .caption = "Tasks: "
  68. };