comparison src/getloadavg.c @ 5494:861f2601a38b

Sync s/ files with GNU 23.1.92, delete obsolete stuff, simplify other stuff --- ChangeLog 2010-02-20 Ben Wing <ben@xemacs.org> * configure: * configure.ac (XE_EXPAND_VARIABLE): * configure.ac (TAB): irix6-0.h renamed to irix6-5.h; sco7.h deleted. bsd4-3.h renamed to bsd-common.h. Simplify setting machine= for intel386, mips. Put back linux.h. Incorporate some stuff about debug/optimization flags in irix6-5.h into configure.ac. Add a comment about LCC stuff with optimization flags that should be fixed up. --- src/ChangeLog 2010-02-20 Ben Wing <ben@xemacs.org> * sysdep.c: * sysdep.c (qxe_reliable_signal): * sysdep.c (retry_open_1): * sysdep.c (retry_close): * sysdep.c (retry_read_1): * sysdep.c (retry_write_1): * sysdep.c (retry_fopen): * sysdep.c (retry_fclose): * sysdep.c (retry_fread): * sysdep.c (retry_fwrite): Remove conditionalization on INTERRUPTIBLE_IO, INTERRUPTIBLE_OPEN, INTERRUPTIBLE_CLOSE -- always check for EINTR as there's no harm in it. Remove old #if 0'ed out SA_RESTART code. Use manifest constant IRIX6_5 instead of IRIX. * Makefile.in.in: * dired.c: * dired.c (Ffile_attributes): Use manifest constant IRIX6_5 instead of IRIX. Eliminate constant BSD4_2, use BSD4_3 instead. * getloadavg.c: * getloadavg.c (getloadavg): * getloadavg.c (LDAV_PRIVILEGED): * getloadavg.c (LDAV_DONE): Sync with GNU 23.1.92. 2010-02-20 Ben Wing <ben@xemacs.org> * s/README: * s/aix4-2.h: * s/aix4-2.h (MAIL_USE_LOCKF): * s/bsd-common.h: * s/freebsd.h: * s/freebsd.h (Carey): * s/freebsd.h (or): * s/gnu.h: * s/gnu.h (DATA_START): * s/hpux11-shr.h: * s/hpux11.h: * s/hpux11.h (random): * s/irix6-5.h: * s/linux.h: * s/mach-bsd4-3.h: * s/netbsd.h: * s/netbsd.h (or): * s/netbsd.h (A_TEXT_OFFSET): * s/netbsd.h (NO_MATHERR): * s/openbsd.h: * s/sol2.h: * s/usg5-4-2.h: * s/usg5-4.h: * s/usg5-4.h (or): * s/usg5-4.h (ORDINARY_LINK): Sync with GNU 23.1.92. Put back linux.h. Rename: bsd4-3.h -> bsd-common.h; irix6-0.h -> irix6-5.h. Delete: template.h (useless). Delete a whole bunch of obsolete stuff in sol2.h, linux.h, freebsd.h -- assume anything over 10 years old is sufficiently obsolete to be deleted. Remove LIBS_DEBUG, C_DEBUG_SWITCH, C_OPTIMIZE_SWITCH, KERNEL_FILE, LDAV_SYMBOL, most remaining PTY stuff, remaining TERMINFO/TERMCAP stuff. Update README appropriately. Remove stuff in hpux11-shr.h duplicated in hpux11.h. Remove sco7.h, identical with usg5-4-2.h. Remove unused POSIX flag, BROKEN_TIOC*, NO_SIOCTL_H. 2010-02-20 Ben Wing <ben@xemacs.org> * m/arm.h: * m/hp800.h: * m/intel386.h: * m/mips.h: * m/powerpc.h: * m/sparc.h: * m/template.h: Temporarily ifdef out all defines involving load-average stuff -- using define ENABLE_SM_FILE_DECLS_OF_LOADAVG_STUFF, which is not defined. I'm pretty sure getloadavg.c will take care of defining stuff properly on all systems without the need to duplicate the info in m/*. --ben Delete LIBS_TERMCAP; configure auto-handles this. Delete extra stuff in hp800.h. Delete C_DEBUG_SWITCH, C_OPTIMIZE_SWITCH, LDAV_SYMBOL; incorporate some stuff into configure.ac.
author Ben Wing <ben@xemacs.org>
date Sat, 20 Feb 2010 06:03:00 -0600
parents aa5ed11f473b
children 1f0b15040456
comparison
equal deleted inserted replaced
5493:f62141fe007d 5494:861f2601a38b
1 /* Get the system load averages. 1 /* Get the system load averages.
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995 2 Copyright (C) 1985-1989, 1991-1995, 1997, 2001-2008
3 Free Software Foundation, Inc. 3 Free Software Foundation, Inc.
4 Copyright (C) 2010 Ben Wing.
5
6 NOTE: The canonical source of this file is maintained with gnulib.
7 Bugs can be reported to bug-gnulib@gnu.org.
4 8
5 This file is part of XEmacs. 9 This file is part of XEmacs.
6 10
7 XEmacs is free software; you can redistribute it and/or modify it 11 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 12 under the terms of the GNU General Public License as published by the
17 You should have received a copy of the GNU General Public License 21 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to 22 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */ 24 Boston, MA 02111-1307, USA. */
21 25
26 /* Synced up with: FSF 23.1.92. */
27 /* Synced by: Ben Wing. */
28
22 /* Compile-time symbols that this file uses: 29 /* Compile-time symbols that this file uses:
30
31 HAVE_SYS_PSTAT_H Defined if the pstat() function is available
32 (HPUX only); the GNU version uses
33 pstat_getdynamic() and HAVE_PSTAT_GETDYNAMIC.
34
35 [[ GNU says: The best way to get the
36 definition is through the AC_FUNC_GETLOADAVG
37 macro that comes with autoconf 2.13 or newer.
38 If that isn't an option, then just put
39 AC_CHECK_FUNCS(pstat_getdynamic) in your
40 configure.in file. ]] But we don't do this,
41 apparently we do the equivalent of
42 AC_FUNC_GETLOADAVG dynamically.
23 43
24 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist. 44 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
25 KERNEL_FILE Pathname of the kernel to nlist. 45 KERNEL_FILE Pathname of the kernel to nlist.
26 LDAV_CVT() Scale the load average from the kernel. 46 LDAV_CVT() Scale the load average from the kernel.
27 Returns a double. 47 Returns a double.
28 LDAV_SYMBOL Name of kernel symbol giving load average. 48 LDAV_SYMBOL Name of kernel symbol giving load average.
29 LOAD_AVE_TYPE Type of the load average array in the kernel. 49 LOAD_AVE_TYPE Type of the load average array in the kernel.
30 Must be defined; otherwise, no load average 50 Must be defined; or we have libkstat;
31 is available. 51 otherwise, no load average is available.
32 NLIST_STRUCT Include nlist.h, not a.out.h, and 52 NLIST_STRUCT Include nlist.h, not a.out.h, and
33 the nlist n_name element is a pointer, 53 the nlist n_name element is a pointer,
34 not an array. 54 not an array.
35 LINUX_LDAV_FILE [__linux__]: File containing load averages. 55 LINUX_LDAV_FILE [__linux__]: File containing load averages.
56 HAVE_LOCALE_H locale.h is available.
57 HAVE_SETLOCALE The `setlocale' function is available.
36 58
37 Specific system predefines this file uses, aside from setting 59 Specific system predefines this file uses, aside from setting
38 default values if not emacs: 60 default values if not emacs:
39 61
40 BSD Real BSD, not just BSD-like. 62 BSD Real BSD, not just BSD-like.
53 We also #define LDAV_PRIVILEGED if a program will require 75 We also #define LDAV_PRIVILEGED if a program will require
54 special installation to be able to call getloadavg. */ 76 special installation to be able to call getloadavg. */
55 77
56 /* This should always be first. */ 78 /* This should always be first. */
57 #ifdef HAVE_CONFIG_H 79 #ifdef HAVE_CONFIG_H
58 #include <config.h> 80 # include <config.h>
59 #endif 81 #endif
60 82
61 #include "lisp.h" 83 #include "lisp.h"
62 #include "sysfile.h" /* for encapsulated open, close, read, write */ 84 #include "sysfile.h" /* for encapsulated open, close, read, write */
85
86 #ifdef HAVE_LOCALE_H
87 # include <locale.h>
88 #endif
89 #ifndef HAVE_SETLOCALE
90 # define setlocale(Category, Locale) /* empty */
91 #endif
63 92
64 #ifndef HAVE_GETLOADAVG 93 #ifndef HAVE_GETLOADAVG
65 94
66 /* The existing Emacs configuration files define a macro called 95 /* The existing Emacs configuration files define a macro called
67 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and 96 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
71 100
72 For backwards compatibility, we'll define LDAV_CVT in terms of 101 For backwards compatibility, we'll define LDAV_CVT in terms of
73 LOAD_AVE_CVT, but future machine config files should just define 102 LOAD_AVE_CVT, but future machine config files should just define
74 LDAV_CVT directly. */ 103 LDAV_CVT directly. */
75 104
76 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT) 105 # if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
77 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0) 106 # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
78 #endif 107 # endif
79 108
80 #if defined (HAVE_KSTAT_H) 109 #if defined (HAVE_KSTAT_H)
81 #include <kstat.h> 110 #include <kstat.h>
82 #endif /* HAVE_KSTAT_H */ 111 #endif /* HAVE_KSTAT_H */
83 112
113 /* Same issues as for NeXT apply to the HURD-based GNU system. */
114 # ifdef __GNU__
115 # undef BSD
116 # undef FSCALE
117 # endif /* __GNU__ */
118
84 /* Set values that are different from the defaults, which are 119 /* Set values that are different from the defaults, which are
85 set a little farther down with #ifndef. */ 120 set a little farther down with #ifndef. */
86 121
87 122
88 /* Some shorthands. */ 123 /* Some shorthands. */
89 124
90 #if defined (HPUX) && !defined (hpux) 125 # if defined (HPUX) && !defined (hpux)
91 #define hpux 126 # define hpux
127 # endif
128
129 # if defined (__hpux) && !defined (hpux)
130 # define hpux
131 # endif
132
133 # if defined (__sun) && !defined (sun)
134 # define sun
135 # endif
136
137 # if defined (__SVR4) && !defined (SVR4)
138 # define SVR4
139 # endif
140
141 # if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
142 # define SUNOS_5
143 # endif
144
145 # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
146 # define OSF_ALPHA
147 # /* XEmacs: First three below from FSF 23.1.92. */
148 # include <sys/mbuf.h>
149 # include <sys/socket.h>
150 # include <net/route.h>
151 # include <netdb.h>
152 # include <netinet/in.h> /* Needed for Digital UNIX V3 */
153 # include <net/proto_net.h>
154 # include <sys/table.h>
155 # endif
156
157 #if defined (__osf__) && (defined (mips) || defined (__mips__))
158 # define OSF_MIPS
159 # include <sys/table.h>
92 #endif 160 #endif
93 161
94 #if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
95 #define SUNOS_5
96 #endif
97
98 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
99 #define OSF_ALPHA
100 #include <netdb.h>
101 #include <netinet/in.h> /* Needed for Digital UNIX V3 */
102 #include <net/proto_net.h>
103 #include <sys/table.h>
104 #endif
105
106 #if defined (__osf__) && (defined (mips) || defined (__mips__))
107 #define OSF_MIPS
108 #include <sys/table.h>
109 #endif
110
111 162
112 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */ 163 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
113 #ifndef LOAD_AVE_TYPE 164 # ifndef LOAD_AVE_TYPE
114 165
115 #ifdef sun 166 # ifdef sun
116 #define LOAD_AVE_TYPE long 167 # define LOAD_AVE_TYPE long
117 #endif 168 # endif
118 169
119 #ifdef decstation 170 # ifdef decstation
120 #define LOAD_AVE_TYPE long 171 # define LOAD_AVE_TYPE long
121 #endif 172 # endif
122 173
123 #ifdef sgi 174 # ifdef sgi
124 #define LOAD_AVE_TYPE long 175 # define LOAD_AVE_TYPE long
125 #endif 176 # endif
126 177
127 #ifdef SVR4 178 # ifdef SVR4
128 #define LOAD_AVE_TYPE long 179 # define LOAD_AVE_TYPE long
129 #endif 180 # endif
130 181
131 #ifdef sony_news 182 # ifdef sony_news
132 #define LOAD_AVE_TYPE long 183 # define LOAD_AVE_TYPE long
133 #endif 184 # endif
134 185
135 #ifdef OSF_ALPHA 186 # ifdef OSF_ALPHA
136 #define LOAD_AVE_TYPE long 187 # define LOAD_AVE_TYPE long
137 #endif 188 # endif
138 189
139 #if defined (ardent) && defined (titan) 190 # if defined (ardent) && defined (titan)
140 #define LOAD_AVE_TYPE long 191 # define LOAD_AVE_TYPE long
141 #endif 192 # endif
142 193
143 #ifdef _AIX 194 # ifdef _AIX
144 #define LOAD_AVE_TYPE long 195 # define LOAD_AVE_TYPE long
145 #endif 196 # endif
146 197
147 #endif /* No LOAD_AVE_TYPE. */ 198 # endif /* No LOAD_AVE_TYPE. */
148 199
149 #ifdef OSF_ALPHA 200 # ifdef OSF_ALPHA
150 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1, 201 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
151 according to ghazi@noc.rutgers.edu. */ 202 according to ghazi@noc.rutgers.edu. */
152 #undef FSCALE 203 # undef FSCALE
153 #define FSCALE 1024.0 204 # define FSCALE 1024.0
154 #endif 205 # endif
155 206
156 #ifndef FSCALE 207 # ifndef FSCALE
157 208
158 /* SunOS and some others define FSCALE in sys/param.h. */ 209 /* SunOS and some others define FSCALE in sys/param.h. */
159 210
160 #if defined(MIPS) || defined(SVR4) || defined(decstation) 211 # if defined(MIPS) || defined(SVR4) || defined(decstation)
161 #define FSCALE 256 212 # define FSCALE 256
162 #endif 213 # endif
163 214
164 #if defined (sgi) 215 # if defined (sgi)
165 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined 216 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
166 above under #ifdef MIPS. But we want the sgi value. */ 217 above under #ifdef MIPS. But we want the sgi value. */
167 #undef FSCALE 218 # undef FSCALE
168 #define FSCALE 1000.0 219 # define FSCALE 1000.0
220 # endif
221
222 # if defined (ardent) && defined (titan)
223 # define FSCALE 65536.0
224 # endif
225
226 # ifdef _AIX
227 # define FSCALE 65536.0
228 # endif
229
230 # endif /* Not FSCALE. */
231
232 # if !defined (LDAV_CVT) && defined (FSCALE)
233 # define LDAV_CVT(n) (((double) (n)) / FSCALE)
234 # endif
235
236 #if 0 /* XEmacs change ... we check for nlist.h during configure and
237 automatically set NLIST_STRUCT if so; we have no HAVE_NLIST_H. */
238 # ifndef NLIST_STRUCT
239 # if HAVE_NLIST_H
240 # define NLIST_STRUCT
241 # endif
242 # endif
169 #endif 243 #endif
170 244
171 #if defined (ardent) && defined (titan) 245 # if defined(sgi) || (defined(mips) && !defined(BSD))
172 #define FSCALE 65536.0 246 # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
173 #endif 247 # endif
174 248
175 #ifdef _AIX 249 # if !defined (KERNEL_FILE) && defined (hpux)
176 #define FSCALE 65536.0 250 # define KERNEL_FILE "/hp-ux"
177 #endif 251 # endif
178 252
179 #endif /* Not FSCALE. */ 253 # if !defined(KERNEL_FILE) && (defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
180 254 # define KERNEL_FILE "/unix"
181 #if !defined (LDAV_CVT) && defined (FSCALE) 255 # endif
182 #define LDAV_CVT(n) (((double) (n)) / FSCALE) 256
183 #endif 257 # if !defined(LDAV_SYMBOL) && (defined(hpux) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
184 258 # define LDAV_SYMBOL "avenrun"
185 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */ 259 # endif
186 #ifndef NLIST_STRUCT 260
187 261 # ifdef HAVE_UNISTD_H
188 #ifdef sun 262 # include <unistd.h>
189 #define NLIST_STRUCT 263 # endif
190 #endif 264
191 265 # include <stdio.h>
192 #ifdef decstation
193 #define NLIST_STRUCT
194 #endif
195
196 #ifdef hpux
197 #define NLIST_STRUCT
198 #endif
199
200 #ifdef sgi
201 #define NLIST_STRUCT
202 #endif
203
204 #ifdef SVR4
205 #define NLIST_STRUCT
206 #endif
207
208 #ifdef sony_news
209 #define NLIST_STRUCT
210 #endif
211
212 #ifdef OSF_ALPHA
213 #define NLIST_STRUCT
214 #endif
215
216 #if defined (ardent) && defined (titan)
217 #define NLIST_STRUCT
218 #endif
219
220 #ifdef butterfly
221 #define NLIST_STRUCT
222 #endif
223
224 #ifdef _AIX
225 #define NLIST_STRUCT
226 #endif
227
228 #endif /* defined (NLIST_STRUCT) */
229
230
231 #if defined(sgi) || (defined(mips) && !defined(BSD))
232 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
233 #endif
234
235 #if !defined (KERNEL_FILE) && defined (hpux)
236 #define KERNEL_FILE "/hp-ux"
237 #endif
238
239 #if !defined(KERNEL_FILE) && (defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
240 #define KERNEL_FILE "/unix"
241 #endif
242
243 #if !defined(LDAV_SYMBOL) && (defined(hpux) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
244 #define LDAV_SYMBOL "avenrun"
245 #endif
246
247 #ifdef HAVE_UNISTD_H
248 #include <unistd.h>
249 #endif
250
251 #include <stdio.h>
252 266
253 /* LOAD_AVE_TYPE should only get defined if we're going to use the 267 /* LOAD_AVE_TYPE should only get defined if we're going to use the
254 nlist method. */ 268 nlist method. */
255 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL)) 269 # if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
256 #define LOAD_AVE_TYPE double 270 # define LOAD_AVE_TYPE double
257 #endif 271 # endif
258 272
259 #ifdef LOAD_AVE_TYPE 273 # ifdef LOAD_AVE_TYPE
260 274
261 #ifndef NLIST_STRUCT 275 # ifndef __linux__
262 #include <a.out.h> 276 # ifndef NLIST_STRUCT
263 #else /* NLIST_STRUCT */ 277 # include <a.out.h>
264 #include <nlist.h> 278 # else /* NLIST_STRUCT */
265 #endif /* NLIST_STRUCT */ 279 # include <nlist.h>
266 280 # endif /* NLIST_STRUCT */
267 #ifdef SUNOS_5 281
268 #include <fcntl.h> 282 # ifdef SUNOS_5
269 #include <kvm.h> 283 # include <fcntl.h>
270 #endif 284 # include <kvm.h>
271 285 # include <kstat.h>
272 #ifndef KERNEL_FILE 286 # endif
273 #define KERNEL_FILE "/vmunix" 287
274 #endif /* KERNEL_FILE */ 288 /* XEmacs: GNU says: defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC) */
275 289 # if defined (HAVE_SYS_PSTAT_H)
276 #ifndef LDAV_SYMBOL 290 # include <sys/pstat.h>
277 #define LDAV_SYMBOL "_avenrun" 291 # endif /* HAVE_SYS_PSTAT_H (on HPUX) */
278 #endif /* LDAV_SYMBOL */ 292
279 293 # ifndef KERNEL_FILE
280 #ifndef LDAV_CVT 294 # define KERNEL_FILE "/vmunix"
281 #define LDAV_CVT(n) ((double) (n)) 295 # endif /* KERNEL_FILE */
282 #endif /* !LDAV_CVT */ 296
283 297 # ifndef LDAV_SYMBOL
284 #endif /* LOAD_AVE_TYPE */ 298 # define LDAV_SYMBOL "_avenrun"
285 299 # endif /* LDAV_SYMBOL */
286 #ifdef sgi 300 # endif /* __linux__ */
287 #include <sys/sysmp.h> 301
288 #endif /* sgi */ 302 # ifndef LDAV_CVT
289 303 # define LDAV_CVT(n) ((double) (n))
290 #if defined (HAVE_SYS_PSTAT_H) 304 # endif /* !LDAV_CVT */
291 #include <sys/pstat.h> 305
292 #endif /* HAVE_SYS_PSTAT_H (on HPUX) */ 306 # endif /* LOAD_AVE_TYPE */
307
308 # if defined(__GNU__) && !defined (NeXT)
309 /* XEmacs -- FSF added code for the HURD, which depends on some NeXT
310 code that we deleted as outdated. We hardly care about compiling
311 on the HURD, but I'm leaving this stuff here just in case; if someone
312 wants to get this working, figure out which NeXT code needs to be
313 restored. */
314 # error "Code not working currently"
315 /* Note that NeXT Openstep defines __GNU__ even though it should not. */
316 /* GNU system acts much like NeXT, for load average purposes,
317 but not exactly. */
318 # define NeXT
319 # define host_self mach_host_self
320 # endif /* defined(__GNU__) && !defined (NeXT) */
321
322 /* (ifdef NeXT ... deleted) */
323
324 # ifdef sgi
325 # include <sys/sysmp.h>
326 # endif /* sgi */
293 327
294 328
295 /* Avoid static vars inside a function since in HPUX they dump as pure. */ 329 /* Avoid static vars inside a function since in HPUX they dump as pure. */
296 330
297 #ifdef LOAD_AVE_TYPE 331 #if !defined(HAVE_LIBKSTAT) && defined(LOAD_AVE_TYPE)
298 /* File descriptor open to /dev/kmem */ 332 /* File descriptor open to /dev/kmem or VMS load ave driver. */
299 static int channel; 333 static int channel;
300 /* Nonzero iff channel is valid. */ 334 /* Nonzero if channel is valid. */
301 static int getloadavg_initialized; 335 static int getloadavg_initialized;
302 /* Offset in kmem to seek to read load average, or 0 means invalid. */ 336 /* Offset in kmem to seek to read load average, or 0 means invalid. */
303 static long offset; 337 static long offset;
304 338
305 #ifndef sgi 339 # if !defined(sgi) && !defined(__linux__)
306 static struct nlist nl[2]; 340 static struct nlist name_list[2];
307 #endif /* not sgi */ 341 # endif /* Not Linux or sgi */
308 342
309 #ifdef SUNOS_5 343 # ifdef SUNOS_5
310 static kvm_t *kd; 344 static kvm_t *kd;
311 #endif /* SUNOS_5 */ 345 # endif /* SUNOS_5 */
312 346
313 #ifndef countof 347 #ifndef countof
314 # define countof(x) (sizeof (x) / sizeof (*(x))) 348 # define countof(x) (sizeof (x) / sizeof (*(x)))
315 #endif 349 #endif
316 350
317 #endif /* LOAD_AVE_TYPE */ 351 #endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */
318 352
319 /* Put the 1 minute, 5 minute and 15 minute load averages 353 /* Put the 1 minute, 5 minute and 15 minute load averages
320 into the first NELEM elements of LOADAVG. 354 into the first NELEM elements of LOADAVG.
321 Return the number written (never more than 3, but may be less than NELEM), 355 Return the number written (never more than 3, but may be less than NELEM),
322 or -1 if an error occurred. */ 356 or -1 if an error occurred. */
326 int 360 int
327 getloadavg (double loadavg[], int nelem) 361 getloadavg (double loadavg[], int nelem)
328 { 362 {
329 int elem = 0; /* Return value. */ 363 int elem = 0; /* Return value. */
330 364
331 #ifdef NO_GET_LOAD_AVG 365 # ifdef NO_GET_LOAD_AVG
332 #define LDAV_DONE 366 # define LDAV_DONE
333 /* Set errno to zero to indicate that there was no particular error; 367 /* Set errno to zero to indicate that there was no particular error;
334 this function just can't work at all on this system. */ 368 this function just can't work at all on this system. */
335 errno = 0; 369 errno = 0;
336 elem = -2; 370 elem = -2;
337 #endif /* NO_GET_LOAD_AVG */ 371 # endif /* NO_GET_LOAD_AVG */
338 372
339 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT) 373 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT)
340 #define LDAV_DONE 374 /* Use libkstat because we don't have to be root. */
341 /* getloadavg is best implemented using kstat (kernel stats), on 375 # define LDAV_DONE
342 systems (like SunOS5) that support it, since you don't need special
343 privileges to use it.
344
345 Initial implementation courtesy Zlatko Calusic <zcalusic@carnet.hr>.
346 Integrated to XEmacs by Hrvoje Niksic <hniksic@xemacs.org>.
347 Additional cleanup by Hrvoje Niksic, based on code published by
348 Casper Dik <Casper.Dik@Holland.Sun.Com>. */
349 kstat_ctl_t *kc; 376 kstat_ctl_t *kc;
350 kstat_t *ksp; 377 kstat_t *ksp;
351 static char *avestrings[] = { "avenrun_1min", 378 kstat_named_t *kn;
352 "avenrun_5min",
353 "avenrun_15min" };
354
355 if (nelem > countof (avestrings))
356 nelem = countof (avestrings);
357 379
358 kc = kstat_open (); 380 kc = kstat_open ();
359 if (!kc) 381 if (kc == 0)
360 return -1; 382 return -1;
361 ksp = kstat_lookup (kc, "unix", 0, "system_misc"); 383 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
362 if (!ksp) 384 if (ksp == 0 )
385 return -1;
386 if (kstat_read (kc, ksp, 0) == -1)
387 return -1;
388
389
390 kn = kstat_data_lookup (ksp, "avenrun_1min");
391 if (kn == 0)
363 { 392 {
364 kstat_close (kc); 393 /* Return -1 if no load average information is available. */
365 return -1; 394 nelem = 0;
395 elem = -1;
366 } 396 }
367 if (kstat_read (kc, ksp, 0) < 0) 397
398 if (nelem >= 1)
399 loadavg[elem++] = (double) kn->value.ul/FSCALE;
400
401 if (nelem >= 2)
368 { 402 {
369 kstat_close (kc); 403 kn = kstat_data_lookup (ksp, "avenrun_5min");
370 return -1; 404 if (kn != 0)
405 {
406 loadavg[elem++] = (double) kn->value.ul/FSCALE;
407
408 if (nelem >= 3)
409 {
410 kn = kstat_data_lookup (ksp, "avenrun_15min");
411 if (kn != 0)
412 loadavg[elem++] = (double) kn->value.ul/FSCALE;
413 }
414 }
371 } 415 }
372 for (elem = 0; elem < nelem; elem++) 416
373 {
374 kstat_named_t *kn =
375 (kstat_named_t *) kstat_data_lookup (ksp, avestrings[elem]);
376 if (!kn)
377 {
378 kstat_close (kc);
379 return -1;
380 }
381 loadavg[elem] = (double)kn->value.ul / FSCALE;
382 }
383 kstat_close (kc); 417 kstat_close (kc);
384 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */ 418 # endif /* HAVE_LIBKSTAT */
385 419 # if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H)
386 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H) 420 /* GNU says: .... && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC) */
387 #define LDAV_DONE 421 # define LDAV_DONE
388 /* This is totally undocumented, and is not guaranteed to work, but 422 /* This is totally undocumented, and is not guaranteed to work, but
389 mayhap it might .... If it does work, it will work only on HP-UX 423 mayhap it might .... If it does work, it will work only on HP-UX
390 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */ 424 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */
391 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */ 425 # undef LOAD_AVE_TYPE
392 #undef LOAD_AVE_CVT 426 # undef LOAD_AVE_CVT
393 #undef LDAV_SYMBOL 427 # undef LDAV_SYMBOL
394 struct pst_dynamic procinfo; 428
429 struct pst_dynamic dyn_info;
430 #if 0 /* GNU 23.1.92 */
431 /* Use pstat_getdynamic() because we don't have to be root. */
432 if (pstat_getdynamic (&dyn_info, sizeof (dyn_info), 0, 0) < 0)
433 return -1;
434 #else
395 union pstun statbuf; 435 union pstun statbuf;
396 436 statbuf.pst_dynamic = &dyn_info;
397 statbuf.pst_dynamic = &procinfo; 437 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (dyn_info), 0, 0) == -1)
398 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1) 438 return -1;
399 return (-1);
400 loadavg[elem++] = procinfo.psd_avg_1_min;
401 loadavg[elem++] = procinfo.psd_avg_5_min;
402 loadavg[elem++] = procinfo.psd_avg_15_min;
403 #endif /* HPUX */
404
405 #if !defined (LDAV_DONE) && defined (__linux__)
406 #define LDAV_DONE
407 #undef LOAD_AVE_TYPE
408
409 #ifndef LINUX_LDAV_FILE
410 #define LINUX_LDAV_FILE "/proc/loadavg"
411 #endif 439 #endif
440 if (nelem > 0)
441 loadavg[elem++] = dyn_info.psd_avg_1_min;
442 if (nelem > 1)
443 loadavg[elem++] = dyn_info.psd_avg_5_min;
444 if (nelem > 2)
445 loadavg[elem++] = dyn_info.psd_avg_15_min;
446
447 # endif /* HAVE_SYS_PSTAT_H (GNU: hpux && HAVE_PSTAT_GETDYNAMIC) */
448
449 # if !defined (LDAV_DONE) && defined (__linux__)
450 # define LDAV_DONE
451 # undef LOAD_AVE_TYPE
452
453 # ifndef LINUX_LDAV_FILE
454 # define LINUX_LDAV_FILE "/proc/loadavg"
455 # endif
412 456
413 char ldavgbuf[40]; 457 char ldavgbuf[40];
414 double load_ave[3]; 458 double load_ave[3];
415 int fd, count; 459 int fd, count;
416 460
420 count = retry_read (fd, ldavgbuf, 40); 464 count = retry_read (fd, ldavgbuf, 40);
421 (void) retry_close (fd); 465 (void) retry_close (fd);
422 if (count <= 0) 466 if (count <= 0)
423 return -1; 467 return -1;
424 468
469 /* The following sscanf must use the C locale. */
470 setlocale (LC_NUMERIC, "C");
425 count = sscanf (ldavgbuf, "%lf %lf %lf", 471 count = sscanf (ldavgbuf, "%lf %lf %lf",
426 &load_ave[0], &load_ave[1], &load_ave[2]); 472 &load_ave[0], &load_ave[1], &load_ave[2]);
473 setlocale (LC_NUMERIC, "");
427 if (count < 1) 474 if (count < 1)
428 return -1; 475 return -1;
429 476
430 for (elem = 0; elem < nelem && elem < count; elem++) 477 for (elem = 0; elem < nelem && elem < count; elem++)
431 loadavg[elem] = load_ave[elem]; 478 loadavg[elem] = load_ave[elem];
432 #endif /* __linux__ */ 479
433 480 return elem;
434 #if !defined (LDAV_DONE) && defined (__NetBSD__) || defined (__OpenBSD__) 481
435 #define LDAV_DONE 482 # endif /* __linux__ */
436 #undef LOAD_AVE_TYPE 483
437 484 # if !defined (LDAV_DONE) && (defined (__NetBSD__) || defined (__OpenBSD__))
438 #ifndef NETBSD_LDAV_FILE 485 # define LDAV_DONE
439 #define NETBSD_LDAV_FILE "/kern/loadavg" 486 # undef LOAD_AVE_TYPE
440 #endif 487
488 # ifndef NETBSD_LDAV_FILE
489 # define NETBSD_LDAV_FILE "/kern/loadavg"
490 # endif
441 491
442 unsigned long int load_ave[3], scale; 492 unsigned long int load_ave[3], scale;
443 int count; 493 int count;
444 FILE *fp; 494 FILE *fp;
445 495
469 519
470 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) 520 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE)
471 521
472 /* UNIX-specific code -- read the average from /dev/kmem. */ 522 /* UNIX-specific code -- read the average from /dev/kmem. */
473 523
474 #define LDAV_PRIVILEGED /* This code requires special installation. */ 524 # define LDAV_PRIVILEGED /* This code requires special installation. */
475 525
476 LOAD_AVE_TYPE load_ave[3]; 526 LOAD_AVE_TYPE load_ave[3];
477 527
478 /* Get the address of LDAV_SYMBOL. */ 528 /* Get the address of LDAV_SYMBOL. */
479 if (offset == 0) 529 if (offset == 0)
480 { 530 {
481 #ifndef sgi 531 # ifndef sgi
482 #ifndef NLIST_STRUCT 532 # ifndef NLIST_STRUCT
483 strcpy (nl[0].n_name, LDAV_SYMBOL); 533 strcpy (name_list[0].n_name, LDAV_SYMBOL);
484 strcpy (nl[1].n_name, ""); 534 strcpy (name_list[1].n_name, "");
485 #else /* NLIST_STRUCT */ 535 # else /* NLIST_STRUCT */
486 nl[0].n_name = (char *) LDAV_SYMBOL; 536 /* #### XEmacs deletes HAVE_STRUCT_NLIST_N_UN_N_NAME code. Why? */
487 nl[1].n_name = 0; 537 name_list[0].n_name = (char *) LDAV_SYMBOL;
488 #endif /* NLIST_STRUCT */ 538 name_list[1].n_name = 0;
489 539 # endif /* NLIST_STRUCT */
490 #ifndef SUNOS_5 540
541 # ifndef SUNOS_5
491 if ( 542 if (
492 #if !(defined (_AIX) && !defined (ps2)) 543 # if !(defined (_AIX) && !defined (ps2))
493 nlist (KERNEL_FILE, nl) 544 nlist (KERNEL_FILE, name_list)
494 #else /* _AIX */ 545 # else /* _AIX */
495 knlist (nl, 1, sizeof (nl[0])) 546 knlist (name_list, 1, sizeof (name_list[0]))
496 #endif 547 # endif
497 >= 0) 548 >= 0)
498 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */ 549 /* Omit "&& name_list[0].n_type != 0 " -- it breaks on Sun386i. */
499 { 550 {
500 #ifdef FIXUP_KERNEL_SYMBOL_ADDR 551 # ifdef FIXUP_KERNEL_SYMBOL_ADDR
501 FIXUP_KERNEL_SYMBOL_ADDR (nl); 552 FIXUP_KERNEL_SYMBOL_ADDR (name_list);
502 #endif 553 # endif
503 offset = nl[0].n_value; 554 offset = name_list[0].n_value;
504 } 555 }
505 #endif /* !SUNOS_5 */ 556 # endif /* !SUNOS_5 */
506 #else /* sgi */ 557 # else /* sgi */
507 int ldav_off; 558 int ldav_off;
508 559
509 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN); 560 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
510 if (ldav_off != -1) 561 if (ldav_off != -1)
511 offset = (long) ldav_off & 0x7fffffff; 562 offset = (long) ldav_off & 0x7fffffff;
512 #endif /* sgi */ 563 # endif /* sgi */
513 } 564 }
514 565
515 /* Make sure we have /dev/kmem open. */ 566 /* Make sure we have /dev/kmem open. */
516 if (!getloadavg_initialized) 567 if (!getloadavg_initialized)
517 { 568 {
518 #ifndef SUNOS_5 569 # ifndef SUNOS_5
519 channel = retry_open ("/dev/kmem", 0); 570 channel = retry_open ("/dev/kmem", 0);
520 if (channel >= 0) 571 if (channel >= 0)
521 { 572 {
522 /* Set the channel to close on exec, so it does not 573 /* Set the channel to close on exec, so it does not
523 litter any child's descriptor table. */ 574 litter any child's descriptor table. */
524 #ifdef FD_SETFD 575 # ifdef F_SETFD
525 #ifndef FD_CLOEXEC 576 # ifndef FD_CLOEXEC
526 #define FD_CLOEXEC 1 577 # define FD_CLOEXEC 1
527 #endif 578 # endif
528 (void) fcntl (channel, F_SETFD, FD_CLOEXEC); 579 (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
529 #endif 580 # endif
530 getloadavg_initialized = 1; 581 getloadavg_initialized = 1;
531 } 582 }
532 #else /* SUNOS_5 */ 583 # else /* SUNOS_5 */
533 /* We pass 0 for the kernel, corefile, and swapfile names 584 /* We pass 0 for the kernel, corefile, and swapfile names
534 to use the currently running kernel. */ 585 to use the currently running kernel. */
535 kd = kvm_open (0, 0, 0, O_RDONLY, 0); 586 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
536 if (kd != 0) 587 if (kd != 0)
537 { 588 {
538 /* nlist the currently running kernel. */ 589 /* nlist the currently running kernel. */
539 kvm_nlist (kd, nl); 590 kvm_nlist (kd, name_list);
540 offset = nl[0].n_value; 591 offset = name_list[0].n_value;
541 getloadavg_initialized = 1; 592 getloadavg_initialized = 1;
542 } 593 }
543 #endif /* SUNOS_5 */ 594 # endif /* SUNOS_5 */
544 } 595 }
545 596
546 /* If we can, get the load average values. */ 597 /* If we can, get the load average values. */
547 if (offset && getloadavg_initialized) 598 if (offset && getloadavg_initialized)
548 { 599 {
549 /* Try to read the load. */ 600 /* Try to read the load. */
550 #ifndef SUNOS_5 601 # ifndef SUNOS_5
551 if (lseek (channel, offset, 0) == -1L 602 if (lseek (channel, offset, 0) == -1L
552 || retry_read (channel, (char *) load_ave, sizeof (load_ave)) 603 || retry_read (channel, (char *) load_ave, sizeof (load_ave))
553 != sizeof (load_ave)) 604 != sizeof (load_ave))
554 { 605 {
555 retry_close (channel); 606 retry_close (channel);
556 getloadavg_initialized = 0; 607 getloadavg_initialized = 0;
557 } 608 }
558 #else /* SUNOS_5 */ 609 # else /* SUNOS_5 */
559 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave)) 610 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
560 != sizeof (load_ave)) 611 != sizeof (load_ave))
561 { 612 {
562 kvm_close (kd); 613 kvm_close (kd);
563 getloadavg_initialized = 0; 614 getloadavg_initialized = 0;
564 } 615 }
565 #endif /* SUNOS_5 */ 616 # endif /* SUNOS_5 */
566 } 617 }
567 618
568 if (offset == 0 || !getloadavg_initialized) 619 if (offset == 0 || !getloadavg_initialized)
569 return -1; 620 return -1;
570 621
573 if (nelem > 1) 624 if (nelem > 1)
574 loadavg[elem++] = LDAV_CVT (load_ave[1]); 625 loadavg[elem++] = LDAV_CVT (load_ave[1]);
575 if (nelem > 2) 626 if (nelem > 2)
576 loadavg[elem++] = LDAV_CVT (load_ave[2]); 627 loadavg[elem++] = LDAV_CVT (load_ave[2]);
577 628
578 #define LDAV_DONE 629 # define LDAV_DONE
579 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */ 630 # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
580 631
581 return elem; 632 return elem;
582 } 633 }
583 634
584 #endif /* ! HAVE_GETLOADAVG */ 635 #endif /* ! HAVE_GETLOADAVG */
619 } 670 }
620 671
621 exit (0); 672 exit (0);
622 } 673 }
623 #endif /* TEST */ 674 #endif /* TEST */
675
676 /* arch-tag: 2b37a242-6289-41f4-8cd5-0e73fd615db1
677 (do not change this comment) */