~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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
#include <sys/stat.h>
 
27
#include <sys/swap.h>
 
28
 
 
29
#include "config.h"
 
30
 
 
31
#ifdef HAVE_KSTAT
 
32
#include <kstat.h>
 
33
#endif
 
34
 
 
35
#include "ksysguardd.h"
 
36
#include "Command.h"
 
37
#include "Memory.h"
 
38
 
 
39
static int Dirty = 1;
 
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;
 
44
 
 
45
/*
 
46
 *  this is borrowed from top's m_sunos5 module
 
47
 *  used by permission from William LeFebvre
 
48
 */
 
49
static int pageshift;
 
50
static long (*p_pagetok) ();
 
51
#define pagetok(size) ((*p_pagetok)(size))
 
52
 
 
53
long pagetok_none( long size ) {
 
54
        return( size );
 
55
}
 
56
 
 
57
long pagetok_left( long size ) {
 
58
        return( size << pageshift );
 
59
}
 
60
 
 
61
long pagetok_right( long size ) {
 
62
        return( size >> pageshift );
 
63
}
 
64
 
 
65
void initMemory( struct SensorModul* sm ) {
 
66
 
 
67
        long i = sysconf( _SC_PAGESIZE );
 
68
 
 
69
        pageshift = 0;
 
70
        while( (i >>= 1) > 0 )
 
71
                pageshift++;
 
72
 
 
73
        /* calculate an amount to shift to K values */
 
74
        /* remember that log base 2 of 1024 is 10 (i.e.: 2^10 = 1024) */
 
75
        pageshift -= 10;
 
76
 
 
77
        /* now determine which pageshift function is appropriate for the 
 
78
        result (have to because x << y is undefined for y < 0) */
 
79
        if( pageshift > 0 ) {
 
80
                /* this is the most likely */
 
81
                p_pagetok = pagetok_left;
 
82
        } else if( pageshift == 0 ) {
 
83
                p_pagetok = pagetok_none;
 
84
        } else {
 
85
                p_pagetok = pagetok_right;
 
86
                pageshift = -pageshift;
 
87
        }
 
88
 
 
89
#ifdef HAVE_KSTAT
 
90
        registerMonitor( "mem/physical/free", "integer",
 
91
                                        printMemFree, printMemFreeInfo, sm );
 
92
        registerMonitor( "mem/physical/used", "integer",
 
93
                                        printMemUsed, printMemUsedInfo, sm );
 
94
#endif
 
95
        registerMonitor( "mem/swap/free", "integer",
 
96
                                        printSwapFree, printSwapFreeInfo, sm );
 
97
        registerMonitor( "mem/swap/used", "integer",
 
98
                                        printSwapUsed, printSwapUsedInfo, sm );
 
99
}
 
100
 
 
101
void exitMemory( void ) {
 
102
}
 
103
 
 
104
int updateMemory( void ) {
 
105
 
 
106
        struct swaptable        *swt;
 
107
        struct swapent          *ste;
 
108
        int                     i;
 
109
        int                     ndevs;
 
110
        long                    swaptotal;
 
111
        long                    swapfree;
 
112
        char                    dummy[128];
 
113
#ifdef HAVE_KSTAT
 
114
        kstat_ctl_t             *kctl;
 
115
        kstat_t                 *ksp;
 
116
        kstat_named_t           *kdata;
 
117
#endif /* HAVE_KSTAT */
 
118
 
 
119
        if( (ndevs = swapctl( SC_GETNSWP, NULL )) < 1 )
 
120
                return( 0 );
 
121
        if( (swt = (struct swaptable *) malloc(
 
122
                        sizeof( int )
 
123
                        + ndevs * sizeof( struct swapent ))) == NULL )
 
124
                return( 0 );
 
125
 
 
126
        /*
 
127
         *  fill in the required fields and retrieve the info thru swapctl()
 
128
         */
 
129
        swt->swt_n = ndevs;
 
130
        ste = &(swt->swt_ent[0]);
 
131
        for( i = 0; i < ndevs; i++ ) {
 
132
                /*
 
133
                 *  since we'renot interested in the path(s),
 
134
                 *  we'll re-use the same buffer
 
135
                 */
 
136
                ste->ste_path = dummy;
 
137
                ste++;
 
138
        }
 
139
        swapctl( SC_LIST, swt );
 
140
 
 
141
        swaptotal = swapfree = 0L;
 
142
 
 
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;
 
149
                }
 
150
                ste++;
 
151
        }
 
152
        free( swt );
 
153
 
 
154
        totalswap = pagetok( swaptotal );
 
155
        freeswap = pagetok( swapfree );
 
156
 
 
157
#ifdef HAVE_KSTAT
 
158
        /*
 
159
         *  get a kstat handle and update the user's kstat chain
 
160
         */
 
161
        if( (kctl = kstat_open()) == NULL )
 
162
                return( 0 );
 
163
        while( kstat_chain_update( kctl ) != 0 )
 
164
                ;
 
165
 
 
166
        totalmem = pagetok( sysconf( _SC_PHYS_PAGES ));
 
167
 
 
168
        /*
 
169
         *  traverse the kstat chain to find the appropriate statistics
 
170
         */
 
171
        if( (ksp = kstat_lookup( kctl, "unix", 0, "system_pages" )) == NULL )
 
172
                return( 0 );
 
173
        if( kstat_read( kctl, ksp, NULL ) == -1 )
 
174
                return( 0 );
 
175
 
 
176
        /*
 
177
         *  lookup the data
 
178
         */
 
179
         kdata = (kstat_named_t *) kstat_data_lookup( ksp, "freemem" );
 
180
         if( kdata != NULL )
 
181
                freemem = pagetok( kdata->value.ui32 );
 
182
 
 
183
        kstat_close( kctl );
 
184
#endif /* ! HAVE_KSTAT */
 
185
 
 
186
        Dirty = 0;
 
187
 
 
188
        return( 0 );
 
189
}
 
190
 
 
191
void printMemFreeInfo( const char *cmd ) {
 
192
        if( Dirty )
 
193
                updateMemory();
 
194
        fprintf(CurrentClient, "Free Memory\t0\t%ld\tKB\n", totalmem );
 
195
}
 
196
 
 
197
void printMemFree( const char *cmd ) {
 
198
        if( Dirty )
 
199
                updateMemory();
 
200
        fprintf(CurrentClient, "%ld\n", freemem );
 
201
}
 
202
 
 
203
void printMemUsedInfo( const char *cmd ) {
 
204
        if( Dirty )
 
205
                updateMemory();
 
206
        fprintf(CurrentClient, "Used Memory\t0\t%ld\tKB\n", totalmem );
 
207
}
 
208
 
 
209
void printMemUsed( const char *cmd ) {
 
210
        if( Dirty )
 
211
                updateMemory();
 
212
        fprintf(CurrentClient, "%ld\n", totalmem - freemem );
 
213
}
 
214
 
 
215
void printSwapFreeInfo( const char *cmd ) {
 
216
        if( Dirty )
 
217
                updateMemory();
 
218
        fprintf(CurrentClient, "Free Swap\t0\t%ld\tKB\n", totalswap );
 
219
}
 
220
 
 
221
void printSwapFree( const char *cmd ) {
 
222
        if( Dirty )
 
223
                updateMemory();
 
224
        fprintf(CurrentClient, "%ld\n", freeswap );
 
225
}
 
226
 
 
227
void printSwapUsedInfo( const char *cmd ) {
 
228
        if( Dirty )
 
229
                updateMemory();
 
230
        fprintf(CurrentClient, "Used Swap\t0\t%ld\tKB\n", totalswap );
 
231
}
 
232
 
 
233
void printSwapUsed( const char *cmd ) {
 
234
        if( Dirty )
 
235
                updateMemory();
 
236
        fprintf(CurrentClient, "%ld\n", totalswap - freeswap );
 
237
}