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. */
145 #if (defined(MORE_BSD) || defined(sun) || defined(decstation) || defined(_SEQUENT_) || defined(sgi) || defined(SVR4) || defined(sony_news) || defined(sequent) || defined (OSF_ALPHA) || (defined (ardent) && defined (titan)) || defined (tek4300))
146 #define LOAD_AVE_TYPE long
152 /* SunOS and some others define FSCALE in sys/param.h. */
155 #define FSCALE 2048.0
158 #if defined(MIPS) || defined(SVR4) || defined(decstation)
162 #if defined (sgi) || defined (sequent)
163 #define FSCALE 1000.0
166 #if defined (ardent) && defined (titan)
167 #define FSCALE 65536.0
174 #endif /* Not FSCALE. */
176 #if !defined (LDAV_CVT) && defined (FSCALE)
177 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
180 /* VAX C can't handle multi-line #ifs. */
181 #if !defined(NLIST_STRUCT) && (defined(MORE_BSD) || defined(sun) || defined(decstation) || defined(hpux) || defined(_SEQUENT_) || defined(sequent) || defined(sgi) || defined(SVR4) || defined(sony_news) || defined (OSF_ALPHA) || (defined (ardent) && defined (titan)) || defined (tek4300) || defined (butterfly))
186 #if defined(sgi) || (defined(mips) && !defined(BSD))
187 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
191 #if !defined (KERNEL_FILE) && defined (sequent)
192 #define KERNEL_FILE "/dynix"
195 #if !defined (KERNEL_FILE) && defined (hpux)
196 #define KERNEL_FILE "/hp-ux"
199 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
200 #define KERNEL_FILE "/unix"
204 #if !defined (LDAV_SYMBOL) && defined (alliant)
205 #define LDAV_SYMBOL "_Loadavg"
208 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
209 #define LDAV_SYMBOL "avenrun"
223 /* LOAD_AVE_TYPE should only get defined if we're going to use the
225 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
226 #define LOAD_AVE_TYPE double
234 #else /* NLIST_STRUCT */
236 #endif /* NLIST_STRUCT */
244 #define KERNEL_FILE "/vmunix"
245 #endif /* KERNEL_FILE */
248 #define LDAV_SYMBOL "_avenrun"
249 #endif /* LDAV_SYMBOL */
257 #include <vms/iodef.h>
262 #define LDAV_CVT(n) ((double) (n))
263 #endif /* !LDAV_CVT */
265 #endif /* LOAD_AVE_TYPE */
268 #ifdef HAVE_MACH_MACH_H
269 #include <mach/mach.h>
276 #include <sys/sysmp.h>
282 #include <sys/time.h>
283 #include <sys/wait.h>
284 #include <sys/syscall.h>
287 #include <machine/cpu.h>
288 #include <inq_stats/statistics.h>
289 #include <inq_stats/sysstats.h>
290 #include <inq_stats/cpustats.h>
291 #include <inq_stats/procstats.h>
292 #else /* Not UMAX_43. */
293 #include <sys/sysdefs.h>
294 #include <sys/statistics.h>
295 #include <sys/sysstats.h>
296 #include <sys/cpudefs.h>
297 #include <sys/cpustats.h>
298 #include <sys/procstats.h>
299 #endif /* Not UMAX_43. */
303 #include <sys/dg_sys_info.h>
306 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
309 #include <sys/file.h>
312 /* Avoid static vars inside a function since in HPUX they dump as pure. */
315 static processor_set_t default_set
;
316 static int getloadavg_initialized
;
320 static unsigned int cpus
= 0;
321 static unsigned int samples
;
325 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
329 /* File descriptor open to /dev/kmem or VMS load ave driver. */
331 /* Nonzero iff channel is valid. */
332 static int getloadavg_initialized
;
333 /* Offset in kmem to seek to read load average, or 0 means invalid. */
336 #if !defined(VMS) && !defined(sgi)
337 static struct nlist nl
[2];
338 #endif /* Not VMS or sgi */
344 #endif /* LOAD_AVE_TYPE */
346 /* Put the 1 minute, 5 minute and 15 minute load averages
347 into the first NELEM elements of LOADAVG.
348 Return the number written (never more than 3),
349 or -1 if an error occurred. */
352 getloadavg (loadavg
, nelem
)
356 int elem
= 0; /* Return value. */
358 #ifdef NO_GET_LOAD_AVG
360 /* Set errno to zero to indicate that there was no particular error;
361 this function just can't work at all on this system. */
366 #if !defined (LDAV_DONE) && defined (LINUX)
370 #ifndef LINUX_LDAV_FILE
371 #define LINUX_LDAV_FILE "/proc/loadavg"
378 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
381 count
= read (fd
, ldavgbuf
, 40);
386 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
387 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
391 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
392 loadavg
[elem
] = load_ave
[elem
];
398 #if !defined (LDAV_DONE) && defined (NeXT)
400 /* The NeXT code was adapted from iscreen 3.2. */
403 struct processor_set_basic_info info
;
408 /* We only know how to get the 1-minute average for this system. */
413 if (!getloadavg_initialized
)
415 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
416 getloadavg_initialized
= 1;
419 if (getloadavg_initialized
)
421 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
422 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
423 (processor_set_info_t
) &info
, &info_count
)
425 getloadavg_initialized
= 0;
429 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
433 if (!getloadavg_initialized
)
437 #if !defined (LDAV_DONE) && defined (UMAX)
439 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
440 have a /dev/kmem. Information about the workings of the running kernel
441 can be gathered with inq_stats system calls.
442 We only know how to get the 1-minute average for this system. */
444 struct proc_summary proc_sum_data
;
445 struct stat_descr proc_info
;
447 register unsigned int i
, j
;
451 register unsigned int c
, i
;
452 struct cpu_config conf
;
453 struct stat_descr desc
;
456 desc
.sd_subsys
= SUBSYS_CPU
;
457 desc
.sd_type
= CPUTYPE_CONFIG
;
458 desc
.sd_addr
= (char *) &conf
;
459 desc
.sd_size
= sizeof conf
;
461 if (inq_stats (1, &desc
))
465 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
467 struct class_stats stats
;
468 bzero ((char *) &stats
, sizeof stats
);
470 desc
.sd_type
= CPUTYPE_CLASS
;
472 desc
.sd_addr
= (char *) &stats
;
473 desc
.sd_size
= sizeof stats
;
475 if (inq_stats (1, &desc
))
478 c
+= stats
.class_numcpus
;
481 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
484 proc_info
.sd_next
= 0;
485 proc_info
.sd_subsys
= SUBSYS_PROC
;
486 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
487 proc_info
.sd_addr
= (char *) &proc_sum_data
;
488 proc_info
.sd_size
= sizeof (struct proc_summary
);
489 proc_info
.sd_sizeused
= 0;
491 if (inq_stats (1, &proc_info
) != 0)
494 load
= proc_sum_data
.ps_nrunnable
;
496 for (i
= samples
- 1; i
> 0; --i
)
498 load
+= proc_sum_data
.ps_nrun
[j
];
499 if (j
++ == PS_NRUNSIZE
)
504 loadavg
[elem
++] = load
/ samples
/ cpus
;
507 #if !defined (LDAV_DONE) && defined (DGUX)
509 /* This call can return -1 for an error, but with good args
510 it's not supposed to fail. The first argument is for no
511 apparent reason of type `long int *'. */
512 dg_sys_info ((long int *) &load_info
,
513 DG_SYS_INFO_LOAD_INFO_TYPE
,
514 DG_SYS_INFO_LOAD_VERSION_0
);
517 loadavg
[elem
++] = load_info
.one_minute
;
519 loadavg
[elem
++] = load_info
.five_minute
;
521 loadavg
[elem
++] = load_info
.fifteen_minute
;
524 #if !defined (LDAV_DONE) && defined (apollo)
526 /* Apollo code from lisch@mentorg.com (Ray Lischner).
528 This system call is not documented. The load average is obtained as
529 three long integers, for the load average over the past minute,
530 five minutes, and fifteen minutes. Each value is a scaled integer,
531 with 16 bits of integer part and 16 bits of fraction part.
533 I'm not sure which operating system first supported this system call,
534 but I know that SR10.2 supports it. */
536 extern void proc1_$
get_loadav ();
537 unsigned long load_ave
[3];
539 proc1_$
get_loadav (load_ave
);
542 loadavg
[elem
++] = load_ave
[0] / 65536.0;
544 loadavg
[elem
++] = load_ave
[1] / 65536.0;
546 loadavg
[elem
++] = load_ave
[2] / 65536.0;
549 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
552 struct tbl_loadavg load_ave
;
553 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
555 = (load_ave
.tl_lscale
== 0
556 ? load_ave
.tl_avenrun
.d
[0]
557 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
558 #endif /* OSF_MIPS */
560 #if !defined (LDAV_DONE) && defined (VMS)
561 /* VMS specific code -- read from the Load Ave driver. */
563 LOAD_AVE_TYPE load_ave
[3];
564 static int getloadavg_initialized
= 0;
573 /* Ensure that there is a channel open to the load ave device. */
574 if (!getloadavg_initialized
)
576 /* Attempt to open the channel. */
578 descriptor
.dsc$w_length
= 18;
579 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
581 $
DESCRIPTOR (descriptor
, "LAV0:");
583 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
584 getloadavg_initialized
= 1;
587 /* Read the load average vector. */
588 if (getloadavg_initialized
589 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
590 load_ave
, 12, 0, 0, 0, 0) & 1))
592 sys$
dassgn (channel
);
593 getloadavg_initialized
= 0;
596 if (!getloadavg_initialized
)
600 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
602 /* UNIX-specific code -- read the average from /dev/kmem. */
604 #define LDAV_PRIVILEGED /* This code requires special installation. */
606 LOAD_AVE_TYPE load_ave
[3];
608 /* Get the address of LDAV_SYMBOL. */
614 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
615 strcpy (nl
[1].n_name
, "");
616 #else /* NLIST_STRUCT */
617 #ifdef NLIST_NAME_UNION
618 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
619 nl
[1].n_un
.n_name
= 0;
620 #else /* not NLIST_NAME_UNION */
621 nl
[0].n_name
= LDAV_SYMBOL
;
623 #endif /* not NLIST_NAME_UNION */
624 #endif /* NLIST_STRUCT */
626 if (nlist (KERNEL_FILE
, nl
) >= 0)
627 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
629 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
630 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
632 offset
= nl
[0].n_value
;
637 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
639 offset
= (long) ldav_off
& 0x7fffffff;
641 #endif /* !SUNOS_5 */
644 /* Make sure we have /dev/kmem open. */
645 if (!getloadavg_initialized
)
648 channel
= open ("/dev/kmem", 0);
650 getloadavg_initialized
= 1;
652 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
656 getloadavg_initialized
= 1;
661 /* If we can, get the load average values. */
662 if (offset
&& getloadavg_initialized
)
664 /* Try to read the load. */
666 if (lseek (channel
, offset
, 0) == -1L
667 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
668 != sizeof (load_ave
))
671 getloadavg_initialized
= 0;
674 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
675 != sizeof (load_ave
))
678 getloadavg_initialized
= 0;
683 if (offset
== 0 || !getloadavg_initialized
)
685 #endif /* LOAD_AVE_TYPE and not VMS */
687 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
689 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
691 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
693 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
696 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
701 /* Set errno to zero to indicate that there was no particular error;
702 this function just can't work at all on this system. */
717 naptime
= atoi (argv
[1]);
727 errno
= 0; /* Don't be misled if it doesn't set errno. */
728 loads
= getloadavg (avg
, 3);
731 perror ("Error getting load average");
735 printf ("1-minute: %f ", avg
[0]);
737 printf ("5-minute: %f ", avg
[1]);
739 printf ("15-minute: %f ", avg
[2]);