CPUMeter.c 9.39 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - CPUMeter.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 "CPUMeter.h"

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

Hisham Muhammad's avatar
Hisham Muhammad committed
14
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
17
18
#include <stdlib.h>
#include <string.h>
#include <math.h>

Hisham Muhammad's avatar
Hisham Muhammad committed
19
20
21
/*{
#include "Meter.h"
}*/
Hisham Muhammad's avatar
Hisham Muhammad committed
22

23
int CPUMeter_attributes[] = {
24
   CPU_NICE, CPU_NORMAL, CPU_KERNEL, CPU_IRQ, CPU_SOFTIRQ, CPU_STEAL, CPU_GUEST, CPU_IOWAIT
25
};
Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
29
30
31
32
33

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif

34
static void CPUMeter_init(Meter* this) {
35
36
   int cpu = this->param;
   if (this->pl->cpuCount > 1) {
37
      char caption[10];
Hisham Muhammad's avatar
Hisham Muhammad committed
38
      sprintf(caption, "%-3d", Settings_cpuId(this->pl->settings, cpu - 1));
39
      Meter_setCaption(this, caption);
Hisham Muhammad's avatar
Hisham Muhammad committed
40
   }
41
42
   if (this->param == 0)
      Meter_setCaption(this, "Avg");
Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
}

45
static void CPUMeter_setValues(Meter* this, char* buffer, int size) {
46
47
   int cpu = this->param;
   if (cpu > this->pl->cpuCount) {
Hisham Muhammad's avatar
Hisham Muhammad committed
48
49
50
      snprintf(buffer, size, "absent");
      return;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
51
   double percent = Platform_setCPUValues(this, cpu);
52
   snprintf(buffer, size, "%5.1f%%", percent);
Hisham Muhammad's avatar
Hisham Muhammad committed
53
54
}

55
static void CPUMeter_display(Object* cast, RichString* out) {
Hisham Muhammad's avatar
Hisham Muhammad committed
56
57
   char buffer[50];
   Meter* this = (Meter*)cast;
58
   RichString_prune(out);
59
   if (this->param > this->pl->cpuCount) {
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
62
      RichString_append(out, CRT_colors[METER_TEXT], "absent");
      return;
   }
63
   sprintf(buffer, "%5.1f%% ", this->values[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
64
65
   RichString_append(out, CRT_colors[METER_TEXT], ":");
   RichString_append(out, CRT_colors[CPU_NORMAL], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
66
   if (this->pl->settings->detailedCPUTime) {
67
68
69
70
71
      sprintf(buffer, "%5.1f%% ", this->values[2]);
      RichString_append(out, CRT_colors[METER_TEXT], "sy:");
      RichString_append(out, CRT_colors[CPU_KERNEL], buffer);
      sprintf(buffer, "%5.1f%% ", this->values[0]);
      RichString_append(out, CRT_colors[METER_TEXT], "ni:");
72
      RichString_append(out, CRT_colors[CPU_NICE_TEXT], buffer);
73
74
75
76
77
78
      sprintf(buffer, "%5.1f%% ", this->values[3]);
      RichString_append(out, CRT_colors[METER_TEXT], "hi:");
      RichString_append(out, CRT_colors[CPU_IRQ], buffer);
      sprintf(buffer, "%5.1f%% ", this->values[4]);
      RichString_append(out, CRT_colors[METER_TEXT], "si:");
      RichString_append(out, CRT_colors[CPU_SOFTIRQ], buffer);
79
80
81
82
83
84
85
      if (this->values[5]) {
         sprintf(buffer, "%5.1f%% ", this->values[5]);
         RichString_append(out, CRT_colors[METER_TEXT], "st:");
         RichString_append(out, CRT_colors[CPU_STEAL], buffer);
      }
      if (this->values[6]) {
         sprintf(buffer, "%5.1f%% ", this->values[6]);
86
87
88
         RichString_append(out, CRT_colors[METER_TEXT], "gu:");
         RichString_append(out, CRT_colors[CPU_GUEST], buffer);
      }
89
90
91
      sprintf(buffer, "%5.1f%% ", this->values[7]);
      RichString_append(out, CRT_colors[METER_TEXT], "wa:");
      RichString_append(out, CRT_colors[CPU_IOWAIT], buffer);
92
93
94
95
96
97
   } else {
      sprintf(buffer, "%5.1f%% ", this->values[2]);
      RichString_append(out, CRT_colors[METER_TEXT], "sys:");
      RichString_append(out, CRT_colors[CPU_KERNEL], buffer);
      sprintf(buffer, "%5.1f%% ", this->values[0]);
      RichString_append(out, CRT_colors[METER_TEXT], "low:");
98
      RichString_append(out, CRT_colors[CPU_NICE_TEXT], buffer);
99
100
101
102
103
      if (this->values[3]) {
         sprintf(buffer, "%5.1f%% ", this->values[3]);
         RichString_append(out, CRT_colors[METER_TEXT], "vir:");
         RichString_append(out, CRT_colors[CPU_GUEST], buffer);
      }
104
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
105
}
106

107
108
static void AllCPUsMeter_getRange(Meter* this, int* start, int* count) {
   int cpus = this->pl->cpuCount;
109
   switch(Meter_name(this)[0]) {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
      default:
      case 'A': // All
         *start = 0;
         *count = cpus;
         break;
      case 'L': // First Half
         *start = 0;
         *count = (cpus+1) / 2;
         break;
      case 'R': // Second Half
         *start = (cpus+1) / 2;
         *count = cpus / 2;
         break;
   }
}

126
static void AllCPUsMeter_init(Meter* this) {
127
   int cpus = this->pl->cpuCount;
128
   if (!this->drawData)
129
      this->drawData = calloc(cpus, sizeof(Meter*));
130
   Meter** meters = (Meter**) this->drawData;
131
132
133
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   for (int i = 0; i < count; i++) {
134
      if (!meters[i])
135
136
         meters[i] = Meter_new(this->pl, start+i+1, (MeterClass*) Class(CPUMeter));
      Meter_init(meters[i]);
137
   }
138
139
140
   if (this->mode == 0)
      this->mode = BAR_METERMODE;
   int h = Meter_modes[this->mode]->h;
141
   if (strchr(Meter_name(this), '2'))
142
143
144
      this->h = h * ((count+1) / 2);
   else
      this->h = h * count;
145
146
}

147
static void AllCPUsMeter_done(Meter* this) {
148
   Meter** meters = (Meter**) this->drawData;
149
150
151
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   for (int i = 0; i < count; i++)
152
153
154
      Meter_delete((Object*)meters[i]);
}

155
static void AllCPUsMeter_updateMode(Meter* this, int mode) {
156
   Meter** meters = (Meter**) this->drawData;
157
   this->mode = mode;
158
159
160
161
162
163
   int h = Meter_modes[mode]->h;
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   for (int i = 0; i < count; i++) {
      Meter_setMode(meters[i], mode);
   }
164
   if (strchr(Meter_name(this), '2'))
165
166
167
      this->h = h * ((count+1) / 2);
   else
      this->h = h * count;
168
169
}

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
static void DualColCPUsMeter_draw(Meter* this, int x, int y, int w) {
   Meter** meters = (Meter**) this->drawData;
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   int height = (count+1)/2;
   int startY = y;
   for (int i = 0; i < height; i++) {
      meters[i]->draw(meters[i], x, y, (w-2)/2);
      y += meters[i]->h;
   }
   y = startY;
   for (int i = height; i < count; i++) {
      meters[i]->draw(meters[i], x+(w-1)/2+2, y, (w-2)/2);
      y += meters[i]->h;
   }
}

static void SingleColCPUsMeter_draw(Meter* this, int x, int y, int w) {
188
   Meter** meters = (Meter**) this->drawData;
189
190
191
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   for (int i = 0; i < count; i++) {
192
193
      meters[i]->draw(meters[i], x, y, w);
      y += meters[i]->h;
194
195
   }
}
196

197
198
199
200
201
202
MeterClass CPUMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
203
   .setValues = CPUMeter_setValues, 
204
   .defaultMode = BAR_METERMODE,
205
   .maxItems = 8,
206
207
208
209
210
211
212
213
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "CPU",
   .uiName = "CPU",
   .caption = "CPU",
   .init = CPUMeter_init
};

214
215
216
217
218
219
220
MeterClass AllCPUsMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
221
222
223
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "AllCPUs",
224
   .uiName = "CPUs (1/1)",
225
   .description = "CPUs (1/1): all CPUs",
226
   .caption = "CPU",
227
   .draw = SingleColCPUsMeter_draw,
228
   .init = AllCPUsMeter_init,
229
   .updateMode = AllCPUsMeter_updateMode,
230
231
   .done = AllCPUsMeter_done
};
232

233
234
235
236
237
238
239
MeterClass AllCPUs2Meter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
240
241
242
243
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "AllCPUs2",
   .uiName = "CPUs (1&2/2)",
244
   .description = "CPUs (1&2/2): all CPUs in 2 shorter columns",
245
246
247
   .caption = "CPU",
   .draw = DualColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
248
   .updateMode = AllCPUsMeter_updateMode,
249
250
251
   .done = AllCPUsMeter_done
};

252
253
254
255
256
257
258
MeterClass LeftCPUsMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
259
260
261
262
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "LeftCPUs",
   .uiName = "CPUs (1/2)",
263
   .description = "CPUs (1/2): first half of list",
264
265
266
   .caption = "CPU",
   .draw = SingleColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
267
   .updateMode = AllCPUsMeter_updateMode,
268
269
270
   .done = AllCPUsMeter_done
};

271
272
273
274
275
276
277
MeterClass RightCPUsMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
278
279
280
281
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "RightCPUs",
   .uiName = "CPUs (2/2)",
282
   .description = "CPUs (2/2): second half of list",
283
284
285
   .caption = "CPU",
   .draw = SingleColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
286
   .updateMode = AllCPUsMeter_updateMode,
287
288
289
   .done = AllCPUsMeter_done
};

290
291
292
293
294
295
296
MeterClass LeftCPUs2Meter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
297
298
299
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "LeftCPUs2",
300
   .description = "CPUs (1&2/4): first half in 2 shorter columns",
301
302
303
304
   .uiName = "CPUs (1&2/4)",
   .caption = "CPU",
   .draw = DualColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
305
   .updateMode = AllCPUsMeter_updateMode,
306
307
308
   .done = AllCPUsMeter_done
};

309
310
311
312
313
314
315
MeterClass RightCPUs2Meter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
316
317
318
319
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "RightCPUs2",
   .uiName = "CPUs (3&4/4)",
320
   .description = "CPUs (3&4/4): second half in 2 shorter columns",
321
322
323
   .caption = "CPU",
   .draw = DualColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
324
   .updateMode = AllCPUsMeter_updateMode,
325
326
327
   .done = AllCPUsMeter_done
};