1 /* Get the system load averages.
3 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994,
4 1995, 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007 Free Software
7 NOTE: The canonical source of this file is maintained with gnulib.
8 Bugs can be reported to bug-gnulib@gnu.org.
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Compile-time symbols that this file uses:
25 HAVE_PSTAT_GETDYNAMIC Define this if your system has the
26 pstat_getdynamic function. I think it
27 is unique to HPUX9. The best way to get the
28 definition is through the AC_FUNC_GETLOADAVG
29 macro that comes with autoconf 2.13 or newer.
30 If that isn't an option, then just put
31 AC_CHECK_FUNCS(pstat_getdynamic) in your
33 HAVE_LIBPERFSTAT Define this if your system has the
34 perfstat_cpu_total function in libperfstat (AIX).
35 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
36 KERNEL_FILE Name of the kernel file to nlist.
37 LDAV_CVT() Scale the load average from the kernel.
39 LDAV_SYMBOL Name of kernel symbol giving load average.
40 LOAD_AVE_TYPE Type of the load average array in the kernel.
41 Must be defined unless one of
42 apollo, DGUX, NeXT, or UMAX is defined;
44 otherwise, no load average is available.
45 HAVE_NLIST_H nlist.h is available. NLIST_STRUCT defaults
47 NLIST_STRUCT Include nlist.h, not a.out.h, and
48 the nlist n_name element is a pointer,
50 HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'.
51 LINUX_LDAV_FILE [__linux__, __CYGWIN__]: File containing
54 Specific system predefines this file uses, aside from setting
55 default values if not emacs:
58 BSD Real BSD, not just BSD-like.
61 eunice UNIX emulator under VMS.
63 __MSDOS__ No-op for MSDOS.
66 sequent Sequent Dynix 3.x.x (BSD)
67 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
68 sony_news NEWS-OS (works at least for 4.1C)
72 WINDOWS32 No-op for Windows95/NT.
73 __linux__ Linux: assumes /proc file system mounted.
74 Support from Michael K. Johnson.
75 __CYGWIN__ Cygwin emulates linux /proc/loadavg.
76 __NetBSD__ NetBSD: assumes /kern file system mounted.
78 In addition, to avoid nesting many #ifdefs, we internally set
79 LDAV_DONE to indicate that the load average has been computed.
81 We also #define LDAV_PRIVILEGED if a program will require
82 special installation to be able to call getloadavg. */
84 /* "configure" defines CONFIGURING_GETLOADAVG to sidestep problems
85 with partially-configured source directories. */
87 #ifndef CONFIGURING_GETLOADAVG
96 /* Exclude all the code except the test program at the end
97 if the system has its own `getloadavg' function. */
99 #ifndef HAVE_GETLOADAVG
101 # include <sys/types.h>
103 /* Both the Emacs and non-Emacs sections want this. Some
104 configuration files' definitions for the LOAD_AVE_CVT macro (like
105 sparc.h's) use macros like FSCALE, defined here. */
106 # if defined (unix) || defined (__unix)
107 # include <sys/param.h>
110 # include "c-strtod.h"
111 # include "cloexec.h"
112 # include "intprops.h"
115 /* The existing Emacs configuration files define a macro called
116 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
117 returns the load average multiplied by 100. What we actually want
118 is a macro called LDAV_CVT, which returns the load average as an
121 For backwards compatibility, we'll define LDAV_CVT in terms of
122 LOAD_AVE_CVT, but future machine config files should just define
123 LDAV_CVT directly. */
125 # if !defined (LDAV_CVT) && defined (LOAD_AVE_CVT)
126 # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
129 # if !defined (BSD) && defined (ultrix)
130 /* Ultrix behaves like BSD on Vaxen. */
135 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
136 conflicts with the definition understood in this file, that this
140 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
141 defined to mean that the nlist method should be used, which is not true. */
145 /* Same issues as for NeXT apply to the HURD-based GNU system. */
149 # endif /* __GNU__ */
151 /* Set values that are different from the defaults, which are
152 set a little farther down with #ifndef. */
155 /* Some shorthands. */
157 # if defined (HPUX) && !defined (hpux)
161 # if defined (__hpux) && !defined (hpux)
165 # if defined (__sun) && !defined (sun)
169 # if defined (hp300) && !defined (hpux)
173 # if defined (ultrix) && defined (mips)
177 # if defined (__SVR4) && !defined (SVR4)
181 # if (defined (sun) && defined (SVR4)) || defined (SOLARIS2)
185 # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
187 # include <sys/mbuf.h>
188 # include <sys/socket.h>
189 # include <net/route.h>
190 # include <sys/table.h>
191 /* Tru64 4.0D's table.h redefines sys */
195 # if defined (__osf__) && (defined (mips) || defined (__mips__))
197 # include <sys/table.h>
200 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
201 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
202 that with a couple of other things and we'll have a unique match. */
203 # if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
204 # define tek4300 /* Define by emacs, but not by other users. */
208 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
209 # ifndef LOAD_AVE_TYPE
212 # define LOAD_AVE_TYPE long
216 # define LOAD_AVE_TYPE long
220 # define LOAD_AVE_TYPE long
224 # define LOAD_AVE_TYPE long
228 # define LOAD_AVE_TYPE long
232 # define LOAD_AVE_TYPE long
236 # define LOAD_AVE_TYPE long
240 # define LOAD_AVE_TYPE long
244 # define LOAD_AVE_TYPE long
247 # if defined (ardent) && defined (titan)
248 # define LOAD_AVE_TYPE long
252 # define LOAD_AVE_TYPE long
255 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
256 # define LOAD_AVE_TYPE long
259 # if defined _AIX && ! defined HAVE_LIBPERFSTAT
260 # define LOAD_AVE_TYPE long
264 # define LOAD_AVE_TYPE double
266 # define LDAV_CVT(n) (n)
270 # endif /* No LOAD_AVE_TYPE. */
273 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
274 according to ghazi@noc.rutgers.edu. */
276 # define FSCALE 1024.0
279 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
280 /* <sys/param.h> defines an incorrect value for FSCALE on an
281 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
283 # define FSCALE 100.0
289 /* SunOS and some others define FSCALE in sys/param.h. */
292 # define FSCALE 2048.0
295 # if defined (MIPS) || defined (SVR4) || defined (decstation)
299 # if defined (sgi) || defined (sequent)
300 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
301 above under #ifdef MIPS. But we want the sgi value. */
303 # define FSCALE 1000.0
306 # if defined (ardent) && defined (titan)
307 # define FSCALE 65536.0
311 # define FSCALE 100.0
314 # if defined _AIX && !defined HAVE_LIBPERFSTAT
315 # define FSCALE 65536.0
318 # endif /* Not FSCALE. */
320 # if !defined (LDAV_CVT) && defined (FSCALE)
321 # define LDAV_CVT(n) (((double) (n)) / FSCALE)
324 # ifndef NLIST_STRUCT
326 # define NLIST_STRUCT
330 # if defined (sgi) || (defined (mips) && !defined (BSD))
331 # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
335 # if !defined (KERNEL_FILE) && defined (sequent)
336 # define KERNEL_FILE "/dynix"
339 # if !defined (KERNEL_FILE) && defined (hpux)
340 # define KERNEL_FILE "/hp-ux"
343 # if !defined (KERNEL_FILE) && (defined (_SEQUENT_) || defined (MIPS) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
344 # define KERNEL_FILE "/unix"
348 # if !defined (LDAV_SYMBOL) && defined (alliant)
349 # define LDAV_SYMBOL "_Loadavg"
352 # if !defined (LDAV_SYMBOL) && ((defined (hpux) && !defined (hp9000s300)) || defined (_SEQUENT_) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)) || (defined (_AIX) && !defined(HAVE_LIBPERFSTAT)))
353 # define LDAV_SYMBOL "avenrun"
358 /* LOAD_AVE_TYPE should only get defined if we're going to use the
360 # if !defined (LOAD_AVE_TYPE) && (defined (BSD) || defined (LDAV_CVT) || defined (KERNEL_FILE) || defined (LDAV_SYMBOL))
361 # define LOAD_AVE_TYPE double
364 # ifdef LOAD_AVE_TYPE
368 # ifndef NLIST_STRUCT
370 # else /* NLIST_STRUCT */
372 # endif /* NLIST_STRUCT */
380 # if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
381 # include <sys/pstat.h>
385 # define KERNEL_FILE "/vmunix"
386 # endif /* KERNEL_FILE */
389 # define LDAV_SYMBOL "_avenrun"
390 # endif /* LDAV_SYMBOL */
391 # endif /* __linux__ */
397 # include <descrip.h>
399 # include <vms/iodef.h>
404 # define LDAV_CVT(n) ((double) (n))
405 # endif /* !LDAV_CVT */
407 # endif /* LOAD_AVE_TYPE */
409 # if defined HAVE_LIBPERFSTAT
410 # include <libperfstat.h>
411 # include <sys/proc.h>
417 # if defined (__GNU__) && !defined (NeXT)
418 /* Note that NeXT Openstep defines __GNU__ even though it should not. */
419 /* GNU system acts much like NeXT, for load average purposes,
422 # define host_self mach_host_self
426 # ifdef HAVE_MACH_MACH_H
427 # include <mach/mach.h>
434 # include <sys/sysmp.h>
439 # include <sys/time.h>
440 # include <sys/wait.h>
441 # include <sys/syscall.h>
444 # include <machine/cpu.h>
445 # include <inq_stats/statistics.h>
446 # include <inq_stats/sysstats.h>
447 # include <inq_stats/cpustats.h>
448 # include <inq_stats/procstats.h>
449 # else /* Not UMAX_43. */
450 # include <sys/sysdefs.h>
451 # include <sys/statistics.h>
452 # include <sys/sysstats.h>
453 # include <sys/cpudefs.h>
454 # include <sys/cpustats.h>
455 # include <sys/procstats.h>
456 # endif /* Not UMAX_43. */
460 # include <sys/dg_sys_info.h>
463 # include "fcntl--.h"
465 /* Avoid static vars inside a function since in HPUX they dump as pure. */
468 static processor_set_t default_set;
469 static bool getloadavg_initialized;
473 static unsigned int cpus = 0;
474 static unsigned int samples;
478 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
481 # if !defined (HAVE_LIBKSTAT) && defined (LOAD_AVE_TYPE)
482 /* File descriptor open to /dev/kmem or VMS load ave driver. */
484 /* True iff channel is valid. */
485 static bool getloadavg_initialized;
486 /* Offset in kmem to seek to read load average, or 0 means invalid. */
489 # if ! defined __VMS && ! defined sgi && ! defined __linux__
490 static struct nlist nl[2];
495 # endif /* SUNOS_5 */
497 # endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */
499 /* Put the 1 minute, 5 minute and 15 minute load averages
500 into the first NELEM elements of LOADAVG.
501 Return the number written (never more than 3, but may be less than NELEM),
502 or -1 if an error occurred. */
505 getloadavg (double loadavg[], int nelem)
507 int elem = 0; /* Return value. */
509 # ifdef NO_GET_LOAD_AVG
511 /* Set errno to zero to indicate that there was no particular error;
512 this function just can't work at all on this system. */
517 # if !defined (LDAV_DONE) && defined (HAVE_LIBKSTAT)
518 /* Use libkstat because we don't have to be root. */
527 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
530 if (kstat_read (kc, ksp, 0) == -1)
534 kn = kstat_data_lookup (ksp, "avenrun_1min");
537 /* Return -1 if no load average information is available. */
543 loadavg[elem++] = (double) kn->value.ul / FSCALE;
547 kn = kstat_data_lookup (ksp, "avenrun_5min");
550 loadavg[elem++] = (double) kn->value.ul / FSCALE;
554 kn = kstat_data_lookup (ksp, "avenrun_15min");
556 loadavg[elem++] = (double) kn->value.ul / FSCALE;
562 # endif /* HAVE_LIBKSTAT */
564 # if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
565 /* Use pstat_getdynamic() because we don't have to be root. */
567 # undef LOAD_AVE_TYPE
569 struct pst_dynamic dyn_info;
570 if (pstat_getdynamic (&dyn_info, sizeof (dyn_info), 0, 0) < 0)
573 loadavg[elem++] = dyn_info.psd_avg_1_min;
575 loadavg[elem++] = dyn_info.psd_avg_5_min;
577 loadavg[elem++] = dyn_info.psd_avg_15_min;
579 # endif /* hpux && HAVE_PSTAT_GETDYNAMIC */
581 # if ! defined LDAV_DONE && defined HAVE_LIBPERFSTAT
583 # undef LOAD_AVE_TYPE
584 /* Use perfstat_cpu_total because we don't have to be root. */
586 perfstat_cpu_total_t cpu_stats;
587 int result = perfstat_cpu_total (NULL, &cpu_stats, sizeof cpu_stats, 1);
590 loadavg[0] = cpu_stats.loadavg[0] / (double)(1 << SBITS);
591 loadavg[1] = cpu_stats.loadavg[1] / (double)(1 << SBITS);
592 loadavg[2] = cpu_stats.loadavg[2] / (double)(1 << SBITS);
597 # if !defined (LDAV_DONE) && (defined (__linux__) || defined (__CYGWIN__))
599 # undef LOAD_AVE_TYPE
601 # ifndef LINUX_LDAV_FILE
602 # define LINUX_LDAV_FILE "/proc/loadavg"
605 char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
606 char const *ptr = ldavgbuf;
609 fd = open (LINUX_LDAV_FILE, O_RDONLY);
612 count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
616 ldavgbuf[count] = '\0';
618 for (elem = 0; elem < nelem; elem++)
621 double d = c_strtod (ptr, &endptr);
634 # endif /* __linux__ || __CYGWIN__ */
636 # if !defined (LDAV_DONE) && defined (__NetBSD__)
638 # undef LOAD_AVE_TYPE
640 # ifndef NETBSD_LDAV_FILE
641 # define NETBSD_LDAV_FILE "/kern/loadavg"
644 unsigned long int load_ave[3], scale;
648 fp = fopen (NETBSD_LDAV_FILE, "r");
651 count = fscanf (fp, "%lu %lu %lu %lu\n",
652 &load_ave[0], &load_ave[1], &load_ave[2],
658 for (elem = 0; elem < nelem; elem++)
659 loadavg[elem] = (double) load_ave[elem] / (double) scale;
663 # endif /* __NetBSD__ */
665 # if !defined (LDAV_DONE) && defined (NeXT)
667 /* The NeXT code was adapted from iscreen 3.2. */
670 struct processor_set_basic_info info;
671 unsigned int info_count;
673 /* We only know how to get the 1-minute average for this system,
674 so even if the caller asks for more than 1, we only return 1. */
676 if (!getloadavg_initialized)
678 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
679 getloadavg_initialized = true;
682 if (getloadavg_initialized)
684 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
685 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
686 (processor_set_info_t) &info, &info_count)
688 getloadavg_initialized = false;
692 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
696 if (!getloadavg_initialized)
700 # if !defined (LDAV_DONE) && defined (UMAX)
702 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
703 have a /dev/kmem. Information about the workings of the running kernel
704 can be gathered with inq_stats system calls.
705 We only know how to get the 1-minute average for this system. */
707 struct proc_summary proc_sum_data;
708 struct stat_descr proc_info;
710 register unsigned int i, j;
714 register unsigned int c, i;
715 struct cpu_config conf;
716 struct stat_descr desc;
719 desc.sd_subsys = SUBSYS_CPU;
720 desc.sd_type = CPUTYPE_CONFIG;
721 desc.sd_addr = (char *) &conf;
722 desc.sd_size = sizeof conf;
724 if (inq_stats (1, &desc))
728 for (i = 0; i < conf.config_maxclass; ++i)
730 struct class_stats stats;
731 bzero ((char *) &stats, sizeof stats);
733 desc.sd_type = CPUTYPE_CLASS;
735 desc.sd_addr = (char *) &stats;
736 desc.sd_size = sizeof stats;
738 if (inq_stats (1, &desc))
741 c += stats.class_numcpus;
744 samples = cpus < 2 ? 3 : (2 * cpus / 3);
747 proc_info.sd_next = 0;
748 proc_info.sd_subsys = SUBSYS_PROC;
749 proc_info.sd_type = PROCTYPE_SUMMARY;
750 proc_info.sd_addr = (char *) &proc_sum_data;
751 proc_info.sd_size = sizeof (struct proc_summary);
752 proc_info.sd_sizeused = 0;
754 if (inq_stats (1, &proc_info) != 0)
757 load = proc_sum_data.ps_nrunnable;
759 for (i = samples - 1; i > 0; --i)
761 load += proc_sum_data.ps_nrun[j];
762 if (j++ == PS_NRUNSIZE)
767 loadavg[elem++] = load / samples / cpus;
770 # if !defined (LDAV_DONE) && defined (DGUX)
772 /* This call can return -1 for an error, but with good args
773 it's not supposed to fail. The first argument is for no
774 apparent reason of type `long int *'. */
775 dg_sys_info ((long int *) &load_info,
776 DG_SYS_INFO_LOAD_INFO_TYPE,
777 DG_SYS_INFO_LOAD_VERSION_0);
780 loadavg[elem++] = load_info.one_minute;
782 loadavg[elem++] = load_info.five_minute;
784 loadavg[elem++] = load_info.fifteen_minute;
787 # if !defined (LDAV_DONE) && defined (apollo)
789 /* Apollo code from lisch@mentorg.com (Ray Lischner).
791 This system call is not documented. The load average is obtained as
792 three long integers, for the load average over the past minute,
793 five minutes, and fifteen minutes. Each value is a scaled integer,
794 with 16 bits of integer part and 16 bits of fraction part.
796 I'm not sure which operating system first supported this system call,
797 but I know that SR10.2 supports it. */
799 extern void proc1_$get_loadav ();
800 unsigned long load_ave[3];
802 proc1_$get_loadav (load_ave);
805 loadavg[elem++] = load_ave[0] / 65536.0;
807 loadavg[elem++] = load_ave[1] / 65536.0;
809 loadavg[elem++] = load_ave[2] / 65536.0;
812 # if !defined (LDAV_DONE) && defined (OSF_MIPS)
815 struct tbl_loadavg load_ave;
816 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
818 = (load_ave.tl_lscale == 0
819 ? load_ave.tl_avenrun.d[0]
820 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
821 # endif /* OSF_MIPS */
823 # if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
826 /* A faithful emulation is going to have to be saved for a rainy day. */
827 for ( ; elem < nelem; elem++)
831 # endif /* __MSDOS__ || WINDOWS32 */
833 # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
836 struct tbl_loadavg load_ave;
837 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
838 for (elem = 0; elem < nelem; elem++)
840 = (load_ave.tl_lscale == 0
841 ? load_ave.tl_avenrun.d[elem]
842 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
843 # endif /* OSF_ALPHA */
845 # if ! defined LDAV_DONE && defined __VMS
846 /* VMS specific code -- read from the Load Ave driver. */
848 LOAD_AVE_TYPE load_ave[3];
849 static bool getloadavg_initialized;
858 /* Ensure that there is a channel open to the load ave device. */
859 if (!getloadavg_initialized)
861 /* Attempt to open the channel. */
863 descriptor.dsc$w_length = 18;
864 descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
866 $DESCRIPTOR (descriptor, "LAV0:");
868 if (sys$assign (&descriptor, &channel, 0, 0) & 1)
869 getloadavg_initialized = true;
872 /* Read the load average vector. */
873 if (getloadavg_initialized
874 && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
875 load_ave, 12, 0, 0, 0, 0) & 1))
877 sys$dassgn (channel);
878 getloadavg_initialized = false;
881 if (!getloadavg_initialized)
883 # endif /* ! defined LDAV_DONE && defined __VMS */
885 # if ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS
887 /* UNIX-specific code -- read the average from /dev/kmem. */
889 # define LDAV_PRIVILEGED /* This code requires special installation. */
891 LOAD_AVE_TYPE load_ave[3];
893 /* Get the address of LDAV_SYMBOL. */
897 # ifndef NLIST_STRUCT
898 strcpy (nl[0].n_name, LDAV_SYMBOL);
899 strcpy (nl[1].n_name, "");
900 # else /* NLIST_STRUCT */
901 # ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
902 nl[0].n_un.n_name = LDAV_SYMBOL;
903 nl[1].n_un.n_name = 0;
904 # else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
905 nl[0].n_name = LDAV_SYMBOL;
907 # endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
908 # endif /* NLIST_STRUCT */
912 # if !(defined (_AIX) && !defined (ps2))
913 nlist (KERNEL_FILE, nl)
915 knlist (nl, 1, sizeof (nl[0]))
918 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
920 # ifdef FIXUP_KERNEL_SYMBOL_ADDR
921 FIXUP_KERNEL_SYMBOL_ADDR (nl);
923 offset = nl[0].n_value;
925 # endif /* !SUNOS_5 */
929 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
931 offset = (long int) ldav_off & 0x7fffffff;
935 /* Make sure we have /dev/kmem open. */
936 if (!getloadavg_initialized)
939 channel = open ("/dev/kmem", O_RDONLY);
942 /* Set the channel to close on exec, so it does not
943 litter any child's descriptor table. */
944 set_cloexec_flag (channel, true);
945 getloadavg_initialized = true;
948 /* We pass 0 for the kernel, corefile, and swapfile names
949 to use the currently running kernel. */
950 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
953 /* nlist the currently running kernel. */
955 offset = nl[0].n_value;
956 getloadavg_initialized = true;
958 # endif /* SUNOS_5 */
961 /* If we can, get the load average values. */
962 if (offset && getloadavg_initialized)
964 /* Try to read the load. */
966 if (lseek (channel, offset, 0) == -1L
967 || read (channel, (char *) load_ave, sizeof (load_ave))
968 != sizeof (load_ave))
971 getloadavg_initialized = false;
974 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
975 != sizeof (load_ave))
978 getloadavg_initialized = false;
980 # endif /* SUNOS_5 */
983 if (offset == 0 || !getloadavg_initialized)
985 # endif /* ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS */
987 # if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
989 loadavg[elem++] = LDAV_CVT (load_ave[0]);
991 loadavg[elem++] = LDAV_CVT (load_ave[1]);
993 loadavg[elem++] = LDAV_CVT (load_ave[2]);
996 # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
998 # if !defined LDAV_DONE
999 /* Set errno to zero to indicate that there was no particular error;
1000 this function just can't work at all on this system. */
1007 #endif /* ! HAVE_GETLOADAVG */
1011 main (int argc, char **argv)
1016 naptime = atoi (argv[1]);
1023 errno = 0; /* Don't be misled if it doesn't set errno. */
1024 loads = getloadavg (avg, 3);
1027 perror ("Error getting load average");
1028 return EXIT_FAILURE;
1031 printf ("1-minute: %f ", avg[0]);
1033 printf ("5-minute: %f ", avg[1]);
1035 printf ("15-minute: %f ", avg[2]);
1044 return EXIT_SUCCESS;