2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
6
Solaris support by Torsten Kasch <tk@Genetik.Uni-Bielefeld.DE>
8
This program is free software; you can redistribute it and/or
9
modify it under the terms of version 2 of the GNU General Public
10
License as published by the Free Software Foundation.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29
/* Stop <sys/swap.h> from crapping out on 32-bit architectures. */
31
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
32
# undef _FILE_OFFSET_BITS
33
# define _FILE_OFFSET_BITS 32
44
#include "ksysguardd.h"
49
static t_memsize totalmem = (t_memsize) 0;
50
static t_memsize freemem = (t_memsize) 0;
51
static long totalswap = 0L;
52
static long freeswap = 0L;
53
static struct anoninfo am_swap;
56
* this is borrowed from top's m_sunos5 module
57
* used by permission from William LeFebvre
60
static long (*p_pagetok) ();
61
#define pagetok(size) ((*p_pagetok)(size))
63
long pagetok_none( long size ) {
67
long pagetok_left( long size ) {
68
return( size << pageshift );
71
long pagetok_right( long size ) {
72
return( size >> pageshift );
75
void initMemory( struct SensorModul* sm ) {
77
long i = sysconf( _SC_PAGESIZE );
80
while( (i >>= 1) > 0 )
83
/* calculate an amount to shift to K values */
84
/* remember that log base 2 of 1024 is 10 (i.e.: 2^10 = 1024) */
87
/* now determine which pageshift function is appropriate for the
88
result (have to because x << y is undefined for y < 0) */
90
/* this is the most likely */
91
p_pagetok = pagetok_left;
92
} else if( pageshift == 0 ) {
93
p_pagetok = pagetok_none;
95
p_pagetok = pagetok_right;
96
pageshift = -pageshift;
100
registerMonitor( "mem/physical/free", "integer",
101
printMemFree, printMemFreeInfo, sm );
102
registerMonitor( "mem/physical/used", "integer",
103
printMemUsed, printMemUsedInfo, sm );
105
registerMonitor( "mem/swap/free", "integer",
106
printSwapFree, printSwapFreeInfo, sm );
107
registerMonitor( "mem/swap/used", "integer",
108
printSwapUsed, printSwapUsedInfo, sm );
111
void exitMemory( void ) {
114
int updateMemory( void ) {
122
kstat_named_t *kdata;
123
#endif /* HAVE_KSTAT */
124
swaptotal = swapused = swapfree = 0L;
127
* Retrieve overall swap information from anonymous memory structure -
128
* which is the same way "swap -s" retrieves it's statistics.
130
* swapctl(SC_LIST, void *arg) does not return what we are looking for.
133
if (swapctl(SC_AINFO, &am_swap) == -1)
136
swaptotal = am_swap.ani_max;
137
swapused = am_swap.ani_resv;
138
swapfree = swaptotal - swapused;
140
totalswap = pagetok(swaptotal);
141
freeswap = pagetok(swapfree);
145
* get a kstat handle and update the user's kstat chain
147
if( (kctl = kstat_open()) == NULL )
149
while( kstat_chain_update( kctl ) != 0 )
152
totalmem = pagetok( sysconf( _SC_PHYS_PAGES ));
155
* traverse the kstat chain to find the appropriate statistics
157
if( (ksp = kstat_lookup( kctl, "unix", 0, "system_pages" )) == NULL )
159
if( kstat_read( kctl, ksp, NULL ) == -1 )
165
kdata = (kstat_named_t *) kstat_data_lookup( ksp, "freemem" );
167
freemem = pagetok( kdata->value.ui32 );
170
#endif /* ! HAVE_KSTAT */
177
void printMemFreeInfo( const char *cmd ) {
180
fprintf(CurrentClient, "Free Memory\t0\t%ld\tKB\n", totalmem );
183
void printMemFree( const char *cmd ) {
186
fprintf(CurrentClient, "%ld\n", freemem );
189
void printMemUsedInfo( const char *cmd ) {
192
fprintf(CurrentClient, "Used Memory\t0\t%ld\tKB\n", totalmem );
195
void printMemUsed( const char *cmd ) {
198
fprintf(CurrentClient, "%ld\n", totalmem - freemem );
201
void printSwapFreeInfo( const char *cmd ) {
204
fprintf(CurrentClient, "Free Swap\t0\t%ld\tKB\n", totalswap );
207
void printSwapFree( const char *cmd ) {
210
fprintf(CurrentClient, "%ld\n", freeswap );
213
void printSwapUsedInfo( const char *cmd ) {
216
fprintf(CurrentClient, "Used Swap\t0\t%ld\tKB\n", totalswap );
219
void printSwapUsed( const char *cmd ) {
222
fprintf(CurrentClient, "%ld\n", totalswap - freeswap );