TasksMeter.c 2.54 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
Hisham Muhammad's avatar
Hisham Muhammad committed
2
htop - TasksMeter.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "TasksMeter.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
10
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11
12
#include "CRT.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
15
16
/*{
#include "Meter.h"
}*/

17
int TasksMeter_attributes[] = {
Hisham Muhammad's avatar
Hisham Muhammad committed
18
   CPU_KERNEL, PROCESS_THREAD, PROCESS, TASKS_RUNNING
19
};
20

21
static void TasksMeter_updateValues(Meter* this, char* buffer, int len) {
22
23
24
25
26
27
28
29
   ProcessList* pl = this->pl;
   this->values[0] = pl->kernelThreads;
   this->values[1] = pl->userlandThreads;
   this->values[2] = pl->totalTasks - pl->kernelThreads - pl->userlandThreads;
   this->values[3] = pl->runningTasks;
   if (pl->totalTasks > this->total) {
      this->total = pl->totalTasks;
   }
30
31
32
33
   if (this->pl->settings->hideKernelThreads) {
      this->values[0] = 0;
   }
   snprintf(buffer, len, "%d/%d", (int) this->values[3], (int) this->total);
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
}

36
static void TasksMeter_display(Object* cast, RichString* out) {
Hisham Muhammad's avatar
Hisham Muhammad committed
37
   Meter* this = (Meter*)cast;
Hisham Muhammad's avatar
Hisham Muhammad committed
38
   Settings* settings = this->pl->settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
39
   char buffer[20];
Hisham Muhammad's avatar
Hisham Muhammad committed
40
41
42
43
   
   int processes = (int) this->values[2];
   
   sprintf(buffer, "%d", processes);
44
   RichString_write(out, CRT_colors[METER_VALUE], buffer);
45
46
   int threadValueColor = CRT_colors[METER_VALUE];
   int threadCaptionColor = CRT_colors[METER_TEXT];
Hisham Muhammad's avatar
Hisham Muhammad committed
47
   if (settings->highlightThreads) {
48
49
50
      threadValueColor = CRT_colors[PROCESS_THREAD_BASENAME];
      threadCaptionColor = CRT_colors[PROCESS_THREAD];
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
51
   if (!settings->hideUserlandThreads) {
52
      RichString_append(out, CRT_colors[METER_TEXT], ", ");
Hisham Muhammad's avatar
Hisham Muhammad committed
53
      sprintf(buffer, "%d", (int)this->values[1]);
54
55
56
      RichString_append(out, threadValueColor, buffer);
      RichString_append(out, threadCaptionColor, " thr");
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
57
   if (!settings->hideKernelThreads) {
58
      RichString_append(out, CRT_colors[METER_TEXT], ", ");
Hisham Muhammad's avatar
Hisham Muhammad committed
59
      sprintf(buffer, "%d", (int)this->values[0]);
60
61
62
63
      RichString_append(out, threadValueColor, buffer);
      RichString_append(out, threadCaptionColor, " kthr");
   }
   RichString_append(out, CRT_colors[METER_TEXT], "; ");
Hisham Muhammad's avatar
Hisham Muhammad committed
64
   sprintf(buffer, "%d", (int)this->values[3]);
Hisham Muhammad's avatar
Hisham Muhammad committed
65
66
67
   RichString_append(out, CRT_colors[TASKS_RUNNING], buffer);
   RichString_append(out, CRT_colors[METER_TEXT], " running");
}
68

69
70
71
72
73
74
MeterClass TasksMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = TasksMeter_display,
   },
75
   .updateValues = TasksMeter_updateValues,
76
   .defaultMode = TEXT_METERMODE,
Hisham Muhammad's avatar
Hisham Muhammad committed
77
   .maxItems = 4,
78
   .total = 100.0,
79
80
81
82
83
   .attributes = TasksMeter_attributes, 
   .name = "Tasks",
   .uiName = "Task counter",
   .caption = "Tasks: "
};