1 /* Get the system load averages.
3 Copyright (C) 1985-1989, 1991-1995, 1997, 1999-2000, 2003-2011 Free Software
6 NOTE: The canonical source of this file is maintained with gnulib.
7 Bugs can be reported to bug-gnulib@gnu.org.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Compile-time symbols that this file uses:
24 HAVE_PSTAT_GETDYNAMIC Define this if your system has the
25 pstat_getdynamic function. I think it
26 is unique to HPUX9. The best way to get the
27 definition is through the AC_FUNC_GETLOADAVG
28 macro that comes with autoconf 2.13 or newer.
29 If that isn't an option, then just put
30 AC_CHECK_FUNCS(pstat_getdynamic) in your
32 HAVE_LIBPERFSTAT Define this if your system has the
33 perfstat_cpu_total function in libperfstat (AIX).
34 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
35 KERNEL_FILE Name of the kernel file to nlist.
36 LDAV_CVT() Scale the load average from the kernel.
38 LDAV_SYMBOL Name of kernel symbol giving load average.
39 LOAD_AVE_TYPE Type of the load average array in the kernel.
40 Must be defined unless one of
41 apollo, DGUX, NeXT, or UMAX is defined;
43 otherwise, no load average is available.
44 HAVE_NLIST_H nlist.h is available. NLIST_STRUCT defaults
46 NLIST_STRUCT Include nlist.h, not a.out.h.
47 N_NAME_POINTER The nlist n_name element is a pointer,
49 HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'.
50 LINUX_LDAV_FILE [__linux__, __CYGWIN__]: File containing
53 Specific system predefines this file uses, aside from setting
54 default values if not emacs:
57 BSD Real BSD, not just BSD-like.
60 eunice UNIX emulator under VMS.
62 __MSDOS__ No-op for MSDOS.
65 sequent Sequent Dynix 3.x.x (BSD)
66 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
67 sony_news NEWS-OS (works at least for 4.1C)
71 WINDOWS32 No-op for Windows95/NT.
72 __linux__ Linux: assumes /proc file system mounted.
73 Support from Michael K. Johnson.
74 __CYGWIN__ Cygwin emulates linux /proc/loadavg.
75 __NetBSD__ NetBSD: assumes /kern file system mounted.
77 In addition, to avoid nesting many #ifdefs, we internally set
78 LDAV_DONE to indicate that the load average has been computed.
80 We also #define LDAV_PRIVILEGED if a program will require
81 special installation to be able to call getloadavg. */
83 /* "configure" defines CONFIGURING_GETLOADAVG to sidestep problems
84 with partially-configured source directories. */
86 #ifndef CONFIGURING_GETLOADAVG
97 /* Exclude all the code except the test program at the end
98 if the system has its own `getloadavg' function. */
100 #ifndef HAVE_GETLOADAVG
102 # include <sys/types.h>
104 /* Both the Emacs and non-Emacs sections want this. Some
105 configuration files' definitions for the LOAD_AVE_CVT macro (like
106 sparc.h's) use macros like FSCALE, defined here. */
107 # if defined (unix) || defined (__unix)
108 # include <sys/param.h>
111 # include "intprops.h"
113 /* The existing Emacs configuration files define a macro called
114 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
115 returns the load average multiplied by 100. What we actually want
116 is a macro called LDAV_CVT, which returns the load average as an
119 For backwards compatibility, we'll define LDAV_CVT in terms of
120 LOAD_AVE_CVT, but future machine config files should just define
121 LDAV_CVT directly. */
123 # if !defined (LDAV_CVT) && defined (LOAD_AVE_CVT)
124 # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
127 # if !defined (BSD) && defined (ultrix)
128 /* Ultrix behaves like BSD on Vaxen. */
133 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
134 conflicts with the definition understood in this file, that this
138 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
139 defined to mean that the nlist method should be used, which is not true. */
143 /* Same issues as for NeXT apply to the HURD-based GNU system. */
147 # endif /* __GNU__ */
149 /* Set values that are different from the defaults, which are
150 set a little farther down with #ifndef. */
153 /* Some shorthands. */
155 # if defined (HPUX) && !defined (hpux)
159 # if defined (__hpux) && !defined (hpux)
163 # if defined (__sun) && !defined (sun)
167 # if defined (hp300) && !defined (hpux)
171 # if defined (ultrix) && defined (mips)
175 # if defined (__SVR4) && !defined (SVR4)
179 # if (defined (sun) && defined (SVR4)) || defined (SOLARIS2)
183 # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
185 # include <sys/mbuf.h>
186 # include <sys/socket.h>
187 # include <net/route.h>
188 # include <sys/table.h>
189 /* Tru64 4.0D's table.h redefines sys */
193 # if defined (__osf__) && (defined (mips) || defined (__mips__))
195 # include <sys/table.h>
198 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
199 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
200 that with a couple of other things and we'll have a unique match. */
201 # if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
202 # define tek4300 /* Define by emacs, but not by other users. */
206 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
207 # ifndef LOAD_AVE_TYPE
210 # define LOAD_AVE_TYPE long
214 # define LOAD_AVE_TYPE long
218 # define LOAD_AVE_TYPE long
222 # define LOAD_AVE_TYPE long
226 # define LOAD_AVE_TYPE long
230 # define LOAD_AVE_TYPE long
234 # define LOAD_AVE_TYPE long
238 # define LOAD_AVE_TYPE long
242 # define LOAD_AVE_TYPE long
245 # if defined (ardent) && defined (titan)
246 # define LOAD_AVE_TYPE long
250 # define LOAD_AVE_TYPE long
253 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
254 # define LOAD_AVE_TYPE long
257 # if defined _AIX && ! defined HAVE_LIBPERFSTAT
258 # define LOAD_AVE_TYPE long
262 # define LOAD_AVE_TYPE double
264 # define LDAV_CVT(n) (n)
268 # endif /* No LOAD_AVE_TYPE. */
271 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
272 according to ghazi@noc.rutgers.edu. */
274 # define FSCALE 1024.0
277 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
278 /* <sys/param.h> defines an incorrect value for FSCALE on an
279 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
281 # define FSCALE 100.0
287 /* SunOS and some others define FSCALE in sys/param.h. */
290 # define FSCALE 2048.0
293 # if defined (MIPS) || defined (SVR4) || defined (decstation)
297 # if defined (sgi) || defined (sequent)
298 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
299 above under #ifdef MIPS. But we want the sgi value. */
301 # define FSCALE 1000.0
304 # if defined (ardent) && defined (titan)
305 # define FSCALE 65536.0
309 # define FSCALE 100.0
312 # if defined _AIX && !defined HAVE_LIBPERFSTAT
313 # define FSCALE 65536.0
316 # endif /* Not FSCALE. */
318 # if !defined (LDAV_CVT) && defined (FSCALE)
319 # define LDAV_CVT(n) (((double) (n)) / FSCALE)
322 # ifndef NLIST_STRUCT
324 # define NLIST_STRUCT
328 # if defined (sgi) || (defined (mips) && !defined (BSD))
329 # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
333 # if !defined (KERNEL_FILE) && defined (sequent)
334 # define KERNEL_FILE "/dynix"
337 # if !defined (KERNEL_FILE) && defined (hpux)
338 # define KERNEL_FILE "/hp-ux"
341 # if !defined (KERNEL_FILE) && (defined (_SEQUENT_) || defined (MIPS) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
342 # define KERNEL_FILE "/unix"
346 # if !defined (LDAV_SYMBOL) && defined (alliant)
347 # define LDAV_SYMBOL "_Loadavg"
350 # 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)))
351 # define LDAV_SYMBOL "avenrun"
356 /* LOAD_AVE_TYPE should only get defined if we're going to use the
358 # if !defined (LOAD_AVE_TYPE) && (defined (BSD) || defined (LDAV_CVT) || defined (KERNEL_FILE) || defined (LDAV_SYMBOL))
359 # define LOAD_AVE_TYPE double
362 # ifdef LOAD_AVE_TYPE
366 # ifndef NLIST_STRUCT
368 # else /* NLIST_STRUCT */
370 # endif /* NLIST_STRUCT */
377 # if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
378 # include <sys/pstat.h>
382 # define KERNEL_FILE "/vmunix"
383 # endif /* KERNEL_FILE */
386 # define LDAV_SYMBOL "_avenrun"
387 # endif /* LDAV_SYMBOL */
388 # endif /* __linux__ */
394 # include <descrip.h>
396 # include <vms/iodef.h>
401 # define LDAV_CVT(n) ((double) (n))
402 # endif /* !LDAV_CVT */
404 # endif /* LOAD_AVE_TYPE */
406 # if defined HAVE_LIBPERFSTAT
407 # include <sys/protosw.h>
408 # include <libperfstat.h>
409 # include <sys/proc.h>
415 # if defined (__GNU__) && !defined (NeXT)
416 /* Note that NeXT Openstep defines __GNU__ even though it should not. */
417 /* GNU system acts much like NeXT, for load average purposes,
420 # define host_self mach_host_self
424 # ifdef HAVE_MACH_MACH_H
425 # include <mach/mach.h>
432 # include <sys/sysmp.h>
437 # include <sys/time.h>
438 # include <sys/wait.h>
439 # include <sys/syscall.h>
442 # include <machine/cpu.h>
443 # include <inq_stats/statistics.h>
444 # include <inq_stats/sysstats.h>
445 # include <inq_stats/cpustats.h>
446 # include <inq_stats/procstats.h>
447 # else /* Not UMAX_43. */
448 # include <sys/sysdefs.h>
449 # include <sys/statistics.h>
450 # include <sys/sysstats.h>
451 # include <sys/cpudefs.h>
452 # include <sys/cpustats.h>
453 # include <sys/procstats.h>
454 # endif /* Not UMAX_43. */
458 # include <sys/dg_sys_info.h>
461 # if (defined __linux__ || defined __CYGWIN__ || defined SUNOS_5 \
462 || (defined LOAD_AVE_TYPE && ! defined __VMS))
466 /* Avoid static vars inside a function since in HPUX they dump as pure. */
469 static processor_set_t default_set;
470 static bool getloadavg_initialized;
474 static unsigned int cpus = 0;
475 static unsigned int samples;
479 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
482 # if !defined (HAVE_LIBKSTAT) && defined (LOAD_AVE_TYPE)
483 /* File descriptor open to /dev/kmem or VMS load ave driver. */
485 /* True if channel is valid. */
486 static bool getloadavg_initialized;
487 /* Offset in kmem to seek to read load average, or 0 means invalid. */
490 # if ! defined __VMS && ! defined sgi && ! defined __linux__
491 static struct nlist name_list[2];
496 # endif /* SUNOS_5 */
498 # endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */
500 /* Put the 1 minute, 5 minute and 15 minute load averages
501 into the first NELEM elements of LOADAVG.
502 Return the number written (never more than 3, but may be less than NELEM),
503 or -1 if an error occurred. */
506 getloadavg (double loadavg[], int nelem)
508 int elem = 0; /* Return value. */
510 # ifdef NO_GET_LOAD_AVG
512 /* Set errno to zero to indicate that there was no particular error;
513 this function just can't work at all on this system. */
518 # if !defined (LDAV_DONE) && defined (HAVE_LIBKSTAT)
519 /* Use libkstat because we don't have to be root. */
528 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
531 if (kstat_read (kc, ksp, 0) == -1)
535 kn = kstat_data_lookup (ksp, "avenrun_1min");
538 /* Return -1 if no load average information is available. */
544 loadavg[elem++] = (double) kn->value.ul / FSCALE;
548 kn = kstat_data_lookup (ksp, "avenrun_5min");
551 loadavg[elem++] = (double) kn->value.ul / FSCALE;
555 kn = kstat_data_lookup (ksp, "avenrun_15min");
557 loadavg[elem++] = (double) kn->value.ul / FSCALE;
563 # endif /* HAVE_LIBKSTAT */
565 # if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
566 /* Use pstat_getdynamic() because we don't have to be root. */
568 # undef LOAD_AVE_TYPE
570 struct pst_dynamic dyn_info;
571 if (pstat_getdynamic (&dyn_info, sizeof (dyn_info), 0, 0) < 0)
574 loadavg[elem++] = dyn_info.psd_avg_1_min;
576 loadavg[elem++] = dyn_info.psd_avg_5_min;
578 loadavg[elem++] = dyn_info.psd_avg_15_min;
580 # endif /* hpux && HAVE_PSTAT_GETDYNAMIC */
582 # if ! defined LDAV_DONE && defined HAVE_LIBPERFSTAT
584 # undef LOAD_AVE_TYPE
585 /* Use perfstat_cpu_total because we don't have to be root. */
587 perfstat_cpu_total_t cpu_stats;
588 int result = perfstat_cpu_total (NULL, &cpu_stats, sizeof cpu_stats, 1);
591 loadavg[0] = cpu_stats.loadavg[0] / (double)(1 << SBITS);
592 loadavg[1] = cpu_stats.loadavg[1] / (double)(1 << SBITS);
593 loadavg[2] = cpu_stats.loadavg[2] / (double)(1 << SBITS);
598 # if !defined (LDAV_DONE) && (defined (__linux__) || defined (__CYGWIN__))
600 # undef LOAD_AVE_TYPE
602 # ifndef LINUX_LDAV_FILE
603 # define LINUX_LDAV_FILE "/proc/loadavg"
606 char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
607 char const *ptr = ldavgbuf;
610 fd = open (LINUX_LDAV_FILE, O_RDONLY);
613 count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
617 ldavgbuf[count] = '\0';
619 for (elem = 0; elem < nelem; elem++)
621 double numerator = 0;
622 double denominator = 1;
627 /* Finish if this number is missing, and report an error if all
629 if (! ('0' <= *ptr && *ptr <= '9'))
636 while ('0' <= *ptr && *ptr <= '9')
637 numerator = 10 * numerator + (*ptr++ - '0');
640 for (ptr++; '0' <= *ptr && *ptr <= '9'; ptr++)
641 numerator = 10 * numerator + (*ptr - '0'), denominator *= 10;
643 loadavg[elem++] = numerator / denominator;
648 # endif /* __linux__ || __CYGWIN__ */
650 # if !defined (LDAV_DONE) && defined (__NetBSD__)
652 # undef LOAD_AVE_TYPE
654 # ifndef NETBSD_LDAV_FILE
655 # define NETBSD_LDAV_FILE "/kern/loadavg"
658 unsigned long int load_ave[3], scale;
662 fp = fopen (NETBSD_LDAV_FILE, "r");
665 count = fscanf (fp, "%lu %lu %lu %lu\n",
666 &load_ave[0], &load_ave[1], &load_ave[2],
672 for (elem = 0; elem < nelem; elem++)
673 loadavg[elem] = (double) load_ave[elem] / (double) scale;
677 # endif /* __NetBSD__ */
679 # if !defined (LDAV_DONE) && defined (NeXT)
681 /* The NeXT code was adapted from iscreen 3.2. */
684 struct processor_set_basic_info info;
685 unsigned int info_count;
687 /* We only know how to get the 1-minute average for this system,
688 so even if the caller asks for more than 1, we only return 1. */
690 if (!getloadavg_initialized)
692 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
693 getloadavg_initialized = true;
696 if (getloadavg_initialized)
698 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
699 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
700 (processor_set_info_t) &info, &info_count)
702 getloadavg_initialized = false;
706 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
710 if (!getloadavg_initialized)
714 # if !defined (LDAV_DONE) && defined (UMAX)
716 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
717 have a /dev/kmem. Information about the workings of the running kernel
718 can be gathered with inq_stats system calls.
719 We only know how to get the 1-minute average for this system. */
721 struct proc_summary proc_sum_data;
722 struct stat_descr proc_info;
724 register unsigned int i, j;
728 register unsigned int c, i;
729 struct cpu_config conf;
730 struct stat_descr desc;
733 desc.sd_subsys = SUBSYS_CPU;
734 desc.sd_type = CPUTYPE_CONFIG;
735 desc.sd_addr = (char *) &conf;
736 desc.sd_size = sizeof conf;
738 if (inq_stats (1, &desc))
742 for (i = 0; i < conf.config_maxclass; ++i)
744 struct class_stats stats;
745 memset (&stats, 0, sizeof stats);
747 desc.sd_type = CPUTYPE_CLASS;
749 desc.sd_addr = (char *) &stats;
750 desc.sd_size = sizeof stats;
752 if (inq_stats (1, &desc))
755 c += stats.class_numcpus;
758 samples = cpus < 2 ? 3 : (2 * cpus / 3);
761 proc_info.sd_next = 0;
762 proc_info.sd_subsys = SUBSYS_PROC;
763 proc_info.sd_type = PROCTYPE_SUMMARY;
764 proc_info.sd_addr = (char *) &proc_sum_data;
765 proc_info.sd_size = sizeof (struct proc_summary);
766 proc_info.sd_sizeused = 0;
768 if (inq_stats (1, &proc_info) != 0)
771 load = proc_sum_data.ps_nrunnable;
773 for (i = samples - 1; i > 0; --i)
775 load += proc_sum_data.ps_nrun[j];
776 if (j++ == PS_NRUNSIZE)
781 loadavg[elem++] = load / samples / cpus;
784 # if !defined (LDAV_DONE) && defined (DGUX)
786 /* This call can return -1 for an error, but with good args
787 it's not supposed to fail. The first argument is for no
788 apparent reason of type `long int *'. */
789 dg_sys_info ((long int *) &load_info,
790 DG_SYS_INFO_LOAD_INFO_TYPE,
791 DG_SYS_INFO_LOAD_VERSION_0);
794 loadavg[elem++] = load_info.one_minute;
796 loadavg[elem++] = load_info.five_minute;
798 loadavg[elem++] = load_info.fifteen_minute;
801 # if !defined (LDAV_DONE) && defined (apollo)
803 /* Apollo code from lisch@mentorg.com (Ray Lischner).
805 This system call is not documented. The load average is obtained as
806 three long integers, for the load average over the past minute,
807 five minutes, and fifteen minutes. Each value is a scaled integer,
808 with 16 bits of integer part and 16 bits of fraction part.
810 I'm not sure which operating system first supported this system call,
811 but I know that SR10.2 supports it. */
813 extern void proc1_$get_loadav ();
814 unsigned long load_ave[3];
816 proc1_$get_loadav (load_ave);
819 loadavg[elem++] = load_ave[0] / 65536.0;
821 loadavg[elem++] = load_ave[1] / 65536.0;
823 loadavg[elem++] = load_ave[2] / 65536.0;
826 # if !defined (LDAV_DONE) && defined (OSF_MIPS)
829 struct tbl_loadavg load_ave;
830 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
832 = (load_ave.tl_lscale == 0
833 ? load_ave.tl_avenrun.d[0]
834 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
835 # endif /* OSF_MIPS */
837 # if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
840 /* A faithful emulation is going to have to be saved for a rainy day. */
841 for ( ; elem < nelem; elem++)
845 # endif /* __MSDOS__ || WINDOWS32 */
847 # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
850 struct tbl_loadavg load_ave;
851 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
852 for (elem = 0; elem < nelem; elem++)
854 = (load_ave.tl_lscale == 0
855 ? load_ave.tl_avenrun.d[elem]
856 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
857 # endif /* OSF_ALPHA */
859 # if ! defined LDAV_DONE && defined __VMS
860 /* VMS specific code -- read from the Load Ave driver. */
862 LOAD_AVE_TYPE load_ave[3];
863 static bool getloadavg_initialized;
872 /* Ensure that there is a channel open to the load ave device. */
873 if (!getloadavg_initialized)
875 /* Attempt to open the channel. */
877 descriptor.dsc$w_length = 18;
878 descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
880 $DESCRIPTOR (descriptor, "LAV0:");
882 if (sys$assign (&descriptor, &channel, 0, 0) & 1)
883 getloadavg_initialized = true;
886 /* Read the load average vector. */
887 if (getloadavg_initialized
888 && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
889 load_ave, 12, 0, 0, 0, 0) & 1))
891 sys$dassgn (channel);
892 getloadavg_initialized = false;
895 if (!getloadavg_initialized)
897 # endif /* ! defined LDAV_DONE && defined __VMS */
899 # if ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS
901 /* UNIX-specific code -- read the average from /dev/kmem. */
903 # define LDAV_PRIVILEGED /* This code requires special installation. */
905 LOAD_AVE_TYPE load_ave[3];
907 /* Get the address of LDAV_SYMBOL. */
911 # if ! defined NLIST_STRUCT || ! defined N_NAME_POINTER
912 strcpy (name_list[0].n_name, LDAV_SYMBOL);
913 strcpy (name_list[1].n_name, "");
914 # else /* NLIST_STRUCT */
915 # ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
916 name_list[0].n_un.n_name = LDAV_SYMBOL;
917 name_list[1].n_un.n_name = 0;
918 # else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
919 name_list[0].n_name = LDAV_SYMBOL;
920 name_list[1].n_name = 0;
921 # endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
922 # endif /* NLIST_STRUCT */
926 # if !(defined (_AIX) && !defined (ps2))
927 nlist (KERNEL_FILE, name_list)
929 knlist (name_list, 1, sizeof (name_list[0]))
932 /* Omit "&& name_list[0].n_type != 0 " -- it breaks on Sun386i. */
934 # ifdef FIXUP_KERNEL_SYMBOL_ADDR
935 FIXUP_KERNEL_SYMBOL_ADDR (name_list);
937 offset = name_list[0].n_value;
939 # endif /* !SUNOS_5 */
943 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
945 offset = (long int) ldav_off & 0x7fffffff;
949 /* Make sure we have /dev/kmem open. */
950 if (!getloadavg_initialized)
953 /* Set the channel to close on exec, so it does not
954 litter any child's descriptor table. */
958 int fd = open ("/dev/kmem", O_RDONLY | O_CLOEXEC);
962 if (fd <= STDERR_FILENO)
964 int fd1 = fcntl (fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
972 getloadavg_initialized = true;
976 /* We pass 0 for the kernel, corefile, and swapfile names
977 to use the currently running kernel. */
978 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
981 /* nlist the currently running kernel. */
982 kvm_nlist (kd, name_list);
983 offset = name_list[0].n_value;
984 getloadavg_initialized = true;
986 # endif /* SUNOS_5 */
989 /* If we can, get the load average values. */
990 if (offset && getloadavg_initialized)
992 /* Try to read the load. */
994 if (lseek (channel, offset, 0) == -1L
995 || read (channel, (char *) load_ave, sizeof (load_ave))
996 != sizeof (load_ave))
999 getloadavg_initialized = false;
1001 # else /* SUNOS_5 */
1002 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
1003 != sizeof (load_ave))
1006 getloadavg_initialized = false;
1008 # endif /* SUNOS_5 */
1011 if (offset == 0 || !getloadavg_initialized)
1013 # endif /* ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS */
1015 # if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
1017 loadavg[elem++] = LDAV_CVT (load_ave[0]);
1019 loadavg[elem++] = LDAV_CVT (load_ave[1]);
1021 loadavg[elem++] = LDAV_CVT (load_ave[2]);
1024 # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
1026 # if !defined LDAV_DONE
1027 /* Set errno to zero to indicate that there was no particular error;
1028 this function just can't work at all on this system. */
1035 #endif /* ! HAVE_GETLOADAVG */
1039 main (int argc, char **argv)
1044 naptime = atoi (argv[1]);
1051 errno = 0; /* Don't be misled if it doesn't set errno. */
1052 loads = getloadavg (avg, 3);
1055 perror ("Error getting load average");
1056 return EXIT_FAILURE;
1059 printf ("1-minute: %f ", avg[0]);
1061 printf ("5-minute: %f ", avg[1]);
1063 printf ("15-minute: %f ", avg[2]);
1072 return EXIT_SUCCESS;