CPUMeter.c 3.57 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-2006 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "CPUMeter.h"
#include "Meter.h"

#include "ProcessList.h"

#include <stdlib.h>
#include <curses.h>
#include <string.h>
#include <math.h>

#include "debug.h"
#include <assert.h>

21
22
/* private property */
static int CPUMeter_attributes[] = { CPU_NICE, CPU_NORMAL, CPU_KERNEL };
Hisham Muhammad's avatar
Hisham Muhammad committed
23

24
25
26
27
28
29
30
31
32
33
34
35
/* private */
MeterType CPUMeter = {
   .setValues = CPUMeter_setValues, 
   .display = CPUMeter_display,
   .mode = BAR_METERMODE,
   .items = 3,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "CPU",
   .uiName = "CPU",
   .caption = "CPU",
   .init = CPUMeter_init
Hisham Muhammad's avatar
Hisham Muhammad committed
36
37
};

38
39
40
41
42
43
44
45
46
47
48
/* private */
MeterType AllCPUsMeter = {
   .mode = 0,
   .items = 1,
   .total = 100.0,
   .attributes = CPUMeter_attributes, 
   .name = "AllCPUs",
   .uiName = "All CPUs",
   .caption = "CPU",
   .draw = AllCPUsMeter_draw,
   .init = AllCPUsMeter_init,
49
   .setMode = AllCPUsMeter_setMode,
50
51
   .done = AllCPUsMeter_done
};
Hisham Muhammad's avatar
Hisham Muhammad committed
52
53
54
55
56
57
58
59

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

60
61
62
63
void CPUMeter_init(Meter* this) {
   int processor = this->param;
   if (this->pl->processorCount > 1) {
      char caption[10];
Hisham Muhammad's avatar
Hisham Muhammad committed
64
      sprintf(caption, "%-3d", processor);
65
      Meter_setCaption(this, caption);
Hisham Muhammad's avatar
Hisham Muhammad committed
66
   }
67
68
   if (this->param == 0)
      Meter_setCaption(this, "Avg");
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
}

71
72
73
74
75
76
77
78
79
void CPUMeter_setValues(Meter* this, char* buffer, int size) {
   ProcessList* pl = this->pl;
   int processor = this->param;
   double total = (double) pl->totalPeriod[processor];
   this->values[0] = pl->nicePeriod[processor] / total * 100.0;
   this->values[1] = pl->userPeriod[processor] / total * 100.0;
   this->values[2] = pl->systemPeriod[processor] / total * 100.0;
   double cpu = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2])));
   snprintf(buffer, size, "%5.1f%%", cpu );
Hisham Muhammad's avatar
Hisham Muhammad committed
80
81
82
83
84
85
}

void CPUMeter_display(Object* cast, RichString* out) {
   char buffer[50];
   Meter* this = (Meter*)cast;
   RichString_prune(out);
86
   sprintf(buffer, "%5.1f%% ", this->values[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
87
88
   RichString_append(out, CRT_colors[METER_TEXT], ":");
   RichString_append(out, CRT_colors[CPU_NORMAL], buffer);
89
   sprintf(buffer, "%5.1f%% ", this->values[2]);
Hisham Muhammad's avatar
Hisham Muhammad committed
90
91
   RichString_append(out, CRT_colors[METER_TEXT], "sys:");
   RichString_append(out, CRT_colors[CPU_KERNEL], buffer);
92
   sprintf(buffer, "%5.1f%% ", this->values[0]);
Hisham Muhammad's avatar
Hisham Muhammad committed
93
94
95
   RichString_append(out, CRT_colors[METER_TEXT], "low:");
   RichString_append(out, CRT_colors[CPU_NICE], buffer);
}
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

void AllCPUsMeter_init(Meter* this) {
   int processors = this->pl->processorCount;
   this->drawBuffer = malloc(sizeof(Meter*) * processors);
   Meter** meters = (Meter**) this->drawBuffer;
   for (int i = 0; i < processors; i++)
      meters[i] = Meter_new(this->pl, i+1, &CPUMeter);
   this->h = processors;
   this->mode = BAR_METERMODE;
}

void AllCPUsMeter_done(Meter* this) {
   int processors = this->pl->processorCount;
   Meter** meters = (Meter**) this->drawBuffer;
   for (int i = 0; i < processors; i++)
      Meter_delete((Object*)meters[i]);
}

114
115
116
117
118
119
120
void AllCPUsMeter_setMode(Meter* this, int mode) {
   this->mode = mode;
   int processors = this->pl->processorCount;
   int h = Meter_modes[this->mode]->h;
   this->h = h * processors;
}

121
122
123
124
125
void AllCPUsMeter_draw(Meter* this, int x, int y, int w) {
   int processors = this->pl->processorCount;
   Meter** meters = (Meter**) this->drawBuffer;
   for (int i = 0; i < processors; i++) {
      Meter_setMode(meters[i], this->mode);
126
127
      meters[i]->draw(meters[i], x, y, w);
      y += meters[i]->h;
128
129
   }
}