From d3c9975943df58e293359b87905d19ff1fd52061 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Apr 2016 13:00:15 +0200 Subject: Imported Upstream version 0.5 --- CPUMeter.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 CPUMeter.c (limited to 'CPUMeter.c') diff --git a/CPUMeter.c b/CPUMeter.c new file mode 100644 index 0000000..8e598d0 --- /dev/null +++ b/CPUMeter.c @@ -0,0 +1,80 @@ +/* +htop +(C) 2004 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 "ProcessList.h" + +#include +#include +#include +#include +#include + +#include "debug.h" +#include + +/*{ + +typedef struct CPUMeter_ CPUMeter; + +struct CPUMeter_ { + Meter super; + ProcessList* pl; + int processor; +}; + +}*/ + +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); + sprintf(caption, "%-3d", processor); + } + Meter_init((Meter*)this, NULL, caption, 3); + ((Meter*)this)->name = malloc(20); + sprintf(((Meter*)this)->name, "CPU(%d)", processor); + ((Meter*)this)->attributes[0] = CRT_colors[CPU_NICE]; + ((Meter*)this)->attributes[1] = CRT_colors[CPU_NORMAL]; + ((Meter*)this)->attributes[2] = CRT_colors[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; +} + +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_display(Object* cast, RichString* out) { + char buffer[50]; + Meter* this = (Meter*)cast; + RichString_prune(out); + sprintf(buffer, "%5.1f%% ", this->values[1] * 100.0); + RichString_append(out, CRT_colors[METER_TEXT], ":"); + RichString_append(out, this->attributes[1], buffer); + sprintf(buffer, "%5.1f%% ", this->values[2] * 100.0); + RichString_append(out, CRT_colors[METER_TEXT], "sys:"); + RichString_append(out, this->attributes[2], buffer); + sprintf(buffer, "%5.1f%% ", this->values[0] * 100.0); + RichString_append(out, CRT_colors[METER_TEXT], "low:"); + RichString_append(out, this->attributes[0], buffer); +} -- cgit v1.2.3