2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2011-12-26 21:35:57 +00:00
|
|
|
htop - LoadAverageMeter.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 "LoadAverageMeter.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
|
|
|
#include "CRT.h"
|
|
|
|
#include "debug.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
#include <curses.h>
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <assert.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
|
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 LoadAverageMeter_attributes[] = {
|
|
|
|
LOAD_AVERAGE_FIFTEEN, LOAD_AVERAGE_FIVE, LOAD_AVERAGE_ONE
|
|
|
|
};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
int LoadMeter_attributes[] = { LOAD };
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2006-06-06 20:41:01 +00:00
|
|
|
static inline void LoadAverageMeter_scan(double* one, double* five, double* fifteen) {
|
2006-03-04 18:16:49 +00:00
|
|
|
int activeProcs, totalProcs, lastProc;
|
2011-09-08 02:54:02 +00:00
|
|
|
*one = 0; *five = 0; *fifteen = 0;
|
2006-03-04 18:16:49 +00:00
|
|
|
FILE *fd = fopen(PROCDIR "/loadavg", "r");
|
2011-09-08 02:54:02 +00:00
|
|
|
if (fd) {
|
2011-10-25 00:05:46 +00:00
|
|
|
int total = fscanf(fd, "%32lf %32lf %32lf %32d/%32d %32d", one, five, fifteen,
|
2011-09-08 02:54:02 +00:00
|
|
|
&activeProcs, &totalProcs, &lastProc);
|
|
|
|
(void) total;
|
|
|
|
assert(total == 6);
|
|
|
|
fclose(fd);
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void LoadAverageMeter_setValues(Meter* this, char* buffer, int size) {
|
2006-04-10 20:40:38 +00:00
|
|
|
LoadAverageMeter_scan(&this->values[2], &this->values[1], &this->values[0]);
|
|
|
|
snprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[2], this->values[1], this->values[0]);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void LoadAverageMeter_display(Object* cast, RichString* out) {
|
2006-03-04 18:16:49 +00:00
|
|
|
Meter* this = (Meter*)cast;
|
|
|
|
char buffer[20];
|
|
|
|
sprintf(buffer, "%.2f ", this->values[2]);
|
2010-11-22 12:40:20 +00:00
|
|
|
RichString_write(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer);
|
2006-03-04 18:16:49 +00:00
|
|
|
sprintf(buffer, "%.2f ", this->values[1]);
|
|
|
|
RichString_append(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer);
|
|
|
|
sprintf(buffer, "%.2f ", this->values[0]);
|
2006-04-10 20:40:38 +00:00
|
|
|
RichString_append(out, CRT_colors[LOAD_AVERAGE_ONE], buffer);
|
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void LoadMeter_setValues(Meter* this, char* buffer, int size) {
|
2006-04-10 20:40:38 +00:00
|
|
|
double five, fifteen;
|
|
|
|
LoadAverageMeter_scan(&this->values[0], &five, &fifteen);
|
|
|
|
if (this->values[0] > this->total) {
|
|
|
|
this->total = this->values[0];
|
|
|
|
}
|
|
|
|
snprintf(buffer, size, "%.2f", this->values[0]);
|
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void LoadMeter_display(Object* cast, RichString* out) {
|
2006-04-10 20:40:38 +00:00
|
|
|
Meter* this = (Meter*)cast;
|
|
|
|
char buffer[20];
|
|
|
|
sprintf(buffer, "%.2f ", ((Meter*)this)->values[0]);
|
2010-11-22 12:40:20 +00:00
|
|
|
RichString_write(out, CRT_colors[LOAD], buffer);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2008-03-09 08:58:38 +00:00
|
|
|
|
|
|
|
MeterType LoadAverageMeter = {
|
|
|
|
.setValues = LoadAverageMeter_setValues,
|
|
|
|
.display = LoadAverageMeter_display,
|
|
|
|
.mode = TEXT_METERMODE,
|
|
|
|
.items = 3,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = LoadAverageMeter_attributes,
|
|
|
|
.name = "LoadAverage",
|
|
|
|
.uiName = "Load average",
|
|
|
|
.caption = "Load average: "
|
|
|
|
};
|
|
|
|
|
|
|
|
MeterType LoadMeter = {
|
|
|
|
.setValues = LoadMeter_setValues,
|
|
|
|
.display = LoadMeter_display,
|
|
|
|
.mode = TEXT_METERMODE,
|
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = LoadMeter_attributes,
|
|
|
|
.name = "Load",
|
|
|
|
.uiName = "Load",
|
|
|
|
.caption = "Load: "
|
|
|
|
};
|