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

#include "LoadAverageMeter.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
10
11

#include "CRT.h"
#include "debug.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
13

#include <curses.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
14
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
15

Hisham Muhammad's avatar
Hisham Muhammad committed
16
17
18
/*{
#include "Meter.h"
}*/
Hisham Muhammad's avatar
Hisham Muhammad committed
19

20
21
22
int LoadAverageMeter_attributes[] = {
   LOAD_AVERAGE_FIFTEEN, LOAD_AVERAGE_FIVE, LOAD_AVERAGE_ONE
};
Hisham Muhammad's avatar
Hisham Muhammad committed
23

24
int LoadMeter_attributes[] = { LOAD };
Hisham Muhammad's avatar
Hisham Muhammad committed
25

26
static inline void LoadAverageMeter_scan(double* one, double* five, double* fifteen) {
Hisham Muhammad's avatar
Hisham Muhammad committed
27
   int activeProcs, totalProcs, lastProc;
Hisham Muhammad's avatar
Hisham Muhammad committed
28
   *one = 0; *five = 0; *fifteen = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
29
   FILE *fd = fopen(PROCDIR "/loadavg", "r");
Hisham Muhammad's avatar
Hisham Muhammad committed
30
   if (fd) {
31
      int total = fscanf(fd, "%32lf %32lf %32lf %32d/%32d %32d", one, five, fifteen,
Hisham Muhammad's avatar
Hisham Muhammad committed
32
33
34
35
36
         &activeProcs, &totalProcs, &lastProc);
      (void) total;
      assert(total == 6);
      fclose(fd);
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
}

39
static void LoadAverageMeter_setValues(Meter* this, char* buffer, int size) {
40
41
   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
42
43
}

44
static void LoadAverageMeter_display(Object* cast, RichString* out) {
Hisham Muhammad's avatar
Hisham Muhammad committed
45
46
47
   Meter* this = (Meter*)cast;
   char buffer[20];
   sprintf(buffer, "%.2f ", this->values[2]);
48
   RichString_write(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
51
   sprintf(buffer, "%.2f ", this->values[1]);
   RichString_append(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer);
   sprintf(buffer, "%.2f ", this->values[0]);
52
53
54
   RichString_append(out, CRT_colors[LOAD_AVERAGE_ONE], buffer);
}

55
static void LoadMeter_setValues(Meter* this, char* buffer, int size) {
56
57
58
59
60
61
62
63
   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]);
}

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

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