LoadAverageMeter.c 2.52 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "LoadAverageMeter.h"
#include "Meter.h"

#include <curses.h>

#include "debug.h"

15
16
17
int LoadAverageMeter_attributes[] = {
   LOAD_AVERAGE_FIFTEEN, LOAD_AVERAGE_FIVE, LOAD_AVERAGE_ONE
};
Hisham Muhammad's avatar
Hisham Muhammad committed
18

19
int LoadMeter_attributes[] = { LOAD };
Hisham Muhammad's avatar
Hisham Muhammad committed
20

21
static inline void LoadAverageMeter_scan(double* one, double* five, double* fifteen) {
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24
25
26
27
28
29
30
   int activeProcs, totalProcs, lastProc;
   FILE *fd = fopen(PROCDIR "/loadavg", "r");
   int read = fscanf(fd, "%lf %lf %lf %d/%d %d", one, five, fifteen,
      &activeProcs, &totalProcs, &lastProc);
   (void) read;
   assert(read == 6);
   fclose(fd);
}

31
static void LoadAverageMeter_setValues(Meter* this, char* buffer, int size) {
32
33
   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]);
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
}

36
static void LoadAverageMeter_display(Object* cast, RichString* out) {
Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
   Meter* this = (Meter*)cast;
   char buffer[20];
39
   RichString_init(out);
Hisham Muhammad's avatar
Hisham Muhammad committed
40
   sprintf(buffer, "%.2f ", this->values[2]);
41
   RichString_append(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
42
43
44
   sprintf(buffer, "%.2f ", this->values[1]);
   RichString_append(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer);
   sprintf(buffer, "%.2f ", this->values[0]);
45
46
47
   RichString_append(out, CRT_colors[LOAD_AVERAGE_ONE], buffer);
}

48
static void LoadMeter_setValues(Meter* this, char* buffer, int size) {
49
50
51
52
53
54
55
56
   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]);
}

57
static void LoadMeter_display(Object* cast, RichString* out) {
58
59
   Meter* this = (Meter*)cast;
   char buffer[20];
60
   RichString_init(out);
61
62
   sprintf(buffer, "%.2f ", ((Meter*)this)->values[0]);
   RichString_append(out, CRT_colors[LOAD], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
63
}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

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: "
};