1 /* Get the system load averages.
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995
3 Free Software Foundation, Inc.
5 This file is part of XEmacs.
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
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
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. */
24 /* Compile-time symbols that this file uses:
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.
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,
38 NLIST_NAME_UNION struct nlist has an n_un member, not n_name.
39 LINUX_LDAV_FILE [__linux__]: File containing load averages.
41 Specific system predefines this file uses, aside from setting
42 default values if not emacs:
45 BSD Real BSD, not just BSD-like.
49 MSDOS No-op for MSDOS.
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)
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 __OpenBSD__ OpenBSD: ditto.
63 In addition, to avoid nesting many #ifdefs, we internally set
64 LDAV_DONE to indicate that the load average has been computed.
66 We also #define LDAV_PRIVILEGED if a program will require
67 special installation to be able to call getloadavg. */
69 /* This should always be first. */
74 #include <sys/types.h>
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. */
80 #include <sys/param.h>
85 #include "sysfile.h" /* for encapsulated open, close, read, write */
88 /* Exclude all the code except the test program at the end
89 if the system has its own `getloadavg' function.
91 The declaration of `errno' is needed by the test program
92 as well as the function itself, so it comes first. */
100 #ifndef HAVE_GETLOADAVG
102 /* The existing Emacs configuration files define a macro called
103 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
104 returns the load average multiplied by 100. What we actually want
105 is a macro called LDAV_CVT, which returns the load average as an
108 For backwards compatibility, we'll define LDAV_CVT in terms of
109 LOAD_AVE_CVT, but future machine config files should just define
110 LDAV_CVT directly. */
112 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
113 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
117 #if defined (HAVE_KSTAT_H)
119 #endif /* HAVE_KSTAT_H */
122 #if !defined (BSD) && defined (ultrix)
123 /* Ultrix behaves like BSD on Vaxen. */
128 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
129 conflicts with the definition understood in this file, that this
133 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
134 defined to mean that the nlist method should be used, which is not true. */
138 /* Set values that are different from the defaults, which are
139 set a little farther down with #ifndef. */
142 /* Some shorthands. */
144 #if defined (HPUX) && !defined (hpux)
148 #if defined(hp300) && !defined(hpux)
152 #if defined(ultrix) && defined(mips)
156 #if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
160 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
163 #include <netinet/in.h> /* Needed for Digital UNIX V3 */
164 #include <net/proto_net.h>
165 #include <sys/table.h>
168 #if defined (__osf__) && (defined (mips) || defined (__mips__))
170 #include <sys/table.h>
173 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
174 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
175 that with a couple of other things and we'll have a unique match. */
176 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
177 #define tek4300 /* Define by emacs, but not by other users. */
181 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
182 #ifndef LOAD_AVE_TYPE
185 #define LOAD_AVE_TYPE long
189 #define LOAD_AVE_TYPE long
193 #define LOAD_AVE_TYPE long
197 #define LOAD_AVE_TYPE long
201 #define LOAD_AVE_TYPE long
205 #define LOAD_AVE_TYPE long
209 #define LOAD_AVE_TYPE long
213 #define LOAD_AVE_TYPE long
217 #define LOAD_AVE_TYPE long
220 #if defined (ardent) && defined (titan)
221 #define LOAD_AVE_TYPE long
225 #define LOAD_AVE_TYPE long
228 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
229 #define LOAD_AVE_TYPE long
233 #define LOAD_AVE_TYPE long
237 #define LOAD_AVE_TYPE double
239 #define LDAV_CVT(n) (n)
243 #endif /* No LOAD_AVE_TYPE. */
246 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
247 according to ghazi@noc.rutgers.edu. */
249 #define FSCALE 1024.0
252 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
253 /* <sys/param.h> defines an incorrect value for FSCALE on an
254 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
262 /* SunOS and some others define FSCALE in sys/param.h. */
265 #define FSCALE 2048.0
268 #if defined(MIPS) || defined(SVR4) || defined(decstation)
272 #if defined (sgi) || defined (sequent)
273 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
274 above under #ifdef MIPS. But we want the sgi value. */
276 #define FSCALE 1000.0
279 #if defined (ardent) && defined (titan)
280 #define FSCALE 65536.0
288 #define FSCALE 65536.0
291 #endif /* Not FSCALE. */
293 #if !defined (LDAV_CVT) && defined (FSCALE)
294 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
297 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
316 #if defined (_SEQUENT_) || defined (sequent)
336 #if defined (ardent) && defined (titan)
348 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
356 #endif /* defined (NLIST_STRUCT) */
359 #if defined(sgi) || (defined(mips) && !defined(BSD))
360 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
364 #if !defined (KERNEL_FILE) && defined (sequent)
365 #define KERNEL_FILE "/dynix"
368 #if !defined (KERNEL_FILE) && defined (hpux)
369 #define KERNEL_FILE "/hp-ux"
372 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
373 #define KERNEL_FILE "/unix"
377 #if !defined (LDAV_SYMBOL) && defined (alliant)
378 #define LDAV_SYMBOL "_Loadavg"
381 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
382 #define LDAV_SYMBOL "avenrun"
391 /* LOAD_AVE_TYPE should only get defined if we're going to use the
393 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
394 #define LOAD_AVE_TYPE double
401 #else /* NLIST_STRUCT */
403 #endif /* NLIST_STRUCT */
411 #define KERNEL_FILE "/vmunix"
412 #endif /* KERNEL_FILE */
415 #define LDAV_SYMBOL "_avenrun"
416 #endif /* LDAV_SYMBOL */
419 #define LDAV_CVT(n) ((double) (n))
420 #endif /* !LDAV_CVT */
422 #endif /* LOAD_AVE_TYPE */
425 #ifdef HAVE_MACH_MACH_H
426 #include <mach/mach.h>
433 #include <sys/sysmp.h>
439 #include <sys/time.h>
440 #include <sys/wait.h>
441 #include <sys/syscall.h>
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. */
460 #include <sys/dg_sys_info.h>
464 #if defined (HAVE_SYS_PSTAT_H)
465 #include <sys/pstat.h>
466 #endif /* HAVE_SYS_PSTAT_H (on HPUX) */
469 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
472 #include <sys/file.h>
475 /* Avoid static vars inside a function since in HPUX they dump as pure. */
478 static processor_set_t default_set;
479 static int getloadavg_initialized;
483 static unsigned int cpus = 0;
484 static unsigned int samples;
488 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
492 /* File descriptor open to /dev/kmem */
494 /* Nonzero iff channel is valid. */
495 static int getloadavg_initialized;
496 /* Offset in kmem to seek to read load average, or 0 means invalid. */
500 static struct nlist nl[2];
508 # define countof(x) (sizeof (x) / sizeof (*(x)))
511 #endif /* LOAD_AVE_TYPE */
513 /* Put the 1 minute, 5 minute and 15 minute load averages
514 into the first NELEM elements of LOADAVG.
515 Return the number written (never more than 3, but may be less than NELEM),
516 or -1 if an error occurred. */
519 getloadavg (double loadavg[], int nelem)
521 int elem = 0; /* Return value. */
523 #ifdef NO_GET_LOAD_AVG
525 /* Set errno to zero to indicate that there was no particular error;
526 this function just can't work at all on this system. */
529 #endif /* NO_GET_LOAD_AVG */
531 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT)
533 /* getloadavg is best implemented using kstat (kernel stats), on
534 systems (like SunOS5) that support it, since you don't need special
535 privileges to use it.
537 Initial implementation courtesy Zlatko Calusic <zcalusic@carnet.hr>.
538 Integrated to XEmacs by Hrvoje Niksic <hniksic@srce.hr>.
539 Additional cleanup by Hrvoje Niksic, based on code published by
540 Casper Dik <Casper.Dik@Holland.Sun.Com>. */
543 static char *avestrings[] = { "avenrun_1min",
547 if (nelem > countof (avestrings))
548 nelem = countof (avestrings);
553 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
559 if (kstat_read (kc, ksp, 0) < 0)
564 for (elem = 0; elem < nelem; elem++)
567 (kstat_named_t *) kstat_data_lookup (ksp, avestrings[elem]);
573 loadavg[elem] = (double)kn->value.ul / FSCALE;
576 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */
578 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H)
580 /* This is totally undocumented, and is not guaranteed to work, but
581 mayhap it might .... If it does work, it will work only on HP-UX
582 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */
583 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */
586 struct pst_dynamic procinfo;
589 statbuf.pst_dynamic = &procinfo;
590 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1)
592 loadavg[elem++] = procinfo.psd_avg_1_min;
593 loadavg[elem++] = procinfo.psd_avg_5_min;
594 loadavg[elem++] = procinfo.psd_avg_15_min;
597 #if !defined (LDAV_DONE) && defined (__linux__)
601 #ifndef LINUX_LDAV_FILE
602 #define LINUX_LDAV_FILE "/proc/loadavg"
609 fd = open (LINUX_LDAV_FILE, O_RDONLY);
612 count = read (fd, ldavgbuf, 40);
617 count = sscanf (ldavgbuf, "%lf %lf %lf",
618 &load_ave[0], &load_ave[1], &load_ave[2]);
622 for (elem = 0; elem < nelem && elem < count; elem++)
623 loadavg[elem] = load_ave[elem];
624 #endif /* __linux__ */
626 #if !defined (LDAV_DONE) && defined (__NetBSD__) || defined (__OpenBSD__)
630 #ifndef NETBSD_LDAV_FILE
631 #define NETBSD_LDAV_FILE "/kern/loadavg"
634 unsigned long int load_ave[3], scale;
638 fp = fopen (NETBSD_LDAV_FILE, "r");
641 count = fscanf (fp, "%lu %lu %lu %lu\n",
642 &load_ave[0], &load_ave[1], &load_ave[2],
648 for (elem = 0; elem < nelem; elem++)
649 loadavg[elem] = (double) load_ave[elem] / (double) scale;
650 #endif /* __NetBSD__ or __OpenBSD__ */
652 #if !defined (LDAV_DONE) && defined (NeXT)
654 /* The NeXT code was adapted from iscreen 3.2. */
657 struct processor_set_basic_info info;
660 /* We only know how to get the 1-minute average for this system,
661 so even if the caller asks for more than 1, we only return 1. */
663 if (!getloadavg_initialized)
665 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
666 getloadavg_initialized = 1;
669 if (getloadavg_initialized)
671 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
672 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
673 (processor_set_info_t) &info, &info_count)
675 getloadavg_initialized = 0;
679 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
683 if (!getloadavg_initialized)
687 #if !defined (LDAV_DONE) && defined (UMAX)
689 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
690 have a /dev/kmem. Information about the workings of the running kernel
691 can be gathered with inq_stats system calls.
692 We only know how to get the 1-minute average for this system. */
694 struct proc_summary proc_sum_data;
695 struct stat_descr proc_info;
697 REGISTER unsigned int i, j;
701 REGISTER unsigned int c, i;
702 struct cpu_config conf;
703 struct stat_descr desc;
706 desc.sd_subsys = SUBSYS_CPU;
707 desc.sd_type = CPUTYPE_CONFIG;
708 desc.sd_addr = (char *) &conf;
709 desc.sd_size = sizeof conf;
711 if (inq_stats (1, &desc))
715 for (i = 0; i < conf.config_maxclass; ++i)
717 struct class_stats stats;
718 memset ((char *) &stats, 0, sizeof stats);
720 desc.sd_type = CPUTYPE_CLASS;
722 desc.sd_addr = (char *) &stats;
723 desc.sd_size = sizeof stats;
725 if (inq_stats (1, &desc))
728 c += stats.class_numcpus;
731 samples = cpus < 2 ? 3 : (2 * cpus / 3);
734 proc_info.sd_next = 0;
735 proc_info.sd_subsys = SUBSYS_PROC;
736 proc_info.sd_type = PROCTYPE_SUMMARY;
737 proc_info.sd_addr = (char *) &proc_sum_data;
738 proc_info.sd_size = sizeof (struct proc_summary);
739 proc_info.sd_sizeused = 0;
741 if (inq_stats (1, &proc_info) != 0)
744 load = proc_sum_data.ps_nrunnable;
746 for (i = samples - 1; i > 0; --i)
748 load += proc_sum_data.ps_nrun[j];
749 if (j++ == PS_NRUNSIZE)
754 loadavg[elem++] = load / samples / cpus;
757 #if !defined (LDAV_DONE) && defined (DGUX)
759 /* This call can return -1 for an error, but with good args
760 it's not supposed to fail. The first argument is for no
761 apparent reason of type `long int *'. */
762 dg_sys_info ((long int *) &load_info,
763 DG_SYS_INFO_LOAD_INFO_TYPE,
764 DG_SYS_INFO_LOAD_VERSION_0);
767 loadavg[elem++] = load_info.one_minute;
769 loadavg[elem++] = load_info.five_minute;
771 loadavg[elem++] = load_info.fifteen_minute;
774 #if !defined (LDAV_DONE) && defined (apollo)
776 /* Apollo code from lisch@mentorg.com (Ray Lischner).
778 This system call is not documented. The load average is obtained as
779 three long integers, for the load average over the past minute,
780 five minutes, and fifteen minutes. Each value is a scaled integer,
781 with 16 bits of integer part and 16 bits of fraction part.
783 I'm not sure which operating system first supported this system call,
784 but I know that SR10.2 supports it. */
786 extern void proc1_$get_loadav ();
787 unsigned long load_ave[3];
789 proc1_$get_loadav (load_ave);
792 loadavg[elem++] = load_ave[0] / 65536.0;
794 loadavg[elem++] = load_ave[1] / 65536.0;
796 loadavg[elem++] = load_ave[2] / 65536.0;
799 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
802 struct tbl_loadavg load_ave;
803 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
805 = (load_ave.tl_lscale == 0
806 ? load_ave.tl_avenrun.d[0]
807 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
808 #endif /* OSF_MIPS */
810 #if !defined (LDAV_DONE) && (defined (MSDOS) || defined (WIN32))
813 /* A faithful emulation is going to have to be saved for a rainy day. */
814 for ( ; elem < nelem; elem++)
820 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
823 struct tbl_loadavg load_ave;
824 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
825 for (elem = 0; elem < nelem; elem++)
827 = (load_ave.tl_lscale == 0
828 ? load_ave.tl_avenrun.d[elem]
829 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
830 #endif /* OSF_ALPHA */
832 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE)
834 /* UNIX-specific code -- read the average from /dev/kmem. */
836 #define LDAV_PRIVILEGED /* This code requires special installation. */
838 LOAD_AVE_TYPE load_ave[3];
840 /* Get the address of LDAV_SYMBOL. */
845 strcpy (nl[0].n_name, LDAV_SYMBOL);
846 strcpy (nl[1].n_name, "");
847 #else /* NLIST_STRUCT */
848 #ifdef NLIST_NAME_UNION
849 nl[0].n_un.n_name = LDAV_SYMBOL;
850 nl[1].n_un.n_name = 0;
851 #else /* not NLIST_NAME_UNION */
852 nl[0].n_name = (char *) LDAV_SYMBOL;
854 #endif /* not NLIST_NAME_UNION */
855 #endif /* NLIST_STRUCT */
859 #if !(defined (_AIX) && !defined (ps2))
860 nlist (KERNEL_FILE, nl)
862 knlist (nl, 1, sizeof (nl[0]))
865 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
867 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
868 FIXUP_KERNEL_SYMBOL_ADDR (nl);
870 offset = nl[0].n_value;
872 #endif /* !SUNOS_5 */
876 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
878 offset = (long) ldav_off & 0x7fffffff;
882 /* Make sure we have /dev/kmem open. */
883 if (!getloadavg_initialized)
886 channel = open ("/dev/kmem", 0);
889 /* Set the channel to close on exec, so it does not
890 litter any child's descriptor table. */
895 (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
897 getloadavg_initialized = 1;
900 /* We pass 0 for the kernel, corefile, and swapfile names
901 to use the currently running kernel. */
902 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
905 /* nlist the currently running kernel. */
907 offset = nl[0].n_value;
908 getloadavg_initialized = 1;
913 /* If we can, get the load average values. */
914 if (offset && getloadavg_initialized)
916 /* Try to read the load. */
918 if (lseek (channel, offset, 0) == -1L
919 || read (channel, (char *) load_ave, sizeof (load_ave))
920 != sizeof (load_ave))
923 getloadavg_initialized = 0;
926 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
927 != sizeof (load_ave))
930 getloadavg_initialized = 0;
935 if (offset == 0 || !getloadavg_initialized)
939 loadavg[elem++] = LDAV_CVT (load_ave[0]);
941 loadavg[elem++] = LDAV_CVT (load_ave[1]);
943 loadavg[elem++] = LDAV_CVT (load_ave[2]);
946 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
951 #endif /* ! HAVE_GETLOADAVG */
955 main (int argc, char **argv)
960 naptime = atoi (argv[1]);
967 errno = 0; /* Don't be misled if it doesn't set errno. */
968 loads = getloadavg (avg, 3);
971 perror ("Error getting load average");
975 printf ("1-minute: %f ", avg[0]);
977 printf ("5-minute: %f ", avg[1]);
979 printf ("15-minute: %f ", avg[2]);
994 /* Emulate getloadavg. */
996 getloadavg (double loadavg[], int nelem)
1000 /* A faithful emulation is going to have to be saved for a rainy day. */
1001 for (i = 0; i < nelem; i++)
1008 #endif /*__GNUWIN32__*/