aboutsummaryrefslogtreecommitdiffstats
path: root/CPUMeter.c
diff options
context:
space:
mode:
authorDiogo Ferreira <defer@cyngn.com>2015-01-14 11:13:52 +0000
committerDiogo Ferreira <defer@cyngn.com>2015-01-14 11:38:57 +0000
commit841159c272ac08b1cd5bdef5167a8ce11383fbb5 (patch)
treed7272ef150e58eaa1acc9ab62cf9e6c0aecbc2da /CPUMeter.c
parente1a8535012ab86d3e926bebfb7731dab1eb7320a (diff)
downloadandroid_external_htop-cm-14.0.tar.gz
android_external_htop-cm-14.0.tar.bz2
android_external_htop-cm-14.0.zip
Change-Id: I416c44803b3a79c2fd752e342ea113875fa533e0
Diffstat (limited to 'CPUMeter.c')
-rw-r--r--CPUMeter.c283
1 files changed, 223 insertions, 60 deletions
diff --git a/CPUMeter.c b/CPUMeter.c
index a1144ea..95b397f 100644
--- a/CPUMeter.c
+++ b/CPUMeter.c
@@ -1,25 +1,26 @@
/*
htop - CPUMeter.c
-(C) 2004-2010 Hisham H. Muhammad
+(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "CPUMeter.h"
-#include "Meter.h"
+#include "CRT.h"
#include "ProcessList.h"
+#include <assert.h>
#include <stdlib.h>
-#include <curses.h>
#include <string.h>
#include <math.h>
-#include "debug.h"
-#include <assert.h>
+/*{
+#include "Meter.h"
+}*/
int CPUMeter_attributes[] = {
- CPU_NICE, CPU_NORMAL, CPU_KERNEL, CPU_IRQ, CPU_SOFTIRQ, CPU_IOWAIT, CPU_STEAL, CPU_GUEST
+ CPU_NICE, CPU_NORMAL, CPU_KERNEL, CPU_IRQ, CPU_SOFTIRQ, CPU_STEAL, CPU_GUEST, CPU_IOWAIT
};
#ifndef MIN
@@ -33,7 +34,7 @@ static void CPUMeter_init(Meter* this) {
int cpu = this->param;
if (this->pl->cpuCount > 1) {
char caption[10];
- sprintf(caption, "%-3d", cpu);
+ sprintf(caption, "%-3d", ProcessList_cpuId(this->pl, cpu - 1));
Meter_setCaption(this, caption);
}
if (this->param == 0)
@@ -50,24 +51,29 @@ static void CPUMeter_setValues(Meter* this, char* buffer, int size) {
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;
+ double* v = this->values;
+ v[0] = cpuData->nicePeriod / total * 100.0;
+ v[1] = cpuData->userPeriod / total * 100.0;
if (pl->detailedCPUTime) {
- 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;
- this->type->items = 8;
- percent = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2]+
- this->values[3]+this->values[4])));
+ 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;
+ Meter_setItems(this, 8);
+ 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];
+ }
} else {
- this->values[2] = cpuData->systemAllPeriod / total * 100.0;
- this->values[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
- this->type->items = 4;
- percent = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2]+this->values[3])));
+ v[2] = cpuData->systemAllPeriod / total * 100.0;
+ v[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
+ Meter_setItems(this, 4);
+ percent = v[0]+v[1]+v[2]+v[3];
}
+ percent = MIN(100.0, MAX(0.0, percent));
if (isnan(percent)) percent = 0.0;
snprintf(buffer, size, "%5.1f%%", percent);
}
@@ -89,31 +95,33 @@ static void CPUMeter_display(Object* cast, RichString* out) {
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);
+ RichString_append(out, CRT_colors[CPU_NICE_TEXT], 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);
- sprintf(buffer, "%5.1f%% ", this->values[5]);
- RichString_append(out, CRT_colors[METER_TEXT], "wa:");
- RichString_append(out, CRT_colors[CPU_IOWAIT], buffer);
- 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]);
+ 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]);
RichString_append(out, CRT_colors[METER_TEXT], "gu:");
RichString_append(out, CRT_colors[CPU_GUEST], buffer);
}
+ sprintf(buffer, "%5.1f%% ", this->values[7]);
+ RichString_append(out, CRT_colors[METER_TEXT], "wa:");
+ RichString_append(out, CRT_colors[CPU_IOWAIT], buffer);
} 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);
+ RichString_append(out, CRT_colors[CPU_NICE_TEXT], buffer);
if (this->values[3]) {
sprintf(buffer, "%5.1f%% ", this->values[3]);
RichString_append(out, CRT_colors[METER_TEXT], "vir:");
@@ -122,45 +130,105 @@ static void CPUMeter_display(Object* cast, RichString* out) {
}
}
+static void AllCPUsMeter_getRange(Meter* this, int* start, int* count) {
+ int cpus = this->pl->cpuCount;
+ switch(Meter_name(this)[0]) {
+ 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;
+ }
+}
+
static void AllCPUsMeter_init(Meter* this) {
int cpus = this->pl->cpuCount;
- this->drawBuffer = malloc(sizeof(Meter*) * cpus);
- Meter** meters = (Meter**) this->drawBuffer;
- for (int i = 0; i < cpus; i++)
- meters[i] = Meter_new(this->pl, i+1, &CPUMeter);
- this->h = cpus;
- this->mode = BAR_METERMODE;
+ if (!this->drawData)
+ this->drawData = calloc(cpus, sizeof(Meter*));
+ Meter** meters = (Meter**) this->drawData;
+ int start, count;
+ AllCPUsMeter_getRange(this, &start, &count);
+ for (int i = 0; i < count; i++) {
+ if (!meters[i])
+ meters[i] = Meter_new(this->pl, start+i+1, (MeterClass*) Class(CPUMeter));
+ Meter_init(meters[i]);
+ }
+ if (this->mode == 0)
+ this->mode = BAR_METERMODE;
+ int h = Meter_modes[this->mode]->h;
+ if (strchr(Meter_name(this), '2'))
+ this->h = h * ((count+1) / 2);
+ else
+ this->h = h * count;
}
static void AllCPUsMeter_done(Meter* this) {
- int cpus = this->pl->cpuCount;
- Meter** meters = (Meter**) this->drawBuffer;
- for (int i = 0; i < cpus; i++)
+ Meter** meters = (Meter**) this->drawData;
+ int start, count;
+ AllCPUsMeter_getRange(this, &start, &count);
+ for (int i = 0; i < count; i++)
Meter_delete((Object*)meters[i]);
}
-static void AllCPUsMeter_setMode(Meter* this, int mode) {
+static void AllCPUsMeter_updateMode(Meter* this, int mode) {
+ Meter** meters = (Meter**) this->drawData;
this->mode = mode;
- int cpus = this->pl->cpuCount;
- int h = Meter_modes[this->mode]->h;
- this->h = h * cpus;
+ 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);
+ }
+ if (strchr(Meter_name(this), '2'))
+ this->h = h * ((count+1) / 2);
+ else
+ this->h = h * count;
}
-static void AllCPUsMeter_draw(Meter* this, int x, int y, int w) {
- int cpus = this->pl->cpuCount;
- Meter** meters = (Meter**) this->drawBuffer;
- for (int i = 0; i < cpus; i++) {
- Meter_setMode(meters[i], this->mode);
+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) {
+ Meter** meters = (Meter**) this->drawData;
+ int start, count;
+ AllCPUsMeter_getRange(this, &start, &count);
+ for (int i = 0; i < count; i++) {
meters[i]->draw(meters[i], x, y, w);
y += meters[i]->h;
}
}
-MeterType CPUMeter = {
+MeterClass CPUMeter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete,
+ .display = CPUMeter_display
+ },
.setValues = CPUMeter_setValues,
- .display = CPUMeter_display,
- .mode = BAR_METERMODE,
- .items = 8,
+ .defaultMode = BAR_METERMODE,
+ .maxItems = 8,
.total = 100.0,
.attributes = CPUMeter_attributes,
.name = "CPU",
@@ -169,16 +237,111 @@ MeterType CPUMeter = {
.init = CPUMeter_init
};
-MeterType AllCPUsMeter = {
- .mode = 0,
- .items = 1,
+MeterClass AllCPUsMeter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete,
+ .display = CPUMeter_display
+ },
+ .defaultMode = CUSTOM_METERMODE,
.total = 100.0,
.attributes = CPUMeter_attributes,
.name = "AllCPUs",
- .uiName = "All CPUs",
+ .uiName = "CPUs (1/1)",
+ .caption = "CPU",
+ .draw = SingleColCPUsMeter_draw,
+ .init = AllCPUsMeter_init,
+ .updateMode = AllCPUsMeter_updateMode,
+ .done = AllCPUsMeter_done
+};
+
+MeterClass AllCPUs2Meter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete,
+ .display = CPUMeter_display
+ },
+ .defaultMode = CUSTOM_METERMODE,
+ .total = 100.0,
+ .attributes = CPUMeter_attributes,
+ .name = "AllCPUs2",
+ .uiName = "CPUs (1&2/2)",
+ .caption = "CPU",
+ .draw = DualColCPUsMeter_draw,
+ .init = AllCPUsMeter_init,
+ .updateMode = AllCPUsMeter_updateMode,
+ .done = AllCPUsMeter_done
+};
+
+MeterClass LeftCPUsMeter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete,
+ .display = CPUMeter_display
+ },
+ .defaultMode = CUSTOM_METERMODE,
+ .total = 100.0,
+ .attributes = CPUMeter_attributes,
+ .name = "LeftCPUs",
+ .uiName = "CPUs (1/2)",
+ .caption = "CPU",
+ .draw = SingleColCPUsMeter_draw,
+ .init = AllCPUsMeter_init,
+ .updateMode = AllCPUsMeter_updateMode,
+ .done = AllCPUsMeter_done
+};
+
+MeterClass RightCPUsMeter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete,
+ .display = CPUMeter_display
+ },
+ .defaultMode = CUSTOM_METERMODE,
+ .total = 100.0,
+ .attributes = CPUMeter_attributes,
+ .name = "RightCPUs",
+ .uiName = "CPUs (2/2)",
.caption = "CPU",
- .draw = AllCPUsMeter_draw,
+ .draw = SingleColCPUsMeter_draw,
.init = AllCPUsMeter_init,
- .setMode = AllCPUsMeter_setMode,
+ .updateMode = AllCPUsMeter_updateMode,
.done = AllCPUsMeter_done
};
+
+MeterClass LeftCPUs2Meter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete,
+ .display = CPUMeter_display
+ },
+ .defaultMode = CUSTOM_METERMODE,
+ .total = 100.0,
+ .attributes = CPUMeter_attributes,
+ .name = "LeftCPUs2",
+ .uiName = "CPUs (1&2/4)",
+ .caption = "CPU",
+ .draw = DualColCPUsMeter_draw,
+ .init = AllCPUsMeter_init,
+ .updateMode = AllCPUsMeter_updateMode,
+ .done = AllCPUsMeter_done
+};
+
+MeterClass RightCPUs2Meter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete,
+ .display = CPUMeter_display
+ },
+ .defaultMode = CUSTOM_METERMODE,
+ .total = 100.0,
+ .attributes = CPUMeter_attributes,
+ .name = "RightCPUs2",
+ .uiName = "CPUs (3&4/4)",
+ .caption = "CPU",
+ .draw = DualColCPUsMeter_draw,
+ .init = AllCPUsMeter_init,
+ .updateMode = AllCPUsMeter_updateMode,
+ .done = AllCPUsMeter_done
+};
+