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.
35
#include "ksysguardd.h"
40
static t_memsize totalmem = (t_memsize) 0;
41
static t_memsize freemem = (t_memsize) 0;
42
static long totalswap = 0L;
43
static long freeswap = 0L;
46
* this is borrowed from top's m_sunos5 module
47
* used by permission from William LeFebvre
50
static long (*p_pagetok) ();
51
#define pagetok(size) ((*p_pagetok)(size))
53
long pagetok_none( long size ) {
57
long pagetok_left( long size ) {
58
return( size << pageshift );
61
long pagetok_right( long size ) {
62
return( size >> pageshift );
65
void initMemory( struct SensorModul* sm ) {
67
long i = sysconf( _SC_PAGESIZE );
70
while( (i >>= 1) > 0 )
73
/* calculate an amount to shift to K values */
74
/* remember that log base 2 of 1024 is 10 (i.e.: 2^10 = 1024) */
77
/* now determine which pageshift function is appropriate for the
78
result (have to because x << y is undefined for y < 0) */
80
/* this is the most likely */
81
p_pagetok = pagetok_left;
82
} else if( pageshift == 0 ) {
83
p_pagetok = pagetok_none;
85
p_pagetok = pagetok_right;
86
pageshift = -pageshift;
90
registerMonitor( "mem/physical/free", "integer",
91
printMemFree, printMemFreeInfo, sm );
92
registerMonitor( "mem/physical/used", "integer",
93
printMemUsed, printMemUsedInfo, sm );
95
registerMonitor( "mem/swap/free", "integer",
96
printSwapFree, printSwapFreeInfo, sm );
97
registerMonitor( "mem/swap/used", "integer",
98
printSwapUsed, printSwapUsedInfo, sm );
101
void exitMemory( void ) {
104
int updateMemory( void ) {
106
struct swaptable *swt;
116
kstat_named_t *kdata;
117
#endif /* HAVE_KSTAT */
119
if( (ndevs = swapctl( SC_GETNSWP, NULL )) < 1 )
121
if( (swt = (struct swaptable *) malloc(
123
+ ndevs * sizeof( struct swapent ))) == NULL )
127
* fill in the required fields and retrieve the info thru swapctl()
130
ste = &(swt->swt_ent[0]);
131
for( i = 0; i < ndevs; i++ ) {
133
* since we'renot interested in the path(s),
134
* we'll re-use the same buffer
136
ste->ste_path = dummy;
139
swapctl( SC_LIST, swt );
141
swaptotal = swapfree = 0L;
143
ste = &(swt->swt_ent[0]);
144
for( i = 0; i < ndevs; i++ ) {
145
if( (! (ste->ste_flags & ST_INDEL))
146
&& (! (ste->ste_flags & ST_DOINGDEL)) ) {
147
swaptotal += ste->ste_pages;
148
swapfree += ste->ste_free;
154
totalswap = pagetok( swaptotal );
155
freeswap = pagetok( swapfree );
159
* get a kstat handle and update the user's kstat chain
161
if( (kctl = kstat_open()) == NULL )
163
while( kstat_chain_update( kctl ) != 0 )
166
totalmem = pagetok( sysconf( _SC_PHYS_PAGES ));
169
* traverse the kstat chain to find the appropriate statistics
171
if( (ksp = kstat_lookup( kctl, "unix", 0, "system_pages" )) == NULL )
173
if( kstat_read( kctl, ksp, NULL ) == -1 )
179
kdata = (kstat_named_t *) kstat_data_lookup( ksp, "freemem" );
181
freemem = pagetok( kdata->value.ui32 );
184
#endif /* ! HAVE_KSTAT */
191
void printMemFreeInfo( const char *cmd ) {
194
fprintf(CurrentClient, "Free Memory\t0\t%ld\tKB\n", totalmem );
197
void printMemFree( const char *cmd ) {
200
fprintf(CurrentClient, "%ld\n", freemem );
203
void printMemUsedInfo( const char *cmd ) {
206
fprintf(CurrentClient, "Used Memory\t0\t%ld\tKB\n", totalmem );
209
void printMemUsed( const char *cmd ) {
212
fprintf(CurrentClient, "%ld\n", totalmem - freemem );
215
void printSwapFreeInfo( const char *cmd ) {
218
fprintf(CurrentClient, "Free Swap\t0\t%ld\tKB\n", totalswap );
221
void printSwapFree( const char *cmd ) {
224
fprintf(CurrentClient, "%ld\n", freeswap );
227
void printSwapUsedInfo( const char *cmd ) {
230
fprintf(CurrentClient, "Used Swap\t0\t%ld\tKB\n", totalswap );
233
void printSwapUsed( const char *cmd ) {
236
fprintf(CurrentClient, "%ld\n", totalswap - freeswap );