CPUMeter.c 10.1 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 "ProcessList.h"

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

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

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

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

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

44
static void CPUMeter_setValues(Meter* this, char* buffer, int size) {
45
   ProcessList* pl = this->pl;
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;
   }
51
52
53
   CPUData* cpuData = &(pl->cpus[cpu]);
   double total = (double) ( cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod);
   double percent;
54
55
56
   double* v = this->values;
   v[0] = cpuData->nicePeriod / total * 100.0;
   v[1] = cpuData->userPeriod / total * 100.0;
57
   if (pl->detailedCPUTime) {
58
59
60
61
62
63
      v[2] = cpuData->systemPeriod / total * 100.0;
      v[3] = cpuData->irqPeriod / total * 100.0;
      v[4] = cpuData->softIrqPeriod / total * 100.0;
      v[5] = cpuData->stealPeriod / total * 100.0;
      v[6] = cpuData->guestPeriod / total * 100.0;
      v[7] = cpuData->ioWaitPeriod / total * 100.0;
64
      Meter_setItems(this, 8);
65
66
67
68
69
      if (pl->accountGuestInCPUMeter) {
         percent = v[0]+v[1]+v[2]+v[3]+v[4]+v[5]+v[6];
      } else {
         percent = v[0]+v[1]+v[2]+v[3]+v[4];
      }       
70
   } else {
71
72
      v[2] = cpuData->systemAllPeriod / total * 100.0;
      v[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
73
      Meter_setItems(this, 4);
74
      percent = v[0]+v[1]+v[2]+v[3];
75
   }
76
   percent = MIN(100.0, MAX(0.0, percent));      
77
78
   if (isnan(percent)) percent = 0.0;
   snprintf(buffer, size, "%5.1f%%", percent);
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
}

81
static void CPUMeter_display(Object* cast, RichString* out) {
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
   char buffer[50];
   Meter* this = (Meter*)cast;
84
   RichString_prune(out);
85
   if (this->param > this->pl->cpuCount) {
Hisham Muhammad's avatar
Hisham Muhammad committed
86
87
88
      RichString_append(out, CRT_colors[METER_TEXT], "absent");
      return;
   }
89
   sprintf(buffer, "%5.1f%% ", this->values[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
90
91
   RichString_append(out, CRT_colors[METER_TEXT], ":");
   RichString_append(out, CRT_colors[CPU_NORMAL], buffer);
92
   if (this->pl->detailedCPUTime) {
93
94
95
96
97
98
99
100
101
102
103
104
      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:");
      RichString_append(out, CRT_colors[CPU_NICE], buffer);
      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);
105
106
107
108
109
110
111
      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]);
112
113
114
         RichString_append(out, CRT_colors[METER_TEXT], "gu:");
         RichString_append(out, CRT_colors[CPU_GUEST], buffer);
      }
115
116
117
      sprintf(buffer, "%5.1f%% ", this->values[7]);
      RichString_append(out, CRT_colors[METER_TEXT], "wa:");
      RichString_append(out, CRT_colors[CPU_IOWAIT], buffer);
118
119
120
121
122
123
124
   } 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:");
      RichString_append(out, CRT_colors[CPU_NICE], buffer);
125
126
127
128
129
      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);
      }
130
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
131
}
132

133
134
static void AllCPUsMeter_getRange(Meter* this, int* start, int* count) {
   int cpus = this->pl->cpuCount;
135
   switch(Meter_name(this)[0]) {
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
      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;
   }
}

152
static void AllCPUsMeter_init(Meter* this) {
153
   int cpus = this->pl->cpuCount;
154
155
   if (!this->drawData)
      this->drawData = calloc(sizeof(Meter*), cpus);
156
   Meter** meters = (Meter**) this->drawData;
157
158
159
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   for (int i = 0; i < count; i++) {
160
      if (!meters[i])
161
162
         meters[i] = Meter_new(this->pl, start+i+1, (MeterClass*) Class(CPUMeter));
      Meter_init(meters[i]);
163
   }
164
165
166
   if (this->mode == 0)
      this->mode = BAR_METERMODE;
   int h = Meter_modes[this->mode]->h;
167
   if (strchr(Meter_name(this), '2'))
168
169
170
      this->h = h * ((count+1) / 2);
   else
      this->h = h * count;
171
172
}

173
static void AllCPUsMeter_done(Meter* this) {
174
   Meter** meters = (Meter**) this->drawData;
175
176
177
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   for (int i = 0; i < count; i++)
178
179
180
      Meter_delete((Object*)meters[i]);
}

181
static void AllCPUsMeter_updateMode(Meter* this, int mode) {
182
   Meter** meters = (Meter**) this->drawData;
183
   this->mode = mode;
184
185
186
187
188
189
   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);
   }
190
   if (strchr(Meter_name(this), '2'))
191
192
193
      this->h = h * ((count+1) / 2);
   else
      this->h = h * count;
194
195
}

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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) {
214
   Meter** meters = (Meter**) this->drawData;
215
216
217
   int start, count;
   AllCPUsMeter_getRange(this, &start, &count);
   for (int i = 0; i < count; i++) {
218
219
      meters[i]->draw(meters[i], x, y, w);
      y += meters[i]->h;
220
221
   }
}
222

223
224
225
226
227
228
MeterClass CPUMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
229
   .setValues = CPUMeter_setValues, 
230
   .defaultMode = BAR_METERMODE,
231
   .items = 8,
232
233
234
235
236
237
238
239
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "CPU",
   .uiName = "CPU",
   .caption = "CPU",
   .init = CPUMeter_init
};

240
241
242
243
244
245
246
MeterClass AllCPUsMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
247
248
249
250
   .items = 1,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "AllCPUs",
251
   .uiName = "CPUs (1/1)",
252
   .caption = "CPU",
253
   .draw = SingleColCPUsMeter_draw,
254
   .init = AllCPUsMeter_init,
255
   .updateMode = AllCPUsMeter_updateMode,
256
257
   .done = AllCPUsMeter_done
};
258

259
260
261
262
263
264
265
MeterClass AllCPUs2Meter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
266
267
268
269
270
271
272
273
   .items = 1,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "AllCPUs2",
   .uiName = "CPUs (1&2/2)",
   .caption = "CPU",
   .draw = DualColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
274
   .updateMode = AllCPUsMeter_updateMode,
275
276
277
   .done = AllCPUsMeter_done
};

278
279
280
281
282
283
284
MeterClass LeftCPUsMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
285
286
287
288
289
290
291
292
   .items = 1,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "LeftCPUs",
   .uiName = "CPUs (1/2)",
   .caption = "CPU",
   .draw = SingleColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
293
   .updateMode = AllCPUsMeter_updateMode,
294
295
296
   .done = AllCPUsMeter_done
};

297
298
299
300
301
302
303
MeterClass RightCPUsMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
304
305
306
307
308
309
310
311
   .items = 1,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "RightCPUs",
   .uiName = "CPUs (2/2)",
   .caption = "CPU",
   .draw = SingleColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
312
   .updateMode = AllCPUsMeter_updateMode,
313
314
315
   .done = AllCPUsMeter_done
};

316
317
318
319
320
321
322
MeterClass LeftCPUs2Meter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
323
324
325
326
327
328
329
330
   .items = 1,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "LeftCPUs2",
   .uiName = "CPUs (1&2/4)",
   .caption = "CPU",
   .draw = DualColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
331
   .updateMode = AllCPUsMeter_updateMode,
332
333
334
   .done = AllCPUsMeter_done
};

335
336
337
338
339
340
341
MeterClass RightCPUs2Meter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = CPUMeter_display
   },
   .defaultMode = CUSTOM_METERMODE,
342
343
344
345
346
347
348
349
   .items = 1,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "RightCPUs2",
   .uiName = "CPUs (3&4/4)",
   .caption = "CPU",
   .draw = DualColCPUsMeter_draw,
   .init = AllCPUsMeter_init,
350
   .updateMode = AllCPUsMeter_updateMode,
351
352
353
   .done = AllCPUsMeter_done
};