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 program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 /* Compile-time symbols that this file uses:
21 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
22 KERNEL_FILE Pathname of the kernel to nlist.
23 LDAV_CVT() Scale the load average from the kernel.
25 LDAV_SYMBOL Name of kernel symbol giving load average.
26 LOAD_AVE_TYPE Type of the load average array in the kernel.
27 Must be defined unless one of
28 apollo, DGUX, NeXT, or UMAX is defined;
29 otherwise, no load average is available.
30 NLIST_STRUCT Include nlist.h, not a.out.h, and
31 the nlist n_name element is a pointer,
33 NLIST_NAME_UNION struct nlist has an n_un member, not n_name.
34 LINUX_LDAV_FILE [__linux__]: File containing load averages.
36 Specific system predefines this file uses, aside from setting
37 default values if not emacs:
40 BSD Real BSD, not just BSD-like.
43 eunice UNIX emulator under VMS.
47 sequent Sequent Dynix 3.x.x (BSD)
48 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
49 sony_news NEWS-OS (works at least for 4.1C)
53 __linux__ Linux: assumes /proc filesystem mounted.
54 Support from Michael K. Johnson.
55 __NetBSD__ NetBSD: assumes /kern filesystem mounted.
57 In addition, to avoid nesting many #ifdefs, we internally set
58 LDAV_DONE to indicate that the load average has been computed.
60 We also #define LDAV_PRIVILEGED if a program will require
61 special installation to be able to call getloadavg. */
63 /* This should always be first. */
68 #include <sys/types.h>
70 /* Both the Emacs and non-Emacs sections want this. Some
71 configuration files' definitions for the LOAD_AVE_CVT macro (like
72 sparc.h's) use macros like FSCALE, defined here. */
74 #include <sys/param.h>
78 /* Exclude all the code except the test program at the end
79 if the system has its own `getloadavg' function.
81 The declaration of `errno' is needed by the test program
82 as well as the function itself, so it comes first. */
90 #ifndef HAVE_GETLOADAVG
93 /* The existing Emacs configuration files define a macro called
94 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
95 returns the load average multiplied by 100. What we actually want
96 is a macro called LDAV_CVT, which returns the load average as an
99 For backwards compatibility, we'll define LDAV_CVT in terms of
100 LOAD_AVE_CVT, but future machine config files should just define
101 LDAV_CVT directly. */
103 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
104 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
107 #if !defined (BSD) && defined (ultrix)
108 /* Ultrix behaves like BSD on Vaxen. */
113 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
114 conflicts with the definition understood in this file, that this
118 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
119 defined to mean that the nlist method should be used, which is not true. */
123 /* Set values that are different from the defaults, which are
124 set a little farther down with #ifndef. */
127 /* Some shorthands. */
129 #if defined (HPUX) && !defined (hpux)
133 #if defined(hp300) && !defined(hpux)
137 #if defined(ultrix) && defined(mips)
141 #if defined(sun) && defined(SVR4)
145 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
147 #include <sys/table.h>
150 #if defined (__osf__) && (defined (mips) || defined (__mips__))
152 #include <sys/table.h>
155 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
156 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
157 that with a couple of other things and we'll have a unique match. */
158 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
159 #define tek4300 /* Define by emacs, but not by other users. */
163 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
164 #ifndef LOAD_AVE_TYPE
167 #define LOAD_AVE_TYPE long
171 #define LOAD_AVE_TYPE long
175 #define LOAD_AVE_TYPE long
179 #define LOAD_AVE_TYPE long
183 #define LOAD_AVE_TYPE long
187 #define LOAD_AVE_TYPE long
191 #define LOAD_AVE_TYPE long
195 #define LOAD_AVE_TYPE long
199 #define LOAD_AVE_TYPE long
202 #if defined (ardent) && defined (titan)
203 #define LOAD_AVE_TYPE long
207 #define LOAD_AVE_TYPE long
210 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
211 #define LOAD_AVE_TYPE long
215 #define LOAD_AVE_TYPE long
219 #define LOAD_AVE_TYPE double
221 #define LDAV_CVT(n) (n)
225 #endif /* No LOAD_AVE_TYPE. */
228 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
229 according to ghazi@noc.rutgers.edu. */
231 #define FSCALE 1024.0
234 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
235 /* <sys/param.h> defines an incorrect value for FSCALE on an
236 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
244 /* SunOS and some others define FSCALE in sys/param.h. */
247 #define FSCALE 2048.0
250 #if defined(MIPS) || defined(SVR4) || defined(decstation)
254 #if defined (sgi) || defined (sequent)
255 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
256 above under #ifdef MIPS. But we want the sgi value. */
258 #define FSCALE 1000.0
261 #if defined (ardent) && defined (titan)
262 #define FSCALE 65536.0
270 #define FSCALE 65536.0
273 #endif /* Not FSCALE. */
275 #if !defined (LDAV_CVT) && defined (FSCALE)
276 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
279 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
298 #if defined (_SEQUENT_) || defined (sequent)
318 #if defined (ardent) && defined (titan)
330 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
338 #endif /* defined (NLIST_STRUCT) */
341 #if defined(sgi) || (defined(mips) && !defined(BSD))
342 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
346 #if !defined (KERNEL_FILE) && defined (sequent)
347 #define KERNEL_FILE "/dynix"
350 #if !defined (KERNEL_FILE) && defined (hpux)
351 #define KERNEL_FILE "/hp-ux"
354 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
355 #define KERNEL_FILE "/unix"
359 #if !defined (LDAV_SYMBOL) && defined (alliant)
360 #define LDAV_SYMBOL "_Loadavg"
363 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
364 #define LDAV_SYMBOL "avenrun"
373 /* LOAD_AVE_TYPE should only get defined if we're going to use the
375 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
376 #define LOAD_AVE_TYPE double
384 #else /* NLIST_STRUCT */
386 #endif /* NLIST_STRUCT */
394 #define KERNEL_FILE "/vmunix"
395 #endif /* KERNEL_FILE */
398 #define LDAV_SYMBOL "_avenrun"
399 #endif /* LDAV_SYMBOL */
407 #include <vms/iodef.h>
412 #define LDAV_CVT(n) ((double) (n))
413 #endif /* !LDAV_CVT */
415 #endif /* LOAD_AVE_TYPE */
418 #ifdef HAVE_MACH_MACH_H
419 #include <mach/mach.h>
426 #include <sys/sysmp.h>
432 #include <sys/time.h>
433 #include <sys/wait.h>
434 #include <sys/syscall.h>
437 #include <machine/cpu.h>
438 #include <inq_stats/statistics.h>
439 #include <inq_stats/sysstats.h>
440 #include <inq_stats/cpustats.h>
441 #include <inq_stats/procstats.h>
442 #else /* Not UMAX_43. */
443 #include <sys/sysdefs.h>
444 #include <sys/statistics.h>
445 #include <sys/sysstats.h>
446 #include <sys/cpudefs.h>
447 #include <sys/cpustats.h>
448 #include <sys/procstats.h>
449 #endif /* Not UMAX_43. */
453 #include <sys/dg_sys_info.h>
456 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
459 #include <sys/file.h>
462 /* Avoid static vars inside a function since in HPUX they dump as pure. */
465 static processor_set_t default_set
;
466 static int getloadavg_initialized
;
470 static unsigned int cpus
= 0;
471 static unsigned int samples
;
475 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
479 /* File descriptor open to /dev/kmem or VMS load ave driver. */
481 /* Nonzero iff channel is valid. */
482 static int getloadavg_initialized
;
483 /* Offset in kmem to seek to read load average, or 0 means invalid. */
486 #if !defined(VMS) && !defined(sgi)
487 static struct nlist nl
[2];
488 #endif /* Not VMS or sgi */
494 #endif /* LOAD_AVE_TYPE */
496 /* Put the 1 minute, 5 minute and 15 minute load averages
497 into the first NELEM elements of LOADAVG.
498 Return the number written (never more than 3, but may be less than NELEM),
499 or -1 if an error occurred. */
502 getloadavg (loadavg
, nelem
)
506 int elem
= 0; /* Return value. */
508 #ifdef NO_GET_LOAD_AVG
510 /* Set errno to zero to indicate that there was no particular error;
511 this function just can't work at all on this system. */
516 #if !defined (LDAV_DONE) && defined (__linux__)
520 #ifndef LINUX_LDAV_FILE
521 #define LINUX_LDAV_FILE "/proc/loadavg"
528 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
531 count
= read (fd
, ldavgbuf
, 40);
536 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
537 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
541 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
542 loadavg
[elem
] = load_ave
[elem
];
546 #endif /* __linux__ */
548 #if !defined (LDAV_DONE) && defined (__NetBSD__)
552 #ifndef NETBSD_LDAV_FILE
553 #define NETBSD_LDAV_FILE "/kern/loadavg"
556 unsigned long int load_ave
[3], scale
;
560 fp
= fopen (NETBSD_LDAV_FILE
, "r");
563 count
= fscanf (fp
, "%lu %lu %lu %lu\n",
564 &load_ave
[0], &load_ave
[1], &load_ave
[2],
570 for (elem
= 0; elem
< nelem
; elem
++)
571 loadavg
[elem
] = (double) load_ave
[elem
] / (double) scale
;
575 #endif /* __NetBSD__ */
577 #if !defined (LDAV_DONE) && defined (NeXT)
579 /* The NeXT code was adapted from iscreen 3.2. */
582 struct processor_set_basic_info info
;
585 /* We only know how to get the 1-minute average for this system,
586 so even if the caller asks for more than 1, we only return 1. */
588 if (!getloadavg_initialized
)
590 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
591 getloadavg_initialized
= 1;
594 if (getloadavg_initialized
)
596 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
597 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
598 (processor_set_info_t
) &info
, &info_count
)
600 getloadavg_initialized
= 0;
604 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
608 if (!getloadavg_initialized
)
612 #if !defined (LDAV_DONE) && defined (UMAX)
614 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
615 have a /dev/kmem. Information about the workings of the running kernel
616 can be gathered with inq_stats system calls.
617 We only know how to get the 1-minute average for this system. */
619 struct proc_summary proc_sum_data
;
620 struct stat_descr proc_info
;
622 register unsigned int i
, j
;
626 register unsigned int c
, i
;
627 struct cpu_config conf
;
628 struct stat_descr desc
;
631 desc
.sd_subsys
= SUBSYS_CPU
;
632 desc
.sd_type
= CPUTYPE_CONFIG
;
633 desc
.sd_addr
= (char *) &conf
;
634 desc
.sd_size
= sizeof conf
;
636 if (inq_stats (1, &desc
))
640 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
642 struct class_stats stats
;
643 bzero ((char *) &stats
, sizeof stats
);
645 desc
.sd_type
= CPUTYPE_CLASS
;
647 desc
.sd_addr
= (char *) &stats
;
648 desc
.sd_size
= sizeof stats
;
650 if (inq_stats (1, &desc
))
653 c
+= stats
.class_numcpus
;
656 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
659 proc_info
.sd_next
= 0;
660 proc_info
.sd_subsys
= SUBSYS_PROC
;
661 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
662 proc_info
.sd_addr
= (char *) &proc_sum_data
;
663 proc_info
.sd_size
= sizeof (struct proc_summary
);
664 proc_info
.sd_sizeused
= 0;
666 if (inq_stats (1, &proc_info
) != 0)
669 load
= proc_sum_data
.ps_nrunnable
;
671 for (i
= samples
- 1; i
> 0; --i
)
673 load
+= proc_sum_data
.ps_nrun
[j
];
674 if (j
++ == PS_NRUNSIZE
)
679 loadavg
[elem
++] = load
/ samples
/ cpus
;
682 #if !defined (LDAV_DONE) && defined (DGUX)
684 /* This call can return -1 for an error, but with good args
685 it's not supposed to fail. The first argument is for no
686 apparent reason of type `long int *'. */
687 dg_sys_info ((long int *) &load_info
,
688 DG_SYS_INFO_LOAD_INFO_TYPE
,
689 DG_SYS_INFO_LOAD_VERSION_0
);
692 loadavg
[elem
++] = load_info
.one_minute
;
694 loadavg
[elem
++] = load_info
.five_minute
;
696 loadavg
[elem
++] = load_info
.fifteen_minute
;
699 #if !defined (LDAV_DONE) && defined (apollo)
701 /* Apollo code from lisch@mentorg.com (Ray Lischner).
703 This system call is not documented. The load average is obtained as
704 three long integers, for the load average over the past minute,
705 five minutes, and fifteen minutes. Each value is a scaled integer,
706 with 16 bits of integer part and 16 bits of fraction part.
708 I'm not sure which operating system first supported this system call,
709 but I know that SR10.2 supports it. */
711 extern void proc1_$
get_loadav ();
712 unsigned long load_ave
[3];
714 proc1_$
get_loadav (load_ave
);
717 loadavg
[elem
++] = load_ave
[0] / 65536.0;
719 loadavg
[elem
++] = load_ave
[1] / 65536.0;
721 loadavg
[elem
++] = load_ave
[2] / 65536.0;
724 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
727 struct tbl_loadavg load_ave
;
728 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
730 = (load_ave
.tl_lscale
== 0
731 ? load_ave
.tl_avenrun
.d
[0]
732 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
733 #endif /* OSF_MIPS */
735 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
738 struct tbl_loadavg load_ave
;
739 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
740 for (elem
= 0; elem
< nelem
; elem
++)
742 = (load_ave
.tl_lscale
== 0
743 ? load_ave
.tl_avenrun
.d
[elem
]
744 : (load_ave
.tl_avenrun
.l
[elem
] / (double) load_ave
.tl_lscale
));
745 #endif /* OSF_ALPHA */
747 #if !defined (LDAV_DONE) && defined (VMS)
748 /* VMS specific code -- read from the Load Ave driver. */
750 LOAD_AVE_TYPE load_ave
[3];
751 static int getloadavg_initialized
= 0;
760 /* Ensure that there is a channel open to the load ave device. */
761 if (!getloadavg_initialized
)
763 /* Attempt to open the channel. */
765 descriptor
.dsc$w_length
= 18;
766 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
768 $
DESCRIPTOR (descriptor
, "LAV0:");
770 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
771 getloadavg_initialized
= 1;
774 /* Read the load average vector. */
775 if (getloadavg_initialized
776 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
777 load_ave
, 12, 0, 0, 0, 0) & 1))
779 sys$
dassgn (channel
);
780 getloadavg_initialized
= 0;
783 if (!getloadavg_initialized
)
787 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
789 /* UNIX-specific code -- read the average from /dev/kmem. */
791 #define LDAV_PRIVILEGED /* This code requires special installation. */
793 LOAD_AVE_TYPE load_ave
[3];
795 /* Get the address of LDAV_SYMBOL. */
800 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
801 strcpy (nl
[1].n_name
, "");
802 #else /* NLIST_STRUCT */
803 #ifdef NLIST_NAME_UNION
804 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
805 nl
[1].n_un
.n_name
= 0;
806 #else /* not NLIST_NAME_UNION */
807 nl
[0].n_name
= LDAV_SYMBOL
;
809 #endif /* not NLIST_NAME_UNION */
810 #endif /* NLIST_STRUCT */
814 #if !(defined (_AIX) && !defined (ps2))
815 nlist (KERNEL_FILE
, nl
)
817 knlist (nl
, 1, sizeof (nl
[0]))
820 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
822 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
823 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
825 offset
= nl
[0].n_value
;
827 #endif /* !SUNOS_5 */
831 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
833 offset
= (long) ldav_off
& 0x7fffffff;
837 /* Make sure we have /dev/kmem open. */
838 if (!getloadavg_initialized
)
841 channel
= open ("/dev/kmem", 0);
844 /* Set the channel to close on exec, so it does not
845 litter any child's descriptor table. */
850 (void) fcntl (channel
, F_SETFD
, FD_CLOEXEC
);
852 getloadavg_initialized
= 1;
855 /* We pass 0 for the kernel, corefile, and swapfile names
856 to use the currently running kernel. */
857 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
860 /* nlist the currently running kernel. */
862 offset
= nl
[0].n_value
;
863 getloadavg_initialized
= 1;
868 /* If we can, get the load average values. */
869 if (offset
&& getloadavg_initialized
)
871 /* Try to read the load. */
873 if (lseek (channel
, offset
, 0) == -1L
874 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
875 != sizeof (load_ave
))
878 getloadavg_initialized
= 0;
881 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
882 != sizeof (load_ave
))
885 getloadavg_initialized
= 0;
890 if (offset
== 0 || !getloadavg_initialized
)
892 #endif /* LOAD_AVE_TYPE and not VMS */
894 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
896 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
898 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
900 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
903 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
908 /* Set errno to zero to indicate that there was no particular error;
909 this function just can't work at all on this system. */
915 #endif /* ! HAVE_GETLOADAVG */
926 naptime
= atoi (argv
[1]);
933 errno
= 0; /* Don't be misled if it doesn't set errno. */
934 loads
= getloadavg (avg
, 3);
937 perror ("Error getting load average");
941 printf ("1-minute: %f ", avg
[0]);
943 printf ("5-minute: %f ", avg
[1]);
945 printf ("15-minute: %f ", avg
[2]);