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 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 2, or (at your option)
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, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 /* Compile-time symbols that this file uses:
27 HAVE_PSTAT_GETDYNAMIC Define this if your system has the
28 pstat_getdynamic function. I think it
29 is unique to HPUX9. The best way to get the
30 definition is through the AC_FUNC_GETLOADAVG
31 macro that comes with autoconf 2.13 or newer.
32 If that isn't an option, then just put
33 AC_CHECK_FUNCS(pstat_getdynamic) in your
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. */
88 # ifndef CONFIGURING_GETLOADAVG
93 #ifndef CONFIGURING_GETLOADAVG
101 /* Exclude all the code except the test program at the end
102 if the system has its own `getloadavg' function. */
104 #ifndef HAVE_GETLOADAVG
106 # include <sys/types.h>
108 /* Both the Emacs and non-Emacs sections want this. Some
109 configuration files' definitions for the LOAD_AVE_CVT macro (like
110 sparc.h's) use macros like FSCALE, defined here. */
111 # if defined (unix) || defined (__unix)
112 # include <sys/param.h>
115 # include "c-strtod.h"
116 # include "cloexec.h"
117 # include "intprops.h"
120 /* The existing Emacs configuration files define a macro called
121 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
122 returns the load average multiplied by 100. What we actually want
123 is a macro called LDAV_CVT, which returns the load average as an
126 For backwards compatibility, we'll define LDAV_CVT in terms of
127 LOAD_AVE_CVT, but future machine config files should just define
128 LDAV_CVT directly. */
130 # if !defined (LDAV_CVT) && defined (LOAD_AVE_CVT)
131 # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
134 # if !defined (BSD) && defined (ultrix)
135 /* Ultrix behaves like BSD on Vaxen. */
140 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
141 conflicts with the definition understood in this file, that this
145 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
146 defined to mean that the nlist method should be used, which is not true. */
150 /* Same issues as for NeXT apply to the HURD-based GNU system. */
154 # endif /* __GNU__ */
156 /* Set values that are different from the defaults, which are
157 set a little farther down with #ifndef. */
160 /* Some shorthands. */
162 # if defined (HPUX) && !defined (hpux)
166 # if defined (__hpux) && !defined (hpux)
170 # if defined (__sun) && !defined (sun)
174 # if defined (hp300) && !defined (hpux)
178 # if defined (ultrix) && defined (mips)
182 # if defined (__SVR4) && !defined (SVR4)
186 # if (defined (sun) && defined (SVR4)) || defined (SOLARIS2)
190 # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
192 # include <sys/mbuf.h>
193 # include <sys/socket.h>
194 # include <net/route.h>
195 # include <sys/table.h>
198 # if defined (__osf__) && (defined (mips) || defined (__mips__))
200 # include <sys/table.h>
203 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
204 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
205 that with a couple of other things and we'll have a unique match. */
206 # if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
207 # define tek4300 /* Define by emacs, but not by other users. */
211 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
212 # ifndef LOAD_AVE_TYPE
215 # define LOAD_AVE_TYPE long
219 # define LOAD_AVE_TYPE long
223 # define LOAD_AVE_TYPE long
227 # define LOAD_AVE_TYPE long
231 # define LOAD_AVE_TYPE long
235 # define LOAD_AVE_TYPE long
239 # define LOAD_AVE_TYPE long
243 # define LOAD_AVE_TYPE long
247 # define LOAD_AVE_TYPE long
250 # if defined (ardent) && defined (titan)
251 # define LOAD_AVE_TYPE long
255 # define LOAD_AVE_TYPE long
258 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
259 # define LOAD_AVE_TYPE long
263 # define LOAD_AVE_TYPE long
267 # define LOAD_AVE_TYPE double
269 # define LDAV_CVT(n) (n)
273 # endif /* No LOAD_AVE_TYPE. */
276 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
277 according to ghazi@noc.rutgers.edu. */
279 # define FSCALE 1024.0
282 # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
283 /* <sys/param.h> defines an incorrect value for FSCALE on an
284 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
286 # define FSCALE 100.0
292 /* SunOS and some others define FSCALE in sys/param.h. */
295 # define FSCALE 2048.0
298 # if defined (MIPS) || defined (SVR4) || defined (decstation)
302 # if defined (sgi) || defined (sequent)
303 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
304 above under #ifdef MIPS. But we want the sgi value. */
306 # define FSCALE 1000.0
309 # if defined (ardent) && defined (titan)
310 # define FSCALE 65536.0
314 # define FSCALE 100.0
318 # define FSCALE 65536.0
321 # endif /* Not FSCALE. */
323 # if !defined (LDAV_CVT) && defined (FSCALE)
324 # define LDAV_CVT(n) (((double) (n)) / FSCALE)
327 # ifndef NLIST_STRUCT
329 # define NLIST_STRUCT
333 # if defined (sgi) || (defined (mips) && !defined (BSD))
334 # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
338 # if !defined (KERNEL_FILE) && defined (sequent)
339 # define KERNEL_FILE "/dynix"
342 # if !defined (KERNEL_FILE) && defined (hpux)
343 # define KERNEL_FILE "/hp-ux"
346 # if !defined (KERNEL_FILE) && (defined (_SEQUENT_) || defined (MIPS) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
347 # define KERNEL_FILE "/unix"
351 # if !defined (LDAV_SYMBOL) && defined (alliant)
352 # define LDAV_SYMBOL "_Loadavg"
355 # if !defined (LDAV_SYMBOL) && ((defined (hpux) && !defined (hp9000s300)) || defined (_SEQUENT_) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
356 # define LDAV_SYMBOL "avenrun"
361 /* LOAD_AVE_TYPE should only get defined if we're going to use the
363 # if !defined (LOAD_AVE_TYPE) && (defined (BSD) || defined (LDAV_CVT) || defined (KERNEL_FILE) || defined (LDAV_SYMBOL))
364 # define LOAD_AVE_TYPE double
367 # ifdef LOAD_AVE_TYPE
371 # ifndef NLIST_STRUCT
373 # else /* NLIST_STRUCT */
375 # endif /* NLIST_STRUCT */
383 # if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
384 # include <sys/pstat.h>
388 # define KERNEL_FILE "/vmunix"
389 # endif /* KERNEL_FILE */
392 # define LDAV_SYMBOL "_avenrun"
393 # endif /* LDAV_SYMBOL */
394 # endif /* __linux__ */
400 # include <descrip.h>
402 # include <vms/iodef.h>
407 # define LDAV_CVT(n) ((double) (n))
408 # endif /* !LDAV_CVT */
410 # endif /* LOAD_AVE_TYPE */
412 # if defined (__GNU__) && !defined (NeXT)
413 /* Note that NeXT Openstep defines __GNU__ even though it should not. */
414 /* GNU system acts much like NeXT, for load average purposes,
417 # define host_self mach_host_self
421 # ifdef HAVE_MACH_MACH_H
422 # include <mach/mach.h>
429 # include <sys/sysmp.h>
434 # include <sys/time.h>
435 # include <sys/wait.h>
436 # include <sys/syscall.h>
439 # include <machine/cpu.h>
440 # include <inq_stats/statistics.h>
441 # include <inq_stats/sysstats.h>
442 # include <inq_stats/cpustats.h>
443 # include <inq_stats/procstats.h>
444 # else /* Not UMAX_43. */
445 # include <sys/sysdefs.h>
446 # include <sys/statistics.h>
447 # include <sys/sysstats.h>
448 # include <sys/cpudefs.h>
449 # include <sys/cpustats.h>
450 # include <sys/procstats.h>
451 # endif /* Not UMAX_43. */
455 # include <sys/dg_sys_info.h>
458 # include "fcntl--.h"
460 /* Avoid static vars inside a function since in HPUX they dump as pure. */
463 static processor_set_t default_set;
464 static bool getloadavg_initialized;
468 static unsigned int cpus = 0;
469 static unsigned int samples;
473 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
476 # if !defined (HAVE_LIBKSTAT) && defined (LOAD_AVE_TYPE)
477 /* File descriptor open to /dev/kmem or VMS load ave driver. */
479 /* True iff channel is valid. */
480 static bool getloadavg_initialized;
481 /* Offset in kmem to seek to read load average, or 0 means invalid. */
484 # if ! defined __VMS && ! defined sgi && ! defined __linux__
485 static struct nlist nl[2];
490 # endif /* SUNOS_5 */
492 # endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */
494 /* Put the 1 minute, 5 minute and 15 minute load averages
495 into the first NELEM elements of LOADAVG.
496 Return the number written (never more than 3, but may be less than NELEM),
497 or -1 if an error occurred. */
500 getloadavg (double loadavg[], int nelem)
502 int elem = 0; /* Return value. */
504 # ifdef NO_GET_LOAD_AVG
506 /* Set errno to zero to indicate that there was no particular error;
507 this function just can't work at all on this system. */
512 # if !defined (LDAV_DONE) && defined (HAVE_LIBKSTAT)
513 /* Use libkstat because we don't have to be root. */
522 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
525 if (kstat_read (kc, ksp, 0) == -1)
529 kn = kstat_data_lookup (ksp, "avenrun_1min");
532 /* Return -1 if no load average information is available. */
538 loadavg[elem++] = (double) kn->value.ul / FSCALE;
542 kn = kstat_data_lookup (ksp, "avenrun_5min");
545 loadavg[elem++] = (double) kn->value.ul / FSCALE;
549 kn = kstat_data_lookup (ksp, "avenrun_15min");
551 loadavg[elem++] = (double) kn->value.ul / FSCALE;
557 # endif /* HAVE_LIBKSTAT */
559 # if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
560 /* Use pstat_getdynamic() because we don't have to be root. */
562 # undef LOAD_AVE_TYPE
564 struct pst_dynamic dyn_info;
565 if (pstat_getdynamic (&dyn_info, sizeof (dyn_info), 0, 0) < 0)
568 loadavg[elem++] = dyn_info.psd_avg_1_min;
570 loadavg[elem++] = dyn_info.psd_avg_5_min;
572 loadavg[elem++] = dyn_info.psd_avg_15_min;
574 # endif /* hpux && HAVE_PSTAT_GETDYNAMIC */
576 # if !defined (LDAV_DONE) && (defined (__linux__) || defined (__CYGWIN__))
578 # undef LOAD_AVE_TYPE
580 # ifndef LINUX_LDAV_FILE
581 # define LINUX_LDAV_FILE "/proc/loadavg"
584 char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
585 char const *ptr = ldavgbuf;
588 fd = open (LINUX_LDAV_FILE, O_RDONLY);
591 count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
595 ldavgbuf[count] = '\0';
597 for (elem = 0; elem < nelem; elem++)
600 double d = c_strtod (ptr, &endptr);
613 # endif /* __linux__ || __CYGWIN__ */
615 # if !defined (LDAV_DONE) && defined (__NetBSD__)
617 # undef LOAD_AVE_TYPE
619 # ifndef NETBSD_LDAV_FILE
620 # define NETBSD_LDAV_FILE "/kern/loadavg"
623 unsigned long int load_ave[3], scale;
627 fp = fopen (NETBSD_LDAV_FILE, "r");
630 count = fscanf (fp, "%lu %lu %lu %lu\n",
631 &load_ave[0], &load_ave[1], &load_ave[2],
637 for (elem = 0; elem < nelem; elem++)
638 loadavg[elem] = (double) load_ave[elem] / (double) scale;
642 # endif /* __NetBSD__ */
644 # if !defined (LDAV_DONE) && defined (NeXT)
646 /* The NeXT code was adapted from iscreen 3.2. */
649 struct processor_set_basic_info info;
650 unsigned int info_count;
652 /* We only know how to get the 1-minute average for this system,
653 so even if the caller asks for more than 1, we only return 1. */
655 if (!getloadavg_initialized)
657 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
658 getloadavg_initialized = true;
661 if (getloadavg_initialized)
663 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
664 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
665 (processor_set_info_t) &info, &info_count)
667 getloadavg_initialized = false;
671 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
675 if (!getloadavg_initialized)
679 # if !defined (LDAV_DONE) && defined (UMAX)
681 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
682 have a /dev/kmem. Information about the workings of the running kernel
683 can be gathered with inq_stats system calls.
684 We only know how to get the 1-minute average for this system. */
686 struct proc_summary proc_sum_data;
687 struct stat_descr proc_info;
689 register unsigned int i, j;
693 register unsigned int c, i;
694 struct cpu_config conf;
695 struct stat_descr desc;
698 desc.sd_subsys = SUBSYS_CPU;
699 desc.sd_type = CPUTYPE_CONFIG;
700 desc.sd_addr = (char *) &conf;
701 desc.sd_size = sizeof conf;
703 if (inq_stats (1, &desc))
707 for (i = 0; i < conf.config_maxclass; ++i)
709 struct class_stats stats;
710 bzero ((char *) &stats, sizeof stats);
712 desc.sd_type = CPUTYPE_CLASS;
714 desc.sd_addr = (char *) &stats;
715 desc.sd_size = sizeof stats;
717 if (inq_stats (1, &desc))
720 c += stats.class_numcpus;
723 samples = cpus < 2 ? 3 : (2 * cpus / 3);
726 proc_info.sd_next = 0;
727 proc_info.sd_subsys = SUBSYS_PROC;
728 proc_info.sd_type = PROCTYPE_SUMMARY;
729 proc_info.sd_addr = (char *) &proc_sum_data;
730 proc_info.sd_size = sizeof (struct proc_summary);
731 proc_info.sd_sizeused = 0;
733 if (inq_stats (1, &proc_info) != 0)
736 load = proc_sum_data.ps_nrunnable;
738 for (i = samples - 1; i > 0; --i)
740 load += proc_sum_data.ps_nrun[j];
741 if (j++ == PS_NRUNSIZE)
746 loadavg[elem++] = load / samples / cpus;
749 # if !defined (LDAV_DONE) && defined (DGUX)
751 /* This call can return -1 for an error, but with good args
752 it's not supposed to fail. The first argument is for no
753 apparent reason of type `long int *'. */
754 dg_sys_info ((long int *) &load_info,
755 DG_SYS_INFO_LOAD_INFO_TYPE,
756 DG_SYS_INFO_LOAD_VERSION_0);
759 loadavg[elem++] = load_info.one_minute;
761 loadavg[elem++] = load_info.five_minute;
763 loadavg[elem++] = load_info.fifteen_minute;
766 # if !defined (LDAV_DONE) && defined (apollo)
768 /* Apollo code from lisch@mentorg.com (Ray Lischner).
770 This system call is not documented. The load average is obtained as
771 three long integers, for the load average over the past minute,
772 five minutes, and fifteen minutes. Each value is a scaled integer,
773 with 16 bits of integer part and 16 bits of fraction part.
775 I'm not sure which operating system first supported this system call,
776 but I know that SR10.2 supports it. */
778 extern void proc1_$get_loadav ();
779 unsigned long load_ave[3];
781 proc1_$get_loadav (load_ave);
784 loadavg[elem++] = load_ave[0] / 65536.0;
786 loadavg[elem++] = load_ave[1] / 65536.0;
788 loadavg[elem++] = load_ave[2] / 65536.0;
791 # if !defined (LDAV_DONE) && defined (OSF_MIPS)
794 struct tbl_loadavg load_ave;
795 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
797 = (load_ave.tl_lscale == 0
798 ? load_ave.tl_avenrun.d[0]
799 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
800 # endif /* OSF_MIPS */
802 # if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
805 /* A faithful emulation is going to have to be saved for a rainy day. */
806 for ( ; elem < nelem; elem++)
810 # endif /* __MSDOS__ || WINDOWS32 */
812 # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
815 struct tbl_loadavg load_ave;
816 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
817 for (elem = 0; elem < nelem; elem++)
819 = (load_ave.tl_lscale == 0
820 ? load_ave.tl_avenrun.d[elem]
821 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
822 # endif /* OSF_ALPHA */
824 # if ! defined LDAV_DONE && defined __VMS
825 /* VMS specific code -- read from the Load Ave driver. */
827 LOAD_AVE_TYPE load_ave[3];
828 static bool getloadavg_initialized;
837 /* Ensure that there is a channel open to the load ave device. */
838 if (!getloadavg_initialized)
840 /* Attempt to open the channel. */
842 descriptor.dsc$w_length = 18;
843 descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
845 $DESCRIPTOR (descriptor, "LAV0:");
847 if (sys$assign (&descriptor, &channel, 0, 0) & 1)
848 getloadavg_initialized = true;
851 /* Read the load average vector. */
852 if (getloadavg_initialized
853 && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
854 load_ave, 12, 0, 0, 0, 0) & 1))
856 sys$dassgn (channel);
857 getloadavg_initialized = false;
860 if (!getloadavg_initialized)
862 # endif /* ! defined LDAV_DONE && defined __VMS */
864 # if ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS
866 /* UNIX-specific code -- read the average from /dev/kmem. */
868 # define LDAV_PRIVILEGED /* This code requires special installation. */
870 LOAD_AVE_TYPE load_ave[3];
872 /* Get the address of LDAV_SYMBOL. */
876 # ifndef NLIST_STRUCT
877 strcpy (nl[0].n_name, LDAV_SYMBOL);
878 strcpy (nl[1].n_name, "");
879 # else /* NLIST_STRUCT */
880 # ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
881 nl[0].n_un.n_name = LDAV_SYMBOL;
882 nl[1].n_un.n_name = 0;
883 # else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
884 nl[0].n_name = LDAV_SYMBOL;
886 # endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
887 # endif /* NLIST_STRUCT */
891 # if !(defined (_AIX) && !defined (ps2))
892 nlist (KERNEL_FILE, nl)
894 knlist (nl, 1, sizeof (nl[0]))
897 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
899 # ifdef FIXUP_KERNEL_SYMBOL_ADDR
900 FIXUP_KERNEL_SYMBOL_ADDR (nl);
902 offset = nl[0].n_value;
904 # endif /* !SUNOS_5 */
908 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
910 offset = (long int) ldav_off & 0x7fffffff;
914 /* Make sure we have /dev/kmem open. */
915 if (!getloadavg_initialized)
918 channel = open ("/dev/kmem", O_RDONLY);
921 /* Set the channel to close on exec, so it does not
922 litter any child's descriptor table. */
923 set_cloexec_flag (channel, true);
924 getloadavg_initialized = true;
927 /* We pass 0 for the kernel, corefile, and swapfile names
928 to use the currently running kernel. */
929 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
932 /* nlist the currently running kernel. */
934 offset = nl[0].n_value;
935 getloadavg_initialized = true;
937 # endif /* SUNOS_5 */
940 /* If we can, get the load average values. */
941 if (offset && getloadavg_initialized)
943 /* Try to read the load. */
945 if (lseek (channel, offset, 0) == -1L
946 || read (channel, (char *) load_ave, sizeof (load_ave))
947 != sizeof (load_ave))
950 getloadavg_initialized = false;
953 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
954 != sizeof (load_ave))
957 getloadavg_initialized = false;
959 # endif /* SUNOS_5 */
962 if (offset == 0 || !getloadavg_initialized)
964 # endif /* ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS */
966 # if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
968 loadavg[elem++] = LDAV_CVT (load_ave[0]);
970 loadavg[elem++] = LDAV_CVT (load_ave[1]);
972 loadavg[elem++] = LDAV_CVT (load_ave[2]);
975 # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
977 # if !defined LDAV_DONE
978 /* Set errno to zero to indicate that there was no particular error;
979 this function just can't work at all on this system. */
986 #endif /* ! HAVE_GETLOADAVG */
990 main (int argc, char **argv)
995 naptime = atoi (argv[1]);
1002 errno = 0; /* Don't be misled if it doesn't set errno. */
1003 loads = getloadavg (avg, 3);
1006 perror ("Error getting load average");
1007 return EXIT_FAILURE;
1010 printf ("1-minute: %f ", avg[0]);
1012 printf ("5-minute: %f ", avg[1]);
1014 printf ("15-minute: %f ", avg[2]);
1023 return EXIT_SUCCESS;