LoadAverageMeter.c 2.76 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

#include "CRT.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11

Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
13

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

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

22
int LoadMeter_attributes[] = { LOAD };
Hisham Muhammad's avatar
Hisham Muhammad committed
23

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

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

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

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

62
static void LoadMeter_display(Object* cast, RichString* out) {
63
64
65
   Meter* this = (Meter*)cast;
   char buffer[20];
   sprintf(buffer, "%.2f ", ((Meter*)this)->values[0]);
66
   RichString_write(out, CRT_colors[LOAD], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
67
}
68

69
70
71
72
73
74
MeterClass LoadAverageMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = LoadAverageMeter_display,
   },
75
   .setValues = LoadAverageMeter_setValues, 
76
   .defaultMode = TEXT_METERMODE,
77
   .maxItems = 3,
78
79
80
81
82
83
84
   .total = 100.0,
   .attributes = LoadAverageMeter_attributes,
   .name = "LoadAverage",
   .uiName = "Load average",
   .caption = "Load average: "
};

85
86
87
88
89
90
MeterClass LoadMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = LoadMeter_display,
   },
91
   .setValues = LoadMeter_setValues, 
92
   .defaultMode = TEXT_METERMODE,
93
94
95
96
97
98
   .total = 100.0,
   .attributes = LoadMeter_attributes,
   .name = "Load",
   .uiName = "Load",
   .caption = "Load: "
};