LoadAverageMeter.c 2.48 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"
11
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12

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

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

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

23
static void LoadAverageMeter_setValues(Meter* this, char* buffer, int size) {
24
25
   Platform_getLoadAverage(&this->values[0], &this->values[1], &this->values[2]);
   snprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]);
Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
}

28
static void LoadAverageMeter_display(Object* cast, RichString* out) {
Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
   Meter* this = (Meter*)cast;
   char buffer[20];
31
32
   sprintf(buffer, "%.2f ", this->values[0]);
   RichString_write(out, CRT_colors[LOAD_AVERAGE_ONE], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
33
34
   sprintf(buffer, "%.2f ", this->values[1]);
   RichString_append(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer);
35
36
   sprintf(buffer, "%.2f ", this->values[2]);
   RichString_append(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer);
37
38
}

39
static void LoadMeter_setValues(Meter* this, char* buffer, int size) {
40
   double five, fifteen;
41
   Platform_getLoadAverage(&this->values[0], &five, &fifteen);
42
43
44
45
46
47
   if (this->values[0] > this->total) {
      this->total = this->values[0];
   }
   snprintf(buffer, size, "%.2f", this->values[0]);
}

48
static void LoadMeter_display(Object* cast, RichString* out) {
49
50
51
   Meter* this = (Meter*)cast;
   char buffer[20];
   sprintf(buffer, "%.2f ", ((Meter*)this)->values[0]);
52
   RichString_write(out, CRT_colors[LOAD], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
53
}
54

55
56
57
58
59
60
MeterClass LoadAverageMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = LoadAverageMeter_display,
   },
61
   .setValues = LoadAverageMeter_setValues, 
62
   .defaultMode = TEXT_METERMODE,
63
   .maxItems = 3,
64
65
66
67
   .total = 100.0,
   .attributes = LoadAverageMeter_attributes,
   .name = "LoadAverage",
   .uiName = "Load average",
68
   .description = "Load averages: 1 minute, 5 minutes, 15 minutes",
69
70
71
   .caption = "Load average: "
};

72
73
74
75
76
77
MeterClass LoadMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = LoadMeter_display,
   },
78
   .setValues = LoadMeter_setValues, 
79
   .defaultMode = TEXT_METERMODE,
80
81
82
83
   .total = 100.0,
   .attributes = LoadMeter_attributes,
   .name = "Load",
   .uiName = "Load",
84
   .description = "Load: average of ready processes in the last minute",
85
86
   .caption = "Load: "
};