1 /* Get the system load averages.
3 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994,
4 1995, 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007 Free Software
5 Foundation, Inc.
7 NOTE: The canonical source of this file is maintained with gnulib.
8 Bugs can be reported to bug-gnulib@gnu.org.
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Compile-time symbols that this file uses:
25 HAVE_PSTAT_GETDYNAMIC Define this if your system has the
26 pstat_getdynamic function. I think it
27 is unique to HPUX9. The best way to get the
28 definition is through the AC_FUNC_GETLOADAVG
29 macro that comes with autoconf 2.13 or newer.
30 If that isn't an option, then just put
31 AC_CHECK_FUNCS(pstat_getdynamic) in your
32 configure.in file.
33 HAVE_LIBPERFSTAT Define this if your system has the
34 perfstat_cpu_total function in libperfstat (AIX).
35 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
36 KERNEL_FILE Name of the kernel file to nlist.
37 LDAV_CVT() Scale the load average from the kernel.
38 Returns a double.
39 LDAV_SYMBOL Name of kernel symbol giving load average.
40 LOAD_AVE_TYPE Type of the load average array in the kernel.
41 Must be defined unless one of
42 apollo, DGUX, NeXT, or UMAX is defined;
43 or we have libkstat;
44 otherwise, no load average is available.
45 HAVE_NLIST_H nlist.h is available. NLIST_STRUCT defaults
46 to this.
47 NLIST_STRUCT Include nlist.h, not a.out.h, and
48 the nlist n_name element is a pointer,
49 not an array.
50 HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'.
51 LINUX_LDAV_FILE [__linux__, __CYGWIN__]: File containing
52 load averages.
54 Specific system predefines this file uses, aside from setting
55 default values if not emacs:
57 apollo
58 BSD Real BSD, not just BSD-like.
59 convex
60 DGUX
61 eunice UNIX emulator under VMS.
62 hpux
63 __MSDOS__ No-op for MSDOS.
64 NeXT
65 sgi
66 sequent Sequent Dynix 3.x.x (BSD)
67 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
68 sony_news NEWS-OS (works at least for 4.1C)
69 UMAX
70 UMAX4_3
71 VMS
72 WINDOWS32 No-op for Windows95/NT.
73 __linux__ Linux: assumes /proc file system mounted.
74 Support from Michael K. Johnson.
75 __CYGWIN__ Cygwin emulates linux /proc/loadavg.
76 __NetBSD__ NetBSD: assumes /kern file system mounted.
78 In addition, to avoid nesting many #ifdefs, we internally set
79 LDAV_DONE to indicate that the load average has been computed.
81 We also #define LDAV_PRIVILEGED if a program will require
82 special installation to be able to call getloadavg. */
84 /* "configure" defines CONFIGURING_GETLOADAVG to sidestep problems
85 with partially-configured source directories. */
87 #ifndef CONFIGURING_GETLOADAVG
88 # include <config.h>
89 # include <stdbool.h>
90 #endif
92 #include <errno.h>
93 #include <stdio.h>
94 #include <stdlib.h>
96 /* Exclude all the code except the test program at the end
97 if the system has its own `getloadavg' function. */
99 #ifndef HAVE_GETLOADAVG
101 # include <sys/types.h>
103 /* Both the Emacs and non-Emacs sections want this. Some
104 configuration files' definitions for the LOAD_AVE_CVT macro (like
105 sparc.h's) use macros like FSCALE, defined here. */
106 # if defined (unix) || defined (__unix)
107 # include <sys/param.h>
108 # endif
110 # include "c-strtod.h"
111 # include "cloexec.h"
112 # include "intprops.h"
113 # include "xalloc.h"
115 /* The existing Emacs configuration files define a macro called
116 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
117 returns the load average multiplied by 100. What we actually want
118 is a macro called LDAV_CVT, which returns the load average as an
119 unmultiplied double.
121 For backwards compatibility, we'll define LDAV_CVT in terms of
122 LOAD_AVE_CVT, but future machine config files should just define
123 LDAV_CVT directly. */
125 # if !defined (LDAV_CVT) && defined (LOAD_AVE_CVT)
126 # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
127 # endif
129 # if !defined (BSD) && defined (ultrix)
130 /* Ultrix behaves like BSD on Vaxen. */
131 # define BSD
132 # endif
134 # ifdef NeXT
135 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
136 conflicts with the definition understood in this file, that this
137 really is BSD. */
138 # undef BSD
140 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
141 defined to mean that the nlist method should be used, which is not true. */
142 # undef FSCALE
143 # endif
145 /* Same issues as for NeXT apply to the HURD-based GNU system. */
146 # ifdef __GNU__
147 # undef BSD
148 # undef FSCALE
149 # endif /* __GNU__ */
151 /* Set values that are different from the defaults, which are
152 set a little farther down with #ifndef. */
155 /* Some shorthands. */
157 # if defined (HPUX) && !defined (hpux)
158 # define hpux
159 # endif
161 # if defined (__hpux) && !defined (hpux)
162 # define hpux
163 # endif
165 # if defined (__sun) && !defined (sun)
166 # define sun
167 # endif
169 # if defined (hp300) && !defined (hpux)
170 # define MORE_BSD
171 # endif
173 # if defined (ultrix) && defined (mips)
174 # define decstation
175 # endif
177 # if defined (__SVR4) && !defined (SVR4)
178 # define SVR4
179 # endif
181 # if (defined (sun) && defined (SVR4)) || defined (SOLARIS2)
182 # define SUNOS_5
183 # endif
185 # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
186 # define OSF_ALPHA
187 # include <sys/mbuf.h>
188 # include <sys/socket.h>
189 # include <net/route.h>
190 # include <sys/table.h>
191 /* Tru64 4.0D's table.h redefines sys */
192 # undef sys
193 # endif
195 # if defined (__osf__) && (defined (mips) || defined (__mips__))
196 # define OSF_MIPS
197 # include <sys/table.h>
198 # endif
200 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
201 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
202 that with a couple of other things and we'll have a unique match. */
203 # if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
204 # define tek4300 /* Define by emacs, but not by other users. */
205 # endif
208 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
209 # ifndef LOAD_AVE_TYPE
211 # ifdef MORE_BSD
212 # define LOAD_AVE_TYPE long
213 # endif
215 # ifdef sun
216 # define LOAD_AVE_TYPE long
217 # endif
219 # ifdef decstation
220 # define LOAD_AVE_TYPE long
221 # endif
223 # ifdef _SEQUENT_
224 # define LOAD_AVE_TYPE long
225 # endif
227 # ifdef sgi
228 # define LOAD_AVE_TYPE long
229 # endif
231 # ifdef SVR4
232 # define LOAD_AVE_TYPE long
233 # endif
235 # ifdef sony_news
236 # define LOAD_AVE_TYPE long
237 # endif
239 # ifdef sequent
240 # define LOAD_AVE_TYPE long
241 # endif
243 # ifdef OSF_ALPHA
244 # define LOAD_AVE_TYPE long
245 # endif
247 # if defined (ardent) && defined (titan)
248 # define LOAD_AVE_TYPE long
249 # endif
251 # ifdef tek4300
252 # define LOAD_AVE_TYPE long
253 # endif
255 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
256 # define LOAD_AVE_TYPE long
257 # endif
259 # if defined _AIX && ! defined HAVE_LIBPERFSTAT
260 # define LOAD_AVE_TYPE long
261 # endif
263 # ifdef convex
264 # define LOAD_AVE_TYPE double
265 # ifndef LDAV_CVT
266 # define LDAV_CVT(n) (n)
267 # endif
268 # endif
270 # endif /* No LOAD_AVE_TYPE. */
272 # ifdef OSF_ALPHA
273 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
274 according to ghazi@noc.rutgers.edu. */
275 # undef FSCALE
276 # define FSCALE 1024.0
277 # endif
279 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
280 /* <sys/param.h> defines an incorrect value for FSCALE on an
281 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
282 # undef FSCALE
283 # define FSCALE 100.0
284 # endif
287 # ifndef FSCALE
289 /* SunOS and some others define FSCALE in sys/param.h. */
291 # ifdef MORE_BSD
292 # define FSCALE 2048.0
293 # endif
295 # if defined (MIPS) || defined (SVR4) || defined (decstation)
296 # define FSCALE 256
297 # endif
299 # if defined (sgi) || defined (sequent)
300 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
301 above under #ifdef MIPS. But we want the sgi value. */
302 # undef FSCALE
303 # define FSCALE 1000.0
304 # endif
306 # if defined (ardent) && defined (titan)
307 # define FSCALE 65536.0
308 # endif
310 # ifdef tek4300
311 # define FSCALE 100.0
312 # endif
314 # if defined _AIX && !defined HAVE_LIBPERFSTAT
315 # define FSCALE 65536.0
316 # endif
318 # endif /* Not FSCALE. */
320 # if !defined (LDAV_CVT) && defined (FSCALE)
321 # define LDAV_CVT(n) (((double) (n)) / FSCALE)
322 # endif
324 # ifndef NLIST_STRUCT
325 # if HAVE_NLIST_H
326 # define NLIST_STRUCT
327 # endif
328 # endif
330 # if defined (sgi) || (defined (mips) && !defined (BSD))
331 # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
332 # endif
335 # if !defined (KERNEL_FILE) && defined (sequent)
336 # define KERNEL_FILE "/dynix"
337 # endif
339 # if !defined (KERNEL_FILE) && defined (hpux)
340 # define KERNEL_FILE "/hp-ux"
341 # endif
343 # if !defined (KERNEL_FILE) && (defined (_SEQUENT_) || defined (MIPS) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
344 # define KERNEL_FILE "/unix"
345 # endif
348 # if !defined (LDAV_SYMBOL) && defined (alliant)
349 # define LDAV_SYMBOL "_Loadavg"
350 # endif
352 # if !defined (LDAV_SYMBOL) && ((defined (hpux) && !defined (hp9000s300)) || defined (_SEQUENT_) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)) || (defined (_AIX) && !defined(HAVE_LIBPERFSTAT)))
353 # define LDAV_SYMBOL "avenrun"
354 # endif
356 # include <unistd.h>
358 /* LOAD_AVE_TYPE should only get defined if we're going to use the
359 nlist method. */
360 # if !defined (LOAD_AVE_TYPE) && (defined (BSD) || defined (LDAV_CVT) || defined (KERNEL_FILE) || defined (LDAV_SYMBOL))
361 # define LOAD_AVE_TYPE double
362 # endif
364 # ifdef LOAD_AVE_TYPE
366 # ifndef __VMS
367 # ifndef __linux__
368 # ifndef NLIST_STRUCT
369 # include <a.out.h>
370 # else /* NLIST_STRUCT */
371 # include <nlist.h>
372 # endif /* NLIST_STRUCT */
374 # ifdef SUNOS_5
375 # include <fcntl.h>
376 # include <kvm.h>
377 # include <kstat.h>
378 # endif
380 # if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
381 # include <sys/pstat.h>
382 # endif
384 # ifndef KERNEL_FILE
385 # define KERNEL_FILE "/vmunix"
386 # endif /* KERNEL_FILE */
388 # ifndef LDAV_SYMBOL
389 # define LDAV_SYMBOL "_avenrun"
390 # endif /* LDAV_SYMBOL */
391 # endif /* __linux__ */
393 # else /* __VMS */
395 # ifndef eunice
396 # include <iodef.h>
397 # include <descrip.h>
398 # else /* eunice */
399 # include <vms/iodef.h>
400 # endif /* eunice */
401 # endif /* __VMS */
403 # ifndef LDAV_CVT
404 # define LDAV_CVT(n) ((double) (n))
405 # endif /* !LDAV_CVT */
407 # endif /* LOAD_AVE_TYPE */
409 # if defined HAVE_LIBPERFSTAT
410 # include <libperfstat.h>
411 # include <sys/proc.h>
412 # ifndef SBITS
413 # define SBITS 16
414 # endif
415 # endif
417 # if defined (__GNU__) && !defined (NeXT)
418 /* Note that NeXT Openstep defines __GNU__ even though it should not. */
419 /* GNU system acts much like NeXT, for load average purposes,
420 but not exactly. */
421 # define NeXT
422 # define host_self mach_host_self
423 # endif
425 # ifdef NeXT
426 # ifdef HAVE_MACH_MACH_H
427 # include <mach/mach.h>
428 # else
429 # include <mach.h>
430 # endif
431 # endif /* NeXT */
433 # ifdef sgi
434 # include <sys/sysmp.h>
435 # endif /* sgi */
437 # ifdef UMAX
438 # include <signal.h>
439 # include <sys/time.h>
440 # include <sys/wait.h>
441 # include <sys/syscall.h>
443 # ifdef UMAX_43
444 # include <machine/cpu.h>
445 # include <inq_stats/statistics.h>
446 # include <inq_stats/sysstats.h>
447 # include <inq_stats/cpustats.h>
448 # include <inq_stats/procstats.h>
449 # else /* Not UMAX_43. */
450 # include <sys/sysdefs.h>
451 # include <sys/statistics.h>
452 # include <sys/sysstats.h>
453 # include <sys/cpudefs.h>
454 # include <sys/cpustats.h>
455 # include <sys/procstats.h>
456 # endif /* Not UMAX_43. */
457 # endif /* UMAX */
459 # ifdef DGUX
460 # include <sys/dg_sys_info.h>
461 # endif
463 # include "fcntl--.h"
464 \f
465 /* Avoid static vars inside a function since in HPUX they dump as pure. */
467 # ifdef NeXT
468 static processor_set_t default_set;
469 static bool getloadavg_initialized;
470 # endif /* NeXT */
472 # ifdef UMAX
473 static unsigned int cpus = 0;
474 static unsigned int samples;
475 # endif /* UMAX */
477 # ifdef DGUX
478 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
479 # endif /* DGUX */
481 # if !defined (HAVE_LIBKSTAT) && defined (LOAD_AVE_TYPE)
482 /* File descriptor open to /dev/kmem or VMS load ave driver. */
483 static int channel;
484 /* True iff channel is valid. */
485 static bool getloadavg_initialized;
486 /* Offset in kmem to seek to read load average, or 0 means invalid. */
487 static long offset;
489 # if ! defined __VMS && ! defined sgi && ! defined __linux__
490 static struct nlist nl[2];
491 # endif
493 # ifdef SUNOS_5
494 static kvm_t *kd;
495 # endif /* SUNOS_5 */
497 # endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */
498 \f
499 /* Put the 1 minute, 5 minute and 15 minute load averages
500 into the first NELEM elements of LOADAVG.
501 Return the number written (never more than 3, but may be less than NELEM),
502 or -1 if an error occurred. */
504 int
505 getloadavg (double loadavg[], int nelem)
506 {
507 int elem = 0; /* Return value. */
509 # ifdef NO_GET_LOAD_AVG
510 # define LDAV_DONE
511 /* Set errno to zero to indicate that there was no particular error;
512 this function just can't work at all on this system. */
513 errno = 0;
514 elem = -1;
515 # endif
517 # if !defined (LDAV_DONE) && defined (HAVE_LIBKSTAT)
518 /* Use libkstat because we don't have to be root. */
519 # define LDAV_DONE
520 kstat_ctl_t *kc;
521 kstat_t *ksp;
522 kstat_named_t *kn;
524 kc = kstat_open ();
525 if (kc == 0)
526 return -1;
527 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
528 if (ksp == 0)
529 return -1;
530 if (kstat_read (kc, ksp, 0) == -1)
531 return -1;
534 kn = kstat_data_lookup (ksp, "avenrun_1min");
535 if (kn == 0)
536 {
537 /* Return -1 if no load average information is available. */
538 nelem = 0;
539 elem = -1;
540 }
542 if (nelem >= 1)
543 loadavg[elem++] = (double) kn->value.ul / FSCALE;
545 if (nelem >= 2)
546 {
547 kn = kstat_data_lookup (ksp, "avenrun_5min");
548 if (kn != 0)
549 {
550 loadavg[elem++] = (double) kn->value.ul / FSCALE;
552 if (nelem >= 3)
553 {
554 kn = kstat_data_lookup (ksp, "avenrun_15min");
555 if (kn != 0)
556 loadavg[elem++] = (double) kn->value.ul / FSCALE;
557 }
558 }
559 }
561 kstat_close (kc);
562 # endif /* HAVE_LIBKSTAT */
564 # if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
565 /* Use pstat_getdynamic() because we don't have to be root. */
566 # define LDAV_DONE
567 # undef LOAD_AVE_TYPE
569 struct pst_dynamic dyn_info;
570 if (pstat_getdynamic (&dyn_info, sizeof (dyn_info), 0, 0) < 0)
571 return -1;
572 if (nelem > 0)
573 loadavg[elem++] = dyn_info.psd_avg_1_min;
574 if (nelem > 1)
575 loadavg[elem++] = dyn_info.psd_avg_5_min;
576 if (nelem > 2)
577 loadavg[elem++] = dyn_info.psd_avg_15_min;
579 # endif /* hpux && HAVE_PSTAT_GETDYNAMIC */
581 # if ! defined LDAV_DONE && defined HAVE_LIBPERFSTAT
582 # define LDAV_DONE
583 # undef LOAD_AVE_TYPE
584 /* Use perfstat_cpu_total because we don't have to be root. */
585 {
586 perfstat_cpu_total_t cpu_stats;
587 int result = perfstat_cpu_total (NULL, &cpu_stats, sizeof cpu_stats, 1);
588 if (result == -1)
589 return result;
590 loadavg[0] = cpu_stats.loadavg[0] / (double)(1 << SBITS);
591 loadavg[1] = cpu_stats.loadavg[1] / (double)(1 << SBITS);
592 loadavg[2] = cpu_stats.loadavg[2] / (double)(1 << SBITS);
593 elem = 3;
594 }
595 # endif
597 # if !defined (LDAV_DONE) && (defined (__linux__) || defined (__CYGWIN__))
598 # define LDAV_DONE
599 # undef LOAD_AVE_TYPE
601 # ifndef LINUX_LDAV_FILE
602 # define LINUX_LDAV_FILE "/proc/loadavg"
603 # endif
605 char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
606 char const *ptr = ldavgbuf;
607 int fd, count;
609 fd = open (LINUX_LDAV_FILE, O_RDONLY);
610 if (fd == -1)
611 return -1;
612 count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
613 (void) close (fd);
614 if (count <= 0)
615 return -1;
616 ldavgbuf[count] = '\0';
618 for (elem = 0; elem < nelem; elem++)
619 {
620 char *endptr;
621 double d = c_strtod (ptr, &endptr);
622 if (ptr == endptr)
623 {
624 if (elem == 0)
625 return -1;
626 break;
627 }
628 loadavg[elem] = d;
629 ptr = endptr;
630 }
632 return elem;
634 # endif /* __linux__ || __CYGWIN__ */
636 # if !defined (LDAV_DONE) && defined (__NetBSD__)
637 # define LDAV_DONE
638 # undef LOAD_AVE_TYPE
640 # ifndef NETBSD_LDAV_FILE
641 # define NETBSD_LDAV_FILE "/kern/loadavg"
642 # endif
644 unsigned long int load_ave[3], scale;
645 int count;
646 FILE *fp;
648 fp = fopen (NETBSD_LDAV_FILE, "r");
649 if (fp == NULL)
650 return -1;
651 count = fscanf (fp, "%lu %lu %lu %lu\n",
652 &load_ave[0], &load_ave[1], &load_ave[2],
653 &scale);
654 (void) fclose (fp);
655 if (count != 4)
656 return -1;
658 for (elem = 0; elem < nelem; elem++)
659 loadavg[elem] = (double) load_ave[elem] / (double) scale;
661 return elem;
663 # endif /* __NetBSD__ */
665 # if !defined (LDAV_DONE) && defined (NeXT)
666 # define LDAV_DONE
667 /* The NeXT code was adapted from iscreen 3.2. */
669 host_t host;
670 struct processor_set_basic_info info;
671 unsigned int info_count;
673 /* We only know how to get the 1-minute average for this system,
674 so even if the caller asks for more than 1, we only return 1. */
676 if (!getloadavg_initialized)
677 {
678 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
679 getloadavg_initialized = true;
680 }
682 if (getloadavg_initialized)
683 {
684 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
685 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
686 (processor_set_info_t) &info, &info_count)
687 != KERN_SUCCESS)
688 getloadavg_initialized = false;
689 else
690 {
691 if (nelem > 0)
692 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
693 }
694 }
696 if (!getloadavg_initialized)
697 return -1;
698 # endif /* NeXT */
700 # if !defined (LDAV_DONE) && defined (UMAX)
701 # define LDAV_DONE
702 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
703 have a /dev/kmem. Information about the workings of the running kernel
704 can be gathered with inq_stats system calls.
705 We only know how to get the 1-minute average for this system. */
707 struct proc_summary proc_sum_data;
708 struct stat_descr proc_info;
709 double load;
710 register unsigned int i, j;
712 if (cpus == 0)
713 {
714 register unsigned int c, i;
715 struct cpu_config conf;
716 struct stat_descr desc;
718 desc.sd_next = 0;
719 desc.sd_subsys = SUBSYS_CPU;
720 desc.sd_type = CPUTYPE_CONFIG;
721 desc.sd_addr = (char *) &conf;
722 desc.sd_size = sizeof conf;
724 if (inq_stats (1, &desc))
725 return -1;
727 c = 0;
728 for (i = 0; i < conf.config_maxclass; ++i)
729 {
730 struct class_stats stats;
731 bzero ((char *) &stats, sizeof stats);
733 desc.sd_type = CPUTYPE_CLASS;
734 desc.sd_objid = i;
735 desc.sd_addr = (char *) &stats;
736 desc.sd_size = sizeof stats;
738 if (inq_stats (1, &desc))
739 return -1;
741 c += stats.class_numcpus;
742 }
743 cpus = c;
744 samples = cpus < 2 ? 3 : (2 * cpus / 3);
745 }
747 proc_info.sd_next = 0;
748 proc_info.sd_subsys = SUBSYS_PROC;
749 proc_info.sd_type = PROCTYPE_SUMMARY;
750 proc_info.sd_addr = (char *) &proc_sum_data;
751 proc_info.sd_size = sizeof (struct proc_summary);
752 proc_info.sd_sizeused = 0;
754 if (inq_stats (1, &proc_info) != 0)
755 return -1;
757 load = proc_sum_data.ps_nrunnable;
758 j = 0;
759 for (i = samples - 1; i > 0; --i)
760 {
761 load += proc_sum_data.ps_nrun[j];
762 if (j++ == PS_NRUNSIZE)
763 j = 0;
764 }
766 if (nelem > 0)
767 loadavg[elem++] = load / samples / cpus;
768 # endif /* UMAX */
770 # if !defined (LDAV_DONE) && defined (DGUX)
771 # define LDAV_DONE
772 /* This call can return -1 for an error, but with good args
773 it's not supposed to fail. The first argument is for no
774 apparent reason of type `long int *'. */
775 dg_sys_info ((long int *) &load_info,
776 DG_SYS_INFO_LOAD_INFO_TYPE,
777 DG_SYS_INFO_LOAD_VERSION_0);
779 if (nelem > 0)
780 loadavg[elem++] = load_info.one_minute;
781 if (nelem > 1)
782 loadavg[elem++] = load_info.five_minute;
783 if (nelem > 2)
784 loadavg[elem++] = load_info.fifteen_minute;
785 # endif /* DGUX */
787 # if !defined (LDAV_DONE) && defined (apollo)
788 # define LDAV_DONE
789 /* Apollo code from lisch@mentorg.com (Ray Lischner).
791 This system call is not documented. The load average is obtained as
792 three long integers, for the load average over the past minute,
793 five minutes, and fifteen minutes. Each value is a scaled integer,
794 with 16 bits of integer part and 16 bits of fraction part.
796 I'm not sure which operating system first supported this system call,
797 but I know that SR10.2 supports it. */
799 extern void proc1_$get_loadav ();
800 unsigned long load_ave[3];
802 proc1_$get_loadav (load_ave);
804 if (nelem > 0)
805 loadavg[elem++] = load_ave[0] / 65536.0;
806 if (nelem > 1)
807 loadavg[elem++] = load_ave[1] / 65536.0;
808 if (nelem > 2)
809 loadavg[elem++] = load_ave[2] / 65536.0;
810 # endif /* apollo */
812 # if !defined (LDAV_DONE) && defined (OSF_MIPS)
813 # define LDAV_DONE
815 struct tbl_loadavg load_ave;
816 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
817 loadavg[elem++]
818 = (load_ave.tl_lscale == 0
819 ? load_ave.tl_avenrun.d[0]
820 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
821 # endif /* OSF_MIPS */
823 # if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
824 # define LDAV_DONE
826 /* A faithful emulation is going to have to be saved for a rainy day. */
827 for ( ; elem < nelem; elem++)
828 {
829 loadavg[elem] = 0.0;
830 }
831 # endif /* __MSDOS__ || WINDOWS32 */
833 # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
834 # define LDAV_DONE
836 struct tbl_loadavg load_ave;
837 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
838 for (elem = 0; elem < nelem; elem++)
839 loadavg[elem]
840 = (load_ave.tl_lscale == 0
841 ? load_ave.tl_avenrun.d[elem]
842 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
843 # endif /* OSF_ALPHA */
845 # if ! defined LDAV_DONE && defined __VMS
846 /* VMS specific code -- read from the Load Ave driver. */
848 LOAD_AVE_TYPE load_ave[3];
849 static bool getloadavg_initialized;
850 # ifdef eunice
851 struct
852 {
853 int dsc$w_length;
854 char *dsc$a_pointer;
855 } descriptor;
856 # endif
858 /* Ensure that there is a channel open to the load ave device. */
859 if (!getloadavg_initialized)
860 {
861 /* Attempt to open the channel. */
862 # ifdef eunice
863 descriptor.dsc$w_length = 18;
864 descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
865 # else
866 $DESCRIPTOR (descriptor, "LAV0:");
867 # endif
868 if (sys$assign (&descriptor, &channel, 0, 0) & 1)
869 getloadavg_initialized = true;
870 }
872 /* Read the load average vector. */
873 if (getloadavg_initialized
874 && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
875 load_ave, 12, 0, 0, 0, 0) & 1))
876 {
877 sys$dassgn (channel);
878 getloadavg_initialized = false;
879 }
881 if (!getloadavg_initialized)
882 return -1;
883 # endif /* ! defined LDAV_DONE && defined __VMS */
885 # if ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS
887 /* UNIX-specific code -- read the average from /dev/kmem. */
889 # define LDAV_PRIVILEGED /* This code requires special installation. */
891 LOAD_AVE_TYPE load_ave[3];
893 /* Get the address of LDAV_SYMBOL. */
894 if (offset == 0)
895 {
896 # ifndef sgi
897 # ifndef NLIST_STRUCT
898 strcpy (nl[0].n_name, LDAV_SYMBOL);
899 strcpy (nl[1].n_name, "");
900 # else /* NLIST_STRUCT */
901 # ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
902 nl[0].n_un.n_name = LDAV_SYMBOL;
903 nl[1].n_un.n_name = 0;
904 # else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
905 nl[0].n_name = LDAV_SYMBOL;
906 nl[1].n_name = 0;
907 # endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
908 # endif /* NLIST_STRUCT */
910 # ifndef SUNOS_5
911 if (
912 # if !(defined (_AIX) && !defined (ps2))
913 nlist (KERNEL_FILE, nl)
914 # else /* _AIX */
915 knlist (nl, 1, sizeof (nl[0]))
916 # endif
917 >= 0)
918 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
919 {
920 # ifdef FIXUP_KERNEL_SYMBOL_ADDR
921 FIXUP_KERNEL_SYMBOL_ADDR (nl);
922 # endif
923 offset = nl[0].n_value;
924 }
925 # endif /* !SUNOS_5 */
926 # else /* sgi */
927 int ldav_off;
929 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
930 if (ldav_off != -1)
931 offset = (long int) ldav_off & 0x7fffffff;
932 # endif /* sgi */
933 }
935 /* Make sure we have /dev/kmem open. */
936 if (!getloadavg_initialized)
937 {
938 # ifndef SUNOS_5
939 channel = open ("/dev/kmem", O_RDONLY);
940 if (channel >= 0)
941 {
942 /* Set the channel to close on exec, so it does not
943 litter any child's descriptor table. */
944 set_cloexec_flag (channel, true);
945 getloadavg_initialized = true;
946 }
947 # else /* SUNOS_5 */
948 /* We pass 0 for the kernel, corefile, and swapfile names
949 to use the currently running kernel. */
950 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
951 if (kd != 0)
952 {
953 /* nlist the currently running kernel. */
954 kvm_nlist (kd, nl);
955 offset = nl[0].n_value;
956 getloadavg_initialized = true;
957 }
958 # endif /* SUNOS_5 */
959 }
961 /* If we can, get the load average values. */
962 if (offset && getloadavg_initialized)
963 {
964 /* Try to read the load. */
965 # ifndef SUNOS_5
966 if (lseek (channel, offset, 0) == -1L
967 || read (channel, (char *) load_ave, sizeof (load_ave))
968 != sizeof (load_ave))
969 {
970 close (channel);
971 getloadavg_initialized = false;
972 }
973 # else /* SUNOS_5 */
974 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
975 != sizeof (load_ave))
976 {
977 kvm_close (kd);
978 getloadavg_initialized = false;
979 }
980 # endif /* SUNOS_5 */
981 }
983 if (offset == 0 || !getloadavg_initialized)
984 return -1;
985 # endif /* ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS */
987 # if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
988 if (nelem > 0)
989 loadavg[elem++] = LDAV_CVT (load_ave[0]);
990 if (nelem > 1)
991 loadavg[elem++] = LDAV_CVT (load_ave[1]);
992 if (nelem > 2)
993 loadavg[elem++] = LDAV_CVT (load_ave[2]);
995 # define LDAV_DONE
996 # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
998 # if !defined LDAV_DONE
999 /* Set errno to zero to indicate that there was no particular error;
1000 this function just can't work at all on this system. */
1001 errno = 0;
1002 elem = -1;
1003 # endif
1004 return elem;
1005 }
1007 #endif /* ! HAVE_GETLOADAVG */
1008 \f
1009 #ifdef TEST
1010 int
1011 main (int argc, char **argv)
1012 {
1013 int naptime = 0;
1015 if (argc > 1)
1016 naptime = atoi (argv[1]);
1018 while (1)
1019 {
1020 double avg[3];
1021 int loads;
1023 errno = 0; /* Don't be misled if it doesn't set errno. */
1024 loads = getloadavg (avg, 3);
1025 if (loads == -1)
1026 {
1027 perror ("Error getting load average");
1028 return EXIT_FAILURE;
1029 }
1030 if (loads > 0)
1031 printf ("1-minute: %f ", avg[0]);
1032 if (loads > 1)
1033 printf ("5-minute: %f ", avg[1]);
1034 if (loads > 2)
1035 printf ("15-minute: %f ", avg[2]);
1036 if (loads > 0)
1037 putchar ('\n');
1039 if (naptime == 0)
1040 break;
1041 sleep (naptime);
1042 }
1044 return EXIT_SUCCESS;
1045 }
1046 #endif /* TEST */