2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop - CPUMeter.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2006-03-04 18:16:49 +00:00
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CPUMeter.h"
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "CRT.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "ProcessList.h"
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <assert.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
/*{
|
|
|
|
#include "Meter.h"
|
|
|
|
}*/
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2006-06-06 20:41:01 +00:00
|
|
|
int CPUMeter_attributes[] = {
|
2010-08-24 23:20:38 +00:00
|
|
|
CPU_NICE, CPU_NORMAL, CPU_KERNEL, CPU_IRQ, CPU_SOFTIRQ, CPU_IOWAIT, CPU_STEAL, CPU_GUEST
|
2006-06-06 20:41:01 +00:00
|
|
|
};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void CPUMeter_init(Meter* this) {
|
2010-08-24 23:20:38 +00:00
|
|
|
int cpu = this->param;
|
|
|
|
if (this->pl->cpuCount > 1) {
|
2006-04-10 20:40:38 +00:00
|
|
|
char caption[10];
|
2011-09-08 02:48:53 +00:00
|
|
|
sprintf(caption, "%-3d", ProcessList_cpuId(this->pl, cpu - 1));
|
2006-04-10 20:40:38 +00:00
|
|
|
Meter_setCaption(this, caption);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-04-10 20:40:38 +00:00
|
|
|
if (this->param == 0)
|
|
|
|
Meter_setCaption(this, "Avg");
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void CPUMeter_setValues(Meter* this, char* buffer, int size) {
|
2006-04-10 20:40:38 +00:00
|
|
|
ProcessList* pl = this->pl;
|
2010-08-24 23:20:38 +00:00
|
|
|
int cpu = this->param;
|
|
|
|
if (cpu > this->pl->cpuCount) {
|
2009-06-02 04:51:23 +00:00
|
|
|
snprintf(buffer, size, "absent");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 23:20:38 +00:00
|
|
|
CPUData* cpuData = &(pl->cpus[cpu]);
|
|
|
|
double total = (double) ( cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod);
|
|
|
|
double percent;
|
|
|
|
this->values[0] = cpuData->nicePeriod / total * 100.0;
|
|
|
|
this->values[1] = cpuData->userPeriod / total * 100.0;
|
2007-11-09 00:40:59 +00:00
|
|
|
if (pl->detailedCPUTime) {
|
2010-08-24 23:20:38 +00:00
|
|
|
this->values[2] = cpuData->systemPeriod / total * 100.0;
|
|
|
|
this->values[3] = cpuData->irqPeriod / total * 100.0;
|
|
|
|
this->values[4] = cpuData->softIrqPeriod / total * 100.0;
|
|
|
|
this->values[5] = cpuData->ioWaitPeriod / total * 100.0;
|
|
|
|
this->values[6] = cpuData->stealPeriod / total * 100.0;
|
|
|
|
this->values[7] = cpuData->guestPeriod / total * 100.0;
|
2012-12-05 15:12:20 +00:00
|
|
|
Meter_setItems(this, 8);
|
2010-08-24 23:20:38 +00:00
|
|
|
percent = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2]+
|
2007-11-09 00:40:59 +00:00
|
|
|
this->values[3]+this->values[4])));
|
2006-10-04 14:21:27 +00:00
|
|
|
} else {
|
2010-08-24 23:20:38 +00:00
|
|
|
this->values[2] = cpuData->systemAllPeriod / total * 100.0;
|
|
|
|
this->values[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
|
2012-12-05 15:12:20 +00:00
|
|
|
Meter_setItems(this, 4);
|
2010-08-24 23:20:38 +00:00
|
|
|
percent = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2]+this->values[3])));
|
2006-10-04 14:21:27 +00:00
|
|
|
}
|
2010-08-24 23:20:38 +00:00
|
|
|
if (isnan(percent)) percent = 0.0;
|
|
|
|
snprintf(buffer, size, "%5.1f%%", percent);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void CPUMeter_display(Object* cast, RichString* out) {
|
2006-03-04 18:16:49 +00:00
|
|
|
char buffer[50];
|
|
|
|
Meter* this = (Meter*)cast;
|
2010-11-22 12:40:20 +00:00
|
|
|
RichString_prune(out);
|
2010-08-24 23:20:38 +00:00
|
|
|
if (this->param > this->pl->cpuCount) {
|
2009-06-02 04:51:23 +00:00
|
|
|
RichString_append(out, CRT_colors[METER_TEXT], "absent");
|
|
|
|
return;
|
|
|
|
}
|
2006-04-10 20:40:38 +00:00
|
|
|
sprintf(buffer, "%5.1f%% ", this->values[1]);
|
2006-03-04 18:16:49 +00:00
|
|
|
RichString_append(out, CRT_colors[METER_TEXT], ":");
|
|
|
|
RichString_append(out, CRT_colors[CPU_NORMAL], buffer);
|
2007-11-09 00:40:59 +00:00
|
|
|
if (this->pl->detailedCPUTime) {
|
2006-10-04 14:21:27 +00:00
|
|
|
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);
|
2007-11-09 00:40:59 +00:00
|
|
|
sprintf(buffer, "%5.1f%% ", this->values[5]);
|
|
|
|
RichString_append(out, CRT_colors[METER_TEXT], "wa:");
|
|
|
|
RichString_append(out, CRT_colors[CPU_IOWAIT], buffer);
|
2010-08-24 23:20:38 +00:00
|
|
|
sprintf(buffer, "%5.1f%% ", this->values[6]);
|
|
|
|
RichString_append(out, CRT_colors[METER_TEXT], "st:");
|
|
|
|
RichString_append(out, CRT_colors[CPU_STEAL], buffer);
|
|
|
|
if (this->values[7]) {
|
|
|
|
sprintf(buffer, "%5.1f%% ", this->values[7]);
|
|
|
|
RichString_append(out, CRT_colors[METER_TEXT], "gu:");
|
|
|
|
RichString_append(out, CRT_colors[CPU_GUEST], buffer);
|
|
|
|
}
|
2006-10-04 14:21:27 +00:00
|
|
|
} 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);
|
2010-08-24 23:20:38 +00:00
|
|
|
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);
|
|
|
|
}
|
2006-10-04 14:21:27 +00:00
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2011-11-18 06:08:56 +00:00
|
|
|
static void AllCPUsMeter_getRange(Meter* this, int* start, int* count) {
|
|
|
|
int cpus = this->pl->cpuCount;
|
2012-12-05 15:12:20 +00:00
|
|
|
switch(Meter_name(this)[0]) {
|
2011-11-18 06:08:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void AllCPUsMeter_init(Meter* this) {
|
2010-08-24 23:20:38 +00:00
|
|
|
int cpus = this->pl->cpuCount;
|
2011-03-31 20:24:59 +00:00
|
|
|
if (!this->drawData)
|
|
|
|
this->drawData = calloc(sizeof(Meter*), cpus);
|
2011-03-22 20:37:08 +00:00
|
|
|
Meter** meters = (Meter**) this->drawData;
|
2011-11-18 06:08:56 +00:00
|
|
|
int start, count;
|
|
|
|
AllCPUsMeter_getRange(this, &start, &count);
|
|
|
|
for (int i = 0; i < count; i++) {
|
2011-03-31 20:24:59 +00:00
|
|
|
if (!meters[i])
|
2012-12-05 15:12:20 +00:00
|
|
|
meters[i] = Meter_new(this->pl, start+i+1, (MeterClass*) Class(CPUMeter));
|
|
|
|
Meter_init(meters[i]);
|
2011-03-31 20:24:59 +00:00
|
|
|
}
|
2011-11-18 06:08:56 +00:00
|
|
|
if (this->mode == 0)
|
|
|
|
this->mode = BAR_METERMODE;
|
|
|
|
int h = Meter_modes[this->mode]->h;
|
2012-12-05 15:12:20 +00:00
|
|
|
if (strchr(Meter_name(this), '2'))
|
2011-11-18 06:08:56 +00:00
|
|
|
this->h = h * ((count+1) / 2);
|
|
|
|
else
|
|
|
|
this->h = h * count;
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void AllCPUsMeter_done(Meter* this) {
|
2011-03-22 20:37:08 +00:00
|
|
|
Meter** meters = (Meter**) this->drawData;
|
2011-11-18 06:08:56 +00:00
|
|
|
int start, count;
|
|
|
|
AllCPUsMeter_getRange(this, &start, &count);
|
|
|
|
for (int i = 0; i < count; i++)
|
2006-04-10 20:40:38 +00:00
|
|
|
Meter_delete((Object*)meters[i]);
|
|
|
|
}
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
static void AllCPUsMeter_updateMode(Meter* this, int mode) {
|
2011-11-18 06:08:56 +00:00
|
|
|
Meter** meters = (Meter**) this->drawData;
|
2006-05-09 18:35:51 +00:00
|
|
|
this->mode = mode;
|
2011-11-18 06:08:56 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-12-05 15:12:20 +00:00
|
|
|
if (strchr(Meter_name(this), '2'))
|
2011-11-18 06:08:56 +00:00
|
|
|
this->h = h * ((count+1) / 2);
|
|
|
|
else
|
|
|
|
this->h = h * count;
|
2006-05-09 18:35:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 06:08:56 +00:00
|
|
|
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) {
|
2011-03-22 20:37:08 +00:00
|
|
|
Meter** meters = (Meter**) this->drawData;
|
2011-11-18 06:08:56 +00:00
|
|
|
int start, count;
|
|
|
|
AllCPUsMeter_getRange(this, &start, &count);
|
|
|
|
for (int i = 0; i < count; i++) {
|
2006-05-09 18:35:51 +00:00
|
|
|
meters[i]->draw(meters[i], x, y, w);
|
|
|
|
y += meters[i]->h;
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-09 08:58:38 +00:00
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass CPUMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = CPUMeter_display
|
|
|
|
},
|
2008-03-09 08:58:38 +00:00
|
|
|
.setValues = CPUMeter_setValues,
|
2012-12-05 15:12:20 +00:00
|
|
|
.defaultMode = BAR_METERMODE,
|
2010-08-24 23:20:38 +00:00
|
|
|
.items = 8,
|
2008-03-09 08:58:38 +00:00
|
|
|
.total = 100.0,
|
|
|
|
.attributes = CPUMeter_attributes,
|
|
|
|
.name = "CPU",
|
|
|
|
.uiName = "CPU",
|
|
|
|
.caption = "CPU",
|
|
|
|
.init = CPUMeter_init
|
|
|
|
};
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass AllCPUsMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = CPUMeter_display
|
|
|
|
},
|
|
|
|
.defaultMode = CUSTOM_METERMODE,
|
2008-03-09 08:58:38 +00:00
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = CPUMeter_attributes,
|
|
|
|
.name = "AllCPUs",
|
2011-11-18 06:08:56 +00:00
|
|
|
.uiName = "CPUs (1/1)",
|
2008-03-09 08:58:38 +00:00
|
|
|
.caption = "CPU",
|
2011-11-18 06:08:56 +00:00
|
|
|
.draw = SingleColCPUsMeter_draw,
|
2008-03-09 08:58:38 +00:00
|
|
|
.init = AllCPUsMeter_init,
|
2012-12-05 15:12:20 +00:00
|
|
|
.updateMode = AllCPUsMeter_updateMode,
|
2008-03-09 08:58:38 +00:00
|
|
|
.done = AllCPUsMeter_done
|
|
|
|
};
|
2011-11-18 06:08:56 +00:00
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass AllCPUs2Meter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = CPUMeter_display
|
|
|
|
},
|
|
|
|
.defaultMode = CUSTOM_METERMODE,
|
2011-11-18 06:08:56 +00:00
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = CPUMeter_attributes,
|
|
|
|
.name = "AllCPUs2",
|
|
|
|
.uiName = "CPUs (1&2/2)",
|
|
|
|
.caption = "CPU",
|
|
|
|
.draw = DualColCPUsMeter_draw,
|
|
|
|
.init = AllCPUsMeter_init,
|
2012-12-05 15:12:20 +00:00
|
|
|
.updateMode = AllCPUsMeter_updateMode,
|
2011-11-18 06:08:56 +00:00
|
|
|
.done = AllCPUsMeter_done
|
|
|
|
};
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass LeftCPUsMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = CPUMeter_display
|
|
|
|
},
|
|
|
|
.defaultMode = CUSTOM_METERMODE,
|
2011-11-18 06:08:56 +00:00
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = CPUMeter_attributes,
|
|
|
|
.name = "LeftCPUs",
|
|
|
|
.uiName = "CPUs (1/2)",
|
|
|
|
.caption = "CPU",
|
|
|
|
.draw = SingleColCPUsMeter_draw,
|
|
|
|
.init = AllCPUsMeter_init,
|
2012-12-05 15:12:20 +00:00
|
|
|
.updateMode = AllCPUsMeter_updateMode,
|
2011-11-18 06:08:56 +00:00
|
|
|
.done = AllCPUsMeter_done
|
|
|
|
};
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass RightCPUsMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = CPUMeter_display
|
|
|
|
},
|
|
|
|
.defaultMode = CUSTOM_METERMODE,
|
2011-11-18 06:08:56 +00:00
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = CPUMeter_attributes,
|
|
|
|
.name = "RightCPUs",
|
|
|
|
.uiName = "CPUs (2/2)",
|
|
|
|
.caption = "CPU",
|
|
|
|
.draw = SingleColCPUsMeter_draw,
|
|
|
|
.init = AllCPUsMeter_init,
|
2012-12-05 15:12:20 +00:00
|
|
|
.updateMode = AllCPUsMeter_updateMode,
|
2011-11-18 06:08:56 +00:00
|
|
|
.done = AllCPUsMeter_done
|
|
|
|
};
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass LeftCPUs2Meter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = CPUMeter_display
|
|
|
|
},
|
|
|
|
.defaultMode = CUSTOM_METERMODE,
|
2011-11-18 06:08:56 +00:00
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = CPUMeter_attributes,
|
|
|
|
.name = "LeftCPUs2",
|
|
|
|
.uiName = "CPUs (1&2/4)",
|
|
|
|
.caption = "CPU",
|
|
|
|
.draw = DualColCPUsMeter_draw,
|
|
|
|
.init = AllCPUsMeter_init,
|
2012-12-05 15:12:20 +00:00
|
|
|
.updateMode = AllCPUsMeter_updateMode,
|
2011-11-18 06:08:56 +00:00
|
|
|
.done = AllCPUsMeter_done
|
|
|
|
};
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass RightCPUs2Meter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = CPUMeter_display
|
|
|
|
},
|
|
|
|
.defaultMode = CUSTOM_METERMODE,
|
2011-11-18 06:08:56 +00:00
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = CPUMeter_attributes,
|
|
|
|
.name = "RightCPUs2",
|
|
|
|
.uiName = "CPUs (3&4/4)",
|
|
|
|
.caption = "CPU",
|
|
|
|
.draw = DualColCPUsMeter_draw,
|
|
|
|
.init = AllCPUsMeter_init,
|
2012-12-05 15:12:20 +00:00
|
|
|
.updateMode = AllCPUsMeter_updateMode,
|
2011-11-18 06:08:56 +00:00
|
|
|
.done = AllCPUsMeter_done
|
|
|
|
};
|
|
|
|
|