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