0
|
1 /* Get the system load averages.
|
16
|
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995
|
84
|
3 Free Software Foundation, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
0
|
6
|
84
|
7 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
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
0
|
11
|
84
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
0
|
16
|
84
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
231
|
22 #ifndef __CYGWIN32__
|
0
|
23
|
|
24 /* Compile-time symbols that this file uses:
|
|
25
|
|
26 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
|
|
27 KERNEL_FILE Pathname of the kernel to nlist.
|
|
28 LDAV_CVT() Scale the load average from the kernel.
|
|
29 Returns a double.
|
|
30 LDAV_SYMBOL Name of kernel symbol giving load average.
|
|
31 LOAD_AVE_TYPE Type of the load average array in the kernel.
|
|
32 Must be defined unless one of
|
|
33 apollo, DGUX, NeXT, or UMAX is defined;
|
|
34 otherwise, no load average is available.
|
|
35 NLIST_STRUCT Include nlist.h, not a.out.h, and
|
|
36 the nlist n_name element is a pointer,
|
|
37 not an array.
|
|
38 NLIST_NAME_UNION struct nlist has an n_un member, not n_name.
|
|
39 LINUX_LDAV_FILE [__linux__]: File containing load averages.
|
|
40
|
|
41 Specific system predefines this file uses, aside from setting
|
|
42 default values if not emacs:
|
|
43
|
|
44 apollo
|
|
45 BSD Real BSD, not just BSD-like.
|
|
46 convex
|
|
47 DGUX
|
|
48 hpux
|
|
49 MSDOS No-op for MSDOS.
|
|
50 NeXT
|
|
51 sgi
|
|
52 sequent Sequent Dynix 3.x.x (BSD)
|
|
53 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
|
|
54 sony_news NEWS-OS (works at least for 4.1C)
|
|
55 UMAX
|
|
56 UMAX4_3
|
|
57 WIN32 No-op for Windows95/NT.
|
|
58 __linux__ Linux: assumes /proc filesystem mounted.
|
|
59 Support from Michael K. Johnson.
|
|
60 __NetBSD__ NetBSD: assumes /kern filesystem mounted.
|
|
61
|
|
62 In addition, to avoid nesting many #ifdefs, we internally set
|
|
63 LDAV_DONE to indicate that the load average has been computed.
|
|
64
|
|
65 We also #define LDAV_PRIVILEGED if a program will require
|
|
66 special installation to be able to call getloadavg. */
|
|
67
|
|
68 /* This should always be first. */
|
|
69 #ifdef HAVE_CONFIG_H
|
|
70 #include <config.h>
|
|
71 #endif
|
|
72
|
|
73 #include <sys/types.h>
|
|
74
|
|
75 /* Both the Emacs and non-Emacs sections want this. Some
|
|
76 configuration files' definitions for the LOAD_AVE_CVT macro (like
|
|
77 sparc.h's) use macros like FSCALE, defined here. */
|
|
78 #ifdef unix
|
|
79 #include <sys/param.h>
|
|
80 #endif
|
|
81
|
48
|
82 #ifdef XEMACS
|
0
|
83 #include "lisp.h" /* for encapsulated open, close, read, write */
|
48
|
84 #endif /* XEMACS */
|
0
|
85
|
|
86 /* Exclude all the code except the test program at the end
|
|
87 if the system has its own `getloadavg' function.
|
|
88
|
|
89 The declaration of `errno' is needed by the test program
|
|
90 as well as the function itself, so it comes first. */
|
|
91
|
|
92 #include <errno.h>
|
|
93
|
|
94 #ifndef errno
|
|
95 extern int errno;
|
|
96 #endif
|
|
97
|
|
98 #ifndef HAVE_GETLOADAVG
|
|
99
|
|
100 /* The existing Emacs configuration files define a macro called
|
|
101 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
|
|
102 returns the load average multiplied by 100. What we actually want
|
|
103 is a macro called LDAV_CVT, which returns the load average as an
|
|
104 unmultiplied double.
|
|
105
|
|
106 For backwards compatibility, we'll define LDAV_CVT in terms of
|
|
107 LOAD_AVE_CVT, but future machine config files should just define
|
|
108 LDAV_CVT directly. */
|
|
109
|
|
110 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
|
|
111 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
|
|
112 #endif
|
|
113
|
48
|
114 #ifdef XEMACS
|
|
115 #if defined (HAVE_KSTAT_H)
|
|
116 #include <kstat.h>
|
|
117 #endif /* HAVE_KSTAT_H */
|
|
118 #endif /* XEMACS */
|
|
119
|
0
|
120 #if !defined (BSD) && defined (ultrix)
|
|
121 /* Ultrix behaves like BSD on Vaxen. */
|
|
122 #define BSD
|
|
123 #endif
|
|
124
|
|
125 #ifdef NeXT
|
|
126 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
|
|
127 conflicts with the definition understood in this file, that this
|
|
128 really is BSD. */
|
|
129 #undef BSD
|
|
130
|
|
131 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
|
|
132 defined to mean that the nlist method should be used, which is not true. */
|
|
133 #undef FSCALE
|
|
134 #endif
|
|
135
|
|
136 /* Set values that are different from the defaults, which are
|
|
137 set a little farther down with #ifndef. */
|
|
138
|
|
139
|
|
140 /* Some shorthands. */
|
|
141
|
|
142 #if defined (HPUX) && !defined (hpux)
|
|
143 #define hpux
|
|
144 #endif
|
|
145
|
|
146 #if defined(hp300) && !defined(hpux)
|
|
147 #define MORE_BSD
|
|
148 #endif
|
|
149
|
|
150 #if defined(ultrix) && defined(mips)
|
|
151 #define decstation
|
|
152 #endif
|
|
153
|
48
|
154 #if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
|
0
|
155 #define SUNOS_5
|
|
156 #endif
|
|
157
|
|
158 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
|
|
159 #define OSF_ALPHA
|
151
|
160 #include <netdb.h>
|
|
161 #include <netinet/in.h> /* Needed for Digital UNIX V3 */
|
|
162 #include <net/proto_net.h>
|
0
|
163 #include <sys/table.h>
|
|
164 #endif
|
|
165
|
|
166 #if defined (__osf__) && (defined (mips) || defined (__mips__))
|
|
167 #define OSF_MIPS
|
|
168 #include <sys/table.h>
|
|
169 #endif
|
|
170
|
|
171 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
|
|
172 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
|
|
173 that with a couple of other things and we'll have a unique match. */
|
|
174 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
|
|
175 #define tek4300 /* Define by emacs, but not by other users. */
|
|
176 #endif
|
|
177
|
|
178
|
|
179 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
|
|
180 #ifndef LOAD_AVE_TYPE
|
|
181
|
|
182 #ifdef MORE_BSD
|
|
183 #define LOAD_AVE_TYPE long
|
|
184 #endif
|
|
185
|
|
186 #ifdef sun
|
|
187 #define LOAD_AVE_TYPE long
|
|
188 #endif
|
|
189
|
|
190 #ifdef decstation
|
|
191 #define LOAD_AVE_TYPE long
|
|
192 #endif
|
|
193
|
|
194 #ifdef _SEQUENT_
|
|
195 #define LOAD_AVE_TYPE long
|
|
196 #endif
|
|
197
|
|
198 #ifdef sgi
|
|
199 #define LOAD_AVE_TYPE long
|
|
200 #endif
|
|
201
|
|
202 #ifdef SVR4
|
|
203 #define LOAD_AVE_TYPE long
|
|
204 #endif
|
|
205
|
|
206 #ifdef sony_news
|
|
207 #define LOAD_AVE_TYPE long
|
|
208 #endif
|
|
209
|
|
210 #ifdef sequent
|
|
211 #define LOAD_AVE_TYPE long
|
|
212 #endif
|
|
213
|
|
214 #ifdef OSF_ALPHA
|
|
215 #define LOAD_AVE_TYPE long
|
|
216 #endif
|
|
217
|
|
218 #if defined (ardent) && defined (titan)
|
|
219 #define LOAD_AVE_TYPE long
|
|
220 #endif
|
|
221
|
|
222 #ifdef tek4300
|
|
223 #define LOAD_AVE_TYPE long
|
|
224 #endif
|
|
225
|
|
226 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
|
|
227 #define LOAD_AVE_TYPE long
|
|
228 #endif
|
|
229
|
|
230 #ifdef _AIX
|
|
231 #define LOAD_AVE_TYPE long
|
|
232 #endif
|
|
233
|
|
234 #ifdef convex
|
|
235 #define LOAD_AVE_TYPE double
|
|
236 #ifndef LDAV_CVT
|
|
237 #define LDAV_CVT(n) (n)
|
|
238 #endif
|
|
239 #endif
|
|
240
|
|
241 #endif /* No LOAD_AVE_TYPE. */
|
|
242
|
|
243 #ifdef OSF_ALPHA
|
|
244 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
|
|
245 according to ghazi@noc.rutgers.edu. */
|
|
246 #undef FSCALE
|
|
247 #define FSCALE 1024.0
|
|
248 #endif
|
|
249
|
|
250 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
|
|
251 /* <sys/param.h> defines an incorrect value for FSCALE on an
|
|
252 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
|
|
253 #undef FSCALE
|
|
254 #define FSCALE 100.0
|
|
255 #endif
|
|
256
|
|
257
|
|
258 #ifndef FSCALE
|
|
259
|
|
260 /* SunOS and some others define FSCALE in sys/param.h. */
|
|
261
|
|
262 #ifdef MORE_BSD
|
|
263 #define FSCALE 2048.0
|
|
264 #endif
|
|
265
|
|
266 #if defined(MIPS) || defined(SVR4) || defined(decstation)
|
|
267 #define FSCALE 256
|
|
268 #endif
|
|
269
|
|
270 #if defined (sgi) || defined (sequent)
|
|
271 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
|
|
272 above under #ifdef MIPS. But we want the sgi value. */
|
|
273 #undef FSCALE
|
|
274 #define FSCALE 1000.0
|
|
275 #endif
|
|
276
|
|
277 #if defined (ardent) && defined (titan)
|
|
278 #define FSCALE 65536.0
|
|
279 #endif
|
|
280
|
|
281 #ifdef tek4300
|
|
282 #define FSCALE 100.0
|
|
283 #endif
|
|
284
|
|
285 #ifdef _AIX
|
|
286 #define FSCALE 65536.0
|
|
287 #endif
|
|
288
|
|
289 #endif /* Not FSCALE. */
|
|
290
|
|
291 #if !defined (LDAV_CVT) && defined (FSCALE)
|
|
292 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
|
|
293 #endif
|
|
294
|
|
295 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
|
|
296 #ifndef NLIST_STRUCT
|
|
297
|
|
298 #ifdef MORE_BSD
|
|
299 #define NLIST_STRUCT
|
|
300 #endif
|
|
301
|
|
302 #ifdef sun
|
|
303 #define NLIST_STRUCT
|
|
304 #endif
|
|
305
|
|
306 #ifdef decstation
|
|
307 #define NLIST_STRUCT
|
|
308 #endif
|
|
309
|
|
310 #ifdef hpux
|
|
311 #define NLIST_STRUCT
|
|
312 #endif
|
|
313
|
|
314 #if defined (_SEQUENT_) || defined (sequent)
|
|
315 #define NLIST_STRUCT
|
|
316 #endif
|
|
317
|
|
318 #ifdef sgi
|
|
319 #define NLIST_STRUCT
|
|
320 #endif
|
|
321
|
|
322 #ifdef SVR4
|
|
323 #define NLIST_STRUCT
|
|
324 #endif
|
|
325
|
|
326 #ifdef sony_news
|
|
327 #define NLIST_STRUCT
|
|
328 #endif
|
|
329
|
|
330 #ifdef OSF_ALPHA
|
|
331 #define NLIST_STRUCT
|
|
332 #endif
|
|
333
|
|
334 #if defined (ardent) && defined (titan)
|
|
335 #define NLIST_STRUCT
|
|
336 #endif
|
|
337
|
|
338 #ifdef tek4300
|
|
339 #define NLIST_STRUCT
|
|
340 #endif
|
|
341
|
|
342 #ifdef butterfly
|
|
343 #define NLIST_STRUCT
|
|
344 #endif
|
|
345
|
|
346 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
|
|
347 #define NLIST_STRUCT
|
|
348 #endif
|
|
349
|
|
350 #ifdef _AIX
|
|
351 #define NLIST_STRUCT
|
|
352 #endif
|
|
353
|
|
354 #endif /* defined (NLIST_STRUCT) */
|
|
355
|
|
356
|
|
357 #if defined(sgi) || (defined(mips) && !defined(BSD))
|
|
358 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
|
|
359 #endif
|
|
360
|
|
361
|
|
362 #if !defined (KERNEL_FILE) && defined (sequent)
|
|
363 #define KERNEL_FILE "/dynix"
|
|
364 #endif
|
|
365
|
|
366 #if !defined (KERNEL_FILE) && defined (hpux)
|
|
367 #define KERNEL_FILE "/hp-ux"
|
|
368 #endif
|
|
369
|
|
370 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
|
|
371 #define KERNEL_FILE "/unix"
|
|
372 #endif
|
|
373
|
|
374
|
|
375 #if !defined (LDAV_SYMBOL) && defined (alliant)
|
|
376 #define LDAV_SYMBOL "_Loadavg"
|
|
377 #endif
|
|
378
|
|
379 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
|
|
380 #define LDAV_SYMBOL "avenrun"
|
|
381 #endif
|
|
382
|
48
|
383 #ifdef HAVE_UNISTD_H
|
0
|
384 #include <unistd.h>
|
48
|
385 #endif
|
0
|
386
|
|
387 #include <stdio.h>
|
|
388
|
|
389 /* LOAD_AVE_TYPE should only get defined if we're going to use the
|
|
390 nlist method. */
|
|
391 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
|
|
392 #define LOAD_AVE_TYPE double
|
|
393 #endif
|
|
394
|
|
395 #ifdef LOAD_AVE_TYPE
|
|
396
|
|
397 #ifndef NLIST_STRUCT
|
|
398 #include <a.out.h>
|
|
399 #else /* NLIST_STRUCT */
|
|
400 #include <nlist.h>
|
|
401 #endif /* NLIST_STRUCT */
|
|
402
|
|
403 #ifdef SUNOS_5
|
|
404 #include <fcntl.h>
|
|
405 #include <kvm.h>
|
|
406 #endif
|
|
407
|
|
408 #ifndef KERNEL_FILE
|
|
409 #define KERNEL_FILE "/vmunix"
|
|
410 #endif /* KERNEL_FILE */
|
|
411
|
|
412 #ifndef LDAV_SYMBOL
|
|
413 #define LDAV_SYMBOL "_avenrun"
|
|
414 #endif /* LDAV_SYMBOL */
|
|
415
|
|
416 #ifndef LDAV_CVT
|
|
417 #define LDAV_CVT(n) ((double) (n))
|
|
418 #endif /* !LDAV_CVT */
|
|
419
|
|
420 #endif /* LOAD_AVE_TYPE */
|
|
421
|
|
422 #ifdef NeXT
|
|
423 #ifdef HAVE_MACH_MACH_H
|
|
424 #include <mach/mach.h>
|
|
425 #else
|
|
426 #include <mach.h>
|
|
427 #endif
|
|
428 #endif /* NeXT */
|
|
429
|
|
430 #ifdef sgi
|
|
431 #include <sys/sysmp.h>
|
|
432 #endif /* sgi */
|
|
433
|
|
434 #ifdef UMAX
|
|
435 #include <stdio.h>
|
|
436 #include <signal.h>
|
|
437 #include <sys/time.h>
|
|
438 #include <sys/wait.h>
|
|
439 #include <sys/syscall.h>
|
|
440
|
|
441 #ifdef UMAX_43
|
|
442 #include <machine/cpu.h>
|
|
443 #include <inq_stats/statistics.h>
|
|
444 #include <inq_stats/sysstats.h>
|
|
445 #include <inq_stats/cpustats.h>
|
|
446 #include <inq_stats/procstats.h>
|
|
447 #else /* Not UMAX_43. */
|
|
448 #include <sys/sysdefs.h>
|
|
449 #include <sys/statistics.h>
|
|
450 #include <sys/sysstats.h>
|
|
451 #include <sys/cpudefs.h>
|
|
452 #include <sys/cpustats.h>
|
|
453 #include <sys/procstats.h>
|
|
454 #endif /* Not UMAX_43. */
|
|
455 #endif /* UMAX */
|
|
456
|
|
457 #ifdef DGUX
|
|
458 #include <sys/dg_sys_info.h>
|
|
459 #endif
|
|
460
|
48
|
461 #ifdef XEMACS
|
|
462 #if defined (HAVE_SYS_PSTAT_H)
|
0
|
463 #include <sys/pstat.h>
|
48
|
464 #endif /* HAVE_SYS_PSTAT_H (on HPUX) */
|
|
465 #endif /* XEMACS */
|
0
|
466
|
|
467 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
|
|
468 #include <fcntl.h>
|
|
469 #else
|
|
470 #include <sys/file.h>
|
|
471 #endif
|
|
472
|
|
473 /* Avoid static vars inside a function since in HPUX they dump as pure. */
|
|
474
|
|
475 #ifdef NeXT
|
|
476 static processor_set_t default_set;
|
|
477 static int getloadavg_initialized;
|
|
478 #endif /* NeXT */
|
|
479
|
|
480 #ifdef UMAX
|
|
481 static unsigned int cpus = 0;
|
|
482 static unsigned int samples;
|
|
483 #endif /* UMAX */
|
|
484
|
|
485 #ifdef DGUX
|
|
486 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
|
|
487 #endif /* DGUX */
|
|
488
|
|
489 #ifdef LOAD_AVE_TYPE
|
209
|
490 /* File descriptor open to /dev/kmem */
|
0
|
491 static int channel;
|
|
492 /* Nonzero iff channel is valid. */
|
|
493 static int getloadavg_initialized;
|
|
494 /* Offset in kmem to seek to read load average, or 0 means invalid. */
|
|
495 static long offset;
|
|
496
|
209
|
497 #ifndef sgi
|
0
|
498 static struct nlist nl[2];
|
209
|
499 #endif /* not sgi */
|
0
|
500
|
|
501 #ifdef SUNOS_5
|
|
502 static kvm_t *kd;
|
|
503 #endif /* SUNOS_5 */
|
|
504
|
|
505 #endif /* LOAD_AVE_TYPE */
|
|
506
|
|
507 /* Put the 1 minute, 5 minute and 15 minute load averages
|
|
508 into the first NELEM elements of LOADAVG.
|
|
509 Return the number written (never more than 3, but may be less than NELEM),
|
|
510 or -1 if an error occurred. */
|
|
511
|
|
512 int
|
84
|
513 getloadavg (double loadavg[], int nelem)
|
0
|
514 {
|
|
515 int elem = 0; /* Return value. */
|
|
516
|
|
517 #ifdef NO_GET_LOAD_AVG
|
|
518 #define LDAV_DONE
|
|
519 /* Set errno to zero to indicate that there was no particular error;
|
|
520 this function just can't work at all on this system. */
|
|
521 errno = 0;
|
|
522 elem = -1;
|
|
523 #endif
|
|
524
|
48
|
525 #ifdef XEMACS
|
|
526 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT)
|
|
527 #define LDAV_DONE
|
185
|
528
|
48
|
529 /* getloadavg is best implemented using kstat (kernel stats),
|
|
530 on systems (like SunOS5) that support it,
|
|
531 since you don't have to be superusers to use it.
|
|
532 Thanks to Zlatko Calusic <zcalusic@srce.hr>.
|
|
533 Integrated to XEmacs by Hrvoje Niksic <hniksic@srce.hr>. */
|
|
534 static kstat_ctl_t *kc;
|
|
535 static kstat_t *ksp;
|
|
536 static kstat_named_t *buf;
|
|
537
|
|
538 if (!getloadavg_initialized)
|
|
539 {
|
|
540 kc = kstat_open();
|
|
541 if (!kc)
|
|
542 return -1;
|
|
543 getloadavg_initialized = 1;
|
|
544 }
|
|
545 ksp = kstat_lookup(kc, "unix", 0, "system_misc");
|
|
546 if (!ksp)
|
|
547 return -1;
|
|
548 if (kstat_read(kc, ksp, ksp->ks_data) < 0)
|
|
549 return -1;
|
185
|
550 buf = (kstat_named_t *) malloc (ksp->ks_data_size);
|
48
|
551 if (!buf)
|
|
552 return -1;
|
|
553 memcpy(buf, ksp->ks_data, ksp->ks_data_size);
|
|
554 if (nelem > 3)
|
|
555 nelem = 3;
|
|
556 for (elem = 0; elem < nelem; elem++)
|
|
557 loadavg[elem] = (buf + 6 + elem)->value.ul / 256.0;
|
|
558 free(buf);
|
185
|
559
|
48
|
560 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */
|
82
|
561
|
|
562 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H)
|
|
563 #define LDAV_DONE
|
|
564 /* This is totally undocumented, and is not guaranteed to work, but
|
|
565 mayhap it might .... If it does work, it will work only on HP-UX
|
|
566 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */
|
|
567 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */
|
|
568 #undef LOAD_AVE_CVT
|
|
569 #undef LDAV_SYMBOL
|
|
570 struct pst_dynamic procinfo;
|
|
571 union pstun statbuf;
|
|
572
|
|
573 statbuf.pst_dynamic = &procinfo;
|
|
574 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1)
|
|
575 return (-1);
|
|
576 loadavg[elem++] = procinfo.psd_avg_1_min;
|
|
577 loadavg[elem++] = procinfo.psd_avg_5_min;
|
|
578 loadavg[elem++] = procinfo.psd_avg_15_min;
|
|
579 #endif /* HPUX */
|
|
580
|
48
|
581 #endif /* XEMACS */
|
|
582
|
0
|
583 #if !defined (LDAV_DONE) && defined (__linux__)
|
|
584 #define LDAV_DONE
|
|
585 #undef LOAD_AVE_TYPE
|
|
586
|
|
587 #ifndef LINUX_LDAV_FILE
|
|
588 #define LINUX_LDAV_FILE "/proc/loadavg"
|
|
589 #endif
|
|
590
|
|
591 char ldavgbuf[40];
|
|
592 double load_ave[3];
|
|
593 int fd, count;
|
|
594
|
|
595 fd = open (LINUX_LDAV_FILE, O_RDONLY);
|
|
596 if (fd == -1)
|
|
597 return -1;
|
|
598 count = read (fd, ldavgbuf, 40);
|
|
599 (void) close (fd);
|
|
600 if (count <= 0)
|
|
601 return -1;
|
|
602
|
|
603 count = sscanf (ldavgbuf, "%lf %lf %lf",
|
|
604 &load_ave[0], &load_ave[1], &load_ave[2]);
|
|
605 if (count < 1)
|
|
606 return -1;
|
|
607
|
|
608 for (elem = 0; elem < nelem && elem < count; elem++)
|
|
609 loadavg[elem] = load_ave[elem];
|
|
610
|
|
611 return elem;
|
|
612
|
|
613 #endif /* __linux__ */
|
|
614
|
|
615 #if !defined (LDAV_DONE) && defined (__NetBSD__)
|
|
616 #define LDAV_DONE
|
|
617 #undef LOAD_AVE_TYPE
|
|
618
|
|
619 #ifndef NETBSD_LDAV_FILE
|
|
620 #define NETBSD_LDAV_FILE "/kern/loadavg"
|
|
621 #endif
|
|
622
|
|
623 unsigned long int load_ave[3], scale;
|
|
624 int count;
|
|
625 FILE *fp;
|
|
626
|
|
627 fp = fopen (NETBSD_LDAV_FILE, "r");
|
|
628 if (fp == NULL)
|
|
629 return -1;
|
|
630 count = fscanf (fp, "%lu %lu %lu %lu\n",
|
|
631 &load_ave[0], &load_ave[1], &load_ave[2],
|
|
632 &scale);
|
|
633 (void) fclose (fp);
|
|
634 if (count != 4)
|
|
635 return -1;
|
|
636
|
|
637 for (elem = 0; elem < nelem; elem++)
|
|
638 loadavg[elem] = (double) load_ave[elem] / (double) scale;
|
|
639
|
|
640 return elem;
|
|
641
|
|
642 #endif /* __NetBSD__ */
|
|
643
|
|
644 #if !defined (LDAV_DONE) && defined (NeXT)
|
|
645 #define LDAV_DONE
|
|
646 /* The NeXT code was adapted from iscreen 3.2. */
|
|
647
|
|
648 host_t host;
|
|
649 struct processor_set_basic_info info;
|
|
650 unsigned info_count;
|
|
651
|
48
|
652 /* We only know how to get the 1-minute average for this system,
|
|
653 so even if the caller asks for more than 1, we only return 1. */
|
0
|
654
|
|
655 if (!getloadavg_initialized)
|
|
656 {
|
|
657 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
|
|
658 getloadavg_initialized = 1;
|
|
659 }
|
|
660
|
|
661 if (getloadavg_initialized)
|
|
662 {
|
|
663 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
|
|
664 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
|
|
665 (processor_set_info_t) &info, &info_count)
|
|
666 != KERN_SUCCESS)
|
|
667 getloadavg_initialized = 0;
|
|
668 else
|
|
669 {
|
|
670 if (nelem > 0)
|
|
671 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
|
|
672 }
|
|
673 }
|
|
674
|
|
675 if (!getloadavg_initialized)
|
|
676 return -1;
|
|
677 #endif /* NeXT */
|
|
678
|
|
679 #if !defined (LDAV_DONE) && defined (UMAX)
|
|
680 #define LDAV_DONE
|
|
681 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
|
|
682 have a /dev/kmem. Information about the workings of the running kernel
|
|
683 can be gathered with inq_stats system calls.
|
|
684 We only know how to get the 1-minute average for this system. */
|
|
685
|
|
686 struct proc_summary proc_sum_data;
|
|
687 struct stat_descr proc_info;
|
|
688 double load;
|
203
|
689 REGISTER unsigned int i, j;
|
0
|
690
|
|
691 if (cpus == 0)
|
|
692 {
|
203
|
693 REGISTER unsigned int c, i;
|
0
|
694 struct cpu_config conf;
|
|
695 struct stat_descr desc;
|
|
696
|
|
697 desc.sd_next = 0;
|
|
698 desc.sd_subsys = SUBSYS_CPU;
|
|
699 desc.sd_type = CPUTYPE_CONFIG;
|
|
700 desc.sd_addr = (char *) &conf;
|
|
701 desc.sd_size = sizeof conf;
|
|
702
|
|
703 if (inq_stats (1, &desc))
|
|
704 return -1;
|
|
705
|
|
706 c = 0;
|
|
707 for (i = 0; i < conf.config_maxclass; ++i)
|
|
708 {
|
|
709 struct class_stats stats;
|
|
710 bzero ((char *) &stats, sizeof stats);
|
|
711
|
|
712 desc.sd_type = CPUTYPE_CLASS;
|
|
713 desc.sd_objid = i;
|
|
714 desc.sd_addr = (char *) &stats;
|
|
715 desc.sd_size = sizeof stats;
|
|
716
|
|
717 if (inq_stats (1, &desc))
|
|
718 return -1;
|
|
719
|
|
720 c += stats.class_numcpus;
|
|
721 }
|
|
722 cpus = c;
|
|
723 samples = cpus < 2 ? 3 : (2 * cpus / 3);
|
|
724 }
|
|
725
|
|
726 proc_info.sd_next = 0;
|
|
727 proc_info.sd_subsys = SUBSYS_PROC;
|
|
728 proc_info.sd_type = PROCTYPE_SUMMARY;
|
|
729 proc_info.sd_addr = (char *) &proc_sum_data;
|
|
730 proc_info.sd_size = sizeof (struct proc_summary);
|
|
731 proc_info.sd_sizeused = 0;
|
|
732
|
|
733 if (inq_stats (1, &proc_info) != 0)
|
|
734 return -1;
|
|
735
|
|
736 load = proc_sum_data.ps_nrunnable;
|
|
737 j = 0;
|
|
738 for (i = samples - 1; i > 0; --i)
|
|
739 {
|
|
740 load += proc_sum_data.ps_nrun[j];
|
|
741 if (j++ == PS_NRUNSIZE)
|
|
742 j = 0;
|
|
743 }
|
|
744
|
|
745 if (nelem > 0)
|
|
746 loadavg[elem++] = load / samples / cpus;
|
|
747 #endif /* UMAX */
|
|
748
|
|
749 #if !defined (LDAV_DONE) && defined (DGUX)
|
|
750 #define LDAV_DONE
|
|
751 /* This call can return -1 for an error, but with good args
|
|
752 it's not supposed to fail. The first argument is for no
|
|
753 apparent reason of type `long int *'. */
|
|
754 dg_sys_info ((long int *) &load_info,
|
|
755 DG_SYS_INFO_LOAD_INFO_TYPE,
|
|
756 DG_SYS_INFO_LOAD_VERSION_0);
|
|
757
|
|
758 if (nelem > 0)
|
|
759 loadavg[elem++] = load_info.one_minute;
|
|
760 if (nelem > 1)
|
|
761 loadavg[elem++] = load_info.five_minute;
|
|
762 if (nelem > 2)
|
|
763 loadavg[elem++] = load_info.fifteen_minute;
|
|
764 #endif /* DGUX */
|
|
765
|
|
766 #if !defined (LDAV_DONE) && defined (apollo)
|
|
767 #define LDAV_DONE
|
|
768 /* Apollo code from lisch@mentorg.com (Ray Lischner).
|
|
769
|
|
770 This system call is not documented. The load average is obtained as
|
|
771 three long integers, for the load average over the past minute,
|
|
772 five minutes, and fifteen minutes. Each value is a scaled integer,
|
|
773 with 16 bits of integer part and 16 bits of fraction part.
|
|
774
|
|
775 I'm not sure which operating system first supported this system call,
|
|
776 but I know that SR10.2 supports it. */
|
|
777
|
|
778 extern void proc1_$get_loadav ();
|
|
779 unsigned long load_ave[3];
|
|
780
|
|
781 proc1_$get_loadav (load_ave);
|
|
782
|
|
783 if (nelem > 0)
|
|
784 loadavg[elem++] = load_ave[0] / 65536.0;
|
|
785 if (nelem > 1)
|
|
786 loadavg[elem++] = load_ave[1] / 65536.0;
|
|
787 if (nelem > 2)
|
|
788 loadavg[elem++] = load_ave[2] / 65536.0;
|
|
789 #endif /* apollo */
|
|
790
|
|
791 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
|
|
792 #define LDAV_DONE
|
|
793
|
|
794 struct tbl_loadavg load_ave;
|
|
795 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
|
48
|
796 loadavg[elem++]
|
|
797 = (load_ave.tl_lscale == 0
|
|
798 ? load_ave.tl_avenrun.d[0]
|
|
799 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
|
0
|
800 #endif /* OSF_MIPS */
|
|
801
|
|
802 #if !defined (LDAV_DONE) && (defined (MSDOS) || defined (WIN32))
|
|
803 #define LDAV_DONE
|
|
804
|
|
805 /* A faithful emulation is going to have to be saved for a rainy day. */
|
|
806 for ( ; elem < nelem; elem++)
|
|
807 {
|
|
808 loadavg[elem] = 0.0;
|
|
809 }
|
|
810 #endif /* MSDOS */
|
|
811
|
|
812 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
|
|
813 #define LDAV_DONE
|
|
814
|
|
815 struct tbl_loadavg load_ave;
|
|
816 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
|
|
817 for (elem = 0; elem < nelem; elem++)
|
|
818 loadavg[elem]
|
|
819 = (load_ave.tl_lscale == 0
|
|
820 ? load_ave.tl_avenrun.d[elem]
|
|
821 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
|
|
822 #endif /* OSF_ALPHA */
|
|
823
|
209
|
824 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE)
|
0
|
825
|
|
826 /* UNIX-specific code -- read the average from /dev/kmem. */
|
|
827
|
|
828 #define LDAV_PRIVILEGED /* This code requires special installation. */
|
|
829
|
|
830 LOAD_AVE_TYPE load_ave[3];
|
|
831
|
|
832 /* Get the address of LDAV_SYMBOL. */
|
|
833 if (offset == 0)
|
|
834 {
|
|
835 #ifndef sgi
|
|
836 #ifndef NLIST_STRUCT
|
|
837 strcpy (nl[0].n_name, LDAV_SYMBOL);
|
|
838 strcpy (nl[1].n_name, "");
|
|
839 #else /* NLIST_STRUCT */
|
|
840 #ifdef NLIST_NAME_UNION
|
|
841 nl[0].n_un.n_name = LDAV_SYMBOL;
|
|
842 nl[1].n_un.n_name = 0;
|
|
843 #else /* not NLIST_NAME_UNION */
|
84
|
844 nl[0].n_name = (char *) LDAV_SYMBOL;
|
0
|
845 nl[1].n_name = 0;
|
|
846 #endif /* not NLIST_NAME_UNION */
|
|
847 #endif /* NLIST_STRUCT */
|
|
848
|
|
849 #ifndef SUNOS_5
|
|
850 if (
|
|
851 #if !(defined (_AIX) && !defined (ps2))
|
|
852 nlist (KERNEL_FILE, nl)
|
|
853 #else /* _AIX */
|
|
854 knlist (nl, 1, sizeof (nl[0]))
|
|
855 #endif
|
|
856 >= 0)
|
|
857 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
|
|
858 {
|
|
859 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
|
|
860 FIXUP_KERNEL_SYMBOL_ADDR (nl);
|
|
861 #endif
|
|
862 offset = nl[0].n_value;
|
|
863 }
|
|
864 #endif /* !SUNOS_5 */
|
|
865 #else /* sgi */
|
|
866 int ldav_off;
|
|
867
|
|
868 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
|
|
869 if (ldav_off != -1)
|
|
870 offset = (long) ldav_off & 0x7fffffff;
|
|
871 #endif /* sgi */
|
|
872 }
|
|
873
|
|
874 /* Make sure we have /dev/kmem open. */
|
|
875 if (!getloadavg_initialized)
|
|
876 {
|
|
877 #ifndef SUNOS_5
|
|
878 channel = open ("/dev/kmem", 0);
|
|
879 if (channel >= 0)
|
|
880 {
|
|
881 /* Set the channel to close on exec, so it does not
|
|
882 litter any child's descriptor table. */
|
|
883 #ifdef FD_SETFD
|
|
884 #ifndef FD_CLOEXEC
|
|
885 #define FD_CLOEXEC 1
|
|
886 #endif
|
|
887 (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
|
|
888 #endif
|
|
889 getloadavg_initialized = 1;
|
|
890 }
|
|
891 #else /* SUNOS_5 */
|
|
892 /* We pass 0 for the kernel, corefile, and swapfile names
|
|
893 to use the currently running kernel. */
|
|
894 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
|
|
895 if (kd != 0)
|
|
896 {
|
|
897 /* nlist the currently running kernel. */
|
|
898 kvm_nlist (kd, nl);
|
|
899 offset = nl[0].n_value;
|
|
900 getloadavg_initialized = 1;
|
|
901 }
|
|
902 #endif /* SUNOS_5 */
|
|
903 }
|
|
904
|
|
905 /* If we can, get the load average values. */
|
|
906 if (offset && getloadavg_initialized)
|
|
907 {
|
|
908 /* Try to read the load. */
|
|
909 #ifndef SUNOS_5
|
|
910 if (lseek (channel, offset, 0) == -1L
|
|
911 || read (channel, (char *) load_ave, sizeof (load_ave))
|
|
912 != sizeof (load_ave))
|
|
913 {
|
|
914 close (channel);
|
|
915 getloadavg_initialized = 0;
|
|
916 }
|
|
917 #else /* SUNOS_5 */
|
|
918 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
|
|
919 != sizeof (load_ave))
|
|
920 {
|
|
921 kvm_close (kd);
|
|
922 getloadavg_initialized = 0;
|
|
923 }
|
|
924 #endif /* SUNOS_5 */
|
|
925 }
|
|
926
|
|
927 if (offset == 0 || !getloadavg_initialized)
|
|
928 return -1;
|
|
929
|
|
930 if (nelem > 0)
|
|
931 loadavg[elem++] = LDAV_CVT (load_ave[0]);
|
|
932 if (nelem > 1)
|
|
933 loadavg[elem++] = LDAV_CVT (load_ave[1]);
|
|
934 if (nelem > 2)
|
|
935 loadavg[elem++] = LDAV_CVT (load_ave[2]);
|
|
936
|
|
937 #define LDAV_DONE
|
|
938 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
|
|
939
|
|
940 #ifdef LDAV_DONE
|
|
941 return elem;
|
|
942 #else
|
|
943 /* Set errno to zero to indicate that there was no particular error;
|
|
944 this function just can't work at all on this system. */
|
|
945 errno = 0;
|
|
946 return -1;
|
|
947 #endif
|
|
948 }
|
|
949
|
|
950 #endif /* ! HAVE_GETLOADAVG */
|
|
951
|
|
952 #ifdef TEST
|
|
953 void
|
84
|
954 main (int argc, char **argv)
|
0
|
955 {
|
|
956 int naptime = 0;
|
|
957
|
|
958 if (argc > 1)
|
|
959 naptime = atoi (argv[1]);
|
|
960
|
|
961 while (1)
|
|
962 {
|
|
963 double avg[3];
|
|
964 int loads;
|
|
965
|
|
966 errno = 0; /* Don't be misled if it doesn't set errno. */
|
|
967 loads = getloadavg (avg, 3);
|
|
968 if (loads == -1)
|
|
969 {
|
|
970 perror ("Error getting load average");
|
|
971 exit (1);
|
|
972 }
|
|
973 if (loads > 0)
|
|
974 printf ("1-minute: %f ", avg[0]);
|
|
975 if (loads > 1)
|
|
976 printf ("5-minute: %f ", avg[1]);
|
|
977 if (loads > 2)
|
|
978 printf ("15-minute: %f ", avg[2]);
|
|
979 if (loads > 0)
|
|
980 putchar ('\n');
|
|
981
|
|
982 if (naptime == 0)
|
|
983 break;
|
|
984 sleep (naptime);
|
|
985 }
|
|
986
|
|
987 exit (0);
|
|
988 }
|
|
989 #endif /* TEST */
|
231
|
990
|
|
991 #else
|
|
992
|
|
993 /* Emulate getloadavg. */
|
|
994 int
|
|
995 getloadavg (double loadavg[], int nelem)
|
|
996 {
|
|
997 int i;
|
|
998
|
|
999 /* A faithful emulation is going to have to be saved for a rainy day. */
|
|
1000 for (i = 0; i < nelem; i++)
|
|
1001 {
|
|
1002 loadavg[i] = 0.0;
|
|
1003 }
|
|
1004 return i;
|
|
1005 }
|
|
1006
|
|
1007 #endif /*__GNUWIN32__*/
|
|
1008
|