~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/Solaris/Memory.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
   
 
4
        Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
 
5
 
 
6
        Solaris support by Torsten Kasch <tk@Genetik.Uni-Bielefeld.DE>
 
7
    
 
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.
 
11
 
 
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.
 
16
 
 
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.
 
20
 
 
21
*/
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <unistd.h>
 
26
 
 
27
#include "config.h"
 
28
 
 
29
/* Stop <sys/swap.h> from crapping out on 32-bit architectures. */
 
30
 
 
31
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
 
32
# undef _FILE_OFFSET_BITS
 
33
# define _FILE_OFFSET_BITS 32
 
34
#endif
 
35
 
 
36
#include <sys/stat.h>
 
37
#include <sys/swap.h>
 
38
#include <vm/anon.h>
 
39
 
 
40
#ifdef HAVE_KSTAT
 
41
#include <kstat.h>
 
42
#endif
 
43
 
 
44
#include "ksysguardd.h"
 
45
#include "Command.h"
 
46
#include "Memory.h"
 
47
 
 
48
static int Dirty = 1;
 
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;
 
54
 
 
55
/*
 
56
 *  this is borrowed from top's m_sunos5 module
 
57
 *  used by permission from William LeFebvre
 
58
 */
 
59
static int pageshift;
 
60
static long (*p_pagetok) ();
 
61
#define pagetok(size) ((*p_pagetok)(size))
 
62
 
 
63
long pagetok_none( long size ) {
 
64
        return( size );
 
65
}
 
66
 
 
67
long pagetok_left( long size ) {
 
68
        return( size << pageshift );
 
69
}
 
70
 
 
71
long pagetok_right( long size ) {
 
72
        return( size >> pageshift );
 
73
}
 
74
 
 
75
void initMemory( struct SensorModul* sm ) {
 
76
 
 
77
        long i = sysconf( _SC_PAGESIZE );
 
78
 
 
79
        pageshift = 0;
 
80
        while( (i >>= 1) > 0 )
 
81
                pageshift++;
 
82
 
 
83
        /* calculate an amount to shift to K values */
 
84
        /* remember that log base 2 of 1024 is 10 (i.e.: 2^10 = 1024) */
 
85
        pageshift -= 10;
 
86
 
 
87
        /* now determine which pageshift function is appropriate for the 
 
88
        result (have to because x << y is undefined for y < 0) */
 
89
        if( pageshift > 0 ) {
 
90
                /* this is the most likely */
 
91
                p_pagetok = pagetok_left;
 
92
        } else if( pageshift == 0 ) {
 
93
                p_pagetok = pagetok_none;
 
94
        } else {
 
95
                p_pagetok = pagetok_right;
 
96
                pageshift = -pageshift;
 
97
        }
 
98
 
 
99
#ifdef HAVE_KSTAT
 
100
        registerMonitor( "mem/physical/free", "integer",
 
101
                                        printMemFree, printMemFreeInfo, sm );
 
102
        registerMonitor( "mem/physical/used", "integer",
 
103
                                        printMemUsed, printMemUsedInfo, sm );
 
104
#endif
 
105
        registerMonitor( "mem/swap/free", "integer",
 
106
                                        printSwapFree, printSwapFreeInfo, sm );
 
107
        registerMonitor( "mem/swap/used", "integer",
 
108
                                        printSwapUsed, printSwapUsedInfo, sm );
 
109
}
 
110
 
 
111
void exitMemory( void ) {
 
112
}
 
113
 
 
114
int updateMemory( void ) {
 
115
 
 
116
        long                    swaptotal;
 
117
        long                    swapfree;
 
118
        long                    swapused;
 
119
#ifdef HAVE_KSTAT
 
120
        kstat_ctl_t             *kctl;
 
121
        kstat_t                 *ksp;
 
122
        kstat_named_t           *kdata;
 
123
#endif /* HAVE_KSTAT */
 
124
        swaptotal = swapused = swapfree = 0L;
 
125
 
 
126
        /*
 
127
         *  Retrieve overall swap information from anonymous memory structure -
 
128
         *  which is the same way "swap -s" retrieves it's statistics.
 
129
         *
 
130
         *  swapctl(SC_LIST, void *arg) does not return what we are looking for.
 
131
         */
 
132
 
 
133
        if (swapctl(SC_AINFO, &am_swap) == -1)
 
134
                return(0);
 
135
 
 
136
        swaptotal = am_swap.ani_max;
 
137
        swapused = am_swap.ani_resv;
 
138
        swapfree = swaptotal - swapused;
 
139
 
 
140
        totalswap = pagetok(swaptotal);
 
141
        freeswap = pagetok(swapfree);
 
142
 
 
143
#ifdef HAVE_KSTAT
 
144
        /*
 
145
         *  get a kstat handle and update the user's kstat chain
 
146
         */
 
147
        if( (kctl = kstat_open()) == NULL )
 
148
                return( 0 );
 
149
        while( kstat_chain_update( kctl ) != 0 )
 
150
                ;
 
151
 
 
152
        totalmem = pagetok( sysconf( _SC_PHYS_PAGES ));
 
153
 
 
154
        /*
 
155
         *  traverse the kstat chain to find the appropriate statistics
 
156
         */
 
157
        if( (ksp = kstat_lookup( kctl, "unix", 0, "system_pages" )) == NULL )
 
158
                return( 0 );
 
159
        if( kstat_read( kctl, ksp, NULL ) == -1 )
 
160
                return( 0 );
 
161
 
 
162
        /*
 
163
         *  lookup the data
 
164
         */
 
165
         kdata = (kstat_named_t *) kstat_data_lookup( ksp, "freemem" );
 
166
         if( kdata != NULL )
 
167
                freemem = pagetok( kdata->value.ui32 );
 
168
 
 
169
        kstat_close( kctl );
 
170
#endif /* ! HAVE_KSTAT */
 
171
 
 
172
        Dirty = 0;
 
173
 
 
174
        return( 0 );
 
175
}
 
176
 
 
177
void printMemFreeInfo( const char *cmd ) {
 
178
        if( Dirty )
 
179
                updateMemory();
 
180
        fprintf(CurrentClient, "Free Memory\t0\t%ld\tKB\n", totalmem );
 
181
}
 
182
 
 
183
void printMemFree( const char *cmd ) {
 
184
        if( Dirty )
 
185
                updateMemory();
 
186
        fprintf(CurrentClient, "%ld\n", freemem );
 
187
}
 
188
 
 
189
void printMemUsedInfo( const char *cmd ) {
 
190
        if( Dirty )
 
191
                updateMemory();
 
192
        fprintf(CurrentClient, "Used Memory\t0\t%ld\tKB\n", totalmem );
 
193
}
 
194
 
 
195
void printMemUsed( const char *cmd ) {
 
196
        if( Dirty )
 
197
                updateMemory();
 
198
        fprintf(CurrentClient, "%ld\n", totalmem - freemem );
 
199
}
 
200
 
 
201
void printSwapFreeInfo( const char *cmd ) {
 
202
        if( Dirty )
 
203
                updateMemory();
 
204
        fprintf(CurrentClient, "Free Swap\t0\t%ld\tKB\n", totalswap );
 
205
}
 
206
 
 
207
void printSwapFree( const char *cmd ) {
 
208
        if( Dirty )
 
209
                updateMemory();
 
210
        fprintf(CurrentClient, "%ld\n", freeswap );
 
211
}
 
212
 
 
213
void printSwapUsedInfo( const char *cmd ) {
 
214
        if( Dirty )
 
215
                updateMemory();
 
216
        fprintf(CurrentClient, "Used Swap\t0\t%ld\tKB\n", totalswap );
 
217
}
 
218
 
 
219
void printSwapUsed( const char *cmd ) {
 
220
        if( Dirty )
 
221
                updateMemory();
 
222
        fprintf(CurrentClient, "%ld\n", totalswap - freeswap );
 
223
}