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]: Name of 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>
75 /* The existing Emacs configuration files define a macro called
76 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
77 returns the load average multiplied by 100. What we actually want
78 is a macro called LDAV_CVT, which returns the load average as an
81 For backwards compatibility, we'll define LDAV_CVT in terms of
82 LOAD_AVE_CVT, but future machine config files should just define
85 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
86 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
89 #if !defined (BSD) && defined (ultrix)
90 /* Ultrix behaves like BSD on Vaxen. */
95 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
96 conflicts with the definition understood in this file, that this
100 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
101 defined to mean that the nlist method should be used, which is not true. */
105 /* Set values that are different from the defaults, which are
106 set a little farther down with #ifndef. */
109 /* Some shorthands. */
111 #if defined (HPUX) && !defined (hpux)
115 #if defined(hp300) && !defined(hpux)
119 #if defined(ultrix) && defined(mips)
123 #if defined(sun) && defined(SVR4)
127 #if defined (__osf__) && defined (__alpha__)
131 #if defined (__osf__) && (defined (mips) || defined (__mips__))
133 #include <sys/table.h>
136 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
137 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
138 that with a couple of other things and we'll have a unique match. */
139 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
140 #define tek4300 /* Define by emacs, but not by other users. */
144 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
145 #ifndef LOAD_AVE_TYPE
148 #define LOAD_AVE_TYPE long
152 #define LOAD_AVE_TYPE long
156 #define LOAD_AVE_TYPE long
160 #define LOAD_AVE_TYPE long
164 #define LOAD_AVE_TYPE long
168 #define LOAD_AVE_TYPE long
172 #define LOAD_AVE_TYPE long
176 #define LOAD_AVE_TYPE long
180 #define LOAD_AVE_TYPE long
183 #if defined (ardent) && defined (titan)
184 #define LOAD_AVE_TYPE long
188 #define LOAD_AVE_TYPE long
191 #endif /* No LOAD_AVE_TYPE. */
195 /* SunOS and some others define FSCALE in sys/param.h. */
198 #define FSCALE 2048.0
201 #if defined(MIPS) || defined(SVR4) || defined(decstation)
205 #if defined (sgi) || defined (sequent)
206 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
207 above under #ifdef MIPS. But we want the sgi value. */
209 #define FSCALE 1000.0
212 #if defined (ardent) && defined (titan)
213 #define FSCALE 65536.0
220 #endif /* Not FSCALE. */
222 #if !defined (LDAV_CVT) && defined (FSCALE)
223 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
226 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
245 #if defined (_SEQUENT_) || defined (sequent)
265 #if defined (ardent) && defined (titan)
277 #endif /* defined (NLIST_STRUCT) */
280 #if defined(sgi) || (defined(mips) && !defined(BSD))
281 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
285 #if !defined (KERNEL_FILE) && defined (sequent)
286 #define KERNEL_FILE "/dynix"
289 #if !defined (KERNEL_FILE) && defined (hpux)
290 #define KERNEL_FILE "/hp-ux"
293 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
294 #define KERNEL_FILE "/unix"
298 #if !defined (LDAV_SYMBOL) && defined (alliant)
299 #define LDAV_SYMBOL "_Loadavg"
302 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
303 #define LDAV_SYMBOL "avenrun"
317 /* LOAD_AVE_TYPE should only get defined if we're going to use the
319 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
320 #define LOAD_AVE_TYPE double
328 #else /* NLIST_STRUCT */
330 #endif /* NLIST_STRUCT */
338 #define KERNEL_FILE "/vmunix"
339 #endif /* KERNEL_FILE */
342 #define LDAV_SYMBOL "_avenrun"
343 #endif /* LDAV_SYMBOL */
351 #include <vms/iodef.h>
356 #define LDAV_CVT(n) ((double) (n))
357 #endif /* !LDAV_CVT */
359 #endif /* LOAD_AVE_TYPE */
362 #ifdef HAVE_MACH_MACH_H
363 #include <mach/mach.h>
370 #include <sys/sysmp.h>
376 #include <sys/time.h>
377 #include <sys/wait.h>
378 #include <sys/syscall.h>
381 #include <machine/cpu.h>
382 #include <inq_stats/statistics.h>
383 #include <inq_stats/sysstats.h>
384 #include <inq_stats/cpustats.h>
385 #include <inq_stats/procstats.h>
386 #else /* Not UMAX_43. */
387 #include <sys/sysdefs.h>
388 #include <sys/statistics.h>
389 #include <sys/sysstats.h>
390 #include <sys/cpudefs.h>
391 #include <sys/cpustats.h>
392 #include <sys/procstats.h>
393 #endif /* Not UMAX_43. */
397 #include <sys/dg_sys_info.h>
400 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
403 #include <sys/file.h>
406 /* Avoid static vars inside a function since in HPUX they dump as pure. */
409 static processor_set_t default_set
;
410 static int getloadavg_initialized
;
414 static unsigned int cpus
= 0;
415 static unsigned int samples
;
419 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
423 /* File descriptor open to /dev/kmem or VMS load ave driver. */
425 /* Nonzero iff channel is valid. */
426 static int getloadavg_initialized
;
427 /* Offset in kmem to seek to read load average, or 0 means invalid. */
430 #if !defined(VMS) && !defined(sgi)
431 static struct nlist nl
[2];
432 #endif /* Not VMS or sgi */
438 #endif /* LOAD_AVE_TYPE */
440 /* Put the 1 minute, 5 minute and 15 minute load averages
441 into the first NELEM elements of LOADAVG.
442 Return the number written (never more than 3, but may be less than NELEM),
443 or -1 if an error occurred. */
446 getloadavg (loadavg
, nelem
)
450 int elem
= 0; /* Return value. */
452 #ifdef NO_GET_LOAD_AVG
454 /* Set errno to zero to indicate that there was no particular error;
455 this function just can't work at all on this system. */
460 #if !defined (LDAV_DONE) && defined (LINUX)
464 #ifndef LINUX_LDAV_FILE
465 #define LINUX_LDAV_FILE "/proc/loadavg"
472 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
475 count
= read (fd
, ldavgbuf
, 40);
480 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
481 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
485 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
486 loadavg
[elem
] = load_ave
[elem
];
492 #if !defined (LDAV_DONE) && defined (NeXT)
494 /* The NeXT code was adapted from iscreen 3.2. */
497 struct processor_set_basic_info info
;
500 /* We only know how to get the 1-minute average for this system,
501 so even if the caller asks for more than 1, we only return 1. */
503 if (!getloadavg_initialized
)
505 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
506 getloadavg_initialized
= 1;
509 if (getloadavg_initialized
)
511 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
512 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
513 (processor_set_info_t
) &info
, &info_count
)
515 getloadavg_initialized
= 0;
519 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
523 if (!getloadavg_initialized
)
527 #if !defined (LDAV_DONE) && defined (UMAX)
529 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
530 have a /dev/kmem. Information about the workings of the running kernel
531 can be gathered with inq_stats system calls.
532 We only know how to get the 1-minute average for this system. */
534 struct proc_summary proc_sum_data
;
535 struct stat_descr proc_info
;
537 register unsigned int i
, j
;
541 register unsigned int c
, i
;
542 struct cpu_config conf
;
543 struct stat_descr desc
;
546 desc
.sd_subsys
= SUBSYS_CPU
;
547 desc
.sd_type
= CPUTYPE_CONFIG
;
548 desc
.sd_addr
= (char *) &conf
;
549 desc
.sd_size
= sizeof conf
;
551 if (inq_stats (1, &desc
))
555 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
557 struct class_stats stats
;
558 bzero ((char *) &stats
, sizeof stats
);
560 desc
.sd_type
= CPUTYPE_CLASS
;
562 desc
.sd_addr
= (char *) &stats
;
563 desc
.sd_size
= sizeof stats
;
565 if (inq_stats (1, &desc
))
568 c
+= stats
.class_numcpus
;
571 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
574 proc_info
.sd_next
= 0;
575 proc_info
.sd_subsys
= SUBSYS_PROC
;
576 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
577 proc_info
.sd_addr
= (char *) &proc_sum_data
;
578 proc_info
.sd_size
= sizeof (struct proc_summary
);
579 proc_info
.sd_sizeused
= 0;
581 if (inq_stats (1, &proc_info
) != 0)
584 load
= proc_sum_data
.ps_nrunnable
;
586 for (i
= samples
- 1; i
> 0; --i
)
588 load
+= proc_sum_data
.ps_nrun
[j
];
589 if (j
++ == PS_NRUNSIZE
)
594 loadavg
[elem
++] = load
/ samples
/ cpus
;
597 #if !defined (LDAV_DONE) && defined (DGUX)
599 /* This call can return -1 for an error, but with good args
600 it's not supposed to fail. The first argument is for no
601 apparent reason of type `long int *'. */
602 dg_sys_info ((long int *) &load_info
,
603 DG_SYS_INFO_LOAD_INFO_TYPE
,
604 DG_SYS_INFO_LOAD_VERSION_0
);
607 loadavg
[elem
++] = load_info
.one_minute
;
609 loadavg
[elem
++] = load_info
.five_minute
;
611 loadavg
[elem
++] = load_info
.fifteen_minute
;
614 #if !defined (LDAV_DONE) && defined (apollo)
616 /* Apollo code from lisch@mentorg.com (Ray Lischner).
618 This system call is not documented. The load average is obtained as
619 three long integers, for the load average over the past minute,
620 five minutes, and fifteen minutes. Each value is a scaled integer,
621 with 16 bits of integer part and 16 bits of fraction part.
623 I'm not sure which operating system first supported this system call,
624 but I know that SR10.2 supports it. */
626 extern void proc1_$
get_loadav ();
627 unsigned long load_ave
[3];
629 proc1_$
get_loadav (load_ave
);
632 loadavg
[elem
++] = load_ave
[0] / 65536.0;
634 loadavg
[elem
++] = load_ave
[1] / 65536.0;
636 loadavg
[elem
++] = load_ave
[2] / 65536.0;
639 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
642 struct tbl_loadavg load_ave
;
643 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
645 = (load_ave
.tl_lscale
== 0
646 ? load_ave
.tl_avenrun
.d
[0]
647 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
648 #endif /* OSF_MIPS */
650 #if !defined (LDAV_DONE) && defined (VMS)
651 /* VMS specific code -- read from the Load Ave driver. */
653 LOAD_AVE_TYPE load_ave
[3];
654 static int getloadavg_initialized
= 0;
663 /* Ensure that there is a channel open to the load ave device. */
664 if (!getloadavg_initialized
)
666 /* Attempt to open the channel. */
668 descriptor
.dsc$w_length
= 18;
669 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
671 $
DESCRIPTOR (descriptor
, "LAV0:");
673 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
674 getloadavg_initialized
= 1;
677 /* Read the load average vector. */
678 if (getloadavg_initialized
679 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
680 load_ave
, 12, 0, 0, 0, 0) & 1))
682 sys$
dassgn (channel
);
683 getloadavg_initialized
= 0;
686 if (!getloadavg_initialized
)
690 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
692 /* UNIX-specific code -- read the average from /dev/kmem. */
694 #define LDAV_PRIVILEGED /* This code requires special installation. */
696 LOAD_AVE_TYPE load_ave
[3];
698 /* Get the address of LDAV_SYMBOL. */
703 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
704 strcpy (nl
[1].n_name
, "");
705 #else /* NLIST_STRUCT */
706 #ifdef NLIST_NAME_UNION
707 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
708 nl
[1].n_un
.n_name
= 0;
709 #else /* not NLIST_NAME_UNION */
710 nl
[0].n_name
= LDAV_SYMBOL
;
712 #endif /* not NLIST_NAME_UNION */
713 #endif /* NLIST_STRUCT */
716 if (nlist (KERNEL_FILE
, nl
) >= 0)
717 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
719 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
720 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
722 offset
= nl
[0].n_value
;
724 #endif /* !SUNOS_5 */
728 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
730 offset
= (long) ldav_off
& 0x7fffffff;
734 /* Make sure we have /dev/kmem open. */
735 if (!getloadavg_initialized
)
738 channel
= open ("/dev/kmem", 0);
740 getloadavg_initialized
= 1;
742 /* We pass 0 for the kernel, corefile, and swapfile names
743 to use the currently running kernel. */
744 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
747 /* nlist the currently running kernel. */
749 offset
= nl
[0].n_value
;
750 getloadavg_initialized
= 1;
755 /* If we can, get the load average values. */
756 if (offset
&& getloadavg_initialized
)
758 /* Try to read the load. */
760 if (lseek (channel
, offset
, 0) == -1L
761 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
762 != sizeof (load_ave
))
765 getloadavg_initialized
= 0;
768 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
769 != sizeof (load_ave
))
772 getloadavg_initialized
= 0;
777 if (offset
== 0 || !getloadavg_initialized
)
779 #endif /* LOAD_AVE_TYPE and not VMS */
781 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
783 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
785 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
787 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
790 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
795 /* Set errno to zero to indicate that there was no particular error;
796 this function just can't work at all on this system. */
811 naptime
= atoi (argv
[1]);
821 errno
= 0; /* Don't be misled if it doesn't set errno. */
822 loads
= getloadavg (avg
, 3);
825 perror ("Error getting load average");
829 printf ("1-minute: %f ", avg
[0]);
831 printf ("5-minute: %f ", avg
[1]);
833 printf ("15-minute: %f ", avg
[2]);