mirror of
https://github.com/xzeldon/htop.git
synced 2025-07-14 13:04:35 +03:00
- Overhaul meters implementation;
- add AllCPUsMeter; - because of that, the new .htoprc is incompatible with previous released versions.
This commit is contained in:
111
CPUMeter.c
111
CPUMeter.c
@ -18,17 +18,36 @@ in the source distribution for its full text.
|
||||
#include "debug.h"
|
||||
#include <assert.h>
|
||||
|
||||
/*{
|
||||
/* private property */
|
||||
static int CPUMeter_attributes[] = { CPU_NICE, CPU_NORMAL, CPU_KERNEL };
|
||||
|
||||
typedef struct CPUMeter_ CPUMeter;
|
||||
|
||||
struct CPUMeter_ {
|
||||
Meter super;
|
||||
ProcessList* pl;
|
||||
int processor;
|
||||
/* 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
|
||||
};
|
||||
|
||||
}*/
|
||||
/* 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,
|
||||
.done = AllCPUsMeter_done
|
||||
};
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
||||
@ -37,50 +56,66 @@ struct CPUMeter_ {
|
||||
#define MAX(a,b) ((a)>(b)?(a):(b))
|
||||
#endif
|
||||
|
||||
CPUMeter* CPUMeter_new(ProcessList* pl, int processor) {
|
||||
CPUMeter* this = malloc(sizeof(CPUMeter));
|
||||
char* caption;
|
||||
if (pl->processorCount == 1 || processor == 0) {
|
||||
caption = String_copy("CPU");
|
||||
} else {
|
||||
caption = (char*) malloc(4);
|
||||
void CPUMeter_init(Meter* this) {
|
||||
int processor = this->param;
|
||||
if (this->pl->processorCount > 1) {
|
||||
char caption[10];
|
||||
sprintf(caption, "%-3d", processor);
|
||||
Meter_setCaption(this, caption);
|
||||
}
|
||||
Meter_init((Meter*)this, NULL, caption, 3);
|
||||
((Meter*)this)->name = malloc(20);
|
||||
sprintf(((Meter*)this)->name, "CPU(%d)", processor);
|
||||
((Meter*)this)->attributes[0] = CPU_NICE;
|
||||
((Meter*)this)->attributes[1] = CPU_NORMAL;
|
||||
((Meter*)this)->attributes[2] = CPU_KERNEL;
|
||||
((Meter*)this)->setValues = CPUMeter_setValues;
|
||||
((Object*)this)->display = CPUMeter_display;
|
||||
((Meter*)this)->total = 1.0;
|
||||
Meter_setMode((Meter*)this, BAR);
|
||||
this->processor = processor;
|
||||
this->pl = pl;
|
||||
return this;
|
||||
if (this->param == 0)
|
||||
Meter_setCaption(this, "Avg");
|
||||
}
|
||||
|
||||
void CPUMeter_setValues(Meter* cast) {
|
||||
CPUMeter* this = (CPUMeter*)cast;
|
||||
cast->values[0] = this->pl->nicePeriod[this->processor] / (double)this->pl->totalPeriod[this->processor];
|
||||
cast->values[1] = this->pl->userPeriod[this->processor] / (double)this->pl->totalPeriod[this->processor];
|
||||
cast->values[2] = this->pl->systemPeriod[this->processor] / (double)this->pl->totalPeriod[this->processor];
|
||||
double cpu = MIN(100.0, MAX(0.0, (cast->values[0]+cast->values[1]+cast->values[2])*100.0 ));
|
||||
snprintf(cast->displayBuffer.c, 7, "%5.1f%%", cpu );
|
||||
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 );
|
||||
}
|
||||
|
||||
void CPUMeter_display(Object* cast, RichString* out) {
|
||||
char buffer[50];
|
||||
Meter* this = (Meter*)cast;
|
||||
RichString_prune(out);
|
||||
sprintf(buffer, "%5.1f%% ", this->values[1] * 100.0);
|
||||
sprintf(buffer, "%5.1f%% ", this->values[1]);
|
||||
RichString_append(out, CRT_colors[METER_TEXT], ":");
|
||||
RichString_append(out, CRT_colors[CPU_NORMAL], buffer);
|
||||
sprintf(buffer, "%5.1f%% ", this->values[2] * 100.0);
|
||||
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] * 100.0);
|
||||
sprintf(buffer, "%5.1f%% ", this->values[0]);
|
||||
RichString_append(out, CRT_colors[METER_TEXT], "low:");
|
||||
RichString_append(out, CRT_colors[CPU_NICE], buffer);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
void AllCPUsMeter_draw(Meter* this, int x, int y, int w) {
|
||||
int processors = this->pl->processorCount;
|
||||
Meter** meters = (Meter**) this->drawBuffer;
|
||||
this->h = Meter_modes[this->mode]->h * processors;
|
||||
for (int i = 0; i < processors; i++) {
|
||||
Meter_setMode(meters[i], this->mode);
|
||||
meters[i]->draw(meters[i], x, y+i, w);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user