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.
55 In addition, to avoid nesting many #ifdefs, we internally set
56 LDAV_DONE to indicate that the load average has been computed.
58 We also #define LDAV_PRIVILEGED if a program will require
59 special installation to be able to call getloadavg. */
61 #include <sys/types.h>
63 /* Both the Emacs and non-Emacs sections want this. Some
64 configuration files' definitions for the LOAD_AVE_CVT macro (like
65 sparc.h's) use macros like FSCALE, defined here. */
67 #include <sys/param.h>
72 /* We use <config.h> instead of "config.h" so that a compilation
73 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
74 (which it would do because getloadavg.c was found in $srcdir). */
79 /* Exclude all the code except the test program at the end
80 if the system has its own `getloadavg' function. */
82 #ifndef HAVE_GETLOADAVG
85 /* The existing Emacs configuration files define a macro called
86 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
87 returns the load average multiplied by 100. What we actually want
88 is a macro called LDAV_CVT, which returns the load average as an
91 For backwards compatibility, we'll define LDAV_CVT in terms of
92 LOAD_AVE_CVT, but future machine config files should just define
95 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
96 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
99 #if !defined (BSD) && defined (ultrix)
100 /* Ultrix behaves like BSD on Vaxen. */
105 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
106 conflicts with the definition understood in this file, that this
110 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
111 defined to mean that the nlist method should be used, which is not true. */
115 /* Set values that are different from the defaults, which are
116 set a little farther down with #ifndef. */
119 /* Some shorthands. */
121 #if defined (HPUX) && !defined (hpux)
125 #if defined(hp300) && !defined(hpux)
129 #if defined(ultrix) && defined(mips)
133 #if defined(sun) && defined(SVR4)
137 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
141 #if defined (__osf__) && (defined (mips) || defined (__mips__))
143 #include <sys/table.h>
146 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
147 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
148 that with a couple of other things and we'll have a unique match. */
149 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
150 #define tek4300 /* Define by emacs, but not by other users. */
154 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
155 #ifndef LOAD_AVE_TYPE
158 #define LOAD_AVE_TYPE long
162 #define LOAD_AVE_TYPE long
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
193 #if defined (ardent) && defined (titan)
194 #define LOAD_AVE_TYPE long
198 #define LOAD_AVE_TYPE long
201 #endif /* No LOAD_AVE_TYPE. */
204 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
205 according to ghazi@noc.rutgers.edu. */
207 #define FSCALE 1024.0
213 /* SunOS and some others define FSCALE in sys/param.h. */
216 #define FSCALE 2048.0
219 #if defined(MIPS) || defined(SVR4) || defined(decstation)
223 #if defined (sgi) || defined (sequent)
224 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
225 above under #ifdef MIPS. But we want the sgi value. */
227 #define FSCALE 1000.0
230 #if defined (ardent) && defined (titan)
231 #define FSCALE 65536.0
238 #endif /* Not FSCALE. */
240 #if !defined (LDAV_CVT) && defined (FSCALE)
241 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
244 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
263 #if defined (_SEQUENT_) || defined (sequent)
283 #if defined (ardent) && defined (titan)
295 #endif /* defined (NLIST_STRUCT) */
298 #if defined(sgi) || (defined(mips) && !defined(BSD))
299 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
303 #if !defined (KERNEL_FILE) && defined (sequent)
304 #define KERNEL_FILE "/dynix"
307 #if !defined (KERNEL_FILE) && defined (hpux)
308 #define KERNEL_FILE "/hp-ux"
311 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
312 #define KERNEL_FILE "/unix"
316 #if !defined (LDAV_SYMBOL) && defined (alliant)
317 #define LDAV_SYMBOL "_Loadavg"
320 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
321 #define LDAV_SYMBOL "avenrun"
335 /* LOAD_AVE_TYPE should only get defined if we're going to use the
337 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
338 #define LOAD_AVE_TYPE double
346 #else /* NLIST_STRUCT */
348 #endif /* NLIST_STRUCT */
356 #define KERNEL_FILE "/vmunix"
357 #endif /* KERNEL_FILE */
360 #define LDAV_SYMBOL "_avenrun"
361 #endif /* LDAV_SYMBOL */
369 #include <vms/iodef.h>
374 #define LDAV_CVT(n) ((double) (n))
375 #endif /* !LDAV_CVT */
377 #endif /* LOAD_AVE_TYPE */
380 #ifdef HAVE_MACH_MACH_H
381 #include <mach/mach.h>
388 #include <sys/sysmp.h>
394 #include <sys/time.h>
395 #include <sys/wait.h>
396 #include <sys/syscall.h>
399 #include <machine/cpu.h>
400 #include <inq_stats/statistics.h>
401 #include <inq_stats/sysstats.h>
402 #include <inq_stats/cpustats.h>
403 #include <inq_stats/procstats.h>
404 #else /* Not UMAX_43. */
405 #include <sys/sysdefs.h>
406 #include <sys/statistics.h>
407 #include <sys/sysstats.h>
408 #include <sys/cpudefs.h>
409 #include <sys/cpustats.h>
410 #include <sys/procstats.h>
411 #endif /* Not UMAX_43. */
415 #include <sys/dg_sys_info.h>
418 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
421 #include <sys/file.h>
424 /* Avoid static vars inside a function since in HPUX they dump as pure. */
427 static processor_set_t default_set
;
428 static int getloadavg_initialized
;
432 static unsigned int cpus
= 0;
433 static unsigned int samples
;
437 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
441 /* File descriptor open to /dev/kmem or VMS load ave driver. */
443 /* Nonzero iff channel is valid. */
444 static int getloadavg_initialized
;
445 /* Offset in kmem to seek to read load average, or 0 means invalid. */
448 #if !defined(VMS) && !defined(sgi)
449 static struct nlist nl
[2];
450 #endif /* Not VMS or sgi */
456 #endif /* LOAD_AVE_TYPE */
458 /* Put the 1 minute, 5 minute and 15 minute load averages
459 into the first NELEM elements of LOADAVG.
460 Return the number written (never more than 3, but may be less than NELEM),
461 or -1 if an error occurred. */
464 getloadavg (loadavg
, nelem
)
468 int elem
= 0; /* Return value. */
470 #ifdef NO_GET_LOAD_AVG
472 /* Set errno to zero to indicate that there was no particular error;
473 this function just can't work at all on this system. */
478 #if !defined (LDAV_DONE) && defined (__linux__)
482 #ifndef LINUX_LDAV_FILE
483 #define LINUX_LDAV_FILE "/proc/loadavg"
490 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
493 count
= read (fd
, ldavgbuf
, 40);
498 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
499 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
503 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
504 loadavg
[elem
] = load_ave
[elem
];
508 #endif /* __linux__ */
510 #if !defined (LDAV_DONE) && defined (NeXT)
512 /* The NeXT code was adapted from iscreen 3.2. */
515 struct processor_set_basic_info info
;
518 /* We only know how to get the 1-minute average for this system,
519 so even if the caller asks for more than 1, we only return 1. */
521 if (!getloadavg_initialized
)
523 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
524 getloadavg_initialized
= 1;
527 if (getloadavg_initialized
)
529 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
530 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
531 (processor_set_info_t
) &info
, &info_count
)
533 getloadavg_initialized
= 0;
537 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
541 if (!getloadavg_initialized
)
545 #if !defined (LDAV_DONE) && defined (UMAX)
547 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
548 have a /dev/kmem. Information about the workings of the running kernel
549 can be gathered with inq_stats system calls.
550 We only know how to get the 1-minute average for this system. */
552 struct proc_summary proc_sum_data
;
553 struct stat_descr proc_info
;
555 register unsigned int i
, j
;
559 register unsigned int c
, i
;
560 struct cpu_config conf
;
561 struct stat_descr desc
;
564 desc
.sd_subsys
= SUBSYS_CPU
;
565 desc
.sd_type
= CPUTYPE_CONFIG
;
566 desc
.sd_addr
= (char *) &conf
;
567 desc
.sd_size
= sizeof conf
;
569 if (inq_stats (1, &desc
))
573 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
575 struct class_stats stats
;
576 bzero ((char *) &stats
, sizeof stats
);
578 desc
.sd_type
= CPUTYPE_CLASS
;
580 desc
.sd_addr
= (char *) &stats
;
581 desc
.sd_size
= sizeof stats
;
583 if (inq_stats (1, &desc
))
586 c
+= stats
.class_numcpus
;
589 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
592 proc_info
.sd_next
= 0;
593 proc_info
.sd_subsys
= SUBSYS_PROC
;
594 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
595 proc_info
.sd_addr
= (char *) &proc_sum_data
;
596 proc_info
.sd_size
= sizeof (struct proc_summary
);
597 proc_info
.sd_sizeused
= 0;
599 if (inq_stats (1, &proc_info
) != 0)
602 load
= proc_sum_data
.ps_nrunnable
;
604 for (i
= samples
- 1; i
> 0; --i
)
606 load
+= proc_sum_data
.ps_nrun
[j
];
607 if (j
++ == PS_NRUNSIZE
)
612 loadavg
[elem
++] = load
/ samples
/ cpus
;
615 #if !defined (LDAV_DONE) && defined (DGUX)
617 /* This call can return -1 for an error, but with good args
618 it's not supposed to fail. The first argument is for no
619 apparent reason of type `long int *'. */
620 dg_sys_info ((long int *) &load_info
,
621 DG_SYS_INFO_LOAD_INFO_TYPE
,
622 DG_SYS_INFO_LOAD_VERSION_0
);
625 loadavg
[elem
++] = load_info
.one_minute
;
627 loadavg
[elem
++] = load_info
.five_minute
;
629 loadavg
[elem
++] = load_info
.fifteen_minute
;
632 #if !defined (LDAV_DONE) && defined (apollo)
634 /* Apollo code from lisch@mentorg.com (Ray Lischner).
636 This system call is not documented. The load average is obtained as
637 three long integers, for the load average over the past minute,
638 five minutes, and fifteen minutes. Each value is a scaled integer,
639 with 16 bits of integer part and 16 bits of fraction part.
641 I'm not sure which operating system first supported this system call,
642 but I know that SR10.2 supports it. */
644 extern void proc1_$
get_loadav ();
645 unsigned long load_ave
[3];
647 proc1_$
get_loadav (load_ave
);
650 loadavg
[elem
++] = load_ave
[0] / 65536.0;
652 loadavg
[elem
++] = load_ave
[1] / 65536.0;
654 loadavg
[elem
++] = load_ave
[2] / 65536.0;
657 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
660 struct tbl_loadavg load_ave
;
661 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
663 = (load_ave
.tl_lscale
== 0
664 ? load_ave
.tl_avenrun
.d
[0]
665 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
666 #endif /* OSF_MIPS */
668 #if !defined (LDAV_DONE) && defined (VMS)
669 /* VMS specific code -- read from the Load Ave driver. */
671 LOAD_AVE_TYPE load_ave
[3];
672 static int getloadavg_initialized
= 0;
681 /* Ensure that there is a channel open to the load ave device. */
682 if (!getloadavg_initialized
)
684 /* Attempt to open the channel. */
686 descriptor
.dsc$w_length
= 18;
687 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
689 $
DESCRIPTOR (descriptor
, "LAV0:");
691 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
692 getloadavg_initialized
= 1;
695 /* Read the load average vector. */
696 if (getloadavg_initialized
697 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
698 load_ave
, 12, 0, 0, 0, 0) & 1))
700 sys$
dassgn (channel
);
701 getloadavg_initialized
= 0;
704 if (!getloadavg_initialized
)
708 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
710 /* UNIX-specific code -- read the average from /dev/kmem. */
712 #define LDAV_PRIVILEGED /* This code requires special installation. */
714 LOAD_AVE_TYPE load_ave
[3];
716 /* Get the address of LDAV_SYMBOL. */
721 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
722 strcpy (nl
[1].n_name
, "");
723 #else /* NLIST_STRUCT */
724 #ifdef NLIST_NAME_UNION
725 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
726 nl
[1].n_un
.n_name
= 0;
727 #else /* not NLIST_NAME_UNION */
728 nl
[0].n_name
= LDAV_SYMBOL
;
730 #endif /* not NLIST_NAME_UNION */
731 #endif /* NLIST_STRUCT */
734 if (nlist (KERNEL_FILE
, nl
) >= 0)
735 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
737 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
738 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
740 offset
= nl
[0].n_value
;
742 #endif /* !SUNOS_5 */
746 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
748 offset
= (long) ldav_off
& 0x7fffffff;
752 /* Make sure we have /dev/kmem open. */
753 if (!getloadavg_initialized
)
756 channel
= open ("/dev/kmem", 0);
758 getloadavg_initialized
= 1;
760 /* We pass 0 for the kernel, corefile, and swapfile names
761 to use the currently running kernel. */
762 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
765 /* nlist the currently running kernel. */
767 offset
= nl
[0].n_value
;
768 getloadavg_initialized
= 1;
773 /* If we can, get the load average values. */
774 if (offset
&& getloadavg_initialized
)
776 /* Try to read the load. */
778 if (lseek (channel
, offset
, 0) == -1L
779 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
780 != sizeof (load_ave
))
783 getloadavg_initialized
= 0;
786 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
787 != sizeof (load_ave
))
790 getloadavg_initialized
= 0;
795 if (offset
== 0 || !getloadavg_initialized
)
797 #endif /* LOAD_AVE_TYPE and not VMS */
799 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
801 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
803 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
805 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
808 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
813 /* Set errno to zero to indicate that there was no particular error;
814 this function just can't work at all on this system. */
820 #endif /* ! HAVE_GETLOADAVG */
831 naptime
= atoi (argv
[1]);
838 errno
= 0; /* Don't be misled if it doesn't set errno. */
839 loads
= getloadavg (avg
, 3);
842 perror ("Error getting load average");
846 printf ("1-minute: %f ", avg
[0]);
848 printf ("5-minute: %f ", avg
[1]);
850 printf ("15-minute: %f ", avg
[2]);