1 /* Get the system load averages.
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994
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>
76 /* Exclude all the code except the test program at the end
77 if the system has its own `getloadavg' function.
79 The declaration of `errno' is needed by the test program
80 as well as the function itself, so it comes first. */
88 #ifndef HAVE_GETLOADAVG
91 /* The existing Emacs configuration files define a macro called
92 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
93 returns the load average multiplied by 100. What we actually want
94 is a macro called LDAV_CVT, which returns the load average as an
97 For backwards compatibility, we'll define LDAV_CVT in terms of
98 LOAD_AVE_CVT, but future machine config files should just define
101 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
102 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
105 #if !defined (BSD) && defined (ultrix)
106 /* Ultrix behaves like BSD on Vaxen. */
111 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
112 conflicts with the definition understood in this file, that this
116 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
117 defined to mean that the nlist method should be used, which is not true. */
121 /* Set values that are different from the defaults, which are
122 set a little farther down with #ifndef. */
125 /* Some shorthands. */
127 #if defined (HPUX) && !defined (hpux)
131 #if defined(hp300) && !defined(hpux)
135 #if defined(ultrix) && defined(mips)
139 #if defined(sun) && defined(SVR4)
143 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
145 #include <sys/table.h>
148 #if defined (__osf__) && (defined (mips) || defined (__mips__))
150 #include <sys/table.h>
153 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
154 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
155 that with a couple of other things and we'll have a unique match. */
156 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
157 #define tek4300 /* Define by emacs, but not by other users. */
161 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
162 #ifndef LOAD_AVE_TYPE
165 #define LOAD_AVE_TYPE long
169 #define LOAD_AVE_TYPE long
173 #define LOAD_AVE_TYPE long
177 #define LOAD_AVE_TYPE long
181 #define LOAD_AVE_TYPE long
185 #define LOAD_AVE_TYPE long
189 #define LOAD_AVE_TYPE long
193 #define LOAD_AVE_TYPE long
197 #define LOAD_AVE_TYPE long
200 #if defined (ardent) && defined (titan)
201 #define LOAD_AVE_TYPE long
205 #define LOAD_AVE_TYPE long
208 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
209 #define LOAD_AVE_TYPE long
212 #endif /* No LOAD_AVE_TYPE. */
215 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
216 according to ghazi@noc.rutgers.edu. */
218 #define FSCALE 1024.0
224 /* SunOS and some others define FSCALE in sys/param.h. */
227 #define FSCALE 2048.0
230 #if defined(MIPS) || defined(SVR4) || defined(decstation)
234 #if defined (sgi) || defined (sequent)
235 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
236 above under #ifdef MIPS. But we want the sgi value. */
238 #define FSCALE 1000.0
241 #if defined (ardent) && defined (titan)
242 #define FSCALE 65536.0
249 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
250 /* <sys/param.h> defines an incorrect value for FSCALE on an
251 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
256 #endif /* Not FSCALE. */
258 #if !defined (LDAV_CVT) && defined (FSCALE)
259 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
262 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
281 #if defined (_SEQUENT_) || defined (sequent)
301 #if defined (ardent) && defined (titan)
313 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
317 #endif /* defined (NLIST_STRUCT) */
320 #if defined(sgi) || (defined(mips) && !defined(BSD))
321 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
325 #if !defined (KERNEL_FILE) && defined (sequent)
326 #define KERNEL_FILE "/dynix"
329 #if !defined (KERNEL_FILE) && defined (hpux)
330 #define KERNEL_FILE "/hp-ux"
333 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
334 #define KERNEL_FILE "/unix"
338 #if !defined (LDAV_SYMBOL) && defined (alliant)
339 #define LDAV_SYMBOL "_Loadavg"
342 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
343 #define LDAV_SYMBOL "avenrun"
352 /* LOAD_AVE_TYPE should only get defined if we're going to use the
354 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
355 #define LOAD_AVE_TYPE double
363 #else /* NLIST_STRUCT */
365 #endif /* NLIST_STRUCT */
373 #define KERNEL_FILE "/vmunix"
374 #endif /* KERNEL_FILE */
377 #define LDAV_SYMBOL "_avenrun"
378 #endif /* LDAV_SYMBOL */
386 #include <vms/iodef.h>
391 #define LDAV_CVT(n) ((double) (n))
392 #endif /* !LDAV_CVT */
394 #endif /* LOAD_AVE_TYPE */
397 #ifdef HAVE_MACH_MACH_H
398 #include <mach/mach.h>
405 #include <sys/sysmp.h>
411 #include <sys/time.h>
412 #include <sys/wait.h>
413 #include <sys/syscall.h>
416 #include <machine/cpu.h>
417 #include <inq_stats/statistics.h>
418 #include <inq_stats/sysstats.h>
419 #include <inq_stats/cpustats.h>
420 #include <inq_stats/procstats.h>
421 #else /* Not UMAX_43. */
422 #include <sys/sysdefs.h>
423 #include <sys/statistics.h>
424 #include <sys/sysstats.h>
425 #include <sys/cpudefs.h>
426 #include <sys/cpustats.h>
427 #include <sys/procstats.h>
428 #endif /* Not UMAX_43. */
432 #include <sys/dg_sys_info.h>
435 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
438 #include <sys/file.h>
441 /* Avoid static vars inside a function since in HPUX they dump as pure. */
444 static processor_set_t default_set
;
445 static int getloadavg_initialized
;
449 static unsigned int cpus
= 0;
450 static unsigned int samples
;
454 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
458 /* File descriptor open to /dev/kmem or VMS load ave driver. */
460 /* Nonzero iff channel is valid. */
461 static int getloadavg_initialized
;
462 /* Offset in kmem to seek to read load average, or 0 means invalid. */
465 #if !defined(VMS) && !defined(sgi)
466 static struct nlist nl
[2];
467 #endif /* Not VMS or sgi */
473 #endif /* LOAD_AVE_TYPE */
475 /* Put the 1 minute, 5 minute and 15 minute load averages
476 into the first NELEM elements of LOADAVG.
477 Return the number written (never more than 3, but may be less than NELEM),
478 or -1 if an error occurred. */
481 getloadavg (loadavg
, nelem
)
485 int elem
= 0; /* Return value. */
487 #ifdef NO_GET_LOAD_AVG
489 /* Set errno to zero to indicate that there was no particular error;
490 this function just can't work at all on this system. */
495 #if !defined (LDAV_DONE) && defined (__linux__)
499 #ifndef LINUX_LDAV_FILE
500 #define LINUX_LDAV_FILE "/proc/loadavg"
507 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
510 count
= read (fd
, ldavgbuf
, 40);
515 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
516 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
520 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
521 loadavg
[elem
] = load_ave
[elem
];
525 #endif /* __linux__ */
527 #if !defined (LDAV_DONE) && defined (__NetBSD__)
531 #ifndef NETBSD_LDAV_FILE
532 #define NETBSD_LDAV_FILE "/kern/loadavg"
535 unsigned long int load_ave
[3], scale
;
539 fp
= fopen (NETBSD_LDAV_FILE
, "r");
542 count
= fscanf (fp
, "%lu %lu %lu %lu\n",
543 &load_ave
[0], &load_ave
[1], &load_ave
[2],
549 for (elem
= 0; elem
< nelem
; elem
++)
550 loadavg
[elem
] = (double) load_ave
[elem
] / (double) scale
;
554 #endif /* __NetBSD__ */
556 #if !defined (LDAV_DONE) && defined (NeXT)
558 /* The NeXT code was adapted from iscreen 3.2. */
561 struct processor_set_basic_info info
;
564 /* We only know how to get the 1-minute average for this system,
565 so even if the caller asks for more than 1, we only return 1. */
567 if (!getloadavg_initialized
)
569 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
570 getloadavg_initialized
= 1;
573 if (getloadavg_initialized
)
575 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
576 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
577 (processor_set_info_t
) &info
, &info_count
)
579 getloadavg_initialized
= 0;
583 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
587 if (!getloadavg_initialized
)
591 #if !defined (LDAV_DONE) && defined (UMAX)
593 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
594 have a /dev/kmem. Information about the workings of the running kernel
595 can be gathered with inq_stats system calls.
596 We only know how to get the 1-minute average for this system. */
598 struct proc_summary proc_sum_data
;
599 struct stat_descr proc_info
;
601 register unsigned int i
, j
;
605 register unsigned int c
, i
;
606 struct cpu_config conf
;
607 struct stat_descr desc
;
610 desc
.sd_subsys
= SUBSYS_CPU
;
611 desc
.sd_type
= CPUTYPE_CONFIG
;
612 desc
.sd_addr
= (char *) &conf
;
613 desc
.sd_size
= sizeof conf
;
615 if (inq_stats (1, &desc
))
619 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
621 struct class_stats stats
;
622 bzero ((char *) &stats
, sizeof stats
);
624 desc
.sd_type
= CPUTYPE_CLASS
;
626 desc
.sd_addr
= (char *) &stats
;
627 desc
.sd_size
= sizeof stats
;
629 if (inq_stats (1, &desc
))
632 c
+= stats
.class_numcpus
;
635 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
638 proc_info
.sd_next
= 0;
639 proc_info
.sd_subsys
= SUBSYS_PROC
;
640 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
641 proc_info
.sd_addr
= (char *) &proc_sum_data
;
642 proc_info
.sd_size
= sizeof (struct proc_summary
);
643 proc_info
.sd_sizeused
= 0;
645 if (inq_stats (1, &proc_info
) != 0)
648 load
= proc_sum_data
.ps_nrunnable
;
650 for (i
= samples
- 1; i
> 0; --i
)
652 load
+= proc_sum_data
.ps_nrun
[j
];
653 if (j
++ == PS_NRUNSIZE
)
658 loadavg
[elem
++] = load
/ samples
/ cpus
;
661 #if !defined (LDAV_DONE) && defined (DGUX)
663 /* This call can return -1 for an error, but with good args
664 it's not supposed to fail. The first argument is for no
665 apparent reason of type `long int *'. */
666 dg_sys_info ((long int *) &load_info
,
667 DG_SYS_INFO_LOAD_INFO_TYPE
,
668 DG_SYS_INFO_LOAD_VERSION_0
);
671 loadavg
[elem
++] = load_info
.one_minute
;
673 loadavg
[elem
++] = load_info
.five_minute
;
675 loadavg
[elem
++] = load_info
.fifteen_minute
;
678 #if !defined (LDAV_DONE) && defined (apollo)
680 /* Apollo code from lisch@mentorg.com (Ray Lischner).
682 This system call is not documented. The load average is obtained as
683 three long integers, for the load average over the past minute,
684 five minutes, and fifteen minutes. Each value is a scaled integer,
685 with 16 bits of integer part and 16 bits of fraction part.
687 I'm not sure which operating system first supported this system call,
688 but I know that SR10.2 supports it. */
690 extern void proc1_$
get_loadav ();
691 unsigned long load_ave
[3];
693 proc1_$
get_loadav (load_ave
);
696 loadavg
[elem
++] = load_ave
[0] / 65536.0;
698 loadavg
[elem
++] = load_ave
[1] / 65536.0;
700 loadavg
[elem
++] = load_ave
[2] / 65536.0;
703 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
706 struct tbl_loadavg load_ave
;
707 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
709 = (load_ave
.tl_lscale
== 0
710 ? load_ave
.tl_avenrun
.d
[0]
711 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
712 #endif /* OSF_MIPS */
714 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
717 struct tbl_loadavg load_ave
;
718 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
719 for (elem
= 0; elem
< nelem
; elem
++)
721 = (load_ave
.tl_lscale
== 0
722 ? load_ave
.tl_avenrun
.d
[elem
]
723 : (load_ave
.tl_avenrun
.l
[elem
] / (double) load_ave
.tl_lscale
));
724 #endif /* OSF_ALPHA */
726 #if !defined (LDAV_DONE) && defined (VMS)
727 /* VMS specific code -- read from the Load Ave driver. */
729 LOAD_AVE_TYPE load_ave
[3];
730 static int getloadavg_initialized
= 0;
739 /* Ensure that there is a channel open to the load ave device. */
740 if (!getloadavg_initialized
)
742 /* Attempt to open the channel. */
744 descriptor
.dsc$w_length
= 18;
745 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
747 $
DESCRIPTOR (descriptor
, "LAV0:");
749 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
750 getloadavg_initialized
= 1;
753 /* Read the load average vector. */
754 if (getloadavg_initialized
755 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
756 load_ave
, 12, 0, 0, 0, 0) & 1))
758 sys$
dassgn (channel
);
759 getloadavg_initialized
= 0;
762 if (!getloadavg_initialized
)
766 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
768 /* UNIX-specific code -- read the average from /dev/kmem. */
770 #define LDAV_PRIVILEGED /* This code requires special installation. */
772 LOAD_AVE_TYPE load_ave
[3];
774 /* Get the address of LDAV_SYMBOL. */
779 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
780 strcpy (nl
[1].n_name
, "");
781 #else /* NLIST_STRUCT */
782 #ifdef NLIST_NAME_UNION
783 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
784 nl
[1].n_un
.n_name
= 0;
785 #else /* not NLIST_NAME_UNION */
786 nl
[0].n_name
= LDAV_SYMBOL
;
788 #endif /* not NLIST_NAME_UNION */
789 #endif /* NLIST_STRUCT */
792 if (nlist (KERNEL_FILE
, nl
) >= 0)
793 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
795 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
796 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
798 offset
= nl
[0].n_value
;
800 #endif /* !SUNOS_5 */
804 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
806 offset
= (long) ldav_off
& 0x7fffffff;
810 /* Make sure we have /dev/kmem open. */
811 if (!getloadavg_initialized
)
814 channel
= open ("/dev/kmem", 0);
816 getloadavg_initialized
= 1;
818 /* We pass 0 for the kernel, corefile, and swapfile names
819 to use the currently running kernel. */
820 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
823 /* nlist the currently running kernel. */
825 offset
= nl
[0].n_value
;
826 getloadavg_initialized
= 1;
831 /* If we can, get the load average values. */
832 if (offset
&& getloadavg_initialized
)
834 /* Try to read the load. */
836 if (lseek (channel
, offset
, 0) == -1L
837 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
838 != sizeof (load_ave
))
841 getloadavg_initialized
= 0;
844 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
845 != sizeof (load_ave
))
848 getloadavg_initialized
= 0;
853 if (offset
== 0 || !getloadavg_initialized
)
855 #endif /* LOAD_AVE_TYPE and not VMS */
857 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
859 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
861 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
863 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
866 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
871 /* Set errno to zero to indicate that there was no particular error;
872 this function just can't work at all on this system. */
878 #endif /* ! HAVE_GETLOADAVG */
889 naptime
= atoi (argv
[1]);
896 errno
= 0; /* Don't be misled if it doesn't set errno. */
897 loads
= getloadavg (avg
, 3);
900 perror ("Error getting load average");
904 printf ("1-minute: %f ", avg
[0]);
906 printf ("5-minute: %f ", avg
[1]);
908 printf ("15-minute: %f ", avg
[2]);