casacore
Loading...
Searching...
No Matches
HostInfoSolaris.h
Go to the documentation of this file.
1//# HostInfo_solaris.h: Solaris specific memory, swap, and CPU code.
2
3 /*
4 ** This is a greatly MODIFIED version of a "top" machine dependent file.
5 ** The only resemblance it bears to the original is with respect to the
6 ** mechanics of finding various system details. The copyright details
7 ** follow.
8 **
9 ** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
10 **
11 ** Top users/processes display for Unix
12 ** Version 3
13 **
14 ** This program may be freely redistributed,
15 ** but this entire comment MUST remain intact.
16 **
17 ** Copyright (c) 1984, 1989, William LeFebvre, Rice University
18 ** Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
19 ** Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
20 ** Copyright (c) 1996, William LeFebvre, Group sys Consulting
21 ** Copyright (c) 2002, Associated Universities Inc.
22 */
23
24#ifndef CASA_HOSTINFOSOLARIS_H
25#define CASA_HOSTINFOSOLARIS_H
26
27# if defined(HOSTINFO_DO_IMPLEMENT)
28
29/*
30 *
31 * LIBS: -lkstat
32 *
33 * AUTHOR: Darrell Schiebel <drs@nrao.edu>
34 *
35 * ORIGINAL AUTHORS: Torsten Kasch <torsten@techfak.uni-bielefeld.de>
36 * Robert Boucher <boucher@sofkin.ca>
37 * ORIGINAL CONTRIBUTORS: Marc Cohen <marc@aai.com>
38 * Charles Hedrick <hedrick@geneva.rutgers.edu>
39 * William L. Jones <jones@chpc>
40 * Petri Kutvonen <kutvonen@cs.helsinki.fi>
41 * Casper Dik <casper.dik@sun.com>
42 * Tim Pugh <tpugh@oce.orst.edu>
43 */
44
45#include <stdio.h>
46#include <unistd.h>
47#include <stdlib.h>
48#include <string.h>
49#include <kvm.h>
50#include <sys/types.h>
51#include <sys/sysinfo.h>
52#include <sys/swap.h>
53
54#include <kstat.h>
55
56namespace casacore { //# NAMESPACE CASACORE - BEGIN
57
58// <summary>
59// HostInfo for Solaris machines.
60// </summary>
61
62// <use visibility=local>
63
64// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
65// </reviewed>
66
67// <prerequisite>
68// <li> <linkto class=HostInfo>HostInfo</linkto>
69// </prerequisite>
70
71// <synopsis>
72// This file provides the Solaris specific functions for HostInfo.
73// It is selectively included by HostInfo.cc.
74// </synopsis>
75//
76// <group name="HostInfo">
77
78/*
79 * Some kstats are fixed at 32 bits, these will be specified as ui32; some
80 * are "natural" size (32 bit on 32 bit Solaris, 64 on 64 bit Solaris
81 * we'll make those unsigned long)
82 * Older Solaris doesn't define KSTAT_DATA_UINT32, those are always 32 bit.
83 */
84# ifndef KSTAT_DATA_UINT32
85# define ui32 ul
86# endif
87
88#ifdef SC_AINFO
89#undef USE_ANONINFO /* Use swapctl() instead */
90#endif
91
92#define NO_NPROC
93
94/* pagetok function is really a pointer to an appropriate function */
95#define pagetok(size) ((*p_pagetok)(size))
96
97#ifndef USE_ANONINFO
98static void get_swapinfo(int *total, int *fr);
99#endif
100
101class HostMachineInfo {
102friend class HostInfo;
103
104 void kupdate( );
105 HostMachineInfo( );
106 ~HostMachineInfo( );
107 void update_info( );
108
109 static int pageshift;
110 int (*p_pagetok) (int);
111
112 static inline int pagetok_none(int size) { return(size); }
113 static inline int pagetok_left(int size) { return(size << pageshift); }
114 static inline int pagetok_right(int size) { return(size >> pageshift); }
115
116 int valid;
117 kstat_ctl_t *kc;
118
119 int cpus;
120
121 ptrdiff_t memory_total;
122 ptrdiff_t memory_used;
123 ptrdiff_t memory_free;
124
125 ptrdiff_t swap_total;
126 ptrdiff_t swap_used;
127 ptrdiff_t swap_free;
128};
129
130// </group>
131
132
133int HostMachineInfo::pageshift = 0;
134
135HostMachineInfo::~HostMachineInfo( ) { if ( kc ) kstat_close(kc); }
136
137HostMachineInfo::HostMachineInfo( ) : valid(1), kc(NULL)
138{
139 int i;
140
141 /* calculate pageshift value */
142 i = sysconf(_SC_PAGESIZE);
143 pageshift = 0;
144 while ((i >>= 1) > 0) pageshift++;
145
146 /* calculate an amount to shift to K values */
147 /* remember that log base 2 of 1024 is 10 (i.e.: 2^10 = 1024) */
148 pageshift -= 10;
149
150 /* now determine which pageshift function is appropriate for the
151 result (have to because x << y is undefined for y < 0) */
152 if (pageshift > 0)
153 {
154 /* this is the most likely */
155 p_pagetok = pagetok_left;
156 }
157 else if (pageshift == 0)
158 {
159 p_pagetok = pagetok_none;
160 }
161 else
162 {
163 p_pagetok = pagetok_right;
164 pageshift = -pageshift;
165 }
166
167 long maxmem = sysconf(_SC_PHYS_PAGES);
168 memory_total = pagetok (maxmem);
169
170 /* use kstat to update all processor information */
171 kupdate( );
172
173 kstat_t *ks = kstat_lookup(kc, "unix", 0, "system_misc");
174 if (kstat_read(kc, ks, 0) == -1) {
175 perror("kstat_read");
176 valid = 0;
177 }
178 kstat_named_t *kn = (kstat_named_t*) kstat_data_lookup(ks, "ncpus");
179 cpus = kn->value.ui32;
180}
181
182void HostMachineInfo::kupdate( )
183{
184 kid_t nkcid;
185 int i;
186 static kid_t kcid = 0;
187
188 /*
189 * 0. kstat_open
190 */
191
192 if (!kc)
193 {
194 kc = kstat_open();
195 if (!kc)
196 {
197 perror("kstat_open ");
198 valid = 0;
199 }
200 kcid = kc->kc_chain_id;
201 }
202
203 /* keep doing it until no more changes */
204 kcid_changed:
205
206 /*
207 * 1. kstat_chain_update
208 */
209 nkcid = kstat_chain_update(kc);
210 if (nkcid)
211 {
212 /* UPDKCID will abort if nkcid is -1, so no need to check */
213 kcid = nkcid;
214 }
215 if (nkcid == -1) {
216 perror("kstat_read ");
217 valid = 0;
218 }
219 if (nkcid != 0)
220 goto kcid_changed;
221}
222
223
224void HostMachineInfo::update_info( )
225{
226 static long freemem;
227 static int swaptotal;
228 static int swapfree;
229 kstat_t *ks;
230 kstat_named_t *kn;
231
232 ks = kstat_lookup(kc, "unix", 0, "system_pages");
233 if (kstat_read(kc, ks, 0) == -1) {
234 perror("kstat_read");
235 valid = 0;
236 }
237 kn = (kstat_named_t*) kstat_data_lookup(ks, "freemem");
238 if (kn)
239 freemem = kn->value.ul;
240
241 memory_free = pagetok (freemem);
242 memory_used = memory_total - memory_free;
243
244 get_swapinfo(&swaptotal, &swapfree);
245 swap_total = pagetok(swaptotal);
246 swap_used = pagetok(swaptotal - swapfree);
247 swap_free = pagetok(swapfree);
248}
249
250#ifndef USE_ANONINFO
251void get_swapinfo(int *total, int *fr)
252
253{
254#ifdef SC_AINFO
255 struct anoninfo anon;
256
257 if (swapctl(SC_AINFO, &anon) == -1) {
258 *total = *fr = 0;
259 return;
260 }
261 *total = anon.ani_max;
262 *fr = anon.ani_max - anon.ani_resv;
263#else
264 int cnt, i;
265 int t, f;
266 struct swaptable *swt;
267 struct swapent *ste;
268 static char path[256];
269
270 /* get total number of swap entries */
271 cnt = swapctl(SC_GETNSWP, 0);
272
273 /* allocate enough space to hold count + n swapents */
274 swt = (struct swaptable *)malloc(sizeof(int) +
275 cnt * sizeof(struct swapent));
276 if (swt == NULL)
277 {
278 *total = 0;
279 *fr = 0;
280 return;
281 }
282 swt->swt_n = cnt;
283
284 /* fill in ste_path pointers: we don't care about the paths, so we point
285 them all to the same buffer */
286 ste = &(swt->swt_ent[0]);
287 i = cnt;
288 while (--i >= 0)
289 {
290 ste++->ste_path = path;
291 }
292
293 /* grab all swap info */
294 swapctl(SC_LIST, swt);
295
296 /* walk thru the structs and sum up the fields */
297 t = f = 0;
298 ste = &(swt->swt_ent[0]);
299 i = cnt;
300 while (--i >= 0)
301 {
302 /* dont count slots being deleted */
303 if (!(ste->ste_flags & ST_INDEL) &&
304 !(ste->ste_flags & ST_DOINGDEL))
305 {
306 t += ste->ste_pages;
307 f += ste->ste_free;
308 }
309 ste++;
310 }
311
312 /* fill in the results */
313 *total = t;
314 *fr = f;
315 free(swt);
316#endif /* SC_AINFO */
317}
318#endif /* USE_ANONINFO */
319
320
321} //# NAMESPACE CASACORE - END
322
323# endif
324#endif
free(pool)
this file contains all the compiler specific defines
Definition mainpage.dox:28