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: dito.
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++)
566 kstat_named_t *kn = kstat_data_lookup (ksp, avestrings[elem]);
572 loadavg[elem] = (double)kn->value.ul / FSCALE;
575 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */
577 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H)
579 /* This is totally undocumented, and is not guaranteed to work, but
580 mayhap it might .... If it does work, it will work only on HP-UX
581 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */
582 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */
585 struct pst_dynamic procinfo;
588 statbuf.pst_dynamic = &procinfo;
589 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1)
591 loadavg[elem++] = procinfo.psd_avg_1_min;
592 loadavg[elem++] = procinfo.psd_avg_5_min;
593 loadavg[elem++] = procinfo.psd_avg_15_min;
596 #if !defined (LDAV_DONE) && defined (__linux__)
600 #ifndef LINUX_LDAV_FILE
601 #define LINUX_LDAV_FILE "/proc/loadavg"
608 fd = open (LINUX_LDAV_FILE, O_RDONLY);
611 count = read (fd, ldavgbuf, 40);
616 count = sscanf (ldavgbuf, "%lf %lf %lf",
617 &load_ave[0], &load_ave[1], &load_ave[2]);
621 for (elem = 0; elem < nelem && elem < count; elem++)
622 loadavg[elem] = load_ave[elem];
623 #endif /* __linux__ */
625 #if !defined (LDAV_DONE) && defined (__NetBSD__) || defined (__OpenBSD__)
629 #ifndef NETBSD_LDAV_FILE
630 #define NETBSD_LDAV_FILE "/kern/loadavg"
633 unsigned long int load_ave[3], scale;
637 fp = fopen (NETBSD_LDAV_FILE, "r");
640 count = fscanf (fp, "%lu %lu %lu %lu\n",
641 &load_ave[0], &load_ave[1], &load_ave[2],
647 for (elem = 0; elem < nelem; elem++)
648 loadavg[elem] = (double) load_ave[elem] / (double) scale;
649 #endif /* __NetBSD__ or __OpenBSD__ */
651 #if !defined (LDAV_DONE) && defined (NeXT)
653 /* The NeXT code was adapted from iscreen 3.2. */
656 struct processor_set_basic_info info;
659 /* We only know how to get the 1-minute average for this system,
660 so even if the caller asks for more than 1, we only return 1. */
662 if (!getloadavg_initialized)
664 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
665 getloadavg_initialized = 1;
668 if (getloadavg_initialized)
670 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
671 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
672 (processor_set_info_t) &info, &info_count)
674 getloadavg_initialized = 0;
678 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
682 if (!getloadavg_initialized)
686 #if !defined (LDAV_DONE) && defined (UMAX)
688 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
689 have a /dev/kmem. Information about the workings of the running kernel
690 can be gathered with inq_stats system calls.
691 We only know how to get the 1-minute average for this system. */
693 struct proc_summary proc_sum_data;
694 struct stat_descr proc_info;
696 REGISTER unsigned int i, j;
700 REGISTER unsigned int c, i;
701 struct cpu_config conf;
702 struct stat_descr desc;
705 desc.sd_subsys = SUBSYS_CPU;
706 desc.sd_type = CPUTYPE_CONFIG;
707 desc.sd_addr = (char *) &conf;
708 desc.sd_size = sizeof conf;
710 if (inq_stats (1, &desc))
714 for (i = 0; i < conf.config_maxclass; ++i)
716 struct class_stats stats;
717 memset ((char *) &stats, 0, sizeof stats);
719 desc.sd_type = CPUTYPE_CLASS;
721 desc.sd_addr = (char *) &stats;
722 desc.sd_size = sizeof stats;
724 if (inq_stats (1, &desc))
727 c += stats.class_numcpus;
730 samples = cpus < 2 ? 3 : (2 * cpus / 3);
733 proc_info.sd_next = 0;
734 proc_info.sd_subsys = SUBSYS_PROC;
735 proc_info.sd_type = PROCTYPE_SUMMARY;
736 proc_info.sd_addr = (char *) &proc_sum_data;
737 proc_info.sd_size = sizeof (struct proc_summary);
738 proc_info.sd_sizeused = 0;
740 if (inq_stats (1, &proc_info) != 0)
743 load = proc_sum_data.ps_nrunnable;
745 for (i = samples - 1; i > 0; --i)
747 load += proc_sum_data.ps_nrun[j];
748 if (j++ == PS_NRUNSIZE)
753 loadavg[elem++] = load / samples / cpus;
756 #if !defined (LDAV_DONE) && defined (DGUX)
758 /* This call can return -1 for an error, but with good args
759 it's not supposed to fail. The first argument is for no
760 apparent reason of type `long int *'. */
761 dg_sys_info ((long int *) &load_info,
762 DG_SYS_INFO_LOAD_INFO_TYPE,
763 DG_SYS_INFO_LOAD_VERSION_0);
766 loadavg[elem++] = load_info.one_minute;
768 loadavg[elem++] = load_info.five_minute;
770 loadavg[elem++] = load_info.fifteen_minute;
773 #if !defined (LDAV_DONE) && defined (apollo)
775 /* Apollo code from lisch@mentorg.com (Ray Lischner).
777 This system call is not documented. The load average is obtained as
778 three long integers, for the load average over the past minute,
779 five minutes, and fifteen minutes. Each value is a scaled integer,
780 with 16 bits of integer part and 16 bits of fraction part.
782 I'm not sure which operating system first supported this system call,
783 but I know that SR10.2 supports it. */
785 extern void proc1_$get_loadav ();
786 unsigned long load_ave[3];
788 proc1_$get_loadav (load_ave);
791 loadavg[elem++] = load_ave[0] / 65536.0;
793 loadavg[elem++] = load_ave[1] / 65536.0;
795 loadavg[elem++] = load_ave[2] / 65536.0;
798 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
801 struct tbl_loadavg load_ave;
802 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
804 = (load_ave.tl_lscale == 0
805 ? load_ave.tl_avenrun.d[0]
806 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
807 #endif /* OSF_MIPS */
809 #if !defined (LDAV_DONE) && (defined (MSDOS) || defined (WIN32))
812 /* A faithful emulation is going to have to be saved for a rainy day. */
813 for ( ; elem < nelem; elem++)
819 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
822 struct tbl_loadavg load_ave;
823 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
824 for (elem = 0; elem < nelem; elem++)
826 = (load_ave.tl_lscale == 0
827 ? load_ave.tl_avenrun.d[elem]
828 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
829 #endif /* OSF_ALPHA */
831 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE)
833 /* UNIX-specific code -- read the average from /dev/kmem. */
835 #define LDAV_PRIVILEGED /* This code requires special installation. */
837 LOAD_AVE_TYPE load_ave[3];
839 /* Get the address of LDAV_SYMBOL. */
844 strcpy (nl[0].n_name, LDAV_SYMBOL);
845 strcpy (nl[1].n_name, "");
846 #else /* NLIST_STRUCT */
847 #ifdef NLIST_NAME_UNION
848 nl[0].n_un.n_name = LDAV_SYMBOL;
849 nl[1].n_un.n_name = 0;
850 #else /* not NLIST_NAME_UNION */
851 nl[0].n_name = (char *) LDAV_SYMBOL;
853 #endif /* not NLIST_NAME_UNION */
854 #endif /* NLIST_STRUCT */
858 #if !(defined (_AIX) && !defined (ps2))
859 nlist (KERNEL_FILE, nl)
861 knlist (nl, 1, sizeof (nl[0]))
864 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
866 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
867 FIXUP_KERNEL_SYMBOL_ADDR (nl);
869 offset = nl[0].n_value;
871 #endif /* !SUNOS_5 */
875 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
877 offset = (long) ldav_off & 0x7fffffff;
881 /* Make sure we have /dev/kmem open. */
882 if (!getloadavg_initialized)
885 channel = open ("/dev/kmem", 0);
888 /* Set the channel to close on exec, so it does not
889 litter any child's descriptor table. */
894 (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
896 getloadavg_initialized = 1;
899 /* We pass 0 for the kernel, corefile, and swapfile names
900 to use the currently running kernel. */
901 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
904 /* nlist the currently running kernel. */
906 offset = nl[0].n_value;
907 getloadavg_initialized = 1;
912 /* If we can, get the load average values. */
913 if (offset && getloadavg_initialized)
915 /* Try to read the load. */
917 if (lseek (channel, offset, 0) == -1L
918 || read (channel, (char *) load_ave, sizeof (load_ave))
919 != sizeof (load_ave))
922 getloadavg_initialized = 0;
925 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
926 != sizeof (load_ave))
929 getloadavg_initialized = 0;
934 if (offset == 0 || !getloadavg_initialized)
938 loadavg[elem++] = LDAV_CVT (load_ave[0]);
940 loadavg[elem++] = LDAV_CVT (load_ave[1]);
942 loadavg[elem++] = LDAV_CVT (load_ave[2]);
945 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
950 #endif /* ! HAVE_GETLOADAVG */
954 main (int argc, char **argv)
959 naptime = atoi (argv[1]);
966 errno = 0; /* Don't be misled if it doesn't set errno. */
967 loads = getloadavg (avg, 3);
970 perror ("Error getting load average");
974 printf ("1-minute: %f ", avg[0]);
976 printf ("5-minute: %f ", avg[1]);
978 printf ("15-minute: %f ", avg[2]);
993 /* Emulate getloadavg. */
995 getloadavg (double loadavg[], int nelem)
999 /* A faithful emulation is going to have to be saved for a rainy day. */
1000 for (i = 0; i < nelem; i++)
1007 #endif /*__GNUWIN32__*/