add support for steal/guest CPU time measurement

simplify processor data accounting (add CPUData structure)
remove Process_clone trick
This commit is contained in:
Hisham Muhammad 2010-08-24 23:20:38 +00:00
parent c166e172a8
commit 6486229308
13 changed files with 270 additions and 241 deletions

View File

@ -91,10 +91,10 @@ AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Panel* leftMe
} }
} }
MeterType* type = &CPUMeter; MeterType* type = &CPUMeter;
int processors = settings->pl->processorCount; int cpus = settings->pl->cpuCount;
if (processors > 1) { if (cpus > 1) {
Panel_add(super, (Object*) ListItem_new("CPU average", 0)); Panel_add(super, (Object*) ListItem_new("CPU average", 0));
for (int i = 1; i <= processors; i++) { for (int i = 1; i <= cpus; i++) {
char buffer[50]; char buffer[50];
sprintf(buffer, "%s %d", type->uiName, i); sprintf(buffer, "%s %d", type->uiName, i);
Panel_add(super, (Object*) ListItem_new(buffer, i)); Panel_add(super, (Object*) ListItem_new(buffer, i));

View File

@ -19,7 +19,7 @@ in the source distribution for its full text.
#include <assert.h> #include <assert.h>
int CPUMeter_attributes[] = { int CPUMeter_attributes[] = {
CPU_NICE, CPU_NORMAL, CPU_KERNEL, CPU_IRQ, CPU_SOFTIRQ, CPU_IOWAIT CPU_NICE, CPU_NORMAL, CPU_KERNEL, CPU_IRQ, CPU_SOFTIRQ, CPU_IOWAIT, CPU_STEAL, CPU_GUEST
}; };
#ifndef MIN #ifndef MIN
@ -30,10 +30,10 @@ int CPUMeter_attributes[] = {
#endif #endif
static void CPUMeter_init(Meter* this) { static void CPUMeter_init(Meter* this) {
int processor = this->param; int cpu = this->param;
if (this->pl->processorCount > 1) { if (this->pl->cpuCount > 1) {
char caption[10]; char caption[10];
sprintf(caption, "%-3d", processor); sprintf(caption, "%-3d", cpu);
Meter_setCaption(this, caption); Meter_setCaption(this, caption);
} }
if (this->param == 0) if (this->param == 0)
@ -42,37 +42,41 @@ static void CPUMeter_init(Meter* this) {
static void CPUMeter_setValues(Meter* this, char* buffer, int size) { static void CPUMeter_setValues(Meter* this, char* buffer, int size) {
ProcessList* pl = this->pl; ProcessList* pl = this->pl;
int processor = this->param; int cpu = this->param;
if (processor > this->pl->processorCount) { if (cpu > this->pl->cpuCount) {
snprintf(buffer, size, "absent"); snprintf(buffer, size, "absent");
return; return;
} }
double total = (double) (pl->totalPeriod[processor] == 0 ? 1 : pl->totalPeriod[processor]); CPUData* cpuData = &(pl->cpus[cpu]);
double cpu; double total = (double) ( cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod);
this->values[0] = pl->nicePeriod[processor] / total * 100.0; double percent;
this->values[1] = pl->userPeriod[processor] / total * 100.0; this->values[0] = cpuData->nicePeriod / total * 100.0;
this->values[1] = cpuData->userPeriod / total * 100.0;
if (pl->detailedCPUTime) { if (pl->detailedCPUTime) {
this->values[2] = pl->systemPeriod[processor] / total * 100.0; this->values[2] = cpuData->systemPeriod / total * 100.0;
this->values[3] = pl->irqPeriod[processor] / total * 100.0; this->values[3] = cpuData->irqPeriod / total * 100.0;
this->values[4] = pl->softIrqPeriod[processor] / total * 100.0; this->values[4] = cpuData->softIrqPeriod / total * 100.0;
this->values[5] = pl->ioWaitPeriod[processor] / total * 100.0; this->values[5] = cpuData->ioWaitPeriod / total * 100.0;
this->type->items = 6; this->values[6] = cpuData->stealPeriod / total * 100.0;
cpu = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2]+ 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]))); this->values[3]+this->values[4])));
} else { } else {
this->values[2] = pl->systemAllPeriod[processor] / total * 100.0; this->values[2] = cpuData->systemAllPeriod / total * 100.0;
this->type->items = 3; this->values[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
cpu = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2]))); this->type->items = 4;
percent = MIN(100.0, MAX(0.0, (this->values[0]+this->values[1]+this->values[2]+this->values[3])));
} }
if (isnan(cpu)) cpu = 0.0; if (isnan(percent)) percent = 0.0;
snprintf(buffer, size, "%5.1f%%", cpu ); snprintf(buffer, size, "%5.1f%%", percent);
} }
static void CPUMeter_display(Object* cast, RichString* out) { static void CPUMeter_display(Object* cast, RichString* out) {
char buffer[50]; char buffer[50];
Meter* this = (Meter*)cast; Meter* this = (Meter*)cast;
RichString_init(out); RichString_init(out);
if (this->param > this->pl->processorCount) { if (this->param > this->pl->cpuCount) {
RichString_append(out, CRT_colors[METER_TEXT], "absent"); RichString_append(out, CRT_colors[METER_TEXT], "absent");
return; return;
} }
@ -95,6 +99,14 @@ static void CPUMeter_display(Object* cast, RichString* out) {
sprintf(buffer, "%5.1f%% ", this->values[5]); sprintf(buffer, "%5.1f%% ", this->values[5]);
RichString_append(out, CRT_colors[METER_TEXT], "wa:"); RichString_append(out, CRT_colors[METER_TEXT], "wa:");
RichString_append(out, CRT_colors[CPU_IOWAIT], buffer); 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]);
RichString_append(out, CRT_colors[METER_TEXT], "gu:");
RichString_append(out, CRT_colors[CPU_GUEST], buffer);
}
} else { } else {
sprintf(buffer, "%5.1f%% ", this->values[2]); sprintf(buffer, "%5.1f%% ", this->values[2]);
RichString_append(out, CRT_colors[METER_TEXT], "sys:"); RichString_append(out, CRT_colors[METER_TEXT], "sys:");
@ -102,37 +114,42 @@ static void CPUMeter_display(Object* cast, RichString* out) {
sprintf(buffer, "%5.1f%% ", this->values[0]); sprintf(buffer, "%5.1f%% ", this->values[0]);
RichString_append(out, CRT_colors[METER_TEXT], "low:"); RichString_append(out, CRT_colors[METER_TEXT], "low:");
RichString_append(out, CRT_colors[CPU_NICE], buffer); RichString_append(out, CRT_colors[CPU_NICE], buffer);
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);
}
} }
} }
static void AllCPUsMeter_init(Meter* this) { static void AllCPUsMeter_init(Meter* this) {
int processors = this->pl->processorCount; int cpus = this->pl->cpuCount;
this->drawBuffer = malloc(sizeof(Meter*) * processors); this->drawBuffer = malloc(sizeof(Meter*) * cpus);
Meter** meters = (Meter**) this->drawBuffer; Meter** meters = (Meter**) this->drawBuffer;
for (int i = 0; i < processors; i++) for (int i = 0; i < cpus; i++)
meters[i] = Meter_new(this->pl, i+1, &CPUMeter); meters[i] = Meter_new(this->pl, i+1, &CPUMeter);
this->h = processors; this->h = cpus;
this->mode = BAR_METERMODE; this->mode = BAR_METERMODE;
} }
static void AllCPUsMeter_done(Meter* this) { static void AllCPUsMeter_done(Meter* this) {
int processors = this->pl->processorCount; int cpus = this->pl->cpuCount;
Meter** meters = (Meter**) this->drawBuffer; Meter** meters = (Meter**) this->drawBuffer;
for (int i = 0; i < processors; i++) for (int i = 0; i < cpus; i++)
Meter_delete((Object*)meters[i]); Meter_delete((Object*)meters[i]);
} }
static void AllCPUsMeter_setMode(Meter* this, int mode) { static void AllCPUsMeter_setMode(Meter* this, int mode) {
this->mode = mode; this->mode = mode;
int processors = this->pl->processorCount; int cpus = this->pl->cpuCount;
int h = Meter_modes[this->mode]->h; int h = Meter_modes[this->mode]->h;
this->h = h * processors; this->h = h * cpus;
} }
static void AllCPUsMeter_draw(Meter* this, int x, int y, int w) { static void AllCPUsMeter_draw(Meter* this, int x, int y, int w) {
int processors = this->pl->processorCount; int cpus = this->pl->cpuCount;
Meter** meters = (Meter**) this->drawBuffer; Meter** meters = (Meter**) this->drawBuffer;
for (int i = 0; i < processors; i++) { for (int i = 0; i < cpus; i++) {
Meter_setMode(meters[i], this->mode); Meter_setMode(meters[i], this->mode);
meters[i]->draw(meters[i], x, y, w); meters[i]->draw(meters[i], x, y, w);
y += meters[i]->h; y += meters[i]->h;
@ -143,7 +160,7 @@ MeterType CPUMeter = {
.setValues = CPUMeter_setValues, .setValues = CPUMeter_setValues,
.display = CPUMeter_display, .display = CPUMeter_display,
.mode = BAR_METERMODE, .mode = BAR_METERMODE,
.items = 6, .items = 8,
.total = 100.0, .total = 100.0,
.attributes = CPUMeter_attributes, .attributes = CPUMeter_attributes,
.name = "CPU", .name = "CPU",

78
CRT.c
View File

@ -11,6 +11,7 @@ in the source distribution for its full text.
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <execinfo.h>
#include "String.h" #include "String.h"
@ -93,14 +94,16 @@ typedef enum ColorElements_ {
CHECK_MARK, CHECK_MARK,
CHECK_TEXT, CHECK_TEXT,
CLOCK, CLOCK,
HELP_BOLD,
HOSTNAME,
CPU_NICE, CPU_NICE,
CPU_NORMAL, CPU_NORMAL,
CPU_KERNEL, CPU_KERNEL,
HELP_BOLD,
CPU_IOWAIT, CPU_IOWAIT,
CPU_IRQ, CPU_IRQ,
CPU_SOFTIRQ, CPU_SOFTIRQ,
HOSTNAME, CPU_STEAL,
CPU_GUEST,
LAST_COLORELEMENT LAST_COLORELEMENT
} ColorElements; } ColorElements;
@ -116,11 +119,16 @@ int CRT_colors[LAST_COLORELEMENT] = { 0 };
char* CRT_termType; char* CRT_termType;
void *backtraceArray[128];
static void CRT_handleSIGSEGV(int sgn) { static void CRT_handleSIGSEGV(int sgn) {
(void) sgn; (void) sgn;
CRT_done(); CRT_done();
fprintf(stderr, "htop " VERSION " aborted. Please report bug at http://htop.sf.net\n"); fprintf(stderr, "\n\nhtop " VERSION " aborting. Please report bug at http://htop.sf.net\n");
exit(1); size_t size = backtrace(backtraceArray, sizeof(backtraceArray));
fprintf(stderr, "Backtrace: \n");
backtrace_symbols_fd(backtraceArray, size, 2);
abort();
} }
static void CRT_handleSIGTERM(int sgn) { static void CRT_handleSIGTERM(int sgn) {
@ -257,17 +265,19 @@ void CRT_setColors(int colorScheme) {
CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD; CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD;
CRT_colors[LOAD] = A_BOLD; CRT_colors[LOAD] = A_BOLD;
CRT_colors[HELP_BOLD] = A_BOLD; CRT_colors[HELP_BOLD] = A_BOLD;
CRT_colors[CPU_NICE] = A_NORMAL;
CRT_colors[CPU_NORMAL] = A_BOLD;
CRT_colors[CPU_KERNEL] = A_BOLD;
CRT_colors[CLOCK] = A_BOLD; CRT_colors[CLOCK] = A_BOLD;
CRT_colors[CHECK_BOX] = A_BOLD; CRT_colors[CHECK_BOX] = A_BOLD;
CRT_colors[CHECK_MARK] = A_NORMAL; CRT_colors[CHECK_MARK] = A_NORMAL;
CRT_colors[CHECK_TEXT] = A_NORMAL; CRT_colors[CHECK_TEXT] = A_NORMAL;
CRT_colors[HOSTNAME] = A_BOLD;
CRT_colors[CPU_NICE] = A_NORMAL;
CRT_colors[CPU_NORMAL] = A_BOLD;
CRT_colors[CPU_KERNEL] = A_BOLD;
CRT_colors[CPU_IOWAIT] = A_NORMAL; CRT_colors[CPU_IOWAIT] = A_NORMAL;
CRT_colors[CPU_IRQ] = A_BOLD; CRT_colors[CPU_IRQ] = A_BOLD;
CRT_colors[CPU_SOFTIRQ] = A_BOLD; CRT_colors[CPU_SOFTIRQ] = A_BOLD;
CRT_colors[HOSTNAME] = A_BOLD; CRT_colors[CPU_STEAL] = A_REVERSE;
CRT_colors[CPU_GUEST] = A_REVERSE;
} else if (CRT_colorScheme == COLORSCHEME_BLACKONWHITE) { } else if (CRT_colorScheme == COLORSCHEME_BLACKONWHITE) {
CRT_colors[RESET_COLOR] = ColorPair(Black,White); CRT_colors[RESET_COLOR] = ColorPair(Black,White);
CRT_colors[DEFAULT_COLOR] = ColorPair(Black,White); CRT_colors[DEFAULT_COLOR] = ColorPair(Black,White);
@ -316,17 +326,19 @@ void CRT_setColors(int colorScheme) {
CRT_colors[LOAD_AVERAGE_ONE] = ColorPair(Black,White); CRT_colors[LOAD_AVERAGE_ONE] = ColorPair(Black,White);
CRT_colors[LOAD] = ColorPair(Black,White); CRT_colors[LOAD] = ColorPair(Black,White);
CRT_colors[HELP_BOLD] = ColorPair(Blue,White); CRT_colors[HELP_BOLD] = ColorPair(Blue,White);
CRT_colors[CPU_NICE] = ColorPair(Cyan,White);
CRT_colors[CPU_NORMAL] = ColorPair(Green,White);
CRT_colors[CPU_KERNEL] = ColorPair(Red,White);
CRT_colors[CLOCK] = ColorPair(Black,White); CRT_colors[CLOCK] = ColorPair(Black,White);
CRT_colors[CHECK_BOX] = ColorPair(Blue,White); CRT_colors[CHECK_BOX] = ColorPair(Blue,White);
CRT_colors[CHECK_MARK] = ColorPair(Black,White); CRT_colors[CHECK_MARK] = ColorPair(Black,White);
CRT_colors[CHECK_TEXT] = ColorPair(Black,White); CRT_colors[CHECK_TEXT] = ColorPair(Black,White);
CRT_colors[HOSTNAME] = ColorPair(Black,White);
CRT_colors[CPU_NICE] = ColorPair(Cyan,White);
CRT_colors[CPU_NORMAL] = ColorPair(Green,White);
CRT_colors[CPU_KERNEL] = ColorPair(Red,White);
CRT_colors[CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black); CRT_colors[CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black);
CRT_colors[CPU_IRQ] = ColorPair(Blue,White); CRT_colors[CPU_IRQ] = ColorPair(Blue,White);
CRT_colors[CPU_SOFTIRQ] = ColorPair(Blue,White); CRT_colors[CPU_SOFTIRQ] = ColorPair(Blue,White);
CRT_colors[HOSTNAME] = ColorPair(Black,White); CRT_colors[CPU_STEAL] = ColorPair(Cyan,White);
CRT_colors[CPU_GUEST] = ColorPair(Cyan,White);
} else if (CRT_colorScheme == COLORSCHEME_BLACKONWHITE2) { } else if (CRT_colorScheme == COLORSCHEME_BLACKONWHITE2) {
CRT_colors[RESET_COLOR] = ColorPair(Black,Black); CRT_colors[RESET_COLOR] = ColorPair(Black,Black);
CRT_colors[DEFAULT_COLOR] = ColorPair(Black,Black); CRT_colors[DEFAULT_COLOR] = ColorPair(Black,Black);
@ -375,17 +387,19 @@ void CRT_setColors(int colorScheme) {
CRT_colors[LOAD_AVERAGE_ONE] = ColorPair(Black,Black); CRT_colors[LOAD_AVERAGE_ONE] = ColorPair(Black,Black);
CRT_colors[LOAD] = ColorPair(White,Black); CRT_colors[LOAD] = ColorPair(White,Black);
CRT_colors[HELP_BOLD] = ColorPair(Blue,Black); CRT_colors[HELP_BOLD] = ColorPair(Blue,Black);
CRT_colors[CPU_NICE] = ColorPair(Cyan,Black);
CRT_colors[CPU_NORMAL] = ColorPair(Green,Black);
CRT_colors[CPU_KERNEL] = ColorPair(Red,Black);
CRT_colors[CLOCK] = ColorPair(White,Black); CRT_colors[CLOCK] = ColorPair(White,Black);
CRT_colors[CHECK_BOX] = ColorPair(Blue,Black); CRT_colors[CHECK_BOX] = ColorPair(Blue,Black);
CRT_colors[CHECK_MARK] = ColorPair(Black,Black); CRT_colors[CHECK_MARK] = ColorPair(Black,Black);
CRT_colors[CHECK_TEXT] = ColorPair(Black,Black); CRT_colors[CHECK_TEXT] = ColorPair(Black,Black);
CRT_colors[HOSTNAME] = ColorPair(White,Black);
CRT_colors[CPU_NICE] = ColorPair(Cyan,Black);
CRT_colors[CPU_NORMAL] = ColorPair(Green,Black);
CRT_colors[CPU_KERNEL] = ColorPair(Red,Black);
CRT_colors[CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black); CRT_colors[CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black);
CRT_colors[CPU_IRQ] = A_BOLD | ColorPair(Blue,Black); CRT_colors[CPU_IRQ] = A_BOLD | ColorPair(Blue,Black);
CRT_colors[CPU_SOFTIRQ] = ColorPair(Blue,Black); CRT_colors[CPU_SOFTIRQ] = ColorPair(Blue,Black);
CRT_colors[HOSTNAME] = ColorPair(White,Black); CRT_colors[CPU_STEAL] = ColorPair(Black,Black);
CRT_colors[CPU_GUEST] = ColorPair(Black,Black);
} else if (CRT_colorScheme == COLORSCHEME_MIDNIGHT) { } else if (CRT_colorScheme == COLORSCHEME_MIDNIGHT) {
CRT_colors[RESET_COLOR] = ColorPair(White,Blue); CRT_colors[RESET_COLOR] = ColorPair(White,Blue);
CRT_colors[DEFAULT_COLOR] = ColorPair(White,Blue); CRT_colors[DEFAULT_COLOR] = ColorPair(White,Blue);
@ -434,17 +448,19 @@ void CRT_setColors(int colorScheme) {
CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(White,Blue); CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(White,Blue);
CRT_colors[LOAD] = A_BOLD | ColorPair(White,Blue); CRT_colors[LOAD] = A_BOLD | ColorPair(White,Blue);
CRT_colors[HELP_BOLD] = A_BOLD | ColorPair(Cyan,Blue); CRT_colors[HELP_BOLD] = A_BOLD | ColorPair(Cyan,Blue);
CRT_colors[CPU_NICE] = A_BOLD | ColorPair(Cyan,Blue);
CRT_colors[CPU_NORMAL] = A_BOLD | ColorPair(Green,Blue);
CRT_colors[CPU_KERNEL] = A_BOLD | ColorPair(Red,Blue);
CRT_colors[CLOCK] = ColorPair(White,Blue); CRT_colors[CLOCK] = ColorPair(White,Blue);
CRT_colors[CHECK_BOX] = ColorPair(Cyan,Blue); CRT_colors[CHECK_BOX] = ColorPair(Cyan,Blue);
CRT_colors[CHECK_MARK] = A_BOLD | ColorPair(White,Blue); CRT_colors[CHECK_MARK] = A_BOLD | ColorPair(White,Blue);
CRT_colors[CHECK_TEXT] = A_NORMAL | ColorPair(White,Blue); CRT_colors[CHECK_TEXT] = A_NORMAL | ColorPair(White,Blue);
CRT_colors[CPU_IOWAIT] = ColorPair(Yellow,Blue); CRT_colors[HOSTNAME] = ColorPair(White,Blue);
CRT_colors[CPU_NICE] = A_BOLD | ColorPair(Cyan,Blue);
CRT_colors[CPU_NORMAL] = A_BOLD | ColorPair(Green,Blue);
CRT_colors[CPU_KERNEL] = A_BOLD | ColorPair(Red,Blue);
CRT_colors[CPU_IOWAIT] = A_BOLD | ColorPair(Blue,Blue);
CRT_colors[CPU_IRQ] = A_BOLD | ColorPair(Black,Blue); CRT_colors[CPU_IRQ] = A_BOLD | ColorPair(Black,Blue);
CRT_colors[CPU_SOFTIRQ] = ColorPair(Black,Blue); CRT_colors[CPU_SOFTIRQ] = ColorPair(Black,Blue);
CRT_colors[HOSTNAME] = ColorPair(White,Blue); CRT_colors[CPU_STEAL] = ColorPair(White,Blue);
CRT_colors[CPU_GUEST] = ColorPair(White,Blue);
} else if (CRT_colorScheme == COLORSCHEME_BLACKNIGHT) { } else if (CRT_colorScheme == COLORSCHEME_BLACKNIGHT) {
CRT_colors[RESET_COLOR] = ColorPair(Cyan,Black); CRT_colors[RESET_COLOR] = ColorPair(Cyan,Black);
CRT_colors[DEFAULT_COLOR] = ColorPair(Cyan,Black); CRT_colors[DEFAULT_COLOR] = ColorPair(Cyan,Black);
@ -493,17 +509,19 @@ void CRT_setColors(int colorScheme) {
CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(Green,Black); CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(Green,Black);
CRT_colors[LOAD] = A_BOLD; CRT_colors[LOAD] = A_BOLD;
CRT_colors[HELP_BOLD] = A_BOLD | ColorPair(Cyan,Black); CRT_colors[HELP_BOLD] = A_BOLD | ColorPair(Cyan,Black);
CRT_colors[CPU_NICE] = ColorPair(Blue,Black);
CRT_colors[CPU_NORMAL] = ColorPair(Green,Black);
CRT_colors[CPU_KERNEL] = ColorPair(Red,Black);
CRT_colors[CLOCK] = ColorPair(Green,Black); CRT_colors[CLOCK] = ColorPair(Green,Black);
CRT_colors[CHECK_BOX] = ColorPair(Green,Black); CRT_colors[CHECK_BOX] = ColorPair(Green,Black);
CRT_colors[CHECK_MARK] = A_BOLD | ColorPair(Green,Black); CRT_colors[CHECK_MARK] = A_BOLD | ColorPair(Green,Black);
CRT_colors[CHECK_TEXT] = ColorPair(Cyan,Black); CRT_colors[CHECK_TEXT] = ColorPair(Cyan,Black);
CRT_colors[HOSTNAME] = ColorPair(Green,Black);
CRT_colors[CPU_NICE] = ColorPair(Blue,Black);
CRT_colors[CPU_NORMAL] = ColorPair(Green,Black);
CRT_colors[CPU_KERNEL] = ColorPair(Red,Black);
CRT_colors[CPU_IOWAIT] = ColorPair(Yellow,Black); CRT_colors[CPU_IOWAIT] = ColorPair(Yellow,Black);
CRT_colors[CPU_IRQ] = A_BOLD | ColorPair(Blue,Black); CRT_colors[CPU_IRQ] = A_BOLD | ColorPair(Blue,Black);
CRT_colors[CPU_SOFTIRQ] = ColorPair(Blue,Black); CRT_colors[CPU_SOFTIRQ] = ColorPair(Blue,Black);
CRT_colors[HOSTNAME] = ColorPair(Green,Black); CRT_colors[CPU_STEAL] = ColorPair(Cyan,Black);
CRT_colors[CPU_GUEST] = ColorPair(Cyan,Black);
} else { } else {
/* Default */ /* Default */
CRT_colors[RESET_COLOR] = ColorPair(White,Black); CRT_colors[RESET_COLOR] = ColorPair(White,Black);
@ -553,16 +571,18 @@ void CRT_setColors(int colorScheme) {
CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD; CRT_colors[LOAD_AVERAGE_ONE] = A_BOLD;
CRT_colors[LOAD] = A_BOLD; CRT_colors[LOAD] = A_BOLD;
CRT_colors[HELP_BOLD] = A_BOLD | ColorPair(Cyan,Black); CRT_colors[HELP_BOLD] = A_BOLD | ColorPair(Cyan,Black);
CRT_colors[CPU_NICE] = ColorPair(Blue,Black);
CRT_colors[CPU_NORMAL] = ColorPair(Green,Black);
CRT_colors[CPU_KERNEL] = ColorPair(Red,Black);
CRT_colors[CLOCK] = A_BOLD; CRT_colors[CLOCK] = A_BOLD;
CRT_colors[CHECK_BOX] = ColorPair(Cyan,Black); CRT_colors[CHECK_BOX] = ColorPair(Cyan,Black);
CRT_colors[CHECK_MARK] = A_BOLD; CRT_colors[CHECK_MARK] = A_BOLD;
CRT_colors[CHECK_TEXT] = A_NORMAL; CRT_colors[CHECK_TEXT] = A_NORMAL;
CRT_colors[HOSTNAME] = A_BOLD;
CRT_colors[CPU_NICE] = ColorPair(Blue,Black);
CRT_colors[CPU_NORMAL] = ColorPair(Green,Black);
CRT_colors[CPU_KERNEL] = ColorPair(Red,Black);
CRT_colors[CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black); CRT_colors[CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black);
CRT_colors[CPU_IRQ] = ColorPair(Yellow,Black); CRT_colors[CPU_IRQ] = ColorPair(Yellow,Black);
CRT_colors[CPU_SOFTIRQ] = ColorPair(Magenta,Black); CRT_colors[CPU_SOFTIRQ] = ColorPair(Magenta,Black);
CRT_colors[HOSTNAME] = A_BOLD; CRT_colors[CPU_STEAL] = ColorPair(Cyan,Black);
CRT_colors[CPU_GUEST] = ColorPair(Cyan,Black);
} }
} }

9
CRT.h
View File

@ -14,6 +14,7 @@ in the source distribution for its full text.
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <execinfo.h>
#include "String.h" #include "String.h"
@ -95,14 +96,16 @@ typedef enum ColorElements_ {
CHECK_MARK, CHECK_MARK,
CHECK_TEXT, CHECK_TEXT,
CLOCK, CLOCK,
HELP_BOLD,
HOSTNAME,
CPU_NICE, CPU_NICE,
CPU_NORMAL, CPU_NORMAL,
CPU_KERNEL, CPU_KERNEL,
HELP_BOLD,
CPU_IOWAIT, CPU_IOWAIT,
CPU_IRQ, CPU_IRQ,
CPU_SOFTIRQ, CPU_SOFTIRQ,
HOSTNAME, CPU_STEAL,
CPU_GUEST,
LAST_COLORELEMENT LAST_COLORELEMENT
} ColorElements; } ColorElements;
@ -117,6 +120,8 @@ extern int CRT_colors[LAST_COLORELEMENT];
char* CRT_termType; char* CRT_termType;
void *backtraceArray[128];
// TODO: pass an instance of Settings instead. // TODO: pass an instance of Settings instead.
void CRT_init(int delay, int colorScheme); void CRT_init(int delay, int colorScheme);

View File

@ -73,6 +73,6 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
Panel_add(super, (Object*) CheckItem_new(String_copy("Highlight program \"basename\""), &(settings->pl->highlightBaseName), false)); Panel_add(super, (Object*) CheckItem_new(String_copy("Highlight program \"basename\""), &(settings->pl->highlightBaseName), false));
Panel_add(super, (Object*) CheckItem_new(String_copy("Highlight megabytes in memory counters"), &(settings->pl->highlightMegabytes), false)); Panel_add(super, (Object*) CheckItem_new(String_copy("Highlight megabytes in memory counters"), &(settings->pl->highlightMegabytes), false));
Panel_add(super, (Object*) CheckItem_new(String_copy("Leave a margin around header"), &(settings->header->margin), false)); Panel_add(super, (Object*) CheckItem_new(String_copy("Leave a margin around header"), &(settings->header->margin), false));
Panel_add(super, (Object*) CheckItem_new(String_copy("Detailed CPU time (System/IO-Wait/Hard-IRQ/Soft-IRQ)"), &(settings->pl->detailedCPUTime), false)); Panel_add(super, (Object*) CheckItem_new(String_copy("Detailed CPU time (System/IO-Wait/Hard-IRQ/Soft-IRQ/Steal/Guest)"), &(settings->pl->detailedCPUTime), false));
return this; return this;
} }

View File

@ -12,7 +12,7 @@ applications_DATA = htop.desktop
pixmapdir = $(datadir)/pixmaps pixmapdir = $(datadir)/pixmaps
pixmap_DATA = htop.png pixmap_DATA = htop.png
htop_CFLAGS = -pedantic -Wall -std=c99 -D_XOPEN_SOURCE_EXTENDED htop_CFLAGS = -pedantic -Wall -std=c99 -rdynamic -D_XOPEN_SOURCE_EXTENDED
AM_CPPFLAGS = -DSYSCONFDIR=\"$(sysconfdir)\" AM_CPPFLAGS = -DSYSCONFDIR=\"$(sysconfdir)\"
myhtopsources = AvailableMetersPanel.c CategoriesPanel.c CheckItem.c \ myhtopsources = AvailableMetersPanel.c CategoriesPanel.c CheckItem.c \

View File

@ -43,17 +43,17 @@ typedef struct OpenFilesScreen_ {
}*/ }*/
static const char* tbFunctions[] = {"Refresh", "Done ", NULL}; static const char* ofsFunctions[] = {"Refresh", "Done ", NULL};
static const char* tbKeys[] = {"F5", "Esc"}; static const char* ofsKeys[] = {"F5", "Esc"};
static int tbEvents[] = {KEY_F(5), 27}; static int ofsEvents[] = {KEY_F(5), 27};
OpenFilesScreen* OpenFilesScreen_new(Process* process) { OpenFilesScreen* OpenFilesScreen_new(Process* process) {
OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen)); OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen));
this->process = process; this->process = process;
this->display = Panel_new(0, 1, COLS, LINES-3, LISTITEM_CLASS, true, ListItem_compare); this->display = Panel_new(0, 1, COLS, LINES-3, LISTITEM_CLASS, true, ListItem_compare);
this->bar = FunctionBar_new(tbFunctions, tbKeys, tbEvents); this->bar = FunctionBar_new(ofsFunctions, ofsKeys, ofsEvents);
this->tracing = true; this->tracing = true;
return this; return this;
} }

View File

@ -471,27 +471,6 @@ Process* Process_new(struct ProcessList_ *pl) {
return this; return this;
} }
Process* Process_clone(Process* this) {
Process* copy = malloc(sizeof(Process));
#if HAVE_TASKSTATS
this->io_rchar = 0;
this->io_wchar = 0;
this->io_syscr = 0;
this->io_syscw = 0;
this->io_read_bytes = 0;
this->io_rate_read_bps = 0;
this->io_rate_read_time = 0;
this->io_write_bytes = 0;
this->io_rate_write_bps = 0;
this->io_rate_write_time = 0;
this->io_cancelled_write_bytes = 0;
#endif
memcpy(copy, this, sizeof(Process));
this->comm = NULL;
this->pid = 0;
return copy;
}
void Process_toggleTag(Process* this) { void Process_toggleTag(Process* this) {
this->tag = this->tag == true ? false : true; this->tag = this->tag == true ? false : true;
} }

View File

@ -176,8 +176,6 @@ void Process_delete(Object* cast);
Process* Process_new(struct ProcessList_ *pl); Process* Process_new(struct ProcessList_ *pl);
Process* Process_clone(Process* this);
void Process_toggleTag(Process* this); void Process_toggleTag(Process* this);
bool Process_setPriority(Process* this, int priority); bool Process_setPriority(Process* this, int priority);

View File

@ -33,6 +33,7 @@ in the source distribution for its full text.
#include <assert.h> #include <assert.h>
/*{ /*{
#ifndef PROCDIR #ifndef PROCDIR
#define PROCDIR "/proc" #define PROCDIR "/proc"
#endif #endif
@ -53,10 +54,6 @@ in the source distribution for its full text.
#define MAX_READ 2048 #define MAX_READ 2048
#endif #endif
#ifndef PER_PROCESSOR_FIELDS
#define PER_PROCESSOR_FIELDS 22
#endif
}*/ }*/
/*{ /*{
@ -65,40 +62,45 @@ in the source distribution for its full text.
typedef int(*vxscanf)(void*, const char*, va_list); typedef int(*vxscanf)(void*, const char*, va_list);
#endif #endif
typedef struct CPUData_ {
unsigned long long int totalTime;
unsigned long long int userTime;
unsigned long long int systemTime;
unsigned long long int systemAllTime;
unsigned long long int idleAllTime;
unsigned long long int idleTime;
unsigned long long int niceTime;
unsigned long long int ioWaitTime;
unsigned long long int irqTime;
unsigned long long int softIrqTime;
unsigned long long int stealTime;
unsigned long long int guestTime;
unsigned long long int totalPeriod;
unsigned long long int userPeriod;
unsigned long long int systemPeriod;
unsigned long long int systemAllPeriod;
unsigned long long int idleAllPeriod;
unsigned long long int idlePeriod;
unsigned long long int nicePeriod;
unsigned long long int ioWaitPeriod;
unsigned long long int irqPeriod;
unsigned long long int softIrqPeriod;
unsigned long long int stealPeriod;
unsigned long long int guestPeriod;
} CPUData;
typedef struct ProcessList_ { typedef struct ProcessList_ {
Vector* processes; Vector* processes;
Vector* processes2; Vector* processes2;
Hashtable* processTable; Hashtable* processTable;
Process* prototype;
UsersTable* usersTable; UsersTable* usersTable;
int processorCount; int cpuCount;
int totalTasks; int totalTasks;
int runningTasks; int runningTasks;
// Must match number of PER_PROCESSOR_FIELDS constant CPUData* cpus;
unsigned long long int* totalTime;
unsigned long long int* userTime;
unsigned long long int* systemTime;
unsigned long long int* systemAllTime;
unsigned long long int* idleAllTime;
unsigned long long int* idleTime;
unsigned long long int* niceTime;
unsigned long long int* ioWaitTime;
unsigned long long int* irqTime;
unsigned long long int* softIrqTime;
unsigned long long int* stealTime;
unsigned long long int* totalPeriod;
unsigned long long int* userPeriod;
unsigned long long int* systemPeriod;
unsigned long long int* systemAllPeriod;
unsigned long long int* idleAllPeriod;
unsigned long long int* idlePeriod;
unsigned long long int* nicePeriod;
unsigned long long int* ioWaitPeriod;
unsigned long long int* irqPeriod;
unsigned long long int* softIrqPeriod;
unsigned long long int* stealPeriod;
unsigned long long int totalMem; unsigned long long int totalMem;
unsigned long long int usedMem; unsigned long long int usedMem;
@ -195,23 +197,12 @@ static inline int ProcessList_xread(ProcessList* this, vxscanf fn, void* buffer,
#endif #endif
static inline void ProcessList_allocatePerProcessorBuffers(ProcessList* this, int procs) {
unsigned long long int** bufferPtr = &(this->totalTime);
unsigned long long int* buffer = calloc(procs * PER_PROCESSOR_FIELDS, sizeof(unsigned long long int));
for (int i = 0; i < PER_PROCESSOR_FIELDS; i++) {
*bufferPtr = buffer;
bufferPtr++;
buffer += procs;
}
}
ProcessList* ProcessList_new(UsersTable* usersTable) { ProcessList* ProcessList_new(UsersTable* usersTable) {
ProcessList* this; ProcessList* this;
this = malloc(sizeof(ProcessList)); this = malloc(sizeof(ProcessList));
this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare); this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare);
this->processTable = Hashtable_new(70, false); this->processTable = Hashtable_new(70, false);
assert(Hashtable_count(this->processTable) == Vector_count(this->processes)); assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
this->prototype = Process_new(this);
this->usersTable = usersTable; this->usersTable = usersTable;
/* tree-view auxiliary buffers */ /* tree-view auxiliary buffers */
@ -224,19 +215,19 @@ ProcessList* ProcessList_new(UsersTable* usersTable) {
FILE* status = fopen(PROCSTATFILE, "r"); FILE* status = fopen(PROCSTATFILE, "r");
assert(status != NULL); assert(status != NULL);
char buffer[256]; char buffer[256];
int procs = -1; int cpus = -1;
do { do {
procs++; cpus++;
fgets(buffer, 255, status); fgets(buffer, 255, status);
} while (String_startsWith(buffer, "cpu")); } while (String_startsWith(buffer, "cpu"));
fclose(status); fclose(status);
this->processorCount = procs - 1; this->cpuCount = cpus - 1;
ProcessList_allocatePerProcessorBuffers(this, procs); this->cpus = calloc(sizeof(CPUData), cpus);
for (int i = 0; i < procs; i++) { for (int i = 0; i < cpus; i++) {
this->totalTime[i] = 1; this->cpus[i].totalTime = 1;
this->totalPeriod[i] = 1; this->cpus[i].totalPeriod = 1;
} }
this->fields = calloc(sizeof(ProcessField), LAST_PROCESSFIELD+1); this->fields = calloc(sizeof(ProcessField), LAST_PROCESSFIELD+1);
@ -265,11 +256,8 @@ void ProcessList_delete(ProcessList* this) {
Hashtable_delete(this->processTable); Hashtable_delete(this->processTable);
Vector_delete(this->processes); Vector_delete(this->processes);
Vector_delete(this->processes2); Vector_delete(this->processes2);
Process_delete((Object*)this->prototype);
// Free first entry only; free(this->cpus);
// other fields are offsets of the same buffer
free(this->totalTime);
#ifdef DEBUG_PROC #ifdef DEBUG_PROC
fclose(this->traceFile); fclose(this->traceFile);
@ -303,8 +291,10 @@ RichString ProcessList_printHeader(ProcessList* this) {
static void ProcessList_add(ProcessList* this, Process* p) { static void ProcessList_add(ProcessList* this, Process* p) {
assert(Vector_indexOf(this->processes, p, Process_pidCompare) == -1); assert(Vector_indexOf(this->processes, p, Process_pidCompare) == -1);
assert(Hashtable_get(this->processTable, p->pid) == NULL); assert(Hashtable_get(this->processTable, p->pid) == NULL);
Vector_add(this->processes, p); Vector_add(this->processes, p);
Hashtable_put(this->processTable, p->pid, p); Hashtable_put(this->processTable, p->pid, p);
assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1); assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1);
assert(Hashtable_get(this->processTable, p->pid) != NULL); assert(Hashtable_get(this->processTable, p->pid) != NULL);
assert(Hashtable_count(this->processTable) == Vector_count(this->processes)); assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
@ -357,7 +347,7 @@ static void ProcessList_buildTree(ProcessList* this, pid_t pid, int level, int i
ProcessList_buildTree(this, process->pid, level+1, nextIndent, direction, process->showChildren); ProcessList_buildTree(this, process->pid, level+1, nextIndent, direction, process->showChildren);
process->indent = indent | (1 << level); process->indent = indent | (1 << level);
} else { } else {
Hashtable_remove(this->processTable, process->pid); ProcessList_remove(this, process);
} }
} }
Vector_delete(children); Vector_delete(children);
@ -532,11 +522,10 @@ static void ProcessList_readIoFile(Process* proc, const char* dirname, char* nam
static bool ProcessList_processEntries(ProcessList* this, const char* dirname, Process* parent, pid_t parentPid, float period) { static bool ProcessList_processEntries(ProcessList* this, const char* dirname, Process* parent, pid_t parentPid, float period) {
DIR* dir; DIR* dir;
struct dirent* entry; struct dirent* entry;
Process* prototype = this->prototype;
dir = opendir(dirname); dir = opendir(dirname);
if (!dir) return false; if (!dir) return false;
int processors = this->processorCount; int cpus = this->cpuCount;
bool showUserlandThreads = !this->hideUserlandThreads; bool showUserlandThreads = !this->hideUserlandThreads;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
char* name = entry->d_name; char* name = entry->d_name;
@ -571,7 +560,7 @@ static bool ProcessList_processEntries(ProcessList* this, const char* dirname, P
if (parent && parent->pid == pid) { if (parent && parent->pid == pid) {
process = parent; process = parent;
} else { } else {
process = prototype; process = Process_new(this);
assert(process->comm == NULL); assert(process->comm == NULL);
process->pid = pid; process->pid = pid;
} }
@ -703,14 +692,16 @@ static bool ProcessList_processEntries(ProcessList* this, const char* dirname, P
} }
fclose(status); fclose(status);
command[PROCESS_COMM_LEN] = '\0'; command[PROCESS_COMM_LEN] = '\0';
free(process->comm);
process->comm = String_copy(command); process->comm = String_copy(command);
} else if (pid != parentPid && this->showThreadNames) { } else if (pid != parentPid && this->showThreadNames) {
free(process->comm);
process->comm = String_copy(command); process->comm = String_copy(command);
} }
int percent_cpu = (process->utime + process->stime - lasttimes) / int percent_cpu = (process->utime + process->stime - lasttimes) /
period * 100.0; period * 100.0;
process->percent_cpu = MAX(MIN(percent_cpu, processors*100.0), 0.0); process->percent_cpu = MAX(MIN(percent_cpu, cpus*100.0), 0.0);
if (isnan(process->percent_cpu)) process->percent_cpu = 0.0; if (isnan(process->percent_cpu)) process->percent_cpu = 0.0;
process->percent_mem = (process->m_resident * PAGE_SIZE_KB) / process->percent_mem = (process->m_resident * PAGE_SIZE_KB) /
@ -723,10 +714,8 @@ static bool ProcessList_processEntries(ProcessList* this, const char* dirname, P
} }
if (!existingProcess) { if (!existingProcess) {
process = Process_clone(process);
ProcessList_add(this, process); ProcessList_add(this, process);
} }
continue; continue;
// Exception handler. // Exception handler.
@ -737,7 +726,8 @@ static bool ProcessList_processEntries(ProcessList* this, const char* dirname, P
} }
if (existingProcess) if (existingProcess)
ProcessList_remove(this, process); ProcessList_remove(this, process);
assert(Hashtable_count(this->processTable) == Vector_count(this->processes)); else
Process_delete((Object*)process);
} }
} }
} }
@ -746,13 +736,13 @@ static bool ProcessList_processEntries(ProcessList* this, const char* dirname, P
} }
void ProcessList_scan(ProcessList* this) { void ProcessList_scan(ProcessList* this) {
unsigned long long int usertime, nicetime, systemtime, systemalltime, idlealltime, idletime, totaltime; unsigned long long int usertime, nicetime, systemtime, systemalltime, idlealltime, idletime, totaltime, virtalltime;
unsigned long long int swapFree = 0; unsigned long long int swapFree = 0;
FILE* status; FILE* status;
status = ProcessList_fopen(this, PROCMEMINFOFILE, "r"); status = ProcessList_fopen(this, PROCMEMINFOFILE, "r");
assert(status != NULL); assert(status != NULL);
int processors = this->processorCount; int cpus = this->cpuCount;
{ {
char buffer[128]; char buffer[128];
while (fgets(buffer, 128, status)) { while (fgets(buffer, 128, status)) {
@ -791,62 +781,66 @@ void ProcessList_scan(ProcessList* this) {
status = ProcessList_fopen(this, PROCSTATFILE, "r"); status = ProcessList_fopen(this, PROCSTATFILE, "r");
assert(status != NULL); assert(status != NULL);
for (int i = 0; i <= processors; i++) { for (int i = 0; i <= cpus; i++) {
char buffer[256]; char buffer[256];
int cpuid; int cpuid;
unsigned long long int ioWait, irq, softIrq, steal; unsigned long long int ioWait, irq, softIrq, steal, guest;
ioWait = irq = softIrq = steal = 0; ioWait = irq = softIrq = steal = guest = 0;
// Dependending on your kernel version, // Dependending on your kernel version,
// 5, 7 or 8 of these fields will be set. // 5, 7 or 8 of these fields will be set.
// The rest will remain at zero. // The rest will remain at zero.
fgets(buffer, 255, status); fgets(buffer, 255, status);
if (i == 0) if (i == 0)
ProcessList_read(this, buffer, "cpu %llu %llu %llu %llu %llu %llu %llu %llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal); ProcessList_read(this, buffer, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest);
else { else {
ProcessList_read(this, buffer, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal); ProcessList_read(this, buffer, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest);
assert(cpuid == i - 1); assert(cpuid == i - 1);
} }
// Fields existing on kernels >= 2.6 // Fields existing on kernels >= 2.6
// (and RHEL's patched kernel 2.4...) // (and RHEL's patched kernel 2.4...)
idlealltime = idletime + ioWait; idlealltime = idletime + ioWait;
systemalltime = systemtime + irq + softIrq + steal; systemalltime = systemtime + irq + softIrq;
totaltime = usertime + nicetime + systemalltime + idlealltime; virtalltime = steal + guest;
assert (usertime >= this->userTime[i]); totaltime = usertime + nicetime + systemalltime + idlealltime + virtalltime;
assert (nicetime >= this->niceTime[i]); CPUData* cpuData = &(this->cpus[i]);
assert (systemtime >= this->systemTime[i]); assert (usertime >= cpuData->userTime);
assert (idletime >= this->idleTime[i]); assert (nicetime >= cpuData->niceTime);
assert (totaltime >= this->totalTime[i]); assert (systemtime >= cpuData->systemTime);
assert (systemalltime >= this->systemAllTime[i]); assert (idletime >= cpuData->idleTime);
assert (idlealltime >= this->idleAllTime[i]); assert (totaltime >= cpuData->totalTime);
assert (ioWait >= this->ioWaitTime[i]); assert (systemalltime >= cpuData->systemAllTime);
assert (irq >= this->irqTime[i]); assert (idlealltime >= cpuData->idleAllTime);
assert (softIrq >= this->softIrqTime[i]); assert (ioWait >= cpuData->ioWaitTime);
assert (steal >= this->stealTime[i]); assert (irq >= cpuData->irqTime);
this->userPeriod[i] = usertime - this->userTime[i]; assert (softIrq >= cpuData->softIrqTime);
this->nicePeriod[i] = nicetime - this->niceTime[i]; assert (steal >= cpuData->stealTime);
this->systemPeriod[i] = systemtime - this->systemTime[i]; assert (guest >= cpuData->guestTime);
this->systemAllPeriod[i] = systemalltime - this->systemAllTime[i]; cpuData->userPeriod = usertime - cpuData->userTime;
this->idleAllPeriod[i] = idlealltime - this->idleAllTime[i]; cpuData->nicePeriod = nicetime - cpuData->niceTime;
this->idlePeriod[i] = idletime - this->idleTime[i]; cpuData->systemPeriod = systemtime - cpuData->systemTime;
this->ioWaitPeriod[i] = ioWait - this->ioWaitTime[i]; cpuData->systemAllPeriod = systemalltime - cpuData->systemAllTime;
this->irqPeriod[i] = irq - this->irqTime[i]; cpuData->idleAllPeriod = idlealltime - cpuData->idleAllTime;
this->softIrqPeriod[i] = softIrq - this->softIrqTime[i]; cpuData->idlePeriod = idletime - cpuData->idleTime;
this->stealPeriod[i] = steal - this->stealTime[i]; cpuData->ioWaitPeriod = ioWait - cpuData->ioWaitTime;
this->totalPeriod[i] = totaltime - this->totalTime[i]; cpuData->irqPeriod = irq - cpuData->irqTime;
this->userTime[i] = usertime; cpuData->softIrqPeriod = softIrq - cpuData->softIrqTime;
this->niceTime[i] = nicetime; cpuData->stealPeriod = steal - cpuData->stealTime;
this->systemTime[i] = systemtime; cpuData->guestPeriod = guest - cpuData->guestTime;
this->systemAllTime[i] = systemalltime; cpuData->totalPeriod = totaltime - cpuData->totalTime;
this->idleAllTime[i] = idlealltime; cpuData->userTime = usertime;
this->idleTime[i] = idletime; cpuData->niceTime = nicetime;
this->ioWaitTime[i] = ioWait; cpuData->systemTime = systemtime;
this->irqTime[i] = irq; cpuData->systemAllTime = systemalltime;
this->softIrqTime[i] = softIrq; cpuData->idleAllTime = idlealltime;
this->stealTime[i] = steal; cpuData->idleTime = idletime;
this->totalTime[i] = totaltime; cpuData->ioWaitTime = ioWait;
cpuData->irqTime = irq;
cpuData->softIrqTime = softIrq;
cpuData->stealTime = steal;
cpuData->guestTime = guest;
cpuData->totalTime = totaltime;
} }
float period = (float)this->totalPeriod[0] / processors; float period = (float)this->cpus[0].totalPeriod / cpus; fclose(status);
fclose(status);
// mark all process as "dirty" // mark all process as "dirty"
for (int i = 0; i < Vector_size(this->processes); i++) { for (int i = 0; i < Vector_size(this->processes); i++) {
@ -884,3 +878,11 @@ ProcessField ProcessList_keyAt(ProcessList* this, int at) {
} }
return COMM; return COMM;
} }
void ProcessList_expandTree(ProcessList* this) {
int size = Vector_size(this->processes);
for (int i = 0; i < size; i++) {
Process* process = (Process*) Vector_get(this->processes, i);
process->showChildren = true;
}
}

View File

@ -35,6 +35,7 @@ in the source distribution for its full text.
#include "debug.h" #include "debug.h"
#include <assert.h> #include <assert.h>
#ifndef PROCDIR #ifndef PROCDIR
#define PROCDIR "/proc" #define PROCDIR "/proc"
#endif #endif
@ -55,50 +56,51 @@ in the source distribution for its full text.
#define MAX_READ 2048 #define MAX_READ 2048
#endif #endif
#ifndef PER_PROCESSOR_FIELDS
#define PER_PROCESSOR_FIELDS 22
#endif
#ifdef DEBUG_PROC #ifdef DEBUG_PROC
typedef int(*vxscanf)(void*, const char*, va_list); typedef int(*vxscanf)(void*, const char*, va_list);
#endif #endif
typedef struct CPUData_ {
unsigned long long int totalTime;
unsigned long long int userTime;
unsigned long long int systemTime;
unsigned long long int systemAllTime;
unsigned long long int idleAllTime;
unsigned long long int idleTime;
unsigned long long int niceTime;
unsigned long long int ioWaitTime;
unsigned long long int irqTime;
unsigned long long int softIrqTime;
unsigned long long int stealTime;
unsigned long long int guestTime;
unsigned long long int totalPeriod;
unsigned long long int userPeriod;
unsigned long long int systemPeriod;
unsigned long long int systemAllPeriod;
unsigned long long int idleAllPeriod;
unsigned long long int idlePeriod;
unsigned long long int nicePeriod;
unsigned long long int ioWaitPeriod;
unsigned long long int irqPeriod;
unsigned long long int softIrqPeriod;
unsigned long long int stealPeriod;
unsigned long long int guestPeriod;
} CPUData;
typedef struct ProcessList_ { typedef struct ProcessList_ {
Vector* processes; Vector* processes;
Vector* processes2; Vector* processes2;
Hashtable* processTable; Hashtable* processTable;
Process* prototype;
UsersTable* usersTable; UsersTable* usersTable;
int processorCount; int cpuCount;
int totalTasks; int totalTasks;
int runningTasks; int runningTasks;
// Must match number of PER_PROCESSOR_FIELDS constant CPUData* cpus;
unsigned long long int* totalTime;
unsigned long long int* userTime;
unsigned long long int* systemTime;
unsigned long long int* systemAllTime;
unsigned long long int* idleAllTime;
unsigned long long int* idleTime;
unsigned long long int* niceTime;
unsigned long long int* ioWaitTime;
unsigned long long int* irqTime;
unsigned long long int* softIrqTime;
unsigned long long int* stealTime;
unsigned long long int* totalPeriod;
unsigned long long int* userPeriod;
unsigned long long int* systemPeriod;
unsigned long long int* systemAllPeriod;
unsigned long long int* idleAllPeriod;
unsigned long long int* idlePeriod;
unsigned long long int* nicePeriod;
unsigned long long int* ioWaitPeriod;
unsigned long long int* irqPeriod;
unsigned long long int* softIrqPeriod;
unsigned long long int* stealPeriod;
unsigned long long int totalMem; unsigned long long int totalMem;
unsigned long long int usedMem; unsigned long long int usedMem;
@ -167,4 +169,6 @@ void ProcessList_scan(ProcessList* this);
ProcessField ProcessList_keyAt(ProcessList* this, int at); ProcessField ProcessList_keyAt(ProcessList* this, int at);
void ProcessList_expandTree(ProcessList* this);
#endif #endif

View File

@ -32,17 +32,17 @@ typedef struct TraceScreen_ {
}*/ }*/
static const char* tbFunctions[] = {"AutoScroll ", "Stop Tracing ", "Done ", NULL}; static const char* tsFunctions[] = {"AutoScroll ", "Stop Tracing ", "Done ", NULL};
static const char* tbKeys[] = {"F4", "F5", "Esc"}; static const char* tsKeys[] = {"F4", "F5", "Esc"};
static int tbEvents[] = {KEY_F(4), KEY_F(5), 27}; static int tsEvents[] = {KEY_F(4), KEY_F(5), 27};
TraceScreen* TraceScreen_new(Process* process) { TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen)); TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen));
this->process = process; this->process = process;
this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true, ListItem_compare); this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true, ListItem_compare);
this->bar = FunctionBar_new(tbFunctions, tbKeys, tbEvents); this->bar = FunctionBar_new(tsFunctions, tsKeys, tsEvents);
this->tracing = true; this->tracing = true;
return this; return this;
} }

22
htop.c
View File

@ -83,13 +83,16 @@ static void showHelp(ProcessList* pl) {
addattrstr(CRT_colors[CPU_KERNEL], "kernel"); addstr("/"); addattrstr(CRT_colors[CPU_KERNEL], "kernel"); addstr("/");
addattrstr(CRT_colors[CPU_IRQ], "irq"); addstr("/"); addattrstr(CRT_colors[CPU_IRQ], "irq"); addstr("/");
addattrstr(CRT_colors[CPU_SOFTIRQ], "soft-irq"); addstr("/"); addattrstr(CRT_colors[CPU_SOFTIRQ], "soft-irq"); addstr("/");
addattrstr(CRT_colors[CPU_IOWAIT], "io-wait"); addattrstr(CRT_colors[CPU_IOWAIT], "io-wait"); addstr("/");
addattrstr(CRT_colors[CPU_STEAL], "steal"); addstr("/");
addattrstr(CRT_colors[CPU_GUEST], "guest");
addattrstr(CRT_colors[BAR_SHADOW], " used%"); addattrstr(CRT_colors[BAR_SHADOW], " used%");
} else { } else {
addattrstr(CRT_colors[CPU_NICE], "low-priority"); addstr("/"); addattrstr(CRT_colors[CPU_NICE], "low-priority"); addstr("/");
addattrstr(CRT_colors[CPU_NORMAL], "normal"); addstr("/"); addattrstr(CRT_colors[CPU_NORMAL], "normal"); addstr("/");
addattrstr(CRT_colors[CPU_KERNEL], "kernel"); addattrstr(CRT_colors[CPU_KERNEL], "kernel"); addstr("/");
addattrstr(CRT_colors[BAR_SHADOW], " used%"); addattrstr(CRT_colors[CPU_STEAL], "virtualiz");
addattrstr(CRT_colors[BAR_SHADOW], " used%");
} }
addattrstr(CRT_colors[BAR_BORDER], "]"); addattrstr(CRT_colors[BAR_BORDER], "]");
attrset(CRT_colors[DEFAULT_COLOR]); attrset(CRT_colors[DEFAULT_COLOR]);
@ -98,13 +101,13 @@ static void showHelp(ProcessList* pl) {
addattrstr(CRT_colors[MEMORY_USED], "used"); addstr("/"); addattrstr(CRT_colors[MEMORY_USED], "used"); addstr("/");
addattrstr(CRT_colors[MEMORY_BUFFERS], "buffers"); addstr("/"); addattrstr(CRT_colors[MEMORY_BUFFERS], "buffers"); addstr("/");
addattrstr(CRT_colors[MEMORY_CACHE], "cache"); addattrstr(CRT_colors[MEMORY_CACHE], "cache");
addattrstr(CRT_colors[BAR_SHADOW], " used/total"); addattrstr(CRT_colors[BAR_SHADOW], " used/total");
addattrstr(CRT_colors[BAR_BORDER], "]"); addattrstr(CRT_colors[BAR_BORDER], "]");
attrset(CRT_colors[DEFAULT_COLOR]); attrset(CRT_colors[DEFAULT_COLOR]);
mvaddstr(5, 0, "Swap bar: "); mvaddstr(5, 0, "Swap bar: ");
addattrstr(CRT_colors[BAR_BORDER], "["); addattrstr(CRT_colors[BAR_BORDER], "[");
addattrstr(CRT_colors[SWAP], "used"); addattrstr(CRT_colors[SWAP], "used");
addattrstr(CRT_colors[BAR_SHADOW], " used/total"); addattrstr(CRT_colors[BAR_SHADOW], " used/total");
addattrstr(CRT_colors[BAR_BORDER], "]"); addattrstr(CRT_colors[BAR_BORDER], "]");
attrset(CRT_colors[DEFAULT_COLOR]); attrset(CRT_colors[DEFAULT_COLOR]);
mvaddstr(6,0, "Type and layout of header meters are configurable in the setup screen."); mvaddstr(6,0, "Type and layout of header meters are configurable in the setup screen.");
@ -122,7 +125,7 @@ static void showHelp(ProcessList* pl) {
mvaddstr(16, 0, " ] F7: higher priority (root only) M: sort by MEM%"); mvaddstr(16, 0, " ] F7: higher priority (root only) M: sort by MEM%");
mvaddstr(17, 0, " [ F8: lower priority (+ nice) T: sort by TIME"); mvaddstr(17, 0, " [ F8: lower priority (+ nice) T: sort by TIME");
#ifdef HAVE_PLPA #ifdef HAVE_PLPA
if (pl->processorCount > 1) if (pl->cpuCount > 1)
mvaddstr(18, 0, " a: set CPU affinity F4 I: invert sort order"); mvaddstr(18, 0, " a: set CPU affinity F4 I: invert sort order");
else else
#endif #endif
@ -143,7 +146,7 @@ static void showHelp(ProcessList* pl) {
mvaddstr(17, 0, " - ] F8"); mvaddstr(17,40, " T"); mvaddstr(17, 0, " - ] F8"); mvaddstr(17,40, " T");
mvaddstr(18,40, " F4 I"); mvaddstr(18,40, " F4 I");
#if HAVE_PLPA #if HAVE_PLPA
if (pl->processorCount > 1) if (pl->cpuCount > 1)
mvaddstr(18, 0, " a:"); mvaddstr(18, 0, " a:");
#endif #endif
mvaddstr(19, 0, " F2 S"); mvaddstr(19,40, " F6 >"); mvaddstr(19, 0, " F2 S"); mvaddstr(19,40, " F6 >");
@ -685,12 +688,12 @@ int main(int argc, char** argv) {
#ifdef HAVE_PLPA #ifdef HAVE_PLPA
case 'a': case 'a':
{ {
if (pl->processorCount == 1) if (pl->cpuCount == 1)
break; break;
unsigned long curr = Process_getAffinity((Process*) Panel_getSelected(panel)); unsigned long curr = Process_getAffinity((Process*) Panel_getSelected(panel));
Panel* affinityPanel = AffinityPanel_new(pl->processorCount, curr); Panel* affinityPanel = AffinityPanel_new(pl->cpuCount, curr);
const char* fuFunctions[] = {"Set ", "Cancel ", NULL}; const char* fuFunctions[] = {"Set ", "Cancel ", NULL};
void* set = pickFromVector(panel, affinityPanel, 15, headerHeight, fuFunctions, defaultBar); void* set = pickFromVector(panel, affinityPanel, 15, headerHeight, fuFunctions, defaultBar);
@ -782,6 +785,7 @@ int main(int argc, char** argv) {
case KEY_F(5): case KEY_F(5):
refreshTimeout = 0; refreshTimeout = 0;
pl->treeView = !pl->treeView; pl->treeView = !pl->treeView;
ProcessList_expandTree(pl);
settings->changed = true; settings->changed = true;
break; break;
case 'H': case 'H':