summaryrefslogtreecommitdiffstats
path: root/ksysguard/ksysguardd/Linux/Memory.c
blob: ba69a9fb60d649a90f5a949740145a27969d4eb8 (plain)
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
63
64
65
66
67
68
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
    KSysGuard, the KDE System Guard

    Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "Command.h"
#include "ksysguardd.h"

#include "Memory.h"

#define MEMINFOBUFSIZE (2 * 1024)

static char MemInfoBuf[ MEMINFOBUFSIZE ];
static int Dirty = 1;

static unsigned long Total = 0;
static unsigned long MFree = 0;
static unsigned long Appl = 0;
static unsigned long Used = 0;
static unsigned long Buffers = 0;
static unsigned long Cached = 0;
static unsigned long STotal = 0;
static unsigned long SFree = 0;
static unsigned long SUsed = 0;

static void scan_one( const char* buff, const char *key, unsigned long int* val )
{   
  int o;
  char *b = strstr( buff, key );
  if ( b )  
    o = sscanf( b + strlen( key ), ": %lu", val );
}

static void processMemInfo()
{
  scan_one( MemInfoBuf, "MemTotal", &Total );
  scan_one( MemInfoBuf, "MemFree", &MFree );
  scan_one( MemInfoBuf, "Buffers", &Buffers );
  scan_one( MemInfoBuf, "Cached", &Cached );
  scan_one( MemInfoBuf, "SwapTotal", &STotal );
  scan_one( MemInfoBuf, "SwapFree", &SFree );
  Used = Total - MFree;
  Appl = ( Used - ( Buffers + Cached ) );

  if ( STotal == 0 ) /* no swap activated */
    SUsed = 0;
  else
    SUsed = STotal - SFree;

  Dirty = 0;
}

/*
================================ public part =================================
*/

void initMemory( struct SensorModul* sm )
{
  /**
    Make sure that /proc/meminfo exists and is readable. If not we do
    not register any monitors for memory.
   */
  if ( updateMemory() < 0 )
    return;

  registerMonitor( "mem/physical/free", "integer", printMFree, printMFreeInfo, sm );
  registerMonitor( "mem/physical/used", "integer", printUsed, printUsedInfo, sm );
  registerMonitor( "mem/physical/application", "integer", printAppl, printApplInfo, sm );
  registerMonitor( "mem/physical/buf", "integer", printBuffers, printBuffersInfo, sm );
  registerMonitor( "mem/physical/cached", "integer", printCached, printCachedInfo, sm );
  registerMonitor( "mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm );
  registerMonitor( "mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm );
}

void exitMemory( void )
{
}

int updateMemory( void )
{
  /**
    The amount of total and used memory is read from the /proc/meminfo.
    It also tqcontains the information about the swap space.
    The 'file' looks like this:

    MemTotal:       516560 kB
    MemFree:          7812 kB
    MemShared:           0 kB
    Buffers:         80312 kB
    Cached:         236432 kB
    SwapCached:        468 kB
    Active:         291992 kB
    Inactive:       133556 kB
    HighTotal:           0 kB
    HighFree:            0 kB
    LowTotal:       516560 kB
    LowFree:          7812 kB
    SwapTotal:      899632 kB
    SwapFree:       898932 kB
    Dirty:            2736 kB
    Writeback:           0 kB
    Mapped:         155996 kB
    Slab:            73920 kB
    Committed_AS:   315588 kB
    PageTables:       1764 kB
    ReverseMaps:    103458
   */

  int fd;
  size_t n;

  if ( ( fd = open( "/proc/meminfo", O_RDONLY ) ) < 0 ) {
    print_error( "Cannot open \'/proc/meminfo\'!\n"
                 "The kernel needs to be compiled with support\n"
                 "for /proc filesystem enabled!\n" );
    return -1;
  }

  if ( ( n = read( fd, MemInfoBuf, MEMINFOBUFSIZE - 1 ) ) == MEMINFOBUFSIZE - 1 ) {
    log_error( "Internal buffer too small to read \'/proc/mem\'" );
    close( fd );
    return -1;
  }

  close( fd );
  MemInfoBuf[ n ] = '\0';
  Dirty = 1;

  return 0;
}

void printMFree( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "%ld\n", MFree );
}

void printMFreeInfo( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "Free Memory\t0\t%ld\tKB\n", Total );
}

void printUsed( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "%ld\n", Used );
}

void printUsedInfo( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "Used Memory\t0\t%ld\tKB\n", Total );
}

void printAppl( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "%ld\n", Appl );
}

void printApplInfo( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "Application Memory\t0\t%ld\tKB\n", Total );
}

void printBuffers( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "%ld\n", Buffers );
}

void printBuffersInfo( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "Buffer Memory\t0\t%ld\tKB\n", Total );
}

void printCached( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "%ld\n", Cached );
}

void printCachedInfo( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "Cached Memory\t0\t%ld\tKB\n", Total );
}

void printSwapUsed( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "%ld\n", SUsed );
}

void printSwapUsedInfo( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "Used Swap Memory\t0\t%ld\tKB\n", STotal );
}

void printSwapFree( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "%ld\n", SFree );
}

void printSwapFreeInfo( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processMemInfo();

  fprintf( CurrentClient, "Free Swap Memory\t0\t%ld\tKB\n", STotal );
}