1 /* Get the system load averages.
2 Copyright (C) 1985-2012 Free Software Foundation, Inc.
4 GNU Make is free software; you can redistribute it and/or modify it under the
5 terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 3 of the License, or (at your option) any later
9 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 this program. If not, see <http://www.gnu.org/licenses/>. */
16 /* Compile-time symbols that this file uses:
18 HAVE_PSTAT_GETDYNAMIC Define this if your system has the
19 pstat_getdynamic function. I think it
20 is unique to HPUX9. The best way to get the
21 definition is through the AC_FUNC_GETLOADAVG
22 macro that comes with autoconf 2.13 or newer.
23 If that isn't an option, then just put
24 AC_CHECK_FUNCS(pstat_getdynamic) in your
26 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
27 KERNEL_FILE Pathname of the kernel to nlist.
28 LDAV_CVT() Scale the load average from the kernel.
30 LDAV_SYMBOL Name of kernel symbol giving load average.
31 LOAD_AVE_TYPE Type of the load average array in the kernel.
32 Must be defined unless one of
33 apollo, DGUX, NeXT, or UMAX is defined;
35 otherwise, no load average is available.
36 NLIST_STRUCT Include nlist.h, not a.out.h, and
37 the nlist n_name element is a pointer,
39 HAVE_STRUCT_NLIST_N_UN_N_NAME struct nlist has an n_un member, not n_name.
40 LINUX_LDAV_FILE [__linux__]: File containing load averages.
42 Specific system predefines this file uses, aside from setting
43 default values if not emacs:
46 BSD Real BSD, not just BSD-like.
49 eunice UNIX emulator under VMS.
51 __MSDOS__ No-op for MSDOS.
54 sequent Sequent Dynix 3.x.x (BSD)
55 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
56 sony_news NEWS-OS (works at least for 4.1C)
60 WINDOWS32 No-op for Windows95/NT.
61 __linux__ Linux: assumes /proc filesystem mounted.
62 Support from Michael K. Johnson.
63 __NetBSD__ NetBSD: assumes /kern filesystem mounted.
65 In addition, to avoid nesting many #ifdefs, we internally set
66 LDAV_DONE to indicate that the load average has been computed.
68 We also #define LDAV_PRIVILEGED if a program will require
69 special installation to be able to call getloadavg. */
71 /* This should always be first. */
76 #include <sys/types.h>
78 /* Both the Emacs and non-Emacs sections want this. Some
79 configuration files' definitions for the LOAD_AVE_CVT macro (like
80 sparc.h's) use macros like FSCALE, defined here. */
81 #if defined (unix) || defined (__unix)
82 # include <sys/param.h>
86 /* Exclude all the code except the test program at the end
87 if the system has its own 'getloadavg' function.
89 The declaration of 'errno' is needed by the test program
90 as well as the function itself, so it comes first. */
102 # define setlocale(Category, Locale) /* empty */
105 #ifndef HAVE_GETLOADAVG
108 /* The existing Emacs configuration files define a macro called
109 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
110 returns the load average multiplied by 100. What we actually want
111 is a macro called LDAV_CVT, which returns the load average as an
114 For backwards compatibility, we'll define LDAV_CVT in terms of
115 LOAD_AVE_CVT, but future machine config files should just define
116 LDAV_CVT directly. */
118 # if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
119 # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
122 # if !defined (BSD) && defined (ultrix)
123 /* Ultrix behaves like BSD on Vaxen. */
128 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
129 conflicts with the definition understood in this file, that this
133 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
134 defined to mean that the nlist method should be used, which is not true. */
138 /* Same issues as for NeXT apply to the HURD-based GNU system. */
142 # endif /* __GNU__ */
144 /* Set values that are different from the defaults, which are
145 set a little farther down with #ifndef. */
148 /* Some shorthands. */
150 # if defined (HPUX) && !defined (hpux)
154 # if defined (__hpux) && !defined (hpux)
158 # if defined (__sun) && !defined (sun)
162 # if defined(hp300) && !defined(hpux)
166 # if defined(ultrix) && defined(mips)
170 # if defined (__SVR4) && !defined (SVR4)
174 # if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
178 # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
180 # include <sys/mbuf.h>
181 # include <sys/socket.h>
182 # include <net/route.h>
183 # include <sys/table.h>
186 # if defined (__osf__) && (defined (mips) || defined (__mips__))
188 # include <sys/table.h>
191 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
192 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
193 that with a couple of other things and we'll have a unique match. */
194 # if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
195 # define tek4300 /* Define by emacs, but not by other users. */
198 /* AC_FUNC_GETLOADAVG thinks QNX is SVR4, but it isn't. */
199 # if defined(__QNX__)
203 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
204 # ifndef LOAD_AVE_TYPE
207 # define LOAD_AVE_TYPE long
211 # define LOAD_AVE_TYPE long
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
242 # if defined (ardent) && defined (titan)
243 # define LOAD_AVE_TYPE long
247 # define LOAD_AVE_TYPE long
250 # if defined(alliant) && defined(i860) /* Alliant FX/2800 */
251 # define LOAD_AVE_TYPE long
255 # define LOAD_AVE_TYPE long
259 # define LOAD_AVE_TYPE double
261 # define LDAV_CVT(n) (n)
265 # endif /* No LOAD_AVE_TYPE. */
268 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
269 according to ghazi@noc.rutgers.edu. */
271 # define FSCALE 1024.0
274 # if defined(alliant) && defined(i860) /* Alliant FX/2800 */
275 /* <sys/param.h> defines an incorrect value for FSCALE on an
276 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
278 # define FSCALE 100.0
284 /* SunOS and some others define FSCALE in sys/param.h. */
287 # define FSCALE 2048.0
290 # if defined(MIPS) || defined(SVR4) || defined(decstation)
294 # if defined (sgi) || defined (sequent)
295 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
296 above under #ifdef MIPS. But we want the sgi value. */
298 # define FSCALE 1000.0
301 # if defined (ardent) && defined (titan)
302 # define FSCALE 65536.0
306 # define FSCALE 100.0
310 # define FSCALE 65536.0
313 # endif /* Not FSCALE. */
315 # if !defined (LDAV_CVT) && defined (FSCALE)
316 # define LDAV_CVT(n) (((double) (n)) / FSCALE)
320 # if defined(sgi) || (defined(mips) && !defined(BSD))
321 # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
325 # if !defined (KERNEL_FILE) && defined (sequent)
326 # define KERNEL_FILE "/dynix"
329 # if !defined (KERNEL_FILE) && defined (hpux)
330 # define KERNEL_FILE "/hp-ux"
333 # if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
334 # define KERNEL_FILE "/unix"
338 # if !defined (LDAV_SYMBOL) && defined (alliant)
339 # define LDAV_SYMBOL "_Loadavg"
342 # if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
343 # define LDAV_SYMBOL "avenrun"
346 # ifdef HAVE_UNISTD_H
352 /* LOAD_AVE_TYPE should only get defined if we're going to use the
354 # if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL)) && !defined(__riscos__)
355 # define LOAD_AVE_TYPE double
358 # ifdef LOAD_AVE_TYPE
374 # if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
375 # include <sys/pstat.h>
379 # define KERNEL_FILE "/vmunix"
380 # endif /* KERNEL_FILE */
383 # define LDAV_SYMBOL "_avenrun"
384 # endif /* LDAV_SYMBOL */
385 # endif /* __linux__ */
391 # include <descrip.h>
393 # include <vms/iodef.h>
398 # define LDAV_CVT(n) ((double) (n))
399 # endif /* !LDAV_CVT */
401 # endif /* LOAD_AVE_TYPE */
403 # if defined(__GNU__) && !defined (NeXT)
404 /* Note that NeXT Openstep defines __GNU__ even though it should not. */
405 /* GNU system acts much like NeXT, for load average purposes,
408 # define host_self mach_host_self
412 # ifdef HAVE_MACH_MACH_H
413 # include <mach/mach.h>
420 # include <sys/sysmp.h>
426 # include <sys/time.h>
427 # include <sys/wait.h>
428 # include <sys/syscall.h>
431 # include <machine/cpu.h>
432 # include <inq_stats/statistics.h>
433 # include <inq_stats/sysstats.h>
434 # include <inq_stats/cpustats.h>
435 # include <inq_stats/procstats.h>
436 # else /* Not UMAX_43. */
437 # include <sys/sysdefs.h>
438 # include <sys/statistics.h>
439 # include <sys/sysstats.h>
440 # include <sys/cpudefs.h>
441 # include <sys/cpustats.h>
442 # include <sys/procstats.h>
443 # endif /* Not UMAX_43. */
447 # include <sys/dg_sys_info.h>
450 # if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
453 # include <sys/file.h>
457 /* Avoid static vars inside a function since in HPUX they dump as pure. */
460 static processor_set_t default_set
;
461 static int getloadavg_initialized
;
465 static unsigned int cpus
= 0;
466 static unsigned int samples
;
470 static struct dg_sys_info_load_info load_info
; /* what-a-mouthful! */
473 #if !defined(HAVE_LIBKSTAT) && defined(LOAD_AVE_TYPE)
474 /* File descriptor open to /dev/kmem or VMS load ave driver. */
476 /* Nonzero iff channel is valid. */
477 static int getloadavg_initialized
;
478 /* Offset in kmem to seek to read load average, or 0 means invalid. */
481 #if !defined(VMS) && !defined(sgi) && !defined(__linux__)
482 static struct nlist nl
[2];
483 #endif /* Not VMS or sgi */
489 #endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */
491 /* Put the 1 minute, 5 minute and 15 minute load averages
492 into the first NELEM elements of LOADAVG.
493 Return the number written (never more than 3, but may be less than NELEM),
494 or -1 if an error occurred. */
497 getloadavg (double loadavg
[], int nelem
)
499 int elem
= 0; /* Return value. */
501 # ifdef NO_GET_LOAD_AVG
503 /* Set errno to zero to indicate that there was no particular error;
504 this function just can't work at all on this system. */
509 # if !defined (LDAV_DONE) && defined (HAVE_LIBKSTAT)
510 /* Use libkstat because we don't have to be root. */
519 ksp
= kstat_lookup (kc
, "unix", 0, "system_misc");
522 if (kstat_read (kc
, ksp
, 0) == -1)
526 kn
= kstat_data_lookup (ksp
, "avenrun_1min");
529 /* Return -1 if no load average information is available. */
535 loadavg
[elem
++] = (double) kn
->value
.ul
/FSCALE
;
539 kn
= kstat_data_lookup (ksp
, "avenrun_5min");
542 loadavg
[elem
++] = (double) kn
->value
.ul
/FSCALE
;
546 kn
= kstat_data_lookup (ksp
, "avenrun_15min");
548 loadavg
[elem
++] = (double) kn
->value
.ul
/FSCALE
;
554 # endif /* HAVE_LIBKSTAT */
556 # if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
557 /* Use pstat_getdynamic() because we don't have to be root. */
559 # undef LOAD_AVE_TYPE
561 struct pst_dynamic dyn_info
;
562 if (pstat_getdynamic (&dyn_info
, sizeof (dyn_info
), 0, 0) < 0)
565 loadavg
[elem
++] = dyn_info
.psd_avg_1_min
;
567 loadavg
[elem
++] = dyn_info
.psd_avg_5_min
;
569 loadavg
[elem
++] = dyn_info
.psd_avg_15_min
;
571 # endif /* hpux && HAVE_PSTAT_GETDYNAMIC */
573 # if !defined (LDAV_DONE) && defined (__linux__)
575 # undef LOAD_AVE_TYPE
577 # ifndef LINUX_LDAV_FILE
578 # define LINUX_LDAV_FILE "/proc/loadavg"
585 fd
= open (LINUX_LDAV_FILE
, O_RDONLY
);
588 count
= read (fd
, ldavgbuf
, 40);
593 /* The following sscanf must use the C locale. */
594 setlocale (LC_NUMERIC
, "C");
595 count
= sscanf (ldavgbuf
, "%lf %lf %lf",
596 &load_ave
[0], &load_ave
[1], &load_ave
[2]);
597 setlocale (LC_NUMERIC
, "");
601 for (elem
= 0; elem
< nelem
&& elem
< count
; elem
++)
602 loadavg
[elem
] = load_ave
[elem
];
606 # endif /* __linux__ */
608 # if !defined (LDAV_DONE) && defined (__NetBSD__)
610 # undef LOAD_AVE_TYPE
612 # ifndef NETBSD_LDAV_FILE
613 # define NETBSD_LDAV_FILE "/kern/loadavg"
616 unsigned long int load_ave
[3], scale
;
620 fp
= fopen (NETBSD_LDAV_FILE
, "r");
623 count
= fscanf (fp
, "%lu %lu %lu %lu\n",
624 &load_ave
[0], &load_ave
[1], &load_ave
[2],
630 for (elem
= 0; elem
< nelem
; elem
++)
631 loadavg
[elem
] = (double) load_ave
[elem
] / (double) scale
;
635 # endif /* __NetBSD__ */
637 # if !defined (LDAV_DONE) && defined (NeXT)
639 /* The NeXT code was adapted from iscreen 3.2. */
642 struct processor_set_basic_info info
;
645 /* We only know how to get the 1-minute average for this system,
646 so even if the caller asks for more than 1, we only return 1. */
648 if (!getloadavg_initialized
)
650 if (processor_set_default (host_self (), &default_set
) == KERN_SUCCESS
)
651 getloadavg_initialized
= 1;
654 if (getloadavg_initialized
)
656 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
657 if (processor_set_info (default_set
, PROCESSOR_SET_BASIC_INFO
, &host
,
658 (processor_set_info_t
) &info
, &info_count
)
660 getloadavg_initialized
= 0;
664 loadavg
[elem
++] = (double) info
.load_average
/ LOAD_SCALE
;
668 if (!getloadavg_initialized
)
672 # if !defined (LDAV_DONE) && defined (UMAX)
674 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
675 have a /dev/kmem. Information about the workings of the running kernel
676 can be gathered with inq_stats system calls.
677 We only know how to get the 1-minute average for this system. */
679 struct proc_summary proc_sum_data
;
680 struct stat_descr proc_info
;
682 register unsigned int i
, j
;
686 register unsigned int c
, i
;
687 struct cpu_config conf
;
688 struct stat_descr desc
;
691 desc
.sd_subsys
= SUBSYS_CPU
;
692 desc
.sd_type
= CPUTYPE_CONFIG
;
693 desc
.sd_addr
= (char *) &conf
;
694 desc
.sd_size
= sizeof conf
;
696 if (inq_stats (1, &desc
))
700 for (i
= 0; i
< conf
.config_maxclass
; ++i
)
702 struct class_stats stats
;
703 memset (&stats
, '\0', sizeof stats
);
705 desc
.sd_type
= CPUTYPE_CLASS
;
707 desc
.sd_addr
= (char *) &stats
;
708 desc
.sd_size
= sizeof stats
;
710 if (inq_stats (1, &desc
))
713 c
+= stats
.class_numcpus
;
716 samples
= cpus
< 2 ? 3 : (2 * cpus
/ 3);
719 proc_info
.sd_next
= 0;
720 proc_info
.sd_subsys
= SUBSYS_PROC
;
721 proc_info
.sd_type
= PROCTYPE_SUMMARY
;
722 proc_info
.sd_addr
= (char *) &proc_sum_data
;
723 proc_info
.sd_size
= sizeof (struct proc_summary
);
724 proc_info
.sd_sizeused
= 0;
726 if (inq_stats (1, &proc_info
) != 0)
729 load
= proc_sum_data
.ps_nrunnable
;
731 for (i
= samples
- 1; i
> 0; --i
)
733 load
+= proc_sum_data
.ps_nrun
[j
];
734 if (j
++ == PS_NRUNSIZE
)
739 loadavg
[elem
++] = load
/ samples
/ cpus
;
742 # if !defined (LDAV_DONE) && defined (DGUX)
744 /* This call can return -1 for an error, but with good args
745 it's not supposed to fail. The first argument is for no
746 apparent reason of type 'long int *'. */
747 dg_sys_info ((long int *) &load_info
,
748 DG_SYS_INFO_LOAD_INFO_TYPE
,
749 DG_SYS_INFO_LOAD_VERSION_0
);
752 loadavg
[elem
++] = load_info
.one_minute
;
754 loadavg
[elem
++] = load_info
.five_minute
;
756 loadavg
[elem
++] = load_info
.fifteen_minute
;
759 # if !defined (LDAV_DONE) && defined (apollo)
761 /* Apollo code from lisch@mentorg.com (Ray Lischner).
763 This system call is not documented. The load average is obtained as
764 three long integers, for the load average over the past minute,
765 five minutes, and fifteen minutes. Each value is a scaled integer,
766 with 16 bits of integer part and 16 bits of fraction part.
768 I'm not sure which operating system first supported this system call,
769 but I know that SR10.2 supports it. */
771 extern void proc1_$
get_loadav ();
772 unsigned long load_ave
[3];
774 proc1_$
get_loadav (load_ave
);
777 loadavg
[elem
++] = load_ave
[0] / 65536.0;
779 loadavg
[elem
++] = load_ave
[1] / 65536.0;
781 loadavg
[elem
++] = load_ave
[2] / 65536.0;
784 # if !defined (LDAV_DONE) && defined (OSF_MIPS)
787 struct tbl_loadavg load_ave
;
788 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
790 = (load_ave
.tl_lscale
== 0
791 ? load_ave
.tl_avenrun
.d
[0]
792 : (load_ave
.tl_avenrun
.l
[0] / (double) load_ave
.tl_lscale
));
793 # endif /* OSF_MIPS */
795 # if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
798 /* A faithful emulation is going to have to be saved for a rainy day. */
799 for ( ; elem
< nelem
; elem
++)
803 # endif /* __MSDOS__ || WINDOWS32 */
805 # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
808 struct tbl_loadavg load_ave
;
809 table (TBL_LOADAVG
, 0, &load_ave
, 1, sizeof (load_ave
));
810 for (elem
= 0; elem
< nelem
; elem
++)
812 = (load_ave
.tl_lscale
== 0
813 ? load_ave
.tl_avenrun
.d
[elem
]
814 : (load_ave
.tl_avenrun
.l
[elem
] / (double) load_ave
.tl_lscale
));
815 # endif /* OSF_ALPHA */
817 # if !defined (LDAV_DONE) && defined (VMS)
818 /* VMS specific code -- read from the Load Ave driver. */
820 LOAD_AVE_TYPE load_ave
[3];
821 static int getloadavg_initialized
= 0;
830 /* Ensure that there is a channel open to the load ave device. */
831 if (!getloadavg_initialized
)
833 /* Attempt to open the channel. */
835 descriptor
.dsc$w_length
= 18;
836 descriptor
.dsc$a_pointer
= "$$VMS_LOAD_AVERAGE";
838 $
DESCRIPTOR (descriptor
, "LAV0:");
840 if (sys$
assign (&descriptor
, &channel
, 0, 0) & 1)
841 getloadavg_initialized
= 1;
844 /* Read the load average vector. */
845 if (getloadavg_initialized
846 && !(sys$
qiow (0, channel
, IO$_READVBLK
, 0, 0, 0,
847 load_ave
, 12, 0, 0, 0, 0) & 1))
849 sys$
dassgn (channel
);
850 getloadavg_initialized
= 0;
853 if (!getloadavg_initialized
)
857 # if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
859 /* UNIX-specific code -- read the average from /dev/kmem. */
861 # define LDAV_PRIVILEGED /* This code requires special installation. */
863 LOAD_AVE_TYPE load_ave
[3];
865 /* Get the address of LDAV_SYMBOL. */
869 # ifndef NLIST_STRUCT
870 strcpy (nl
[0].n_name
, LDAV_SYMBOL
);
871 strcpy (nl
[1].n_name
, "");
872 # else /* NLIST_STRUCT */
873 # ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
874 nl
[0].n_un
.n_name
= LDAV_SYMBOL
;
875 nl
[1].n_un
.n_name
= 0;
876 # else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
877 nl
[0].n_name
= LDAV_SYMBOL
;
879 # endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
880 # endif /* NLIST_STRUCT */
884 # if !(defined (_AIX) && !defined (ps2))
885 nlist (KERNEL_FILE
, nl
)
887 knlist (nl
, 1, sizeof (nl
[0]))
890 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
892 # ifdef FIXUP_KERNEL_SYMBOL_ADDR
893 FIXUP_KERNEL_SYMBOL_ADDR (nl
);
895 offset
= nl
[0].n_value
;
897 # endif /* !SUNOS_5 */
901 ldav_off
= sysmp (MP_KERNADDR
, MPKA_AVENRUN
);
903 offset
= (long) ldav_off
& 0x7fffffff;
907 /* Make sure we have /dev/kmem open. */
908 if (!getloadavg_initialized
)
911 channel
= open ("/dev/kmem", 0);
914 /* Set the channel to close on exec, so it does not
915 litter any child's descriptor table. */
918 # define FD_CLOEXEC 1
920 (void) fcntl (channel
, F_SETFD
, FD_CLOEXEC
);
922 getloadavg_initialized
= 1;
925 /* We pass 0 for the kernel, corefile, and swapfile names
926 to use the currently running kernel. */
927 kd
= kvm_open (0, 0, 0, O_RDONLY
, 0);
930 /* nlist the currently running kernel. */
932 offset
= nl
[0].n_value
;
933 getloadavg_initialized
= 1;
935 # endif /* SUNOS_5 */
938 /* If we can, get the load average values. */
939 if (offset
&& getloadavg_initialized
)
941 /* Try to read the load. */
943 if (lseek (channel
, offset
, 0) == -1L
944 || read (channel
, (char *) load_ave
, sizeof (load_ave
))
945 != sizeof (load_ave
))
948 getloadavg_initialized
= 0;
951 if (kvm_read (kd
, offset
, (char *) load_ave
, sizeof (load_ave
))
952 != sizeof (load_ave
))
955 getloadavg_initialized
= 0;
957 # endif /* SUNOS_5 */
960 if (offset
== 0 || !getloadavg_initialized
)
962 # endif /* LOAD_AVE_TYPE and not VMS */
964 # if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
966 loadavg
[elem
++] = LDAV_CVT (load_ave
[0]);
968 loadavg
[elem
++] = LDAV_CVT (load_ave
[1]);
970 loadavg
[elem
++] = LDAV_CVT (load_ave
[2]);
973 # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
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. */
985 #endif /* ! HAVE_GETLOADAVG */
991 main (int argc
, char **argv
)
996 naptime
= atoi (argv
[1]);
1003 errno
= 0; /* Don't be misled if it doesn't set errno. */
1004 loads
= getloadavg (avg
, 3);
1007 perror ("Error getting load average");
1011 printf ("1-minute: %f ", avg
[0]);
1013 printf ("5-minute: %f ", avg
[1]);
1015 printf ("15-minute: %f ", avg
[2]);