1 /* Get the system load averages.
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93
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.
42 eunice UNIX emulator under VMS.
46 sequent Sequent Dynix 3.x.x (BSD)
47 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
48 sony_news NEWS-OS (works at least for 4.1C)
52 __linux__ Linux: assumes /proc filesystem mounted.
53 Support from Michael K. Johnson.
54 __NetBSD__ NetBSD: assumes /kern filesystem mounted.
56 In addition, to avoid nesting many #ifdefs, we internally set
57 LDAV_DONE to indicate that the load average has been computed.
59 We also #define LDAV_PRIVILEGED if a program will require
60 special installation to be able to call getloadavg. */
62 #include <sys/types.h>
64 /* Both the Emacs and non-Emacs sections want this. Some
65 configuration files' definitions for the LOAD_AVE_CVT macro (like
66 sparc.h's) use macros like FSCALE, defined here. */
68 #include <sys/param.h>
73 #if defined (emacs) || defined (CONFIG_BROKETS)
74 /* We use <config.h> instead of "config.h" so that a compilation
75 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
76 (which it would do because it found this file in $srcdir). */
84 /* Exclude all the code except the test program at the end
85 if the system has its own `getloadavg' function. */
87 #ifndef HAVE_GETLOADAVG
90 /* The existing Emacs configuration files define a macro called
91 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
92 returns the load average multiplied by 100. What we actually want
93 is a macro called LDAV_CVT, which returns the load average as an
96 For backwards compatibility, we'll define LDAV_CVT in terms of
97 LOAD_AVE_CVT, but future machine config files should just define
100 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
101 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
104 #if !defined (BSD) && defined (ultrix)
105 /* Ultrix behaves like BSD on Vaxen. */
110 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
111 conflicts with the definition understood in this file, that this
115 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
116 defined to mean that the nlist method should be used, which is not true. */
120 /* Set values that are different from the defaults, which are
121 set a little farther down with #ifndef. */
124 /* Some shorthands. */
126 #if defined (HPUX) && !defined (hpux)
130 #if defined(hp300) && !defined(hpux)
134 #if defined(ultrix) && defined(mips)
138 #if defined(sun) && defined(SVR4)
142 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
146 #if defined (__osf__) && (defined (mips) || defined (__mips__))
148 #include <sys/table.h>
151 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
152 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
153 that with a couple of other things and we'll have a unique match. */
154 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
155 #define tek4300 /* Define by emacs, but not by other users. */
159 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
160 #ifndef LOAD_AVE_TYPE
163 #define LOAD_AVE_TYPE long
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
198 #if defined (ardent) && defined (titan)
199 #define LOAD_AVE_TYPE long
203 #define LOAD_AVE_TYPE long
206 #endif /* No LOAD_AVE_TYPE. */
209 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
210 according to ghazi@noc.rutgers.edu. */
212 #define FSCALE 1024.0
218 /* SunOS and some others define FSCALE in sys/param.h. */
221 #define FSCALE 2048.0
224 #if defined(MIPS) || defined(SVR4) || defined(decstation)
228 #if defined (sgi) || defined (sequent)
229 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
230 above under #ifdef MIPS. But we want the sgi value. */
232 #define FSCALE 1000.0
235 #if defined (ardent) && defined (titan)
236 #define FSCALE 65536.0
243 #endif /* Not FSCALE. */
245 #if !defined (LDAV_CVT) && defined (FSCALE)
246 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
249 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
268 #if defined (_SEQUENT_) || defined (sequent)
288 #if defined (ardent) && defined (titan)
300 #endif /* defined (NLIST_STRUCT) */
303 #if defined(sgi) || (defined(mips) && !defined(BSD))
304 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
308 #if !defined (KERNEL_FILE) && defined (sequent)
309 #define KERNEL_FILE "/dynix"
312 #if !defined (KERNEL_FILE) && defined (hpux)
313 #define KERNEL_FILE "/hp-ux"
316 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
317 #define KERNEL_FILE "/unix"
321 #if !defined (LDAV_SYMBOL) && defined (alliant)
322 #define LDAV_SYMBOL "_Loadavg"
325 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
326 #define LDAV_SYMBOL "avenrun"
340 /* LOAD_AVE_TYPE should only get defined if we're going to use the
342 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
343 #define LOAD_AVE_TYPE double
351 #else /* NLIST_STRUCT */
353 #endif /* NLIST_STRUCT */
361 #define KERNEL_FILE "/vmunix"
362 #endif /* KERNEL_FILE */
365 #define LDAV_SYMBOL "_avenrun"
366 #endif /* LDAV_SYMBOL */
374 #include <vms/iodef.h>
379 #define LDAV_CVT(n) ((double) (n))
380 #endif /* !LDAV_CVT */
382 #endif /* LOAD_AVE_TYPE */
385 #ifdef HAVE_MACH_MACH_H
386 #include <mach/mach.h>
393 #include <sys/sysmp.h>
399 #include <sys/time.h>
400 #include <sys/wait.h>
401 #include <sys/syscall.h>
404 #include <machine/cpu.h>
405 #include <inq_stats/statistics.h>
406 #include <inq_stats/sysstats.h>
407 #include <inq_stats/cpustats.h>
408 #include <inq_stats/procstats.h>
409 #else /* Not UMAX_43. */
410 #include <sys/sysdefs.h>
411 #include <sys/statistics.h>
412 #include <sys/sysstats.h>
413 #include <sys/cpudefs.h>
414 #include <sys/cpustats.h>
415 #include <sys/procstats.h>
416 #endif /* Not UMAX_43. */
420 #include <sys/dg_sys_info.h>
423 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
426 #include <sys/file.h>
429 /* Avoid static vars inside a function since in HPUX they dump as pure. */
432 static processor_set_t default_set
;
433 static int getloadavg_initialized
;
437 static unsigned int cpus
= 0;
438 static unsigned int samples
;
442 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
446 /* File descriptor open to /dev/kmem or VMS load ave driver. */
448 /* Nonzero iff channel is valid. */
449 static int getloadavg_initialized
;
450 /* Offset in kmem to seek to read load average, or 0 means invalid. */
453 #if !defined(VMS) && !defined(sgi)
454 static struct nlist nl
[2];
455 #endif /* Not VMS or sgi */
461 #endif /* LOAD_AVE_TYPE */
463 /* Put the 1 minute, 5 minute and 15 minute load averages
464 into the first NELEM elements of LOADAVG.
465 Return the number written (never more than 3, but may be less than NELEM),
466 or -1 if an error occurred. */
469 getloadavg (loadavg
, nelem
)
473 int elem
= 0; /* Return value. */
475 #ifdef NO_GET_LOAD_AVG
477 /* Set errno to zero to indicate that there was no particular error;
478 this function just can't work at all on this system. */
483 #if !defined (LDAV_DONE) && defined (__linux__)
487 #ifndef LINUX_LDAV_FILE
488 #define LINUX_LDAV_FILE "/proc/loadavg"
495 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
498 count
= read (fd
, ldavgbuf
, 40);
503 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
504 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
508 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
509 loadavg
[elem
] = load_ave
[elem
];
513 #endif /* __linux__ */
515 #if !defined (LDAV_DONE) && defined (__NetBSD__)
519 #ifndef NETBSD_LDAV_FILE
520 #define NETBSD_LDAV_FILE "/kern/loadavg"
523 unsigned long int load_ave
[3], scale
;
527 fp
= fopen (NETBSD_LDAV_FILE
, "r");
530 count
= fscanf (fp
, "%lu %lu %lu %lu\n",
531 &load_ave
[0], &load_ave
[1], &load_ave
[2],
537 for (elem
= 0; elem
< nelem
; elem
++)
538 loadavg
[elem
] = (double) load_ave
[elem
] / (double) scale
;
542 #endif /* __NetBSD__ */
544 #if !defined (LDAV_DONE) && defined (NeXT)
546 /* The NeXT code was adapted from iscreen 3.2. */
549 struct processor_set_basic_info info
;
552 /* We only know how to get the 1-minute average for this system,
553 so even if the caller asks for more than 1, we only return 1. */
555 if (!getloadavg_initialized
)
557 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
558 getloadavg_initialized
= 1;
561 if (getloadavg_initialized
)
563 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
564 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
565 (processor_set_info_t
) &info
, &info_count
)
567 getloadavg_initialized
= 0;
571 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
575 if (!getloadavg_initialized
)
579 #if !defined (LDAV_DONE) && defined (UMAX)
581 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
582 have a /dev/kmem. Information about the workings of the running kernel
583 can be gathered with inq_stats system calls.
584 We only know how to get the 1-minute average for this system. */
586 struct proc_summary proc_sum_data
;
587 struct stat_descr proc_info
;
589 register unsigned int i
, j
;
593 register unsigned int c
, i
;
594 struct cpu_config conf
;
595 struct stat_descr desc
;
598 desc
.sd_subsys
= SUBSYS_CPU
;
599 desc
.sd_type
= CPUTYPE_CONFIG
;
600 desc
.sd_addr
= (char *) &conf
;
601 desc
.sd_size
= sizeof conf
;
603 if (inq_stats (1, &desc
))
607 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
609 struct class_stats stats
;
610 bzero ((char *) &stats
, sizeof stats
);
612 desc
.sd_type
= CPUTYPE_CLASS
;
614 desc
.sd_addr
= (char *) &stats
;
615 desc
.sd_size
= sizeof stats
;
617 if (inq_stats (1, &desc
))
620 c
+= stats
.class_numcpus
;
623 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
626 proc_info
.sd_next
= 0;
627 proc_info
.sd_subsys
= SUBSYS_PROC
;
628 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
629 proc_info
.sd_addr
= (char *) &proc_sum_data
;
630 proc_info
.sd_size
= sizeof (struct proc_summary
);
631 proc_info
.sd_sizeused
= 0;
633 if (inq_stats (1, &proc_info
) != 0)
636 load
= proc_sum_data
.ps_nrunnable
;
638 for (i
= samples
- 1; i
> 0; --i
)
640 load
+= proc_sum_data
.ps_nrun
[j
];
641 if (j
++ == PS_NRUNSIZE
)
646 loadavg
[elem
++] = load
/ samples
/ cpus
;
649 #if !defined (LDAV_DONE) && defined (DGUX)
651 /* This call can return -1 for an error, but with good args
652 it's not supposed to fail. The first argument is for no
653 apparent reason of type `long int *'. */
654 dg_sys_info ((long int *) &load_info
,
655 DG_SYS_INFO_LOAD_INFO_TYPE
,
656 DG_SYS_INFO_LOAD_VERSION_0
);
659 loadavg
[elem
++] = load_info
.one_minute
;
661 loadavg
[elem
++] = load_info
.five_minute
;
663 loadavg
[elem
++] = load_info
.fifteen_minute
;
666 #if !defined (LDAV_DONE) && defined (apollo)
668 /* Apollo code from lisch@mentorg.com (Ray Lischner).
670 This system call is not documented. The load average is obtained as
671 three long integers, for the load average over the past minute,
672 five minutes, and fifteen minutes. Each value is a scaled integer,
673 with 16 bits of integer part and 16 bits of fraction part.
675 I'm not sure which operating system first supported this system call,
676 but I know that SR10.2 supports it. */
678 extern void proc1_$
get_loadav ();
679 unsigned long load_ave
[3];
681 proc1_$
get_loadav (load_ave
);
684 loadavg
[elem
++] = load_ave
[0] / 65536.0;
686 loadavg
[elem
++] = load_ave
[1] / 65536.0;
688 loadavg
[elem
++] = load_ave
[2] / 65536.0;
691 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
694 struct tbl_loadavg load_ave
;
695 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
697 = (load_ave
.tl_lscale
== 0
698 ? load_ave
.tl_avenrun
.d
[0]
699 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
700 #endif /* OSF_MIPS */
702 #if !defined (LDAV_DONE) && defined (VMS)
703 /* VMS specific code -- read from the Load Ave driver. */
705 LOAD_AVE_TYPE load_ave
[3];
706 static int getloadavg_initialized
= 0;
715 /* Ensure that there is a channel open to the load ave device. */
716 if (!getloadavg_initialized
)
718 /* Attempt to open the channel. */
720 descriptor
.dsc$w_length
= 18;
721 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
723 $
DESCRIPTOR (descriptor
, "LAV0:");
725 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
726 getloadavg_initialized
= 1;
729 /* Read the load average vector. */
730 if (getloadavg_initialized
731 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
732 load_ave
, 12, 0, 0, 0, 0) & 1))
734 sys$
dassgn (channel
);
735 getloadavg_initialized
= 0;
738 if (!getloadavg_initialized
)
742 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
744 /* UNIX-specific code -- read the average from /dev/kmem. */
746 #define LDAV_PRIVILEGED /* This code requires special installation. */
748 LOAD_AVE_TYPE load_ave
[3];
750 /* Get the address of LDAV_SYMBOL. */
755 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
756 strcpy (nl
[1].n_name
, "");
757 #else /* NLIST_STRUCT */
758 #ifdef NLIST_NAME_UNION
759 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
760 nl
[1].n_un
.n_name
= 0;
761 #else /* not NLIST_NAME_UNION */
762 nl
[0].n_name
= LDAV_SYMBOL
;
764 #endif /* not NLIST_NAME_UNION */
765 #endif /* NLIST_STRUCT */
768 if (nlist (KERNEL_FILE
, nl
) >= 0)
769 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
771 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
772 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
774 offset
= nl
[0].n_value
;
776 #endif /* !SUNOS_5 */
780 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
782 offset
= (long) ldav_off
& 0x7fffffff;
786 /* Make sure we have /dev/kmem open. */
787 if (!getloadavg_initialized
)
790 channel
= open ("/dev/kmem", 0);
792 getloadavg_initialized
= 1;
794 /* We pass 0 for the kernel, corefile, and swapfile names
795 to use the currently running kernel. */
796 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
799 /* nlist the currently running kernel. */
801 offset
= nl
[0].n_value
;
802 getloadavg_initialized
= 1;
807 /* If we can, get the load average values. */
808 if (offset
&& getloadavg_initialized
)
810 /* Try to read the load. */
812 if (lseek (channel
, offset
, 0) == -1L
813 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
814 != sizeof (load_ave
))
817 getloadavg_initialized
= 0;
820 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
821 != sizeof (load_ave
))
824 getloadavg_initialized
= 0;
829 if (offset
== 0 || !getloadavg_initialized
)
831 #endif /* LOAD_AVE_TYPE and not VMS */
833 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
835 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
837 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
839 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
842 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
847 /* Set errno to zero to indicate that there was no particular error;
848 this function just can't work at all on this system. */
854 #endif /* ! HAVE_GETLOADAVG */
865 naptime
= atoi (argv
[1]);
872 errno
= 0; /* Don't be misled if it doesn't set errno. */
873 loads
= getloadavg (avg
, 3);
876 perror ("Error getting load average");
880 printf ("1-minute: %f ", avg
[0]);
882 printf ("5-minute: %f ", avg
[1]);
884 printf ("15-minute: %f ", avg
[2]);