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 {
102 friend 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
132int HostMachineInfo::pageshift = 0;
133
134HostMachineInfo::~HostMachineInfo() {
135 if (kc) kstat_close(kc);
136}
137
138HostMachineInfo::HostMachineInfo() : valid(1), kc(NULL) {
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 /* this is the most likely */
154 p_pagetok = pagetok_left;
155 } else if (pageshift == 0) {
156 p_pagetok = pagetok_none;
157 } else {
158 p_pagetok = pagetok_right;
159 pageshift = -pageshift;
160 }
161
162 long maxmem = sysconf(_SC_PHYS_PAGES);
163 memory_total = pagetok(maxmem);
164
165 /* use kstat to update all processor information */
166 kupdate();
167
168 kstat_t *ks = kstat_lookup(kc, "unix", 0, "system_misc");
169 if (kstat_read(kc, ks, 0) == -1) {
170 perror("kstat_read");
171 valid = 0;
172 }
173 kstat_named_t *kn = (kstat_named_t *)kstat_data_lookup(ks, "ncpus");
174 cpus = kn->value.ui32;
175}
176
177void HostMachineInfo::kupdate() {
178 kid_t nkcid;
179 int i;
180 static kid_t kcid = 0;
181
182 /*
183 * 0. kstat_open
184 */
185
186 if (!kc) {
187 kc = kstat_open();
188 if (!kc) {
189 perror("kstat_open ");
190 valid = 0;
191 }
192 kcid = kc->kc_chain_id;
193 }
194
195 /* keep doing it until no more changes */
196kcid_changed:
197
198 /*
199 * 1. kstat_chain_update
200 */
201 nkcid = kstat_chain_update(kc);
202 if (nkcid) {
203 /* UPDKCID will abort if nkcid is -1, so no need to check */
204 kcid = nkcid;
205 }
206 if (nkcid == -1) {
207 perror("kstat_read ");
208 valid = 0;
209 }
210 if (nkcid != 0) goto kcid_changed;
211}
212
213void HostMachineInfo::update_info() {
214 static long freemem;
215 static int swaptotal;
216 static int swapfree;
217 kstat_t *ks;
218 kstat_named_t *kn;
219
220 ks = kstat_lookup(kc, "unix", 0, "system_pages");
221 if (kstat_read(kc, ks, 0) == -1) {
222 perror("kstat_read");
223 valid = 0;
224 }
225 kn = (kstat_named_t *)kstat_data_lookup(ks, "freemem");
226 if (kn) freemem = kn->value.ul;
227
228 memory_free = pagetok(freemem);
229 memory_used = memory_total - memory_free;
230
231 get_swapinfo(&swaptotal, &swapfree);
232 swap_total = pagetok(swaptotal);
233 swap_used = pagetok(swaptotal - swapfree);
234 swap_free = pagetok(swapfree);
235}
236
237#ifndef USE_ANONINFO
238void get_swapinfo(int *total, int *fr)
239
240{
241#ifdef SC_AINFO
242 struct anoninfo anon;
243
244 if (swapctl(SC_AINFO, &anon) == -1) {
245 *total = *fr = 0;
246 return;
247 }
248 *total = anon.ani_max;
249 *fr = anon.ani_max - anon.ani_resv;
250#else
251 int cnt, i;
252 int t, f;
253 struct swaptable *swt;
254 struct swapent *ste;
255 static char path[256];
256
257 /* get total number of swap entries */
258 cnt = swapctl(SC_GETNSWP, 0);
259
260 /* allocate enough space to hold count + n swapents */
261 swt = (struct swaptable *)malloc(sizeof(int) + cnt * sizeof(struct swapent));
262 if (swt == NULL) {
263 *total = 0;
264 *fr = 0;
265 return;
266 }
267 swt->swt_n = cnt;
268
269 /* fill in ste_path pointers: we don't care about the paths, so we point
270 them all to the same buffer */
271 ste = &(swt->swt_ent[0]);
272 i = cnt;
273 while (--i >= 0) {
274 ste++->ste_path = path;
275 }
276
277 /* grab all swap info */
278 swapctl(SC_LIST, swt);
279
280 /* walk thru the structs and sum up the fields */
281 t = f = 0;
282 ste = &(swt->swt_ent[0]);
283 i = cnt;
284 while (--i >= 0) {
285 /* dont count slots being deleted */
286 if (!(ste->ste_flags & ST_INDEL) && !(ste->ste_flags & ST_DOINGDEL)) {
287 t += ste->ste_pages;
288 f += ste->ste_free;
289 }
290 ste++;
291 }
292
293 /* fill in the results */
294 *total = t;
295 *fr = f;
296 free(swt);
297#endif /* SC_AINFO */
298}
299#endif /* USE_ANONINFO */
300
301} // namespace casacore
302
303#endif
304#endif
free(pool)
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28