2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of version 2 of the GNU General Public
8
License as published by the Free Software Foundation.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <sys/types.h>
30
#include "ksysguardd.h"
34
#define MEMINFOBUFSIZE (2 * 1024)
36
static char MemInfoBuf[ MEMINFOBUFSIZE ];
39
static unsigned long long Total = 0;
40
static unsigned long long MFree = 0;
41
static unsigned long long Appl = 0;
42
static unsigned long long Used = 0;
43
static unsigned long long Buffers = 0;
44
static unsigned long long Cached = 0;
45
static unsigned long long STotal = 0;
46
static unsigned long long SFree = 0;
47
static unsigned long long SUsed = 0;
49
static void scan_one( const char* buff, const char *key, unsigned long long* val )
52
char *b = strstr( buff, key );
54
o = sscanf( b + strlen( key ), ": %llu", val );
57
static void processMemInfo()
59
scan_one( MemInfoBuf, "MemTotal", &Total );
60
scan_one( MemInfoBuf, "MemFree", &MFree );
61
scan_one( MemInfoBuf, "Buffers", &Buffers );
62
scan_one( MemInfoBuf, "Cached", &Cached );
63
scan_one( MemInfoBuf, "SwapTotal", &STotal );
64
scan_one( MemInfoBuf, "SwapFree", &SFree );
66
Appl = ( Used - ( Buffers + Cached ) );
68
if ( STotal == 0 ) /* no swap activated */
71
SUsed = STotal - SFree;
77
================================ public part =================================
80
void initMemory( struct SensorModul* sm )
83
Make sure that /proc/meminfo exists and is readable. If not we do
84
not register any monitors for memory.
86
if ( updateMemory() < 0 )
89
registerMonitor( "mem/physical/free", "integer", printMFree, printMFreeInfo, sm );
90
registerMonitor( "mem/physical/used", "integer", printUsed, printUsedInfo, sm );
91
registerMonitor( "mem/physical/application", "integer", printAppl, printApplInfo, sm );
92
registerMonitor( "mem/physical/buf", "integer", printBuffers, printBuffersInfo, sm );
93
registerMonitor( "mem/physical/cached", "integer", printCached, printCachedInfo, sm );
94
registerMonitor( "mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm );
95
registerMonitor( "mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm );
98
void exitMemory( void )
102
int updateMemory( void )
105
The amount of total and used memory is read from the /proc/meminfo.
106
It also contains the information about the swap space.
107
The 'file' looks like this:
127
Committed_AS: 315588 kB
135
if ( ( fd = open( "/proc/meminfo", O_RDONLY ) ) < 0 ) {
136
print_error( "Cannot open \'/proc/meminfo\'!\n"
137
"The kernel needs to be compiled with support\n"
138
"for /proc file system enabled!\n" );
142
n = read( fd, MemInfoBuf, MEMINFOBUFSIZE - 1 );
143
if ( n == MEMINFOBUFSIZE - 1 || n <= 0 ) {
144
log_error( "Internal buffer too small to read \'/proc/meminfo\'" );
150
MemInfoBuf[ n ] = '\0';
156
void printMFree( const char* cmd )
163
output( "%llu\n", MFree );
166
void printMFreeInfo( const char* cmd )
173
output( "Free Memory\t0\t%llu\tKB\n", Total );
176
void printUsed( const char* cmd )
183
output( "%llu\n", Used );
186
void printUsedInfo( const char* cmd )
193
output( "Used Memory\t0\t%llu\tKB\n", Total );
196
void printAppl( const char* cmd )
203
output( "%llu\n", Appl );
206
void printApplInfo( const char* cmd )
213
output( "Application Memory\t0\t%llu\tKB\n", Total );
216
void printBuffers( const char* cmd )
223
output( "%llu\n", Buffers );
226
void printBuffersInfo( const char* cmd )
233
output( "Buffer Memory\t0\t%llu\tKB\n", Total );
236
void printCached( const char* cmd )
243
output( "%llu\n", Cached );
246
void printCachedInfo( const char* cmd )
253
output( "Cached Memory\t0\t%llu\tKB\n", Total );
256
void printSwapUsed( const char* cmd )
263
output( "%llu\n", SUsed );
266
void printSwapUsedInfo( const char* cmd )
273
output( "Used Swap Memory\t0\t%llu\tKB\n", STotal );
276
void printSwapFree( const char* cmd )
283
output( "%llu\n", SFree );
286
void printSwapFreeInfo( const char* cmd )
293
output( "Free Swap Memory\t0\t%llu\tKB\n", STotal );