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.
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 /* This should always be first. */
67 #include <sys/types.h>
69 /* Both the Emacs and non-Emacs sections want this. Some
70 configuration files' definitions for the LOAD_AVE_CVT macro (like
71 sparc.h's) use macros like FSCALE, defined here. */
73 #include <sys/param.h>
77 /* Exclude all the code except the test program at the end
78 if the system has its own `getloadavg' function.
80 The declaration of `errno' is needed by the test program
81 as well as the function itself, so it comes first. */
89 #ifndef HAVE_GETLOADAVG
92 /* The existing Emacs configuration files define a macro called
93 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
94 returns the load average multiplied by 100. What we actually want
95 is a macro called LDAV_CVT, which returns the load average as an
98 For backwards compatibility, we'll define LDAV_CVT in terms of
99 LOAD_AVE_CVT, but future machine config files should just define
100 LDAV_CVT directly. */
102 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
103 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
106 #if !defined (BSD) && defined (ultrix)
107 /* Ultrix behaves like BSD on Vaxen. */
112 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
113 conflicts with the definition understood in this file, that this
117 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
118 defined to mean that the nlist method should be used, which is not true. */
122 /* Set values that are different from the defaults, which are
123 set a little farther down with #ifndef. */
126 /* Some shorthands. */
128 #if defined (HPUX) && !defined (hpux)
132 #if defined(hp300) && !defined(hpux)
136 #if defined(ultrix) && defined(mips)
140 #if defined(sun) && defined(SVR4)
144 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
146 #include <sys/table.h>
149 #if defined (__osf__) && (defined (mips) || defined (__mips__))
151 #include <sys/table.h>
154 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
155 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
156 that with a couple of other things and we'll have a unique match. */
157 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
158 #define tek4300 /* Define by emacs, but not by other users. */
162 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
163 #ifndef LOAD_AVE_TYPE
166 #define LOAD_AVE_TYPE long
170 #define LOAD_AVE_TYPE long
174 #define LOAD_AVE_TYPE long
178 #define LOAD_AVE_TYPE long
182 #define LOAD_AVE_TYPE long
186 #define LOAD_AVE_TYPE long
190 #define LOAD_AVE_TYPE long
194 #define LOAD_AVE_TYPE long
198 #define LOAD_AVE_TYPE long
201 #if defined (ardent) && defined (titan)
202 #define LOAD_AVE_TYPE long
206 #define LOAD_AVE_TYPE long
209 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
210 #define LOAD_AVE_TYPE long
213 #endif /* No LOAD_AVE_TYPE. */
216 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
217 according to ghazi@noc.rutgers.edu. */
219 #define FSCALE 1024.0
222 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
223 /* <sys/param.h> defines an incorrect value for FSCALE on an
224 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
232 /* SunOS and some others define FSCALE in sys/param.h. */
235 #define FSCALE 2048.0
238 #if defined(MIPS) || defined(SVR4) || defined(decstation)
242 #if defined (sgi) || defined (sequent)
243 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
244 above under #ifdef MIPS. But we want the sgi value. */
246 #define FSCALE 1000.0
249 #if defined (ardent) && defined (titan)
250 #define FSCALE 65536.0
257 #endif /* Not FSCALE. */
259 #if !defined (LDAV_CVT) && defined (FSCALE)
260 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
263 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
282 #if defined (_SEQUENT_) || defined (sequent)
302 #if defined (ardent) && defined (titan)
314 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
318 #endif /* defined (NLIST_STRUCT) */
321 #if defined(sgi) || (defined(mips) && !defined(BSD))
322 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
326 #if !defined (KERNEL_FILE) && defined (sequent)
327 #define KERNEL_FILE "/dynix"
330 #if !defined (KERNEL_FILE) && defined (hpux)
331 #define KERNEL_FILE "/hp-ux"
334 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
335 #define KERNEL_FILE "/unix"
339 #if !defined (LDAV_SYMBOL) && defined (alliant)
340 #define LDAV_SYMBOL "_Loadavg"
343 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
344 #define LDAV_SYMBOL "avenrun"
353 /* LOAD_AVE_TYPE should only get defined if we're going to use the
355 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
356 #define LOAD_AVE_TYPE double
364 #else /* NLIST_STRUCT */
366 #endif /* NLIST_STRUCT */
374 #define KERNEL_FILE "/vmunix"
375 #endif /* KERNEL_FILE */
378 #define LDAV_SYMBOL "_avenrun"
379 #endif /* LDAV_SYMBOL */
387 #include <vms/iodef.h>
392 #define LDAV_CVT(n) ((double) (n))
393 #endif /* !LDAV_CVT */
395 #endif /* LOAD_AVE_TYPE */
398 #ifdef HAVE_MACH_MACH_H
399 #include <mach/mach.h>
406 #include <sys/sysmp.h>
412 #include <sys/time.h>
413 #include <sys/wait.h>
414 #include <sys/syscall.h>
417 #include <machine/cpu.h>
418 #include <inq_stats/statistics.h>
419 #include <inq_stats/sysstats.h>
420 #include <inq_stats/cpustats.h>
421 #include <inq_stats/procstats.h>
422 #else /* Not UMAX_43. */
423 #include <sys/sysdefs.h>
424 #include <sys/statistics.h>
425 #include <sys/sysstats.h>
426 #include <sys/cpudefs.h>
427 #include <sys/cpustats.h>
428 #include <sys/procstats.h>
429 #endif /* Not UMAX_43. */
433 #include <sys/dg_sys_info.h>
436 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
439 #include <sys/file.h>
442 /* Avoid static vars inside a function since in HPUX they dump as pure. */
445 static processor_set_t default_set
;
446 static int getloadavg_initialized
;
450 static unsigned int cpus
= 0;
451 static unsigned int samples
;
455 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
459 /* File descriptor open to /dev/kmem or VMS load ave driver. */
461 /* Nonzero iff channel is valid. */
462 static int getloadavg_initialized
;
463 /* Offset in kmem to seek to read load average, or 0 means invalid. */
466 #if !defined(VMS) && !defined(sgi)
467 static struct nlist nl
[2];
468 #endif /* Not VMS or sgi */
474 #endif /* LOAD_AVE_TYPE */
476 /* Put the 1 minute, 5 minute and 15 minute load averages
477 into the first NELEM elements of LOADAVG.
478 Return the number written (never more than 3, but may be less than NELEM),
479 or -1 if an error occurred. */
482 getloadavg (loadavg
, nelem
)
486 int elem
= 0; /* Return value. */
488 #ifdef NO_GET_LOAD_AVG
490 /* Set errno to zero to indicate that there was no particular error;
491 this function just can't work at all on this system. */
496 #if !defined (LDAV_DONE) && defined (__linux__)
500 #ifndef LINUX_LDAV_FILE
501 #define LINUX_LDAV_FILE "/proc/loadavg"
508 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
511 count
= read (fd
, ldavgbuf
, 40);
516 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
517 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
521 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
522 loadavg
[elem
] = load_ave
[elem
];
526 #endif /* __linux__ */
528 #if !defined (LDAV_DONE) && defined (__NetBSD__)
532 #ifndef NETBSD_LDAV_FILE
533 #define NETBSD_LDAV_FILE "/kern/loadavg"
536 unsigned long int load_ave
[3], scale
;
540 fp
= fopen (NETBSD_LDAV_FILE
, "r");
543 count
= fscanf (fp
, "%lu %lu %lu %lu\n",
544 &load_ave
[0], &load_ave
[1], &load_ave
[2],
550 for (elem
= 0; elem
< nelem
; elem
++)
551 loadavg
[elem
] = (double) load_ave
[elem
] / (double) scale
;
555 #endif /* __NetBSD__ */
557 #if !defined (LDAV_DONE) && defined (NeXT)
559 /* The NeXT code was adapted from iscreen 3.2. */
562 struct processor_set_basic_info info
;
565 /* We only know how to get the 1-minute average for this system,
566 so even if the caller asks for more than 1, we only return 1. */
568 if (!getloadavg_initialized
)
570 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
571 getloadavg_initialized
= 1;
574 if (getloadavg_initialized
)
576 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
577 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
578 (processor_set_info_t
) &info
, &info_count
)
580 getloadavg_initialized
= 0;
584 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
588 if (!getloadavg_initialized
)
592 #if !defined (LDAV_DONE) && defined (UMAX)
594 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
595 have a /dev/kmem. Information about the workings of the running kernel
596 can be gathered with inq_stats system calls.
597 We only know how to get the 1-minute average for this system. */
599 struct proc_summary proc_sum_data
;
600 struct stat_descr proc_info
;
602 register unsigned int i
, j
;
606 register unsigned int c
, i
;
607 struct cpu_config conf
;
608 struct stat_descr desc
;
611 desc
.sd_subsys
= SUBSYS_CPU
;
612 desc
.sd_type
= CPUTYPE_CONFIG
;
613 desc
.sd_addr
= (char *) &conf
;
614 desc
.sd_size
= sizeof conf
;
616 if (inq_stats (1, &desc
))
620 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
622 struct class_stats stats
;
623 bzero ((char *) &stats
, sizeof stats
);
625 desc
.sd_type
= CPUTYPE_CLASS
;
627 desc
.sd_addr
= (char *) &stats
;
628 desc
.sd_size
= sizeof stats
;
630 if (inq_stats (1, &desc
))
633 c
+= stats
.class_numcpus
;
636 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
639 proc_info
.sd_next
= 0;
640 proc_info
.sd_subsys
= SUBSYS_PROC
;
641 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
642 proc_info
.sd_addr
= (char *) &proc_sum_data
;
643 proc_info
.sd_size
= sizeof (struct proc_summary
);
644 proc_info
.sd_sizeused
= 0;
646 if (inq_stats (1, &proc_info
) != 0)
649 load
= proc_sum_data
.ps_nrunnable
;
651 for (i
= samples
- 1; i
> 0; --i
)
653 load
+= proc_sum_data
.ps_nrun
[j
];
654 if (j
++ == PS_NRUNSIZE
)
659 loadavg
[elem
++] = load
/ samples
/ cpus
;
662 #if !defined (LDAV_DONE) && defined (DGUX)
664 /* This call can return -1 for an error, but with good args
665 it's not supposed to fail. The first argument is for no
666 apparent reason of type `long int *'. */
667 dg_sys_info ((long int *) &load_info
,
668 DG_SYS_INFO_LOAD_INFO_TYPE
,
669 DG_SYS_INFO_LOAD_VERSION_0
);
672 loadavg
[elem
++] = load_info
.one_minute
;
674 loadavg
[elem
++] = load_info
.five_minute
;
676 loadavg
[elem
++] = load_info
.fifteen_minute
;
679 #if !defined (LDAV_DONE) && defined (apollo)
681 /* Apollo code from lisch@mentorg.com (Ray Lischner).
683 This system call is not documented. The load average is obtained as
684 three long integers, for the load average over the past minute,
685 five minutes, and fifteen minutes. Each value is a scaled integer,
686 with 16 bits of integer part and 16 bits of fraction part.
688 I'm not sure which operating system first supported this system call,
689 but I know that SR10.2 supports it. */
691 extern void proc1_$
get_loadav ();
692 unsigned long load_ave
[3];
694 proc1_$
get_loadav (load_ave
);
697 loadavg
[elem
++] = load_ave
[0] / 65536.0;
699 loadavg
[elem
++] = load_ave
[1] / 65536.0;
701 loadavg
[elem
++] = load_ave
[2] / 65536.0;
704 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
707 struct tbl_loadavg load_ave
;
708 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
710 = (load_ave
.tl_lscale
== 0
711 ? load_ave
.tl_avenrun
.d
[0]
712 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
713 #endif /* OSF_MIPS */
715 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
718 struct tbl_loadavg load_ave
;
719 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
720 for (elem
= 0; elem
< nelem
; elem
++)
722 = (load_ave
.tl_lscale
== 0
723 ? load_ave
.tl_avenrun
.d
[elem
]
724 : (load_ave
.tl_avenrun
.l
[elem
] / (double) load_ave
.tl_lscale
));
725 #endif /* OSF_ALPHA */
727 #if !defined (LDAV_DONE) && defined (VMS)
728 /* VMS specific code -- read from the Load Ave driver. */
730 LOAD_AVE_TYPE load_ave
[3];
731 static int getloadavg_initialized
= 0;
740 /* Ensure that there is a channel open to the load ave device. */
741 if (!getloadavg_initialized
)
743 /* Attempt to open the channel. */
745 descriptor
.dsc$w_length
= 18;
746 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
748 $
DESCRIPTOR (descriptor
, "LAV0:");
750 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
751 getloadavg_initialized
= 1;
754 /* Read the load average vector. */
755 if (getloadavg_initialized
756 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
757 load_ave
, 12, 0, 0, 0, 0) & 1))
759 sys$
dassgn (channel
);
760 getloadavg_initialized
= 0;
763 if (!getloadavg_initialized
)
767 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
769 /* UNIX-specific code -- read the average from /dev/kmem. */
771 #define LDAV_PRIVILEGED /* This code requires special installation. */
773 LOAD_AVE_TYPE load_ave
[3];
775 /* Get the address of LDAV_SYMBOL. */
780 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
781 strcpy (nl
[1].n_name
, "");
782 #else /* NLIST_STRUCT */
783 #ifdef NLIST_NAME_UNION
784 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
785 nl
[1].n_un
.n_name
= 0;
786 #else /* not NLIST_NAME_UNION */
787 nl
[0].n_name
= LDAV_SYMBOL
;
789 #endif /* not NLIST_NAME_UNION */
790 #endif /* NLIST_STRUCT */
793 if (nlist (KERNEL_FILE
, nl
) >= 0)
794 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
796 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
797 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
799 offset
= nl
[0].n_value
;
801 #endif /* !SUNOS_5 */
805 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
807 offset
= (long) ldav_off
& 0x7fffffff;
811 /* Make sure we have /dev/kmem open. */
812 if (!getloadavg_initialized
)
815 channel
= open ("/dev/kmem", 0);
817 getloadavg_initialized
= 1;
819 /* We pass 0 for the kernel, corefile, and swapfile names
820 to use the currently running kernel. */
821 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
824 /* nlist the currently running kernel. */
826 offset
= nl
[0].n_value
;
827 getloadavg_initialized
= 1;
832 /* If we can, get the load average values. */
833 if (offset
&& getloadavg_initialized
)
835 /* Try to read the load. */
837 if (lseek (channel
, offset
, 0) == -1L
838 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
839 != sizeof (load_ave
))
842 getloadavg_initialized
= 0;
845 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
846 != sizeof (load_ave
))
849 getloadavg_initialized
= 0;
854 if (offset
== 0 || !getloadavg_initialized
)
856 #endif /* LOAD_AVE_TYPE and not VMS */
858 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
860 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
862 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
864 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
867 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
872 /* Set errno to zero to indicate that there was no particular error;
873 this function just can't work at all on this system. */
879 #endif /* ! HAVE_GETLOADAVG */
890 naptime
= atoi (argv
[1]);
897 errno
= 0; /* Don't be misled if it doesn't set errno. */
898 loads
= getloadavg (avg
, 3);
901 perror ("Error getting load average");
905 printf ("1-minute: %f ", avg
[0]);
907 printf ("5-minute: %f ", avg
[1]);
909 printf ("15-minute: %f ", avg
[2]);