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>
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). */
83 /* Exclude all the code except the test program at the end
84 if the system has its own `getloadavg' function.
86 The declaration of `errno' is needed by the test program
87 as well as the function itself, so it comes first. */
95 #ifndef HAVE_GETLOADAVG
98 /* The existing Emacs configuration files define a macro called
99 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
100 returns the load average multiplied by 100. What we actually want
101 is a macro called LDAV_CVT, which returns the load average as an
104 For backwards compatibility, we'll define LDAV_CVT in terms of
105 LOAD_AVE_CVT, but future machine config files should just define
106 LDAV_CVT directly. */
108 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
109 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
112 #if !defined (BSD) && defined (ultrix)
113 /* Ultrix behaves like BSD on Vaxen. */
118 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
119 conflicts with the definition understood in this file, that this
123 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
124 defined to mean that the nlist method should be used, which is not true. */
128 /* Set values that are different from the defaults, which are
129 set a little farther down with #ifndef. */
132 /* Some shorthands. */
134 #if defined (HPUX) && !defined (hpux)
138 #if defined(hp300) && !defined(hpux)
142 #if defined(ultrix) && defined(mips)
146 #if defined(sun) && defined(SVR4)
150 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
152 #include <sys/table.h>
155 #if defined (__osf__) && (defined (mips) || defined (__mips__))
157 #include <sys/table.h>
160 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
161 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
162 that with a couple of other things and we'll have a unique match. */
163 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
164 #define tek4300 /* Define by emacs, but not by other users. */
168 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
169 #ifndef LOAD_AVE_TYPE
172 #define LOAD_AVE_TYPE long
176 #define LOAD_AVE_TYPE long
180 #define LOAD_AVE_TYPE long
184 #define LOAD_AVE_TYPE long
188 #define LOAD_AVE_TYPE long
192 #define LOAD_AVE_TYPE long
196 #define LOAD_AVE_TYPE long
200 #define LOAD_AVE_TYPE long
204 #define LOAD_AVE_TYPE long
207 #if defined (ardent) && defined (titan)
208 #define LOAD_AVE_TYPE long
212 #define LOAD_AVE_TYPE long
215 #endif /* No LOAD_AVE_TYPE. */
218 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
219 according to ghazi@noc.rutgers.edu. */
221 #define FSCALE 1024.0
227 /* SunOS and some others define FSCALE in sys/param.h. */
230 #define FSCALE 2048.0
233 #if defined(MIPS) || defined(SVR4) || defined(decstation)
237 #if defined (sgi) || defined (sequent)
238 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
239 above under #ifdef MIPS. But we want the sgi value. */
241 #define FSCALE 1000.0
244 #if defined (ardent) && defined (titan)
245 #define FSCALE 65536.0
252 #endif /* Not FSCALE. */
254 #if !defined (LDAV_CVT) && defined (FSCALE)
255 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
258 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
277 #if defined (_SEQUENT_) || defined (sequent)
297 #if defined (ardent) && defined (titan)
309 #endif /* defined (NLIST_STRUCT) */
312 #if defined(sgi) || (defined(mips) && !defined(BSD))
313 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
317 #if !defined (KERNEL_FILE) && defined (sequent)
318 #define KERNEL_FILE "/dynix"
321 #if !defined (KERNEL_FILE) && defined (hpux)
322 #define KERNEL_FILE "/hp-ux"
325 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
326 #define KERNEL_FILE "/unix"
330 #if !defined (LDAV_SYMBOL) && defined (alliant)
331 #define LDAV_SYMBOL "_Loadavg"
334 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
335 #define LDAV_SYMBOL "avenrun"
344 /* LOAD_AVE_TYPE should only get defined if we're going to use the
346 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
347 #define LOAD_AVE_TYPE double
355 #else /* NLIST_STRUCT */
357 #endif /* NLIST_STRUCT */
365 #define KERNEL_FILE "/vmunix"
366 #endif /* KERNEL_FILE */
369 #define LDAV_SYMBOL "_avenrun"
370 #endif /* LDAV_SYMBOL */
378 #include <vms/iodef.h>
383 #define LDAV_CVT(n) ((double) (n))
384 #endif /* !LDAV_CVT */
386 #endif /* LOAD_AVE_TYPE */
389 #ifdef HAVE_MACH_MACH_H
390 #include <mach/mach.h>
397 #include <sys/sysmp.h>
403 #include <sys/time.h>
404 #include <sys/wait.h>
405 #include <sys/syscall.h>
408 #include <machine/cpu.h>
409 #include <inq_stats/statistics.h>
410 #include <inq_stats/sysstats.h>
411 #include <inq_stats/cpustats.h>
412 #include <inq_stats/procstats.h>
413 #else /* Not UMAX_43. */
414 #include <sys/sysdefs.h>
415 #include <sys/statistics.h>
416 #include <sys/sysstats.h>
417 #include <sys/cpudefs.h>
418 #include <sys/cpustats.h>
419 #include <sys/procstats.h>
420 #endif /* Not UMAX_43. */
424 #include <sys/dg_sys_info.h>
427 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
430 #include <sys/file.h>
433 /* Avoid static vars inside a function since in HPUX they dump as pure. */
436 static processor_set_t default_set
;
437 static int getloadavg_initialized
;
441 static unsigned int cpus
= 0;
442 static unsigned int samples
;
446 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
450 /* File descriptor open to /dev/kmem or VMS load ave driver. */
452 /* Nonzero iff channel is valid. */
453 static int getloadavg_initialized
;
454 /* Offset in kmem to seek to read load average, or 0 means invalid. */
457 #if !defined(VMS) && !defined(sgi)
458 static struct nlist nl
[2];
459 #endif /* Not VMS or sgi */
465 #endif /* LOAD_AVE_TYPE */
467 /* Put the 1 minute, 5 minute and 15 minute load averages
468 into the first NELEM elements of LOADAVG.
469 Return the number written (never more than 3, but may be less than NELEM),
470 or -1 if an error occurred. */
473 getloadavg (loadavg
, nelem
)
477 int elem
= 0; /* Return value. */
479 #ifdef NO_GET_LOAD_AVG
481 /* Set errno to zero to indicate that there was no particular error;
482 this function just can't work at all on this system. */
487 #if !defined (LDAV_DONE) && defined (__linux__)
491 #ifndef LINUX_LDAV_FILE
492 #define LINUX_LDAV_FILE "/proc/loadavg"
499 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
502 count
= read (fd
, ldavgbuf
, 40);
507 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
508 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
512 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
513 loadavg
[elem
] = load_ave
[elem
];
517 #endif /* __linux__ */
519 #if !defined (LDAV_DONE) && defined (__NetBSD__)
523 #ifndef NETBSD_LDAV_FILE
524 #define NETBSD_LDAV_FILE "/kern/loadavg"
527 unsigned long int load_ave
[3], scale
;
531 fp
= fopen (NETBSD_LDAV_FILE
, "r");
534 count
= fscanf (fp
, "%lu %lu %lu %lu\n",
535 &load_ave
[0], &load_ave
[1], &load_ave
[2],
541 for (elem
= 0; elem
< nelem
; elem
++)
542 loadavg
[elem
] = (double) load_ave
[elem
] / (double) scale
;
546 #endif /* __NetBSD__ */
548 #if !defined (LDAV_DONE) && defined (NeXT)
550 /* The NeXT code was adapted from iscreen 3.2. */
553 struct processor_set_basic_info info
;
556 /* We only know how to get the 1-minute average for this system,
557 so even if the caller asks for more than 1, we only return 1. */
559 if (!getloadavg_initialized
)
561 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
562 getloadavg_initialized
= 1;
565 if (getloadavg_initialized
)
567 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
568 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
569 (processor_set_info_t
) &info
, &info_count
)
571 getloadavg_initialized
= 0;
575 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
579 if (!getloadavg_initialized
)
583 #if !defined (LDAV_DONE) && defined (UMAX)
585 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
586 have a /dev/kmem. Information about the workings of the running kernel
587 can be gathered with inq_stats system calls.
588 We only know how to get the 1-minute average for this system. */
590 struct proc_summary proc_sum_data
;
591 struct stat_descr proc_info
;
593 register unsigned int i
, j
;
597 register unsigned int c
, i
;
598 struct cpu_config conf
;
599 struct stat_descr desc
;
602 desc
.sd_subsys
= SUBSYS_CPU
;
603 desc
.sd_type
= CPUTYPE_CONFIG
;
604 desc
.sd_addr
= (char *) &conf
;
605 desc
.sd_size
= sizeof conf
;
607 if (inq_stats (1, &desc
))
611 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
613 struct class_stats stats
;
614 bzero ((char *) &stats
, sizeof stats
);
616 desc
.sd_type
= CPUTYPE_CLASS
;
618 desc
.sd_addr
= (char *) &stats
;
619 desc
.sd_size
= sizeof stats
;
621 if (inq_stats (1, &desc
))
624 c
+= stats
.class_numcpus
;
627 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
630 proc_info
.sd_next
= 0;
631 proc_info
.sd_subsys
= SUBSYS_PROC
;
632 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
633 proc_info
.sd_addr
= (char *) &proc_sum_data
;
634 proc_info
.sd_size
= sizeof (struct proc_summary
);
635 proc_info
.sd_sizeused
= 0;
637 if (inq_stats (1, &proc_info
) != 0)
640 load
= proc_sum_data
.ps_nrunnable
;
642 for (i
= samples
- 1; i
> 0; --i
)
644 load
+= proc_sum_data
.ps_nrun
[j
];
645 if (j
++ == PS_NRUNSIZE
)
650 loadavg
[elem
++] = load
/ samples
/ cpus
;
653 #if !defined (LDAV_DONE) && defined (DGUX)
655 /* This call can return -1 for an error, but with good args
656 it's not supposed to fail. The first argument is for no
657 apparent reason of type `long int *'. */
658 dg_sys_info ((long int *) &load_info
,
659 DG_SYS_INFO_LOAD_INFO_TYPE
,
660 DG_SYS_INFO_LOAD_VERSION_0
);
663 loadavg
[elem
++] = load_info
.one_minute
;
665 loadavg
[elem
++] = load_info
.five_minute
;
667 loadavg
[elem
++] = load_info
.fifteen_minute
;
670 #if !defined (LDAV_DONE) && defined (apollo)
672 /* Apollo code from lisch@mentorg.com (Ray Lischner).
674 This system call is not documented. The load average is obtained as
675 three long integers, for the load average over the past minute,
676 five minutes, and fifteen minutes. Each value is a scaled integer,
677 with 16 bits of integer part and 16 bits of fraction part.
679 I'm not sure which operating system first supported this system call,
680 but I know that SR10.2 supports it. */
682 extern void proc1_$
get_loadav ();
683 unsigned long load_ave
[3];
685 proc1_$
get_loadav (load_ave
);
688 loadavg
[elem
++] = load_ave
[0] / 65536.0;
690 loadavg
[elem
++] = load_ave
[1] / 65536.0;
692 loadavg
[elem
++] = load_ave
[2] / 65536.0;
695 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
698 struct tbl_loadavg load_ave
;
699 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
701 = (load_ave
.tl_lscale
== 0
702 ? load_ave
.tl_avenrun
.d
[0]
703 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
704 #endif /* OSF_MIPS */
706 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
709 struct tbl_loadavg load_ave
;
710 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
711 for (elem
= 0; elem
< nelem
; elem
++)
713 = (load_ave
.tl_lscale
== 0
714 ? load_ave
.tl_avenrun
.d
[elem
]
715 : (load_ave
.tl_avenrun
.l
[elem
] / (double) load_ave
.tl_lscale
));
716 #endif /* OSF_ALPHA */
718 #if !defined (LDAV_DONE) && defined (VMS)
719 /* VMS specific code -- read from the Load Ave driver. */
721 LOAD_AVE_TYPE load_ave
[3];
722 static int getloadavg_initialized
= 0;
731 /* Ensure that there is a channel open to the load ave device. */
732 if (!getloadavg_initialized
)
734 /* Attempt to open the channel. */
736 descriptor
.dsc$w_length
= 18;
737 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
739 $
DESCRIPTOR (descriptor
, "LAV0:");
741 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
742 getloadavg_initialized
= 1;
745 /* Read the load average vector. */
746 if (getloadavg_initialized
747 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
748 load_ave
, 12, 0, 0, 0, 0) & 1))
750 sys$
dassgn (channel
);
751 getloadavg_initialized
= 0;
754 if (!getloadavg_initialized
)
758 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
760 /* UNIX-specific code -- read the average from /dev/kmem. */
762 #define LDAV_PRIVILEGED /* This code requires special installation. */
764 LOAD_AVE_TYPE load_ave
[3];
766 /* Get the address of LDAV_SYMBOL. */
771 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
772 strcpy (nl
[1].n_name
, "");
773 #else /* NLIST_STRUCT */
774 #ifdef NLIST_NAME_UNION
775 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
776 nl
[1].n_un
.n_name
= 0;
777 #else /* not NLIST_NAME_UNION */
778 nl
[0].n_name
= LDAV_SYMBOL
;
780 #endif /* not NLIST_NAME_UNION */
781 #endif /* NLIST_STRUCT */
784 if (nlist (KERNEL_FILE
, nl
) >= 0)
785 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
787 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
788 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
790 offset
= nl
[0].n_value
;
792 #endif /* !SUNOS_5 */
796 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
798 offset
= (long) ldav_off
& 0x7fffffff;
802 /* Make sure we have /dev/kmem open. */
803 if (!getloadavg_initialized
)
806 channel
= open ("/dev/kmem", 0);
808 getloadavg_initialized
= 1;
810 /* We pass 0 for the kernel, corefile, and swapfile names
811 to use the currently running kernel. */
812 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
815 /* nlist the currently running kernel. */
817 offset
= nl
[0].n_value
;
818 getloadavg_initialized
= 1;
823 /* If we can, get the load average values. */
824 if (offset
&& getloadavg_initialized
)
826 /* Try to read the load. */
828 if (lseek (channel
, offset
, 0) == -1L
829 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
830 != sizeof (load_ave
))
833 getloadavg_initialized
= 0;
836 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
837 != sizeof (load_ave
))
840 getloadavg_initialized
= 0;
845 if (offset
== 0 || !getloadavg_initialized
)
847 #endif /* LOAD_AVE_TYPE and not VMS */
849 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
851 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
853 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
855 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
858 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
863 /* Set errno to zero to indicate that there was no particular error;
864 this function just can't work at all on this system. */
870 #endif /* ! HAVE_GETLOADAVG */
881 naptime
= atoi (argv
[1]);
888 errno
= 0; /* Don't be misled if it doesn't set errno. */
889 loads
= getloadavg (avg
, 3);
892 perror ("Error getting load average");
896 printf ("1-minute: %f ", avg
[0]);
898 printf ("5-minute: %f ", avg
[1]);
900 printf ("15-minute: %f ", avg
[2]);