aboutsummaryrefslogtreecommitdiffstats
path: root/LoadMeter.c
blob: ce6b098e2703475e2dc8dc13c14ffae799a4e61b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
htop
(C) 2004 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "LoadMeter.h"
#include "Meter.h"

#include "ProcessList.h"

#include "debug.h"

/*{

typedef struct LoadMeter_ LoadMeter;

struct LoadMeter_ {
   Meter super;
   ProcessList* pl;
};

}*/

LoadMeter* LoadMeter_new() {
   LoadMeter* this = malloc(sizeof(LoadMeter));
   Meter_init((Meter*)this, String_copy("Load"), String_copy("Load: "), 1);
   ((Meter*)this)->attributes[0] = CRT_colors[LOAD];
   ((Meter*)this)->setValues = LoadMeter_setValues;
   ((Object*)this)->display = LoadMeter_display;
   Meter_setMode((Meter*)this, GRAPH);
   ((Meter*)this)->total = 1.0;
   return this;
}

/* private */
void LoadMeter_scan(double* one, double* five, double* fifteen) {
   int activeProcs, totalProcs, lastProc;
   FILE *fd = fopen(PROCDIR "/loadavg", "r");
   fscanf(fd, "%lf %lf %lf %d/%d %d", one, five, fifteen,
      &activeProcs, &totalProcs, &lastProc);
   fclose(fd);
}

void LoadMeter_setValues(Meter* cast) {
   double five, fifteen;
   LoadMeter_scan(&cast->values[0], &five, &fifteen);
   if (cast->values[0] > cast->total) {
      cast->total = cast->values[0];
   }
   snprintf(cast->displayBuffer.c, 7, "%.2f", cast->values[0]);
}

void LoadMeter_display(Object* cast, RichString* out) {
   Meter* super = (Meter*)cast;
   LoadMeter* this = (LoadMeter*)cast;
   char buffer[20];
   RichString_prune(out);
   sprintf(buffer, "%.2f ", ((Meter*)this)->values[0]);
   RichString_append(out, super->attributes[0], buffer);
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy