~ubuntu-branches/ubuntu/dapper/htop/dapper

« back to all changes in this revision

Viewing changes to MemoryMeter.c

  • Committer: Bazaar Package Importer
  • Author(s): Bartosz Fenski
  • Date: 2004-11-27 10:10:17 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041127101017-vwknw2ipfvxchno4
Tags: 0.5-1
* New upstream version.
  - fixes problem with wrongly displayed CPU bar (Closes: #283212)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
htop
 
3
(C) 2004 Hisham H. Muhammad
 
4
Released under the GNU GPL, see the COPYING file
 
5
in the source distribution for its full text.
 
6
*/
 
7
 
 
8
#include "MemoryMeter.h"
 
9
#include "Meter.h"
 
10
 
 
11
#include "ProcessList.h"
 
12
 
 
13
#include <stdlib.h>
 
14
#include <curses.h>
 
15
#include <string.h>
 
16
#include <math.h>
 
17
#include <sys/param.h>
 
18
 
 
19
#include "debug.h"
 
20
#include <assert.h>
 
21
 
 
22
/*{
 
23
 
 
24
typedef struct MemoryMeter_ MemoryMeter;
 
25
 
 
26
struct MemoryMeter_ {
 
27
   Meter super;
 
28
   ProcessList* pl;
 
29
   char* wideFormat;
 
30
   int wideLimit;
 
31
};
 
32
 
 
33
}*/
 
34
 
 
35
MemoryMeter* MemoryMeter_new(ProcessList* pl) {
 
36
   MemoryMeter* this = malloc(sizeof(MemoryMeter));
 
37
   Meter_init((Meter*)this, String_copy("Memory"), String_copy("Mem"), 3);
 
38
   ((Meter*)this)->attributes[0] = CRT_colors[MEMORY_USED];
 
39
   ((Meter*)this)->attributes[1] = CRT_colors[MEMORY_BUFFERS];
 
40
   ((Meter*)this)->attributes[2] = CRT_colors[MEMORY_CACHE];
 
41
   ((Meter*)this)->setValues = MemoryMeter_setValues;
 
42
   ((Object*)this)->display = MemoryMeter_display;
 
43
   this->pl = pl;
 
44
   Meter_setMode((Meter*)this, BAR);
 
45
   if (this->pl->totalMem > 1000000) {
 
46
      this->wideFormat = "%7ldk ";
 
47
      this->wideLimit = 22 + 9 * 4;
 
48
   } else if (this->pl->totalMem > 100000) {
 
49
      this->wideFormat = "%6ldk ";
 
50
      this->wideLimit = 22 + 8 * 4;
 
51
   } else {
 
52
      this->wideFormat = "%5ldk ";
 
53
      this->wideLimit = 22 + 7 * 4;
 
54
   }
 
55
   return this;
 
56
}
 
57
 
 
58
void MemoryMeter_setValues(Meter* cast) {
 
59
   MemoryMeter* this = (MemoryMeter*)cast;
 
60
 
 
61
   double totalMem = (double)this->pl->totalMem;
 
62
   long int usedMem = this->pl->usedMem;
 
63
   long int buffersMem = this->pl->buffersMem;
 
64
   long int cachedMem = this->pl->cachedMem;
 
65
   usedMem -= buffersMem + cachedMem;
 
66
   cast->total = totalMem;
 
67
   cast->values[0] = usedMem;
 
68
   cast->values[1] = buffersMem;
 
69
   cast->values[2] = cachedMem;
 
70
   snprintf(cast->displayBuffer.c, 14, "%ld/%ldMB", usedMem / 1024, this->pl->totalMem / 1024);
 
71
}
 
72
 
 
73
void MemoryMeter_display(Object* cast, RichString* out) {
 
74
   char buffer[50];
 
75
   MemoryMeter* this = (MemoryMeter*)cast;
 
76
   Meter* meter = (Meter*)cast;
 
77
   int div = 1024; char* format = "%ldM ";
 
78
   if (meter->w > this->wideLimit) {
 
79
      div = 1; format = this->wideFormat;
 
80
   }
 
81
   long int totalMem = meter->total / div;
 
82
   long int usedMem = meter->values[0] / div;
 
83
   long int buffersMem = meter->values[1] / div;
 
84
   long int cachedMem = meter->values[2] / div;
 
85
   RichString_prune(out);
 
86
   RichString_append(out, CRT_colors[METER_TEXT], ":");
 
87
   sprintf(buffer, format, totalMem);
 
88
   RichString_append(out, CRT_colors[METER_VALUE], buffer);
 
89
   sprintf(buffer, format, usedMem);
 
90
   RichString_append(out, CRT_colors[METER_TEXT], "used:");
 
91
   RichString_append(out, meter->attributes[0], buffer);
 
92
   sprintf(buffer, format, buffersMem);
 
93
   RichString_append(out, CRT_colors[METER_TEXT], "buffers:");
 
94
   RichString_append(out, meter->attributes[1], buffer);
 
95
   sprintf(buffer, format, cachedMem);
 
96
   RichString_append(out, CRT_colors[METER_TEXT], "cache:");
 
97
   RichString_append(out, meter->attributes[2], buffer);
 
98
}